blob: 6ea0afff628ef786ef0f3703f2f86cd0f2ce21e8 [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 Moolenaar41009372013-07-01 22:03:04 +020016static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
17
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020018#if PY_VERSION_HEX < 0x02050000
19typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
20#endif
21
Bram Moolenaar91805fc2011-06-26 04:01:44 +020022#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020023# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020024#else
25# define ENC_OPT "latin1"
26#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020027#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020028
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020029static const char *vim_special_path = "_vim_path_";
30
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020031#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020032#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020033#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaarc476e522013-06-23 13:46:40 +020034#define PyErr_FORMAT(exc, str, tail) PyErr_Format(exc, _(str), tail)
35#define PyErr_VIM_FORMAT(str, tail) PyErr_FORMAT(VimError, str, tail)
36
37#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
38 ? "(NULL)" \
39 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020040
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020041#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020042 N_("empty keys are not allowed"))
43#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
44#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
45#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
46#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
47#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
48#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020049#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020050 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020051#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020052 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020053 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020054
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020055#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
56#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020057#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020058
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020059typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020060typedef void (*runner)(const char *, void *
61#ifdef PY_CAN_RECURSE
62 , PyGILState_STATE *
63#endif
64 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020066static int ConvertFromPyObject(PyObject *, typval_T *);
67static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020068static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020069static PyObject *WindowNew(win_T *, tabpage_T *);
70static PyObject *BufferNew (buf_T *);
71static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072
73static PyInt RangeStart;
74static PyInt RangeEnd;
75
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020076static PyObject *globals;
77
Bram Moolenaarf4258302013-06-02 18:20:17 +020078static PyObject *py_chdir;
79static PyObject *py_fchdir;
80static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020081static PyObject *vim_module;
82static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020083
Bram Moolenaar81c40c52013-06-12 14:41:04 +020084static PyObject *py_find_module;
85static PyObject *py_load_module;
86
87static PyObject *VimError;
88
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020089/*
90 * obtain a lock on the Vim data structures
91 */
92 static void
93Python_Lock_Vim(void)
94{
95}
96
97/*
98 * release a lock on the Vim data structures
99 */
100 static void
101Python_Release_Vim(void)
102{
103}
104
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200105/*
106 * The "todecref" argument holds a pointer to PyObject * that must be
107 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
108 * was needed to generate returned value is object.
109 *
110 * Use Py_XDECREF to decrement reference count.
111 */
112 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200113StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200114{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200115 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200116
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200117 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200118 {
119
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
121 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200122 return NULL;
123
124 *todecref = NULL;
125 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200127 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200128 PyObject *bytes;
129
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200130 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200131 return NULL;
132
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
134 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 {
136 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200138 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200139
140 *todecref = bytes;
141 }
142 else
143 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200144#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200145 PyErr_FORMAT(PyExc_TypeError,
146 N_("expected str() or unicode() instance, but got %s"),
147 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200148#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200149 PyErr_FORMAT(PyExc_TypeError,
150 N_("expected bytes() or str() instance, but got %s"),
151 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200152#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200153 return NULL;
154 }
155
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200156 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200157}
158
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200159#define NUMBER_LONG 1
160#define NUMBER_INT 2
161#define NUMBER_NATURAL 4
162#define NUMBER_UNSIGNED 8
163
164 static int
165NumberToLong(PyObject *obj, long *result, int flags)
166{
167#if PY_MAJOR_VERSION < 3
168 if (PyInt_Check(obj))
169 {
170 *result = PyInt_AsLong(obj);
171 if (PyErr_Occurred())
172 return -1;
173 }
174 else
175#endif
176 if (PyLong_Check(obj))
177 {
178 *result = PyLong_AsLong(obj);
179 if (PyErr_Occurred())
180 return -1;
181 }
182 else if (PyNumber_Check(obj))
183 {
184 PyObject *num;
185
186 if (!(num = PyNumber_Long(obj)))
187 return -1;
188
189 *result = PyLong_AsLong(num);
190
191 Py_DECREF(num);
192
193 if (PyErr_Occurred())
194 return -1;
195 }
196 else
197 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200198#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200199 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200200 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200201 "coercing to long(), but got %s"),
202 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200203#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200204 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200205 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 "but got %s"),
207 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200208#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209 return -1;
210 }
211
212 if (flags & NUMBER_INT)
213 {
214 if (*result > INT_MAX)
215 {
216 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200217 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200218 return -1;
219 }
220 else if (*result < INT_MIN)
221 {
222 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200223 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200224 return -1;
225 }
226 }
227
228 if (flags & NUMBER_NATURAL)
229 {
230 if (*result <= 0)
231 {
232 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200233 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200234 return -1;
235 }
236 }
237 else if (flags & NUMBER_UNSIGNED)
238 {
239 if (*result < 0)
240 {
241 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200242 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200243 return -1;
244 }
245 }
246
247 return 0;
248}
249
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200250 static int
251add_string(PyObject *list, char *s)
252{
253 PyObject *string;
254
255 if (!(string = PyString_FromString(s)))
256 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200257
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200258 if (PyList_Append(list, string))
259 {
260 Py_DECREF(string);
261 return -1;
262 }
263
264 Py_DECREF(string);
265 return 0;
266}
267
268 static PyObject *
269ObjectDir(PyObject *self, char **attributes)
270{
271 PyMethodDef *method;
272 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200273 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200274
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200275 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200276 return NULL;
277
278 if (self)
279 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200280 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200282 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 return NULL;
284 }
285
286 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200287 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 return NULL;
291 }
292
293#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 return NULL;
298 }
299#endif
300
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200301 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200302}
303
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200304/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200305 */
306
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200307/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200308typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200309
310static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311
312typedef struct
313{
314 PyObject_HEAD
315 long softspace;
316 long error;
317} OutputObject;
318
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200319static char *OutputAttrs[] = {
320 "softspace",
321 NULL
322};
323
324 static PyObject *
325OutputDir(PyObject *self)
326{
327 return ObjectDir(self, OutputAttrs);
328}
329
Bram Moolenaar77045652012-09-21 13:46:06 +0200330 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200331OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200332{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200333 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200334 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200335 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200336 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 return -1;
338 }
339
340 if (strcmp(name, "softspace") == 0)
341 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200342 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200343 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200344 return 0;
345 }
346
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200347 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200348 return -1;
349}
350
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200351/* Buffer IO, we write one whole line at a time. */
352static garray_T io_ga = {0, 0, 1, 80, NULL};
353static writefn old_fn = NULL;
354
355 static void
356PythonIO_Flush(void)
357{
358 if (old_fn != NULL && io_ga.ga_len > 0)
359 {
360 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
361 old_fn((char_u *)io_ga.ga_data);
362 }
363 io_ga.ga_len = 0;
364}
365
366 static void
367writer(writefn fn, char_u *str, PyInt n)
368{
369 char_u *ptr;
370
371 /* Flush when switching output function. */
372 if (fn != old_fn)
373 PythonIO_Flush();
374 old_fn = fn;
375
376 /* Write each NL separated line. Text after the last NL is kept for
377 * writing later. */
378 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
379 {
380 PyInt len = ptr - str;
381
382 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
383 break;
384
385 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
386 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
387 fn((char_u *)io_ga.ga_data);
388 str = ptr + 1;
389 n -= len + 1;
390 io_ga.ga_len = 0;
391 }
392
393 /* Put the remaining text into io_ga for later printing. */
394 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
395 {
396 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
397 io_ga.ga_len += (int)n;
398 }
399}
400
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200401 static int
402write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200403{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200404 Py_ssize_t len = 0;
405 char *str = NULL;
406 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
409 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410
411 Py_BEGIN_ALLOW_THREADS
412 Python_Lock_Vim();
413 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
414 Python_Release_Vim();
415 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200416 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200418 return 0;
419}
420
421 static PyObject *
422OutputWrite(OutputObject *self, PyObject *string)
423{
424 if (write_output(self, string))
425 return NULL;
426
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200427 Py_INCREF(Py_None);
428 return Py_None;
429}
430
431 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200432OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200434 PyObject *iterator;
435 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200436
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200437 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200438 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200440 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200441 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200442 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200443 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 Py_DECREF(iterator);
445 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446 return NULL;
447 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200448 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 }
450
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 Py_DECREF(iterator);
452
453 /* Iterator may have finished due to an exception */
454 if (PyErr_Occurred())
455 return NULL;
456
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200457 Py_INCREF(Py_None);
458 return Py_None;
459}
460
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100461 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200462OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100463{
464 /* do nothing */
465 Py_INCREF(Py_None);
466 return Py_None;
467}
468
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200469/***************/
470
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200471static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200472 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200473 {"write", (PyCFunction)OutputWrite, METH_O, ""},
474 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200475 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200476 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200477 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200478};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200479
480static OutputObject Output =
481{
482 PyObject_HEAD_INIT(&OutputType)
483 0,
484 0
485};
486
487static OutputObject Error =
488{
489 PyObject_HEAD_INIT(&OutputType)
490 0,
491 1
492};
493
494 static int
495PythonIO_Init_io(void)
496{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200497 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
498 return -1;
499 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
500 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200501
502 if (PyErr_Occurred())
503 {
504 EMSG(_("E264: Python: Error initialising I/O objects"));
505 return -1;
506 }
507
508 return 0;
509}
510
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200511typedef struct
512{
513 PyObject_HEAD
514 PyObject *module;
515} LoaderObject;
516static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200517
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200518 static void
519LoaderDestructor(LoaderObject *self)
520{
521 Py_DECREF(self->module);
522 DESTRUCTOR_FINISH(self);
523}
524
525 static PyObject *
526LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
527{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200528 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200529
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200530 Py_INCREF(ret);
531 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200532}
533
534static struct PyMethodDef LoaderMethods[] = {
535 /* name, function, calling, doc */
536 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
537 { NULL, NULL, 0, NULL}
538};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200539
540/* Check to see whether a Vim error has been reported, or a keyboard
541 * interrupt has been detected.
542 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200543
544 static void
545VimTryStart(void)
546{
547 ++trylevel;
548}
549
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200550 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200551VimTryEnd(void)
552{
553 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200554 /* Without this it stops processing all subsequent VimL commands and
555 * generates strange error messages if I e.g. try calling Test() in a
556 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200557 did_emsg = FALSE;
558 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200559 if (got_int)
560 {
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200561 did_throw = got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200562 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200564 }
565 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200566 return (PyErr_Occurred() ? -1 : 0);
567 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200568 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200569 {
570 did_throw = FALSE;
571 return -1;
572 }
573 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200574 else
575 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200576 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200577 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200578 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200579 }
580}
581
582 static int
583VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200584{
585 if (got_int)
586 {
587 PyErr_SetNone(PyExc_KeyboardInterrupt);
588 return 1;
589 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200590 return 0;
591}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200592
593/* Vim module - Implementation
594 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200595
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200596 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200597VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200598{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200599 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200600 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200601 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200602
Bram Moolenaar389a1792013-06-23 13:00:44 +0200603 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200604 return NULL;
605
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200606 Py_BEGIN_ALLOW_THREADS
607 Python_Lock_Vim();
608
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200610 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200611 update_screen(VALID);
612
613 Python_Release_Vim();
614 Py_END_ALLOW_THREADS
615
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200616 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200617 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200618 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200619 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200620
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200621 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200622 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200623 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200624}
625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626/*
627 * Function to translate a typval_T into a PyObject; this will recursively
628 * translate lists/dictionaries into their Python equivalents.
629 *
630 * The depth parameter is to avoid infinite recursion, set it to 1 when
631 * you call VimToPython.
632 */
633 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200634VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200635{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200636 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200637 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200638 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639
640 /* Avoid infinite recursion */
641 if (depth > 100)
642 {
643 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200644 ret = Py_None;
645 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646 }
647
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200648 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649 * then and we can use it again. */
650 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
651 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
652 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200653 sprintf(ptrBuf, "%p",
654 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
655 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200656
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200657 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200658 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200659 Py_INCREF(ret);
660 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200661 }
662 }
663
664 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200665 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200666 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200667 else if (our_tv->v_type == VAR_NUMBER)
668 {
669 char buf[NUMBUFLEN];
670
671 /* For backwards compatibility numbers are stored as strings. */
672 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200673 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200674 }
675# ifdef FEAT_FLOAT
676 else if (our_tv->v_type == VAR_FLOAT)
677 {
678 char buf[NUMBUFLEN];
679
680 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200681 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200682 }
683# endif
684 else if (our_tv->v_type == VAR_LIST)
685 {
686 list_T *list = our_tv->vval.v_list;
687 listitem_T *curr;
688
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200689 if (list == NULL)
690 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200692 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200693 return NULL;
694
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200695 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200696 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200697 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200698 return NULL;
699 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200701 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
702 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200703 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200705 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200706 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200709 {
710 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200711 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200712 return NULL;
713 }
714 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715 }
716 }
717 else if (our_tv->v_type == VAR_DICT)
718 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200719
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200720 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
721 long_u todo = ht->ht_used;
722 hashitem_T *hi;
723 dictitem_T *di;
724 if (our_tv->vval.v_dict == NULL)
725 return NULL;
726
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
729
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200730 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200731 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200732 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200733 return NULL;
734 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200736 for (hi = ht->ht_array; todo > 0; ++hi)
737 {
738 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200739 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200740 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200741
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200743 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200744 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200745 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200746 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200747 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200749 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200751 Py_DECREF(newObj);
752 return NULL;
753 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754 }
755 }
756 }
757 else
758 {
759 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 }
762
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200763 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200764}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765
766 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200767VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200769 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200771 PyObject *string;
772 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200773 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200774 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200775
Bram Moolenaar389a1792013-06-23 13:00:44 +0200776 if (!PyArg_ParseTuple(args, "O", &string))
777 return NULL;
778
779 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780 return NULL;
781
782 Py_BEGIN_ALLOW_THREADS
783 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200784 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200785 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200786 Python_Release_Vim();
787 Py_END_ALLOW_THREADS
788
Bram Moolenaar389a1792013-06-23 13:00:44 +0200789 Py_XDECREF(todecref);
790
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200791 if (VimTryEnd())
792 return NULL;
793
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 if (our_tv == NULL)
795 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200796 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797 return NULL;
798 }
799
800 /* Convert the Vim type into a Python type. Create a dictionary that's
801 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200802 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200804 else
805 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200806 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200807 Py_DECREF(lookup_dict);
808 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200809
810
811 Py_BEGIN_ALLOW_THREADS
812 Python_Lock_Vim();
813 free_tv(our_tv);
814 Python_Release_Vim();
815 Py_END_ALLOW_THREADS
816
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200817 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200818}
819
Bram Moolenaardb913952012-06-29 12:54:53 +0200820static PyObject *ConvertToPyObject(typval_T *);
821
822 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200823VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200824{
Bram Moolenaardb913952012-06-29 12:54:53 +0200825 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200826 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200827 char_u *expr;
828 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200829
Bram Moolenaar389a1792013-06-23 13:00:44 +0200830 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200831 return NULL;
832
833 Py_BEGIN_ALLOW_THREADS
834 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200835 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200836 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200837 Python_Release_Vim();
838 Py_END_ALLOW_THREADS
839
Bram Moolenaar389a1792013-06-23 13:00:44 +0200840 Py_XDECREF(todecref);
841
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200842 if (VimTryEnd())
843 return NULL;
844
Bram Moolenaardb913952012-06-29 12:54:53 +0200845 if (our_tv == NULL)
846 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200847 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200848 return NULL;
849 }
850
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200851 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200852 Py_BEGIN_ALLOW_THREADS
853 Python_Lock_Vim();
854 free_tv(our_tv);
855 Python_Release_Vim();
856 Py_END_ALLOW_THREADS
857
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200858 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200859}
860
861 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200862VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200863{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200864 char_u *str;
865 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200866 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200867
Bram Moolenaar389a1792013-06-23 13:00:44 +0200868 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200869 return NULL;
870
Bram Moolenaara54bf402012-12-05 16:30:07 +0100871#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200872 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100873#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200874 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100875#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200876
877 Py_XDECREF(todecref);
878
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200879 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200880}
881
Bram Moolenaarf4258302013-06-02 18:20:17 +0200882 static PyObject *
883_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
884{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200885 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200886 PyObject *newwd;
887 PyObject *todecref;
888 char_u *new_dir;
889
Bram Moolenaard4209d22013-06-05 20:34:15 +0200890 if (_chdir == NULL)
891 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200892 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200893 return NULL;
894
895 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
896 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200897 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200898 return NULL;
899 }
900
901 if (!(new_dir = StringToChars(newwd, &todecref)))
902 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200903 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200904 Py_DECREF(newwd);
905 return NULL;
906 }
907
908 VimTryStart();
909
910 if (vim_chdir(new_dir))
911 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200912 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200913 Py_DECREF(newwd);
914 Py_XDECREF(todecref);
915
916 if (VimTryEnd())
917 return NULL;
918
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200919 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200920 return NULL;
921 }
922
923 Py_DECREF(newwd);
924 Py_XDECREF(todecref);
925
926 post_chdir(FALSE);
927
928 if (VimTryEnd())
929 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200930 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200931 return NULL;
932 }
933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200934 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200935}
936
937 static PyObject *
938VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
939{
940 return _VimChdir(py_chdir, args, kwargs);
941}
942
943 static PyObject *
944VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
945{
946 return _VimChdir(py_fchdir, args, kwargs);
947}
948
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200949typedef struct {
950 PyObject *callable;
951 PyObject *result;
952} map_rtp_data;
953
954 static void
955map_rtp_callback(char_u *path, void *_data)
956{
957 void **data = (void **) _data;
958 PyObject *pathObject;
959 map_rtp_data *mr_data = *((map_rtp_data **) data);
960
Bram Moolenaar41009372013-07-01 22:03:04 +0200961 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200962 {
963 *data = NULL;
964 return;
965 }
966
967 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
968 pathObject, NULL);
969
970 Py_DECREF(pathObject);
971
972 if (!mr_data->result || mr_data->result != Py_None)
973 *data = NULL;
974 else
975 {
976 Py_DECREF(mr_data->result);
977 mr_data->result = NULL;
978 }
979}
980
981 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200982VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200983{
984 map_rtp_data data;
985
Bram Moolenaar389a1792013-06-23 13:00:44 +0200986 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200987 data.result = NULL;
988
989 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
990
991 if (data.result == NULL)
992 {
993 if (PyErr_Occurred())
994 return NULL;
995 else
996 {
997 Py_INCREF(Py_None);
998 return Py_None;
999 }
1000 }
1001 return data.result;
1002}
1003
1004/*
1005 * _vim_runtimepath_ special path implementation.
1006 */
1007
1008 static void
1009map_finder_callback(char_u *path, void *_data)
1010{
1011 void **data = (void **) _data;
1012 PyObject *list = *((PyObject **) data);
1013 PyObject *pathObject1, *pathObject2;
1014 char *pathbuf;
1015 size_t pathlen;
1016
1017 pathlen = STRLEN(path);
1018
1019#if PY_MAJOR_VERSION < 3
1020# define PY_MAIN_DIR_STRING "python2"
1021#else
1022# define PY_MAIN_DIR_STRING "python3"
1023#endif
1024#define PY_ALTERNATE_DIR_STRING "pythonx"
1025
1026#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1027 if (!(pathbuf = PyMem_New(char,
1028 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1029 {
1030 PyErr_NoMemory();
1031 *data = NULL;
1032 return;
1033 }
1034
1035 mch_memmove(pathbuf, path, pathlen + 1);
1036 add_pathsep((char_u *) pathbuf);
1037
1038 pathlen = STRLEN(pathbuf);
1039 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1040 PYTHONX_STRING_LENGTH + 1);
1041
1042 if (!(pathObject1 = PyString_FromString(pathbuf)))
1043 {
1044 *data = NULL;
1045 PyMem_Free(pathbuf);
1046 return;
1047 }
1048
1049 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1050 PYTHONX_STRING_LENGTH + 1);
1051
1052 if (!(pathObject2 = PyString_FromString(pathbuf)))
1053 {
1054 Py_DECREF(pathObject1);
1055 PyMem_Free(pathbuf);
1056 *data = NULL;
1057 return;
1058 }
1059
1060 PyMem_Free(pathbuf);
1061
1062 if (PyList_Append(list, pathObject1)
1063 || PyList_Append(list, pathObject2))
1064 *data = NULL;
1065
1066 Py_DECREF(pathObject1);
1067 Py_DECREF(pathObject2);
1068}
1069
1070 static PyObject *
1071Vim_GetPaths(PyObject *self UNUSED)
1072{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001073 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001074
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001075 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001076 return NULL;
1077
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001078 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001079
1080 if (PyErr_Occurred())
1081 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001082 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001083 return NULL;
1084 }
1085
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001086 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001087}
1088
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001089 static PyObject *
1090call_load_module(char *name, int len, PyObject *find_module_result)
1091{
1092 PyObject *fd, *pathname, *description;
1093
Bram Moolenaarc476e522013-06-23 13:46:40 +02001094 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001095 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001096 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001097 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001098 Py_TYPE_NAME(find_module_result));
1099 return NULL;
1100 }
1101 if (PyTuple_GET_SIZE(find_module_result) != 3)
1102 {
1103 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001104 N_("expected 3-tuple as imp.find_module() result, but got "
1105 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001106 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001107 return NULL;
1108 }
1109
1110 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1111 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1112 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1113 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001114 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001115 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001116 return NULL;
1117 }
1118
1119 return PyObject_CallFunction(py_load_module,
1120 "s#OOO", name, len, fd, pathname, description);
1121}
1122
1123 static PyObject *
1124find_module(char *fullname, char *tail, PyObject *new_path)
1125{
1126 PyObject *find_module_result;
1127 PyObject *module;
1128 char *dot;
1129
Bram Moolenaar41009372013-07-01 22:03:04 +02001130 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001131 {
1132 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001133 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001134 * first component
1135 */
1136 PyObject *newest_path;
1137 int partlen = (int) (dot - 1 - tail);
1138
1139 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1140 "s#O", tail, partlen, new_path)))
1141 return NULL;
1142
1143 if (!(module = call_load_module(
1144 fullname,
1145 ((int) (tail - fullname)) + partlen,
1146 find_module_result)))
1147 {
1148 Py_DECREF(find_module_result);
1149 return NULL;
1150 }
1151
1152 Py_DECREF(find_module_result);
1153
1154 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1155 {
1156 Py_DECREF(module);
1157 return NULL;
1158 }
1159
1160 Py_DECREF(module);
1161
1162 module = find_module(fullname, dot + 1, newest_path);
1163
1164 Py_DECREF(newest_path);
1165
1166 return module;
1167 }
1168 else
1169 {
1170 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1171 "sO", tail, new_path)))
1172 return NULL;
1173
1174 if (!(module = call_load_module(
1175 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001176 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001177 find_module_result)))
1178 {
1179 Py_DECREF(find_module_result);
1180 return NULL;
1181 }
1182
1183 Py_DECREF(find_module_result);
1184
1185 return module;
1186 }
1187}
1188
1189 static PyObject *
1190FinderFindModule(PyObject *self, PyObject *args)
1191{
1192 char *fullname;
1193 PyObject *module;
1194 PyObject *new_path;
1195 LoaderObject *loader;
1196
1197 if (!PyArg_ParseTuple(args, "s", &fullname))
1198 return NULL;
1199
1200 if (!(new_path = Vim_GetPaths(self)))
1201 return NULL;
1202
1203 module = find_module(fullname, fullname, new_path);
1204
1205 Py_DECREF(new_path);
1206
1207 if (!module)
1208 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001209 if (PyErr_Occurred())
1210 {
1211 if (PyErr_ExceptionMatches(PyExc_ImportError))
1212 PyErr_Clear();
1213 else
1214 return NULL;
1215 }
1216
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001217 Py_INCREF(Py_None);
1218 return Py_None;
1219 }
1220
1221 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1222 {
1223 Py_DECREF(module);
1224 return NULL;
1225 }
1226
1227 loader->module = module;
1228
1229 return (PyObject *) loader;
1230}
1231
1232 static PyObject *
1233VimPathHook(PyObject *self UNUSED, PyObject *args)
1234{
1235 char *path;
1236
1237 if (PyArg_ParseTuple(args, "s", &path)
1238 && STRCMP(path, vim_special_path) == 0)
1239 {
1240 Py_INCREF(vim_module);
1241 return vim_module;
1242 }
1243
1244 PyErr_Clear();
1245 PyErr_SetNone(PyExc_ImportError);
1246 return NULL;
1247}
1248
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001249/*
1250 * Vim module - Definitions
1251 */
1252
1253static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001254 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001255 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001256 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001257 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1258 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001259 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1260 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001261 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001262 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001263 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1264 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1265 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001266};
1267
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001268/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001269 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001270 */
1271
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001272static PyTypeObject IterType;
1273
1274typedef PyObject *(*nextfun)(void **);
1275typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001276typedef int (*traversefun)(void *, visitproc, void *);
1277typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001278
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001279/* Main purpose of this object is removing the need for do python
1280 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1281 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001282
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001283typedef struct
1284{
1285 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001286 void *cur;
1287 nextfun next;
1288 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001289 traversefun traverse;
1290 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001291} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001292
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001293 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001294IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1295 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001296{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001297 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001298
Bram Moolenaar774267b2013-05-21 20:51:59 +02001299 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001300 self->cur = start;
1301 self->next = next;
1302 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001303 self->traverse = traverse;
1304 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001305
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001306 return (PyObject *)(self);
1307}
1308
1309 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001310IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001311{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001312 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001313 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001314 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001315}
1316
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001317 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001318IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001319{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001320 if (self->traverse != NULL)
1321 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001322 else
1323 return 0;
1324}
1325
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001326/* Mac OSX defines clear() somewhere. */
1327#ifdef clear
1328# undef clear
1329#endif
1330
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001331 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001332IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001333{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001334 if (self->clear != NULL)
1335 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001336 else
1337 return 0;
1338}
1339
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001340 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001341IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001342{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001343 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001344}
1345
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001346 static PyObject *
1347IterIter(PyObject *self)
1348{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001349 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001350 return self;
1351}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001352
Bram Moolenaardb913952012-06-29 12:54:53 +02001353typedef struct pylinkedlist_S {
1354 struct pylinkedlist_S *pll_next;
1355 struct pylinkedlist_S *pll_prev;
1356 PyObject *pll_obj;
1357} pylinkedlist_T;
1358
1359static pylinkedlist_T *lastdict = NULL;
1360static pylinkedlist_T *lastlist = NULL;
1361
1362 static void
1363pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1364{
1365 if (ref->pll_prev == NULL)
1366 {
1367 if (ref->pll_next == NULL)
1368 {
1369 *last = NULL;
1370 return;
1371 }
1372 }
1373 else
1374 ref->pll_prev->pll_next = ref->pll_next;
1375
1376 if (ref->pll_next == NULL)
1377 *last = ref->pll_prev;
1378 else
1379 ref->pll_next->pll_prev = ref->pll_prev;
1380}
1381
1382 static void
1383pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1384{
1385 if (*last == NULL)
1386 ref->pll_prev = NULL;
1387 else
1388 {
1389 (*last)->pll_next = ref;
1390 ref->pll_prev = *last;
1391 }
1392 ref->pll_next = NULL;
1393 ref->pll_obj = self;
1394 *last = ref;
1395}
1396
1397static PyTypeObject DictionaryType;
1398
1399typedef struct
1400{
1401 PyObject_HEAD
1402 dict_T *dict;
1403 pylinkedlist_T ref;
1404} DictionaryObject;
1405
Bram Moolenaara9922d62013-05-30 13:01:18 +02001406static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1407
1408#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1409
Bram Moolenaardb913952012-06-29 12:54:53 +02001410 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001411DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001412{
1413 DictionaryObject *self;
1414
Bram Moolenaara9922d62013-05-30 13:01:18 +02001415 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001416 if (self == NULL)
1417 return NULL;
1418 self->dict = dict;
1419 ++dict->dv_refcount;
1420
1421 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1422
1423 return (PyObject *)(self);
1424}
1425
Bram Moolenaara9922d62013-05-30 13:01:18 +02001426 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001427py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001428{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001429 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001430
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001431 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001432 {
1433 PyErr_NoMemory();
1434 return NULL;
1435 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001436 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001437
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001438 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001439}
1440
1441 static PyObject *
1442DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1443{
1444 DictionaryObject *self;
1445 dict_T *dict;
1446
1447 if (!(dict = py_dict_alloc()))
1448 return NULL;
1449
1450 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1451
1452 --dict->dv_refcount;
1453
1454 if (kwargs || PyTuple_Size(args))
1455 {
1456 PyObject *tmp;
1457 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1458 {
1459 Py_DECREF(self);
1460 return NULL;
1461 }
1462
1463 Py_DECREF(tmp);
1464 }
1465
1466 return (PyObject *)(self);
1467}
1468
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001469 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001470DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001471{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001472 pyll_remove(&self->ref, &lastdict);
1473 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001474
1475 DESTRUCTOR_FINISH(self);
1476}
1477
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001478static char *DictionaryAttrs[] = {
1479 "locked", "scope",
1480 NULL
1481};
1482
1483 static PyObject *
1484DictionaryDir(PyObject *self)
1485{
1486 return ObjectDir(self, DictionaryAttrs);
1487}
1488
Bram Moolenaardb913952012-06-29 12:54:53 +02001489 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001490DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001491{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001492 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001493 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001494 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001495 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001496 return -1;
1497 }
1498
1499 if (strcmp(name, "locked") == 0)
1500 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001501 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001502 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001503 PyErr_SET_STRING(PyExc_TypeError,
1504 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001505 return -1;
1506 }
1507 else
1508 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001509 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001510 if (istrue == -1)
1511 return -1;
1512 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001513 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001514 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001515 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001516 }
1517 return 0;
1518 }
1519 else
1520 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001521 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001522 return -1;
1523 }
1524}
1525
1526 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001527DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001528{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001529 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001530}
1531
Bram Moolenaara9922d62013-05-30 13:01:18 +02001532#define DICT_FLAG_HAS_DEFAULT 0x01
1533#define DICT_FLAG_POP 0x02
1534#define DICT_FLAG_NONE_DEFAULT 0x04
1535#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1536#define DICT_FLAG_RETURN_PAIR 0x10
1537
Bram Moolenaardb913952012-06-29 12:54:53 +02001538 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001539_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001540{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001541 PyObject *keyObject;
1542 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001543 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001544 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001545 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001546 dict_T *dict = self->dict;
1547 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001548 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001549
Bram Moolenaara9922d62013-05-30 13:01:18 +02001550 if (flags & DICT_FLAG_HAS_DEFAULT)
1551 {
1552 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1553 return NULL;
1554 }
1555 else
1556 keyObject = args;
1557
1558 if (flags & DICT_FLAG_RETURN_BOOL)
1559 defObject = Py_False;
1560
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001561 if (!(key = StringToChars(keyObject, &todecref)))
1562 return NULL;
1563
1564 if (*key == NUL)
1565 {
1566 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001567 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001568 return NULL;
1569 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001570
Bram Moolenaara9922d62013-05-30 13:01:18 +02001571 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001572
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001573 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001574
Bram Moolenaara9922d62013-05-30 13:01:18 +02001575 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001576 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577 if (defObject)
1578 {
1579 Py_INCREF(defObject);
1580 return defObject;
1581 }
1582 else
1583 {
1584 PyErr_SetObject(PyExc_KeyError, keyObject);
1585 return NULL;
1586 }
1587 }
1588 else if (flags & DICT_FLAG_RETURN_BOOL)
1589 {
1590 Py_INCREF(Py_True);
1591 return Py_True;
1592 }
1593
1594 di = dict_lookup(hi);
1595
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001596 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001597 return NULL;
1598
1599 if (flags & DICT_FLAG_POP)
1600 {
1601 if (dict->dv_lock)
1602 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001603 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001604 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605 return NULL;
1606 }
1607
1608 hash_remove(&dict->dv_hashtab, hi);
1609 dictitem_free(di);
1610 }
1611
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001612 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001613}
1614
1615 static PyObject *
1616DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1617{
1618 return _DictionaryItem(self, keyObject, 0);
1619}
1620
1621 static int
1622DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1623{
1624 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001625 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001626
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001627 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001628
Bram Moolenaardee2e312013-06-23 16:35:47 +02001629 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001630
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001631 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632}
1633
1634typedef struct
1635{
1636 hashitem_T *ht_array;
1637 long_u ht_used;
1638 hashtab_T *ht;
1639 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001640 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001641} dictiterinfo_T;
1642
1643 static PyObject *
1644DictionaryIterNext(dictiterinfo_T **dii)
1645{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001646 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001647
1648 if (!(*dii)->todo)
1649 return NULL;
1650
1651 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1652 (*dii)->ht->ht_used != (*dii)->ht_used)
1653 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001654 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001655 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001656 return NULL;
1657 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001658
Bram Moolenaara9922d62013-05-30 13:01:18 +02001659 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1660 ++((*dii)->hi);
1661
1662 --((*dii)->todo);
1663
Bram Moolenaar41009372013-07-01 22:03:04 +02001664 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001665 return NULL;
1666
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001667 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001668}
1669
1670 static PyObject *
1671DictionaryIter(DictionaryObject *self)
1672{
1673 dictiterinfo_T *dii;
1674 hashtab_T *ht;
1675
1676 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1677 {
1678 PyErr_NoMemory();
1679 return NULL;
1680 }
1681
1682 ht = &self->dict->dv_hashtab;
1683 dii->ht_array = ht->ht_array;
1684 dii->ht_used = ht->ht_used;
1685 dii->ht = ht;
1686 dii->hi = dii->ht_array;
1687 dii->todo = dii->ht_used;
1688
1689 return IterNew(dii,
1690 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1691 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001692}
1693
1694 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001695DictionaryAssItem(
1696 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001697{
1698 char_u *key;
1699 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001700 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001701 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001702 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001703
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001704 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001705 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001706 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001707 return -1;
1708 }
1709
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001710 if (!(key = StringToChars(keyObject, &todecref)))
1711 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001712
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001713 if (*key == NUL)
1714 {
1715 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001716 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001717 return -1;
1718 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001719
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001720 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001721
1722 if (valObject == NULL)
1723 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001724 hashitem_T *hi;
1725
Bram Moolenaardb913952012-06-29 12:54:53 +02001726 if (di == NULL)
1727 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001728 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001729 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001730 return -1;
1731 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001732 hi = hash_find(&dict->dv_hashtab, di->di_key);
1733 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001734 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001735 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001736 return 0;
1737 }
1738
1739 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001740 {
1741 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001742 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001743 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001744
1745 if (di == NULL)
1746 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001748 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001749 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001750 PyErr_NoMemory();
1751 return -1;
1752 }
1753 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001754 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001755
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001756 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001757 {
1758 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001759 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001760 RAISE_KEY_ADD_FAIL(key);
1761 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001762 return -1;
1763 }
1764 }
1765 else
1766 clear_tv(&di->di_tv);
1767
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001768 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001769
1770 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001771 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001772 return 0;
1773}
1774
Bram Moolenaara9922d62013-05-30 13:01:18 +02001775typedef PyObject *(*hi_to_py)(hashitem_T *);
1776
Bram Moolenaardb913952012-06-29 12:54:53 +02001777 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001779{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001780 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001781 long_u todo = dict->dv_hashtab.ht_used;
1782 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001783 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001784 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001786
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001787 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001788 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1789 {
1790 if (!HASHITEM_EMPTY(hi))
1791 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792 if (!(newObj = hiconvert(hi)))
1793 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001794 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001795 return NULL;
1796 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001797 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001798 --todo;
1799 ++i;
1800 }
1801 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001802 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001803}
1804
Bram Moolenaara9922d62013-05-30 13:01:18 +02001805 static PyObject *
1806dict_key(hashitem_T *hi)
1807{
1808 return PyBytes_FromString((char *)(hi->hi_key));
1809}
1810
1811 static PyObject *
1812DictionaryListKeys(DictionaryObject *self)
1813{
1814 return DictionaryListObjects(self, dict_key);
1815}
1816
1817 static PyObject *
1818dict_val(hashitem_T *hi)
1819{
1820 dictitem_T *di;
1821
1822 di = dict_lookup(hi);
1823 return ConvertToPyObject(&di->di_tv);
1824}
1825
1826 static PyObject *
1827DictionaryListValues(DictionaryObject *self)
1828{
1829 return DictionaryListObjects(self, dict_val);
1830}
1831
1832 static PyObject *
1833dict_item(hashitem_T *hi)
1834{
1835 PyObject *keyObject;
1836 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001837 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001838
1839 if (!(keyObject = dict_key(hi)))
1840 return NULL;
1841
1842 if (!(valObject = dict_val(hi)))
1843 {
1844 Py_DECREF(keyObject);
1845 return NULL;
1846 }
1847
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001848 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001849
1850 Py_DECREF(keyObject);
1851 Py_DECREF(valObject);
1852
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001853 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001854}
1855
1856 static PyObject *
1857DictionaryListItems(DictionaryObject *self)
1858{
1859 return DictionaryListObjects(self, dict_item);
1860}
1861
1862 static PyObject *
1863DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1864{
1865 dict_T *dict = self->dict;
1866
1867 if (dict->dv_lock)
1868 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001869 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001870 return NULL;
1871 }
1872
1873 if (kwargs)
1874 {
1875 typval_T tv;
1876
1877 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1878 return NULL;
1879
1880 VimTryStart();
1881 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1882 clear_tv(&tv);
1883 if (VimTryEnd())
1884 return NULL;
1885 }
1886 else
1887 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001888 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001889
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001890 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001891 return NULL;
1892
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001893 if (PyObject_HasAttrString(obj, "keys"))
1894 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001895 else
1896 {
1897 PyObject *iterator;
1898 PyObject *item;
1899
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001900 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001901 return NULL;
1902
1903 while ((item = PyIter_Next(iterator)))
1904 {
1905 PyObject *fast;
1906 PyObject *keyObject;
1907 PyObject *valObject;
1908 PyObject *todecref;
1909 char_u *key;
1910 dictitem_T *di;
1911
1912 if (!(fast = PySequence_Fast(item, "")))
1913 {
1914 Py_DECREF(iterator);
1915 Py_DECREF(item);
1916 return NULL;
1917 }
1918
1919 Py_DECREF(item);
1920
1921 if (PySequence_Fast_GET_SIZE(fast) != 2)
1922 {
1923 Py_DECREF(iterator);
1924 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001925 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001926 N_("expected sequence element of size 2, "
1927 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001928 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001929 return NULL;
1930 }
1931
1932 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1933
1934 if (!(key = StringToChars(keyObject, &todecref)))
1935 {
1936 Py_DECREF(iterator);
1937 Py_DECREF(fast);
1938 return NULL;
1939 }
1940
1941 di = dictitem_alloc(key);
1942
1943 Py_XDECREF(todecref);
1944
1945 if (di == NULL)
1946 {
1947 Py_DECREF(fast);
1948 Py_DECREF(iterator);
1949 PyErr_NoMemory();
1950 return NULL;
1951 }
1952 di->di_tv.v_lock = 0;
1953 di->di_tv.v_type = VAR_UNKNOWN;
1954
1955 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1956
1957 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1958 {
1959 Py_DECREF(iterator);
1960 Py_DECREF(fast);
1961 dictitem_free(di);
1962 return NULL;
1963 }
1964
1965 Py_DECREF(fast);
1966
1967 if (dict_add(dict, di) == FAIL)
1968 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001969 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001970 Py_DECREF(iterator);
1971 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001972 return NULL;
1973 }
1974 }
1975
1976 Py_DECREF(iterator);
1977
1978 /* Iterator may have finished due to an exception */
1979 if (PyErr_Occurred())
1980 return NULL;
1981 }
1982 }
1983 Py_INCREF(Py_None);
1984 return Py_None;
1985}
1986
1987 static PyObject *
1988DictionaryGet(DictionaryObject *self, PyObject *args)
1989{
1990 return _DictionaryItem(self, args,
1991 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1992}
1993
1994 static PyObject *
1995DictionaryPop(DictionaryObject *self, PyObject *args)
1996{
1997 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1998}
1999
2000 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002001DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002002{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002003 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002004 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002005 PyObject *valObject;
2006 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002007
Bram Moolenaarde71b562013-06-02 17:41:54 +02002008 if (self->dict->dv_hashtab.ht_used == 0)
2009 {
2010 PyErr_SetNone(PyExc_KeyError);
2011 return NULL;
2012 }
2013
2014 hi = self->dict->dv_hashtab.ht_array;
2015 while (HASHITEM_EMPTY(hi))
2016 ++hi;
2017
2018 di = dict_lookup(hi);
2019
2020 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002021 return NULL;
2022
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002023 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002024 {
2025 Py_DECREF(valObject);
2026 return NULL;
2027 }
2028
2029 hash_remove(&self->dict->dv_hashtab, hi);
2030 dictitem_free(di);
2031
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002032 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002033}
2034
2035 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002036DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002037{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002038 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2039}
2040
2041static PySequenceMethods DictionaryAsSeq = {
2042 0, /* sq_length */
2043 0, /* sq_concat */
2044 0, /* sq_repeat */
2045 0, /* sq_item */
2046 0, /* sq_slice */
2047 0, /* sq_ass_item */
2048 0, /* sq_ass_slice */
2049 (objobjproc) DictionaryContains, /* sq_contains */
2050 0, /* sq_inplace_concat */
2051 0, /* sq_inplace_repeat */
2052};
2053
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002054static PyMappingMethods DictionaryAsMapping = {
2055 (lenfunc) DictionaryLength,
2056 (binaryfunc) DictionaryItem,
2057 (objobjargproc) DictionaryAssItem,
2058};
2059
Bram Moolenaardb913952012-06-29 12:54:53 +02002060static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002061 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002062 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2063 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2064 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2065 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2066 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002067 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002068 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002069 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2070 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002071};
2072
2073static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002074static PySequenceMethods ListAsSeq;
2075static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002076
2077typedef struct
2078{
2079 PyObject_HEAD
2080 list_T *list;
2081 pylinkedlist_T ref;
2082} ListObject;
2083
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002084#define NEW_LIST(list) ListNew(&ListType, list)
2085
Bram Moolenaardb913952012-06-29 12:54:53 +02002086 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002087ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002088{
2089 ListObject *self;
2090
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002091 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002092 if (self == NULL)
2093 return NULL;
2094 self->list = list;
2095 ++list->lv_refcount;
2096
2097 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2098
2099 return (PyObject *)(self);
2100}
2101
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002102 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002103py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002104{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002105 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002106
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002107 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002108 {
2109 PyErr_NoMemory();
2110 return NULL;
2111 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002112 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002113
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002114 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002115}
2116
2117 static int
2118list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2119{
2120 PyObject *iterator;
2121 PyObject *item;
2122 listitem_T *li;
2123
2124 if (!(iterator = PyObject_GetIter(obj)))
2125 return -1;
2126
2127 while ((item = PyIter_Next(iterator)))
2128 {
2129 if (!(li = listitem_alloc()))
2130 {
2131 PyErr_NoMemory();
2132 Py_DECREF(item);
2133 Py_DECREF(iterator);
2134 return -1;
2135 }
2136 li->li_tv.v_lock = 0;
2137 li->li_tv.v_type = VAR_UNKNOWN;
2138
2139 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2140 {
2141 Py_DECREF(item);
2142 Py_DECREF(iterator);
2143 listitem_free(li);
2144 return -1;
2145 }
2146
2147 Py_DECREF(item);
2148
2149 list_append(l, li);
2150 }
2151
2152 Py_DECREF(iterator);
2153
2154 /* Iterator may have finished due to an exception */
2155 if (PyErr_Occurred())
2156 return -1;
2157
2158 return 0;
2159}
2160
2161 static PyObject *
2162ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2163{
2164 list_T *list;
2165 PyObject *obj = NULL;
2166
2167 if (kwargs)
2168 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002169 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002170 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002171 return NULL;
2172 }
2173
2174 if (!PyArg_ParseTuple(args, "|O", &obj))
2175 return NULL;
2176
2177 if (!(list = py_list_alloc()))
2178 return NULL;
2179
2180 if (obj)
2181 {
2182 PyObject *lookup_dict;
2183
2184 if (!(lookup_dict = PyDict_New()))
2185 {
2186 list_unref(list);
2187 return NULL;
2188 }
2189
2190 if (list_py_concat(list, obj, lookup_dict) == -1)
2191 {
2192 Py_DECREF(lookup_dict);
2193 list_unref(list);
2194 return NULL;
2195 }
2196
2197 Py_DECREF(lookup_dict);
2198 }
2199
2200 return ListNew(subtype, list);
2201}
2202
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002203 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002204ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002205{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002206 pyll_remove(&self->ref, &lastlist);
2207 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002208
2209 DESTRUCTOR_FINISH(self);
2210}
2211
Bram Moolenaardb913952012-06-29 12:54:53 +02002212 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002213ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002214{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002215 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002216}
2217
2218 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002219ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002220{
2221 listitem_T *li;
2222
Bram Moolenaard6e39182013-05-21 18:30:34 +02002223 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002224 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002225 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002226 return NULL;
2227 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002228 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002229 if (li == NULL)
2230 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002231 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002232 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002233 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002234 return NULL;
2235 }
2236 return ConvertToPyObject(&li->li_tv);
2237}
2238
2239#define PROC_RANGE \
2240 if (last < 0) {\
2241 if (last < -size) \
2242 last = 0; \
2243 else \
2244 last += size; \
2245 } \
2246 if (first < 0) \
2247 first = 0; \
2248 if (first > size) \
2249 first = size; \
2250 if (last > size) \
2251 last = size;
2252
2253 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002254ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002255{
2256 PyInt i;
2257 PyInt size = ListLength(self);
2258 PyInt n;
2259 PyObject *list;
2260 int reversed = 0;
2261
2262 PROC_RANGE
2263 if (first >= last)
2264 first = last;
2265
2266 n = last-first;
2267 list = PyList_New(n);
2268 if (list == NULL)
2269 return NULL;
2270
2271 for (i = 0; i < n; ++i)
2272 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002273 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002274 if (item == NULL)
2275 {
2276 Py_DECREF(list);
2277 return NULL;
2278 }
2279
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002280 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002281 }
2282
2283 return list;
2284}
2285
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002286typedef struct
2287{
2288 listwatch_T lw;
2289 list_T *list;
2290} listiterinfo_T;
2291
2292 static void
2293ListIterDestruct(listiterinfo_T *lii)
2294{
2295 list_rem_watch(lii->list, &lii->lw);
2296 PyMem_Free(lii);
2297}
2298
2299 static PyObject *
2300ListIterNext(listiterinfo_T **lii)
2301{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002302 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002303
2304 if (!((*lii)->lw.lw_item))
2305 return NULL;
2306
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002307 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002308 return NULL;
2309
2310 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2311
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002312 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002313}
2314
2315 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002316ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002317{
2318 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002319 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002320
2321 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2322 {
2323 PyErr_NoMemory();
2324 return NULL;
2325 }
2326
2327 list_add_watch(l, &lii->lw);
2328 lii->lw.lw_item = l->lv_first;
2329 lii->list = l;
2330
2331 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002332 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2333 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002334}
2335
Bram Moolenaardb913952012-06-29 12:54:53 +02002336 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002337ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002338{
2339 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002340 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002341 listitem_T *li;
2342 Py_ssize_t length = ListLength(self);
2343
2344 if (l->lv_lock)
2345 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002346 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002347 return -1;
2348 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002349 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002350 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002351 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002352 return -1;
2353 }
2354
2355 if (obj == NULL)
2356 {
2357 li = list_find(l, (long) index);
2358 list_remove(l, li, li);
2359 clear_tv(&li->li_tv);
2360 vim_free(li);
2361 return 0;
2362 }
2363
2364 if (ConvertFromPyObject(obj, &tv) == -1)
2365 return -1;
2366
2367 if (index == length)
2368 {
2369 if (list_append_tv(l, &tv) == FAIL)
2370 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002371 clear_tv(&tv);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002372 PyErr_SET_VIM(N_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002373 return -1;
2374 }
2375 }
2376 else
2377 {
2378 li = list_find(l, (long) index);
2379 clear_tv(&li->li_tv);
2380 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002381 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002382 }
2383 return 0;
2384}
2385
2386 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002387ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002388{
2389 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002390 PyObject *iterator;
2391 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002392 listitem_T *li;
2393 listitem_T *next;
2394 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002395 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002396 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002397
2398 if (l->lv_lock)
2399 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002400 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002401 return -1;
2402 }
2403
2404 PROC_RANGE
2405
2406 if (first == size)
2407 li = NULL;
2408 else
2409 {
2410 li = list_find(l, (long) first);
2411 if (li == NULL)
2412 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002413 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2414 (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002415 return -1;
2416 }
2417 if (last > first)
2418 {
2419 i = last - first;
2420 while (i-- && li != NULL)
2421 {
2422 next = li->li_next;
2423 listitem_remove(l, li);
2424 li = next;
2425 }
2426 }
2427 }
2428
2429 if (obj == NULL)
2430 return 0;
2431
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002432 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002433 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002434
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002435 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002436 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002437 if (ConvertFromPyObject(item, &v) == -1)
2438 {
2439 Py_DECREF(iterator);
2440 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002441 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002442 }
2443 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002444 if (list_insert_tv(l, &v, li) == FAIL)
2445 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002446 clear_tv(&v);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002447 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002448 return -1;
2449 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002450 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002451 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002452 Py_DECREF(iterator);
Bram Moolenaardee2e312013-06-23 16:35:47 +02002453
2454 if (PyErr_Occurred())
2455 return -1;
2456
Bram Moolenaardb913952012-06-29 12:54:53 +02002457 return 0;
2458}
2459
2460 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002461ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002462{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002463 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002464 PyObject *lookup_dict;
2465
2466 if (l->lv_lock)
2467 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002468 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002469 return NULL;
2470 }
2471
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002472 if (!(lookup_dict = PyDict_New()))
2473 return NULL;
2474
Bram Moolenaardb913952012-06-29 12:54:53 +02002475 if (list_py_concat(l, obj, lookup_dict) == -1)
2476 {
2477 Py_DECREF(lookup_dict);
2478 return NULL;
2479 }
2480 Py_DECREF(lookup_dict);
2481
2482 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002483 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002484}
2485
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002486static char *ListAttrs[] = {
2487 "locked",
2488 NULL
2489};
2490
2491 static PyObject *
2492ListDir(PyObject *self)
2493{
2494 return ObjectDir(self, ListAttrs);
2495}
2496
Bram Moolenaar66b79852012-09-21 14:00:35 +02002497 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002498ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002499{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002500 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002501 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002502 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002503 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002504 return -1;
2505 }
2506
2507 if (strcmp(name, "locked") == 0)
2508 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002509 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002510 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002511 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002512 return -1;
2513 }
2514 else
2515 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002516 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002517 if (istrue == -1)
2518 return -1;
2519 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002520 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002521 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002522 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002523 }
2524 return 0;
2525 }
2526 else
2527 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002528 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002529 return -1;
2530 }
2531}
2532
Bram Moolenaardb913952012-06-29 12:54:53 +02002533static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002534 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2535 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2536 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002537};
2538
2539typedef struct
2540{
2541 PyObject_HEAD
2542 char_u *name;
2543} FunctionObject;
2544
2545static PyTypeObject FunctionType;
2546
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002547#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2548
Bram Moolenaardb913952012-06-29 12:54:53 +02002549 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002550FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002551{
2552 FunctionObject *self;
2553
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002554 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2555
Bram Moolenaardb913952012-06-29 12:54:53 +02002556 if (self == NULL)
2557 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002558
2559 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002560 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002561 if (!translated_function_exists(name))
2562 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002563 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002564 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002565 return NULL;
2566 }
2567 self->name = vim_strsave(name);
2568 func_ref(self->name);
2569 }
2570 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002571 if ((self->name = get_expanded_name(name,
2572 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2573 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002574 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002575 PyErr_FORMAT(PyExc_ValueError,
2576 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002577 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002578 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002579
2580 return (PyObject *)(self);
2581}
2582
2583 static PyObject *
2584FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2585{
2586 PyObject *self;
2587 char_u *name;
2588
2589 if (kwargs)
2590 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002591 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002592 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002593 return NULL;
2594 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002595
Bram Moolenaar389a1792013-06-23 13:00:44 +02002596 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002597 return NULL;
2598
2599 self = FunctionNew(subtype, name);
2600
Bram Moolenaar389a1792013-06-23 13:00:44 +02002601 PyMem_Free(name);
2602
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002603 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002604}
2605
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002606 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002607FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002608{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002609 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002610 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002611
2612 DESTRUCTOR_FINISH(self);
2613}
2614
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002615static char *FunctionAttrs[] = {
2616 "softspace",
2617 NULL
2618};
2619
2620 static PyObject *
2621FunctionDir(PyObject *self)
2622{
2623 return ObjectDir(self, FunctionAttrs);
2624}
2625
Bram Moolenaardb913952012-06-29 12:54:53 +02002626 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002627FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002628{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002629 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002630 typval_T args;
2631 typval_T selfdicttv;
2632 typval_T rettv;
2633 dict_T *selfdict = NULL;
2634 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002635 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002636 int error;
2637
2638 if (ConvertFromPyObject(argsObject, &args) == -1)
2639 return NULL;
2640
2641 if (kwargs != NULL)
2642 {
2643 selfdictObject = PyDict_GetItemString(kwargs, "self");
2644 if (selfdictObject != NULL)
2645 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002646 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002647 {
2648 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002649 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002650 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002651 selfdict = selfdicttv.vval.v_dict;
2652 }
2653 }
2654
Bram Moolenaar71700b82013-05-15 17:49:05 +02002655 Py_BEGIN_ALLOW_THREADS
2656 Python_Lock_Vim();
2657
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002658 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002659 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002660
2661 Python_Release_Vim();
2662 Py_END_ALLOW_THREADS
2663
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002664 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002665 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002666 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002667 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002668 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002669 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002670 }
2671 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002672 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002673
Bram Moolenaardb913952012-06-29 12:54:53 +02002674 clear_tv(&args);
2675 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002676 if (selfdict != NULL)
2677 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002678
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002679 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002680}
2681
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002682 static PyObject *
2683FunctionRepr(FunctionObject *self)
2684{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002685#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002686 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002687 * Finalize */
2688 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002689 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002690#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002691 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002692#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002693}
2694
Bram Moolenaardb913952012-06-29 12:54:53 +02002695static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002696 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2697 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002698};
2699
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002700/*
2701 * Options object
2702 */
2703
2704static PyTypeObject OptionsType;
2705
2706typedef int (*checkfun)(void *);
2707
2708typedef struct
2709{
2710 PyObject_HEAD
2711 int opt_type;
2712 void *from;
2713 checkfun Check;
2714 PyObject *fromObj;
2715} OptionsObject;
2716
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002717 static int
2718dummy_check(void *arg UNUSED)
2719{
2720 return 0;
2721}
2722
2723 static PyObject *
2724OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2725{
2726 OptionsObject *self;
2727
Bram Moolenaar774267b2013-05-21 20:51:59 +02002728 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002729 if (self == NULL)
2730 return NULL;
2731
2732 self->opt_type = opt_type;
2733 self->from = from;
2734 self->Check = Check;
2735 self->fromObj = fromObj;
2736 if (fromObj)
2737 Py_INCREF(fromObj);
2738
2739 return (PyObject *)(self);
2740}
2741
2742 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002743OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002744{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002745 PyObject_GC_UnTrack((void *)(self));
2746 Py_XDECREF(self->fromObj);
2747 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002748}
2749
2750 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002751OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002752{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002753 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002754 return 0;
2755}
2756
2757 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002758OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002759{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002760 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002761 return 0;
2762}
2763
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002764 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002765OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002766{
2767 char_u *key;
2768 int flags;
2769 long numval;
2770 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002771 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002772
Bram Moolenaard6e39182013-05-21 18:30:34 +02002773 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002774 return NULL;
2775
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002776 if (!(key = StringToChars(keyObject, &todecref)))
2777 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002778
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002779 if (*key == NUL)
2780 {
2781 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002782 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002783 return NULL;
2784 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002785
2786 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002787 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002788
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002789 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002790
2791 if (flags == 0)
2792 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002793 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002794 return NULL;
2795 }
2796
2797 if (flags & SOPT_UNSET)
2798 {
2799 Py_INCREF(Py_None);
2800 return Py_None;
2801 }
2802 else if (flags & SOPT_BOOL)
2803 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002804 PyObject *ret;
2805 ret = numval ? Py_True : Py_False;
2806 Py_INCREF(ret);
2807 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002808 }
2809 else if (flags & SOPT_NUM)
2810 return PyInt_FromLong(numval);
2811 else if (flags & SOPT_STRING)
2812 {
2813 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002814 {
Bram Moolenaar41009372013-07-01 22:03:04 +02002815 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002816 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002817 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002818 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002819 else
2820 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002821 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002822 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002823 return NULL;
2824 }
2825 }
2826 else
2827 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002828 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002829 return NULL;
2830 }
2831}
2832
2833 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002834set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002835{
2836 char_u *errmsg;
2837
2838 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2839 {
2840 if (VimTryEnd())
2841 return FAIL;
2842 PyErr_SetVim((char *)errmsg);
2843 return FAIL;
2844 }
2845 return OK;
2846}
2847
2848 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002849set_option_value_for(
2850 char_u *key,
2851 int numval,
2852 char_u *stringval,
2853 int opt_flags,
2854 int opt_type,
2855 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002856{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002857 win_T *save_curwin = NULL;
2858 tabpage_T *save_curtab = NULL;
2859 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002860 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002861
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002862 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002863 switch (opt_type)
2864 {
2865 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002866 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002867 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002868 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002869 if (VimTryEnd())
2870 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002871 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002872 return -1;
2873 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002874 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2875 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002876 break;
2877 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002878 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002879 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002880 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002881 break;
2882 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002883 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002884 break;
2885 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002886 if (set_ret == FAIL)
2887 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002888 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002889}
2890
2891 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002892OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002893{
2894 char_u *key;
2895 int flags;
2896 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002897 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002898 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002899
Bram Moolenaard6e39182013-05-21 18:30:34 +02002900 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002901 return -1;
2902
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002903 if (!(key = StringToChars(keyObject, &todecref)))
2904 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002905
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002906 if (*key == NUL)
2907 {
2908 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002909 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002910 return -1;
2911 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002912
2913 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002914 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002915
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002916 if (flags == 0)
2917 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002918 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002919 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002920 return -1;
2921 }
2922
2923 if (valObject == NULL)
2924 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002925 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002926 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002927 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002928 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002929 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002930 return -1;
2931 }
2932 else if (!(flags & SOPT_GLOBAL))
2933 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002934 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002935 N_("unable to unset option %s "
2936 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002937 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002938 return -1;
2939 }
2940 else
2941 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002942 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002943 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002944 return 0;
2945 }
2946 }
2947
Bram Moolenaard6e39182013-05-21 18:30:34 +02002948 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002949
2950 if (flags & SOPT_BOOL)
2951 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002952 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002953
Bram Moolenaarb983f752013-05-15 16:11:50 +02002954 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002955 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002956 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002957 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002958 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002959 }
2960 else if (flags & SOPT_NUM)
2961 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002962 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002963
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002964 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002965 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002966 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002967 return -1;
2968 }
2969
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002970 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002971 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002972 }
2973 else
2974 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002975 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002976 PyObject *todecref;
2977
2978 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002979 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002980 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002981 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002982 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002983 }
2984
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002985 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002986
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002987 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002988}
2989
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002990static PyMappingMethods OptionsAsMapping = {
2991 (lenfunc) NULL,
2992 (binaryfunc) OptionsItem,
2993 (objobjargproc) OptionsAssItem,
2994};
2995
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002996/* Tabpage object
2997 */
2998
2999typedef struct
3000{
3001 PyObject_HEAD
3002 tabpage_T *tab;
3003} TabPageObject;
3004
3005static PyObject *WinListNew(TabPageObject *tabObject);
3006
3007static PyTypeObject TabPageType;
3008
3009 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003010CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003011{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003012 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003013 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003014 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003015 return -1;
3016 }
3017
3018 return 0;
3019}
3020
3021 static PyObject *
3022TabPageNew(tabpage_T *tab)
3023{
3024 TabPageObject *self;
3025
3026 if (TAB_PYTHON_REF(tab))
3027 {
3028 self = TAB_PYTHON_REF(tab);
3029 Py_INCREF(self);
3030 }
3031 else
3032 {
3033 self = PyObject_NEW(TabPageObject, &TabPageType);
3034 if (self == NULL)
3035 return NULL;
3036 self->tab = tab;
3037 TAB_PYTHON_REF(tab) = self;
3038 }
3039
3040 return (PyObject *)(self);
3041}
3042
3043 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003044TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003045{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3047 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003048
3049 DESTRUCTOR_FINISH(self);
3050}
3051
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003052static char *TabPageAttrs[] = {
3053 "windows", "number", "vars", "window", "valid",
3054 NULL
3055};
3056
3057 static PyObject *
3058TabPageDir(PyObject *self)
3059{
3060 return ObjectDir(self, TabPageAttrs);
3061}
3062
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003063 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003064TabPageAttrValid(TabPageObject *self, char *name)
3065{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003066 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003067
3068 if (strcmp(name, "valid") != 0)
3069 return NULL;
3070
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003071 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3072 Py_INCREF(ret);
3073 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003074}
3075
3076 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003077TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003078{
3079 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003080 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003081 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003082 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003083 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003084 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003085 else if (strcmp(name, "window") == 0)
3086 {
3087 /* For current tab window.c does not bother to set or update tp_curwin
3088 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003089 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003090 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003091 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003092 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003093 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003094 else if (strcmp(name, "__members__") == 0)
3095 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003096 return NULL;
3097}
3098
3099 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003100TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003101{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003102 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003103 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003104 else
3105 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003106 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003107
3108 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003109 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3110 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003111 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003112 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003113 }
3114}
3115
3116static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003117 /* name, function, calling, documentation */
3118 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3119 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003120};
3121
3122/*
3123 * Window list object
3124 */
3125
3126static PyTypeObject TabListType;
3127static PySequenceMethods TabListAsSeq;
3128
3129typedef struct
3130{
3131 PyObject_HEAD
3132} TabListObject;
3133
3134 static PyInt
3135TabListLength(PyObject *self UNUSED)
3136{
3137 tabpage_T *tp = first_tabpage;
3138 PyInt n = 0;
3139
3140 while (tp != NULL)
3141 {
3142 ++n;
3143 tp = tp->tp_next;
3144 }
3145
3146 return n;
3147}
3148
3149 static PyObject *
3150TabListItem(PyObject *self UNUSED, PyInt n)
3151{
3152 tabpage_T *tp;
3153
3154 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3155 if (n == 0)
3156 return TabPageNew(tp);
3157
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003158 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003159 return NULL;
3160}
3161
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003162/*
3163 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003164 */
3165
3166typedef struct
3167{
3168 PyObject_HEAD
3169 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003170 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003171} WindowObject;
3172
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003173static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003174
3175 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003176CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003177{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003178 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003179 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003180 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003181 return -1;
3182 }
3183
3184 return 0;
3185}
3186
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003187 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003188WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003189{
3190 /* We need to handle deletion of windows underneath us.
3191 * If we add a "w_python*_ref" field to the win_T structure,
3192 * then we can get at it in win_free() in vim. We then
3193 * need to create only ONE Python object per window - if
3194 * we try to create a second, just INCREF the existing one
3195 * and return it. The (single) Python object referring to
3196 * the window is stored in "w_python*_ref".
3197 * On a win_free() we set the Python object's win_T* field
3198 * to an invalid value. We trap all uses of a window
3199 * object, and reject them if the win_T* field is invalid.
3200 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003201 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003202 * w_python_ref and w_python3_ref fields respectively.
3203 */
3204
3205 WindowObject *self;
3206
3207 if (WIN_PYTHON_REF(win))
3208 {
3209 self = WIN_PYTHON_REF(win);
3210 Py_INCREF(self);
3211 }
3212 else
3213 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003214 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003215 if (self == NULL)
3216 return NULL;
3217 self->win = win;
3218 WIN_PYTHON_REF(win) = self;
3219 }
3220
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003221 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3222
Bram Moolenaar971db462013-05-12 18:44:48 +02003223 return (PyObject *)(self);
3224}
3225
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003226 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003227WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003228{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003229 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003230 if (self->win && self->win != INVALID_WINDOW_VALUE)
3231 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003232 Py_XDECREF(((PyObject *)(self->tabObject)));
3233 PyObject_GC_Del((void *)(self));
3234}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003235
Bram Moolenaar774267b2013-05-21 20:51:59 +02003236 static int
3237WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3238{
3239 Py_VISIT(((PyObject *)(self->tabObject)));
3240 return 0;
3241}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003242
Bram Moolenaar774267b2013-05-21 20:51:59 +02003243 static int
3244WindowClear(WindowObject *self)
3245{
3246 Py_CLEAR(self->tabObject);
3247 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003248}
3249
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003250 static win_T *
3251get_firstwin(TabPageObject *tabObject)
3252{
3253 if (tabObject)
3254 {
3255 if (CheckTabPage(tabObject))
3256 return NULL;
3257 /* For current tab window.c does not bother to set or update tp_firstwin
3258 */
3259 else if (tabObject->tab == curtab)
3260 return firstwin;
3261 else
3262 return tabObject->tab->tp_firstwin;
3263 }
3264 else
3265 return firstwin;
3266}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003267static char *WindowAttrs[] = {
3268 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3269 "tabpage", "valid",
3270 NULL
3271};
3272
3273 static PyObject *
3274WindowDir(PyObject *self)
3275{
3276 return ObjectDir(self, WindowAttrs);
3277}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003278
Bram Moolenaar971db462013-05-12 18:44:48 +02003279 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003280WindowAttrValid(WindowObject *self, char *name)
3281{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003282 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003283
3284 if (strcmp(name, "valid") != 0)
3285 return NULL;
3286
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003287 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3288 Py_INCREF(ret);
3289 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003290}
3291
3292 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003293WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003294{
3295 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003296 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003297 else if (strcmp(name, "cursor") == 0)
3298 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003299 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003300
3301 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3302 }
3303 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003304 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003305#ifdef FEAT_WINDOWS
3306 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003307 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003308#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003309#ifdef FEAT_VERTSPLIT
3310 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003311 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003312 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003313 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003314#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003315 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003316 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003317 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003318 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3319 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003320 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003321 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003322 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003323 return NULL;
3324 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003325 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003326 }
3327 else if (strcmp(name, "tabpage") == 0)
3328 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003329 Py_INCREF(self->tabObject);
3330 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003331 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003332 else if (strcmp(name, "__members__") == 0)
3333 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003334 else
3335 return NULL;
3336}
3337
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003338 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003339WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003340{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003341 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003342 return -1;
3343
3344 if (strcmp(name, "buffer") == 0)
3345 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003346 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003347 return -1;
3348 }
3349 else if (strcmp(name, "cursor") == 0)
3350 {
3351 long lnum;
3352 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003353
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003354 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003355 return -1;
3356
Bram Moolenaard6e39182013-05-21 18:30:34 +02003357 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003358 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003359 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003360 return -1;
3361 }
3362
3363 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003364 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003365 return -1;
3366
Bram Moolenaard6e39182013-05-21 18:30:34 +02003367 self->win->w_cursor.lnum = lnum;
3368 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003369#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003371#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003372 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003374
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003375 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003376 return 0;
3377 }
3378 else if (strcmp(name, "height") == 0)
3379 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003380 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003381 win_T *savewin;
3382
Bram Moolenaardee2e312013-06-23 16:35:47 +02003383 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003384 return -1;
3385
3386#ifdef FEAT_GUI
3387 need_mouse_correct = TRUE;
3388#endif
3389 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003390 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003391
3392 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003393 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003394 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003395 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003396 return -1;
3397
3398 return 0;
3399 }
3400#ifdef FEAT_VERTSPLIT
3401 else if (strcmp(name, "width") == 0)
3402 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003403 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003404 win_T *savewin;
3405
Bram Moolenaardee2e312013-06-23 16:35:47 +02003406 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003407 return -1;
3408
3409#ifdef FEAT_GUI
3410 need_mouse_correct = TRUE;
3411#endif
3412 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003413 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003414
3415 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003416 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003417 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003418 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003419 return -1;
3420
3421 return 0;
3422 }
3423#endif
3424 else
3425 {
3426 PyErr_SetString(PyExc_AttributeError, name);
3427 return -1;
3428 }
3429}
3430
3431 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003432WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003433{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003434 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003435 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003436 else
3437 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003438 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003439
Bram Moolenaar6d216452013-05-12 19:00:41 +02003440 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003441 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003442 (self));
3443 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003444 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003445 }
3446}
3447
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003448static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003449 /* name, function, calling, documentation */
3450 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3451 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003452};
3453
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003454/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003455 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003456 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003457
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003458static PyTypeObject WinListType;
3459static PySequenceMethods WinListAsSeq;
3460
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003461typedef struct
3462{
3463 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003464 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003465} WinListObject;
3466
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003467 static PyObject *
3468WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003469{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003470 WinListObject *self;
3471
3472 self = PyObject_NEW(WinListObject, &WinListType);
3473 self->tabObject = tabObject;
3474 Py_INCREF(tabObject);
3475
3476 return (PyObject *)(self);
3477}
3478
3479 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003480WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003481{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003482 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003483
3484 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003485 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003486 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003487 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003488
3489 DESTRUCTOR_FINISH(self);
3490}
3491
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003492 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003493WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003494{
3495 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003496 PyInt n = 0;
3497
Bram Moolenaard6e39182013-05-21 18:30:34 +02003498 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003499 return -1;
3500
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003501 while (w != NULL)
3502 {
3503 ++n;
3504 w = W_NEXT(w);
3505 }
3506
3507 return n;
3508}
3509
3510 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003511WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003512{
3513 win_T *w;
3514
Bram Moolenaard6e39182013-05-21 18:30:34 +02003515 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003516 return NULL;
3517
3518 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003519 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003520 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003521
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003522 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003523 return NULL;
3524}
3525
3526/* Convert a Python string into a Vim line.
3527 *
3528 * The result is in allocated memory. All internal nulls are replaced by
3529 * newline characters. It is an error for the string to contain newline
3530 * characters.
3531 *
3532 * On errors, the Python exception data is set, and NULL is returned.
3533 */
3534 static char *
3535StringToLine(PyObject *obj)
3536{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003537 char *str;
3538 char *save;
3539 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003540 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003541 PyInt i;
3542 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003543
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003544 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003545 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003546 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3547 || str == NULL)
3548 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003549 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003550 else if (PyUnicode_Check(obj))
3551 {
3552 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3553 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003554
Bram Moolenaardaa27022013-06-24 22:33:30 +02003555 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003556 || str == NULL)
3557 {
3558 Py_DECREF(bytes);
3559 return NULL;
3560 }
3561 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003562 else
3563 {
3564#if PY_MAJOR_VERSION < 3
3565 PyErr_FORMAT(PyExc_TypeError,
3566 N_("expected str() or unicode() instance, but got %s"),
3567 Py_TYPE_NAME(obj));
3568#else
3569 PyErr_FORMAT(PyExc_TypeError,
3570 N_("expected bytes() or str() instance, but got %s"),
3571 Py_TYPE_NAME(obj));
3572#endif
3573 return NULL;
3574 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003575
3576 /*
3577 * Error checking: String must not contain newlines, as we
3578 * are replacing a single line, and we must replace it with
3579 * a single line.
3580 * A trailing newline is removed, so that append(f.readlines()) works.
3581 */
3582 p = memchr(str, '\n', len);
3583 if (p != NULL)
3584 {
3585 if (p == str + len - 1)
3586 --len;
3587 else
3588 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003589 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003590 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003591 return NULL;
3592 }
3593 }
3594
3595 /* Create a copy of the string, with internal nulls replaced by
3596 * newline characters, as is the vim convention.
3597 */
3598 save = (char *)alloc((unsigned)(len+1));
3599 if (save == NULL)
3600 {
3601 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003602 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003603 return NULL;
3604 }
3605
3606 for (i = 0; i < len; ++i)
3607 {
3608 if (str[i] == '\0')
3609 save[i] = '\n';
3610 else
3611 save[i] = str[i];
3612 }
3613
3614 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003615 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003616
3617 return save;
3618}
3619
3620/* Get a line from the specified buffer. The line number is
3621 * in Vim format (1-based). The line is returned as a Python
3622 * string object.
3623 */
3624 static PyObject *
3625GetBufferLine(buf_T *buf, PyInt n)
3626{
3627 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3628}
3629
3630
3631/* Get a list of lines from the specified buffer. The line numbers
3632 * are in Vim format (1-based). The range is from lo up to, but not
3633 * including, hi. The list is returned as a Python list of string objects.
3634 */
3635 static PyObject *
3636GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3637{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003638 PyInt i;
3639 PyInt n = hi - lo;
3640 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003641
3642 if (list == NULL)
3643 return NULL;
3644
3645 for (i = 0; i < n; ++i)
3646 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003647 PyObject *string = LineToString(
3648 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003649
3650 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003651 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003652 {
3653 Py_DECREF(list);
3654 return NULL;
3655 }
3656
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003657 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003658 }
3659
3660 /* The ownership of the Python list is passed to the caller (ie,
3661 * the caller should Py_DECREF() the object when it is finished
3662 * with it).
3663 */
3664
3665 return list;
3666}
3667
3668/*
3669 * Check if deleting lines made the cursor position invalid.
3670 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3671 * deleted).
3672 */
3673 static void
3674py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3675{
3676 if (curwin->w_cursor.lnum >= lo)
3677 {
3678 /* Adjust the cursor position if it's in/after the changed
3679 * lines. */
3680 if (curwin->w_cursor.lnum >= hi)
3681 {
3682 curwin->w_cursor.lnum += extra;
3683 check_cursor_col();
3684 }
3685 else if (extra < 0)
3686 {
3687 curwin->w_cursor.lnum = lo;
3688 check_cursor();
3689 }
3690 else
3691 check_cursor_col();
3692 changed_cline_bef_curs();
3693 }
3694 invalidate_botline();
3695}
3696
Bram Moolenaar19e60942011-06-19 00:27:51 +02003697/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003698 * Find a window that contains "buf" and switch to it.
3699 * If there is no such window, use the current window and change "curbuf".
3700 * Caller must initialize save_curbuf to NULL.
3701 * restore_win_for_buf() MUST be called later!
3702 */
3703 static void
3704switch_to_win_for_buf(
3705 buf_T *buf,
3706 win_T **save_curwinp,
3707 tabpage_T **save_curtabp,
3708 buf_T **save_curbufp)
3709{
3710 win_T *wp;
3711 tabpage_T *tp;
3712
3713 if (find_win_for_buf(buf, &wp, &tp) == FAIL
3714 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
3715 switch_buffer(save_curbufp, buf);
3716}
3717
3718 static void
3719restore_win_for_buf(
3720 win_T *save_curwin,
3721 tabpage_T *save_curtab,
3722 buf_T *save_curbuf)
3723{
3724 if (save_curbuf == NULL)
3725 restore_win(save_curwin, save_curtab, TRUE);
3726 else
3727 restore_buffer(save_curbuf);
3728}
3729
3730/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02003731 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003732 * in Vim format (1-based). The replacement line is given as
3733 * a Python string object. The object is checked for validity
3734 * and correct format. Errors are returned as a value of FAIL.
3735 * The return value is OK on success.
3736 * If OK is returned and len_change is not NULL, *len_change
3737 * is set to the change in the buffer length.
3738 */
3739 static int
3740SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3741{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003742 buf_T *save_curbuf = NULL;
3743 win_T *save_curwin = NULL;
3744 tabpage_T *save_curtab = NULL;
3745
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003746 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003747 * There are three cases:
3748 * 1. NULL, or None - this is a deletion.
3749 * 2. A string - this is a replacement.
3750 * 3. Anything else - this is an error.
3751 */
3752 if (line == Py_None || line == NULL)
3753 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003754 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003755 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003756
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003757 VimTryStart();
3758
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003759 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003760 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003761 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003762 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003763 else
3764 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003765 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003766 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003767 if (save_curbuf == NULL)
3768 /* Only adjust marks if we managed to switch to a window that
3769 * holds the buffer, otherwise line numbers will be invalid. */
3770 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003771 }
3772
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003773 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003774
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003775 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776 return FAIL;
3777
3778 if (len_change)
3779 *len_change = -1;
3780
3781 return OK;
3782 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003783 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003784 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003785 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003786
3787 if (save == NULL)
3788 return FAIL;
3789
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003790 VimTryStart();
3791
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003792 /* We do not need to free "save" if ml_replace() consumes it. */
3793 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003794 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003795
3796 if (u_savesub((linenr_T)n) == FAIL)
3797 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003798 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003799 vim_free(save);
3800 }
3801 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3802 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003803 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003804 vim_free(save);
3805 }
3806 else
3807 changed_bytes((linenr_T)n, 0);
3808
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003809 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003810
3811 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003812 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003813 check_cursor_col();
3814
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003815 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003816 return FAIL;
3817
3818 if (len_change)
3819 *len_change = 0;
3820
3821 return OK;
3822 }
3823 else
3824 {
3825 PyErr_BadArgument();
3826 return FAIL;
3827 }
3828}
3829
Bram Moolenaar19e60942011-06-19 00:27:51 +02003830/* Replace a range of lines in the specified buffer. The line numbers are in
3831 * Vim format (1-based). The range is from lo up to, but not including, hi.
3832 * The replacement lines are given as a Python list of string objects. The
3833 * list is checked for validity and correct format. Errors are returned as a
3834 * value of FAIL. The return value is OK on success.
3835 * If OK is returned and len_change is not NULL, *len_change
3836 * is set to the change in the buffer length.
3837 */
3838 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003839SetBufferLineList(
3840 buf_T *buf,
3841 PyInt lo,
3842 PyInt hi,
3843 PyObject *list,
3844 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003845{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003846 buf_T *save_curbuf = NULL;
3847 win_T *save_curwin = NULL;
3848 tabpage_T *save_curtab = NULL;
3849
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003850 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003851 * There are three cases:
3852 * 1. NULL, or None - this is a deletion.
3853 * 2. A list - this is a replacement.
3854 * 3. Anything else - this is an error.
3855 */
3856 if (list == Py_None || list == NULL)
3857 {
3858 PyInt i;
3859 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003860
3861 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003862 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003863 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003864
3865 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003866 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003867 else
3868 {
3869 for (i = 0; i < n; ++i)
3870 {
3871 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003873 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003874 break;
3875 }
3876 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003877 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003878 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003879 if (save_curbuf == NULL)
3880 /* Only adjust marks if we managed to switch to a window that
3881 * holds the buffer, otherwise line numbers will be invalid. */
3882 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003883 }
3884
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003885 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003886
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003887 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003888 return FAIL;
3889
3890 if (len_change)
3891 *len_change = -n;
3892
3893 return OK;
3894 }
3895 else if (PyList_Check(list))
3896 {
3897 PyInt i;
3898 PyInt new_len = PyList_Size(list);
3899 PyInt old_len = hi - lo;
3900 PyInt extra = 0; /* lines added to text, can be negative */
3901 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003902
3903 if (new_len == 0) /* avoid allocating zero bytes */
3904 array = NULL;
3905 else
3906 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003907 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003908 if (array == NULL)
3909 {
3910 PyErr_NoMemory();
3911 return FAIL;
3912 }
3913 }
3914
3915 for (i = 0; i < new_len; ++i)
3916 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003917 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003918
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003919 if (!(line = PyList_GetItem(list, i)) ||
3920 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003921 {
3922 while (i)
3923 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003924 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003925 return FAIL;
3926 }
3927 }
3928
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003929 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003930 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003931
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003932 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003933 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003934
3935 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003936 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003937
3938 /* If the size of the range is reducing (ie, new_len < old_len) we
3939 * need to delete some old_len. We do this at the start, by
3940 * repeatedly deleting line "lo".
3941 */
3942 if (!PyErr_Occurred())
3943 {
3944 for (i = 0; i < old_len - new_len; ++i)
3945 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3946 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003947 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003948 break;
3949 }
3950 extra -= i;
3951 }
3952
3953 /* For as long as possible, replace the existing old_len with the
3954 * new old_len. This is a more efficient operation, as it requires
3955 * less memory allocation and freeing.
3956 */
3957 if (!PyErr_Occurred())
3958 {
3959 for (i = 0; i < old_len && i < new_len; ++i)
3960 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3961 == FAIL)
3962 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003963 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003964 break;
3965 }
3966 }
3967 else
3968 i = 0;
3969
3970 /* Now we may need to insert the remaining new old_len. If we do, we
3971 * must free the strings as we finish with them (we can't pass the
3972 * responsibility to vim in this case).
3973 */
3974 if (!PyErr_Occurred())
3975 {
3976 while (i < new_len)
3977 {
3978 if (ml_append((linenr_T)(lo + i - 1),
3979 (char_u *)array[i], 0, FALSE) == FAIL)
3980 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003981 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003982 break;
3983 }
3984 vim_free(array[i]);
3985 ++i;
3986 ++extra;
3987 }
3988 }
3989
3990 /* Free any left-over old_len, as a result of an error */
3991 while (i < new_len)
3992 {
3993 vim_free(array[i]);
3994 ++i;
3995 }
3996
3997 /* Free the array of old_len. All of its contents have now
3998 * been dealt with (either freed, or the responsibility passed
3999 * to vim.
4000 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004001 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004002
4003 /* Adjust marks. Invalidate any which lie in the
4004 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004005 * Only adjust marks if we managed to switch to a window that holds
4006 * the buffer, otherwise line numbers will be invalid. */
4007 if (save_curbuf == NULL)
4008 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004009 (long)MAXLNUM, (long)extra);
4010 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4011
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004012 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004013 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4014
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004015 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004016 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004017
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004018 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004019 return FAIL;
4020
4021 if (len_change)
4022 *len_change = new_len - old_len;
4023
4024 return OK;
4025 }
4026 else
4027 {
4028 PyErr_BadArgument();
4029 return FAIL;
4030 }
4031}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004032
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004033/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 * The line number is in Vim format (1-based). The lines to be inserted are
4035 * given as a Python list of string objects or as a single string. The lines
4036 * to be added are checked for validity and correct format. Errors are
4037 * returned as a value of FAIL. The return value is OK on success.
4038 * If OK is returned and len_change is not NULL, *len_change
4039 * is set to the change in the buffer length.
4040 */
4041 static int
4042InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4043{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004044 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004045 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004046 tabpage_T *save_curtab = NULL;
4047
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004048 /* First of all, we check the type of the supplied Python object.
4049 * It must be a string or a list, or the call is in error.
4050 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004051 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004053 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054
4055 if (str == NULL)
4056 return FAIL;
4057
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004058 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004059 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004060 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004062 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004063 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004064 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004065 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004066 else if (save_curbuf == NULL)
4067 /* Only adjust marks if we managed to switch to a window that
4068 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 appended_lines_mark((linenr_T)n, 1L);
4070
4071 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004072 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073 update_screen(VALID);
4074
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004075 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 return FAIL;
4077
4078 if (len_change)
4079 *len_change = 1;
4080
4081 return OK;
4082 }
4083 else if (PyList_Check(lines))
4084 {
4085 PyInt i;
4086 PyInt size = PyList_Size(lines);
4087 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004089 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090 if (array == NULL)
4091 {
4092 PyErr_NoMemory();
4093 return FAIL;
4094 }
4095
4096 for (i = 0; i < size; ++i)
4097 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004098 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004100 if (!(line = PyList_GetItem(lines, i)) ||
4101 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102 {
4103 while (i)
4104 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004105 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106 return FAIL;
4107 }
4108 }
4109
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004110 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004111 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004112 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113
4114 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004115 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 else
4117 {
4118 for (i = 0; i < size; ++i)
4119 {
4120 if (ml_append((linenr_T)(n + i),
4121 (char_u *)array[i], 0, FALSE) == FAIL)
4122 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004123 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124
4125 /* Free the rest of the lines */
4126 while (i < size)
4127 vim_free(array[i++]);
4128
4129 break;
4130 }
4131 vim_free(array[i]);
4132 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004133 if (i > 0 && save_curbuf == NULL)
4134 /* Only adjust marks if we managed to switch to a window that
4135 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 appended_lines_mark((linenr_T)n, (long)i);
4137 }
4138
4139 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004140 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004141 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004142 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004143
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004144 update_screen(VALID);
4145
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004146 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147 return FAIL;
4148
4149 if (len_change)
4150 *len_change = size;
4151
4152 return OK;
4153 }
4154 else
4155 {
4156 PyErr_BadArgument();
4157 return FAIL;
4158 }
4159}
4160
4161/*
4162 * Common routines for buffers and line ranges
4163 * -------------------------------------------
4164 */
4165
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004166typedef struct
4167{
4168 PyObject_HEAD
4169 buf_T *buf;
4170} BufferObject;
4171
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004172 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004173CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004174{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004175 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004176 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004177 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004178 return -1;
4179 }
4180
4181 return 0;
4182}
4183
4184 static PyObject *
4185RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4186{
4187 if (CheckBuffer(self))
4188 return NULL;
4189
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004190 if (end == -1)
4191 end = self->buf->b_ml.ml_line_count;
4192
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004193 if (n < 0)
4194 n += end - start + 1;
4195
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004196 if (n < 0 || n > end - start)
4197 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004198 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004199 return NULL;
4200 }
4201
4202 return GetBufferLine(self->buf, n+start);
4203}
4204
4205 static PyObject *
4206RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4207{
4208 PyInt size;
4209
4210 if (CheckBuffer(self))
4211 return NULL;
4212
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004213 if (end == -1)
4214 end = self->buf->b_ml.ml_line_count;
4215
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 size = end - start + 1;
4217
4218 if (lo < 0)
4219 lo = 0;
4220 else if (lo > size)
4221 lo = size;
4222 if (hi < 0)
4223 hi = 0;
4224 if (hi < lo)
4225 hi = lo;
4226 else if (hi > size)
4227 hi = size;
4228
4229 return GetBufferLineList(self->buf, lo+start, hi+start);
4230}
4231
4232 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004233RBAsItem(
4234 BufferObject *self,
4235 PyInt n,
4236 PyObject *valObject,
4237 PyInt start,
4238 PyInt end,
4239 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004240{
4241 PyInt len_change;
4242
4243 if (CheckBuffer(self))
4244 return -1;
4245
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004246 if (end == -1)
4247 end = self->buf->b_ml.ml_line_count;
4248
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004249 if (n < 0)
4250 n += end - start + 1;
4251
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004252 if (n < 0 || n > end - start)
4253 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004254 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004255 return -1;
4256 }
4257
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004258 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004259 return -1;
4260
4261 if (new_end)
4262 *new_end = end + len_change;
4263
4264 return 0;
4265}
4266
Bram Moolenaar19e60942011-06-19 00:27:51 +02004267 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004268RBAsSlice(
4269 BufferObject *self,
4270 PyInt lo,
4271 PyInt hi,
4272 PyObject *valObject,
4273 PyInt start,
4274 PyInt end,
4275 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004276{
4277 PyInt size;
4278 PyInt len_change;
4279
4280 /* Self must be a valid buffer */
4281 if (CheckBuffer(self))
4282 return -1;
4283
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004284 if (end == -1)
4285 end = self->buf->b_ml.ml_line_count;
4286
Bram Moolenaar19e60942011-06-19 00:27:51 +02004287 /* Sort out the slice range */
4288 size = end - start + 1;
4289
4290 if (lo < 0)
4291 lo = 0;
4292 else if (lo > size)
4293 lo = size;
4294 if (hi < 0)
4295 hi = 0;
4296 if (hi < lo)
4297 hi = lo;
4298 else if (hi > size)
4299 hi = size;
4300
4301 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004302 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004303 return -1;
4304
4305 if (new_end)
4306 *new_end = end + len_change;
4307
4308 return 0;
4309}
4310
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004311
4312 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004313RBAppend(
4314 BufferObject *self,
4315 PyObject *args,
4316 PyInt start,
4317 PyInt end,
4318 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004319{
4320 PyObject *lines;
4321 PyInt len_change;
4322 PyInt max;
4323 PyInt n;
4324
4325 if (CheckBuffer(self))
4326 return NULL;
4327
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004328 if (end == -1)
4329 end = self->buf->b_ml.ml_line_count;
4330
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004331 max = n = end - start + 1;
4332
4333 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4334 return NULL;
4335
4336 if (n < 0 || n > max)
4337 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004338 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004339 return NULL;
4340 }
4341
4342 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4343 return NULL;
4344
4345 if (new_end)
4346 *new_end = end + len_change;
4347
4348 Py_INCREF(Py_None);
4349 return Py_None;
4350}
4351
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004352/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004353 */
4354
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004355static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004356static PySequenceMethods RangeAsSeq;
4357static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004358
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004359typedef struct
4360{
4361 PyObject_HEAD
4362 BufferObject *buf;
4363 PyInt start;
4364 PyInt end;
4365} RangeObject;
4366
4367 static PyObject *
4368RangeNew(buf_T *buf, PyInt start, PyInt end)
4369{
4370 BufferObject *bufr;
4371 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004372 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004373 if (self == NULL)
4374 return NULL;
4375
4376 bufr = (BufferObject *)BufferNew(buf);
4377 if (bufr == NULL)
4378 {
4379 Py_DECREF(self);
4380 return NULL;
4381 }
4382 Py_INCREF(bufr);
4383
4384 self->buf = bufr;
4385 self->start = start;
4386 self->end = end;
4387
4388 return (PyObject *)(self);
4389}
4390
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004391 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004392RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004393{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004394 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004395 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004396 PyObject_GC_Del((void *)(self));
4397}
4398
4399 static int
4400RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4401{
4402 Py_VISIT(((PyObject *)(self->buf)));
4403 return 0;
4404}
4405
4406 static int
4407RangeClear(RangeObject *self)
4408{
4409 Py_CLEAR(self->buf);
4410 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004411}
4412
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004413 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004414RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004415{
4416 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004417 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004418 return -1; /* ??? */
4419
Bram Moolenaard6e39182013-05-21 18:30:34 +02004420 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004421}
4422
4423 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004424RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004425{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004426 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004427}
4428
4429 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004430RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004431{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004432 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004433}
4434
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004435static char *RangeAttrs[] = {
4436 "start", "end",
4437 NULL
4438};
4439
4440 static PyObject *
4441RangeDir(PyObject *self)
4442{
4443 return ObjectDir(self, RangeAttrs);
4444}
4445
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004446 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004447RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004448{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004449 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004450}
4451
4452 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004453RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004454{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004455 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004456 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4457 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004458 else
4459 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004460 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004461
4462 if (name == NULL)
4463 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004464
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004465 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004466 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004467 }
4468}
4469
4470static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004471 /* name, function, calling, documentation */
4472 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004473 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4474 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004475};
4476
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004477static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004478static PySequenceMethods BufferAsSeq;
4479static PyMappingMethods BufferAsMapping;
4480
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004481 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004482BufferNew(buf_T *buf)
4483{
4484 /* We need to handle deletion of buffers underneath us.
4485 * If we add a "b_python*_ref" field to the buf_T structure,
4486 * then we can get at it in buf_freeall() in vim. We then
4487 * need to create only ONE Python object per buffer - if
4488 * we try to create a second, just INCREF the existing one
4489 * and return it. The (single) Python object referring to
4490 * the buffer is stored in "b_python*_ref".
4491 * Question: what to do on a buf_freeall(). We'll probably
4492 * have to either delete the Python object (DECREF it to
4493 * zero - a bad idea, as it leaves dangling refs!) or
4494 * set the buf_T * value to an invalid value (-1?), which
4495 * means we need checks in all access functions... Bah.
4496 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004497 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004498 * b_python_ref and b_python3_ref fields respectively.
4499 */
4500
4501 BufferObject *self;
4502
4503 if (BUF_PYTHON_REF(buf) != NULL)
4504 {
4505 self = BUF_PYTHON_REF(buf);
4506 Py_INCREF(self);
4507 }
4508 else
4509 {
4510 self = PyObject_NEW(BufferObject, &BufferType);
4511 if (self == NULL)
4512 return NULL;
4513 self->buf = buf;
4514 BUF_PYTHON_REF(buf) = self;
4515 }
4516
4517 return (PyObject *)(self);
4518}
4519
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004520 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004521BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004522{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004523 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4524 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004525
4526 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004527}
4528
Bram Moolenaar971db462013-05-12 18:44:48 +02004529 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004530BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004531{
4532 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004533 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004534 return -1; /* ??? */
4535
Bram Moolenaard6e39182013-05-21 18:30:34 +02004536 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004537}
4538
4539 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004540BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004541{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004542 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004543}
4544
4545 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004546BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004547{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004548 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004549}
4550
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004551static char *BufferAttrs[] = {
4552 "name", "number", "vars", "options", "valid",
4553 NULL
4554};
4555
4556 static PyObject *
4557BufferDir(PyObject *self)
4558{
4559 return ObjectDir(self, BufferAttrs);
4560}
4561
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004562 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004563BufferAttrValid(BufferObject *self, char *name)
4564{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004565 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004566
4567 if (strcmp(name, "valid") != 0)
4568 return NULL;
4569
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004570 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4571 Py_INCREF(ret);
4572 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004573}
4574
4575 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004576BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004577{
4578 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004579 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004580 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004581 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004582 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004583 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004584 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004585 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004586 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4587 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004588 else if (strcmp(name, "__members__") == 0)
4589 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004590 else
4591 return NULL;
4592}
4593
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004594 static int
4595BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4596{
4597 if (CheckBuffer(self))
4598 return -1;
4599
4600 if (strcmp(name, "name") == 0)
4601 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004602 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004603 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004604 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004605 PyObject *todecref;
4606
4607 if (!(val = StringToChars(valObject, &todecref)))
4608 return -1;
4609
4610 VimTryStart();
4611 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4612 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004613 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004614 aucmd_restbuf(&aco);
4615 Py_XDECREF(todecref);
4616 if (VimTryEnd())
4617 return -1;
4618
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004619 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004620 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004621 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004622 return -1;
4623 }
4624 return 0;
4625 }
4626 else
4627 {
4628 PyErr_SetString(PyExc_AttributeError, name);
4629 return -1;
4630 }
4631}
4632
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004633 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004634BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004635{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004636 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004637}
4638
4639 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004640BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004641{
4642 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004643 char_u *pmark;
4644 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004645 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004646 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004647
Bram Moolenaard6e39182013-05-21 18:30:34 +02004648 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004649 return NULL;
4650
Bram Moolenaar389a1792013-06-23 13:00:44 +02004651 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004653
Bram Moolenaar389a1792013-06-23 13:00:44 +02004654 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004655 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004656 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004657 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004658 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004659 return NULL;
4660 }
4661
4662 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004663
4664 Py_XDECREF(todecref);
4665
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004666 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004667 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004668 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004669 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004670 if (VimTryEnd())
4671 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004672
4673 if (posp == NULL)
4674 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004675 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004676 return NULL;
4677 }
4678
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004679 if (posp->lnum <= 0)
4680 {
4681 /* Or raise an error? */
4682 Py_INCREF(Py_None);
4683 return Py_None;
4684 }
4685
4686 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4687}
4688
4689 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004690BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691{
4692 PyInt start;
4693 PyInt end;
4694
Bram Moolenaard6e39182013-05-21 18:30:34 +02004695 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004696 return NULL;
4697
4698 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4699 return NULL;
4700
Bram Moolenaard6e39182013-05-21 18:30:34 +02004701 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004702}
4703
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004704 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004705BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004706{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004707 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004708 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004709 else
4710 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004711 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004712
4713 if (name == NULL)
4714 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004715
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004716 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004717 }
4718}
4719
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004720static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004721 /* name, function, calling, documentation */
4722 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004723 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004724 {"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 +02004725 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4726 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727};
4728
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004729/*
4730 * Buffer list object - Implementation
4731 */
4732
4733static PyTypeObject BufMapType;
4734
4735typedef struct
4736{
4737 PyObject_HEAD
4738} BufMapObject;
4739
4740 static PyInt
4741BufMapLength(PyObject *self UNUSED)
4742{
4743 buf_T *b = firstbuf;
4744 PyInt n = 0;
4745
4746 while (b)
4747 {
4748 ++n;
4749 b = b->b_next;
4750 }
4751
4752 return n;
4753}
4754
4755 static PyObject *
4756BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4757{
4758 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004759 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004760
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004761 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004762 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004763
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004764 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004765
4766 if (b)
4767 return BufferNew(b);
4768 else
4769 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004770 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004771 return NULL;
4772 }
4773}
4774
4775 static void
4776BufMapIterDestruct(PyObject *buffer)
4777{
4778 /* Iteration was stopped before all buffers were processed */
4779 if (buffer)
4780 {
4781 Py_DECREF(buffer);
4782 }
4783}
4784
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004785 static int
4786BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4787{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004788 if (buffer)
4789 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004790 return 0;
4791}
4792
4793 static int
4794BufMapIterClear(PyObject **buffer)
4795{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004796 if (*buffer)
4797 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004798 return 0;
4799}
4800
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004801 static PyObject *
4802BufMapIterNext(PyObject **buffer)
4803{
4804 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004805 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004806
4807 if (!*buffer)
4808 return NULL;
4809
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004810 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004811
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004812 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004813 {
4814 *buffer = NULL;
4815 return NULL;
4816 }
4817
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004818 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004819 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004820 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004821 return NULL;
4822 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004823 /* Do not increment reference: we no longer hold it (decref), but whoever
4824 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004825 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004826}
4827
4828 static PyObject *
4829BufMapIter(PyObject *self UNUSED)
4830{
4831 PyObject *buffer;
4832
4833 buffer = BufferNew(firstbuf);
4834 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004835 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4836 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004837}
4838
4839static PyMappingMethods BufMapAsMapping = {
4840 (lenfunc) BufMapLength,
4841 (binaryfunc) BufMapItem,
4842 (objobjargproc) 0,
4843};
4844
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004845/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004846 */
4847
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004848static char *CurrentAttrs[] = {
4849 "buffer", "window", "line", "range", "tabpage",
4850 NULL
4851};
4852
4853 static PyObject *
4854CurrentDir(PyObject *self)
4855{
4856 return ObjectDir(self, CurrentAttrs);
4857}
4858
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004859 static PyObject *
4860CurrentGetattr(PyObject *self UNUSED, char *name)
4861{
4862 if (strcmp(name, "buffer") == 0)
4863 return (PyObject *)BufferNew(curbuf);
4864 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004865 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004866 else if (strcmp(name, "tabpage") == 0)
4867 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004868 else if (strcmp(name, "line") == 0)
4869 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4870 else if (strcmp(name, "range") == 0)
4871 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004872 else if (strcmp(name, "__members__") == 0)
4873 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004874 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004875#if PY_MAJOR_VERSION < 3
4876 return Py_FindMethod(WindowMethods, self, name);
4877#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004878 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004879#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004880}
4881
4882 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004883CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004884{
4885 if (strcmp(name, "line") == 0)
4886 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004887 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4888 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004889 return -1;
4890
4891 return 0;
4892 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004893 else if (strcmp(name, "buffer") == 0)
4894 {
4895 int count;
4896
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004897 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004898 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004899 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004900 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004901 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004902 return -1;
4903 }
4904
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004905 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004906 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004907 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004908
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004909 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004910 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4911 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004912 if (VimTryEnd())
4913 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004914 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004915 return -1;
4916 }
4917
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004918 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004919 }
4920 else if (strcmp(name, "window") == 0)
4921 {
4922 int count;
4923
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004924 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004925 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004926 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004927 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004928 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004929 return -1;
4930 }
4931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004932 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004933 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004934 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004935
4936 if (!count)
4937 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004938 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004939 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004940 return -1;
4941 }
4942
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004943 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004944 win_goto(((WindowObject *)(valObject))->win);
4945 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004946 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004947 if (VimTryEnd())
4948 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004949 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004950 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004951 return -1;
4952 }
4953
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004954 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004955 }
4956 else if (strcmp(name, "tabpage") == 0)
4957 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004958 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004959 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004960 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004961 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004962 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004963 return -1;
4964 }
4965
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004966 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004967 return -1;
4968
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004969 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004970 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4971 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004972 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004973 if (VimTryEnd())
4974 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004975 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004976 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004977 return -1;
4978 }
4979
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004980 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004981 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004982 else
4983 {
4984 PyErr_SetString(PyExc_AttributeError, name);
4985 return -1;
4986 }
4987}
4988
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004989static struct PyMethodDef CurrentMethods[] = {
4990 /* name, function, calling, documentation */
4991 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4992 { NULL, NULL, 0, NULL}
4993};
4994
Bram Moolenaardb913952012-06-29 12:54:53 +02004995 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004996init_range_cmd(exarg_T *eap)
4997{
4998 RangeStart = eap->line1;
4999 RangeEnd = eap->line2;
5000}
5001
5002 static void
5003init_range_eval(typval_T *rettv UNUSED)
5004{
5005 RangeStart = (PyInt) curwin->w_cursor.lnum;
5006 RangeEnd = RangeStart;
5007}
5008
5009 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005010run_cmd(const char *cmd, void *arg UNUSED
5011#ifdef PY_CAN_RECURSE
5012 , PyGILState_STATE *pygilstate UNUSED
5013#endif
5014 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005015{
Bram Moolenaar41009372013-07-01 22:03:04 +02005016 PyObject *run_ret;
5017 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5018 if (run_ret != NULL)
5019 {
5020 Py_DECREF(run_ret);
5021 }
5022 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5023 {
5024 EMSG2(_(e_py_systemexit), "python");
5025 PyErr_Clear();
5026 }
5027 else
5028 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005029}
5030
5031static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5032static int code_hdr_len = 30;
5033
5034 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005035run_do(const char *cmd, void *arg UNUSED
5036#ifdef PY_CAN_RECURSE
5037 , PyGILState_STATE *pygilstate
5038#endif
5039 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005040{
5041 PyInt lnum;
5042 size_t len;
5043 char *code;
5044 int status;
5045 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005046 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005047
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005048 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005049 {
5050 EMSG(_("cannot save undo information"));
5051 return;
5052 }
5053
5054 len = code_hdr_len + STRLEN(cmd);
5055 code = PyMem_New(char, len + 1);
5056 memcpy(code, code_hdr, code_hdr_len);
5057 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005058 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5059 status = -1;
5060 if (run_ret != NULL)
5061 {
5062 status = 0;
5063 Py_DECREF(run_ret);
5064 }
5065 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5066 {
5067 PyMem_Free(code);
5068 EMSG2(_(e_py_systemexit), "python");
5069 PyErr_Clear();
5070 return;
5071 }
5072 else
5073 PyErr_PrintEx(1);
5074
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005075 PyMem_Free(code);
5076
5077 if (status)
5078 {
5079 EMSG(_("failed to run the code"));
5080 return;
5081 }
5082
5083 status = 0;
5084 pymain = PyImport_AddModule("__main__");
5085 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005086#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005087 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005088#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005089
5090 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5091 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005092 PyObject *line;
5093 PyObject *linenr;
5094 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005095
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005096#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005097 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005098#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005099 if (!(line = GetBufferLine(curbuf, lnum)))
5100 goto err;
5101 if (!(linenr = PyInt_FromLong((long) lnum)))
5102 {
5103 Py_DECREF(line);
5104 goto err;
5105 }
5106 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5107 Py_DECREF(line);
5108 Py_DECREF(linenr);
5109 if (!ret)
5110 goto err;
5111
5112 if (ret != Py_None)
5113 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5114 goto err;
5115
5116 Py_XDECREF(ret);
5117 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005118#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005119 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005120#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005121 }
5122 goto out;
5123err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005124#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005125 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005126#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005127 PyErr_PrintEx(0);
5128 PythonIO_Flush();
5129 status = 1;
5130out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005131#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005132 if (!status)
5133 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005134#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005135 Py_DECREF(pyfunc);
5136 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5137 if (status)
5138 return;
5139 check_cursor();
5140 update_curbuf(NOT_VALID);
5141}
5142
5143 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005144run_eval(const char *cmd, typval_T *rettv
5145#ifdef PY_CAN_RECURSE
5146 , PyGILState_STATE *pygilstate UNUSED
5147#endif
5148 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005149{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005150 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005151
Bram Moolenaar41009372013-07-01 22:03:04 +02005152 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005153 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005154 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005155 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005156 {
5157 EMSG2(_(e_py_systemexit), "python");
5158 PyErr_Clear();
5159 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005160 else
5161 {
5162 if (PyErr_Occurred() && !msg_silent)
5163 PyErr_PrintEx(0);
5164 EMSG(_("E858: Eval did not return a valid python object"));
5165 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005166 }
5167 else
5168 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005169 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005170 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005171 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005172 }
5173 PyErr_Clear();
5174}
5175
5176 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005177set_ref_in_py(const int copyID)
5178{
5179 pylinkedlist_T *cur;
5180 dict_T *dd;
5181 list_T *ll;
5182
5183 if (lastdict != NULL)
5184 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5185 {
5186 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5187 if (dd->dv_copyID != copyID)
5188 {
5189 dd->dv_copyID = copyID;
5190 set_ref_in_ht(&dd->dv_hashtab, copyID);
5191 }
5192 }
5193
5194 if (lastlist != NULL)
5195 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5196 {
5197 ll = ((ListObject *) (cur->pll_obj))->list;
5198 if (ll->lv_copyID != copyID)
5199 {
5200 ll->lv_copyID = copyID;
5201 set_ref_in_list(ll, copyID);
5202 }
5203 }
5204}
5205
5206 static int
5207set_string_copy(char_u *str, typval_T *tv)
5208{
5209 tv->vval.v_string = vim_strsave(str);
5210 if (tv->vval.v_string == NULL)
5211 {
5212 PyErr_NoMemory();
5213 return -1;
5214 }
5215 return 0;
5216}
5217
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005218 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005219pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005220{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005221 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005222 char_u *key;
5223 dictitem_T *di;
5224 PyObject *keyObject;
5225 PyObject *valObject;
5226 Py_ssize_t iter = 0;
5227
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005228 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005229 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005230
5231 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005232 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005233
5234 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5235 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005236 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005237
Bram Moolenaara03e6312013-05-29 22:49:26 +02005238 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005239 {
5240 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005241 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005242 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005243
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005244 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005245 {
5246 dict_unref(dict);
5247 return -1;
5248 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005249
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005250 if (*key == NUL)
5251 {
5252 dict_unref(dict);
5253 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005254 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005255 return -1;
5256 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005257
5258 di = dictitem_alloc(key);
5259
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005260 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005261
5262 if (di == NULL)
5263 {
5264 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005265 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005266 return -1;
5267 }
5268 di->di_tv.v_lock = 0;
5269
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005270 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005271 {
5272 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005273 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005274 return -1;
5275 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005276
5277 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005278 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005279 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005280 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005281 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005282 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005283 return -1;
5284 }
5285 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005286
5287 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005288 return 0;
5289}
5290
5291 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005292pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005293{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005294 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005295 char_u *key;
5296 dictitem_T *di;
5297 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005298 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005299 PyObject *keyObject;
5300 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005301
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005302 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005303 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005304
5305 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005306 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005307
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005308 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005309 {
5310 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005311 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005312 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005313
5314 if (!(iterator = PyObject_GetIter(list)))
5315 {
5316 dict_unref(dict);
5317 Py_DECREF(list);
5318 return -1;
5319 }
5320 Py_DECREF(list);
5321
5322 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005323 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005324 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005325
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005326 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005327 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005328 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005329 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005330 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005331 return -1;
5332 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005333
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005334 if (*key == NUL)
5335 {
5336 Py_DECREF(keyObject);
5337 Py_DECREF(iterator);
5338 Py_XDECREF(todecref);
5339 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005340 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005341 return -1;
5342 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005343
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005344 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005345 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005346 Py_DECREF(keyObject);
5347 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005348 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005349 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005350 return -1;
5351 }
5352
5353 di = dictitem_alloc(key);
5354
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005355 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005356 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005357
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005358 if (di == NULL)
5359 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005360 Py_DECREF(iterator);
5361 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005362 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005363 PyErr_NoMemory();
5364 return -1;
5365 }
5366 di->di_tv.v_lock = 0;
5367
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005368 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005369 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005370 Py_DECREF(iterator);
5371 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005372 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005373 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005374 return -1;
5375 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005376
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005377 Py_DECREF(valObject);
5378
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005379 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005380 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005381 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005382 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005383 dictitem_free(di);
5384 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005385 return -1;
5386 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005387 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005388 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005389 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005390 return 0;
5391}
5392
5393 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005394pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005395{
5396 list_T *l;
5397
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005398 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005399 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005400
5401 tv->v_type = VAR_LIST;
5402 tv->vval.v_list = l;
5403
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005404 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005405 {
5406 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005407 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005408 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005409
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005410 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005411 return 0;
5412}
5413
Bram Moolenaardb913952012-06-29 12:54:53 +02005414typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5415
5416 static int
5417convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005418 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005419{
5420 PyObject *capsule;
5421 char hexBuf[sizeof(void *) * 2 + 3];
5422
5423 sprintf(hexBuf, "%p", obj);
5424
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005425# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005426 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005427# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005428 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005429# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005430 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005431 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005432# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005433 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005434# else
5435 capsule = PyCObject_FromVoidPtr(tv, NULL);
5436# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005437 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5438 {
5439 Py_DECREF(capsule);
5440 tv->v_type = VAR_UNKNOWN;
5441 return -1;
5442 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005443
5444 Py_DECREF(capsule);
5445
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005446 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005447 {
5448 tv->v_type = VAR_UNKNOWN;
5449 return -1;
5450 }
5451 /* As we are not using copy_tv which increments reference count we must
5452 * do it ourself. */
5453 switch(tv->v_type)
5454 {
5455 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5456 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5457 }
5458 }
5459 else
5460 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005461 typval_T *v;
5462
5463# ifdef PY_USE_CAPSULE
5464 v = PyCapsule_GetPointer(capsule, NULL);
5465# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005466 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005467# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005468 copy_tv(v, tv);
5469 }
5470 return 0;
5471}
5472
5473 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005474ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5475{
5476 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005477 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005478
5479 if (!(lookup_dict = PyDict_New()))
5480 return -1;
5481
5482 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5483 {
5484 tv->v_type = VAR_DICT;
5485 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5486 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005487 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005488 }
5489 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005490 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005491 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005493 else
5494 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005495 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005496 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005497 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005498 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005499 }
5500 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005501 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005502}
5503
5504 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005505ConvertFromPyObject(PyObject *obj, typval_T *tv)
5506{
5507 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005508 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005509
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005510 if (!(lookup_dict = PyDict_New()))
5511 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005512 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005513 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005514 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005515}
5516
5517 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005518_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005519{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005520 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005521 {
5522 tv->v_type = VAR_DICT;
5523 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5524 ++tv->vval.v_dict->dv_refcount;
5525 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005526 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005527 {
5528 tv->v_type = VAR_LIST;
5529 tv->vval.v_list = (((ListObject *)(obj))->list);
5530 ++tv->vval.v_list->lv_refcount;
5531 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005532 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005533 {
5534 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5535 return -1;
5536
5537 tv->v_type = VAR_FUNC;
5538 func_ref(tv->vval.v_string);
5539 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005540 else if (PyBytes_Check(obj))
5541 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005542 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005543
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005544 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005545 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005546 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005547 return -1;
5548
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005549 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005550 return -1;
5551
5552 tv->v_type = VAR_STRING;
5553 }
5554 else if (PyUnicode_Check(obj))
5555 {
5556 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005557 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005558
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005559 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005560 if (bytes == NULL)
5561 return -1;
5562
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005564 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005565 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005566 return -1;
5567
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005568 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005569 {
5570 Py_XDECREF(bytes);
5571 return -1;
5572 }
5573 Py_XDECREF(bytes);
5574
5575 tv->v_type = VAR_STRING;
5576 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005577#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005578 else if (PyInt_Check(obj))
5579 {
5580 tv->v_type = VAR_NUMBER;
5581 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005582 if (PyErr_Occurred())
5583 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005584 }
5585#endif
5586 else if (PyLong_Check(obj))
5587 {
5588 tv->v_type = VAR_NUMBER;
5589 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005590 if (PyErr_Occurred())
5591 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005592 }
5593 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005594 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005595#ifdef FEAT_FLOAT
5596 else if (PyFloat_Check(obj))
5597 {
5598 tv->v_type = VAR_FLOAT;
5599 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5600 }
5601#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005602 else if (PyObject_HasAttrString(obj, "keys"))
5603 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005604 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005605 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005606 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005607 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005608 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005609 else if (PyNumber_Check(obj))
5610 {
5611 PyObject *num;
5612
5613 if (!(num = PyNumber_Long(obj)))
5614 return -1;
5615
5616 tv->v_type = VAR_NUMBER;
5617 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5618
5619 Py_DECREF(num);
5620 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005621 else
5622 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005623 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005624 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005625 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005626 return -1;
5627 }
5628 return 0;
5629}
5630
5631 static PyObject *
5632ConvertToPyObject(typval_T *tv)
5633{
5634 if (tv == NULL)
5635 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005636 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005637 return NULL;
5638 }
5639 switch (tv->v_type)
5640 {
5641 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005642 return PyBytes_FromString(tv->vval.v_string == NULL
5643 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005644 case VAR_NUMBER:
5645 return PyLong_FromLong((long) tv->vval.v_number);
5646#ifdef FEAT_FLOAT
5647 case VAR_FLOAT:
5648 return PyFloat_FromDouble((double) tv->vval.v_float);
5649#endif
5650 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005651 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005652 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005653 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005654 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005655 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005656 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005657 case VAR_UNKNOWN:
5658 Py_INCREF(Py_None);
5659 return Py_None;
5660 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005661 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005662 return NULL;
5663 }
5664}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005665
5666typedef struct
5667{
5668 PyObject_HEAD
5669} CurrentObject;
5670static PyTypeObject CurrentType;
5671
5672 static void
5673init_structs(void)
5674{
5675 vim_memset(&OutputType, 0, sizeof(OutputType));
5676 OutputType.tp_name = "vim.message";
5677 OutputType.tp_basicsize = sizeof(OutputObject);
5678 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5679 OutputType.tp_doc = "vim message object";
5680 OutputType.tp_methods = OutputMethods;
5681#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005682 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5683 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005684 OutputType.tp_alloc = call_PyType_GenericAlloc;
5685 OutputType.tp_new = call_PyType_GenericNew;
5686 OutputType.tp_free = call_PyObject_Free;
5687#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005688 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5689 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005690#endif
5691
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005692 vim_memset(&IterType, 0, sizeof(IterType));
5693 IterType.tp_name = "vim.iter";
5694 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005695 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005696 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005697 IterType.tp_iter = (getiterfunc)IterIter;
5698 IterType.tp_iternext = (iternextfunc)IterNext;
5699 IterType.tp_dealloc = (destructor)IterDestructor;
5700 IterType.tp_traverse = (traverseproc)IterTraverse;
5701 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005702
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005703 vim_memset(&BufferType, 0, sizeof(BufferType));
5704 BufferType.tp_name = "vim.buffer";
5705 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005706 BufferType.tp_dealloc = (destructor)BufferDestructor;
5707 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005708 BufferType.tp_as_sequence = &BufferAsSeq;
5709 BufferType.tp_as_mapping = &BufferAsMapping;
5710 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5711 BufferType.tp_doc = "vim buffer object";
5712 BufferType.tp_methods = BufferMethods;
5713#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005714 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005715 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005716 BufferType.tp_alloc = call_PyType_GenericAlloc;
5717 BufferType.tp_new = call_PyType_GenericNew;
5718 BufferType.tp_free = call_PyObject_Free;
5719#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005720 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005721 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005722#endif
5723
5724 vim_memset(&WindowType, 0, sizeof(WindowType));
5725 WindowType.tp_name = "vim.window";
5726 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005727 WindowType.tp_dealloc = (destructor)WindowDestructor;
5728 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005729 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005730 WindowType.tp_doc = "vim Window object";
5731 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005732 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5733 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005734#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005735 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5736 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005737 WindowType.tp_alloc = call_PyType_GenericAlloc;
5738 WindowType.tp_new = call_PyType_GenericNew;
5739 WindowType.tp_free = call_PyObject_Free;
5740#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005741 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5742 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005743#endif
5744
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005745 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5746 TabPageType.tp_name = "vim.tabpage";
5747 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005748 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5749 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005750 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5751 TabPageType.tp_doc = "vim tab page object";
5752 TabPageType.tp_methods = TabPageMethods;
5753#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005754 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005755 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5756 TabPageType.tp_new = call_PyType_GenericNew;
5757 TabPageType.tp_free = call_PyObject_Free;
5758#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005759 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005760#endif
5761
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005762 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5763 BufMapType.tp_name = "vim.bufferlist";
5764 BufMapType.tp_basicsize = sizeof(BufMapObject);
5765 BufMapType.tp_as_mapping = &BufMapAsMapping;
5766 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005767 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005768 BufferType.tp_doc = "vim buffer list";
5769
5770 vim_memset(&WinListType, 0, sizeof(WinListType));
5771 WinListType.tp_name = "vim.windowlist";
5772 WinListType.tp_basicsize = sizeof(WinListType);
5773 WinListType.tp_as_sequence = &WinListAsSeq;
5774 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5775 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005776 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005777
5778 vim_memset(&TabListType, 0, sizeof(TabListType));
5779 TabListType.tp_name = "vim.tabpagelist";
5780 TabListType.tp_basicsize = sizeof(TabListType);
5781 TabListType.tp_as_sequence = &TabListAsSeq;
5782 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5783 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005784
5785 vim_memset(&RangeType, 0, sizeof(RangeType));
5786 RangeType.tp_name = "vim.range";
5787 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005788 RangeType.tp_dealloc = (destructor)RangeDestructor;
5789 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005790 RangeType.tp_as_sequence = &RangeAsSeq;
5791 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005792 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005793 RangeType.tp_doc = "vim Range object";
5794 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005795 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5796 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005797#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005798 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005799 RangeType.tp_alloc = call_PyType_GenericAlloc;
5800 RangeType.tp_new = call_PyType_GenericNew;
5801 RangeType.tp_free = call_PyObject_Free;
5802#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005803 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005804#endif
5805
5806 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5807 CurrentType.tp_name = "vim.currentdata";
5808 CurrentType.tp_basicsize = sizeof(CurrentObject);
5809 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5810 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005811 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005812#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005813 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5814 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005815#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005816 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5817 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005818#endif
5819
5820 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5821 DictionaryType.tp_name = "vim.dictionary";
5822 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005823 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005824 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005825 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005826 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005827 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5828 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005829 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5830 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5831 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005832#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005833 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5834 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005835#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005836 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5837 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005838#endif
5839
5840 vim_memset(&ListType, 0, sizeof(ListType));
5841 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005842 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005843 ListType.tp_basicsize = sizeof(ListObject);
5844 ListType.tp_as_sequence = &ListAsSeq;
5845 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005846 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005847 ListType.tp_doc = "list pushing modifications to vim structure";
5848 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005849 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005850 ListType.tp_new = (newfunc)ListConstructor;
5851 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005852#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005853 ListType.tp_getattro = (getattrofunc)ListGetattro;
5854 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005855#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005856 ListType.tp_getattr = (getattrfunc)ListGetattr;
5857 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005858#endif
5859
5860 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005861 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005862 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005863 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5864 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005865 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005866 FunctionType.tp_doc = "object that calls vim function";
5867 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005868 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005869 FunctionType.tp_new = (newfunc)FunctionConstructor;
5870 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005871#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005872 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005873#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005874 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005875#endif
5876
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005877 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5878 OptionsType.tp_name = "vim.options";
5879 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005880 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005881 OptionsType.tp_doc = "object for manipulating options";
5882 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005883 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5884 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5885 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005886
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005887 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5888 LoaderType.tp_name = "vim.Loader";
5889 LoaderType.tp_basicsize = sizeof(LoaderObject);
5890 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5891 LoaderType.tp_doc = "vim message object";
5892 LoaderType.tp_methods = LoaderMethods;
5893 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5894
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005895#if PY_MAJOR_VERSION >= 3
5896 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5897 vimmodule.m_name = "vim";
5898 vimmodule.m_doc = "Vim Python interface\n";
5899 vimmodule.m_size = -1;
5900 vimmodule.m_methods = VimMethods;
5901#endif
5902}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005903
5904#define PYTYPE_READY(type) \
5905 if (PyType_Ready(&type)) \
5906 return -1;
5907
5908 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02005909init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005910{
5911 PYTYPE_READY(IterType);
5912 PYTYPE_READY(BufferType);
5913 PYTYPE_READY(RangeType);
5914 PYTYPE_READY(WindowType);
5915 PYTYPE_READY(TabPageType);
5916 PYTYPE_READY(BufMapType);
5917 PYTYPE_READY(WinListType);
5918 PYTYPE_READY(TabListType);
5919 PYTYPE_READY(CurrentType);
5920 PYTYPE_READY(DictionaryType);
5921 PYTYPE_READY(ListType);
5922 PYTYPE_READY(FunctionType);
5923 PYTYPE_READY(OptionsType);
5924 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005925 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005926 return 0;
5927}
5928
5929 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005930init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005931{
5932 PyObject *path;
5933 PyObject *path_hook;
5934 PyObject *path_hooks;
5935
5936 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5937 return -1;
5938
5939 if (!(path_hooks = PySys_GetObject("path_hooks")))
5940 {
5941 PyErr_Clear();
5942 path_hooks = PyList_New(1);
5943 PyList_SET_ITEM(path_hooks, 0, path_hook);
5944 if (PySys_SetObject("path_hooks", path_hooks))
5945 {
5946 Py_DECREF(path_hooks);
5947 return -1;
5948 }
5949 Py_DECREF(path_hooks);
5950 }
5951 else if (PyList_Check(path_hooks))
5952 {
5953 if (PyList_Append(path_hooks, path_hook))
5954 {
5955 Py_DECREF(path_hook);
5956 return -1;
5957 }
5958 Py_DECREF(path_hook);
5959 }
5960 else
5961 {
5962 VimTryStart();
5963 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5964 "You should now do the following:\n"
5965 "- append vim.path_hook to sys.path_hooks\n"
5966 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5967 VimTryEnd(); /* Discard the error */
5968 Py_DECREF(path_hook);
5969 return 0;
5970 }
5971
5972 if (!(path = PySys_GetObject("path")))
5973 {
5974 PyErr_Clear();
5975 path = PyList_New(1);
5976 Py_INCREF(vim_special_path_object);
5977 PyList_SET_ITEM(path, 0, vim_special_path_object);
5978 if (PySys_SetObject("path", path))
5979 {
5980 Py_DECREF(path);
5981 return -1;
5982 }
5983 Py_DECREF(path);
5984 }
5985 else if (PyList_Check(path))
5986 {
5987 if (PyList_Append(path, vim_special_path_object))
5988 return -1;
5989 }
5990 else
5991 {
5992 VimTryStart();
5993 EMSG(_("Failed to set path: sys.path is not a list\n"
5994 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5995 VimTryEnd(); /* Discard the error */
5996 }
5997
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005998 return 0;
5999}
6000
6001static BufMapObject TheBufferMap =
6002{
6003 PyObject_HEAD_INIT(&BufMapType)
6004};
6005
6006static WinListObject TheWindowList =
6007{
6008 PyObject_HEAD_INIT(&WinListType)
6009 NULL
6010};
6011
6012static CurrentObject TheCurrent =
6013{
6014 PyObject_HEAD_INIT(&CurrentType)
6015};
6016
6017static TabListObject TheTabPageList =
6018{
6019 PyObject_HEAD_INIT(&TabListType)
6020};
6021
6022static struct numeric_constant {
6023 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006024 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006025} numeric_constants[] = {
6026 {"VAR_LOCKED", VAR_LOCKED},
6027 {"VAR_FIXED", VAR_FIXED},
6028 {"VAR_SCOPE", VAR_SCOPE},
6029 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6030};
6031
6032static struct object_constant {
6033 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006034 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006035} object_constants[] = {
6036 {"buffers", (PyObject *)(void *)&TheBufferMap},
6037 {"windows", (PyObject *)(void *)&TheWindowList},
6038 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6039 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006040
6041 {"Buffer", (PyObject *)&BufferType},
6042 {"Range", (PyObject *)&RangeType},
6043 {"Window", (PyObject *)&WindowType},
6044 {"TabPage", (PyObject *)&TabPageType},
6045 {"Dictionary", (PyObject *)&DictionaryType},
6046 {"List", (PyObject *)&ListType},
6047 {"Function", (PyObject *)&FunctionType},
6048 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006049 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006050};
6051
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006052#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006053 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006054 return -1;
6055
6056#define ADD_CHECKED_OBJECT(m, name, obj) \
6057 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006058 PyObject *valObject = obj; \
6059 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006060 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006061 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006062 }
6063
6064 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006065populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006066{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006067 int i;
6068 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006069 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006070 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006071
6072 for (i = 0; i < (int)(sizeof(numeric_constants)
6073 / sizeof(struct numeric_constant));
6074 ++i)
6075 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006076 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006077
6078 for (i = 0; i < (int)(sizeof(object_constants)
6079 / sizeof(struct object_constant));
6080 ++i)
6081 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006082 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006083
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006084 valObject = object_constants[i].valObject;
6085 Py_INCREF(valObject);
6086 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006087 }
6088
6089 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6090 return -1;
6091 ADD_OBJECT(m, "error", VimError);
6092
Bram Moolenaara9922d62013-05-30 13:01:18 +02006093 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6094 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006095 ADD_CHECKED_OBJECT(m, "options",
6096 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006097
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006098 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006099 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006100 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006101
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006102 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006103 return -1;
6104 ADD_OBJECT(m, "_getcwd", py_getcwd)
6105
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006106 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006107 return -1;
6108 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006109 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006110 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006111 if (PyObject_SetAttrString(other_module, "chdir", attr))
6112 {
6113 Py_DECREF(attr);
6114 return -1;
6115 }
6116 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006117
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006118 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006119 {
6120 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006121 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006122 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006123 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6124 {
6125 Py_DECREF(attr);
6126 return -1;
6127 }
6128 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006129 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006130 else
6131 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006132
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006133 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6134 return -1;
6135
6136 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6137
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006138 if (!(imp = PyImport_ImportModule("imp")))
6139 return -1;
6140
6141 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6142 {
6143 Py_DECREF(imp);
6144 return -1;
6145 }
6146
6147 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6148 {
6149 Py_DECREF(py_find_module);
6150 Py_DECREF(imp);
6151 return -1;
6152 }
6153
6154 Py_DECREF(imp);
6155
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006156 ADD_OBJECT(m, "_find_module", py_find_module);
6157 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006158
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006159 return 0;
6160}