blob: 7c205f8c4f1c9c7a141883ab463efe3e709ec613 [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 Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020075static PyObject *WindowNew(win_T *, tabpage_T *);
76static PyObject *BufferNew (buf_T *);
77static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020078
79static PyInt RangeStart;
80static PyInt RangeEnd;
81
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020082static PyObject *globals;
83
Bram Moolenaarf4258302013-06-02 18:20:17 +020084static PyObject *py_chdir;
85static PyObject *py_fchdir;
86static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020087static PyObject *vim_module;
88static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020089
Bram Moolenaar81c40c52013-06-12 14:41:04 +020090static PyObject *py_find_module;
91static PyObject *py_load_module;
92
93static PyObject *VimError;
94
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020095/*
96 * obtain a lock on the Vim data structures
97 */
98 static void
99Python_Lock_Vim(void)
100{
101}
102
103/*
104 * release a lock on the Vim data structures
105 */
106 static void
107Python_Release_Vim(void)
108{
109}
110
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200111/*
112 * The "todecref" argument holds a pointer to PyObject * that must be
113 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
114 * was needed to generate returned value is object.
115 *
116 * Use Py_XDECREF to decrement reference count.
117 */
118 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200119StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200121 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200124 {
125
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
127 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 return NULL;
129
130 *todecref = NULL;
131 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200132 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200134 PyObject *bytes;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137 return NULL;
138
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200139 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
140 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 {
142 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200143 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200144 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200145
146 *todecref = bytes;
147 }
148 else
149 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200151 PyErr_FORMAT(PyExc_TypeError,
152 N_("expected str() or unicode() instance, but got %s"),
153 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200154#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200155 PyErr_FORMAT(PyExc_TypeError,
156 N_("expected bytes() or str() instance, but got %s"),
157 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200158#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200159 return NULL;
160 }
161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200162 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200163}
164
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200165#define NUMBER_LONG 1
166#define NUMBER_INT 2
167#define NUMBER_NATURAL 4
168#define NUMBER_UNSIGNED 8
169
170 static int
171NumberToLong(PyObject *obj, long *result, int flags)
172{
173#if PY_MAJOR_VERSION < 3
174 if (PyInt_Check(obj))
175 {
176 *result = PyInt_AsLong(obj);
177 if (PyErr_Occurred())
178 return -1;
179 }
180 else
181#endif
182 if (PyLong_Check(obj))
183 {
184 *result = PyLong_AsLong(obj);
185 if (PyErr_Occurred())
186 return -1;
187 }
188 else if (PyNumber_Check(obj))
189 {
190 PyObject *num;
191
192 if (!(num = PyNumber_Long(obj)))
193 return -1;
194
195 *result = PyLong_AsLong(num);
196
197 Py_DECREF(num);
198
199 if (PyErr_Occurred())
200 return -1;
201 }
202 else
203 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200204#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200205 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200206 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 "coercing to long(), but got %s"),
208 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200212 "but got %s"),
213 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215 return -1;
216 }
217
218 if (flags & NUMBER_INT)
219 {
220 if (*result > INT_MAX)
221 {
222 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200223 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200224 return -1;
225 }
226 else if (*result < INT_MIN)
227 {
228 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200229 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200230 return -1;
231 }
232 }
233
234 if (flags & NUMBER_NATURAL)
235 {
236 if (*result <= 0)
237 {
238 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200239 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200240 return -1;
241 }
242 }
243 else if (flags & NUMBER_UNSIGNED)
244 {
245 if (*result < 0)
246 {
247 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200248 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200249 return -1;
250 }
251 }
252
253 return 0;
254}
255
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200256 static int
257add_string(PyObject *list, char *s)
258{
259 PyObject *string;
260
261 if (!(string = PyString_FromString(s)))
262 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200263
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200264 if (PyList_Append(list, string))
265 {
266 Py_DECREF(string);
267 return -1;
268 }
269
270 Py_DECREF(string);
271 return 0;
272}
273
274 static PyObject *
275ObjectDir(PyObject *self, char **attributes)
276{
277 PyMethodDef *method;
278 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200279 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200281 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200282 return NULL;
283
284 if (self)
285 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200286 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200288 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200289 return NULL;
290 }
291
292 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200295 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200296 return NULL;
297 }
298
299#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200302 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200303 return NULL;
304 }
305#endif
306
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308}
309
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200310/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 */
312
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200313/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315
316static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317
318typedef struct
319{
320 PyObject_HEAD
321 long softspace;
322 long error;
323} OutputObject;
324
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200325static char *OutputAttrs[] = {
326 "softspace",
327 NULL
328};
329
330 static PyObject *
331OutputDir(PyObject *self)
332{
333 return ObjectDir(self, OutputAttrs);
334}
335
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200337OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200339 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200341 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200342 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200343 return -1;
344 }
345
346 if (strcmp(name, "softspace") == 0)
347 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200348 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return 0;
351 }
352
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200353 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
355}
356
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200357/* Buffer IO, we write one whole line at a time. */
358static garray_T io_ga = {0, 0, 1, 80, NULL};
359static writefn old_fn = NULL;
360
361 static void
362PythonIO_Flush(void)
363{
364 if (old_fn != NULL && io_ga.ga_len > 0)
365 {
366 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
367 old_fn((char_u *)io_ga.ga_data);
368 }
369 io_ga.ga_len = 0;
370}
371
372 static void
373writer(writefn fn, char_u *str, PyInt n)
374{
375 char_u *ptr;
376
377 /* Flush when switching output function. */
378 if (fn != old_fn)
379 PythonIO_Flush();
380 old_fn = fn;
381
382 /* Write each NL separated line. Text after the last NL is kept for
383 * writing later. */
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
399 /* Put the remaining text into io_ga for later printing. */
400 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
401 {
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 io_ga.ga_len += (int)n;
404 }
405}
406
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200407 static int
408write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 Py_ssize_t len = 0;
411 char *str = NULL;
412 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200413
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200416
417 Py_BEGIN_ALLOW_THREADS
418 Python_Lock_Vim();
419 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
420 Python_Release_Vim();
421 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200422 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200424 return 0;
425}
426
427 static PyObject *
428OutputWrite(OutputObject *self, PyObject *string)
429{
430 if (write_output(self, string))
431 return NULL;
432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433 Py_INCREF(Py_None);
434 return Py_None;
435}
436
437 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200438OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200440 PyObject *iterator;
441 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200448 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200450 Py_DECREF(iterator);
451 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 return NULL;
453 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 }
456
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(iterator);
458
459 /* Iterator may have finished due to an exception */
460 if (PyErr_Occurred())
461 return NULL;
462
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463 Py_INCREF(Py_None);
464 return Py_None;
465}
466
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100467 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200468OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100469{
470 /* do nothing */
471 Py_INCREF(Py_None);
472 return Py_None;
473}
474
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200475/***************/
476
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200477static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200478 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200479 {"write", (PyCFunction)OutputWrite, METH_O, ""},
480 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200481 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200482 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200483 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200484};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200485
486static OutputObject Output =
487{
488 PyObject_HEAD_INIT(&OutputType)
489 0,
490 0
491};
492
493static OutputObject Error =
494{
495 PyObject_HEAD_INIT(&OutputType)
496 0,
497 1
498};
499
500 static int
501PythonIO_Init_io(void)
502{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200503 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
504 return -1;
505 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
506 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200507
508 if (PyErr_Occurred())
509 {
510 EMSG(_("E264: Python: Error initialising I/O objects"));
511 return -1;
512 }
513
514 return 0;
515}
516
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200517typedef struct
518{
519 PyObject_HEAD
520 PyObject *module;
521} LoaderObject;
522static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200523
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200524 static void
525LoaderDestructor(LoaderObject *self)
526{
527 Py_DECREF(self->module);
528 DESTRUCTOR_FINISH(self);
529}
530
531 static PyObject *
532LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
533{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200534 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200536 Py_INCREF(ret);
537 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200538}
539
540static struct PyMethodDef LoaderMethods[] = {
541 /* name, function, calling, doc */
542 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
543 { NULL, NULL, 0, NULL}
544};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200545
546/* Check to see whether a Vim error has been reported, or a keyboard
547 * interrupt has been detected.
548 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200549
550 static void
551VimTryStart(void)
552{
553 ++trylevel;
554}
555
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200557VimTryEnd(void)
558{
559 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200560 /* Without this it stops processing all subsequent VimL commands and
561 * generates strange error messages if I e.g. try calling Test() in a
562 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563 did_emsg = FALSE;
564 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200565 if (got_int)
566 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100567 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100568 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100569 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200570 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200571 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100573 else if (msg_list != NULL && *msg_list != NULL)
574 {
575 int should_free;
576 char_u *msg;
577
578 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
579
580 if (msg == NULL)
581 {
582 PyErr_NoMemory();
583 return -1;
584 }
585
586 PyErr_SetVim((char *) msg);
587
588 free_global_msglist();
589
590 if (should_free)
591 vim_free(msg);
592
593 return -1;
594 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200596 return (PyErr_Occurred() ? -1 : 0);
597 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200598 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200599 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100600 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200601 return -1;
602 }
603 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200604 else
605 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200606 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200607 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200608 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 }
610}
611
612 static int
613VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200614{
615 if (got_int)
616 {
617 PyErr_SetNone(PyExc_KeyboardInterrupt);
618 return 1;
619 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200620 return 0;
621}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200622
623/* Vim module - Implementation
624 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200627VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200629 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200630 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200631 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632
Bram Moolenaar389a1792013-06-23 13:00:44 +0200633 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200634 return NULL;
635
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200636 Py_BEGIN_ALLOW_THREADS
637 Python_Lock_Vim();
638
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200639 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200640 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200641 update_screen(VALID);
642
643 Python_Release_Vim();
644 Py_END_ALLOW_THREADS
645
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200646 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200647 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200648 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200649 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200651 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200653 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654}
655
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656/*
657 * Function to translate a typval_T into a PyObject; this will recursively
658 * translate lists/dictionaries into their Python equivalents.
659 *
660 * The depth parameter is to avoid infinite recursion, set it to 1 when
661 * you call VimToPython.
662 */
663 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200664VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200666 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200667 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200668 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200669
670 /* Avoid infinite recursion */
671 if (depth > 100)
672 {
673 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 ret = Py_None;
675 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200676 }
677
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200678 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679 * then and we can use it again. */
680 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
681 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
682 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200683 sprintf(ptrBuf, "%p",
684 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
685 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200686
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200687 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 Py_INCREF(ret);
690 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 }
692 }
693
694 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200695 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200696 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 else if (our_tv->v_type == VAR_NUMBER)
698 {
699 char buf[NUMBUFLEN];
700
701 /* For backwards compatibility numbers are stored as strings. */
702 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200703 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 }
705# ifdef FEAT_FLOAT
706 else if (our_tv->v_type == VAR_FLOAT)
707 {
708 char buf[NUMBUFLEN];
709
710 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200711 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712 }
713# endif
714 else if (our_tv->v_type == VAR_LIST)
715 {
716 list_T *list = our_tv->vval.v_list;
717 listitem_T *curr;
718
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200719 if (list == NULL)
720 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200722 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200723 return NULL;
724
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
729 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200731 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
732 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200733 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200735 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200736 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200738 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200739 {
740 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200741 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 return NULL;
743 }
744 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 }
746 }
747 else if (our_tv->v_type == VAR_DICT)
748 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200750 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
751 long_u todo = ht->ht_used;
752 hashitem_T *hi;
753 dictitem_T *di;
754 if (our_tv->vval.v_dict == NULL)
755 return NULL;
756
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200757 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200758 return NULL;
759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 return NULL;
764 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200766 for (hi = ht->ht_array; todo > 0; ++hi)
767 {
768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200770 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200772 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200773 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200774 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200775 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200777 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200778 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200780 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781 Py_DECREF(newObj);
782 return NULL;
783 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200784 }
785 }
786 }
787 else
788 {
789 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200790 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 }
792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795
796 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200797VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200799 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200801 PyObject *string;
802 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200804 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805
Bram Moolenaar389a1792013-06-23 13:00:44 +0200806 if (!PyArg_ParseTuple(args, "O", &string))
807 return NULL;
808
809 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810 return NULL;
811
812 Py_BEGIN_ALLOW_THREADS
813 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200814 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200815 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816 Python_Release_Vim();
817 Py_END_ALLOW_THREADS
818
Bram Moolenaar389a1792013-06-23 13:00:44 +0200819 Py_XDECREF(todecref);
820
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200821 if (VimTryEnd())
822 return NULL;
823
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824 if (our_tv == NULL)
825 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200826 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827 return NULL;
828 }
829
830 /* Convert the Vim type into a Python type. Create a dictionary that's
831 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200832 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200833 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200834 else
835 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200837 Py_DECREF(lookup_dict);
838 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839
840
841 Py_BEGIN_ALLOW_THREADS
842 Python_Lock_Vim();
843 free_tv(our_tv);
844 Python_Release_Vim();
845 Py_END_ALLOW_THREADS
846
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200847 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848}
849
Bram Moolenaardb913952012-06-29 12:54:53 +0200850static PyObject *ConvertToPyObject(typval_T *);
851
852 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200853VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200854{
Bram Moolenaardb913952012-06-29 12:54:53 +0200855 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200856 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200857 char_u *expr;
858 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200859
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200861 return NULL;
862
863 Py_BEGIN_ALLOW_THREADS
864 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200865 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200866 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200867 Python_Release_Vim();
868 Py_END_ALLOW_THREADS
869
Bram Moolenaar389a1792013-06-23 13:00:44 +0200870 Py_XDECREF(todecref);
871
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200872 if (VimTryEnd())
873 return NULL;
874
Bram Moolenaardb913952012-06-29 12:54:53 +0200875 if (our_tv == NULL)
876 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200877 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200878 return NULL;
879 }
880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200882 Py_BEGIN_ALLOW_THREADS
883 Python_Lock_Vim();
884 free_tv(our_tv);
885 Python_Release_Vim();
886 Py_END_ALLOW_THREADS
887
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200888 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200889}
890
891 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200892VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200893{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200894 char_u *str;
895 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200896 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200897
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200899 return NULL;
900
Bram Moolenaara54bf402012-12-05 16:30:07 +0100901#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200902 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100903#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200904 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100905#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906
907 Py_XDECREF(todecref);
908
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200910}
911
Bram Moolenaarf4258302013-06-02 18:20:17 +0200912 static PyObject *
913_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
914{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200915 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200916 PyObject *newwd;
917 PyObject *todecref;
918 char_u *new_dir;
919
Bram Moolenaard4209d22013-06-05 20:34:15 +0200920 if (_chdir == NULL)
921 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200922 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200923 return NULL;
924
925 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
926 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928 return NULL;
929 }
930
931 if (!(new_dir = StringToChars(newwd, &todecref)))
932 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200933 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200934 Py_DECREF(newwd);
935 return NULL;
936 }
937
938 VimTryStart();
939
940 if (vim_chdir(new_dir))
941 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200942 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200943 Py_DECREF(newwd);
944 Py_XDECREF(todecref);
945
946 if (VimTryEnd())
947 return NULL;
948
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200949 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200950 return NULL;
951 }
952
953 Py_DECREF(newwd);
954 Py_XDECREF(todecref);
955
956 post_chdir(FALSE);
957
958 if (VimTryEnd())
959 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200960 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200961 return NULL;
962 }
963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200965}
966
967 static PyObject *
968VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
969{
970 return _VimChdir(py_chdir, args, kwargs);
971}
972
973 static PyObject *
974VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
975{
976 return _VimChdir(py_fchdir, args, kwargs);
977}
978
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200979typedef struct {
980 PyObject *callable;
981 PyObject *result;
982} map_rtp_data;
983
984 static void
985map_rtp_callback(char_u *path, void *_data)
986{
987 void **data = (void **) _data;
988 PyObject *pathObject;
989 map_rtp_data *mr_data = *((map_rtp_data **) data);
990
Bram Moolenaar41009372013-07-01 22:03:04 +0200991 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200992 {
993 *data = NULL;
994 return;
995 }
996
997 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
998 pathObject, NULL);
999
1000 Py_DECREF(pathObject);
1001
1002 if (!mr_data->result || mr_data->result != Py_None)
1003 *data = NULL;
1004 else
1005 {
1006 Py_DECREF(mr_data->result);
1007 mr_data->result = NULL;
1008 }
1009}
1010
1011 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001012VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001013{
1014 map_rtp_data data;
1015
Bram Moolenaar389a1792013-06-23 13:00:44 +02001016 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001017 data.result = NULL;
1018
1019 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1020
1021 if (data.result == NULL)
1022 {
1023 if (PyErr_Occurred())
1024 return NULL;
1025 else
1026 {
1027 Py_INCREF(Py_None);
1028 return Py_None;
1029 }
1030 }
1031 return data.result;
1032}
1033
1034/*
1035 * _vim_runtimepath_ special path implementation.
1036 */
1037
1038 static void
1039map_finder_callback(char_u *path, void *_data)
1040{
1041 void **data = (void **) _data;
1042 PyObject *list = *((PyObject **) data);
1043 PyObject *pathObject1, *pathObject2;
1044 char *pathbuf;
1045 size_t pathlen;
1046
1047 pathlen = STRLEN(path);
1048
1049#if PY_MAJOR_VERSION < 3
1050# define PY_MAIN_DIR_STRING "python2"
1051#else
1052# define PY_MAIN_DIR_STRING "python3"
1053#endif
1054#define PY_ALTERNATE_DIR_STRING "pythonx"
1055
1056#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1057 if (!(pathbuf = PyMem_New(char,
1058 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1059 {
1060 PyErr_NoMemory();
1061 *data = NULL;
1062 return;
1063 }
1064
1065 mch_memmove(pathbuf, path, pathlen + 1);
1066 add_pathsep((char_u *) pathbuf);
1067
1068 pathlen = STRLEN(pathbuf);
1069 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1070 PYTHONX_STRING_LENGTH + 1);
1071
1072 if (!(pathObject1 = PyString_FromString(pathbuf)))
1073 {
1074 *data = NULL;
1075 PyMem_Free(pathbuf);
1076 return;
1077 }
1078
1079 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1080 PYTHONX_STRING_LENGTH + 1);
1081
1082 if (!(pathObject2 = PyString_FromString(pathbuf)))
1083 {
1084 Py_DECREF(pathObject1);
1085 PyMem_Free(pathbuf);
1086 *data = NULL;
1087 return;
1088 }
1089
1090 PyMem_Free(pathbuf);
1091
1092 if (PyList_Append(list, pathObject1)
1093 || PyList_Append(list, pathObject2))
1094 *data = NULL;
1095
1096 Py_DECREF(pathObject1);
1097 Py_DECREF(pathObject2);
1098}
1099
1100 static PyObject *
1101Vim_GetPaths(PyObject *self UNUSED)
1102{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001103 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001104
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001105 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001106 return NULL;
1107
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001108 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001109
1110 if (PyErr_Occurred())
1111 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001112 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001113 return NULL;
1114 }
1115
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001116 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117}
1118
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001119 static PyObject *
1120call_load_module(char *name, int len, PyObject *find_module_result)
1121{
1122 PyObject *fd, *pathname, *description;
1123
Bram Moolenaarc476e522013-06-23 13:46:40 +02001124 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001125 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001126 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001127 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001128 Py_TYPE_NAME(find_module_result));
1129 return NULL;
1130 }
1131 if (PyTuple_GET_SIZE(find_module_result) != 3)
1132 {
1133 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001134 N_("expected 3-tuple as imp.find_module() result, but got "
1135 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001136 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001137 return NULL;
1138 }
1139
1140 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1141 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1142 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1143 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001144 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001145 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001146 return NULL;
1147 }
1148
1149 return PyObject_CallFunction(py_load_module,
1150 "s#OOO", name, len, fd, pathname, description);
1151}
1152
1153 static PyObject *
1154find_module(char *fullname, char *tail, PyObject *new_path)
1155{
1156 PyObject *find_module_result;
1157 PyObject *module;
1158 char *dot;
1159
Bram Moolenaar41009372013-07-01 22:03:04 +02001160 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001161 {
1162 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001163 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001164 * first component
1165 */
1166 PyObject *newest_path;
1167 int partlen = (int) (dot - 1 - tail);
1168
1169 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1170 "s#O", tail, partlen, new_path)))
1171 return NULL;
1172
1173 if (!(module = call_load_module(
1174 fullname,
1175 ((int) (tail - fullname)) + partlen,
1176 find_module_result)))
1177 {
1178 Py_DECREF(find_module_result);
1179 return NULL;
1180 }
1181
1182 Py_DECREF(find_module_result);
1183
1184 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1185 {
1186 Py_DECREF(module);
1187 return NULL;
1188 }
1189
1190 Py_DECREF(module);
1191
1192 module = find_module(fullname, dot + 1, newest_path);
1193
1194 Py_DECREF(newest_path);
1195
1196 return module;
1197 }
1198 else
1199 {
1200 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1201 "sO", tail, new_path)))
1202 return NULL;
1203
1204 if (!(module = call_load_module(
1205 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001206 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001207 find_module_result)))
1208 {
1209 Py_DECREF(find_module_result);
1210 return NULL;
1211 }
1212
1213 Py_DECREF(find_module_result);
1214
1215 return module;
1216 }
1217}
1218
1219 static PyObject *
1220FinderFindModule(PyObject *self, PyObject *args)
1221{
1222 char *fullname;
1223 PyObject *module;
1224 PyObject *new_path;
1225 LoaderObject *loader;
1226
1227 if (!PyArg_ParseTuple(args, "s", &fullname))
1228 return NULL;
1229
1230 if (!(new_path = Vim_GetPaths(self)))
1231 return NULL;
1232
1233 module = find_module(fullname, fullname, new_path);
1234
1235 Py_DECREF(new_path);
1236
1237 if (!module)
1238 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001239 if (PyErr_Occurred())
1240 {
1241 if (PyErr_ExceptionMatches(PyExc_ImportError))
1242 PyErr_Clear();
1243 else
1244 return NULL;
1245 }
1246
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001247 Py_INCREF(Py_None);
1248 return Py_None;
1249 }
1250
1251 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1252 {
1253 Py_DECREF(module);
1254 return NULL;
1255 }
1256
1257 loader->module = module;
1258
1259 return (PyObject *) loader;
1260}
1261
1262 static PyObject *
1263VimPathHook(PyObject *self UNUSED, PyObject *args)
1264{
1265 char *path;
1266
1267 if (PyArg_ParseTuple(args, "s", &path)
1268 && STRCMP(path, vim_special_path) == 0)
1269 {
1270 Py_INCREF(vim_module);
1271 return vim_module;
1272 }
1273
1274 PyErr_Clear();
1275 PyErr_SetNone(PyExc_ImportError);
1276 return NULL;
1277}
1278
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001279/*
1280 * Vim module - Definitions
1281 */
1282
1283static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001284 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001285 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001286 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001287 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1288 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001289 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1290 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001291 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001292 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001293 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1294 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1295 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001296};
1297
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001298/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001299 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001300 */
1301
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001302static PyTypeObject IterType;
1303
1304typedef PyObject *(*nextfun)(void **);
1305typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001306typedef int (*traversefun)(void *, visitproc, void *);
1307typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001308
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001309/* Main purpose of this object is removing the need for do python
1310 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1311 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001312
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001313typedef struct
1314{
1315 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001316 void *cur;
1317 nextfun next;
1318 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001319 traversefun traverse;
1320 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001321} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001322
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001323 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1325 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001327 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001328
Bram Moolenaar774267b2013-05-21 20:51:59 +02001329 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001330 self->cur = start;
1331 self->next = next;
1332 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001333 self->traverse = traverse;
1334 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001335
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001336 return (PyObject *)(self);
1337}
1338
1339 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001340IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001341{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001342 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001343 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001344 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345}
1346
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001347 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001349{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001350 if (self->traverse != NULL)
1351 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352 else
1353 return 0;
1354}
1355
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001356/* Mac OSX defines clear() somewhere. */
1357#ifdef clear
1358# undef clear
1359#endif
1360
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001361 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001362IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001363{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001364 if (self->clear != NULL)
1365 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 else
1367 return 0;
1368}
1369
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001370 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001371IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001372{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001373 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001374}
1375
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001376 static PyObject *
1377IterIter(PyObject *self)
1378{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001379 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001380 return self;
1381}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001382
Bram Moolenaardb913952012-06-29 12:54:53 +02001383typedef struct pylinkedlist_S {
1384 struct pylinkedlist_S *pll_next;
1385 struct pylinkedlist_S *pll_prev;
1386 PyObject *pll_obj;
1387} pylinkedlist_T;
1388
1389static pylinkedlist_T *lastdict = NULL;
1390static pylinkedlist_T *lastlist = NULL;
1391
1392 static void
1393pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1394{
1395 if (ref->pll_prev == NULL)
1396 {
1397 if (ref->pll_next == NULL)
1398 {
1399 *last = NULL;
1400 return;
1401 }
1402 }
1403 else
1404 ref->pll_prev->pll_next = ref->pll_next;
1405
1406 if (ref->pll_next == NULL)
1407 *last = ref->pll_prev;
1408 else
1409 ref->pll_next->pll_prev = ref->pll_prev;
1410}
1411
1412 static void
1413pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1414{
1415 if (*last == NULL)
1416 ref->pll_prev = NULL;
1417 else
1418 {
1419 (*last)->pll_next = ref;
1420 ref->pll_prev = *last;
1421 }
1422 ref->pll_next = NULL;
1423 ref->pll_obj = self;
1424 *last = ref;
1425}
1426
1427static PyTypeObject DictionaryType;
1428
1429typedef struct
1430{
1431 PyObject_HEAD
1432 dict_T *dict;
1433 pylinkedlist_T ref;
1434} DictionaryObject;
1435
Bram Moolenaara9922d62013-05-30 13:01:18 +02001436static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1437
1438#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1439
Bram Moolenaardb913952012-06-29 12:54:53 +02001440 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001441DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001442{
1443 DictionaryObject *self;
1444
Bram Moolenaara9922d62013-05-30 13:01:18 +02001445 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001446 if (self == NULL)
1447 return NULL;
1448 self->dict = dict;
1449 ++dict->dv_refcount;
1450
1451 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1452
1453 return (PyObject *)(self);
1454}
1455
Bram Moolenaara9922d62013-05-30 13:01:18 +02001456 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001457py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001458{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001459 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001461 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001462 {
1463 PyErr_NoMemory();
1464 return NULL;
1465 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001467
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001468 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001469}
1470
1471 static PyObject *
1472DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1473{
1474 DictionaryObject *self;
1475 dict_T *dict;
1476
1477 if (!(dict = py_dict_alloc()))
1478 return NULL;
1479
1480 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1481
1482 --dict->dv_refcount;
1483
1484 if (kwargs || PyTuple_Size(args))
1485 {
1486 PyObject *tmp;
1487 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1488 {
1489 Py_DECREF(self);
1490 return NULL;
1491 }
1492
1493 Py_DECREF(tmp);
1494 }
1495
1496 return (PyObject *)(self);
1497}
1498
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001499 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001500DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001501{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502 pyll_remove(&self->ref, &lastdict);
1503 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504
1505 DESTRUCTOR_FINISH(self);
1506}
1507
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001508static char *DictionaryAttrs[] = {
1509 "locked", "scope",
1510 NULL
1511};
1512
1513 static PyObject *
1514DictionaryDir(PyObject *self)
1515{
1516 return ObjectDir(self, DictionaryAttrs);
1517}
1518
Bram Moolenaardb913952012-06-29 12:54:53 +02001519 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001520DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001521{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001522 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001523 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001524 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001525 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001526 return -1;
1527 }
1528
1529 if (strcmp(name, "locked") == 0)
1530 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001531 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001532 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001533 PyErr_SET_STRING(PyExc_TypeError,
1534 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001535 return -1;
1536 }
1537 else
1538 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001539 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001540 if (istrue == -1)
1541 return -1;
1542 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001543 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001544 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001545 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001546 }
1547 return 0;
1548 }
1549 else
1550 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001551 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001552 return -1;
1553 }
1554}
1555
1556 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001557DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001558{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001559 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001560}
1561
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562#define DICT_FLAG_HAS_DEFAULT 0x01
1563#define DICT_FLAG_POP 0x02
1564#define DICT_FLAG_NONE_DEFAULT 0x04
1565#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1566#define DICT_FLAG_RETURN_PAIR 0x10
1567
Bram Moolenaardb913952012-06-29 12:54:53 +02001568 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001569_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001570{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001571 PyObject *keyObject;
1572 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001573 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001574 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001575 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 dict_T *dict = self->dict;
1577 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001578 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001579
Bram Moolenaara9922d62013-05-30 13:01:18 +02001580 if (flags & DICT_FLAG_HAS_DEFAULT)
1581 {
1582 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1583 return NULL;
1584 }
1585 else
1586 keyObject = args;
1587
1588 if (flags & DICT_FLAG_RETURN_BOOL)
1589 defObject = Py_False;
1590
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001591 if (!(key = StringToChars(keyObject, &todecref)))
1592 return NULL;
1593
1594 if (*key == NUL)
1595 {
1596 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001597 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001598 return NULL;
1599 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001600
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001602
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001603 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001604
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001606 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001607 if (defObject)
1608 {
1609 Py_INCREF(defObject);
1610 return defObject;
1611 }
1612 else
1613 {
1614 PyErr_SetObject(PyExc_KeyError, keyObject);
1615 return NULL;
1616 }
1617 }
1618 else if (flags & DICT_FLAG_RETURN_BOOL)
1619 {
1620 Py_INCREF(Py_True);
1621 return Py_True;
1622 }
1623
1624 di = dict_lookup(hi);
1625
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001626 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001627 return NULL;
1628
1629 if (flags & DICT_FLAG_POP)
1630 {
1631 if (dict->dv_lock)
1632 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001633 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001634 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001635 return NULL;
1636 }
1637
1638 hash_remove(&dict->dv_hashtab, hi);
1639 dictitem_free(di);
1640 }
1641
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001642 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001643}
1644
1645 static PyObject *
1646DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1647{
1648 return _DictionaryItem(self, keyObject, 0);
1649}
1650
1651 static int
1652DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1653{
1654 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001655 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001656
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001657 if (rObj == NULL)
1658 return -1;
1659
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001660 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001661
Bram Moolenaardee2e312013-06-23 16:35:47 +02001662 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001663
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001665}
1666
1667typedef struct
1668{
1669 hashitem_T *ht_array;
1670 long_u ht_used;
1671 hashtab_T *ht;
1672 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001673 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001674} dictiterinfo_T;
1675
1676 static PyObject *
1677DictionaryIterNext(dictiterinfo_T **dii)
1678{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001679 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001680
1681 if (!(*dii)->todo)
1682 return NULL;
1683
1684 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1685 (*dii)->ht->ht_used != (*dii)->ht_used)
1686 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001687 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001688 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001689 return NULL;
1690 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001691
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1693 ++((*dii)->hi);
1694
1695 --((*dii)->todo);
1696
Bram Moolenaar41009372013-07-01 22:03:04 +02001697 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001698 return NULL;
1699
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001700 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701}
1702
1703 static PyObject *
1704DictionaryIter(DictionaryObject *self)
1705{
1706 dictiterinfo_T *dii;
1707 hashtab_T *ht;
1708
1709 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1710 {
1711 PyErr_NoMemory();
1712 return NULL;
1713 }
1714
1715 ht = &self->dict->dv_hashtab;
1716 dii->ht_array = ht->ht_array;
1717 dii->ht_used = ht->ht_used;
1718 dii->ht = ht;
1719 dii->hi = dii->ht_array;
1720 dii->todo = dii->ht_used;
1721
1722 return IterNew(dii,
1723 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1724 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001725}
1726
1727 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001728DictionaryAssItem(
1729 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001730{
1731 char_u *key;
1732 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001733 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001734 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001735 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001736
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001737 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001738 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001739 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001740 return -1;
1741 }
1742
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001743 if (!(key = StringToChars(keyObject, &todecref)))
1744 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001745
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001746 if (*key == NUL)
1747 {
1748 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001749 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001750 return -1;
1751 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001752
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001753 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001754
1755 if (valObject == NULL)
1756 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001757 hashitem_T *hi;
1758
Bram Moolenaardb913952012-06-29 12:54:53 +02001759 if (di == NULL)
1760 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001761 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001762 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001763 return -1;
1764 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001765 hi = hash_find(&dict->dv_hashtab, di->di_key);
1766 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001767 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001768 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001769 return 0;
1770 }
1771
1772 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001773 {
1774 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001775 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001776 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001777
1778 if (di == NULL)
1779 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001780 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001781 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001782 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001783 PyErr_NoMemory();
1784 return -1;
1785 }
1786 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001787 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001788
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001789 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001790 {
1791 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001793 RAISE_KEY_ADD_FAIL(key);
1794 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001795 return -1;
1796 }
1797 }
1798 else
1799 clear_tv(&di->di_tv);
1800
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001801 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001802
1803 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001804 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001805 return 0;
1806}
1807
Bram Moolenaara9922d62013-05-30 13:01:18 +02001808typedef PyObject *(*hi_to_py)(hashitem_T *);
1809
Bram Moolenaardb913952012-06-29 12:54:53 +02001810 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001811DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001812{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001813 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001814 long_u todo = dict->dv_hashtab.ht_used;
1815 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001816 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001817 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001818 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001820 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001821 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1822 {
1823 if (!HASHITEM_EMPTY(hi))
1824 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001825 if (!(newObj = hiconvert(hi)))
1826 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001827 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001828 return NULL;
1829 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001830 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 --todo;
1832 ++i;
1833 }
1834 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001835 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001836}
1837
Bram Moolenaara9922d62013-05-30 13:01:18 +02001838 static PyObject *
1839dict_key(hashitem_T *hi)
1840{
1841 return PyBytes_FromString((char *)(hi->hi_key));
1842}
1843
1844 static PyObject *
1845DictionaryListKeys(DictionaryObject *self)
1846{
1847 return DictionaryListObjects(self, dict_key);
1848}
1849
1850 static PyObject *
1851dict_val(hashitem_T *hi)
1852{
1853 dictitem_T *di;
1854
1855 di = dict_lookup(hi);
1856 return ConvertToPyObject(&di->di_tv);
1857}
1858
1859 static PyObject *
1860DictionaryListValues(DictionaryObject *self)
1861{
1862 return DictionaryListObjects(self, dict_val);
1863}
1864
1865 static PyObject *
1866dict_item(hashitem_T *hi)
1867{
1868 PyObject *keyObject;
1869 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001870 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001871
1872 if (!(keyObject = dict_key(hi)))
1873 return NULL;
1874
1875 if (!(valObject = dict_val(hi)))
1876 {
1877 Py_DECREF(keyObject);
1878 return NULL;
1879 }
1880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001881 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001882
1883 Py_DECREF(keyObject);
1884 Py_DECREF(valObject);
1885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001886 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001887}
1888
1889 static PyObject *
1890DictionaryListItems(DictionaryObject *self)
1891{
1892 return DictionaryListObjects(self, dict_item);
1893}
1894
1895 static PyObject *
1896DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1897{
1898 dict_T *dict = self->dict;
1899
1900 if (dict->dv_lock)
1901 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001902 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001903 return NULL;
1904 }
1905
1906 if (kwargs)
1907 {
1908 typval_T tv;
1909
1910 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1911 return NULL;
1912
1913 VimTryStart();
1914 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1915 clear_tv(&tv);
1916 if (VimTryEnd())
1917 return NULL;
1918 }
1919 else
1920 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001921 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001922
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001923 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001924 return NULL;
1925
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001926 if (PyObject_HasAttrString(obj, "keys"))
1927 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001928 else
1929 {
1930 PyObject *iterator;
1931 PyObject *item;
1932
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001933 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001934 return NULL;
1935
1936 while ((item = PyIter_Next(iterator)))
1937 {
1938 PyObject *fast;
1939 PyObject *keyObject;
1940 PyObject *valObject;
1941 PyObject *todecref;
1942 char_u *key;
1943 dictitem_T *di;
1944
1945 if (!(fast = PySequence_Fast(item, "")))
1946 {
1947 Py_DECREF(iterator);
1948 Py_DECREF(item);
1949 return NULL;
1950 }
1951
1952 Py_DECREF(item);
1953
1954 if (PySequence_Fast_GET_SIZE(fast) != 2)
1955 {
1956 Py_DECREF(iterator);
1957 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001958 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001959 N_("expected sequence element of size 2, "
1960 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001961 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001962 return NULL;
1963 }
1964
1965 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1966
1967 if (!(key = StringToChars(keyObject, &todecref)))
1968 {
1969 Py_DECREF(iterator);
1970 Py_DECREF(fast);
1971 return NULL;
1972 }
1973
1974 di = dictitem_alloc(key);
1975
1976 Py_XDECREF(todecref);
1977
1978 if (di == NULL)
1979 {
1980 Py_DECREF(fast);
1981 Py_DECREF(iterator);
1982 PyErr_NoMemory();
1983 return NULL;
1984 }
1985 di->di_tv.v_lock = 0;
1986 di->di_tv.v_type = VAR_UNKNOWN;
1987
1988 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1989
1990 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1991 {
1992 Py_DECREF(iterator);
1993 Py_DECREF(fast);
1994 dictitem_free(di);
1995 return NULL;
1996 }
1997
1998 Py_DECREF(fast);
1999
2000 if (dict_add(dict, di) == FAIL)
2001 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002002 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002003 Py_DECREF(iterator);
2004 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002005 return NULL;
2006 }
2007 }
2008
2009 Py_DECREF(iterator);
2010
2011 /* Iterator may have finished due to an exception */
2012 if (PyErr_Occurred())
2013 return NULL;
2014 }
2015 }
2016 Py_INCREF(Py_None);
2017 return Py_None;
2018}
2019
2020 static PyObject *
2021DictionaryGet(DictionaryObject *self, PyObject *args)
2022{
2023 return _DictionaryItem(self, args,
2024 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2025}
2026
2027 static PyObject *
2028DictionaryPop(DictionaryObject *self, PyObject *args)
2029{
2030 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2031}
2032
2033 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002034DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002035{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002036 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002037 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002038 PyObject *valObject;
2039 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002040
Bram Moolenaarde71b562013-06-02 17:41:54 +02002041 if (self->dict->dv_hashtab.ht_used == 0)
2042 {
2043 PyErr_SetNone(PyExc_KeyError);
2044 return NULL;
2045 }
2046
2047 hi = self->dict->dv_hashtab.ht_array;
2048 while (HASHITEM_EMPTY(hi))
2049 ++hi;
2050
2051 di = dict_lookup(hi);
2052
2053 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002054 return NULL;
2055
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002056 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002057 {
2058 Py_DECREF(valObject);
2059 return NULL;
2060 }
2061
2062 hash_remove(&self->dict->dv_hashtab, hi);
2063 dictitem_free(di);
2064
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002065 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002066}
2067
2068 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002069DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002070{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002071 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2072}
2073
2074static PySequenceMethods DictionaryAsSeq = {
2075 0, /* sq_length */
2076 0, /* sq_concat */
2077 0, /* sq_repeat */
2078 0, /* sq_item */
2079 0, /* sq_slice */
2080 0, /* sq_ass_item */
2081 0, /* sq_ass_slice */
2082 (objobjproc) DictionaryContains, /* sq_contains */
2083 0, /* sq_inplace_concat */
2084 0, /* sq_inplace_repeat */
2085};
2086
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002087static PyMappingMethods DictionaryAsMapping = {
2088 (lenfunc) DictionaryLength,
2089 (binaryfunc) DictionaryItem,
2090 (objobjargproc) DictionaryAssItem,
2091};
2092
Bram Moolenaardb913952012-06-29 12:54:53 +02002093static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002094 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002095 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2096 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2097 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2098 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2099 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002100 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002101 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002102 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2103 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002104};
2105
2106static PyTypeObject ListType;
2107
2108typedef struct
2109{
2110 PyObject_HEAD
2111 list_T *list;
2112 pylinkedlist_T ref;
2113} ListObject;
2114
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002115#define NEW_LIST(list) ListNew(&ListType, list)
2116
Bram Moolenaardb913952012-06-29 12:54:53 +02002117 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002118ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002119{
2120 ListObject *self;
2121
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002122 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002123 if (self == NULL)
2124 return NULL;
2125 self->list = list;
2126 ++list->lv_refcount;
2127
2128 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2129
2130 return (PyObject *)(self);
2131}
2132
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002133 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002134py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002135{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002136 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002137
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002138 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002139 {
2140 PyErr_NoMemory();
2141 return NULL;
2142 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002143 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002144
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002145 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002146}
2147
2148 static int
2149list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2150{
2151 PyObject *iterator;
2152 PyObject *item;
2153 listitem_T *li;
2154
2155 if (!(iterator = PyObject_GetIter(obj)))
2156 return -1;
2157
2158 while ((item = PyIter_Next(iterator)))
2159 {
2160 if (!(li = listitem_alloc()))
2161 {
2162 PyErr_NoMemory();
2163 Py_DECREF(item);
2164 Py_DECREF(iterator);
2165 return -1;
2166 }
2167 li->li_tv.v_lock = 0;
2168 li->li_tv.v_type = VAR_UNKNOWN;
2169
2170 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2171 {
2172 Py_DECREF(item);
2173 Py_DECREF(iterator);
2174 listitem_free(li);
2175 return -1;
2176 }
2177
2178 Py_DECREF(item);
2179
2180 list_append(l, li);
2181 }
2182
2183 Py_DECREF(iterator);
2184
2185 /* Iterator may have finished due to an exception */
2186 if (PyErr_Occurred())
2187 return -1;
2188
2189 return 0;
2190}
2191
2192 static PyObject *
2193ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2194{
2195 list_T *list;
2196 PyObject *obj = NULL;
2197
2198 if (kwargs)
2199 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002200 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002201 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002202 return NULL;
2203 }
2204
2205 if (!PyArg_ParseTuple(args, "|O", &obj))
2206 return NULL;
2207
2208 if (!(list = py_list_alloc()))
2209 return NULL;
2210
2211 if (obj)
2212 {
2213 PyObject *lookup_dict;
2214
2215 if (!(lookup_dict = PyDict_New()))
2216 {
2217 list_unref(list);
2218 return NULL;
2219 }
2220
2221 if (list_py_concat(list, obj, lookup_dict) == -1)
2222 {
2223 Py_DECREF(lookup_dict);
2224 list_unref(list);
2225 return NULL;
2226 }
2227
2228 Py_DECREF(lookup_dict);
2229 }
2230
2231 return ListNew(subtype, list);
2232}
2233
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002234 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002235ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002236{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002237 pyll_remove(&self->ref, &lastlist);
2238 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002239
2240 DESTRUCTOR_FINISH(self);
2241}
2242
Bram Moolenaardb913952012-06-29 12:54:53 +02002243 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002244ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002245{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002246 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002247}
2248
2249 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002250ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002251{
2252 listitem_T *li;
2253
Bram Moolenaard6e39182013-05-21 18:30:34 +02002254 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002255 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002256 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002257 return NULL;
2258 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002259 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002260 if (li == NULL)
2261 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002262 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002263 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002264 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002265 return NULL;
2266 }
2267 return ConvertToPyObject(&li->li_tv);
2268}
2269
Bram Moolenaardb913952012-06-29 12:54:53 +02002270 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002271ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2272 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002273{
2274 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002275 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002276
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002277 if (step == 0)
2278 {
2279 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2280 return NULL;
2281 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002282
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002283 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002284 if (list == NULL)
2285 return NULL;
2286
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002287 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002288 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002289 PyObject *item;
2290
2291 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002292 if (item == NULL)
2293 {
2294 Py_DECREF(list);
2295 return NULL;
2296 }
2297
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002298 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002299 }
2300
2301 return list;
2302}
2303
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002304 static PyObject *
2305ListItem(ListObject *self, PyObject* idx)
2306{
2307#if PY_MAJOR_VERSION < 3
2308 if (PyInt_Check(idx))
2309 {
2310 long _idx = PyInt_AsLong(idx);
2311 return ListIndex(self, _idx);
2312 }
2313 else
2314#endif
2315 if (PyLong_Check(idx))
2316 {
2317 long _idx = PyLong_AsLong(idx);
2318 return ListIndex(self, _idx);
2319 }
2320 else if (PySlice_Check(idx))
2321 {
2322 Py_ssize_t start, stop, step, slicelen;
2323
Bram Moolenaar5395e7a2014-01-14 19:35:56 +01002324 if (PySlice_GetIndicesEx((PySliceObject *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002325 &start, &stop, &step, &slicelen) < 0)
2326 return NULL;
2327 return ListSlice(self, start, step, slicelen);
2328 }
2329 else
2330 {
2331 RAISE_INVALID_INDEX_TYPE(idx);
2332 return NULL;
2333 }
2334}
2335
2336 static void
2337list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2338 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2339{
2340 while (numreplaced--)
2341 {
2342 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2343 listitem_remove(l, lis[slicelen + numreplaced]);
2344 }
2345 while (numadded--)
2346 {
2347 listitem_T *next;
2348
2349 next = lastaddedli->li_prev;
2350 listitem_remove(l, lastaddedli);
2351 lastaddedli = next;
2352 }
2353}
2354
2355 static int
2356ListAssSlice(ListObject *self, Py_ssize_t first,
2357 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2358{
2359 PyObject *iterator;
2360 PyObject *item;
2361 listitem_T *li;
2362 listitem_T *lastaddedli = NULL;
2363 listitem_T *next;
2364 typval_T v;
2365 list_T *l = self->list;
2366 PyInt i;
2367 PyInt j;
2368 PyInt numreplaced = 0;
2369 PyInt numadded = 0;
2370 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002371 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002372
2373 size = ListLength(self);
2374
2375 if (l->lv_lock)
2376 {
2377 RAISE_LOCKED_LIST;
2378 return -1;
2379 }
2380
2381 if (step == 0)
2382 {
2383 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2384 return -1;
2385 }
2386
2387 if (step != 1 && slicelen == 0)
2388 {
2389 /* Nothing to do. Only error out if obj has some items. */
2390 int ret = 0;
2391
2392 if (obj == NULL)
2393 return 0;
2394
2395 if (!(iterator = PyObject_GetIter(obj)))
2396 return -1;
2397
2398 if ((item = PyIter_Next(iterator)))
2399 {
2400 PyErr_FORMAT(PyExc_ValueError,
2401 N_("attempt to assign sequence of size greater then %d "
2402 "to extended slice"), 0);
2403 Py_DECREF(item);
2404 ret = -1;
2405 }
2406 Py_DECREF(iterator);
2407 return ret;
2408 }
2409
2410 if (obj != NULL)
2411 /* XXX May allocate zero bytes. */
2412 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2413 {
2414 PyErr_NoMemory();
2415 return -1;
2416 }
2417
2418 if (first == size)
2419 li = NULL;
2420 else
2421 {
2422 li = list_find(l, (long) first);
2423 if (li == NULL)
2424 {
2425 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2426 (int)first);
2427 if (obj != NULL)
2428 PyMem_Free(lis);
2429 return -1;
2430 }
2431 i = slicelen;
2432 while (i-- && li != NULL)
2433 {
2434 j = step;
2435 next = li;
2436 if (step > 0)
2437 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2438 else
2439 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2440
2441 if (obj == NULL)
2442 listitem_remove(l, li);
2443 else
2444 lis[slicelen - i - 1] = li;
2445
2446 li = next;
2447 }
2448 if (li == NULL && i != -1)
2449 {
2450 PyErr_SET_VIM(N_("internal error: not enough list items"));
2451 if (obj != NULL)
2452 PyMem_Free(lis);
2453 return -1;
2454 }
2455 }
2456
2457 if (obj == NULL)
2458 return 0;
2459
2460 if (!(iterator = PyObject_GetIter(obj)))
2461 {
2462 PyMem_Free(lis);
2463 return -1;
2464 }
2465
2466 i = 0;
2467 while ((item = PyIter_Next(iterator)))
2468 {
2469 if (ConvertFromPyObject(item, &v) == -1)
2470 {
2471 Py_DECREF(iterator);
2472 Py_DECREF(item);
2473 PyMem_Free(lis);
2474 return -1;
2475 }
2476 Py_DECREF(item);
2477 if (list_insert_tv(l, &v, numreplaced < slicelen
2478 ? lis[numreplaced]
2479 : li) == FAIL)
2480 {
2481 clear_tv(&v);
2482 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2483 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2484 PyMem_Free(lis);
2485 return -1;
2486 }
2487 if (numreplaced < slicelen)
2488 {
2489 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
2490 list_remove(l, lis[numreplaced], lis[numreplaced]);
2491 numreplaced++;
2492 }
2493 else
2494 {
2495 if (li)
2496 lastaddedli = li->li_prev;
2497 else
2498 lastaddedli = l->lv_last;
2499 numadded++;
2500 }
2501 clear_tv(&v);
2502 if (step != 1 && i >= slicelen)
2503 {
2504 Py_DECREF(iterator);
2505 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar3b522612014-02-11 16:00:35 +01002506 N_("attempt to assign sequence of size greater then %ld "
2507 "to extended slice"), (long)slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002508 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2509 PyMem_Free(lis);
2510 return -1;
2511 }
2512 ++i;
2513 }
2514 Py_DECREF(iterator);
2515
2516 if (step != 1 && i != slicelen)
2517 {
2518 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar3b522612014-02-11 16:00:35 +01002519 N_("attempt to assign sequence of size %ld to extended slice "
2520 "of size %ld"), (long)i, (long)slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002521 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2522 PyMem_Free(lis);
2523 return -1;
2524 }
2525
2526 if (PyErr_Occurred())
2527 {
2528 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2529 PyMem_Free(lis);
2530 return -1;
2531 }
2532
2533 for (i = 0; i < numreplaced; i++)
2534 listitem_free(lis[i]);
2535 if (step == 1)
2536 for (i = numreplaced; i < slicelen; i++)
2537 listitem_remove(l, lis[i]);
2538
2539 PyMem_Free(lis);
2540
2541 return 0;
2542}
2543
2544 static int
2545ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2546{
2547 typval_T tv;
2548 list_T *l = self->list;
2549 listitem_T *li;
2550 Py_ssize_t length = ListLength(self);
2551
2552 if (l->lv_lock)
2553 {
2554 RAISE_LOCKED_LIST;
2555 return -1;
2556 }
2557 if (index > length || (index == length && obj == NULL))
2558 {
2559 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2560 return -1;
2561 }
2562
2563 if (obj == NULL)
2564 {
2565 li = list_find(l, (long) index);
2566 list_remove(l, li, li);
2567 clear_tv(&li->li_tv);
2568 vim_free(li);
2569 return 0;
2570 }
2571
2572 if (ConvertFromPyObject(obj, &tv) == -1)
2573 return -1;
2574
2575 if (index == length)
2576 {
2577 if (list_append_tv(l, &tv) == FAIL)
2578 {
2579 clear_tv(&tv);
2580 PyErr_SET_VIM(N_("failed to add item to list"));
2581 return -1;
2582 }
2583 }
2584 else
2585 {
2586 li = list_find(l, (long) index);
2587 clear_tv(&li->li_tv);
2588 copy_tv(&tv, &li->li_tv);
2589 clear_tv(&tv);
2590 }
2591 return 0;
2592}
2593
2594 static Py_ssize_t
2595ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2596{
2597#if PY_MAJOR_VERSION < 3
2598 if (PyInt_Check(idx))
2599 {
2600 long _idx = PyInt_AsLong(idx);
2601 return ListAssIndex(self, _idx, obj);
2602 }
2603 else
2604#endif
2605 if (PyLong_Check(idx))
2606 {
2607 long _idx = PyLong_AsLong(idx);
2608 return ListAssIndex(self, _idx, obj);
2609 }
2610 else if (PySlice_Check(idx))
2611 {
2612 Py_ssize_t start, stop, step, slicelen;
2613
Bram Moolenaar5395e7a2014-01-14 19:35:56 +01002614 if (PySlice_GetIndicesEx((PySliceObject *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002615 &start, &stop, &step, &slicelen) < 0)
2616 return -1;
2617 return ListAssSlice(self, start, step, slicelen,
2618 obj);
2619 }
2620 else
2621 {
2622 RAISE_INVALID_INDEX_TYPE(idx);
2623 return -1;
2624 }
2625}
2626
2627 static PyObject *
2628ListConcatInPlace(ListObject *self, PyObject *obj)
2629{
2630 list_T *l = self->list;
2631 PyObject *lookup_dict;
2632
2633 if (l->lv_lock)
2634 {
2635 RAISE_LOCKED_LIST;
2636 return NULL;
2637 }
2638
2639 if (!(lookup_dict = PyDict_New()))
2640 return NULL;
2641
2642 if (list_py_concat(l, obj, lookup_dict) == -1)
2643 {
2644 Py_DECREF(lookup_dict);
2645 return NULL;
2646 }
2647 Py_DECREF(lookup_dict);
2648
2649 Py_INCREF(self);
2650 return (PyObject *)(self);
2651}
2652
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002653typedef struct
2654{
2655 listwatch_T lw;
2656 list_T *list;
2657} listiterinfo_T;
2658
2659 static void
2660ListIterDestruct(listiterinfo_T *lii)
2661{
2662 list_rem_watch(lii->list, &lii->lw);
2663 PyMem_Free(lii);
2664}
2665
2666 static PyObject *
2667ListIterNext(listiterinfo_T **lii)
2668{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002669 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002670
2671 if (!((*lii)->lw.lw_item))
2672 return NULL;
2673
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002674 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002675 return NULL;
2676
2677 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2678
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002679 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002680}
2681
2682 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002683ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002684{
2685 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002686 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002687
2688 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2689 {
2690 PyErr_NoMemory();
2691 return NULL;
2692 }
2693
2694 list_add_watch(l, &lii->lw);
2695 lii->lw.lw_item = l->lv_first;
2696 lii->list = l;
2697
2698 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002699 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2700 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002701}
2702
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002703static char *ListAttrs[] = {
2704 "locked",
2705 NULL
2706};
2707
2708 static PyObject *
2709ListDir(PyObject *self)
2710{
2711 return ObjectDir(self, ListAttrs);
2712}
2713
Bram Moolenaar66b79852012-09-21 14:00:35 +02002714 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002715ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002716{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002717 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002718 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002719 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002720 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002721 return -1;
2722 }
2723
2724 if (strcmp(name, "locked") == 0)
2725 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002726 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002727 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002728 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002729 return -1;
2730 }
2731 else
2732 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002733 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002734 if (istrue == -1)
2735 return -1;
2736 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002737 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002738 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002739 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002740 }
2741 return 0;
2742 }
2743 else
2744 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002745 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002746 return -1;
2747 }
2748}
2749
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002750static PySequenceMethods ListAsSeq = {
2751 (lenfunc) ListLength, /* sq_length, len(x) */
2752 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2753 0, /* RangeRepeat, sq_repeat, x*n */
2754 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2755 0, /* was_sq_slice, x[i:j] */
2756 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2757 0, /* was_sq_ass_slice, x[i:j]=v */
2758 0, /* sq_contains */
2759 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2760 0, /* sq_inplace_repeat */
2761};
2762
2763static PyMappingMethods ListAsMapping = {
2764 /* mp_length */ (lenfunc) ListLength,
2765 /* mp_subscript */ (binaryfunc) ListItem,
2766 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2767};
2768
Bram Moolenaardb913952012-06-29 12:54:53 +02002769static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002770 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2771 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2772 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002773};
2774
2775typedef struct
2776{
2777 PyObject_HEAD
2778 char_u *name;
2779} FunctionObject;
2780
2781static PyTypeObject FunctionType;
2782
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002783#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2784
Bram Moolenaardb913952012-06-29 12:54:53 +02002785 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002786FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002787{
2788 FunctionObject *self;
2789
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002790 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2791
Bram Moolenaardb913952012-06-29 12:54:53 +02002792 if (self == NULL)
2793 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002794
2795 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002796 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002797 if (!translated_function_exists(name))
2798 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002799 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002800 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002801 return NULL;
2802 }
2803 self->name = vim_strsave(name);
2804 func_ref(self->name);
2805 }
2806 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002807 if ((self->name = get_expanded_name(name,
2808 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2809 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002810 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002811 PyErr_FORMAT(PyExc_ValueError,
2812 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002813 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002814 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002815
2816 return (PyObject *)(self);
2817}
2818
2819 static PyObject *
2820FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2821{
2822 PyObject *self;
2823 char_u *name;
2824
2825 if (kwargs)
2826 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002827 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002828 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002829 return NULL;
2830 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002831
Bram Moolenaar389a1792013-06-23 13:00:44 +02002832 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002833 return NULL;
2834
2835 self = FunctionNew(subtype, name);
2836
Bram Moolenaar389a1792013-06-23 13:00:44 +02002837 PyMem_Free(name);
2838
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002839 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002840}
2841
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002842 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002843FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002844{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002845 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002846 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002847
2848 DESTRUCTOR_FINISH(self);
2849}
2850
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002851static char *FunctionAttrs[] = {
2852 "softspace",
2853 NULL
2854};
2855
2856 static PyObject *
2857FunctionDir(PyObject *self)
2858{
2859 return ObjectDir(self, FunctionAttrs);
2860}
2861
Bram Moolenaardb913952012-06-29 12:54:53 +02002862 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002863FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002864{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002865 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002866 typval_T args;
2867 typval_T selfdicttv;
2868 typval_T rettv;
2869 dict_T *selfdict = NULL;
2870 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002872 int error;
2873
2874 if (ConvertFromPyObject(argsObject, &args) == -1)
2875 return NULL;
2876
2877 if (kwargs != NULL)
2878 {
2879 selfdictObject = PyDict_GetItemString(kwargs, "self");
2880 if (selfdictObject != NULL)
2881 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002882 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002883 {
2884 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002885 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002886 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002887 selfdict = selfdicttv.vval.v_dict;
2888 }
2889 }
2890
Bram Moolenaar71700b82013-05-15 17:49:05 +02002891 Py_BEGIN_ALLOW_THREADS
2892 Python_Lock_Vim();
2893
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002894 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002895 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002896
2897 Python_Release_Vim();
2898 Py_END_ALLOW_THREADS
2899
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002900 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002901 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002902 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002903 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002904 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002905 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002906 }
2907 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002908 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002909
Bram Moolenaardb913952012-06-29 12:54:53 +02002910 clear_tv(&args);
2911 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002912 if (selfdict != NULL)
2913 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002914
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002915 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002916}
2917
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002918 static PyObject *
2919FunctionRepr(FunctionObject *self)
2920{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002921#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002922 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002923 * Finalize */
2924 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002925 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002926#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002927 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002928#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002929}
2930
Bram Moolenaardb913952012-06-29 12:54:53 +02002931static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002932 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2933 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002934};
2935
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002936/*
2937 * Options object
2938 */
2939
2940static PyTypeObject OptionsType;
2941
2942typedef int (*checkfun)(void *);
2943
2944typedef struct
2945{
2946 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002947 int opt_type;
2948 void *from;
2949 checkfun Check;
2950 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002951} OptionsObject;
2952
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002953 static int
2954dummy_check(void *arg UNUSED)
2955{
2956 return 0;
2957}
2958
2959 static PyObject *
2960OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2961{
2962 OptionsObject *self;
2963
Bram Moolenaar774267b2013-05-21 20:51:59 +02002964 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002965 if (self == NULL)
2966 return NULL;
2967
2968 self->opt_type = opt_type;
2969 self->from = from;
2970 self->Check = Check;
2971 self->fromObj = fromObj;
2972 if (fromObj)
2973 Py_INCREF(fromObj);
2974
2975 return (PyObject *)(self);
2976}
2977
2978 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002979OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002980{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002981 PyObject_GC_UnTrack((void *)(self));
2982 Py_XDECREF(self->fromObj);
2983 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002984}
2985
2986 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002987OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002988{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002989 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002990 return 0;
2991}
2992
2993 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002994OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002995{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002996 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002997 return 0;
2998}
2999
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003000 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003001OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003002{
3003 char_u *key;
3004 int flags;
3005 long numval;
3006 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003007 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003008
Bram Moolenaard6e39182013-05-21 18:30:34 +02003009 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003010 return NULL;
3011
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003012 if (!(key = StringToChars(keyObject, &todecref)))
3013 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003014
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003015 if (*key == NUL)
3016 {
3017 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003018 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003019 return NULL;
3020 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003021
3022 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003023 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003024
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003025 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003026
3027 if (flags == 0)
3028 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003029 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003030 return NULL;
3031 }
3032
3033 if (flags & SOPT_UNSET)
3034 {
3035 Py_INCREF(Py_None);
3036 return Py_None;
3037 }
3038 else if (flags & SOPT_BOOL)
3039 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003040 PyObject *ret;
3041 ret = numval ? Py_True : Py_False;
3042 Py_INCREF(ret);
3043 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003044 }
3045 else if (flags & SOPT_NUM)
3046 return PyInt_FromLong(numval);
3047 else if (flags & SOPT_STRING)
3048 {
3049 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003050 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003051 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003052 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003053 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003054 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003055 else
3056 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003057 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003058 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003059 return NULL;
3060 }
3061 }
3062 else
3063 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003064 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003065 return NULL;
3066 }
3067}
3068
3069 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003070OptionsContains(OptionsObject *self, PyObject *keyObject)
3071{
3072 char_u *key;
3073 PyObject *todecref;
3074
3075 if (!(key = StringToChars(keyObject, &todecref)))
3076 return -1;
3077
3078 if (*key == NUL)
3079 {
3080 Py_XDECREF(todecref);
3081 return 0;
3082 }
3083
3084 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3085 {
3086 Py_XDECREF(todecref);
3087 return 1;
3088 }
3089 else
3090 {
3091 Py_XDECREF(todecref);
3092 return 0;
3093 }
3094}
3095
3096typedef struct
3097{
3098 void *lastoption;
3099 int opt_type;
3100} optiterinfo_T;
3101
3102 static PyObject *
3103OptionsIterNext(optiterinfo_T **oii)
3104{
3105 char_u *name;
3106
3107 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3108 return PyString_FromString((char *)name);
3109
3110 return NULL;
3111}
3112
3113 static PyObject *
3114OptionsIter(OptionsObject *self)
3115{
3116 optiterinfo_T *oii;
3117
3118 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3119 {
3120 PyErr_NoMemory();
3121 return NULL;
3122 }
3123
3124 oii->opt_type = self->opt_type;
3125 oii->lastoption = NULL;
3126
3127 return IterNew(oii,
3128 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3129 NULL, NULL);
3130}
3131
3132 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003133set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003134{
3135 char_u *errmsg;
3136
3137 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3138 {
3139 if (VimTryEnd())
3140 return FAIL;
3141 PyErr_SetVim((char *)errmsg);
3142 return FAIL;
3143 }
3144 return OK;
3145}
3146
3147 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003148set_option_value_for(
3149 char_u *key,
3150 int numval,
3151 char_u *stringval,
3152 int opt_flags,
3153 int opt_type,
3154 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003155{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003156 win_T *save_curwin = NULL;
3157 tabpage_T *save_curtab = NULL;
3158 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003159 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003160
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003161 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003162 switch (opt_type)
3163 {
3164 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003165 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003166 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003167 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003168 if (VimTryEnd())
3169 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003170 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003171 return -1;
3172 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003173 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3174 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003175 break;
3176 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003177 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003178 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003179 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003180 break;
3181 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003182 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003183 break;
3184 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003185 if (set_ret == FAIL)
3186 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003187 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003188}
3189
3190 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003191OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003192{
3193 char_u *key;
3194 int flags;
3195 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003196 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003197 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003198
Bram Moolenaard6e39182013-05-21 18:30:34 +02003199 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003200 return -1;
3201
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003202 if (!(key = StringToChars(keyObject, &todecref)))
3203 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003204
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003205 if (*key == NUL)
3206 {
3207 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003208 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003209 return -1;
3210 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003211
3212 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003213 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003214
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003215 if (flags == 0)
3216 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003217 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003218 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003219 return -1;
3220 }
3221
3222 if (valObject == NULL)
3223 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003224 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003225 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003226 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003227 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003228 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003229 return -1;
3230 }
3231 else if (!(flags & SOPT_GLOBAL))
3232 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003233 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003234 N_("unable to unset option %s "
3235 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003236 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003237 return -1;
3238 }
3239 else
3240 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003241 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003242 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003243 return 0;
3244 }
3245 }
3246
Bram Moolenaard6e39182013-05-21 18:30:34 +02003247 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003248
3249 if (flags & SOPT_BOOL)
3250 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003251 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003252
Bram Moolenaarb983f752013-05-15 16:11:50 +02003253 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003254 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003255 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003256 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003257 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003258 }
3259 else if (flags & SOPT_NUM)
3260 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003261 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003262
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003263 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003264 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003265 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003266 return -1;
3267 }
3268
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003269 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003270 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003271 }
3272 else
3273 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003274 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003275 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003276
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003277 if ((val = StringToChars(valObject, &todecref2)))
3278 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003279 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003280 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003281 Py_XDECREF(todecref2);
3282 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003283 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003284 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003285 }
3286
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003287 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003288
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003289 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003290}
3291
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003292static PySequenceMethods OptionsAsSeq = {
3293 0, /* sq_length */
3294 0, /* sq_concat */
3295 0, /* sq_repeat */
3296 0, /* sq_item */
3297 0, /* sq_slice */
3298 0, /* sq_ass_item */
3299 0, /* sq_ass_slice */
3300 (objobjproc) OptionsContains, /* sq_contains */
3301 0, /* sq_inplace_concat */
3302 0, /* sq_inplace_repeat */
3303};
3304
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003305static PyMappingMethods OptionsAsMapping = {
3306 (lenfunc) NULL,
3307 (binaryfunc) OptionsItem,
3308 (objobjargproc) OptionsAssItem,
3309};
3310
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003311/* Tabpage object
3312 */
3313
3314typedef struct
3315{
3316 PyObject_HEAD
3317 tabpage_T *tab;
3318} TabPageObject;
3319
3320static PyObject *WinListNew(TabPageObject *tabObject);
3321
3322static PyTypeObject TabPageType;
3323
3324 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003325CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003326{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003327 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003328 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003329 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003330 return -1;
3331 }
3332
3333 return 0;
3334}
3335
3336 static PyObject *
3337TabPageNew(tabpage_T *tab)
3338{
3339 TabPageObject *self;
3340
3341 if (TAB_PYTHON_REF(tab))
3342 {
3343 self = TAB_PYTHON_REF(tab);
3344 Py_INCREF(self);
3345 }
3346 else
3347 {
3348 self = PyObject_NEW(TabPageObject, &TabPageType);
3349 if (self == NULL)
3350 return NULL;
3351 self->tab = tab;
3352 TAB_PYTHON_REF(tab) = self;
3353 }
3354
3355 return (PyObject *)(self);
3356}
3357
3358 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003359TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003360{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003361 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3362 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003363
3364 DESTRUCTOR_FINISH(self);
3365}
3366
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003367static char *TabPageAttrs[] = {
3368 "windows", "number", "vars", "window", "valid",
3369 NULL
3370};
3371
3372 static PyObject *
3373TabPageDir(PyObject *self)
3374{
3375 return ObjectDir(self, TabPageAttrs);
3376}
3377
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003378 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003379TabPageAttrValid(TabPageObject *self, char *name)
3380{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003381 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003382
3383 if (strcmp(name, "valid") != 0)
3384 return NULL;
3385
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003386 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3387 Py_INCREF(ret);
3388 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003389}
3390
3391 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003392TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003393{
3394 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003395 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003396 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003397 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003398 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003399 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003400 else if (strcmp(name, "window") == 0)
3401 {
3402 /* For current tab window.c does not bother to set or update tp_curwin
3403 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003404 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003405 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003406 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003407 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003408 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003409 else if (strcmp(name, "__members__") == 0)
3410 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003411 return NULL;
3412}
3413
3414 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003415TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003416{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003417 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003418 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003419 else
3420 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003421 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003422
3423 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003424 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3425 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003426 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003427 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003428 }
3429}
3430
3431static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003432 /* name, function, calling, documentation */
3433 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3434 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003435};
3436
3437/*
3438 * Window list object
3439 */
3440
3441static PyTypeObject TabListType;
3442static PySequenceMethods TabListAsSeq;
3443
3444typedef struct
3445{
3446 PyObject_HEAD
3447} TabListObject;
3448
3449 static PyInt
3450TabListLength(PyObject *self UNUSED)
3451{
3452 tabpage_T *tp = first_tabpage;
3453 PyInt n = 0;
3454
3455 while (tp != NULL)
3456 {
3457 ++n;
3458 tp = tp->tp_next;
3459 }
3460
3461 return n;
3462}
3463
3464 static PyObject *
3465TabListItem(PyObject *self UNUSED, PyInt n)
3466{
3467 tabpage_T *tp;
3468
3469 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3470 if (n == 0)
3471 return TabPageNew(tp);
3472
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003473 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003474 return NULL;
3475}
3476
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003477/*
3478 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003479 */
3480
3481typedef struct
3482{
3483 PyObject_HEAD
3484 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003485 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003486} WindowObject;
3487
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003488static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003489
3490 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003491CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003492{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003493 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003494 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003495 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003496 return -1;
3497 }
3498
3499 return 0;
3500}
3501
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003502 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003503WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003504{
3505 /* We need to handle deletion of windows underneath us.
3506 * If we add a "w_python*_ref" field to the win_T structure,
3507 * then we can get at it in win_free() in vim. We then
3508 * need to create only ONE Python object per window - if
3509 * we try to create a second, just INCREF the existing one
3510 * and return it. The (single) Python object referring to
3511 * the window is stored in "w_python*_ref".
3512 * On a win_free() we set the Python object's win_T* field
3513 * to an invalid value. We trap all uses of a window
3514 * object, and reject them if the win_T* field is invalid.
3515 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003516 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003517 * w_python_ref and w_python3_ref fields respectively.
3518 */
3519
3520 WindowObject *self;
3521
3522 if (WIN_PYTHON_REF(win))
3523 {
3524 self = WIN_PYTHON_REF(win);
3525 Py_INCREF(self);
3526 }
3527 else
3528 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003529 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003530 if (self == NULL)
3531 return NULL;
3532 self->win = win;
3533 WIN_PYTHON_REF(win) = self;
3534 }
3535
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003536 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3537
Bram Moolenaar971db462013-05-12 18:44:48 +02003538 return (PyObject *)(self);
3539}
3540
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003541 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003542WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003543{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003544 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003545 if (self->win && self->win != INVALID_WINDOW_VALUE)
3546 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003547 Py_XDECREF(((PyObject *)(self->tabObject)));
3548 PyObject_GC_Del((void *)(self));
3549}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003550
Bram Moolenaar774267b2013-05-21 20:51:59 +02003551 static int
3552WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3553{
3554 Py_VISIT(((PyObject *)(self->tabObject)));
3555 return 0;
3556}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003557
Bram Moolenaar774267b2013-05-21 20:51:59 +02003558 static int
3559WindowClear(WindowObject *self)
3560{
3561 Py_CLEAR(self->tabObject);
3562 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003563}
3564
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003565 static win_T *
3566get_firstwin(TabPageObject *tabObject)
3567{
3568 if (tabObject)
3569 {
3570 if (CheckTabPage(tabObject))
3571 return NULL;
3572 /* For current tab window.c does not bother to set or update tp_firstwin
3573 */
3574 else if (tabObject->tab == curtab)
3575 return firstwin;
3576 else
3577 return tabObject->tab->tp_firstwin;
3578 }
3579 else
3580 return firstwin;
3581}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003582static char *WindowAttrs[] = {
3583 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3584 "tabpage", "valid",
3585 NULL
3586};
3587
3588 static PyObject *
3589WindowDir(PyObject *self)
3590{
3591 return ObjectDir(self, WindowAttrs);
3592}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003593
Bram Moolenaar971db462013-05-12 18:44:48 +02003594 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003595WindowAttrValid(WindowObject *self, char *name)
3596{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003597 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003598
3599 if (strcmp(name, "valid") != 0)
3600 return NULL;
3601
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003602 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3603 Py_INCREF(ret);
3604 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003605}
3606
3607 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003608WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003609{
3610 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003611 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003612 else if (strcmp(name, "cursor") == 0)
3613 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003614 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003615
3616 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3617 }
3618 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003619 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003620#ifdef FEAT_WINDOWS
3621 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003622 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003623#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003624#ifdef FEAT_VERTSPLIT
3625 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003626 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003627 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003628 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003629#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003630 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003631 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003632 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003633 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3634 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003635 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003636 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003637 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003638 return NULL;
3639 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003640 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003641 }
3642 else if (strcmp(name, "tabpage") == 0)
3643 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003644 Py_INCREF(self->tabObject);
3645 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003646 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003647 else if (strcmp(name, "__members__") == 0)
3648 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003649 else
3650 return NULL;
3651}
3652
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003653 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003654WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003655{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003656 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003657 return -1;
3658
3659 if (strcmp(name, "buffer") == 0)
3660 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003661 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003662 return -1;
3663 }
3664 else if (strcmp(name, "cursor") == 0)
3665 {
3666 long lnum;
3667 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003668
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003669 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003670 return -1;
3671
Bram Moolenaard6e39182013-05-21 18:30:34 +02003672 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003673 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003674 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003675 return -1;
3676 }
3677
3678 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003679 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003680 return -1;
3681
Bram Moolenaard6e39182013-05-21 18:30:34 +02003682 self->win->w_cursor.lnum = lnum;
3683 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003684#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003685 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003686#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003687 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003688 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003689
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003690 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691 return 0;
3692 }
3693 else if (strcmp(name, "height") == 0)
3694 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003695 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696 win_T *savewin;
3697
Bram Moolenaardee2e312013-06-23 16:35:47 +02003698 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003699 return -1;
3700
3701#ifdef FEAT_GUI
3702 need_mouse_correct = TRUE;
3703#endif
3704 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003705 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003706
3707 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003708 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003709 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003710 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003711 return -1;
3712
3713 return 0;
3714 }
3715#ifdef FEAT_VERTSPLIT
3716 else if (strcmp(name, "width") == 0)
3717 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003718 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003719 win_T *savewin;
3720
Bram Moolenaardee2e312013-06-23 16:35:47 +02003721 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003722 return -1;
3723
3724#ifdef FEAT_GUI
3725 need_mouse_correct = TRUE;
3726#endif
3727 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003728 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003729
3730 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003731 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003732 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003733 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003734 return -1;
3735
3736 return 0;
3737 }
3738#endif
3739 else
3740 {
3741 PyErr_SetString(PyExc_AttributeError, name);
3742 return -1;
3743 }
3744}
3745
3746 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003747WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003748{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003749 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003750 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003751 else
3752 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003753 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003754
Bram Moolenaar6d216452013-05-12 19:00:41 +02003755 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003756 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003757 (self));
3758 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003759 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003760 }
3761}
3762
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003763static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003764 /* name, function, calling, documentation */
3765 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3766 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003767};
3768
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003769/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003770 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003771 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003772
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003773static PyTypeObject WinListType;
3774static PySequenceMethods WinListAsSeq;
3775
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003776typedef struct
3777{
3778 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003779 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003780} WinListObject;
3781
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003782 static PyObject *
3783WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003784{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003785 WinListObject *self;
3786
3787 self = PyObject_NEW(WinListObject, &WinListType);
3788 self->tabObject = tabObject;
3789 Py_INCREF(tabObject);
3790
3791 return (PyObject *)(self);
3792}
3793
3794 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003795WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003796{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003797 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003798
3799 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003800 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003801 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003802 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003803
3804 DESTRUCTOR_FINISH(self);
3805}
3806
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003807 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003808WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003809{
3810 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003811 PyInt n = 0;
3812
Bram Moolenaard6e39182013-05-21 18:30:34 +02003813 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003814 return -1;
3815
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003816 while (w != NULL)
3817 {
3818 ++n;
3819 w = W_NEXT(w);
3820 }
3821
3822 return n;
3823}
3824
3825 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003826WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003827{
3828 win_T *w;
3829
Bram Moolenaard6e39182013-05-21 18:30:34 +02003830 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003831 return NULL;
3832
3833 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003834 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003835 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003836
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003837 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003838 return NULL;
3839}
3840
3841/* Convert a Python string into a Vim line.
3842 *
3843 * The result is in allocated memory. All internal nulls are replaced by
3844 * newline characters. It is an error for the string to contain newline
3845 * characters.
3846 *
3847 * On errors, the Python exception data is set, and NULL is returned.
3848 */
3849 static char *
3850StringToLine(PyObject *obj)
3851{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003852 char *str;
3853 char *save;
3854 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003855 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003856 PyInt i;
3857 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003858
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003859 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003860 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003861 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3862 || str == NULL)
3863 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003864 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003865 else if (PyUnicode_Check(obj))
3866 {
3867 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3868 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003869
Bram Moolenaardaa27022013-06-24 22:33:30 +02003870 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003871 || str == NULL)
3872 {
3873 Py_DECREF(bytes);
3874 return NULL;
3875 }
3876 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003877 else
3878 {
3879#if PY_MAJOR_VERSION < 3
3880 PyErr_FORMAT(PyExc_TypeError,
3881 N_("expected str() or unicode() instance, but got %s"),
3882 Py_TYPE_NAME(obj));
3883#else
3884 PyErr_FORMAT(PyExc_TypeError,
3885 N_("expected bytes() or str() instance, but got %s"),
3886 Py_TYPE_NAME(obj));
3887#endif
3888 return NULL;
3889 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003890
3891 /*
3892 * Error checking: String must not contain newlines, as we
3893 * are replacing a single line, and we must replace it with
3894 * a single line.
3895 * A trailing newline is removed, so that append(f.readlines()) works.
3896 */
3897 p = memchr(str, '\n', len);
3898 if (p != NULL)
3899 {
3900 if (p == str + len - 1)
3901 --len;
3902 else
3903 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003904 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003905 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003906 return NULL;
3907 }
3908 }
3909
3910 /* Create a copy of the string, with internal nulls replaced by
3911 * newline characters, as is the vim convention.
3912 */
3913 save = (char *)alloc((unsigned)(len+1));
3914 if (save == NULL)
3915 {
3916 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003917 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003918 return NULL;
3919 }
3920
3921 for (i = 0; i < len; ++i)
3922 {
3923 if (str[i] == '\0')
3924 save[i] = '\n';
3925 else
3926 save[i] = str[i];
3927 }
3928
3929 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003930 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003931
3932 return save;
3933}
3934
3935/* Get a line from the specified buffer. The line number is
3936 * in Vim format (1-based). The line is returned as a Python
3937 * string object.
3938 */
3939 static PyObject *
3940GetBufferLine(buf_T *buf, PyInt n)
3941{
3942 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3943}
3944
3945
3946/* Get a list of lines from the specified buffer. The line numbers
3947 * are in Vim format (1-based). The range is from lo up to, but not
3948 * including, hi. The list is returned as a Python list of string objects.
3949 */
3950 static PyObject *
3951GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3952{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003953 PyInt i;
3954 PyInt n = hi - lo;
3955 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003956
3957 if (list == NULL)
3958 return NULL;
3959
3960 for (i = 0; i < n; ++i)
3961 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003962 PyObject *string = LineToString(
3963 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003964
3965 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003966 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003967 {
3968 Py_DECREF(list);
3969 return NULL;
3970 }
3971
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003972 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003973 }
3974
3975 /* The ownership of the Python list is passed to the caller (ie,
3976 * the caller should Py_DECREF() the object when it is finished
3977 * with it).
3978 */
3979
3980 return list;
3981}
3982
3983/*
3984 * Check if deleting lines made the cursor position invalid.
3985 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3986 * deleted).
3987 */
3988 static void
3989py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3990{
3991 if (curwin->w_cursor.lnum >= lo)
3992 {
3993 /* Adjust the cursor position if it's in/after the changed
3994 * lines. */
3995 if (curwin->w_cursor.lnum >= hi)
3996 {
3997 curwin->w_cursor.lnum += extra;
3998 check_cursor_col();
3999 }
4000 else if (extra < 0)
4001 {
4002 curwin->w_cursor.lnum = lo;
4003 check_cursor();
4004 }
4005 else
4006 check_cursor_col();
4007 changed_cline_bef_curs();
4008 }
4009 invalidate_botline();
4010}
4011
Bram Moolenaar19e60942011-06-19 00:27:51 +02004012/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004013 * Find a window that contains "buf" and switch to it.
4014 * If there is no such window, use the current window and change "curbuf".
4015 * Caller must initialize save_curbuf to NULL.
4016 * restore_win_for_buf() MUST be called later!
4017 */
4018 static void
4019switch_to_win_for_buf(
4020 buf_T *buf,
4021 win_T **save_curwinp,
4022 tabpage_T **save_curtabp,
4023 buf_T **save_curbufp)
4024{
4025 win_T *wp;
4026 tabpage_T *tp;
4027
4028 if (find_win_for_buf(buf, &wp, &tp) == FAIL
4029 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4030 switch_buffer(save_curbufp, buf);
4031}
4032
4033 static void
4034restore_win_for_buf(
4035 win_T *save_curwin,
4036 tabpage_T *save_curtab,
4037 buf_T *save_curbuf)
4038{
4039 if (save_curbuf == NULL)
4040 restore_win(save_curwin, save_curtab, TRUE);
4041 else
4042 restore_buffer(save_curbuf);
4043}
4044
4045/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004046 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004047 * in Vim format (1-based). The replacement line is given as
4048 * a Python string object. The object is checked for validity
4049 * and correct format. Errors are returned as a value of FAIL.
4050 * The return value is OK on success.
4051 * If OK is returned and len_change is not NULL, *len_change
4052 * is set to the change in the buffer length.
4053 */
4054 static int
4055SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4056{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004057 buf_T *save_curbuf = NULL;
4058 win_T *save_curwin = NULL;
4059 tabpage_T *save_curtab = NULL;
4060
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004061 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 * There are three cases:
4063 * 1. NULL, or None - this is a deletion.
4064 * 2. A string - this is a replacement.
4065 * 3. Anything else - this is an error.
4066 */
4067 if (line == Py_None || line == NULL)
4068 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004070 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004072 VimTryStart();
4073
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004075 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004077 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078 else
4079 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004080 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004082 if (save_curbuf == NULL)
4083 /* Only adjust marks if we managed to switch to a window that
4084 * holds the buffer, otherwise line numbers will be invalid. */
4085 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 }
4087
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004088 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004090 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 return FAIL;
4092
4093 if (len_change)
4094 *len_change = -1;
4095
4096 return OK;
4097 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004098 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004100 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101
4102 if (save == NULL)
4103 return FAIL;
4104
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004105 VimTryStart();
4106
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 /* We do not need to free "save" if ml_replace() consumes it. */
4108 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004109 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004110
4111 if (u_savesub((linenr_T)n) == FAIL)
4112 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004113 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 vim_free(save);
4115 }
4116 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4117 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004118 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 vim_free(save);
4120 }
4121 else
4122 changed_bytes((linenr_T)n, 0);
4123
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004124 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125
4126 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004127 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004128 check_cursor_col();
4129
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004130 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 return FAIL;
4132
4133 if (len_change)
4134 *len_change = 0;
4135
4136 return OK;
4137 }
4138 else
4139 {
4140 PyErr_BadArgument();
4141 return FAIL;
4142 }
4143}
4144
Bram Moolenaar19e60942011-06-19 00:27:51 +02004145/* Replace a range of lines in the specified buffer. The line numbers are in
4146 * Vim format (1-based). The range is from lo up to, but not including, hi.
4147 * The replacement lines are given as a Python list of string objects. The
4148 * list is checked for validity and correct format. Errors are returned as a
4149 * value of FAIL. The return value is OK on success.
4150 * If OK is returned and len_change is not NULL, *len_change
4151 * is set to the change in the buffer length.
4152 */
4153 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004154SetBufferLineList(
4155 buf_T *buf,
4156 PyInt lo,
4157 PyInt hi,
4158 PyObject *list,
4159 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004160{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004161 buf_T *save_curbuf = NULL;
4162 win_T *save_curwin = NULL;
4163 tabpage_T *save_curtab = NULL;
4164
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004165 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004166 * There are three cases:
4167 * 1. NULL, or None - this is a deletion.
4168 * 2. A list - this is a replacement.
4169 * 3. Anything else - this is an error.
4170 */
4171 if (list == Py_None || list == NULL)
4172 {
4173 PyInt i;
4174 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004175
4176 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004177 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004178 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004179
4180 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004181 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004182 else
4183 {
4184 for (i = 0; i < n; ++i)
4185 {
4186 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4187 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004188 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004189 break;
4190 }
4191 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004192 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004193 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004194 if (save_curbuf == NULL)
4195 /* Only adjust marks if we managed to switch to a window that
4196 * holds the buffer, otherwise line numbers will be invalid. */
4197 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004198 }
4199
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004200 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004201
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004202 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004203 return FAIL;
4204
4205 if (len_change)
4206 *len_change = -n;
4207
4208 return OK;
4209 }
4210 else if (PyList_Check(list))
4211 {
4212 PyInt i;
4213 PyInt new_len = PyList_Size(list);
4214 PyInt old_len = hi - lo;
4215 PyInt extra = 0; /* lines added to text, can be negative */
4216 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004217
4218 if (new_len == 0) /* avoid allocating zero bytes */
4219 array = NULL;
4220 else
4221 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004222 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004223 if (array == NULL)
4224 {
4225 PyErr_NoMemory();
4226 return FAIL;
4227 }
4228 }
4229
4230 for (i = 0; i < new_len; ++i)
4231 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004232 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004233
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004234 if (!(line = PyList_GetItem(list, i)) ||
4235 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004236 {
4237 while (i)
4238 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004239 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004240 return FAIL;
4241 }
4242 }
4243
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004244 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004245 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004246
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004247 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004248 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004249
4250 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004251 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004252
4253 /* If the size of the range is reducing (ie, new_len < old_len) we
4254 * need to delete some old_len. We do this at the start, by
4255 * repeatedly deleting line "lo".
4256 */
4257 if (!PyErr_Occurred())
4258 {
4259 for (i = 0; i < old_len - new_len; ++i)
4260 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4261 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004262 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004263 break;
4264 }
4265 extra -= i;
4266 }
4267
4268 /* For as long as possible, replace the existing old_len with the
4269 * new old_len. This is a more efficient operation, as it requires
4270 * less memory allocation and freeing.
4271 */
4272 if (!PyErr_Occurred())
4273 {
4274 for (i = 0; i < old_len && i < new_len; ++i)
4275 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4276 == FAIL)
4277 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004278 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004279 break;
4280 }
4281 }
4282 else
4283 i = 0;
4284
4285 /* Now we may need to insert the remaining new old_len. If we do, we
4286 * must free the strings as we finish with them (we can't pass the
4287 * responsibility to vim in this case).
4288 */
4289 if (!PyErr_Occurred())
4290 {
4291 while (i < new_len)
4292 {
4293 if (ml_append((linenr_T)(lo + i - 1),
4294 (char_u *)array[i], 0, FALSE) == FAIL)
4295 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004296 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004297 break;
4298 }
4299 vim_free(array[i]);
4300 ++i;
4301 ++extra;
4302 }
4303 }
4304
4305 /* Free any left-over old_len, as a result of an error */
4306 while (i < new_len)
4307 {
4308 vim_free(array[i]);
4309 ++i;
4310 }
4311
4312 /* Free the array of old_len. All of its contents have now
4313 * been dealt with (either freed, or the responsibility passed
4314 * to vim.
4315 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004316 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004317
4318 /* Adjust marks. Invalidate any which lie in the
4319 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004320 * Only adjust marks if we managed to switch to a window that holds
4321 * the buffer, otherwise line numbers will be invalid. */
4322 if (save_curbuf == NULL)
4323 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004324 (long)MAXLNUM, (long)extra);
4325 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4326
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004327 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004328 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4329
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004330 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004331 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004332
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004333 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004334 return FAIL;
4335
4336 if (len_change)
4337 *len_change = new_len - old_len;
4338
4339 return OK;
4340 }
4341 else
4342 {
4343 PyErr_BadArgument();
4344 return FAIL;
4345 }
4346}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004347
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004348/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004349 * The line number is in Vim format (1-based). The lines to be inserted are
4350 * given as a Python list of string objects or as a single string. The lines
4351 * to be added are checked for validity and correct format. Errors are
4352 * returned as a value of FAIL. The return value is OK on success.
4353 * If OK is returned and len_change is not NULL, *len_change
4354 * is set to the change in the buffer length.
4355 */
4356 static int
4357InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4358{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004359 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004360 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004361 tabpage_T *save_curtab = NULL;
4362
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004363 /* First of all, we check the type of the supplied Python object.
4364 * It must be a string or a list, or the call is in error.
4365 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004366 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004367 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004368 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004369
4370 if (str == NULL)
4371 return FAIL;
4372
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004373 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004374 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004375 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004376
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004377 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004378 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004379 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004380 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004381 else if (save_curbuf == NULL)
4382 /* Only adjust marks if we managed to switch to a window that
4383 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004384 appended_lines_mark((linenr_T)n, 1L);
4385
4386 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004387 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004388 update_screen(VALID);
4389
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004390 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004391 return FAIL;
4392
4393 if (len_change)
4394 *len_change = 1;
4395
4396 return OK;
4397 }
4398 else if (PyList_Check(lines))
4399 {
4400 PyInt i;
4401 PyInt size = PyList_Size(lines);
4402 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004403
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004404 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004405 if (array == NULL)
4406 {
4407 PyErr_NoMemory();
4408 return FAIL;
4409 }
4410
4411 for (i = 0; i < size; ++i)
4412 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004413 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004414
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004415 if (!(line = PyList_GetItem(lines, i)) ||
4416 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004417 {
4418 while (i)
4419 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004420 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421 return FAIL;
4422 }
4423 }
4424
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004425 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004426 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004427 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428
4429 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004430 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431 else
4432 {
4433 for (i = 0; i < size; ++i)
4434 {
4435 if (ml_append((linenr_T)(n + i),
4436 (char_u *)array[i], 0, FALSE) == FAIL)
4437 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004438 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004439
4440 /* Free the rest of the lines */
4441 while (i < size)
4442 vim_free(array[i++]);
4443
4444 break;
4445 }
4446 vim_free(array[i]);
4447 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004448 if (i > 0 && save_curbuf == NULL)
4449 /* Only adjust marks if we managed to switch to a window that
4450 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004451 appended_lines_mark((linenr_T)n, (long)i);
4452 }
4453
4454 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004455 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004456 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004457 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004458
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004459 update_screen(VALID);
4460
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004461 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004462 return FAIL;
4463
4464 if (len_change)
4465 *len_change = size;
4466
4467 return OK;
4468 }
4469 else
4470 {
4471 PyErr_BadArgument();
4472 return FAIL;
4473 }
4474}
4475
4476/*
4477 * Common routines for buffers and line ranges
4478 * -------------------------------------------
4479 */
4480
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004481typedef struct
4482{
4483 PyObject_HEAD
4484 buf_T *buf;
4485} BufferObject;
4486
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004487 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004488CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004489{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004490 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004491 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004492 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004493 return -1;
4494 }
4495
4496 return 0;
4497}
4498
4499 static PyObject *
4500RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4501{
4502 if (CheckBuffer(self))
4503 return NULL;
4504
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004505 if (end == -1)
4506 end = self->buf->b_ml.ml_line_count;
4507
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004508 if (n < 0)
4509 n += end - start + 1;
4510
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004511 if (n < 0 || n > end - start)
4512 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004513 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004514 return NULL;
4515 }
4516
4517 return GetBufferLine(self->buf, n+start);
4518}
4519
4520 static PyObject *
4521RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4522{
4523 PyInt size;
4524
4525 if (CheckBuffer(self))
4526 return NULL;
4527
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004528 if (end == -1)
4529 end = self->buf->b_ml.ml_line_count;
4530
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004531 size = end - start + 1;
4532
4533 if (lo < 0)
4534 lo = 0;
4535 else if (lo > size)
4536 lo = size;
4537 if (hi < 0)
4538 hi = 0;
4539 if (hi < lo)
4540 hi = lo;
4541 else if (hi > size)
4542 hi = size;
4543
4544 return GetBufferLineList(self->buf, lo+start, hi+start);
4545}
4546
4547 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004548RBAsItem(
4549 BufferObject *self,
4550 PyInt n,
4551 PyObject *valObject,
4552 PyInt start,
4553 PyInt end,
4554 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004555{
4556 PyInt len_change;
4557
4558 if (CheckBuffer(self))
4559 return -1;
4560
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004561 if (end == -1)
4562 end = self->buf->b_ml.ml_line_count;
4563
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004564 if (n < 0)
4565 n += end - start + 1;
4566
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004567 if (n < 0 || n > end - start)
4568 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004569 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004570 return -1;
4571 }
4572
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004573 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004574 return -1;
4575
4576 if (new_end)
4577 *new_end = end + len_change;
4578
4579 return 0;
4580}
4581
Bram Moolenaar19e60942011-06-19 00:27:51 +02004582 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004583RBAsSlice(
4584 BufferObject *self,
4585 PyInt lo,
4586 PyInt hi,
4587 PyObject *valObject,
4588 PyInt start,
4589 PyInt end,
4590 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004591{
4592 PyInt size;
4593 PyInt len_change;
4594
4595 /* Self must be a valid buffer */
4596 if (CheckBuffer(self))
4597 return -1;
4598
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004599 if (end == -1)
4600 end = self->buf->b_ml.ml_line_count;
4601
Bram Moolenaar19e60942011-06-19 00:27:51 +02004602 /* Sort out the slice range */
4603 size = end - start + 1;
4604
4605 if (lo < 0)
4606 lo = 0;
4607 else if (lo > size)
4608 lo = size;
4609 if (hi < 0)
4610 hi = 0;
4611 if (hi < lo)
4612 hi = lo;
4613 else if (hi > size)
4614 hi = size;
4615
4616 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004617 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004618 return -1;
4619
4620 if (new_end)
4621 *new_end = end + len_change;
4622
4623 return 0;
4624}
4625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004626
4627 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004628RBAppend(
4629 BufferObject *self,
4630 PyObject *args,
4631 PyInt start,
4632 PyInt end,
4633 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004634{
4635 PyObject *lines;
4636 PyInt len_change;
4637 PyInt max;
4638 PyInt n;
4639
4640 if (CheckBuffer(self))
4641 return NULL;
4642
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004643 if (end == -1)
4644 end = self->buf->b_ml.ml_line_count;
4645
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004646 max = n = end - start + 1;
4647
4648 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4649 return NULL;
4650
4651 if (n < 0 || n > max)
4652 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004653 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004654 return NULL;
4655 }
4656
4657 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4658 return NULL;
4659
4660 if (new_end)
4661 *new_end = end + len_change;
4662
4663 Py_INCREF(Py_None);
4664 return Py_None;
4665}
4666
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004667/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004668 */
4669
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004670static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004671static PySequenceMethods RangeAsSeq;
4672static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004673
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004674typedef struct
4675{
4676 PyObject_HEAD
4677 BufferObject *buf;
4678 PyInt start;
4679 PyInt end;
4680} RangeObject;
4681
4682 static PyObject *
4683RangeNew(buf_T *buf, PyInt start, PyInt end)
4684{
4685 BufferObject *bufr;
4686 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004687 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004688 if (self == NULL)
4689 return NULL;
4690
4691 bufr = (BufferObject *)BufferNew(buf);
4692 if (bufr == NULL)
4693 {
4694 Py_DECREF(self);
4695 return NULL;
4696 }
4697 Py_INCREF(bufr);
4698
4699 self->buf = bufr;
4700 self->start = start;
4701 self->end = end;
4702
4703 return (PyObject *)(self);
4704}
4705
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004706 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004707RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004708{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004709 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004710 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004711 PyObject_GC_Del((void *)(self));
4712}
4713
4714 static int
4715RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4716{
4717 Py_VISIT(((PyObject *)(self->buf)));
4718 return 0;
4719}
4720
4721 static int
4722RangeClear(RangeObject *self)
4723{
4724 Py_CLEAR(self->buf);
4725 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004726}
4727
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004728 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004729RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004730{
4731 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004732 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004733 return -1; /* ??? */
4734
Bram Moolenaard6e39182013-05-21 18:30:34 +02004735 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004736}
4737
4738 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004739RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004740{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004741 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004742}
4743
4744 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004745RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004746{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004747 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004748}
4749
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004750static char *RangeAttrs[] = {
4751 "start", "end",
4752 NULL
4753};
4754
4755 static PyObject *
4756RangeDir(PyObject *self)
4757{
4758 return ObjectDir(self, RangeAttrs);
4759}
4760
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004761 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004762RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004763{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004764 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004765}
4766
4767 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004768RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004769{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004770 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004771 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4772 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004773 else
4774 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004775 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004776
4777 if (name == NULL)
4778 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004779
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004780 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004781 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004782 }
4783}
4784
4785static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004786 /* name, function, calling, documentation */
4787 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004788 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4789 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004790};
4791
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004792static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004793static PySequenceMethods BufferAsSeq;
4794static PyMappingMethods BufferAsMapping;
4795
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004796 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004797BufferNew(buf_T *buf)
4798{
4799 /* We need to handle deletion of buffers underneath us.
4800 * If we add a "b_python*_ref" field to the buf_T structure,
4801 * then we can get at it in buf_freeall() in vim. We then
4802 * need to create only ONE Python object per buffer - if
4803 * we try to create a second, just INCREF the existing one
4804 * and return it. The (single) Python object referring to
4805 * the buffer is stored in "b_python*_ref".
4806 * Question: what to do on a buf_freeall(). We'll probably
4807 * have to either delete the Python object (DECREF it to
4808 * zero - a bad idea, as it leaves dangling refs!) or
4809 * set the buf_T * value to an invalid value (-1?), which
4810 * means we need checks in all access functions... Bah.
4811 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004812 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004813 * b_python_ref and b_python3_ref fields respectively.
4814 */
4815
4816 BufferObject *self;
4817
4818 if (BUF_PYTHON_REF(buf) != NULL)
4819 {
4820 self = BUF_PYTHON_REF(buf);
4821 Py_INCREF(self);
4822 }
4823 else
4824 {
4825 self = PyObject_NEW(BufferObject, &BufferType);
4826 if (self == NULL)
4827 return NULL;
4828 self->buf = buf;
4829 BUF_PYTHON_REF(buf) = self;
4830 }
4831
4832 return (PyObject *)(self);
4833}
4834
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004835 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004836BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004837{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004838 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4839 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004840
4841 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004842}
4843
Bram Moolenaar971db462013-05-12 18:44:48 +02004844 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004845BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004846{
4847 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004848 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004849 return -1; /* ??? */
4850
Bram Moolenaard6e39182013-05-21 18:30:34 +02004851 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004852}
4853
4854 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004855BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004856{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004857 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004858}
4859
4860 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004861BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004862{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004863 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004864}
4865
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004866static char *BufferAttrs[] = {
4867 "name", "number", "vars", "options", "valid",
4868 NULL
4869};
4870
4871 static PyObject *
4872BufferDir(PyObject *self)
4873{
4874 return ObjectDir(self, BufferAttrs);
4875}
4876
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004877 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004878BufferAttrValid(BufferObject *self, char *name)
4879{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004880 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004881
4882 if (strcmp(name, "valid") != 0)
4883 return NULL;
4884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004885 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4886 Py_INCREF(ret);
4887 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004888}
4889
4890 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004891BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004892{
4893 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004894 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004895 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004896 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004897 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004898 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004899 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004900 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004901 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4902 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004903 else if (strcmp(name, "__members__") == 0)
4904 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004905 else
4906 return NULL;
4907}
4908
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004909 static int
4910BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4911{
4912 if (CheckBuffer(self))
4913 return -1;
4914
4915 if (strcmp(name, "name") == 0)
4916 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004917 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004918 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004919 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004920 PyObject *todecref;
4921
4922 if (!(val = StringToChars(valObject, &todecref)))
4923 return -1;
4924
4925 VimTryStart();
4926 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4927 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004928 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004929 aucmd_restbuf(&aco);
4930 Py_XDECREF(todecref);
4931 if (VimTryEnd())
4932 return -1;
4933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004934 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004935 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004936 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004937 return -1;
4938 }
4939 return 0;
4940 }
4941 else
4942 {
4943 PyErr_SetString(PyExc_AttributeError, name);
4944 return -1;
4945 }
4946}
4947
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004948 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004949BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004950{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004951 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004952}
4953
4954 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004955BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004956{
4957 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004958 char_u *pmark;
4959 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004960 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004961 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004962
Bram Moolenaard6e39182013-05-21 18:30:34 +02004963 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004964 return NULL;
4965
Bram Moolenaar389a1792013-06-23 13:00:44 +02004966 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004967 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004968
Bram Moolenaar389a1792013-06-23 13:00:44 +02004969 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004970 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004971 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004972 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004973 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004974 return NULL;
4975 }
4976
4977 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004978
4979 Py_XDECREF(todecref);
4980
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004981 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004982 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004983 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004984 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004985 if (VimTryEnd())
4986 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004987
4988 if (posp == NULL)
4989 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004990 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004991 return NULL;
4992 }
4993
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004994 if (posp->lnum <= 0)
4995 {
4996 /* Or raise an error? */
4997 Py_INCREF(Py_None);
4998 return Py_None;
4999 }
5000
5001 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5002}
5003
5004 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005005BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005006{
5007 PyInt start;
5008 PyInt end;
5009
Bram Moolenaard6e39182013-05-21 18:30:34 +02005010 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005011 return NULL;
5012
5013 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5014 return NULL;
5015
Bram Moolenaard6e39182013-05-21 18:30:34 +02005016 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005017}
5018
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005019 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005020BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005021{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005022 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005023 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005024 else
5025 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005026 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005027
5028 if (name == NULL)
5029 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005031 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005032 }
5033}
5034
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005035static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005036 /* name, function, calling, documentation */
5037 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005038 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005039 {"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 +02005040 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5041 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005042};
5043
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005044/*
5045 * Buffer list object - Implementation
5046 */
5047
5048static PyTypeObject BufMapType;
5049
5050typedef struct
5051{
5052 PyObject_HEAD
5053} BufMapObject;
5054
5055 static PyInt
5056BufMapLength(PyObject *self UNUSED)
5057{
5058 buf_T *b = firstbuf;
5059 PyInt n = 0;
5060
5061 while (b)
5062 {
5063 ++n;
5064 b = b->b_next;
5065 }
5066
5067 return n;
5068}
5069
5070 static PyObject *
5071BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5072{
5073 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005074 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005075
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005076 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005077 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005078
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005079 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005080
5081 if (b)
5082 return BufferNew(b);
5083 else
5084 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005085 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005086 return NULL;
5087 }
5088}
5089
5090 static void
5091BufMapIterDestruct(PyObject *buffer)
5092{
5093 /* Iteration was stopped before all buffers were processed */
5094 if (buffer)
5095 {
5096 Py_DECREF(buffer);
5097 }
5098}
5099
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005100 static int
5101BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5102{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005103 if (buffer)
5104 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005105 return 0;
5106}
5107
5108 static int
5109BufMapIterClear(PyObject **buffer)
5110{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005111 if (*buffer)
5112 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005113 return 0;
5114}
5115
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005116 static PyObject *
5117BufMapIterNext(PyObject **buffer)
5118{
5119 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005120 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005121
5122 if (!*buffer)
5123 return NULL;
5124
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005125 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005126
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005127 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005128 {
5129 *buffer = NULL;
5130 return NULL;
5131 }
5132
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005133 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005134 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005135 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005136 return NULL;
5137 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005138 /* Do not increment reference: we no longer hold it (decref), but whoever
5139 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005140 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005141}
5142
5143 static PyObject *
5144BufMapIter(PyObject *self UNUSED)
5145{
5146 PyObject *buffer;
5147
5148 buffer = BufferNew(firstbuf);
5149 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005150 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5151 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005152}
5153
5154static PyMappingMethods BufMapAsMapping = {
5155 (lenfunc) BufMapLength,
5156 (binaryfunc) BufMapItem,
5157 (objobjargproc) 0,
5158};
5159
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005160/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005161 */
5162
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005163static char *CurrentAttrs[] = {
5164 "buffer", "window", "line", "range", "tabpage",
5165 NULL
5166};
5167
5168 static PyObject *
5169CurrentDir(PyObject *self)
5170{
5171 return ObjectDir(self, CurrentAttrs);
5172}
5173
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005174 static PyObject *
5175CurrentGetattr(PyObject *self UNUSED, char *name)
5176{
5177 if (strcmp(name, "buffer") == 0)
5178 return (PyObject *)BufferNew(curbuf);
5179 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005180 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005181 else if (strcmp(name, "tabpage") == 0)
5182 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005183 else if (strcmp(name, "line") == 0)
5184 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5185 else if (strcmp(name, "range") == 0)
5186 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005187 else if (strcmp(name, "__members__") == 0)
5188 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005189 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005190#if PY_MAJOR_VERSION < 3
5191 return Py_FindMethod(WindowMethods, self, name);
5192#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005193 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005194#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005195}
5196
5197 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005198CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005199{
5200 if (strcmp(name, "line") == 0)
5201 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005202 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5203 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005204 return -1;
5205
5206 return 0;
5207 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005208 else if (strcmp(name, "buffer") == 0)
5209 {
5210 int count;
5211
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005212 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005213 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005214 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005215 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005216 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005217 return -1;
5218 }
5219
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005220 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005221 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005222 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005223
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005224 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005225 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5226 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005227 if (VimTryEnd())
5228 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005229 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005230 return -1;
5231 }
5232
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005233 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005234 }
5235 else if (strcmp(name, "window") == 0)
5236 {
5237 int count;
5238
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005239 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005240 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005241 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005242 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005243 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005244 return -1;
5245 }
5246
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005247 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005248 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005249 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005250
5251 if (!count)
5252 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005253 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005254 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005255 return -1;
5256 }
5257
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005258 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005259 win_goto(((WindowObject *)(valObject))->win);
5260 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005261 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005262 if (VimTryEnd())
5263 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005264 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005265 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005266 return -1;
5267 }
5268
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005269 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005270 }
5271 else if (strcmp(name, "tabpage") == 0)
5272 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005273 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005274 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005275 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005276 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005277 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005278 return -1;
5279 }
5280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005281 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005282 return -1;
5283
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005284 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005285 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5286 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005287 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005288 if (VimTryEnd())
5289 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005290 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005291 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005292 return -1;
5293 }
5294
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005295 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005296 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005297 else
5298 {
5299 PyErr_SetString(PyExc_AttributeError, name);
5300 return -1;
5301 }
5302}
5303
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005304static struct PyMethodDef CurrentMethods[] = {
5305 /* name, function, calling, documentation */
5306 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5307 { NULL, NULL, 0, NULL}
5308};
5309
Bram Moolenaardb913952012-06-29 12:54:53 +02005310 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005311init_range_cmd(exarg_T *eap)
5312{
5313 RangeStart = eap->line1;
5314 RangeEnd = eap->line2;
5315}
5316
5317 static void
5318init_range_eval(typval_T *rettv UNUSED)
5319{
5320 RangeStart = (PyInt) curwin->w_cursor.lnum;
5321 RangeEnd = RangeStart;
5322}
5323
5324 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005325run_cmd(const char *cmd, void *arg UNUSED
5326#ifdef PY_CAN_RECURSE
5327 , PyGILState_STATE *pygilstate UNUSED
5328#endif
5329 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005330{
Bram Moolenaar41009372013-07-01 22:03:04 +02005331 PyObject *run_ret;
5332 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5333 if (run_ret != NULL)
5334 {
5335 Py_DECREF(run_ret);
5336 }
5337 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5338 {
5339 EMSG2(_(e_py_systemexit), "python");
5340 PyErr_Clear();
5341 }
5342 else
5343 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005344}
5345
5346static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5347static int code_hdr_len = 30;
5348
5349 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005350run_do(const char *cmd, void *arg UNUSED
5351#ifdef PY_CAN_RECURSE
5352 , PyGILState_STATE *pygilstate
5353#endif
5354 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005355{
5356 PyInt lnum;
5357 size_t len;
5358 char *code;
5359 int status;
5360 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005361 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005362
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005363 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005364 {
5365 EMSG(_("cannot save undo information"));
5366 return;
5367 }
5368
5369 len = code_hdr_len + STRLEN(cmd);
5370 code = PyMem_New(char, len + 1);
5371 memcpy(code, code_hdr, code_hdr_len);
5372 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005373 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5374 status = -1;
5375 if (run_ret != NULL)
5376 {
5377 status = 0;
5378 Py_DECREF(run_ret);
5379 }
5380 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5381 {
5382 PyMem_Free(code);
5383 EMSG2(_(e_py_systemexit), "python");
5384 PyErr_Clear();
5385 return;
5386 }
5387 else
5388 PyErr_PrintEx(1);
5389
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005390 PyMem_Free(code);
5391
5392 if (status)
5393 {
5394 EMSG(_("failed to run the code"));
5395 return;
5396 }
5397
5398 status = 0;
5399 pymain = PyImport_AddModule("__main__");
5400 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005401#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005402 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005403#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005404
5405 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5406 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005407 PyObject *line;
5408 PyObject *linenr;
5409 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005410
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005411#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005412 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005413#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005414 if (!(line = GetBufferLine(curbuf, lnum)))
5415 goto err;
5416 if (!(linenr = PyInt_FromLong((long) lnum)))
5417 {
5418 Py_DECREF(line);
5419 goto err;
5420 }
5421 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5422 Py_DECREF(line);
5423 Py_DECREF(linenr);
5424 if (!ret)
5425 goto err;
5426
5427 if (ret != Py_None)
5428 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5429 goto err;
5430
5431 Py_XDECREF(ret);
5432 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005433#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005434 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005435#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005436 }
5437 goto out;
5438err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005439#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005440 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005441#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005442 PyErr_PrintEx(0);
5443 PythonIO_Flush();
5444 status = 1;
5445out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005446#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005447 if (!status)
5448 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005449#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005450 Py_DECREF(pyfunc);
5451 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5452 if (status)
5453 return;
5454 check_cursor();
5455 update_curbuf(NOT_VALID);
5456}
5457
5458 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005459run_eval(const char *cmd, typval_T *rettv
5460#ifdef PY_CAN_RECURSE
5461 , PyGILState_STATE *pygilstate UNUSED
5462#endif
5463 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005464{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005465 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005466
Bram Moolenaar41009372013-07-01 22:03:04 +02005467 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005468 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005469 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005470 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005471 {
5472 EMSG2(_(e_py_systemexit), "python");
5473 PyErr_Clear();
5474 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005475 else
5476 {
5477 if (PyErr_Occurred() && !msg_silent)
5478 PyErr_PrintEx(0);
5479 EMSG(_("E858: Eval did not return a valid python object"));
5480 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005481 }
5482 else
5483 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005484 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005485 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005486 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005487 }
5488 PyErr_Clear();
5489}
5490
5491 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005492set_ref_in_py(const int copyID)
5493{
5494 pylinkedlist_T *cur;
5495 dict_T *dd;
5496 list_T *ll;
5497
5498 if (lastdict != NULL)
5499 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5500 {
5501 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5502 if (dd->dv_copyID != copyID)
5503 {
5504 dd->dv_copyID = copyID;
5505 set_ref_in_ht(&dd->dv_hashtab, copyID);
5506 }
5507 }
5508
5509 if (lastlist != NULL)
5510 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5511 {
5512 ll = ((ListObject *) (cur->pll_obj))->list;
5513 if (ll->lv_copyID != copyID)
5514 {
5515 ll->lv_copyID = copyID;
5516 set_ref_in_list(ll, copyID);
5517 }
5518 }
5519}
5520
5521 static int
5522set_string_copy(char_u *str, typval_T *tv)
5523{
5524 tv->vval.v_string = vim_strsave(str);
5525 if (tv->vval.v_string == NULL)
5526 {
5527 PyErr_NoMemory();
5528 return -1;
5529 }
5530 return 0;
5531}
5532
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005533 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005534pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005535{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005536 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005537 char_u *key;
5538 dictitem_T *di;
5539 PyObject *keyObject;
5540 PyObject *valObject;
5541 Py_ssize_t iter = 0;
5542
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005543 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005544 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005545
5546 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005547 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005548
5549 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5550 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005551 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005552
Bram Moolenaara03e6312013-05-29 22:49:26 +02005553 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005554 {
5555 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005556 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005557 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005558
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005559 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005560 {
5561 dict_unref(dict);
5562 return -1;
5563 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005564
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005565 if (*key == NUL)
5566 {
5567 dict_unref(dict);
5568 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005569 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005570 return -1;
5571 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005572
5573 di = dictitem_alloc(key);
5574
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005575 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005576
5577 if (di == NULL)
5578 {
5579 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005580 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005581 return -1;
5582 }
5583 di->di_tv.v_lock = 0;
5584
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005585 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005586 {
5587 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005588 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005589 return -1;
5590 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005591
5592 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005593 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005594 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005595 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005596 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005597 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005598 return -1;
5599 }
5600 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005601
5602 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005603 return 0;
5604}
5605
5606 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005607pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005608{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005609 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005610 char_u *key;
5611 dictitem_T *di;
5612 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005613 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005614 PyObject *keyObject;
5615 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005616
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005617 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005618 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005619
5620 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005621 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005622
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005623 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005624 {
5625 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005626 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005627 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005628
5629 if (!(iterator = PyObject_GetIter(list)))
5630 {
5631 dict_unref(dict);
5632 Py_DECREF(list);
5633 return -1;
5634 }
5635 Py_DECREF(list);
5636
5637 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005638 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005639 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005640
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005641 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005642 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005643 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005644 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005645 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005646 return -1;
5647 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005648
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005649 if (*key == NUL)
5650 {
5651 Py_DECREF(keyObject);
5652 Py_DECREF(iterator);
5653 Py_XDECREF(todecref);
5654 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005655 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005656 return -1;
5657 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005658
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005659 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005660 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005661 Py_DECREF(keyObject);
5662 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005663 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005664 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005665 return -1;
5666 }
5667
5668 di = dictitem_alloc(key);
5669
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005670 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005671 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005672
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005673 if (di == NULL)
5674 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005675 Py_DECREF(iterator);
5676 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005677 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005678 PyErr_NoMemory();
5679 return -1;
5680 }
5681 di->di_tv.v_lock = 0;
5682
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005683 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005684 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005685 Py_DECREF(iterator);
5686 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005687 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005688 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005689 return -1;
5690 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005691
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005692 Py_DECREF(valObject);
5693
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005694 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005695 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005696 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005697 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005698 dictitem_free(di);
5699 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005700 return -1;
5701 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005702 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005703 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005704 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005705 return 0;
5706}
5707
5708 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005709pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005710{
5711 list_T *l;
5712
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005713 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005714 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005715
5716 tv->v_type = VAR_LIST;
5717 tv->vval.v_list = l;
5718
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005719 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005720 {
5721 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005722 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005723 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005724
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005725 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005726 return 0;
5727}
5728
Bram Moolenaardb913952012-06-29 12:54:53 +02005729typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5730
5731 static int
5732convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005733 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005734{
5735 PyObject *capsule;
5736 char hexBuf[sizeof(void *) * 2 + 3];
5737
5738 sprintf(hexBuf, "%p", obj);
5739
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005740# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005741 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005742# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005743 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005744# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005745 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005746 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005747# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005748 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005749# else
5750 capsule = PyCObject_FromVoidPtr(tv, NULL);
5751# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005752 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5753 {
5754 Py_DECREF(capsule);
5755 tv->v_type = VAR_UNKNOWN;
5756 return -1;
5757 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005758
5759 Py_DECREF(capsule);
5760
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005761 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005762 {
5763 tv->v_type = VAR_UNKNOWN;
5764 return -1;
5765 }
5766 /* As we are not using copy_tv which increments reference count we must
5767 * do it ourself. */
5768 switch(tv->v_type)
5769 {
5770 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5771 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5772 }
5773 }
5774 else
5775 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005776 typval_T *v;
5777
5778# ifdef PY_USE_CAPSULE
5779 v = PyCapsule_GetPointer(capsule, NULL);
5780# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005781 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005782# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005783 copy_tv(v, tv);
5784 }
5785 return 0;
5786}
5787
5788 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005789ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5790{
5791 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005792 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005793
5794 if (!(lookup_dict = PyDict_New()))
5795 return -1;
5796
5797 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5798 {
5799 tv->v_type = VAR_DICT;
5800 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5801 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005802 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005803 }
5804 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005805 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005806 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005807 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005808 else
5809 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005810 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005811 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005812 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005813 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005814 }
5815 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005816 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005817}
5818
5819 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005820ConvertFromPyObject(PyObject *obj, typval_T *tv)
5821{
5822 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005823 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005824
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005825 if (!(lookup_dict = PyDict_New()))
5826 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005827 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005828 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005829 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005830}
5831
5832 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005833_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005834{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005835 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005836 {
5837 tv->v_type = VAR_DICT;
5838 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5839 ++tv->vval.v_dict->dv_refcount;
5840 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005841 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005842 {
5843 tv->v_type = VAR_LIST;
5844 tv->vval.v_list = (((ListObject *)(obj))->list);
5845 ++tv->vval.v_list->lv_refcount;
5846 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005847 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005848 {
5849 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5850 return -1;
5851
5852 tv->v_type = VAR_FUNC;
5853 func_ref(tv->vval.v_string);
5854 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005855 else if (PyBytes_Check(obj))
5856 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005857 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005858
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005859 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005860 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005861 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005862 return -1;
5863
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005864 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005865 return -1;
5866
5867 tv->v_type = VAR_STRING;
5868 }
5869 else if (PyUnicode_Check(obj))
5870 {
5871 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005872 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005873
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005874 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005875 if (bytes == NULL)
5876 return -1;
5877
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005878 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005879 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005880 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005881 return -1;
5882
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005883 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005884 {
5885 Py_XDECREF(bytes);
5886 return -1;
5887 }
5888 Py_XDECREF(bytes);
5889
5890 tv->v_type = VAR_STRING;
5891 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005892#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005893 else if (PyInt_Check(obj))
5894 {
5895 tv->v_type = VAR_NUMBER;
5896 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005897 if (PyErr_Occurred())
5898 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005899 }
5900#endif
5901 else if (PyLong_Check(obj))
5902 {
5903 tv->v_type = VAR_NUMBER;
5904 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005905 if (PyErr_Occurred())
5906 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005907 }
5908 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005909 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005910#ifdef FEAT_FLOAT
5911 else if (PyFloat_Check(obj))
5912 {
5913 tv->v_type = VAR_FLOAT;
5914 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5915 }
5916#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005917 else if (PyObject_HasAttrString(obj, "keys"))
5918 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005919 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005920 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005921 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005922 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005923 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005924 else if (PyNumber_Check(obj))
5925 {
5926 PyObject *num;
5927
5928 if (!(num = PyNumber_Long(obj)))
5929 return -1;
5930
5931 tv->v_type = VAR_NUMBER;
5932 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5933
5934 Py_DECREF(num);
5935 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005936 else
5937 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005938 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005939 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005940 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005941 return -1;
5942 }
5943 return 0;
5944}
5945
5946 static PyObject *
5947ConvertToPyObject(typval_T *tv)
5948{
5949 if (tv == NULL)
5950 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005951 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005952 return NULL;
5953 }
5954 switch (tv->v_type)
5955 {
5956 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005957 return PyBytes_FromString(tv->vval.v_string == NULL
5958 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005959 case VAR_NUMBER:
5960 return PyLong_FromLong((long) tv->vval.v_number);
5961#ifdef FEAT_FLOAT
5962 case VAR_FLOAT:
5963 return PyFloat_FromDouble((double) tv->vval.v_float);
5964#endif
5965 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005966 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005967 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005968 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005969 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005970 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005971 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005972 case VAR_UNKNOWN:
5973 Py_INCREF(Py_None);
5974 return Py_None;
5975 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005976 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005977 return NULL;
5978 }
5979}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005980
5981typedef struct
5982{
5983 PyObject_HEAD
5984} CurrentObject;
5985static PyTypeObject CurrentType;
5986
5987 static void
5988init_structs(void)
5989{
5990 vim_memset(&OutputType, 0, sizeof(OutputType));
5991 OutputType.tp_name = "vim.message";
5992 OutputType.tp_basicsize = sizeof(OutputObject);
5993 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5994 OutputType.tp_doc = "vim message object";
5995 OutputType.tp_methods = OutputMethods;
5996#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005997 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5998 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005999 OutputType.tp_alloc = call_PyType_GenericAlloc;
6000 OutputType.tp_new = call_PyType_GenericNew;
6001 OutputType.tp_free = call_PyObject_Free;
6002#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006003 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6004 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006005#endif
6006
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006007 vim_memset(&IterType, 0, sizeof(IterType));
6008 IterType.tp_name = "vim.iter";
6009 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006010 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006011 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006012 IterType.tp_iter = (getiterfunc)IterIter;
6013 IterType.tp_iternext = (iternextfunc)IterNext;
6014 IterType.tp_dealloc = (destructor)IterDestructor;
6015 IterType.tp_traverse = (traverseproc)IterTraverse;
6016 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006017
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006018 vim_memset(&BufferType, 0, sizeof(BufferType));
6019 BufferType.tp_name = "vim.buffer";
6020 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006021 BufferType.tp_dealloc = (destructor)BufferDestructor;
6022 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006023 BufferType.tp_as_sequence = &BufferAsSeq;
6024 BufferType.tp_as_mapping = &BufferAsMapping;
6025 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6026 BufferType.tp_doc = "vim buffer object";
6027 BufferType.tp_methods = BufferMethods;
6028#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006029 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006030 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006031 BufferType.tp_alloc = call_PyType_GenericAlloc;
6032 BufferType.tp_new = call_PyType_GenericNew;
6033 BufferType.tp_free = call_PyObject_Free;
6034#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006035 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006036 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006037#endif
6038
6039 vim_memset(&WindowType, 0, sizeof(WindowType));
6040 WindowType.tp_name = "vim.window";
6041 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006042 WindowType.tp_dealloc = (destructor)WindowDestructor;
6043 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006044 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006045 WindowType.tp_doc = "vim Window object";
6046 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006047 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6048 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006049#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006050 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6051 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006052 WindowType.tp_alloc = call_PyType_GenericAlloc;
6053 WindowType.tp_new = call_PyType_GenericNew;
6054 WindowType.tp_free = call_PyObject_Free;
6055#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006056 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6057 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006058#endif
6059
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006060 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6061 TabPageType.tp_name = "vim.tabpage";
6062 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006063 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6064 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006065 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6066 TabPageType.tp_doc = "vim tab page object";
6067 TabPageType.tp_methods = TabPageMethods;
6068#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006069 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006070 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6071 TabPageType.tp_new = call_PyType_GenericNew;
6072 TabPageType.tp_free = call_PyObject_Free;
6073#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006074 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006075#endif
6076
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006077 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6078 BufMapType.tp_name = "vim.bufferlist";
6079 BufMapType.tp_basicsize = sizeof(BufMapObject);
6080 BufMapType.tp_as_mapping = &BufMapAsMapping;
6081 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006082 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006083 BufferType.tp_doc = "vim buffer list";
6084
6085 vim_memset(&WinListType, 0, sizeof(WinListType));
6086 WinListType.tp_name = "vim.windowlist";
6087 WinListType.tp_basicsize = sizeof(WinListType);
6088 WinListType.tp_as_sequence = &WinListAsSeq;
6089 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6090 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006091 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006092
6093 vim_memset(&TabListType, 0, sizeof(TabListType));
6094 TabListType.tp_name = "vim.tabpagelist";
6095 TabListType.tp_basicsize = sizeof(TabListType);
6096 TabListType.tp_as_sequence = &TabListAsSeq;
6097 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6098 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006099
6100 vim_memset(&RangeType, 0, sizeof(RangeType));
6101 RangeType.tp_name = "vim.range";
6102 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006103 RangeType.tp_dealloc = (destructor)RangeDestructor;
6104 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006105 RangeType.tp_as_sequence = &RangeAsSeq;
6106 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006107 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006108 RangeType.tp_doc = "vim Range object";
6109 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006110 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6111 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006112#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006113 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006114 RangeType.tp_alloc = call_PyType_GenericAlloc;
6115 RangeType.tp_new = call_PyType_GenericNew;
6116 RangeType.tp_free = call_PyObject_Free;
6117#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006118 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006119#endif
6120
6121 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6122 CurrentType.tp_name = "vim.currentdata";
6123 CurrentType.tp_basicsize = sizeof(CurrentObject);
6124 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6125 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006126 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006127#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006128 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6129 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006130#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006131 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6132 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006133#endif
6134
6135 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6136 DictionaryType.tp_name = "vim.dictionary";
6137 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006138 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006139 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006140 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006141 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006142 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6143 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006144 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6145 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6146 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006147#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006148 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6149 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006150#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006151 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6152 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006153#endif
6154
6155 vim_memset(&ListType, 0, sizeof(ListType));
6156 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006157 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006158 ListType.tp_basicsize = sizeof(ListObject);
6159 ListType.tp_as_sequence = &ListAsSeq;
6160 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006161 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006162 ListType.tp_doc = "list pushing modifications to vim structure";
6163 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006164 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006165 ListType.tp_new = (newfunc)ListConstructor;
6166 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006167#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006168 ListType.tp_getattro = (getattrofunc)ListGetattro;
6169 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006170#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006171 ListType.tp_getattr = (getattrfunc)ListGetattr;
6172 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006173#endif
6174
6175 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006176 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006177 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006178 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6179 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006180 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006181 FunctionType.tp_doc = "object that calls vim function";
6182 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006183 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006184 FunctionType.tp_new = (newfunc)FunctionConstructor;
6185 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006186#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006187 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006188#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006189 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006190#endif
6191
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006192 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6193 OptionsType.tp_name = "vim.options";
6194 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006195 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006196 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006197 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006198 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006199 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006200 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6201 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6202 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006203
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006204 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6205 LoaderType.tp_name = "vim.Loader";
6206 LoaderType.tp_basicsize = sizeof(LoaderObject);
6207 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6208 LoaderType.tp_doc = "vim message object";
6209 LoaderType.tp_methods = LoaderMethods;
6210 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6211
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006212#if PY_MAJOR_VERSION >= 3
6213 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6214 vimmodule.m_name = "vim";
6215 vimmodule.m_doc = "Vim Python interface\n";
6216 vimmodule.m_size = -1;
6217 vimmodule.m_methods = VimMethods;
6218#endif
6219}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006220
6221#define PYTYPE_READY(type) \
6222 if (PyType_Ready(&type)) \
6223 return -1;
6224
6225 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006226init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006227{
6228 PYTYPE_READY(IterType);
6229 PYTYPE_READY(BufferType);
6230 PYTYPE_READY(RangeType);
6231 PYTYPE_READY(WindowType);
6232 PYTYPE_READY(TabPageType);
6233 PYTYPE_READY(BufMapType);
6234 PYTYPE_READY(WinListType);
6235 PYTYPE_READY(TabListType);
6236 PYTYPE_READY(CurrentType);
6237 PYTYPE_READY(DictionaryType);
6238 PYTYPE_READY(ListType);
6239 PYTYPE_READY(FunctionType);
6240 PYTYPE_READY(OptionsType);
6241 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006242 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006243 return 0;
6244}
6245
6246 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006247init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006248{
6249 PyObject *path;
6250 PyObject *path_hook;
6251 PyObject *path_hooks;
6252
6253 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6254 return -1;
6255
6256 if (!(path_hooks = PySys_GetObject("path_hooks")))
6257 {
6258 PyErr_Clear();
6259 path_hooks = PyList_New(1);
6260 PyList_SET_ITEM(path_hooks, 0, path_hook);
6261 if (PySys_SetObject("path_hooks", path_hooks))
6262 {
6263 Py_DECREF(path_hooks);
6264 return -1;
6265 }
6266 Py_DECREF(path_hooks);
6267 }
6268 else if (PyList_Check(path_hooks))
6269 {
6270 if (PyList_Append(path_hooks, path_hook))
6271 {
6272 Py_DECREF(path_hook);
6273 return -1;
6274 }
6275 Py_DECREF(path_hook);
6276 }
6277 else
6278 {
6279 VimTryStart();
6280 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6281 "You should now do the following:\n"
6282 "- append vim.path_hook to sys.path_hooks\n"
6283 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6284 VimTryEnd(); /* Discard the error */
6285 Py_DECREF(path_hook);
6286 return 0;
6287 }
6288
6289 if (!(path = PySys_GetObject("path")))
6290 {
6291 PyErr_Clear();
6292 path = PyList_New(1);
6293 Py_INCREF(vim_special_path_object);
6294 PyList_SET_ITEM(path, 0, vim_special_path_object);
6295 if (PySys_SetObject("path", path))
6296 {
6297 Py_DECREF(path);
6298 return -1;
6299 }
6300 Py_DECREF(path);
6301 }
6302 else if (PyList_Check(path))
6303 {
6304 if (PyList_Append(path, vim_special_path_object))
6305 return -1;
6306 }
6307 else
6308 {
6309 VimTryStart();
6310 EMSG(_("Failed to set path: sys.path is not a list\n"
6311 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6312 VimTryEnd(); /* Discard the error */
6313 }
6314
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006315 return 0;
6316}
6317
6318static BufMapObject TheBufferMap =
6319{
6320 PyObject_HEAD_INIT(&BufMapType)
6321};
6322
6323static WinListObject TheWindowList =
6324{
6325 PyObject_HEAD_INIT(&WinListType)
6326 NULL
6327};
6328
6329static CurrentObject TheCurrent =
6330{
6331 PyObject_HEAD_INIT(&CurrentType)
6332};
6333
6334static TabListObject TheTabPageList =
6335{
6336 PyObject_HEAD_INIT(&TabListType)
6337};
6338
6339static struct numeric_constant {
6340 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006341 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006342} numeric_constants[] = {
6343 {"VAR_LOCKED", VAR_LOCKED},
6344 {"VAR_FIXED", VAR_FIXED},
6345 {"VAR_SCOPE", VAR_SCOPE},
6346 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6347};
6348
6349static struct object_constant {
6350 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006351 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006352} object_constants[] = {
6353 {"buffers", (PyObject *)(void *)&TheBufferMap},
6354 {"windows", (PyObject *)(void *)&TheWindowList},
6355 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6356 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006357
6358 {"Buffer", (PyObject *)&BufferType},
6359 {"Range", (PyObject *)&RangeType},
6360 {"Window", (PyObject *)&WindowType},
6361 {"TabPage", (PyObject *)&TabPageType},
6362 {"Dictionary", (PyObject *)&DictionaryType},
6363 {"List", (PyObject *)&ListType},
6364 {"Function", (PyObject *)&FunctionType},
6365 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006366 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006367};
6368
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006369#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006370 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006371 return -1;
6372
6373#define ADD_CHECKED_OBJECT(m, name, obj) \
6374 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006375 PyObject *valObject = obj; \
6376 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006377 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006378 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006379 }
6380
6381 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006382populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006383{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006384 int i;
6385 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006386 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006387 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006388
6389 for (i = 0; i < (int)(sizeof(numeric_constants)
6390 / sizeof(struct numeric_constant));
6391 ++i)
6392 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006393 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006394
6395 for (i = 0; i < (int)(sizeof(object_constants)
6396 / sizeof(struct object_constant));
6397 ++i)
6398 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006399 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006400
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006401 valObject = object_constants[i].valObject;
6402 Py_INCREF(valObject);
6403 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006404 }
6405
6406 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6407 return -1;
6408 ADD_OBJECT(m, "error", VimError);
6409
Bram Moolenaara9922d62013-05-30 13:01:18 +02006410 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6411 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006412 ADD_CHECKED_OBJECT(m, "options",
6413 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006414
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006415 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006416 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006417 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006418
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006419 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006420 return -1;
6421 ADD_OBJECT(m, "_getcwd", py_getcwd)
6422
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006423 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006424 return -1;
6425 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006426 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006427 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006428 if (PyObject_SetAttrString(other_module, "chdir", attr))
6429 {
6430 Py_DECREF(attr);
6431 return -1;
6432 }
6433 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006434
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006435 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006436 {
6437 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006438 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006439 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006440 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6441 {
6442 Py_DECREF(attr);
6443 return -1;
6444 }
6445 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006446 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006447 else
6448 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006449
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006450 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6451 return -1;
6452
6453 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6454
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006455 if (!(imp = PyImport_ImportModule("imp")))
6456 return -1;
6457
6458 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6459 {
6460 Py_DECREF(imp);
6461 return -1;
6462 }
6463
6464 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6465 {
6466 Py_DECREF(py_find_module);
6467 Py_DECREF(imp);
6468 return -1;
6469 }
6470
6471 Py_DECREF(imp);
6472
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006473 ADD_OBJECT(m, "_find_module", py_find_module);
6474 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006475
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006476 return 0;
6477}