blob: 1e5a151e7127c886436256e8e84d18eb90c9aa91 [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"},
943 {"load_module", LoaderLoadModule, METH_VARARGS, "Internal use only, tries importing the given module from &rtp by temporary mocking sys.path (to an rtp-based one) and unsetting sys.meta_path and sys.path_hooks"},
944#endif
945 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
946 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
947 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200948};
949
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200950/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200951 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200952 */
953
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200954static PyTypeObject IterType;
955
956typedef PyObject *(*nextfun)(void **);
957typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200958typedef int (*traversefun)(void *, visitproc, void *);
959typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200960
Bram Moolenaar9e74e302013-05-17 21:20:17 +0200961/* Main purpose of this object is removing the need for do python
962 * initialization (i.e. PyType_Ready and setting type attributes) for a big
963 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200964
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200965typedef struct
966{
967 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200968 void *cur;
969 nextfun next;
970 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200971 traversefun traverse;
972 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200973} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200974
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200975 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200976IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
977 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200978{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200979 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200980
Bram Moolenaar774267b2013-05-21 20:51:59 +0200981 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200982 self->cur = start;
983 self->next = next;
984 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200985 self->traverse = traverse;
986 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200987
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200988 return (PyObject *)(self);
989}
990
991 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +0200992IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200993{
Bram Moolenaar774267b2013-05-21 20:51:59 +0200994 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +0200995 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200996 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200997}
998
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +0200999 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001000IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001001{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001002 if (self->traverse != NULL)
1003 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001004 else
1005 return 0;
1006}
1007
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001008/* Mac OSX defines clear() somewhere. */
1009#ifdef clear
1010# undef clear
1011#endif
1012
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001013 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001014IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001015{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001016 if (self->clear != NULL)
1017 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001018 else
1019 return 0;
1020}
1021
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001022 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001023IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001024{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001025 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001026}
1027
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001028 static PyObject *
1029IterIter(PyObject *self)
1030{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001031 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001032 return self;
1033}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001034
Bram Moolenaardb913952012-06-29 12:54:53 +02001035typedef struct pylinkedlist_S {
1036 struct pylinkedlist_S *pll_next;
1037 struct pylinkedlist_S *pll_prev;
1038 PyObject *pll_obj;
1039} pylinkedlist_T;
1040
1041static pylinkedlist_T *lastdict = NULL;
1042static pylinkedlist_T *lastlist = NULL;
1043
1044 static void
1045pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1046{
1047 if (ref->pll_prev == NULL)
1048 {
1049 if (ref->pll_next == NULL)
1050 {
1051 *last = NULL;
1052 return;
1053 }
1054 }
1055 else
1056 ref->pll_prev->pll_next = ref->pll_next;
1057
1058 if (ref->pll_next == NULL)
1059 *last = ref->pll_prev;
1060 else
1061 ref->pll_next->pll_prev = ref->pll_prev;
1062}
1063
1064 static void
1065pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1066{
1067 if (*last == NULL)
1068 ref->pll_prev = NULL;
1069 else
1070 {
1071 (*last)->pll_next = ref;
1072 ref->pll_prev = *last;
1073 }
1074 ref->pll_next = NULL;
1075 ref->pll_obj = self;
1076 *last = ref;
1077}
1078
1079static PyTypeObject DictionaryType;
1080
1081typedef struct
1082{
1083 PyObject_HEAD
1084 dict_T *dict;
1085 pylinkedlist_T ref;
1086} DictionaryObject;
1087
Bram Moolenaara9922d62013-05-30 13:01:18 +02001088static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1089
1090#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1091
Bram Moolenaardb913952012-06-29 12:54:53 +02001092 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001093DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001094{
1095 DictionaryObject *self;
1096
Bram Moolenaara9922d62013-05-30 13:01:18 +02001097 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001098 if (self == NULL)
1099 return NULL;
1100 self->dict = dict;
1101 ++dict->dv_refcount;
1102
1103 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1104
1105 return (PyObject *)(self);
1106}
1107
Bram Moolenaara9922d62013-05-30 13:01:18 +02001108 static dict_T *
1109py_dict_alloc()
1110{
1111 dict_T *r;
1112
1113 if (!(r = dict_alloc()))
1114 {
1115 PyErr_NoMemory();
1116 return NULL;
1117 }
1118 ++r->dv_refcount;
1119
1120 return r;
1121}
1122
1123 static PyObject *
1124DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1125{
1126 DictionaryObject *self;
1127 dict_T *dict;
1128
1129 if (!(dict = py_dict_alloc()))
1130 return NULL;
1131
1132 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1133
1134 --dict->dv_refcount;
1135
1136 if (kwargs || PyTuple_Size(args))
1137 {
1138 PyObject *tmp;
1139 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1140 {
1141 Py_DECREF(self);
1142 return NULL;
1143 }
1144
1145 Py_DECREF(tmp);
1146 }
1147
1148 return (PyObject *)(self);
1149}
1150
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001151 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001152DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001153{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001154 pyll_remove(&self->ref, &lastdict);
1155 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001156
1157 DESTRUCTOR_FINISH(self);
1158}
1159
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001160static char *DictionaryAttrs[] = {
1161 "locked", "scope",
1162 NULL
1163};
1164
1165 static PyObject *
1166DictionaryDir(PyObject *self)
1167{
1168 return ObjectDir(self, DictionaryAttrs);
1169}
1170
Bram Moolenaardb913952012-06-29 12:54:53 +02001171 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001172DictionarySetattr(DictionaryObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001173{
1174 if (val == NULL)
1175 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001176 PyErr_SetString(PyExc_AttributeError,
1177 _("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001178 return -1;
1179 }
1180
1181 if (strcmp(name, "locked") == 0)
1182 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001183 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001184 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001185 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001186 return -1;
1187 }
1188 else
1189 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02001190 int istrue = PyObject_IsTrue(val);
1191 if (istrue == -1)
1192 return -1;
1193 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001194 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001195 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001196 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001197 }
1198 return 0;
1199 }
1200 else
1201 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001202 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001203 return -1;
1204 }
1205}
1206
1207 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001208DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001209{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001210 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001211}
1212
Bram Moolenaara9922d62013-05-30 13:01:18 +02001213#define DICT_FLAG_HAS_DEFAULT 0x01
1214#define DICT_FLAG_POP 0x02
1215#define DICT_FLAG_NONE_DEFAULT 0x04
1216#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1217#define DICT_FLAG_RETURN_PAIR 0x10
1218
Bram Moolenaardb913952012-06-29 12:54:53 +02001219 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001220_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001221{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001222 PyObject *keyObject;
1223 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
1224 PyObject *r;
Bram Moolenaardb913952012-06-29 12:54:53 +02001225 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001226 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001227 dict_T *dict = self->dict;
1228 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001229 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001230
Bram Moolenaara9922d62013-05-30 13:01:18 +02001231 if (flags & DICT_FLAG_HAS_DEFAULT)
1232 {
1233 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1234 return NULL;
1235 }
1236 else
1237 keyObject = args;
1238
1239 if (flags & DICT_FLAG_RETURN_BOOL)
1240 defObject = Py_False;
1241
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001242 if (!(key = StringToChars(keyObject, &todecref)))
1243 return NULL;
1244
1245 if (*key == NUL)
1246 {
1247 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001248 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001249 return NULL;
1250 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001251
Bram Moolenaara9922d62013-05-30 13:01:18 +02001252 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001253
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001254 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001255
Bram Moolenaara9922d62013-05-30 13:01:18 +02001256 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001257 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001258 if (defObject)
1259 {
1260 Py_INCREF(defObject);
1261 return defObject;
1262 }
1263 else
1264 {
1265 PyErr_SetObject(PyExc_KeyError, keyObject);
1266 return NULL;
1267 }
1268 }
1269 else if (flags & DICT_FLAG_RETURN_BOOL)
1270 {
1271 Py_INCREF(Py_True);
1272 return Py_True;
1273 }
1274
1275 di = dict_lookup(hi);
1276
1277 if (!(r = ConvertToPyObject(&di->di_tv)))
1278 return NULL;
1279
1280 if (flags & DICT_FLAG_POP)
1281 {
1282 if (dict->dv_lock)
1283 {
1284 PyErr_SetVim(_("dict is locked"));
1285 Py_DECREF(r);
1286 return NULL;
1287 }
1288
1289 hash_remove(&dict->dv_hashtab, hi);
1290 dictitem_free(di);
1291 }
1292
Bram Moolenaara9922d62013-05-30 13:01:18 +02001293 return r;
1294}
1295
1296 static PyObject *
1297DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1298{
1299 return _DictionaryItem(self, keyObject, 0);
1300}
1301
1302 static int
1303DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1304{
1305 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1306 int r;
1307
1308 r = (rObj == Py_True);
1309
1310 Py_DECREF(Py_True);
1311
1312 return r;
1313}
1314
1315typedef struct
1316{
1317 hashitem_T *ht_array;
1318 long_u ht_used;
1319 hashtab_T *ht;
1320 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001321 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001322} dictiterinfo_T;
1323
1324 static PyObject *
1325DictionaryIterNext(dictiterinfo_T **dii)
1326{
1327 PyObject *r;
1328
1329 if (!(*dii)->todo)
1330 return NULL;
1331
1332 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1333 (*dii)->ht->ht_used != (*dii)->ht_used)
1334 {
1335 PyErr_SetString(PyExc_RuntimeError,
1336 _("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001337 return NULL;
1338 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001339
Bram Moolenaara9922d62013-05-30 13:01:18 +02001340 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1341 ++((*dii)->hi);
1342
1343 --((*dii)->todo);
1344
1345 if (!(r = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
1346 return NULL;
1347
1348 return r;
1349}
1350
1351 static PyObject *
1352DictionaryIter(DictionaryObject *self)
1353{
1354 dictiterinfo_T *dii;
1355 hashtab_T *ht;
1356
1357 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1358 {
1359 PyErr_NoMemory();
1360 return NULL;
1361 }
1362
1363 ht = &self->dict->dv_hashtab;
1364 dii->ht_array = ht->ht_array;
1365 dii->ht_used = ht->ht_used;
1366 dii->ht = ht;
1367 dii->hi = dii->ht_array;
1368 dii->todo = dii->ht_used;
1369
1370 return IterNew(dii,
1371 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1372 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001373}
1374
1375 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001376DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001377{
1378 char_u *key;
1379 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001380 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001381 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001382 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001383
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001384 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001385 {
1386 PyErr_SetVim(_("dict is locked"));
1387 return -1;
1388 }
1389
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001390 if (!(key = StringToChars(keyObject, &todecref)))
1391 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001392
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001393 if (*key == NUL)
1394 {
1395 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001396 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001397 return -1;
1398 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001399
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001400 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001401
1402 if (valObject == NULL)
1403 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001404 hashitem_T *hi;
1405
Bram Moolenaardb913952012-06-29 12:54:53 +02001406 if (di == NULL)
1407 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001408 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001409 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001410 return -1;
1411 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001412 hi = hash_find(&dict->dv_hashtab, di->di_key);
1413 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001414 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001415 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001416 return 0;
1417 }
1418
1419 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001420 {
1421 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001422 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001423 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001424
1425 if (di == NULL)
1426 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001427 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001428 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001429 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001430 PyErr_NoMemory();
1431 return -1;
1432 }
1433 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001434 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001435
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001436 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001437 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001438 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001439 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001440 dictitem_free(di);
Bram Moolenaardb913952012-06-29 12:54:53 +02001441 PyErr_SetVim(_("failed to add key to dictionary"));
1442 return -1;
1443 }
1444 }
1445 else
1446 clear_tv(&di->di_tv);
1447
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001448 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001449
1450 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001451 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001452 return 0;
1453}
1454
Bram Moolenaara9922d62013-05-30 13:01:18 +02001455typedef PyObject *(*hi_to_py)(hashitem_T *);
1456
Bram Moolenaardb913952012-06-29 12:54:53 +02001457 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001458DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001459{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001460 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001461 long_u todo = dict->dv_hashtab.ht_used;
1462 Py_ssize_t i = 0;
1463 PyObject *r;
1464 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001465 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001466
1467 r = PyList_New(todo);
1468 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1469 {
1470 if (!HASHITEM_EMPTY(hi))
1471 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001472 if (!(newObj = hiconvert(hi)))
1473 {
1474 Py_DECREF(r);
1475 return NULL;
1476 }
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001477 PyList_SET_ITEM(r, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001478 --todo;
1479 ++i;
1480 }
1481 }
1482 return r;
1483}
1484
Bram Moolenaara9922d62013-05-30 13:01:18 +02001485 static PyObject *
1486dict_key(hashitem_T *hi)
1487{
1488 return PyBytes_FromString((char *)(hi->hi_key));
1489}
1490
1491 static PyObject *
1492DictionaryListKeys(DictionaryObject *self)
1493{
1494 return DictionaryListObjects(self, dict_key);
1495}
1496
1497 static PyObject *
1498dict_val(hashitem_T *hi)
1499{
1500 dictitem_T *di;
1501
1502 di = dict_lookup(hi);
1503 return ConvertToPyObject(&di->di_tv);
1504}
1505
1506 static PyObject *
1507DictionaryListValues(DictionaryObject *self)
1508{
1509 return DictionaryListObjects(self, dict_val);
1510}
1511
1512 static PyObject *
1513dict_item(hashitem_T *hi)
1514{
1515 PyObject *keyObject;
1516 PyObject *valObject;
1517 PyObject *r;
1518
1519 if (!(keyObject = dict_key(hi)))
1520 return NULL;
1521
1522 if (!(valObject = dict_val(hi)))
1523 {
1524 Py_DECREF(keyObject);
1525 return NULL;
1526 }
1527
1528 r = Py_BuildValue("(OO)", keyObject, valObject);
1529
1530 Py_DECREF(keyObject);
1531 Py_DECREF(valObject);
1532
1533 return r;
1534}
1535
1536 static PyObject *
1537DictionaryListItems(DictionaryObject *self)
1538{
1539 return DictionaryListObjects(self, dict_item);
1540}
1541
1542 static PyObject *
1543DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1544{
1545 dict_T *dict = self->dict;
1546
1547 if (dict->dv_lock)
1548 {
1549 PyErr_SetVim(_("dict is locked"));
1550 return NULL;
1551 }
1552
1553 if (kwargs)
1554 {
1555 typval_T tv;
1556
1557 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1558 return NULL;
1559
1560 VimTryStart();
1561 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1562 clear_tv(&tv);
1563 if (VimTryEnd())
1564 return NULL;
1565 }
1566 else
1567 {
1568 PyObject *object;
1569
1570 if (!PyArg_Parse(args, "(O)", &object))
1571 return NULL;
1572
1573 if (PyObject_HasAttrString(object, "keys"))
1574 return DictionaryUpdate(self, NULL, object);
1575 else
1576 {
1577 PyObject *iterator;
1578 PyObject *item;
1579
1580 if (!(iterator = PyObject_GetIter(object)))
1581 return NULL;
1582
1583 while ((item = PyIter_Next(iterator)))
1584 {
1585 PyObject *fast;
1586 PyObject *keyObject;
1587 PyObject *valObject;
1588 PyObject *todecref;
1589 char_u *key;
1590 dictitem_T *di;
1591
1592 if (!(fast = PySequence_Fast(item, "")))
1593 {
1594 Py_DECREF(iterator);
1595 Py_DECREF(item);
1596 return NULL;
1597 }
1598
1599 Py_DECREF(item);
1600
1601 if (PySequence_Fast_GET_SIZE(fast) != 2)
1602 {
1603 Py_DECREF(iterator);
1604 Py_DECREF(fast);
1605 PyErr_SetString(PyExc_ValueError,
1606 _("expected sequence element of size 2"));
1607 return NULL;
1608 }
1609
1610 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1611
1612 if (!(key = StringToChars(keyObject, &todecref)))
1613 {
1614 Py_DECREF(iterator);
1615 Py_DECREF(fast);
1616 return NULL;
1617 }
1618
1619 di = dictitem_alloc(key);
1620
1621 Py_XDECREF(todecref);
1622
1623 if (di == NULL)
1624 {
1625 Py_DECREF(fast);
1626 Py_DECREF(iterator);
1627 PyErr_NoMemory();
1628 return NULL;
1629 }
1630 di->di_tv.v_lock = 0;
1631 di->di_tv.v_type = VAR_UNKNOWN;
1632
1633 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1634
1635 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1636 {
1637 Py_DECREF(iterator);
1638 Py_DECREF(fast);
1639 dictitem_free(di);
1640 return NULL;
1641 }
1642
1643 Py_DECREF(fast);
1644
1645 if (dict_add(dict, di) == FAIL)
1646 {
1647 Py_DECREF(iterator);
1648 dictitem_free(di);
1649 PyErr_SetVim(_("failed to add key to dictionary"));
1650 return NULL;
1651 }
1652 }
1653
1654 Py_DECREF(iterator);
1655
1656 /* Iterator may have finished due to an exception */
1657 if (PyErr_Occurred())
1658 return NULL;
1659 }
1660 }
1661 Py_INCREF(Py_None);
1662 return Py_None;
1663}
1664
1665 static PyObject *
1666DictionaryGet(DictionaryObject *self, PyObject *args)
1667{
1668 return _DictionaryItem(self, args,
1669 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1670}
1671
1672 static PyObject *
1673DictionaryPop(DictionaryObject *self, PyObject *args)
1674{
1675 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1676}
1677
1678 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001679DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001680{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001681 hashitem_T *hi;
1682 PyObject *r;
1683 PyObject *valObject;
1684 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001685
Bram Moolenaarde71b562013-06-02 17:41:54 +02001686 if (self->dict->dv_hashtab.ht_used == 0)
1687 {
1688 PyErr_SetNone(PyExc_KeyError);
1689 return NULL;
1690 }
1691
1692 hi = self->dict->dv_hashtab.ht_array;
1693 while (HASHITEM_EMPTY(hi))
1694 ++hi;
1695
1696 di = dict_lookup(hi);
1697
1698 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001699 return NULL;
1700
Bram Moolenaarde71b562013-06-02 17:41:54 +02001701 if (!(r = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
1702 {
1703 Py_DECREF(valObject);
1704 return NULL;
1705 }
1706
1707 hash_remove(&self->dict->dv_hashtab, hi);
1708 dictitem_free(di);
1709
1710 return r;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001711}
1712
1713 static PyObject *
1714DictionaryHasKey(DictionaryObject *self, PyObject *args)
1715{
1716 PyObject *keyObject;
1717
1718 if (!PyArg_ParseTuple(args, "O", &keyObject))
1719 return NULL;
1720
1721 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
1722}
1723
1724static PySequenceMethods DictionaryAsSeq = {
1725 0, /* sq_length */
1726 0, /* sq_concat */
1727 0, /* sq_repeat */
1728 0, /* sq_item */
1729 0, /* sq_slice */
1730 0, /* sq_ass_item */
1731 0, /* sq_ass_slice */
1732 (objobjproc) DictionaryContains, /* sq_contains */
1733 0, /* sq_inplace_concat */
1734 0, /* sq_inplace_repeat */
1735};
1736
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001737static PyMappingMethods DictionaryAsMapping = {
1738 (lenfunc) DictionaryLength,
1739 (binaryfunc) DictionaryItem,
1740 (objobjargproc) DictionaryAssItem,
1741};
1742
Bram Moolenaardb913952012-06-29 12:54:53 +02001743static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02001744 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001745 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
1746 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
1747 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
1748 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
1749 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02001750 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02001751 {"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001752 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
1753 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02001754};
1755
1756static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001757static PySequenceMethods ListAsSeq;
1758static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02001759
1760typedef struct
1761{
1762 PyObject_HEAD
1763 list_T *list;
1764 pylinkedlist_T ref;
1765} ListObject;
1766
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001767#define NEW_LIST(list) ListNew(&ListType, list)
1768
Bram Moolenaardb913952012-06-29 12:54:53 +02001769 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001770ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02001771{
1772 ListObject *self;
1773
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001774 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001775 if (self == NULL)
1776 return NULL;
1777 self->list = list;
1778 ++list->lv_refcount;
1779
1780 pyll_add((PyObject *)(self), &self->ref, &lastlist);
1781
1782 return (PyObject *)(self);
1783}
1784
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02001785 static list_T *
1786py_list_alloc()
1787{
1788 list_T *r;
1789
1790 if (!(r = list_alloc()))
1791 {
1792 PyErr_NoMemory();
1793 return NULL;
1794 }
1795 ++r->lv_refcount;
1796
1797 return r;
1798}
1799
1800 static int
1801list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
1802{
1803 PyObject *iterator;
1804 PyObject *item;
1805 listitem_T *li;
1806
1807 if (!(iterator = PyObject_GetIter(obj)))
1808 return -1;
1809
1810 while ((item = PyIter_Next(iterator)))
1811 {
1812 if (!(li = listitem_alloc()))
1813 {
1814 PyErr_NoMemory();
1815 Py_DECREF(item);
1816 Py_DECREF(iterator);
1817 return -1;
1818 }
1819 li->li_tv.v_lock = 0;
1820 li->li_tv.v_type = VAR_UNKNOWN;
1821
1822 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
1823 {
1824 Py_DECREF(item);
1825 Py_DECREF(iterator);
1826 listitem_free(li);
1827 return -1;
1828 }
1829
1830 Py_DECREF(item);
1831
1832 list_append(l, li);
1833 }
1834
1835 Py_DECREF(iterator);
1836
1837 /* Iterator may have finished due to an exception */
1838 if (PyErr_Occurred())
1839 return -1;
1840
1841 return 0;
1842}
1843
1844 static PyObject *
1845ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1846{
1847 list_T *list;
1848 PyObject *obj = NULL;
1849
1850 if (kwargs)
1851 {
1852 PyErr_SetString(PyExc_TypeError,
1853 _("list constructor does not accept keyword arguments"));
1854 return NULL;
1855 }
1856
1857 if (!PyArg_ParseTuple(args, "|O", &obj))
1858 return NULL;
1859
1860 if (!(list = py_list_alloc()))
1861 return NULL;
1862
1863 if (obj)
1864 {
1865 PyObject *lookup_dict;
1866
1867 if (!(lookup_dict = PyDict_New()))
1868 {
1869 list_unref(list);
1870 return NULL;
1871 }
1872
1873 if (list_py_concat(list, obj, lookup_dict) == -1)
1874 {
1875 Py_DECREF(lookup_dict);
1876 list_unref(list);
1877 return NULL;
1878 }
1879
1880 Py_DECREF(lookup_dict);
1881 }
1882
1883 return ListNew(subtype, list);
1884}
1885
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001886 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001887ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001888{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001889 pyll_remove(&self->ref, &lastlist);
1890 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001891
1892 DESTRUCTOR_FINISH(self);
1893}
1894
Bram Moolenaardb913952012-06-29 12:54:53 +02001895 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001896ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001897{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001898 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02001899}
1900
1901 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001902ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02001903{
1904 listitem_T *li;
1905
Bram Moolenaard6e39182013-05-21 18:30:34 +02001906 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001908 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001909 return NULL;
1910 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02001911 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02001912 if (li == NULL)
1913 {
1914 PyErr_SetVim(_("internal error: failed to get vim list item"));
1915 return NULL;
1916 }
1917 return ConvertToPyObject(&li->li_tv);
1918}
1919
1920#define PROC_RANGE \
1921 if (last < 0) {\
1922 if (last < -size) \
1923 last = 0; \
1924 else \
1925 last += size; \
1926 } \
1927 if (first < 0) \
1928 first = 0; \
1929 if (first > size) \
1930 first = size; \
1931 if (last > size) \
1932 last = size;
1933
1934 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001935ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02001936{
1937 PyInt i;
1938 PyInt size = ListLength(self);
1939 PyInt n;
1940 PyObject *list;
1941 int reversed = 0;
1942
1943 PROC_RANGE
1944 if (first >= last)
1945 first = last;
1946
1947 n = last-first;
1948 list = PyList_New(n);
1949 if (list == NULL)
1950 return NULL;
1951
1952 for (i = 0; i < n; ++i)
1953 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02001954 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02001955 if (item == NULL)
1956 {
1957 Py_DECREF(list);
1958 return NULL;
1959 }
1960
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02001961 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02001962 }
1963
1964 return list;
1965}
1966
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001967typedef struct
1968{
1969 listwatch_T lw;
1970 list_T *list;
1971} listiterinfo_T;
1972
1973 static void
1974ListIterDestruct(listiterinfo_T *lii)
1975{
1976 list_rem_watch(lii->list, &lii->lw);
1977 PyMem_Free(lii);
1978}
1979
1980 static PyObject *
1981ListIterNext(listiterinfo_T **lii)
1982{
1983 PyObject *r;
1984
1985 if (!((*lii)->lw.lw_item))
1986 return NULL;
1987
1988 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
1989 return NULL;
1990
1991 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
1992
1993 return r;
1994}
1995
1996 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001997ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001998{
1999 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002000 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002001
2002 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2003 {
2004 PyErr_NoMemory();
2005 return NULL;
2006 }
2007
2008 list_add_watch(l, &lii->lw);
2009 lii->lw.lw_item = l->lv_first;
2010 lii->list = l;
2011
2012 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002013 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2014 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002015}
2016
Bram Moolenaardb913952012-06-29 12:54:53 +02002017 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002018ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002019{
2020 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002021 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002022 listitem_T *li;
2023 Py_ssize_t length = ListLength(self);
2024
2025 if (l->lv_lock)
2026 {
2027 PyErr_SetVim(_("list is locked"));
2028 return -1;
2029 }
2030 if (index>length || (index==length && obj==NULL))
2031 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002032 PyErr_SetString(PyExc_IndexError, _("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002033 return -1;
2034 }
2035
2036 if (obj == NULL)
2037 {
2038 li = list_find(l, (long) index);
2039 list_remove(l, li, li);
2040 clear_tv(&li->li_tv);
2041 vim_free(li);
2042 return 0;
2043 }
2044
2045 if (ConvertFromPyObject(obj, &tv) == -1)
2046 return -1;
2047
2048 if (index == length)
2049 {
2050 if (list_append_tv(l, &tv) == FAIL)
2051 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002052 clear_tv(&tv);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002053 PyErr_SetVim(_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002054 return -1;
2055 }
2056 }
2057 else
2058 {
2059 li = list_find(l, (long) index);
2060 clear_tv(&li->li_tv);
2061 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002062 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002063 }
2064 return 0;
2065}
2066
2067 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002068ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002069{
2070 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002071 PyObject *iterator;
2072 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002073 listitem_T *li;
2074 listitem_T *next;
2075 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002076 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002077 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002078
2079 if (l->lv_lock)
2080 {
2081 PyErr_SetVim(_("list is locked"));
2082 return -1;
2083 }
2084
2085 PROC_RANGE
2086
2087 if (first == size)
2088 li = NULL;
2089 else
2090 {
2091 li = list_find(l, (long) first);
2092 if (li == NULL)
2093 {
2094 PyErr_SetVim(_("internal error: no vim list item"));
2095 return -1;
2096 }
2097 if (last > first)
2098 {
2099 i = last - first;
2100 while (i-- && li != NULL)
2101 {
2102 next = li->li_next;
2103 listitem_remove(l, li);
2104 li = next;
2105 }
2106 }
2107 }
2108
2109 if (obj == NULL)
2110 return 0;
2111
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002112 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002113 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002114
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002115 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002116 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002117 if (ConvertFromPyObject(item, &v) == -1)
2118 {
2119 Py_DECREF(iterator);
2120 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002121 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002122 }
2123 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002124 if (list_insert_tv(l, &v, li) == FAIL)
2125 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002126 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002127 PyErr_SetVim(_("internal error: failed to add item to list"));
2128 return -1;
2129 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002130 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002131 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002132 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02002133 return 0;
2134}
2135
2136 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002137ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002138{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002139 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002140 PyObject *lookup_dict;
2141
2142 if (l->lv_lock)
2143 {
2144 PyErr_SetVim(_("list is locked"));
2145 return NULL;
2146 }
2147
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002148 if (!(lookup_dict = PyDict_New()))
2149 return NULL;
2150
Bram Moolenaardb913952012-06-29 12:54:53 +02002151 if (list_py_concat(l, obj, lookup_dict) == -1)
2152 {
2153 Py_DECREF(lookup_dict);
2154 return NULL;
2155 }
2156 Py_DECREF(lookup_dict);
2157
2158 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002159 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002160}
2161
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002162static char *ListAttrs[] = {
2163 "locked",
2164 NULL
2165};
2166
2167 static PyObject *
2168ListDir(PyObject *self)
2169{
2170 return ObjectDir(self, ListAttrs);
2171}
2172
Bram Moolenaar66b79852012-09-21 14:00:35 +02002173 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002174ListSetattr(ListObject *self, char *name, PyObject *val)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002175{
2176 if (val == NULL)
2177 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002178 PyErr_SetString(PyExc_AttributeError,
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002179 _("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002180 return -1;
2181 }
2182
2183 if (strcmp(name, "locked") == 0)
2184 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002185 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002186 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002187 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002188 return -1;
2189 }
2190 else
2191 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002192 int istrue = PyObject_IsTrue(val);
2193 if (istrue == -1)
2194 return -1;
2195 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002196 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002197 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002198 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002199 }
2200 return 0;
2201 }
2202 else
2203 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002204 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002205 return -1;
2206 }
2207}
2208
Bram Moolenaardb913952012-06-29 12:54:53 +02002209static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002210 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2211 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2212 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002213};
2214
2215typedef struct
2216{
2217 PyObject_HEAD
2218 char_u *name;
2219} FunctionObject;
2220
2221static PyTypeObject FunctionType;
2222
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002223#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2224
Bram Moolenaardb913952012-06-29 12:54:53 +02002225 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002226FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002227{
2228 FunctionObject *self;
2229
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002230 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2231
Bram Moolenaardb913952012-06-29 12:54:53 +02002232 if (self == NULL)
2233 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002234
2235 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002236 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002237 if (!translated_function_exists(name))
2238 {
2239 PyErr_SetString(PyExc_ValueError,
2240 _("unnamed function does not exist"));
2241 return NULL;
2242 }
2243 self->name = vim_strsave(name);
2244 func_ref(self->name);
2245 }
2246 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002247 if ((self->name = get_expanded_name(name,
2248 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2249 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002250 {
Bram Moolenaar018acca2013-05-30 13:37:28 +02002251 PyErr_SetString(PyExc_ValueError, _("function does not exist"));
2252 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002253 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002254
2255 return (PyObject *)(self);
2256}
2257
2258 static PyObject *
2259FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2260{
2261 PyObject *self;
2262 char_u *name;
2263
2264 if (kwargs)
2265 {
2266 PyErr_SetString(PyExc_TypeError,
2267 _("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002268 return NULL;
2269 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002270
2271 if (!PyArg_ParseTuple(args, "s", &name))
2272 return NULL;
2273
2274 self = FunctionNew(subtype, name);
2275
2276 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002277}
2278
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002279 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002280FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002281{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002282 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002283 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002284
2285 DESTRUCTOR_FINISH(self);
2286}
2287
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002288static char *FunctionAttrs[] = {
2289 "softspace",
2290 NULL
2291};
2292
2293 static PyObject *
2294FunctionDir(PyObject *self)
2295{
2296 return ObjectDir(self, FunctionAttrs);
2297}
2298
Bram Moolenaardb913952012-06-29 12:54:53 +02002299 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002300FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002301{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002302 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002303 typval_T args;
2304 typval_T selfdicttv;
2305 typval_T rettv;
2306 dict_T *selfdict = NULL;
2307 PyObject *selfdictObject;
2308 PyObject *result;
2309 int error;
2310
2311 if (ConvertFromPyObject(argsObject, &args) == -1)
2312 return NULL;
2313
2314 if (kwargs != NULL)
2315 {
2316 selfdictObject = PyDict_GetItemString(kwargs, "self");
2317 if (selfdictObject != NULL)
2318 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002319 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002320 {
2321 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002322 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002323 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002324 selfdict = selfdicttv.vval.v_dict;
2325 }
2326 }
2327
Bram Moolenaar71700b82013-05-15 17:49:05 +02002328 Py_BEGIN_ALLOW_THREADS
2329 Python_Lock_Vim();
2330
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002331 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002332 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002333
2334 Python_Release_Vim();
2335 Py_END_ALLOW_THREADS
2336
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002337 if (VimTryEnd())
2338 result = NULL;
2339 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002340 {
2341 result = NULL;
2342 PyErr_SetVim(_("failed to run function"));
2343 }
2344 else
2345 result = ConvertToPyObject(&rettv);
2346
Bram Moolenaardb913952012-06-29 12:54:53 +02002347 clear_tv(&args);
2348 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002349 if (selfdict != NULL)
2350 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002351
2352 return result;
2353}
2354
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002355 static PyObject *
2356FunctionRepr(FunctionObject *self)
2357{
2358 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2359}
2360
Bram Moolenaardb913952012-06-29 12:54:53 +02002361static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002362 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2363 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002364};
2365
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002366/*
2367 * Options object
2368 */
2369
2370static PyTypeObject OptionsType;
2371
2372typedef int (*checkfun)(void *);
2373
2374typedef struct
2375{
2376 PyObject_HEAD
2377 int opt_type;
2378 void *from;
2379 checkfun Check;
2380 PyObject *fromObj;
2381} OptionsObject;
2382
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002383 static int
2384dummy_check(void *arg UNUSED)
2385{
2386 return 0;
2387}
2388
2389 static PyObject *
2390OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2391{
2392 OptionsObject *self;
2393
Bram Moolenaar774267b2013-05-21 20:51:59 +02002394 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002395 if (self == NULL)
2396 return NULL;
2397
2398 self->opt_type = opt_type;
2399 self->from = from;
2400 self->Check = Check;
2401 self->fromObj = fromObj;
2402 if (fromObj)
2403 Py_INCREF(fromObj);
2404
2405 return (PyObject *)(self);
2406}
2407
2408 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002409OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002410{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002411 PyObject_GC_UnTrack((void *)(self));
2412 Py_XDECREF(self->fromObj);
2413 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002414}
2415
2416 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002417OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002418{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002419 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002420 return 0;
2421}
2422
2423 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002424OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002425{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002426 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002427 return 0;
2428}
2429
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002430 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002431OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002432{
2433 char_u *key;
2434 int flags;
2435 long numval;
2436 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002437 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002438
Bram Moolenaard6e39182013-05-21 18:30:34 +02002439 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002440 return NULL;
2441
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002442 if (!(key = StringToChars(keyObject, &todecref)))
2443 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002444
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002445 if (*key == NUL)
2446 {
2447 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002448 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002449 return NULL;
2450 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002451
2452 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002453 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002454
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002455 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002456
2457 if (flags == 0)
2458 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002459 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002460 return NULL;
2461 }
2462
2463 if (flags & SOPT_UNSET)
2464 {
2465 Py_INCREF(Py_None);
2466 return Py_None;
2467 }
2468 else if (flags & SOPT_BOOL)
2469 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002470 PyObject *r;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002471 r = numval ? Py_True : Py_False;
2472 Py_INCREF(r);
2473 return r;
2474 }
2475 else if (flags & SOPT_NUM)
2476 return PyInt_FromLong(numval);
2477 else if (flags & SOPT_STRING)
2478 {
2479 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002480 {
2481 PyObject *r = PyBytes_FromString((char *) stringval);
2482 vim_free(stringval);
2483 return r;
2484 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002485 else
2486 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002487 PyErr_SetString(PyExc_RuntimeError,
2488 _("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002489 return NULL;
2490 }
2491 }
2492 else
2493 {
2494 PyErr_SetVim("Internal error: unknown option type. Should not happen");
2495 return NULL;
2496 }
2497}
2498
2499 static int
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002500set_option_value_err(key, numval, stringval, opt_flags)
2501 char_u *key;
2502 int numval;
2503 char_u *stringval;
2504 int opt_flags;
2505{
2506 char_u *errmsg;
2507
2508 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2509 {
2510 if (VimTryEnd())
2511 return FAIL;
2512 PyErr_SetVim((char *)errmsg);
2513 return FAIL;
2514 }
2515 return OK;
2516}
2517
2518 static int
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002519set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
2520 char_u *key;
2521 int numval;
2522 char_u *stringval;
2523 int opt_flags;
2524 int opt_type;
2525 void *from;
2526{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002527 win_T *save_curwin = NULL;
2528 tabpage_T *save_curtab = NULL;
2529 buf_T *save_curbuf = NULL;
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002530 int r = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002531
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002532 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002533 switch (opt_type)
2534 {
2535 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002536 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
2537 win_find_tabpage((win_T *)from)) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002538 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002539 if (VimTryEnd())
2540 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002541 PyErr_SetVim("Problem while switching windows.");
2542 return -1;
2543 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002544 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002545 restore_win(save_curwin, save_curtab);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002546 if (r == FAIL)
2547 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002548 break;
2549 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002550 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002551 r = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002552 restore_buffer(save_curbuf);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002553 if (r == FAIL)
2554 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002555 break;
2556 case SREQ_GLOBAL:
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002557 r = set_option_value_err(key, numval, stringval, opt_flags);
2558 if (r == FAIL)
2559 return -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002560 break;
2561 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002562 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002563}
2564
2565 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002566OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002567{
2568 char_u *key;
2569 int flags;
2570 int opt_flags;
2571 int r = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002572 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002573
Bram Moolenaard6e39182013-05-21 18:30:34 +02002574 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002575 return -1;
2576
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002577 if (!(key = StringToChars(keyObject, &todecref)))
2578 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002579
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002580 if (*key == NUL)
2581 {
2582 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002583 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002584 return -1;
2585 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002586
2587 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002588 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002589
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002590 if (flags == 0)
2591 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002592 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002593 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002594 return -1;
2595 }
2596
2597 if (valObject == NULL)
2598 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002599 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002600 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002601 PyErr_SetString(PyExc_ValueError,
2602 _("unable to unset global option"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002603 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002604 return -1;
2605 }
2606 else if (!(flags & SOPT_GLOBAL))
2607 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002608 PyErr_SetString(PyExc_ValueError, _("unable to unset option "
2609 "without global value"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002610 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002611 return -1;
2612 }
2613 else
2614 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002615 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002616 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002617 return 0;
2618 }
2619 }
2620
Bram Moolenaard6e39182013-05-21 18:30:34 +02002621 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002622
2623 if (flags & SOPT_BOOL)
2624 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002625 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002626
Bram Moolenaarb983f752013-05-15 16:11:50 +02002627 if (istrue == -1)
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002628 r = -1;
2629 else
2630 r = set_option_value_for(key, istrue, NULL,
2631 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002632 }
2633 else if (flags & SOPT_NUM)
2634 {
2635 int val;
2636
2637#if PY_MAJOR_VERSION < 3
2638 if (PyInt_Check(valObject))
2639 val = PyInt_AsLong(valObject);
2640 else
2641#endif
2642 if (PyLong_Check(valObject))
2643 val = PyLong_AsLong(valObject);
2644 else
2645 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02002646 PyErr_SetString(PyExc_TypeError, _("object must be integer"));
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002647 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002648 return -1;
2649 }
2650
2651 r = set_option_value_for(key, val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002652 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002653 }
2654 else
2655 {
2656 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002657 PyObject *todecref;
2658
2659 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002660 r = set_option_value_for(key, 0, val, opt_flags,
2661 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002662 else
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002663 r = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002664 }
2665
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002666 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002667
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002668 return r;
2669}
2670
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002671static PyMappingMethods OptionsAsMapping = {
2672 (lenfunc) NULL,
2673 (binaryfunc) OptionsItem,
2674 (objobjargproc) OptionsAssItem,
2675};
2676
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002677/* Tabpage object
2678 */
2679
2680typedef struct
2681{
2682 PyObject_HEAD
2683 tabpage_T *tab;
2684} TabPageObject;
2685
2686static PyObject *WinListNew(TabPageObject *tabObject);
2687
2688static PyTypeObject TabPageType;
2689
2690 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002691CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002692{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002693 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002694 {
2695 PyErr_SetVim(_("attempt to refer to deleted tab page"));
2696 return -1;
2697 }
2698
2699 return 0;
2700}
2701
2702 static PyObject *
2703TabPageNew(tabpage_T *tab)
2704{
2705 TabPageObject *self;
2706
2707 if (TAB_PYTHON_REF(tab))
2708 {
2709 self = TAB_PYTHON_REF(tab);
2710 Py_INCREF(self);
2711 }
2712 else
2713 {
2714 self = PyObject_NEW(TabPageObject, &TabPageType);
2715 if (self == NULL)
2716 return NULL;
2717 self->tab = tab;
2718 TAB_PYTHON_REF(tab) = self;
2719 }
2720
2721 return (PyObject *)(self);
2722}
2723
2724 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002725TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002726{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002727 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
2728 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002729
2730 DESTRUCTOR_FINISH(self);
2731}
2732
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002733static char *TabPageAttrs[] = {
2734 "windows", "number", "vars", "window", "valid",
2735 NULL
2736};
2737
2738 static PyObject *
2739TabPageDir(PyObject *self)
2740{
2741 return ObjectDir(self, TabPageAttrs);
2742}
2743
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002744 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002745TabPageAttrValid(TabPageObject *self, char *name)
2746{
2747 PyObject *r;
2748
2749 if (strcmp(name, "valid") != 0)
2750 return NULL;
2751
2752 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
2753 Py_INCREF(r);
2754 return r;
2755}
2756
2757 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002758TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002759{
2760 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002761 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002762 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002763 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002764 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002765 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002766 else if (strcmp(name, "window") == 0)
2767 {
2768 /* For current tab window.c does not bother to set or update tp_curwin
2769 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02002770 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002771 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002772 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002773 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002774 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002775 else if (strcmp(name, "__members__") == 0)
2776 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002777 return NULL;
2778}
2779
2780 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002781TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002782{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002783 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002784 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002785 else
2786 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002787 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002788
2789 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002790 return PyString_FromFormat("<tabpage object (unknown) at %p>",
2791 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002792 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02002793 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002794 }
2795}
2796
2797static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002798 /* name, function, calling, documentation */
2799 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
2800 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002801};
2802
2803/*
2804 * Window list object
2805 */
2806
2807static PyTypeObject TabListType;
2808static PySequenceMethods TabListAsSeq;
2809
2810typedef struct
2811{
2812 PyObject_HEAD
2813} TabListObject;
2814
2815 static PyInt
2816TabListLength(PyObject *self UNUSED)
2817{
2818 tabpage_T *tp = first_tabpage;
2819 PyInt n = 0;
2820
2821 while (tp != NULL)
2822 {
2823 ++n;
2824 tp = tp->tp_next;
2825 }
2826
2827 return n;
2828}
2829
2830 static PyObject *
2831TabListItem(PyObject *self UNUSED, PyInt n)
2832{
2833 tabpage_T *tp;
2834
2835 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
2836 if (n == 0)
2837 return TabPageNew(tp);
2838
2839 PyErr_SetString(PyExc_IndexError, _("no such tab page"));
2840 return NULL;
2841}
2842
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002843/* Window object
2844 */
2845
2846typedef struct
2847{
2848 PyObject_HEAD
2849 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002850 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002851} WindowObject;
2852
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002853static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002854
2855 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002856CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002857{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002858 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02002859 {
2860 PyErr_SetVim(_("attempt to refer to deleted window"));
2861 return -1;
2862 }
2863
2864 return 0;
2865}
2866
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002867 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002868WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02002869{
2870 /* We need to handle deletion of windows underneath us.
2871 * If we add a "w_python*_ref" field to the win_T structure,
2872 * then we can get at it in win_free() in vim. We then
2873 * need to create only ONE Python object per window - if
2874 * we try to create a second, just INCREF the existing one
2875 * and return it. The (single) Python object referring to
2876 * the window is stored in "w_python*_ref".
2877 * On a win_free() we set the Python object's win_T* field
2878 * to an invalid value. We trap all uses of a window
2879 * object, and reject them if the win_T* field is invalid.
2880 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002881 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02002882 * w_python_ref and w_python3_ref fields respectively.
2883 */
2884
2885 WindowObject *self;
2886
2887 if (WIN_PYTHON_REF(win))
2888 {
2889 self = WIN_PYTHON_REF(win);
2890 Py_INCREF(self);
2891 }
2892 else
2893 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02002894 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02002895 if (self == NULL)
2896 return NULL;
2897 self->win = win;
2898 WIN_PYTHON_REF(win) = self;
2899 }
2900
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002901 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
2902
Bram Moolenaar971db462013-05-12 18:44:48 +02002903 return (PyObject *)(self);
2904}
2905
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002906 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002907WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002908{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002909 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02002910 if (self->win && self->win != INVALID_WINDOW_VALUE)
2911 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02002912 Py_XDECREF(((PyObject *)(self->tabObject)));
2913 PyObject_GC_Del((void *)(self));
2914}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002915
Bram Moolenaar774267b2013-05-21 20:51:59 +02002916 static int
2917WindowTraverse(WindowObject *self, visitproc visit, void *arg)
2918{
2919 Py_VISIT(((PyObject *)(self->tabObject)));
2920 return 0;
2921}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002922
Bram Moolenaar774267b2013-05-21 20:51:59 +02002923 static int
2924WindowClear(WindowObject *self)
2925{
2926 Py_CLEAR(self->tabObject);
2927 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002928}
2929
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002930 static win_T *
2931get_firstwin(TabPageObject *tabObject)
2932{
2933 if (tabObject)
2934 {
2935 if (CheckTabPage(tabObject))
2936 return NULL;
2937 /* For current tab window.c does not bother to set or update tp_firstwin
2938 */
2939 else if (tabObject->tab == curtab)
2940 return firstwin;
2941 else
2942 return tabObject->tab->tp_firstwin;
2943 }
2944 else
2945 return firstwin;
2946}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002947static char *WindowAttrs[] = {
2948 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
2949 "tabpage", "valid",
2950 NULL
2951};
2952
2953 static PyObject *
2954WindowDir(PyObject *self)
2955{
2956 return ObjectDir(self, WindowAttrs);
2957}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02002958
Bram Moolenaar971db462013-05-12 18:44:48 +02002959 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02002960WindowAttrValid(WindowObject *self, char *name)
2961{
2962 PyObject *r;
2963
2964 if (strcmp(name, "valid") != 0)
2965 return NULL;
2966
2967 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
2968 Py_INCREF(r);
2969 return r;
2970}
2971
2972 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002973WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002974{
2975 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002976 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002977 else if (strcmp(name, "cursor") == 0)
2978 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002979 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002980
2981 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
2982 }
2983 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002984 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002985#ifdef FEAT_WINDOWS
2986 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002987 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002988#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002989#ifdef FEAT_VERTSPLIT
2990 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002991 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02002992 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002993 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002994#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02002995 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002996 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002997 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002998 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
2999 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003000 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003001 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003002 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003003 return NULL;
3004 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003005 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003006 }
3007 else if (strcmp(name, "tabpage") == 0)
3008 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003009 Py_INCREF(self->tabObject);
3010 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003011 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003012 else if (strcmp(name, "__members__") == 0)
3013 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003014 else
3015 return NULL;
3016}
3017
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003018 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003019WindowSetattr(WindowObject *self, char *name, PyObject *val)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003020{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003021 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003022 return -1;
3023
3024 if (strcmp(name, "buffer") == 0)
3025 {
3026 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
3027 return -1;
3028 }
3029 else if (strcmp(name, "cursor") == 0)
3030 {
3031 long lnum;
3032 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003033
3034 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3035 return -1;
3036
Bram Moolenaard6e39182013-05-21 18:30:34 +02003037 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003038 {
3039 PyErr_SetVim(_("cursor position outside buffer"));
3040 return -1;
3041 }
3042
3043 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003044 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003045 return -1;
3046
Bram Moolenaard6e39182013-05-21 18:30:34 +02003047 self->win->w_cursor.lnum = lnum;
3048 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003049#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003050 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003051#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003052 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003053 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003054
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003055 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003056 return 0;
3057 }
3058 else if (strcmp(name, "height") == 0)
3059 {
3060 int height;
3061 win_T *savewin;
3062
3063 if (!PyArg_Parse(val, "i", &height))
3064 return -1;
3065
3066#ifdef FEAT_GUI
3067 need_mouse_correct = TRUE;
3068#endif
3069 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003070 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003071
3072 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003073 win_setheight(height);
3074 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003075 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003076 return -1;
3077
3078 return 0;
3079 }
3080#ifdef FEAT_VERTSPLIT
3081 else if (strcmp(name, "width") == 0)
3082 {
3083 int width;
3084 win_T *savewin;
3085
3086 if (!PyArg_Parse(val, "i", &width))
3087 return -1;
3088
3089#ifdef FEAT_GUI
3090 need_mouse_correct = TRUE;
3091#endif
3092 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003093 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003094
3095 VimTryStart();
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003096 win_setwidth(width);
3097 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003098 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003099 return -1;
3100
3101 return 0;
3102 }
3103#endif
3104 else
3105 {
3106 PyErr_SetString(PyExc_AttributeError, name);
3107 return -1;
3108 }
3109}
3110
3111 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003112WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003113{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003114 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003115 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003116 else
3117 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003118 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003119
Bram Moolenaar6d216452013-05-12 19:00:41 +02003120 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003121 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003122 (self));
3123 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003124 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003125 }
3126}
3127
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003128static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003129 /* name, function, calling, documentation */
3130 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3131 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003132};
3133
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003134/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003135 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003136 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003137
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003138static PyTypeObject WinListType;
3139static PySequenceMethods WinListAsSeq;
3140
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003141typedef struct
3142{
3143 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003144 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003145} WinListObject;
3146
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003147 static PyObject *
3148WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003149{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003150 WinListObject *self;
3151
3152 self = PyObject_NEW(WinListObject, &WinListType);
3153 self->tabObject = tabObject;
3154 Py_INCREF(tabObject);
3155
3156 return (PyObject *)(self);
3157}
3158
3159 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003160WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003161{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003162 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003163
3164 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003165 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003166 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003167 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003168
3169 DESTRUCTOR_FINISH(self);
3170}
3171
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003172 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003173WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003174{
3175 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003176 PyInt n = 0;
3177
Bram Moolenaard6e39182013-05-21 18:30:34 +02003178 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003179 return -1;
3180
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003181 while (w != NULL)
3182 {
3183 ++n;
3184 w = W_NEXT(w);
3185 }
3186
3187 return n;
3188}
3189
3190 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003192{
3193 win_T *w;
3194
Bram Moolenaard6e39182013-05-21 18:30:34 +02003195 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003196 return NULL;
3197
3198 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003199 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003200 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003201
3202 PyErr_SetString(PyExc_IndexError, _("no such window"));
3203 return NULL;
3204}
3205
3206/* Convert a Python string into a Vim line.
3207 *
3208 * The result is in allocated memory. All internal nulls are replaced by
3209 * newline characters. It is an error for the string to contain newline
3210 * characters.
3211 *
3212 * On errors, the Python exception data is set, and NULL is returned.
3213 */
3214 static char *
3215StringToLine(PyObject *obj)
3216{
3217 const char *str;
3218 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003219 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003220 PyInt len;
3221 PyInt i;
3222 char *p;
3223
3224 if (obj == NULL || !PyString_Check(obj))
3225 {
3226 PyErr_BadArgument();
3227 return NULL;
3228 }
3229
Bram Moolenaar19e60942011-06-19 00:27:51 +02003230 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3231 str = PyString_AsString(bytes);
3232 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003233
3234 /*
3235 * Error checking: String must not contain newlines, as we
3236 * are replacing a single line, and we must replace it with
3237 * a single line.
3238 * A trailing newline is removed, so that append(f.readlines()) works.
3239 */
3240 p = memchr(str, '\n', len);
3241 if (p != NULL)
3242 {
3243 if (p == str + len - 1)
3244 --len;
3245 else
3246 {
3247 PyErr_SetVim(_("string cannot contain newlines"));
3248 return NULL;
3249 }
3250 }
3251
3252 /* Create a copy of the string, with internal nulls replaced by
3253 * newline characters, as is the vim convention.
3254 */
3255 save = (char *)alloc((unsigned)(len+1));
3256 if (save == NULL)
3257 {
3258 PyErr_NoMemory();
3259 return NULL;
3260 }
3261
3262 for (i = 0; i < len; ++i)
3263 {
3264 if (str[i] == '\0')
3265 save[i] = '\n';
3266 else
3267 save[i] = str[i];
3268 }
3269
3270 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +02003271 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003272
3273 return save;
3274}
3275
3276/* Get a line from the specified buffer. The line number is
3277 * in Vim format (1-based). The line is returned as a Python
3278 * string object.
3279 */
3280 static PyObject *
3281GetBufferLine(buf_T *buf, PyInt n)
3282{
3283 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3284}
3285
3286
3287/* Get a list of lines from the specified buffer. The line numbers
3288 * are in Vim format (1-based). The range is from lo up to, but not
3289 * including, hi. The list is returned as a Python list of string objects.
3290 */
3291 static PyObject *
3292GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3293{
3294 PyInt i;
3295 PyInt n = hi - lo;
3296 PyObject *list = PyList_New(n);
3297
3298 if (list == NULL)
3299 return NULL;
3300
3301 for (i = 0; i < n; ++i)
3302 {
3303 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3304
3305 /* Error check - was the Python string creation OK? */
3306 if (str == NULL)
3307 {
3308 Py_DECREF(list);
3309 return NULL;
3310 }
3311
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02003312 PyList_SET_ITEM(list, i, str);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003313 }
3314
3315 /* The ownership of the Python list is passed to the caller (ie,
3316 * the caller should Py_DECREF() the object when it is finished
3317 * with it).
3318 */
3319
3320 return list;
3321}
3322
3323/*
3324 * Check if deleting lines made the cursor position invalid.
3325 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3326 * deleted).
3327 */
3328 static void
3329py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3330{
3331 if (curwin->w_cursor.lnum >= lo)
3332 {
3333 /* Adjust the cursor position if it's in/after the changed
3334 * lines. */
3335 if (curwin->w_cursor.lnum >= hi)
3336 {
3337 curwin->w_cursor.lnum += extra;
3338 check_cursor_col();
3339 }
3340 else if (extra < 0)
3341 {
3342 curwin->w_cursor.lnum = lo;
3343 check_cursor();
3344 }
3345 else
3346 check_cursor_col();
3347 changed_cline_bef_curs();
3348 }
3349 invalidate_botline();
3350}
3351
Bram Moolenaar19e60942011-06-19 00:27:51 +02003352/*
3353 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003354 * in Vim format (1-based). The replacement line is given as
3355 * a Python string object. The object is checked for validity
3356 * and correct format. Errors are returned as a value of FAIL.
3357 * The return value is OK on success.
3358 * If OK is returned and len_change is not NULL, *len_change
3359 * is set to the change in the buffer length.
3360 */
3361 static int
3362SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3363{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003364 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003365 * There are three cases:
3366 * 1. NULL, or None - this is a deletion.
3367 * 2. A string - this is a replacement.
3368 * 3. Anything else - this is an error.
3369 */
3370 if (line == Py_None || line == NULL)
3371 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003372 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003373
3374 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003375 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003376
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003377 VimTryStart();
3378
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003379 if (u_savedel((linenr_T)n, 1L) == FAIL)
3380 PyErr_SetVim(_("cannot save undo information"));
3381 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3382 PyErr_SetVim(_("cannot delete line"));
3383 else
3384 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003385 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003386 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3387 deleted_lines_mark((linenr_T)n, 1L);
3388 }
3389
Bram Moolenaar105bc352013-05-17 16:03:57 +02003390 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003391
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003392 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003393 return FAIL;
3394
3395 if (len_change)
3396 *len_change = -1;
3397
3398 return OK;
3399 }
3400 else if (PyString_Check(line))
3401 {
3402 char *save = StringToLine(line);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003403 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003404
3405 if (save == NULL)
3406 return FAIL;
3407
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003408 VimTryStart();
3409
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003410 /* We do not need to free "save" if ml_replace() consumes it. */
3411 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003412 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003413
3414 if (u_savesub((linenr_T)n) == FAIL)
3415 {
3416 PyErr_SetVim(_("cannot save undo information"));
3417 vim_free(save);
3418 }
3419 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3420 {
3421 PyErr_SetVim(_("cannot replace line"));
3422 vim_free(save);
3423 }
3424 else
3425 changed_bytes((linenr_T)n, 0);
3426
Bram Moolenaar105bc352013-05-17 16:03:57 +02003427 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003428
3429 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003430 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003431 check_cursor_col();
3432
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003433 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003434 return FAIL;
3435
3436 if (len_change)
3437 *len_change = 0;
3438
3439 return OK;
3440 }
3441 else
3442 {
3443 PyErr_BadArgument();
3444 return FAIL;
3445 }
3446}
3447
Bram Moolenaar19e60942011-06-19 00:27:51 +02003448/* Replace a range of lines in the specified buffer. The line numbers are in
3449 * Vim format (1-based). The range is from lo up to, but not including, hi.
3450 * The replacement lines are given as a Python list of string objects. The
3451 * list is checked for validity and correct format. Errors are returned as a
3452 * value of FAIL. The return value is OK on success.
3453 * If OK is returned and len_change is not NULL, *len_change
3454 * is set to the change in the buffer length.
3455 */
3456 static int
3457SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
3458{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003459 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003460 * There are three cases:
3461 * 1. NULL, or None - this is a deletion.
3462 * 2. A list - this is a replacement.
3463 * 3. Anything else - this is an error.
3464 */
3465 if (list == Py_None || list == NULL)
3466 {
3467 PyInt i;
3468 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003469 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003470
3471 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003472 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003473 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003474
3475 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3476 PyErr_SetVim(_("cannot save undo information"));
3477 else
3478 {
3479 for (i = 0; i < n; ++i)
3480 {
3481 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3482 {
3483 PyErr_SetVim(_("cannot delete line"));
3484 break;
3485 }
3486 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003487 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003488 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3489 deleted_lines_mark((linenr_T)lo, (long)i);
3490 }
3491
Bram Moolenaar105bc352013-05-17 16:03:57 +02003492 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003493
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003494 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003495 return FAIL;
3496
3497 if (len_change)
3498 *len_change = -n;
3499
3500 return OK;
3501 }
3502 else if (PyList_Check(list))
3503 {
3504 PyInt i;
3505 PyInt new_len = PyList_Size(list);
3506 PyInt old_len = hi - lo;
3507 PyInt extra = 0; /* lines added to text, can be negative */
3508 char **array;
3509 buf_T *savebuf;
3510
3511 if (new_len == 0) /* avoid allocating zero bytes */
3512 array = NULL;
3513 else
3514 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003515 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003516 if (array == NULL)
3517 {
3518 PyErr_NoMemory();
3519 return FAIL;
3520 }
3521 }
3522
3523 for (i = 0; i < new_len; ++i)
3524 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003525 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003526
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003527 if (!(line = PyList_GetItem(list, i)) ||
3528 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003529 {
3530 while (i)
3531 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003532 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003533 return FAIL;
3534 }
3535 }
3536
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003537 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003538 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003539
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003540 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003541 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003542
3543 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3544 PyErr_SetVim(_("cannot save undo information"));
3545
3546 /* If the size of the range is reducing (ie, new_len < old_len) we
3547 * need to delete some old_len. We do this at the start, by
3548 * repeatedly deleting line "lo".
3549 */
3550 if (!PyErr_Occurred())
3551 {
3552 for (i = 0; i < old_len - new_len; ++i)
3553 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3554 {
3555 PyErr_SetVim(_("cannot delete line"));
3556 break;
3557 }
3558 extra -= i;
3559 }
3560
3561 /* For as long as possible, replace the existing old_len with the
3562 * new old_len. This is a more efficient operation, as it requires
3563 * less memory allocation and freeing.
3564 */
3565 if (!PyErr_Occurred())
3566 {
3567 for (i = 0; i < old_len && i < new_len; ++i)
3568 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3569 == FAIL)
3570 {
3571 PyErr_SetVim(_("cannot replace line"));
3572 break;
3573 }
3574 }
3575 else
3576 i = 0;
3577
3578 /* Now we may need to insert the remaining new old_len. If we do, we
3579 * must free the strings as we finish with them (we can't pass the
3580 * responsibility to vim in this case).
3581 */
3582 if (!PyErr_Occurred())
3583 {
3584 while (i < new_len)
3585 {
3586 if (ml_append((linenr_T)(lo + i - 1),
3587 (char_u *)array[i], 0, FALSE) == FAIL)
3588 {
3589 PyErr_SetVim(_("cannot insert line"));
3590 break;
3591 }
3592 vim_free(array[i]);
3593 ++i;
3594 ++extra;
3595 }
3596 }
3597
3598 /* Free any left-over old_len, as a result of an error */
3599 while (i < new_len)
3600 {
3601 vim_free(array[i]);
3602 ++i;
3603 }
3604
3605 /* Free the array of old_len. All of its contents have now
3606 * been dealt with (either freed, or the responsibility passed
3607 * to vim.
3608 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003609 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003610
3611 /* Adjust marks. Invalidate any which lie in the
3612 * changed range, and move any in the remainder of the buffer.
3613 */
3614 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3615 (long)MAXLNUM, (long)extra);
3616 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3617
Bram Moolenaar105bc352013-05-17 16:03:57 +02003618 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003619 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3620
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003621 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003622 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003623
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003624 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003625 return FAIL;
3626
3627 if (len_change)
3628 *len_change = new_len - old_len;
3629
3630 return OK;
3631 }
3632 else
3633 {
3634 PyErr_BadArgument();
3635 return FAIL;
3636 }
3637}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003638
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003639/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003640 * The line number is in Vim format (1-based). The lines to be inserted are
3641 * given as a Python list of string objects or as a single string. The lines
3642 * to be added are checked for validity and correct format. Errors are
3643 * returned as a value of FAIL. The return value is OK on success.
3644 * If OK is returned and len_change is not NULL, *len_change
3645 * is set to the change in the buffer length.
3646 */
3647 static int
3648InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3649{
3650 /* First of all, we check the type of the supplied Python object.
3651 * It must be a string or a list, or the call is in error.
3652 */
3653 if (PyString_Check(lines))
3654 {
3655 char *str = StringToLine(lines);
3656 buf_T *savebuf;
3657
3658 if (str == NULL)
3659 return FAIL;
3660
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003661 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003662 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003663 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003664
3665 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3666 PyErr_SetVim(_("cannot save undo information"));
3667 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3668 PyErr_SetVim(_("cannot insert line"));
3669 else
3670 appended_lines_mark((linenr_T)n, 1L);
3671
3672 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003673 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003674 update_screen(VALID);
3675
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003676 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003677 return FAIL;
3678
3679 if (len_change)
3680 *len_change = 1;
3681
3682 return OK;
3683 }
3684 else if (PyList_Check(lines))
3685 {
3686 PyInt i;
3687 PyInt size = PyList_Size(lines);
3688 char **array;
3689 buf_T *savebuf;
3690
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003691 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003692 if (array == NULL)
3693 {
3694 PyErr_NoMemory();
3695 return FAIL;
3696 }
3697
3698 for (i = 0; i < size; ++i)
3699 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003700 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003701
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003702 if (!(line = PyList_GetItem(lines, i)) ||
3703 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 {
3705 while (i)
3706 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003707 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708 return FAIL;
3709 }
3710 }
3711
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003712 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003713 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003714 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003715
3716 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3717 PyErr_SetVim(_("cannot save undo information"));
3718 else
3719 {
3720 for (i = 0; i < size; ++i)
3721 {
3722 if (ml_append((linenr_T)(n + i),
3723 (char_u *)array[i], 0, FALSE) == FAIL)
3724 {
3725 PyErr_SetVim(_("cannot insert line"));
3726
3727 /* Free the rest of the lines */
3728 while (i < size)
3729 vim_free(array[i++]);
3730
3731 break;
3732 }
3733 vim_free(array[i]);
3734 }
3735 if (i > 0)
3736 appended_lines_mark((linenr_T)n, (long)i);
3737 }
3738
3739 /* Free the array of lines. All of its contents have now
3740 * been freed.
3741 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003742 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003743
Bram Moolenaar105bc352013-05-17 16:03:57 +02003744 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003745 update_screen(VALID);
3746
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003747 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003748 return FAIL;
3749
3750 if (len_change)
3751 *len_change = size;
3752
3753 return OK;
3754 }
3755 else
3756 {
3757 PyErr_BadArgument();
3758 return FAIL;
3759 }
3760}
3761
3762/*
3763 * Common routines for buffers and line ranges
3764 * -------------------------------------------
3765 */
3766
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003767typedef struct
3768{
3769 PyObject_HEAD
3770 buf_T *buf;
3771} BufferObject;
3772
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003773 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003774CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003775{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003776 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003777 {
3778 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3779 return -1;
3780 }
3781
3782 return 0;
3783}
3784
3785 static PyObject *
3786RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
3787{
3788 if (CheckBuffer(self))
3789 return NULL;
3790
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003791 if (end == -1)
3792 end = self->buf->b_ml.ml_line_count;
3793
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003794 if (n < 0)
3795 n += end - start + 1;
3796
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003797 if (n < 0 || n > end - start)
3798 {
3799 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3800 return NULL;
3801 }
3802
3803 return GetBufferLine(self->buf, n+start);
3804}
3805
3806 static PyObject *
3807RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
3808{
3809 PyInt size;
3810
3811 if (CheckBuffer(self))
3812 return NULL;
3813
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003814 if (end == -1)
3815 end = self->buf->b_ml.ml_line_count;
3816
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003817 size = end - start + 1;
3818
3819 if (lo < 0)
3820 lo = 0;
3821 else if (lo > size)
3822 lo = size;
3823 if (hi < 0)
3824 hi = 0;
3825 if (hi < lo)
3826 hi = lo;
3827 else if (hi > size)
3828 hi = size;
3829
3830 return GetBufferLineList(self->buf, lo+start, hi+start);
3831}
3832
3833 static PyInt
3834RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3835{
3836 PyInt len_change;
3837
3838 if (CheckBuffer(self))
3839 return -1;
3840
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003841 if (end == -1)
3842 end = self->buf->b_ml.ml_line_count;
3843
Bram Moolenaarbd80f352013-05-12 21:16:23 +02003844 if (n < 0)
3845 n += end - start + 1;
3846
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003847 if (n < 0 || n > end - start)
3848 {
3849 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3850 return -1;
3851 }
3852
3853 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3854 return -1;
3855
3856 if (new_end)
3857 *new_end = end + len_change;
3858
3859 return 0;
3860}
3861
Bram Moolenaar19e60942011-06-19 00:27:51 +02003862 static PyInt
3863RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
3864{
3865 PyInt size;
3866 PyInt len_change;
3867
3868 /* Self must be a valid buffer */
3869 if (CheckBuffer(self))
3870 return -1;
3871
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003872 if (end == -1)
3873 end = self->buf->b_ml.ml_line_count;
3874
Bram Moolenaar19e60942011-06-19 00:27:51 +02003875 /* Sort out the slice range */
3876 size = end - start + 1;
3877
3878 if (lo < 0)
3879 lo = 0;
3880 else if (lo > size)
3881 lo = size;
3882 if (hi < 0)
3883 hi = 0;
3884 if (hi < lo)
3885 hi = lo;
3886 else if (hi > size)
3887 hi = size;
3888
3889 if (SetBufferLineList(self->buf, lo + start, hi + start,
3890 val, &len_change) == FAIL)
3891 return -1;
3892
3893 if (new_end)
3894 *new_end = end + len_change;
3895
3896 return 0;
3897}
3898
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003899
3900 static PyObject *
3901RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
3902{
3903 PyObject *lines;
3904 PyInt len_change;
3905 PyInt max;
3906 PyInt n;
3907
3908 if (CheckBuffer(self))
3909 return NULL;
3910
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02003911 if (end == -1)
3912 end = self->buf->b_ml.ml_line_count;
3913
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003914 max = n = end - start + 1;
3915
3916 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
3917 return NULL;
3918
3919 if (n < 0 || n > max)
3920 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02003921 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003922 return NULL;
3923 }
3924
3925 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3926 return NULL;
3927
3928 if (new_end)
3929 *new_end = end + len_change;
3930
3931 Py_INCREF(Py_None);
3932 return Py_None;
3933}
3934
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003935/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003936 */
3937
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003938static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003939static PySequenceMethods RangeAsSeq;
3940static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003941
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003942typedef struct
3943{
3944 PyObject_HEAD
3945 BufferObject *buf;
3946 PyInt start;
3947 PyInt end;
3948} RangeObject;
3949
3950 static PyObject *
3951RangeNew(buf_T *buf, PyInt start, PyInt end)
3952{
3953 BufferObject *bufr;
3954 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003955 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003956 if (self == NULL)
3957 return NULL;
3958
3959 bufr = (BufferObject *)BufferNew(buf);
3960 if (bufr == NULL)
3961 {
3962 Py_DECREF(self);
3963 return NULL;
3964 }
3965 Py_INCREF(bufr);
3966
3967 self->buf = bufr;
3968 self->start = start;
3969 self->end = end;
3970
3971 return (PyObject *)(self);
3972}
3973
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003974 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003975RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003976{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003977 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003978 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02003979 PyObject_GC_Del((void *)(self));
3980}
3981
3982 static int
3983RangeTraverse(RangeObject *self, visitproc visit, void *arg)
3984{
3985 Py_VISIT(((PyObject *)(self->buf)));
3986 return 0;
3987}
3988
3989 static int
3990RangeClear(RangeObject *self)
3991{
3992 Py_CLEAR(self->buf);
3993 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003994}
3995
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003996 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003997RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003998{
3999 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004000 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004001 return -1; /* ??? */
4002
Bram Moolenaard6e39182013-05-21 18:30:34 +02004003 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004004}
4005
4006 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004007RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004008{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004009 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004010}
4011
4012 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004013RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004014{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004016}
4017
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004018static char *RangeAttrs[] = {
4019 "start", "end",
4020 NULL
4021};
4022
4023 static PyObject *
4024RangeDir(PyObject *self)
4025{
4026 return ObjectDir(self, RangeAttrs);
4027}
4028
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004029 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004030RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004031{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004032 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004033}
4034
4035 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004036RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004037{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004038 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004039 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4040 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004041 else
4042 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004043 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004044
4045 if (name == NULL)
4046 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004047
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004048 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004049 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004050 }
4051}
4052
4053static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004054 /* name, function, calling, documentation */
4055 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004056 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4057 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004058};
4059
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004060static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004061static PySequenceMethods BufferAsSeq;
4062static PyMappingMethods BufferAsMapping;
4063
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004064 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004065BufferNew(buf_T *buf)
4066{
4067 /* We need to handle deletion of buffers underneath us.
4068 * If we add a "b_python*_ref" field to the buf_T structure,
4069 * then we can get at it in buf_freeall() in vim. We then
4070 * need to create only ONE Python object per buffer - if
4071 * we try to create a second, just INCREF the existing one
4072 * and return it. The (single) Python object referring to
4073 * the buffer is stored in "b_python*_ref".
4074 * Question: what to do on a buf_freeall(). We'll probably
4075 * have to either delete the Python object (DECREF it to
4076 * zero - a bad idea, as it leaves dangling refs!) or
4077 * set the buf_T * value to an invalid value (-1?), which
4078 * means we need checks in all access functions... Bah.
4079 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004080 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004081 * b_python_ref and b_python3_ref fields respectively.
4082 */
4083
4084 BufferObject *self;
4085
4086 if (BUF_PYTHON_REF(buf) != NULL)
4087 {
4088 self = BUF_PYTHON_REF(buf);
4089 Py_INCREF(self);
4090 }
4091 else
4092 {
4093 self = PyObject_NEW(BufferObject, &BufferType);
4094 if (self == NULL)
4095 return NULL;
4096 self->buf = buf;
4097 BUF_PYTHON_REF(buf) = self;
4098 }
4099
4100 return (PyObject *)(self);
4101}
4102
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004103 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004104BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004105{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004106 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4107 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004108
4109 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004110}
4111
Bram Moolenaar971db462013-05-12 18:44:48 +02004112 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004113BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004114{
4115 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004116 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004117 return -1; /* ??? */
4118
Bram Moolenaard6e39182013-05-21 18:30:34 +02004119 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004120}
4121
4122 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004123BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004124{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004125 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004126}
4127
4128 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004129BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004130{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004131 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004132}
4133
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004134static char *BufferAttrs[] = {
4135 "name", "number", "vars", "options", "valid",
4136 NULL
4137};
4138
4139 static PyObject *
4140BufferDir(PyObject *self)
4141{
4142 return ObjectDir(self, BufferAttrs);
4143}
4144
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004145 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004146BufferAttrValid(BufferObject *self, char *name)
4147{
4148 PyObject *r;
4149
4150 if (strcmp(name, "valid") != 0)
4151 return NULL;
4152
4153 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4154 Py_INCREF(r);
4155 return r;
4156}
4157
4158 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004159BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004160{
4161 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004162 return PyString_FromString((self->buf->b_ffname == NULL
4163 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004164 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004165 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004166 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004167 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004168 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004169 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4170 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004171 else if (strcmp(name, "__members__") == 0)
4172 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004173 else
4174 return NULL;
4175}
4176
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004177 static int
4178BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4179{
4180 if (CheckBuffer(self))
4181 return -1;
4182
4183 if (strcmp(name, "name") == 0)
4184 {
4185 char_u *val;
4186 aco_save_T aco;
4187 int r;
4188 PyObject *todecref;
4189
4190 if (!(val = StringToChars(valObject, &todecref)))
4191 return -1;
4192
4193 VimTryStart();
4194 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4195 aucmd_prepbuf(&aco, self->buf);
4196 r = rename_buffer(val);
4197 aucmd_restbuf(&aco);
4198 Py_XDECREF(todecref);
4199 if (VimTryEnd())
4200 return -1;
4201
4202 if (r == FAIL)
4203 {
4204 PyErr_SetVim(_("failed to rename buffer"));
4205 return -1;
4206 }
4207 return 0;
4208 }
4209 else
4210 {
4211 PyErr_SetString(PyExc_AttributeError, name);
4212 return -1;
4213 }
4214}
4215
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004216 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004217BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004219 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004220}
4221
4222 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004223BufferMark(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004224{
4225 pos_T *posp;
4226 char *pmark;
4227 char mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004228 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004229
Bram Moolenaard6e39182013-05-21 18:30:34 +02004230 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004231 return NULL;
4232
4233 if (!PyArg_ParseTuple(args, "s", &pmark))
4234 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004235
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004236 if (STRLEN(pmark) != 1)
4237 {
4238 PyErr_SetString(PyExc_ValueError,
4239 _("mark name must be a single character"));
4240 return NULL;
4241 }
4242
4243 mark = *pmark;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004244 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004245 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004246 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004247 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004248 if (VimTryEnd())
4249 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004250
4251 if (posp == NULL)
4252 {
4253 PyErr_SetVim(_("invalid mark name"));
4254 return NULL;
4255 }
4256
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004257 if (posp->lnum <= 0)
4258 {
4259 /* Or raise an error? */
4260 Py_INCREF(Py_None);
4261 return Py_None;
4262 }
4263
4264 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4265}
4266
4267 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004268BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269{
4270 PyInt start;
4271 PyInt end;
4272
Bram Moolenaard6e39182013-05-21 18:30:34 +02004273 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004274 return NULL;
4275
4276 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4277 return NULL;
4278
Bram Moolenaard6e39182013-05-21 18:30:34 +02004279 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004280}
4281
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004282 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004283BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004284{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004285 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004286 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004287 else
4288 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004289 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004290
4291 if (name == NULL)
4292 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004293
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004294 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004295 }
4296}
4297
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004298static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004299 /* name, function, calling, documentation */
4300 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4301 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4302 {"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 +02004303 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4304 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004305};
4306
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004307/*
4308 * Buffer list object - Implementation
4309 */
4310
4311static PyTypeObject BufMapType;
4312
4313typedef struct
4314{
4315 PyObject_HEAD
4316} BufMapObject;
4317
4318 static PyInt
4319BufMapLength(PyObject *self UNUSED)
4320{
4321 buf_T *b = firstbuf;
4322 PyInt n = 0;
4323
4324 while (b)
4325 {
4326 ++n;
4327 b = b->b_next;
4328 }
4329
4330 return n;
4331}
4332
4333 static PyObject *
4334BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4335{
4336 buf_T *b;
4337 int bnr;
4338
4339#if PY_MAJOR_VERSION < 3
4340 if (PyInt_Check(keyObject))
4341 bnr = PyInt_AsLong(keyObject);
4342 else
4343#endif
4344 if (PyLong_Check(keyObject))
4345 bnr = PyLong_AsLong(keyObject);
4346 else
4347 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02004348 PyErr_SetString(PyExc_TypeError, _("key must be integer"));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004349 return NULL;
4350 }
4351
4352 b = buflist_findnr(bnr);
4353
4354 if (b)
4355 return BufferNew(b);
4356 else
4357 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004358 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004359 return NULL;
4360 }
4361}
4362
4363 static void
4364BufMapIterDestruct(PyObject *buffer)
4365{
4366 /* Iteration was stopped before all buffers were processed */
4367 if (buffer)
4368 {
4369 Py_DECREF(buffer);
4370 }
4371}
4372
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004373 static int
4374BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4375{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004376 if (buffer)
4377 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004378 return 0;
4379}
4380
4381 static int
4382BufMapIterClear(PyObject **buffer)
4383{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004384 if (*buffer)
4385 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004386 return 0;
4387}
4388
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004389 static PyObject *
4390BufMapIterNext(PyObject **buffer)
4391{
4392 PyObject *next;
4393 PyObject *r;
4394
4395 if (!*buffer)
4396 return NULL;
4397
4398 r = *buffer;
4399
4400 if (CheckBuffer((BufferObject *)(r)))
4401 {
4402 *buffer = NULL;
4403 return NULL;
4404 }
4405
4406 if (!((BufferObject *)(r))->buf->b_next)
4407 next = NULL;
4408 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next)))
4409 return NULL;
4410 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004411 /* Do not increment reference: we no longer hold it (decref), but whoever
4412 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004413 return r;
4414}
4415
4416 static PyObject *
4417BufMapIter(PyObject *self UNUSED)
4418{
4419 PyObject *buffer;
4420
4421 buffer = BufferNew(firstbuf);
4422 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004423 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4424 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004425}
4426
4427static PyMappingMethods BufMapAsMapping = {
4428 (lenfunc) BufMapLength,
4429 (binaryfunc) BufMapItem,
4430 (objobjargproc) 0,
4431};
4432
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004433/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434 */
4435
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004436static char *CurrentAttrs[] = {
4437 "buffer", "window", "line", "range", "tabpage",
4438 NULL
4439};
4440
4441 static PyObject *
4442CurrentDir(PyObject *self)
4443{
4444 return ObjectDir(self, CurrentAttrs);
4445}
4446
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004447 static PyObject *
4448CurrentGetattr(PyObject *self UNUSED, char *name)
4449{
4450 if (strcmp(name, "buffer") == 0)
4451 return (PyObject *)BufferNew(curbuf);
4452 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004453 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004454 else if (strcmp(name, "tabpage") == 0)
4455 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004456 else if (strcmp(name, "line") == 0)
4457 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4458 else if (strcmp(name, "range") == 0)
4459 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004460 else if (strcmp(name, "__members__") == 0)
4461 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004462 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004463#if PY_MAJOR_VERSION < 3
4464 return Py_FindMethod(WindowMethods, self, name);
4465#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004466 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004467#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004468}
4469
4470 static int
4471CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
4472{
4473 if (strcmp(name, "line") == 0)
4474 {
4475 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
4476 return -1;
4477
4478 return 0;
4479 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004480 else if (strcmp(name, "buffer") == 0)
4481 {
4482 int count;
4483
4484 if (value->ob_type != &BufferType)
4485 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004486 PyErr_SetString(PyExc_TypeError, _("expected vim.Buffer object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004487 return -1;
4488 }
4489
4490 if (CheckBuffer((BufferObject *)(value)))
4491 return -1;
4492 count = ((BufferObject *)(value))->buf->b_fnum;
4493
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004494 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004495 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4496 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004497 if (VimTryEnd())
4498 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004499 PyErr_SetVim(_("failed to switch to given buffer"));
4500 return -1;
4501 }
4502
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004503 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004504 }
4505 else if (strcmp(name, "window") == 0)
4506 {
4507 int count;
4508
4509 if (value->ob_type != &WindowType)
4510 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004511 PyErr_SetString(PyExc_TypeError, _("expected vim.Window object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004512 return -1;
4513 }
4514
4515 if (CheckWindow((WindowObject *)(value)))
4516 return -1;
4517 count = get_win_number(((WindowObject *)(value))->win, firstwin);
4518
4519 if (!count)
4520 {
4521 PyErr_SetString(PyExc_ValueError,
4522 _("failed to find window in the current tab page"));
4523 return -1;
4524 }
4525
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004526 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004527 win_goto(((WindowObject *)(value))->win);
4528 if (((WindowObject *)(value))->win != curwin)
4529 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004530 if (VimTryEnd())
4531 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004532 PyErr_SetString(PyExc_RuntimeError,
4533 _("did not switch to the specified window"));
4534 return -1;
4535 }
4536
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004537 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004538 }
4539 else if (strcmp(name, "tabpage") == 0)
4540 {
4541 if (value->ob_type != &TabPageType)
4542 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004543 PyErr_SetString(PyExc_TypeError, _("expected vim.TabPage object"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004544 return -1;
4545 }
4546
4547 if (CheckTabPage((TabPageObject *)(value)))
4548 return -1;
4549
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004550 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004551 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE);
4552 if (((TabPageObject *)(value))->tab != curtab)
4553 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004554 if (VimTryEnd())
4555 return -1;
Bram Moolenaare7614592013-05-15 15:51:08 +02004556 PyErr_SetString(PyExc_RuntimeError,
4557 _("did not switch to the specified tab page"));
4558 return -1;
4559 }
4560
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004561 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004562 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004563 else
4564 {
4565 PyErr_SetString(PyExc_AttributeError, name);
4566 return -1;
4567 }
4568}
4569
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004570static struct PyMethodDef CurrentMethods[] = {
4571 /* name, function, calling, documentation */
4572 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4573 { NULL, NULL, 0, NULL}
4574};
4575
Bram Moolenaardb913952012-06-29 12:54:53 +02004576 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004577init_range_cmd(exarg_T *eap)
4578{
4579 RangeStart = eap->line1;
4580 RangeEnd = eap->line2;
4581}
4582
4583 static void
4584init_range_eval(typval_T *rettv UNUSED)
4585{
4586 RangeStart = (PyInt) curwin->w_cursor.lnum;
4587 RangeEnd = RangeStart;
4588}
4589
4590 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004591run_cmd(const char *cmd, void *arg UNUSED
4592#ifdef PY_CAN_RECURSE
4593 , PyGILState_STATE *pygilstate UNUSED
4594#endif
4595 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004596{
4597 PyRun_SimpleString((char *) cmd);
4598}
4599
4600static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4601static int code_hdr_len = 30;
4602
4603 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004604run_do(const char *cmd, void *arg UNUSED
4605#ifdef PY_CAN_RECURSE
4606 , PyGILState_STATE *pygilstate
4607#endif
4608 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004609{
4610 PyInt lnum;
4611 size_t len;
4612 char *code;
4613 int status;
4614 PyObject *pyfunc, *pymain;
4615
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004616 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004617 {
4618 EMSG(_("cannot save undo information"));
4619 return;
4620 }
4621
4622 len = code_hdr_len + STRLEN(cmd);
4623 code = PyMem_New(char, len + 1);
4624 memcpy(code, code_hdr, code_hdr_len);
4625 STRCPY(code + code_hdr_len, cmd);
4626 status = PyRun_SimpleString(code);
4627 PyMem_Free(code);
4628
4629 if (status)
4630 {
4631 EMSG(_("failed to run the code"));
4632 return;
4633 }
4634
4635 status = 0;
4636 pymain = PyImport_AddModule("__main__");
4637 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004638#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004639 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004640#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004641
4642 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4643 {
4644 PyObject *line, *linenr, *ret;
4645
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004646#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004647 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004648#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004649 if (!(line = GetBufferLine(curbuf, lnum)))
4650 goto err;
4651 if (!(linenr = PyInt_FromLong((long) lnum)))
4652 {
4653 Py_DECREF(line);
4654 goto err;
4655 }
4656 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4657 Py_DECREF(line);
4658 Py_DECREF(linenr);
4659 if (!ret)
4660 goto err;
4661
4662 if (ret != Py_None)
4663 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4664 goto err;
4665
4666 Py_XDECREF(ret);
4667 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004668#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004669 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004670#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004671 }
4672 goto out;
4673err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004674#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004675 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004676#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004677 PyErr_PrintEx(0);
4678 PythonIO_Flush();
4679 status = 1;
4680out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004681#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004682 if (!status)
4683 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004684#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004685 Py_DECREF(pyfunc);
4686 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
4687 if (status)
4688 return;
4689 check_cursor();
4690 update_curbuf(NOT_VALID);
4691}
4692
4693 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004694run_eval(const char *cmd, typval_T *rettv
4695#ifdef PY_CAN_RECURSE
4696 , PyGILState_STATE *pygilstate UNUSED
4697#endif
4698 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004699{
4700 PyObject *r;
4701
4702 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
4703 if (r == NULL)
4704 {
4705 if (PyErr_Occurred() && !msg_silent)
4706 PyErr_PrintEx(0);
4707 EMSG(_("E858: Eval did not return a valid python object"));
4708 }
4709 else
4710 {
4711 if (ConvertFromPyObject(r, rettv) == -1)
4712 EMSG(_("E859: Failed to convert returned python object to vim value"));
4713 Py_DECREF(r);
4714 }
4715 PyErr_Clear();
4716}
4717
4718 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02004719set_ref_in_py(const int copyID)
4720{
4721 pylinkedlist_T *cur;
4722 dict_T *dd;
4723 list_T *ll;
4724
4725 if (lastdict != NULL)
4726 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
4727 {
4728 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
4729 if (dd->dv_copyID != copyID)
4730 {
4731 dd->dv_copyID = copyID;
4732 set_ref_in_ht(&dd->dv_hashtab, copyID);
4733 }
4734 }
4735
4736 if (lastlist != NULL)
4737 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
4738 {
4739 ll = ((ListObject *) (cur->pll_obj))->list;
4740 if (ll->lv_copyID != copyID)
4741 {
4742 ll->lv_copyID = copyID;
4743 set_ref_in_list(ll, copyID);
4744 }
4745 }
4746}
4747
4748 static int
4749set_string_copy(char_u *str, typval_T *tv)
4750{
4751 tv->vval.v_string = vim_strsave(str);
4752 if (tv->vval.v_string == NULL)
4753 {
4754 PyErr_NoMemory();
4755 return -1;
4756 }
4757 return 0;
4758}
4759
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004760 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004761pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004762{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004763 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004764 char_u *key;
4765 dictitem_T *di;
4766 PyObject *keyObject;
4767 PyObject *valObject;
4768 Py_ssize_t iter = 0;
4769
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004770 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004771 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004772
4773 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004774 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004775
4776 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
4777 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004778 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004779
Bram Moolenaara03e6312013-05-29 22:49:26 +02004780 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004781 {
4782 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004783 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004784 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004785
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004786 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004787 {
4788 dict_unref(dict);
4789 return -1;
4790 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004791
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004792 if (*key == NUL)
4793 {
4794 dict_unref(dict);
4795 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004796 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004797 return -1;
4798 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004799
4800 di = dictitem_alloc(key);
4801
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004802 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004803
4804 if (di == NULL)
4805 {
4806 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004807 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004808 return -1;
4809 }
4810 di->di_tv.v_lock = 0;
4811
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004812 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004813 {
4814 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004815 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004816 return -1;
4817 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004818
4819 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004820 {
Bram Moolenaara03e6312013-05-29 22:49:26 +02004821 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004822 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004823 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004824 PyErr_SetVim(_("failed to add key to dictionary"));
4825 return -1;
4826 }
4827 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004828
4829 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004830 return 0;
4831}
4832
4833 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004834pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004835{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004836 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004837 char_u *key;
4838 dictitem_T *di;
4839 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004840 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004841 PyObject *keyObject;
4842 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004843
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004844 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004845 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004846
4847 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004848 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004849
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004850 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004851 {
4852 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004853 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004854 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004855
4856 if (!(iterator = PyObject_GetIter(list)))
4857 {
4858 dict_unref(dict);
4859 Py_DECREF(list);
4860 return -1;
4861 }
4862 Py_DECREF(list);
4863
4864 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004865 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004866 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004867
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004868 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004869 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004870 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004871 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004872 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004873 return -1;
4874 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02004875
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004876 if (*key == NUL)
4877 {
4878 Py_DECREF(keyObject);
4879 Py_DECREF(iterator);
4880 Py_XDECREF(todecref);
4881 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02004882 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004883 return -1;
4884 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004885
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004886 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004887 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004888 Py_DECREF(keyObject);
4889 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004890 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004891 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004892 return -1;
4893 }
4894
4895 di = dictitem_alloc(key);
4896
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004897 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02004898 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004899
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004900 if (di == NULL)
4901 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004902 Py_DECREF(iterator);
4903 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004904 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004905 PyErr_NoMemory();
4906 return -1;
4907 }
4908 di->di_tv.v_lock = 0;
4909
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004910 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004911 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004912 Py_DECREF(iterator);
4913 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004914 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004915 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004916 return -1;
4917 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02004918
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004919 Py_DECREF(valObject);
4920
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004921 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004922 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004923 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004924 dictitem_free(di);
4925 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004926 PyErr_SetVim(_("failed to add key to dictionary"));
4927 return -1;
4928 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004929 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02004930 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004931 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004932 return 0;
4933}
4934
4935 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004936pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004937{
4938 list_T *l;
4939
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004940 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004941 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004942
4943 tv->v_type = VAR_LIST;
4944 tv->vval.v_list = l;
4945
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004946 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004947 {
4948 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004949 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004950 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004951
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02004952 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004953 return 0;
4954}
4955
Bram Moolenaardb913952012-06-29 12:54:53 +02004956typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
4957
4958 static int
4959convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004960 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02004961{
4962 PyObject *capsule;
4963 char hexBuf[sizeof(void *) * 2 + 3];
4964
4965 sprintf(hexBuf, "%p", obj);
4966
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004967# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004968 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004969# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004970 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004971# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02004972 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02004973 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02004974# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02004975 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02004976# else
4977 capsule = PyCObject_FromVoidPtr(tv, NULL);
4978# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02004979 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
4980 {
4981 Py_DECREF(capsule);
4982 tv->v_type = VAR_UNKNOWN;
4983 return -1;
4984 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02004985 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02004986 {
4987 tv->v_type = VAR_UNKNOWN;
4988 return -1;
4989 }
4990 /* As we are not using copy_tv which increments reference count we must
4991 * do it ourself. */
4992 switch(tv->v_type)
4993 {
4994 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
4995 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
4996 }
4997 }
4998 else
4999 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005000 typval_T *v;
5001
5002# ifdef PY_USE_CAPSULE
5003 v = PyCapsule_GetPointer(capsule, NULL);
5004# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005005 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005006# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005007 copy_tv(v, tv);
5008 }
5009 return 0;
5010}
5011
5012 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005013ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5014{
5015 PyObject *lookup_dict;
5016 int r;
5017
5018 if (!(lookup_dict = PyDict_New()))
5019 return -1;
5020
5021 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5022 {
5023 tv->v_type = VAR_DICT;
5024 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5025 ++tv->vval.v_dict->dv_refcount;
5026 r = 0;
5027 }
5028 else if (PyDict_Check(obj))
5029 r = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
5030 else if (PyMapping_Check(obj))
5031 r = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
5032 else
5033 {
5034 PyErr_SetString(PyExc_TypeError,
5035 _("unable to convert object to vim dictionary"));
5036 r = -1;
5037 }
5038 Py_DECREF(lookup_dict);
5039 return r;
5040}
5041
5042 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005043ConvertFromPyObject(PyObject *obj, typval_T *tv)
5044{
5045 PyObject *lookup_dict;
5046 int r;
5047
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005048 if (!(lookup_dict = PyDict_New()))
5049 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005050 r = _ConvertFromPyObject(obj, tv, lookup_dict);
5051 Py_DECREF(lookup_dict);
5052 return r;
5053}
5054
5055 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005056_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005057{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005058 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005059 {
5060 tv->v_type = VAR_DICT;
5061 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5062 ++tv->vval.v_dict->dv_refcount;
5063 }
5064 else if (obj->ob_type == &ListType)
5065 {
5066 tv->v_type = VAR_LIST;
5067 tv->vval.v_list = (((ListObject *)(obj))->list);
5068 ++tv->vval.v_list->lv_refcount;
5069 }
5070 else if (obj->ob_type == &FunctionType)
5071 {
5072 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5073 return -1;
5074
5075 tv->v_type = VAR_FUNC;
5076 func_ref(tv->vval.v_string);
5077 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005078 else if (PyBytes_Check(obj))
5079 {
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005080 char_u *result;
Bram Moolenaardb913952012-06-29 12:54:53 +02005081
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005082 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
5083 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005084 if (result == NULL)
5085 return -1;
5086
5087 if (set_string_copy(result, tv) == -1)
5088 return -1;
5089
5090 tv->v_type = VAR_STRING;
5091 }
5092 else if (PyUnicode_Check(obj))
5093 {
5094 PyObject *bytes;
5095 char_u *result;
5096
Bram Moolenaardb913952012-06-29 12:54:53 +02005097 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
5098 if (bytes == NULL)
5099 return -1;
5100
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005101 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
5102 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005103 if (result == NULL)
5104 return -1;
5105
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005106 if (set_string_copy(result, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005107 {
5108 Py_XDECREF(bytes);
5109 return -1;
5110 }
5111 Py_XDECREF(bytes);
5112
5113 tv->v_type = VAR_STRING;
5114 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005115#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005116 else if (PyInt_Check(obj))
5117 {
5118 tv->v_type = VAR_NUMBER;
5119 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
5120 }
5121#endif
5122 else if (PyLong_Check(obj))
5123 {
5124 tv->v_type = VAR_NUMBER;
5125 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
5126 }
5127 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005128 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005129#ifdef FEAT_FLOAT
5130 else if (PyFloat_Check(obj))
5131 {
5132 tv->v_type = VAR_FLOAT;
5133 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5134 }
5135#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005136 else if (PyObject_HasAttrString(obj, "keys"))
5137 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005138 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005139 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005140 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005141 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005142 else
5143 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02005144 PyErr_SetString(PyExc_TypeError,
5145 _("unable to convert to vim structure"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005146 return -1;
5147 }
5148 return 0;
5149}
5150
5151 static PyObject *
5152ConvertToPyObject(typval_T *tv)
5153{
5154 if (tv == NULL)
5155 {
5156 PyErr_SetVim(_("NULL reference passed"));
5157 return NULL;
5158 }
5159 switch (tv->v_type)
5160 {
5161 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005162 return PyBytes_FromString(tv->vval.v_string == NULL
5163 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005164 case VAR_NUMBER:
5165 return PyLong_FromLong((long) tv->vval.v_number);
5166#ifdef FEAT_FLOAT
5167 case VAR_FLOAT:
5168 return PyFloat_FromDouble((double) tv->vval.v_float);
5169#endif
5170 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005171 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005172 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005173 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005174 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005175 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005176 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005177 case VAR_UNKNOWN:
5178 Py_INCREF(Py_None);
5179 return Py_None;
5180 default:
5181 PyErr_SetVim(_("internal error: invalid value type"));
5182 return NULL;
5183 }
5184}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005185
5186typedef struct
5187{
5188 PyObject_HEAD
5189} CurrentObject;
5190static PyTypeObject CurrentType;
5191
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005192#if PY_MAJOR_VERSION >= 3
5193typedef struct
5194{
5195 PyObject_HEAD
5196} FinderObject;
5197static PyTypeObject FinderType;
5198#endif
5199
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005200 static void
5201init_structs(void)
5202{
5203 vim_memset(&OutputType, 0, sizeof(OutputType));
5204 OutputType.tp_name = "vim.message";
5205 OutputType.tp_basicsize = sizeof(OutputObject);
5206 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5207 OutputType.tp_doc = "vim message object";
5208 OutputType.tp_methods = OutputMethods;
5209#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005210 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5211 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005212 OutputType.tp_alloc = call_PyType_GenericAlloc;
5213 OutputType.tp_new = call_PyType_GenericNew;
5214 OutputType.tp_free = call_PyObject_Free;
5215#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005216 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5217 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005218#endif
5219
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005220 vim_memset(&IterType, 0, sizeof(IterType));
5221 IterType.tp_name = "vim.iter";
5222 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005223 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005224 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005225 IterType.tp_iter = (getiterfunc)IterIter;
5226 IterType.tp_iternext = (iternextfunc)IterNext;
5227 IterType.tp_dealloc = (destructor)IterDestructor;
5228 IterType.tp_traverse = (traverseproc)IterTraverse;
5229 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005230
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005231 vim_memset(&BufferType, 0, sizeof(BufferType));
5232 BufferType.tp_name = "vim.buffer";
5233 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005234 BufferType.tp_dealloc = (destructor)BufferDestructor;
5235 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005236 BufferType.tp_as_sequence = &BufferAsSeq;
5237 BufferType.tp_as_mapping = &BufferAsMapping;
5238 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5239 BufferType.tp_doc = "vim buffer object";
5240 BufferType.tp_methods = BufferMethods;
5241#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005242 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005243 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005244 BufferType.tp_alloc = call_PyType_GenericAlloc;
5245 BufferType.tp_new = call_PyType_GenericNew;
5246 BufferType.tp_free = call_PyObject_Free;
5247#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005248 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005249 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005250#endif
5251
5252 vim_memset(&WindowType, 0, sizeof(WindowType));
5253 WindowType.tp_name = "vim.window";
5254 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005255 WindowType.tp_dealloc = (destructor)WindowDestructor;
5256 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005257 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005258 WindowType.tp_doc = "vim Window object";
5259 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005260 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5261 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005262#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005263 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5264 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005265 WindowType.tp_alloc = call_PyType_GenericAlloc;
5266 WindowType.tp_new = call_PyType_GenericNew;
5267 WindowType.tp_free = call_PyObject_Free;
5268#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005269 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5270 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005271#endif
5272
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005273 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5274 TabPageType.tp_name = "vim.tabpage";
5275 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005276 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5277 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005278 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5279 TabPageType.tp_doc = "vim tab page object";
5280 TabPageType.tp_methods = TabPageMethods;
5281#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005282 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005283 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5284 TabPageType.tp_new = call_PyType_GenericNew;
5285 TabPageType.tp_free = call_PyObject_Free;
5286#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005287 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005288#endif
5289
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005290 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5291 BufMapType.tp_name = "vim.bufferlist";
5292 BufMapType.tp_basicsize = sizeof(BufMapObject);
5293 BufMapType.tp_as_mapping = &BufMapAsMapping;
5294 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005295 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005296 BufferType.tp_doc = "vim buffer list";
5297
5298 vim_memset(&WinListType, 0, sizeof(WinListType));
5299 WinListType.tp_name = "vim.windowlist";
5300 WinListType.tp_basicsize = sizeof(WinListType);
5301 WinListType.tp_as_sequence = &WinListAsSeq;
5302 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5303 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005304 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005305
5306 vim_memset(&TabListType, 0, sizeof(TabListType));
5307 TabListType.tp_name = "vim.tabpagelist";
5308 TabListType.tp_basicsize = sizeof(TabListType);
5309 TabListType.tp_as_sequence = &TabListAsSeq;
5310 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5311 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005312
5313 vim_memset(&RangeType, 0, sizeof(RangeType));
5314 RangeType.tp_name = "vim.range";
5315 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005316 RangeType.tp_dealloc = (destructor)RangeDestructor;
5317 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005318 RangeType.tp_as_sequence = &RangeAsSeq;
5319 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005320 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005321 RangeType.tp_doc = "vim Range object";
5322 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005323 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5324 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005325#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005326 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005327 RangeType.tp_alloc = call_PyType_GenericAlloc;
5328 RangeType.tp_new = call_PyType_GenericNew;
5329 RangeType.tp_free = call_PyObject_Free;
5330#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005331 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005332#endif
5333
5334 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5335 CurrentType.tp_name = "vim.currentdata";
5336 CurrentType.tp_basicsize = sizeof(CurrentObject);
5337 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5338 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005339 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005340#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005341 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5342 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005343#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005344 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5345 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005346#endif
5347
5348 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5349 DictionaryType.tp_name = "vim.dictionary";
5350 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005351 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005352 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005353 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005354 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005355 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5356 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005357 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5358 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5359 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005360#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005361 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5362 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005363#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005364 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5365 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005366#endif
5367
5368 vim_memset(&ListType, 0, sizeof(ListType));
5369 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005370 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005371 ListType.tp_basicsize = sizeof(ListObject);
5372 ListType.tp_as_sequence = &ListAsSeq;
5373 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005374 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005375 ListType.tp_doc = "list pushing modifications to vim structure";
5376 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005377 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005378 ListType.tp_new = (newfunc)ListConstructor;
5379 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005380#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005381 ListType.tp_getattro = (getattrofunc)ListGetattro;
5382 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005383#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005384 ListType.tp_getattr = (getattrfunc)ListGetattr;
5385 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005386#endif
5387
5388 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005389 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005390 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005391 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5392 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005393 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005394 FunctionType.tp_doc = "object that calls vim function";
5395 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005396 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005397 FunctionType.tp_new = (newfunc)FunctionConstructor;
5398 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005399#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005400 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005401#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005402 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005403#endif
5404
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005405 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5406 OptionsType.tp_name = "vim.options";
5407 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005408 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005409 OptionsType.tp_doc = "object for manipulating options";
5410 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005411 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5412 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5413 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005414
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005415#if PY_MAJOR_VERSION >= 3
5416 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5417 vimmodule.m_name = "vim";
5418 vimmodule.m_doc = "Vim Python interface\n";
5419 vimmodule.m_size = -1;
5420 vimmodule.m_methods = VimMethods;
5421#endif
5422}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005423
5424#define PYTYPE_READY(type) \
5425 if (PyType_Ready(&type)) \
5426 return -1;
5427
5428 static int
5429init_types()
5430{
5431 PYTYPE_READY(IterType);
5432 PYTYPE_READY(BufferType);
5433 PYTYPE_READY(RangeType);
5434 PYTYPE_READY(WindowType);
5435 PYTYPE_READY(TabPageType);
5436 PYTYPE_READY(BufMapType);
5437 PYTYPE_READY(WinListType);
5438 PYTYPE_READY(TabListType);
5439 PYTYPE_READY(CurrentType);
5440 PYTYPE_READY(DictionaryType);
5441 PYTYPE_READY(ListType);
5442 PYTYPE_READY(FunctionType);
5443 PYTYPE_READY(OptionsType);
5444 PYTYPE_READY(OutputType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005445#if PY_MAJOR_VERSION >= 3
5446 PYTYPE_READY(FinderType);
5447#endif
5448 return 0;
5449}
5450
5451 static int
5452init_sys_path()
5453{
5454 PyObject *path;
5455 PyObject *path_hook;
5456 PyObject *path_hooks;
5457
5458 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5459 return -1;
5460
5461 if (!(path_hooks = PySys_GetObject("path_hooks")))
5462 {
5463 PyErr_Clear();
5464 path_hooks = PyList_New(1);
5465 PyList_SET_ITEM(path_hooks, 0, path_hook);
5466 if (PySys_SetObject("path_hooks", path_hooks))
5467 {
5468 Py_DECREF(path_hooks);
5469 return -1;
5470 }
5471 Py_DECREF(path_hooks);
5472 }
5473 else if (PyList_Check(path_hooks))
5474 {
5475 if (PyList_Append(path_hooks, path_hook))
5476 {
5477 Py_DECREF(path_hook);
5478 return -1;
5479 }
5480 Py_DECREF(path_hook);
5481 }
5482 else
5483 {
5484 VimTryStart();
5485 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5486 "You should now do the following:\n"
5487 "- append vim.path_hook to sys.path_hooks\n"
5488 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5489 VimTryEnd(); /* Discard the error */
5490 Py_DECREF(path_hook);
5491 return 0;
5492 }
5493
5494 if (!(path = PySys_GetObject("path")))
5495 {
5496 PyErr_Clear();
5497 path = PyList_New(1);
5498 Py_INCREF(vim_special_path_object);
5499 PyList_SET_ITEM(path, 0, vim_special_path_object);
5500 if (PySys_SetObject("path", path))
5501 {
5502 Py_DECREF(path);
5503 return -1;
5504 }
5505 Py_DECREF(path);
5506 }
5507 else if (PyList_Check(path))
5508 {
5509 if (PyList_Append(path, vim_special_path_object))
5510 return -1;
5511 }
5512 else
5513 {
5514 VimTryStart();
5515 EMSG(_("Failed to set path: sys.path is not a list\n"
5516 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5517 VimTryEnd(); /* Discard the error */
5518 }
5519
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005520 return 0;
5521}
5522
5523static BufMapObject TheBufferMap =
5524{
5525 PyObject_HEAD_INIT(&BufMapType)
5526};
5527
5528static WinListObject TheWindowList =
5529{
5530 PyObject_HEAD_INIT(&WinListType)
5531 NULL
5532};
5533
5534static CurrentObject TheCurrent =
5535{
5536 PyObject_HEAD_INIT(&CurrentType)
5537};
5538
5539static TabListObject TheTabPageList =
5540{
5541 PyObject_HEAD_INIT(&TabListType)
5542};
5543
5544static struct numeric_constant {
5545 char *name;
5546 int value;
5547} numeric_constants[] = {
5548 {"VAR_LOCKED", VAR_LOCKED},
5549 {"VAR_FIXED", VAR_FIXED},
5550 {"VAR_SCOPE", VAR_SCOPE},
5551 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5552};
5553
5554static struct object_constant {
5555 char *name;
5556 PyObject *value;
5557} object_constants[] = {
5558 {"buffers", (PyObject *)(void *)&TheBufferMap},
5559 {"windows", (PyObject *)(void *)&TheWindowList},
5560 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5561 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005562
5563 {"Buffer", (PyObject *)&BufferType},
5564 {"Range", (PyObject *)&RangeType},
5565 {"Window", (PyObject *)&WindowType},
5566 {"TabPage", (PyObject *)&TabPageType},
5567 {"Dictionary", (PyObject *)&DictionaryType},
5568 {"List", (PyObject *)&ListType},
5569 {"Function", (PyObject *)&FunctionType},
5570 {"Options", (PyObject *)&OptionsType},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005571#if PY_MAJOR_VERSION >= 3
5572 {"Finder", (PyObject *)&FinderType},
5573#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005574};
5575
5576typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005577typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005578
5579#define ADD_OBJECT(m, name, obj) \
5580 if (add_object(m, name, obj)) \
5581 return -1;
5582
5583#define ADD_CHECKED_OBJECT(m, name, obj) \
5584 { \
5585 PyObject *value = obj; \
5586 if (!value) \
5587 return -1; \
5588 ADD_OBJECT(m, name, value); \
5589 }
5590
5591 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005592populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005593{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005594 int i;
5595 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005596 PyObject *attr;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005597
5598 for (i = 0; i < (int)(sizeof(numeric_constants)
5599 / sizeof(struct numeric_constant));
5600 ++i)
5601 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
5602 PyInt_FromLong(numeric_constants[i].value));
5603
5604 for (i = 0; i < (int)(sizeof(object_constants)
5605 / sizeof(struct object_constant));
5606 ++i)
5607 {
5608 PyObject *value;
5609
5610 value = object_constants[i].value;
5611 Py_INCREF(value);
5612 ADD_OBJECT(m, object_constants[i].name, value);
5613 }
5614
5615 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5616 return -1;
5617 ADD_OBJECT(m, "error", VimError);
5618
Bram Moolenaara9922d62013-05-30 13:01:18 +02005619 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5620 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005621 ADD_CHECKED_OBJECT(m, "options",
5622 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005623
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005624 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005625 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005626 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005627
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005628 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005629 return -1;
5630 ADD_OBJECT(m, "_getcwd", py_getcwd)
5631
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005632 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005633 return -1;
5634 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005635 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005636 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005637 if (PyObject_SetAttrString(other_module, "chdir", attr))
5638 {
5639 Py_DECREF(attr);
5640 return -1;
5641 }
5642 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005643
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005644 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005645 {
5646 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005647 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005648 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005649 if (PyObject_SetAttrString(other_module, "fchdir", attr))
5650 {
5651 Py_DECREF(attr);
5652 return -1;
5653 }
5654 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005655 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005656 else
5657 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005658
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005659 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
5660 return -1;
5661
5662 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
5663
5664#if PY_MAJOR_VERSION >= 3
5665 ADD_OBJECT(m, "_PathFinder", path_finder);
5666 ADD_CHECKED_OBJECT(m, "_find_module",
5667 (py_find_module = PyObject_GetAttrString(path_finder,
5668 "find_module")));
5669#endif
5670
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005671 return 0;
5672}