blob: 48b7be7bebcd4ca3a4a09cf23ae71e2bbe87a805 [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/*
3698 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003699 * in Vim format (1-based). The replacement line is given as
3700 * a Python string object. The object is checked for validity
3701 * and correct format. Errors are returned as a value of FAIL.
3702 * The return value is OK on success.
3703 * If OK is returned and len_change is not NULL, *len_change
3704 * is set to the change in the buffer length.
3705 */
3706 static int
3707SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3708{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003709 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710 * There are three cases:
3711 * 1. NULL, or None - this is a deletion.
3712 * 2. A string - this is a replacement.
3713 * 3. Anything else - this is an error.
3714 */
3715 if (line == Py_None || line == NULL)
3716 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003717 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718
3719 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003720 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003722 VimTryStart();
3723
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003724 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003725 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003726 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003727 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003728 else
3729 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003730 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003731 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3732 deleted_lines_mark((linenr_T)n, 1L);
3733 }
3734
Bram Moolenaar105bc352013-05-17 16:03:57 +02003735 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003736
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003737 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003738 return FAIL;
3739
3740 if (len_change)
3741 *len_change = -1;
3742
3743 return OK;
3744 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003745 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003746 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003747 char *save = StringToLine(line);
3748 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003749
3750 if (save == NULL)
3751 return FAIL;
3752
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003753 VimTryStart();
3754
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003755 /* We do not need to free "save" if ml_replace() consumes it. */
3756 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003757 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003758
3759 if (u_savesub((linenr_T)n) == FAIL)
3760 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003761 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003762 vim_free(save);
3763 }
3764 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3765 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003766 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003767 vim_free(save);
3768 }
3769 else
3770 changed_bytes((linenr_T)n, 0);
3771
Bram Moolenaar105bc352013-05-17 16:03:57 +02003772 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003773
3774 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003775 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776 check_cursor_col();
3777
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003778 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003779 return FAIL;
3780
3781 if (len_change)
3782 *len_change = 0;
3783
3784 return OK;
3785 }
3786 else
3787 {
3788 PyErr_BadArgument();
3789 return FAIL;
3790 }
3791}
3792
Bram Moolenaar19e60942011-06-19 00:27:51 +02003793/* Replace a range of lines in the specified buffer. The line numbers are in
3794 * Vim format (1-based). The range is from lo up to, but not including, hi.
3795 * The replacement lines are given as a Python list of string objects. The
3796 * list is checked for validity and correct format. Errors are returned as a
3797 * value of FAIL. The return value is OK on success.
3798 * If OK is returned and len_change is not NULL, *len_change
3799 * is set to the change in the buffer length.
3800 */
3801 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003802SetBufferLineList(
3803 buf_T *buf,
3804 PyInt lo,
3805 PyInt hi,
3806 PyObject *list,
3807 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003808{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003809 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003810 * There are three cases:
3811 * 1. NULL, or None - this is a deletion.
3812 * 2. A list - this is a replacement.
3813 * 3. Anything else - this is an error.
3814 */
3815 if (list == Py_None || list == NULL)
3816 {
3817 PyInt i;
3818 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003819 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003820
3821 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003822 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003823 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003824
3825 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003826 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003827 else
3828 {
3829 for (i = 0; i < n; ++i)
3830 {
3831 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3832 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003833 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003834 break;
3835 }
3836 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003837 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003838 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3839 deleted_lines_mark((linenr_T)lo, (long)i);
3840 }
3841
Bram Moolenaar105bc352013-05-17 16:03:57 +02003842 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003843
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003844 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003845 return FAIL;
3846
3847 if (len_change)
3848 *len_change = -n;
3849
3850 return OK;
3851 }
3852 else if (PyList_Check(list))
3853 {
3854 PyInt i;
3855 PyInt new_len = PyList_Size(list);
3856 PyInt old_len = hi - lo;
3857 PyInt extra = 0; /* lines added to text, can be negative */
3858 char **array;
3859 buf_T *savebuf;
3860
3861 if (new_len == 0) /* avoid allocating zero bytes */
3862 array = NULL;
3863 else
3864 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003865 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003866 if (array == NULL)
3867 {
3868 PyErr_NoMemory();
3869 return FAIL;
3870 }
3871 }
3872
3873 for (i = 0; i < new_len; ++i)
3874 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003875 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003876
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003877 if (!(line = PyList_GetItem(list, i)) ||
3878 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003879 {
3880 while (i)
3881 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003882 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003883 return FAIL;
3884 }
3885 }
3886
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003887 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003888 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003889
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003890 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003891 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003892
3893 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003894 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003895
3896 /* If the size of the range is reducing (ie, new_len < old_len) we
3897 * need to delete some old_len. We do this at the start, by
3898 * repeatedly deleting line "lo".
3899 */
3900 if (!PyErr_Occurred())
3901 {
3902 for (i = 0; i < old_len - new_len; ++i)
3903 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3904 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003905 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003906 break;
3907 }
3908 extra -= i;
3909 }
3910
3911 /* For as long as possible, replace the existing old_len with the
3912 * new old_len. This is a more efficient operation, as it requires
3913 * less memory allocation and freeing.
3914 */
3915 if (!PyErr_Occurred())
3916 {
3917 for (i = 0; i < old_len && i < new_len; ++i)
3918 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3919 == FAIL)
3920 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003921 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003922 break;
3923 }
3924 }
3925 else
3926 i = 0;
3927
3928 /* Now we may need to insert the remaining new old_len. If we do, we
3929 * must free the strings as we finish with them (we can't pass the
3930 * responsibility to vim in this case).
3931 */
3932 if (!PyErr_Occurred())
3933 {
3934 while (i < new_len)
3935 {
3936 if (ml_append((linenr_T)(lo + i - 1),
3937 (char_u *)array[i], 0, FALSE) == FAIL)
3938 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003939 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003940 break;
3941 }
3942 vim_free(array[i]);
3943 ++i;
3944 ++extra;
3945 }
3946 }
3947
3948 /* Free any left-over old_len, as a result of an error */
3949 while (i < new_len)
3950 {
3951 vim_free(array[i]);
3952 ++i;
3953 }
3954
3955 /* Free the array of old_len. All of its contents have now
3956 * been dealt with (either freed, or the responsibility passed
3957 * to vim.
3958 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003959 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003960
3961 /* Adjust marks. Invalidate any which lie in the
3962 * changed range, and move any in the remainder of the buffer.
3963 */
3964 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3965 (long)MAXLNUM, (long)extra);
3966 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3967
Bram Moolenaar105bc352013-05-17 16:03:57 +02003968 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003969 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3970
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003971 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003972 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003973
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003974 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003975 return FAIL;
3976
3977 if (len_change)
3978 *len_change = new_len - old_len;
3979
3980 return OK;
3981 }
3982 else
3983 {
3984 PyErr_BadArgument();
3985 return FAIL;
3986 }
3987}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003989/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003990 * The line number is in Vim format (1-based). The lines to be inserted are
3991 * given as a Python list of string objects or as a single string. The lines
3992 * to be added are checked for validity and correct format. Errors are
3993 * returned as a value of FAIL. The return value is OK on success.
3994 * If OK is returned and len_change is not NULL, *len_change
3995 * is set to the change in the buffer length.
3996 */
3997 static int
3998InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3999{
4000 /* First of all, we check the type of the supplied Python object.
4001 * It must be a string or a list, or the call is in error.
4002 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004003 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004004 {
4005 char *str = StringToLine(lines);
4006 buf_T *savebuf;
4007
4008 if (str == NULL)
4009 return FAIL;
4010
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004011 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004012 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004013 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004014
4015 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004016 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004017 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004018 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004019 else
4020 appended_lines_mark((linenr_T)n, 1L);
4021
4022 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004023 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004024 update_screen(VALID);
4025
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004026 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004027 return FAIL;
4028
4029 if (len_change)
4030 *len_change = 1;
4031
4032 return OK;
4033 }
4034 else if (PyList_Check(lines))
4035 {
4036 PyInt i;
4037 PyInt size = PyList_Size(lines);
4038 char **array;
4039 buf_T *savebuf;
4040
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004041 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042 if (array == NULL)
4043 {
4044 PyErr_NoMemory();
4045 return FAIL;
4046 }
4047
4048 for (i = 0; i < size; ++i)
4049 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004050 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004052 if (!(line = PyList_GetItem(lines, i)) ||
4053 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 {
4055 while (i)
4056 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004057 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004058 return FAIL;
4059 }
4060 }
4061
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004063 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004064 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065
4066 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004067 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 else
4069 {
4070 for (i = 0; i < size; ++i)
4071 {
4072 if (ml_append((linenr_T)(n + i),
4073 (char_u *)array[i], 0, FALSE) == FAIL)
4074 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004075 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076
4077 /* Free the rest of the lines */
4078 while (i < size)
4079 vim_free(array[i++]);
4080
4081 break;
4082 }
4083 vim_free(array[i]);
4084 }
4085 if (i > 0)
4086 appended_lines_mark((linenr_T)n, (long)i);
4087 }
4088
4089 /* Free the array of lines. All of its contents have now
4090 * been freed.
4091 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004092 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093
Bram Moolenaar105bc352013-05-17 16:03:57 +02004094 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 update_screen(VALID);
4096
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004097 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 return FAIL;
4099
4100 if (len_change)
4101 *len_change = size;
4102
4103 return OK;
4104 }
4105 else
4106 {
4107 PyErr_BadArgument();
4108 return FAIL;
4109 }
4110}
4111
4112/*
4113 * Common routines for buffers and line ranges
4114 * -------------------------------------------
4115 */
4116
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004117typedef struct
4118{
4119 PyObject_HEAD
4120 buf_T *buf;
4121} BufferObject;
4122
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004123 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004124CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004126 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004128 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129 return -1;
4130 }
4131
4132 return 0;
4133}
4134
4135 static PyObject *
4136RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4137{
4138 if (CheckBuffer(self))
4139 return NULL;
4140
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004141 if (end == -1)
4142 end = self->buf->b_ml.ml_line_count;
4143
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004144 if (n < 0)
4145 n += end - start + 1;
4146
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147 if (n < 0 || n > end - start)
4148 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004149 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150 return NULL;
4151 }
4152
4153 return GetBufferLine(self->buf, n+start);
4154}
4155
4156 static PyObject *
4157RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4158{
4159 PyInt size;
4160
4161 if (CheckBuffer(self))
4162 return NULL;
4163
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004164 if (end == -1)
4165 end = self->buf->b_ml.ml_line_count;
4166
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167 size = end - start + 1;
4168
4169 if (lo < 0)
4170 lo = 0;
4171 else if (lo > size)
4172 lo = size;
4173 if (hi < 0)
4174 hi = 0;
4175 if (hi < lo)
4176 hi = lo;
4177 else if (hi > size)
4178 hi = size;
4179
4180 return GetBufferLineList(self->buf, lo+start, hi+start);
4181}
4182
4183 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004184RBAsItem(
4185 BufferObject *self,
4186 PyInt n,
4187 PyObject *valObject,
4188 PyInt start,
4189 PyInt end,
4190 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004191{
4192 PyInt len_change;
4193
4194 if (CheckBuffer(self))
4195 return -1;
4196
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004197 if (end == -1)
4198 end = self->buf->b_ml.ml_line_count;
4199
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004200 if (n < 0)
4201 n += end - start + 1;
4202
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004203 if (n < 0 || n > end - start)
4204 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004205 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004206 return -1;
4207 }
4208
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004209 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004210 return -1;
4211
4212 if (new_end)
4213 *new_end = end + len_change;
4214
4215 return 0;
4216}
4217
Bram Moolenaar19e60942011-06-19 00:27:51 +02004218 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004219RBAsSlice(
4220 BufferObject *self,
4221 PyInt lo,
4222 PyInt hi,
4223 PyObject *valObject,
4224 PyInt start,
4225 PyInt end,
4226 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004227{
4228 PyInt size;
4229 PyInt len_change;
4230
4231 /* Self must be a valid buffer */
4232 if (CheckBuffer(self))
4233 return -1;
4234
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004235 if (end == -1)
4236 end = self->buf->b_ml.ml_line_count;
4237
Bram Moolenaar19e60942011-06-19 00:27:51 +02004238 /* Sort out the slice range */
4239 size = end - start + 1;
4240
4241 if (lo < 0)
4242 lo = 0;
4243 else if (lo > size)
4244 lo = size;
4245 if (hi < 0)
4246 hi = 0;
4247 if (hi < lo)
4248 hi = lo;
4249 else if (hi > size)
4250 hi = size;
4251
4252 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004253 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004254 return -1;
4255
4256 if (new_end)
4257 *new_end = end + len_change;
4258
4259 return 0;
4260}
4261
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004262
4263 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004264RBAppend(
4265 BufferObject *self,
4266 PyObject *args,
4267 PyInt start,
4268 PyInt end,
4269 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270{
4271 PyObject *lines;
4272 PyInt len_change;
4273 PyInt max;
4274 PyInt n;
4275
4276 if (CheckBuffer(self))
4277 return NULL;
4278
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004279 if (end == -1)
4280 end = self->buf->b_ml.ml_line_count;
4281
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004282 max = n = end - start + 1;
4283
4284 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4285 return NULL;
4286
4287 if (n < 0 || n > max)
4288 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004289 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004290 return NULL;
4291 }
4292
4293 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4294 return NULL;
4295
4296 if (new_end)
4297 *new_end = end + len_change;
4298
4299 Py_INCREF(Py_None);
4300 return Py_None;
4301}
4302
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004303/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004304 */
4305
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004306static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004307static PySequenceMethods RangeAsSeq;
4308static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004309
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004310typedef struct
4311{
4312 PyObject_HEAD
4313 BufferObject *buf;
4314 PyInt start;
4315 PyInt end;
4316} RangeObject;
4317
4318 static PyObject *
4319RangeNew(buf_T *buf, PyInt start, PyInt end)
4320{
4321 BufferObject *bufr;
4322 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004323 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004324 if (self == NULL)
4325 return NULL;
4326
4327 bufr = (BufferObject *)BufferNew(buf);
4328 if (bufr == NULL)
4329 {
4330 Py_DECREF(self);
4331 return NULL;
4332 }
4333 Py_INCREF(bufr);
4334
4335 self->buf = bufr;
4336 self->start = start;
4337 self->end = end;
4338
4339 return (PyObject *)(self);
4340}
4341
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004342 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004343RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004344{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004345 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004346 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004347 PyObject_GC_Del((void *)(self));
4348}
4349
4350 static int
4351RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4352{
4353 Py_VISIT(((PyObject *)(self->buf)));
4354 return 0;
4355}
4356
4357 static int
4358RangeClear(RangeObject *self)
4359{
4360 Py_CLEAR(self->buf);
4361 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004362}
4363
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004364 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004365RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004366{
4367 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004368 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004369 return -1; /* ??? */
4370
Bram Moolenaard6e39182013-05-21 18:30:34 +02004371 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004372}
4373
4374 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004375RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004376{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004377 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004378}
4379
4380 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004381RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004382{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004383 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004384}
4385
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004386static char *RangeAttrs[] = {
4387 "start", "end",
4388 NULL
4389};
4390
4391 static PyObject *
4392RangeDir(PyObject *self)
4393{
4394 return ObjectDir(self, RangeAttrs);
4395}
4396
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004397 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004398RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004399{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004400 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004401}
4402
4403 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004404RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004405{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004406 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004407 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4408 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004409 else
4410 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004411 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004412
4413 if (name == NULL)
4414 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004415
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004416 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004417 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004418 }
4419}
4420
4421static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004422 /* name, function, calling, documentation */
4423 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004424 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4425 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004426};
4427
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004428static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004429static PySequenceMethods BufferAsSeq;
4430static PyMappingMethods BufferAsMapping;
4431
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004432 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004433BufferNew(buf_T *buf)
4434{
4435 /* We need to handle deletion of buffers underneath us.
4436 * If we add a "b_python*_ref" field to the buf_T structure,
4437 * then we can get at it in buf_freeall() in vim. We then
4438 * need to create only ONE Python object per buffer - if
4439 * we try to create a second, just INCREF the existing one
4440 * and return it. The (single) Python object referring to
4441 * the buffer is stored in "b_python*_ref".
4442 * Question: what to do on a buf_freeall(). We'll probably
4443 * have to either delete the Python object (DECREF it to
4444 * zero - a bad idea, as it leaves dangling refs!) or
4445 * set the buf_T * value to an invalid value (-1?), which
4446 * means we need checks in all access functions... Bah.
4447 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004448 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004449 * b_python_ref and b_python3_ref fields respectively.
4450 */
4451
4452 BufferObject *self;
4453
4454 if (BUF_PYTHON_REF(buf) != NULL)
4455 {
4456 self = BUF_PYTHON_REF(buf);
4457 Py_INCREF(self);
4458 }
4459 else
4460 {
4461 self = PyObject_NEW(BufferObject, &BufferType);
4462 if (self == NULL)
4463 return NULL;
4464 self->buf = buf;
4465 BUF_PYTHON_REF(buf) = self;
4466 }
4467
4468 return (PyObject *)(self);
4469}
4470
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004471 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004472BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004473{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004474 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4475 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004476
4477 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004478}
4479
Bram Moolenaar971db462013-05-12 18:44:48 +02004480 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004481BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004482{
4483 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004484 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004485 return -1; /* ??? */
4486
Bram Moolenaard6e39182013-05-21 18:30:34 +02004487 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004488}
4489
4490 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004491BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004492{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004493 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004494}
4495
4496 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004497BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004498{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004499 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004500}
4501
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004502static char *BufferAttrs[] = {
4503 "name", "number", "vars", "options", "valid",
4504 NULL
4505};
4506
4507 static PyObject *
4508BufferDir(PyObject *self)
4509{
4510 return ObjectDir(self, BufferAttrs);
4511}
4512
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004513 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004514BufferAttrValid(BufferObject *self, char *name)
4515{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004516 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004517
4518 if (strcmp(name, "valid") != 0)
4519 return NULL;
4520
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004521 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4522 Py_INCREF(ret);
4523 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004524}
4525
4526 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004527BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004528{
4529 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004530 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004531 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004532 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004533 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004534 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004535 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004536 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004537 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4538 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004539 else if (strcmp(name, "__members__") == 0)
4540 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004541 else
4542 return NULL;
4543}
4544
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004545 static int
4546BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4547{
4548 if (CheckBuffer(self))
4549 return -1;
4550
4551 if (strcmp(name, "name") == 0)
4552 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004553 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004554 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004555 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004556 PyObject *todecref;
4557
4558 if (!(val = StringToChars(valObject, &todecref)))
4559 return -1;
4560
4561 VimTryStart();
4562 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4563 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004564 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004565 aucmd_restbuf(&aco);
4566 Py_XDECREF(todecref);
4567 if (VimTryEnd())
4568 return -1;
4569
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004570 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004571 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004572 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004573 return -1;
4574 }
4575 return 0;
4576 }
4577 else
4578 {
4579 PyErr_SetString(PyExc_AttributeError, name);
4580 return -1;
4581 }
4582}
4583
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004584 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004585BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004586{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004587 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004588}
4589
4590 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004591BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004592{
4593 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004594 char_u *pmark;
4595 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004596 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004597 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004598
Bram Moolenaard6e39182013-05-21 18:30:34 +02004599 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004600 return NULL;
4601
Bram Moolenaar389a1792013-06-23 13:00:44 +02004602 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004603 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004604
Bram Moolenaar389a1792013-06-23 13:00:44 +02004605 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004606 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004607 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004608 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004609 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004610 return NULL;
4611 }
4612
4613 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004614
4615 Py_XDECREF(todecref);
4616
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004617 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004618 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004619 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004620 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004621 if (VimTryEnd())
4622 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004623
4624 if (posp == NULL)
4625 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004626 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004627 return NULL;
4628 }
4629
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004630 if (posp->lnum <= 0)
4631 {
4632 /* Or raise an error? */
4633 Py_INCREF(Py_None);
4634 return Py_None;
4635 }
4636
4637 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4638}
4639
4640 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004641BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004642{
4643 PyInt start;
4644 PyInt end;
4645
Bram Moolenaard6e39182013-05-21 18:30:34 +02004646 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004647 return NULL;
4648
4649 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4650 return NULL;
4651
Bram Moolenaard6e39182013-05-21 18:30:34 +02004652 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004653}
4654
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004655 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004656BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004657{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004658 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004659 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004660 else
4661 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004662 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004663
4664 if (name == NULL)
4665 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004666
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004667 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004668 }
4669}
4670
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004671static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004672 /* name, function, calling, documentation */
4673 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004674 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004675 {"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 +02004676 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4677 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004678};
4679
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004680/*
4681 * Buffer list object - Implementation
4682 */
4683
4684static PyTypeObject BufMapType;
4685
4686typedef struct
4687{
4688 PyObject_HEAD
4689} BufMapObject;
4690
4691 static PyInt
4692BufMapLength(PyObject *self UNUSED)
4693{
4694 buf_T *b = firstbuf;
4695 PyInt n = 0;
4696
4697 while (b)
4698 {
4699 ++n;
4700 b = b->b_next;
4701 }
4702
4703 return n;
4704}
4705
4706 static PyObject *
4707BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4708{
4709 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004710 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004711
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004712 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004713 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004714
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004715 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004716
4717 if (b)
4718 return BufferNew(b);
4719 else
4720 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004721 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004722 return NULL;
4723 }
4724}
4725
4726 static void
4727BufMapIterDestruct(PyObject *buffer)
4728{
4729 /* Iteration was stopped before all buffers were processed */
4730 if (buffer)
4731 {
4732 Py_DECREF(buffer);
4733 }
4734}
4735
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004736 static int
4737BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4738{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004739 if (buffer)
4740 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004741 return 0;
4742}
4743
4744 static int
4745BufMapIterClear(PyObject **buffer)
4746{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004747 if (*buffer)
4748 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004749 return 0;
4750}
4751
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004752 static PyObject *
4753BufMapIterNext(PyObject **buffer)
4754{
4755 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004756 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004757
4758 if (!*buffer)
4759 return NULL;
4760
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004761 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004762
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004763 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004764 {
4765 *buffer = NULL;
4766 return NULL;
4767 }
4768
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004769 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004770 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004771 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004772 return NULL;
4773 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004774 /* Do not increment reference: we no longer hold it (decref), but whoever
4775 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004776 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004777}
4778
4779 static PyObject *
4780BufMapIter(PyObject *self UNUSED)
4781{
4782 PyObject *buffer;
4783
4784 buffer = BufferNew(firstbuf);
4785 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004786 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4787 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004788}
4789
4790static PyMappingMethods BufMapAsMapping = {
4791 (lenfunc) BufMapLength,
4792 (binaryfunc) BufMapItem,
4793 (objobjargproc) 0,
4794};
4795
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004796/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004797 */
4798
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004799static char *CurrentAttrs[] = {
4800 "buffer", "window", "line", "range", "tabpage",
4801 NULL
4802};
4803
4804 static PyObject *
4805CurrentDir(PyObject *self)
4806{
4807 return ObjectDir(self, CurrentAttrs);
4808}
4809
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004810 static PyObject *
4811CurrentGetattr(PyObject *self UNUSED, char *name)
4812{
4813 if (strcmp(name, "buffer") == 0)
4814 return (PyObject *)BufferNew(curbuf);
4815 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004816 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004817 else if (strcmp(name, "tabpage") == 0)
4818 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004819 else if (strcmp(name, "line") == 0)
4820 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4821 else if (strcmp(name, "range") == 0)
4822 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004823 else if (strcmp(name, "__members__") == 0)
4824 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004825 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004826#if PY_MAJOR_VERSION < 3
4827 return Py_FindMethod(WindowMethods, self, name);
4828#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004829 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004830#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004831}
4832
4833 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004834CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004835{
4836 if (strcmp(name, "line") == 0)
4837 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004838 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4839 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004840 return -1;
4841
4842 return 0;
4843 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004844 else if (strcmp(name, "buffer") == 0)
4845 {
4846 int count;
4847
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004848 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004849 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004850 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004851 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004852 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004853 return -1;
4854 }
4855
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004856 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004857 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004858 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004859
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004860 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004861 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4862 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004863 if (VimTryEnd())
4864 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004865 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004866 return -1;
4867 }
4868
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004869 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004870 }
4871 else if (strcmp(name, "window") == 0)
4872 {
4873 int count;
4874
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004875 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004876 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004877 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004878 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004879 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004880 return -1;
4881 }
4882
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004883 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004884 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004885 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004886
4887 if (!count)
4888 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004889 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004890 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004891 return -1;
4892 }
4893
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004894 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004895 win_goto(((WindowObject *)(valObject))->win);
4896 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004897 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004898 if (VimTryEnd())
4899 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004900 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004901 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004902 return -1;
4903 }
4904
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004905 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004906 }
4907 else if (strcmp(name, "tabpage") == 0)
4908 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004909 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004910 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004911 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004912 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004913 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004914 return -1;
4915 }
4916
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004917 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004918 return -1;
4919
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004920 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004921 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4922 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004923 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004924 if (VimTryEnd())
4925 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004926 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004927 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004928 return -1;
4929 }
4930
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004931 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004932 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004933 else
4934 {
4935 PyErr_SetString(PyExc_AttributeError, name);
4936 return -1;
4937 }
4938}
4939
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004940static struct PyMethodDef CurrentMethods[] = {
4941 /* name, function, calling, documentation */
4942 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4943 { NULL, NULL, 0, NULL}
4944};
4945
Bram Moolenaardb913952012-06-29 12:54:53 +02004946 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004947init_range_cmd(exarg_T *eap)
4948{
4949 RangeStart = eap->line1;
4950 RangeEnd = eap->line2;
4951}
4952
4953 static void
4954init_range_eval(typval_T *rettv UNUSED)
4955{
4956 RangeStart = (PyInt) curwin->w_cursor.lnum;
4957 RangeEnd = RangeStart;
4958}
4959
4960 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004961run_cmd(const char *cmd, void *arg UNUSED
4962#ifdef PY_CAN_RECURSE
4963 , PyGILState_STATE *pygilstate UNUSED
4964#endif
4965 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004966{
Bram Moolenaar41009372013-07-01 22:03:04 +02004967 PyObject *run_ret;
4968 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
4969 if (run_ret != NULL)
4970 {
4971 Py_DECREF(run_ret);
4972 }
4973 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
4974 {
4975 EMSG2(_(e_py_systemexit), "python");
4976 PyErr_Clear();
4977 }
4978 else
4979 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004980}
4981
4982static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4983static int code_hdr_len = 30;
4984
4985 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004986run_do(const char *cmd, void *arg UNUSED
4987#ifdef PY_CAN_RECURSE
4988 , PyGILState_STATE *pygilstate
4989#endif
4990 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004991{
4992 PyInt lnum;
4993 size_t len;
4994 char *code;
4995 int status;
4996 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02004997 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004998
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004999 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005000 {
5001 EMSG(_("cannot save undo information"));
5002 return;
5003 }
5004
5005 len = code_hdr_len + STRLEN(cmd);
5006 code = PyMem_New(char, len + 1);
5007 memcpy(code, code_hdr, code_hdr_len);
5008 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005009 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5010 status = -1;
5011 if (run_ret != NULL)
5012 {
5013 status = 0;
5014 Py_DECREF(run_ret);
5015 }
5016 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5017 {
5018 PyMem_Free(code);
5019 EMSG2(_(e_py_systemexit), "python");
5020 PyErr_Clear();
5021 return;
5022 }
5023 else
5024 PyErr_PrintEx(1);
5025
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005026 PyMem_Free(code);
5027
5028 if (status)
5029 {
5030 EMSG(_("failed to run the code"));
5031 return;
5032 }
5033
5034 status = 0;
5035 pymain = PyImport_AddModule("__main__");
5036 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005037#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005038 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005039#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005040
5041 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5042 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005043 PyObject *line;
5044 PyObject *linenr;
5045 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005046
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005047#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005048 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005049#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005050 if (!(line = GetBufferLine(curbuf, lnum)))
5051 goto err;
5052 if (!(linenr = PyInt_FromLong((long) lnum)))
5053 {
5054 Py_DECREF(line);
5055 goto err;
5056 }
5057 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5058 Py_DECREF(line);
5059 Py_DECREF(linenr);
5060 if (!ret)
5061 goto err;
5062
5063 if (ret != Py_None)
5064 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5065 goto err;
5066
5067 Py_XDECREF(ret);
5068 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005069#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005070 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005071#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005072 }
5073 goto out;
5074err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005075#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005076 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005077#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005078 PyErr_PrintEx(0);
5079 PythonIO_Flush();
5080 status = 1;
5081out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005082#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005083 if (!status)
5084 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005085#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005086 Py_DECREF(pyfunc);
5087 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5088 if (status)
5089 return;
5090 check_cursor();
5091 update_curbuf(NOT_VALID);
5092}
5093
5094 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005095run_eval(const char *cmd, typval_T *rettv
5096#ifdef PY_CAN_RECURSE
5097 , PyGILState_STATE *pygilstate UNUSED
5098#endif
5099 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005100{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005101 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005102
Bram Moolenaar41009372013-07-01 22:03:04 +02005103 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005104 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005105 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005106 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005107 {
5108 EMSG2(_(e_py_systemexit), "python");
5109 PyErr_Clear();
5110 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005111 else
5112 {
5113 if (PyErr_Occurred() && !msg_silent)
5114 PyErr_PrintEx(0);
5115 EMSG(_("E858: Eval did not return a valid python object"));
5116 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005117 }
5118 else
5119 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005120 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005121 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005122 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005123 }
5124 PyErr_Clear();
5125}
5126
5127 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005128set_ref_in_py(const int copyID)
5129{
5130 pylinkedlist_T *cur;
5131 dict_T *dd;
5132 list_T *ll;
5133
5134 if (lastdict != NULL)
5135 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5136 {
5137 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5138 if (dd->dv_copyID != copyID)
5139 {
5140 dd->dv_copyID = copyID;
5141 set_ref_in_ht(&dd->dv_hashtab, copyID);
5142 }
5143 }
5144
5145 if (lastlist != NULL)
5146 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5147 {
5148 ll = ((ListObject *) (cur->pll_obj))->list;
5149 if (ll->lv_copyID != copyID)
5150 {
5151 ll->lv_copyID = copyID;
5152 set_ref_in_list(ll, copyID);
5153 }
5154 }
5155}
5156
5157 static int
5158set_string_copy(char_u *str, typval_T *tv)
5159{
5160 tv->vval.v_string = vim_strsave(str);
5161 if (tv->vval.v_string == NULL)
5162 {
5163 PyErr_NoMemory();
5164 return -1;
5165 }
5166 return 0;
5167}
5168
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005170pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005171{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005172 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005173 char_u *key;
5174 dictitem_T *di;
5175 PyObject *keyObject;
5176 PyObject *valObject;
5177 Py_ssize_t iter = 0;
5178
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005179 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005180 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005181
5182 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005183 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005184
5185 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5186 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005187 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005188
Bram Moolenaara03e6312013-05-29 22:49:26 +02005189 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005190 {
5191 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005192 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005193 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005194
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005195 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005196 {
5197 dict_unref(dict);
5198 return -1;
5199 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005200
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005201 if (*key == NUL)
5202 {
5203 dict_unref(dict);
5204 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005205 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005206 return -1;
5207 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005208
5209 di = dictitem_alloc(key);
5210
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005211 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005212
5213 if (di == NULL)
5214 {
5215 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005216 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005217 return -1;
5218 }
5219 di->di_tv.v_lock = 0;
5220
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005221 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005222 {
5223 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005224 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005225 return -1;
5226 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005227
5228 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005229 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005230 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005231 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005233 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005234 return -1;
5235 }
5236 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005237
5238 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005239 return 0;
5240}
5241
5242 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005243pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005244{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005245 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005246 char_u *key;
5247 dictitem_T *di;
5248 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005249 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005250 PyObject *keyObject;
5251 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005252
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005253 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005254 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005255
5256 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005257 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005259 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005260 {
5261 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005262 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005263 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005264
5265 if (!(iterator = PyObject_GetIter(list)))
5266 {
5267 dict_unref(dict);
5268 Py_DECREF(list);
5269 return -1;
5270 }
5271 Py_DECREF(list);
5272
5273 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005274 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005275 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005276
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005277 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005278 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005279 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005280 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005281 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005282 return -1;
5283 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005284
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005285 if (*key == NUL)
5286 {
5287 Py_DECREF(keyObject);
5288 Py_DECREF(iterator);
5289 Py_XDECREF(todecref);
5290 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005291 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005292 return -1;
5293 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005294
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005295 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005296 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005297 Py_DECREF(keyObject);
5298 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005299 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005300 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005301 return -1;
5302 }
5303
5304 di = dictitem_alloc(key);
5305
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005306 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005307 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005308
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005309 if (di == NULL)
5310 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005311 Py_DECREF(iterator);
5312 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005313 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005314 PyErr_NoMemory();
5315 return -1;
5316 }
5317 di->di_tv.v_lock = 0;
5318
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005319 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005320 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005321 Py_DECREF(iterator);
5322 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005323 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005324 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005325 return -1;
5326 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005327
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005328 Py_DECREF(valObject);
5329
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005330 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005331 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005332 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005333 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005334 dictitem_free(di);
5335 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005336 return -1;
5337 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005338 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005339 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005340 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005341 return 0;
5342}
5343
5344 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005345pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005346{
5347 list_T *l;
5348
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005349 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005350 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005351
5352 tv->v_type = VAR_LIST;
5353 tv->vval.v_list = l;
5354
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005355 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005356 {
5357 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005358 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005359 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005360
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005361 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005362 return 0;
5363}
5364
Bram Moolenaardb913952012-06-29 12:54:53 +02005365typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5366
5367 static int
5368convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005369 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005370{
5371 PyObject *capsule;
5372 char hexBuf[sizeof(void *) * 2 + 3];
5373
5374 sprintf(hexBuf, "%p", obj);
5375
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005376# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005377 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005378# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005379 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005380# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005381 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005382 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005383# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005384 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005385# else
5386 capsule = PyCObject_FromVoidPtr(tv, NULL);
5387# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005388 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5389 {
5390 Py_DECREF(capsule);
5391 tv->v_type = VAR_UNKNOWN;
5392 return -1;
5393 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005394
5395 Py_DECREF(capsule);
5396
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005397 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005398 {
5399 tv->v_type = VAR_UNKNOWN;
5400 return -1;
5401 }
5402 /* As we are not using copy_tv which increments reference count we must
5403 * do it ourself. */
5404 switch(tv->v_type)
5405 {
5406 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5407 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5408 }
5409 }
5410 else
5411 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005412 typval_T *v;
5413
5414# ifdef PY_USE_CAPSULE
5415 v = PyCapsule_GetPointer(capsule, NULL);
5416# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005417 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005418# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005419 copy_tv(v, tv);
5420 }
5421 return 0;
5422}
5423
5424 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005425ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5426{
5427 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005428 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005429
5430 if (!(lookup_dict = PyDict_New()))
5431 return -1;
5432
5433 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5434 {
5435 tv->v_type = VAR_DICT;
5436 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5437 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005438 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005439 }
5440 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005441 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005442 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005443 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005444 else
5445 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005446 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005447 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005448 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005449 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005450 }
5451 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005452 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005453}
5454
5455 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005456ConvertFromPyObject(PyObject *obj, typval_T *tv)
5457{
5458 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005459 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005460
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005461 if (!(lookup_dict = PyDict_New()))
5462 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005463 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005464 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005465 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005466}
5467
5468 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005469_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005470{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005471 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005472 {
5473 tv->v_type = VAR_DICT;
5474 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5475 ++tv->vval.v_dict->dv_refcount;
5476 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005477 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005478 {
5479 tv->v_type = VAR_LIST;
5480 tv->vval.v_list = (((ListObject *)(obj))->list);
5481 ++tv->vval.v_list->lv_refcount;
5482 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005483 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005484 {
5485 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5486 return -1;
5487
5488 tv->v_type = VAR_FUNC;
5489 func_ref(tv->vval.v_string);
5490 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005491 else if (PyBytes_Check(obj))
5492 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005493 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005494
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005495 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005496 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005497 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005498 return -1;
5499
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005500 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005501 return -1;
5502
5503 tv->v_type = VAR_STRING;
5504 }
5505 else if (PyUnicode_Check(obj))
5506 {
5507 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005508 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005509
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005510 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005511 if (bytes == NULL)
5512 return -1;
5513
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005514 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005515 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005516 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005517 return -1;
5518
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005519 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005520 {
5521 Py_XDECREF(bytes);
5522 return -1;
5523 }
5524 Py_XDECREF(bytes);
5525
5526 tv->v_type = VAR_STRING;
5527 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005528#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005529 else if (PyInt_Check(obj))
5530 {
5531 tv->v_type = VAR_NUMBER;
5532 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005533 if (PyErr_Occurred())
5534 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005535 }
5536#endif
5537 else if (PyLong_Check(obj))
5538 {
5539 tv->v_type = VAR_NUMBER;
5540 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005541 if (PyErr_Occurred())
5542 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005543 }
5544 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005545 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005546#ifdef FEAT_FLOAT
5547 else if (PyFloat_Check(obj))
5548 {
5549 tv->v_type = VAR_FLOAT;
5550 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5551 }
5552#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005553 else if (PyObject_HasAttrString(obj, "keys"))
5554 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005555 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005556 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005557 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005558 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005559 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005560 else if (PyNumber_Check(obj))
5561 {
5562 PyObject *num;
5563
5564 if (!(num = PyNumber_Long(obj)))
5565 return -1;
5566
5567 tv->v_type = VAR_NUMBER;
5568 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5569
5570 Py_DECREF(num);
5571 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005572 else
5573 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005574 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005575 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005576 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005577 return -1;
5578 }
5579 return 0;
5580}
5581
5582 static PyObject *
5583ConvertToPyObject(typval_T *tv)
5584{
5585 if (tv == NULL)
5586 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005587 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005588 return NULL;
5589 }
5590 switch (tv->v_type)
5591 {
5592 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005593 return PyBytes_FromString(tv->vval.v_string == NULL
5594 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005595 case VAR_NUMBER:
5596 return PyLong_FromLong((long) tv->vval.v_number);
5597#ifdef FEAT_FLOAT
5598 case VAR_FLOAT:
5599 return PyFloat_FromDouble((double) tv->vval.v_float);
5600#endif
5601 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005602 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005603 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005604 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005605 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005606 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005607 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005608 case VAR_UNKNOWN:
5609 Py_INCREF(Py_None);
5610 return Py_None;
5611 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005612 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005613 return NULL;
5614 }
5615}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005616
5617typedef struct
5618{
5619 PyObject_HEAD
5620} CurrentObject;
5621static PyTypeObject CurrentType;
5622
5623 static void
5624init_structs(void)
5625{
5626 vim_memset(&OutputType, 0, sizeof(OutputType));
5627 OutputType.tp_name = "vim.message";
5628 OutputType.tp_basicsize = sizeof(OutputObject);
5629 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5630 OutputType.tp_doc = "vim message object";
5631 OutputType.tp_methods = OutputMethods;
5632#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005633 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5634 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005635 OutputType.tp_alloc = call_PyType_GenericAlloc;
5636 OutputType.tp_new = call_PyType_GenericNew;
5637 OutputType.tp_free = call_PyObject_Free;
5638#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005639 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5640 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005641#endif
5642
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005643 vim_memset(&IterType, 0, sizeof(IterType));
5644 IterType.tp_name = "vim.iter";
5645 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005646 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005647 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005648 IterType.tp_iter = (getiterfunc)IterIter;
5649 IterType.tp_iternext = (iternextfunc)IterNext;
5650 IterType.tp_dealloc = (destructor)IterDestructor;
5651 IterType.tp_traverse = (traverseproc)IterTraverse;
5652 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005653
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005654 vim_memset(&BufferType, 0, sizeof(BufferType));
5655 BufferType.tp_name = "vim.buffer";
5656 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005657 BufferType.tp_dealloc = (destructor)BufferDestructor;
5658 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005659 BufferType.tp_as_sequence = &BufferAsSeq;
5660 BufferType.tp_as_mapping = &BufferAsMapping;
5661 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5662 BufferType.tp_doc = "vim buffer object";
5663 BufferType.tp_methods = BufferMethods;
5664#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005665 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005666 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005667 BufferType.tp_alloc = call_PyType_GenericAlloc;
5668 BufferType.tp_new = call_PyType_GenericNew;
5669 BufferType.tp_free = call_PyObject_Free;
5670#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005671 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005672 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005673#endif
5674
5675 vim_memset(&WindowType, 0, sizeof(WindowType));
5676 WindowType.tp_name = "vim.window";
5677 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005678 WindowType.tp_dealloc = (destructor)WindowDestructor;
5679 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005680 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005681 WindowType.tp_doc = "vim Window object";
5682 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005683 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5684 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005685#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005686 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5687 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005688 WindowType.tp_alloc = call_PyType_GenericAlloc;
5689 WindowType.tp_new = call_PyType_GenericNew;
5690 WindowType.tp_free = call_PyObject_Free;
5691#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005692 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5693 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005694#endif
5695
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005696 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5697 TabPageType.tp_name = "vim.tabpage";
5698 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005699 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5700 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005701 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5702 TabPageType.tp_doc = "vim tab page object";
5703 TabPageType.tp_methods = TabPageMethods;
5704#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005705 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005706 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5707 TabPageType.tp_new = call_PyType_GenericNew;
5708 TabPageType.tp_free = call_PyObject_Free;
5709#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005710 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005711#endif
5712
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005713 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5714 BufMapType.tp_name = "vim.bufferlist";
5715 BufMapType.tp_basicsize = sizeof(BufMapObject);
5716 BufMapType.tp_as_mapping = &BufMapAsMapping;
5717 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005718 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005719 BufferType.tp_doc = "vim buffer list";
5720
5721 vim_memset(&WinListType, 0, sizeof(WinListType));
5722 WinListType.tp_name = "vim.windowlist";
5723 WinListType.tp_basicsize = sizeof(WinListType);
5724 WinListType.tp_as_sequence = &WinListAsSeq;
5725 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5726 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005727 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005728
5729 vim_memset(&TabListType, 0, sizeof(TabListType));
5730 TabListType.tp_name = "vim.tabpagelist";
5731 TabListType.tp_basicsize = sizeof(TabListType);
5732 TabListType.tp_as_sequence = &TabListAsSeq;
5733 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5734 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005735
5736 vim_memset(&RangeType, 0, sizeof(RangeType));
5737 RangeType.tp_name = "vim.range";
5738 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005739 RangeType.tp_dealloc = (destructor)RangeDestructor;
5740 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005741 RangeType.tp_as_sequence = &RangeAsSeq;
5742 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005743 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005744 RangeType.tp_doc = "vim Range object";
5745 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005746 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5747 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005748#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005749 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005750 RangeType.tp_alloc = call_PyType_GenericAlloc;
5751 RangeType.tp_new = call_PyType_GenericNew;
5752 RangeType.tp_free = call_PyObject_Free;
5753#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005754 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005755#endif
5756
5757 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5758 CurrentType.tp_name = "vim.currentdata";
5759 CurrentType.tp_basicsize = sizeof(CurrentObject);
5760 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5761 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005762 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005763#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005764 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5765 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005766#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005767 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5768 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005769#endif
5770
5771 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5772 DictionaryType.tp_name = "vim.dictionary";
5773 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005774 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005775 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005776 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005777 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005778 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5779 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005780 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5781 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5782 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005783#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005784 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5785 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005786#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005787 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5788 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005789#endif
5790
5791 vim_memset(&ListType, 0, sizeof(ListType));
5792 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005793 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005794 ListType.tp_basicsize = sizeof(ListObject);
5795 ListType.tp_as_sequence = &ListAsSeq;
5796 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005797 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005798 ListType.tp_doc = "list pushing modifications to vim structure";
5799 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005800 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005801 ListType.tp_new = (newfunc)ListConstructor;
5802 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005803#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005804 ListType.tp_getattro = (getattrofunc)ListGetattro;
5805 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005806#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005807 ListType.tp_getattr = (getattrfunc)ListGetattr;
5808 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005809#endif
5810
5811 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005812 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005813 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005814 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5815 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005816 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005817 FunctionType.tp_doc = "object that calls vim function";
5818 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005819 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005820 FunctionType.tp_new = (newfunc)FunctionConstructor;
5821 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005822#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005823 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005824#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005825 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005826#endif
5827
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005828 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5829 OptionsType.tp_name = "vim.options";
5830 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005831 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005832 OptionsType.tp_doc = "object for manipulating options";
5833 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005834 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5835 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5836 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005837
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005838 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5839 LoaderType.tp_name = "vim.Loader";
5840 LoaderType.tp_basicsize = sizeof(LoaderObject);
5841 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5842 LoaderType.tp_doc = "vim message object";
5843 LoaderType.tp_methods = LoaderMethods;
5844 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5845
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005846#if PY_MAJOR_VERSION >= 3
5847 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5848 vimmodule.m_name = "vim";
5849 vimmodule.m_doc = "Vim Python interface\n";
5850 vimmodule.m_size = -1;
5851 vimmodule.m_methods = VimMethods;
5852#endif
5853}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005854
5855#define PYTYPE_READY(type) \
5856 if (PyType_Ready(&type)) \
5857 return -1;
5858
5859 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02005860init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005861{
5862 PYTYPE_READY(IterType);
5863 PYTYPE_READY(BufferType);
5864 PYTYPE_READY(RangeType);
5865 PYTYPE_READY(WindowType);
5866 PYTYPE_READY(TabPageType);
5867 PYTYPE_READY(BufMapType);
5868 PYTYPE_READY(WinListType);
5869 PYTYPE_READY(TabListType);
5870 PYTYPE_READY(CurrentType);
5871 PYTYPE_READY(DictionaryType);
5872 PYTYPE_READY(ListType);
5873 PYTYPE_READY(FunctionType);
5874 PYTYPE_READY(OptionsType);
5875 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005876 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005877 return 0;
5878}
5879
5880 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005881init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005882{
5883 PyObject *path;
5884 PyObject *path_hook;
5885 PyObject *path_hooks;
5886
5887 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5888 return -1;
5889
5890 if (!(path_hooks = PySys_GetObject("path_hooks")))
5891 {
5892 PyErr_Clear();
5893 path_hooks = PyList_New(1);
5894 PyList_SET_ITEM(path_hooks, 0, path_hook);
5895 if (PySys_SetObject("path_hooks", path_hooks))
5896 {
5897 Py_DECREF(path_hooks);
5898 return -1;
5899 }
5900 Py_DECREF(path_hooks);
5901 }
5902 else if (PyList_Check(path_hooks))
5903 {
5904 if (PyList_Append(path_hooks, path_hook))
5905 {
5906 Py_DECREF(path_hook);
5907 return -1;
5908 }
5909 Py_DECREF(path_hook);
5910 }
5911 else
5912 {
5913 VimTryStart();
5914 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5915 "You should now do the following:\n"
5916 "- append vim.path_hook to sys.path_hooks\n"
5917 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5918 VimTryEnd(); /* Discard the error */
5919 Py_DECREF(path_hook);
5920 return 0;
5921 }
5922
5923 if (!(path = PySys_GetObject("path")))
5924 {
5925 PyErr_Clear();
5926 path = PyList_New(1);
5927 Py_INCREF(vim_special_path_object);
5928 PyList_SET_ITEM(path, 0, vim_special_path_object);
5929 if (PySys_SetObject("path", path))
5930 {
5931 Py_DECREF(path);
5932 return -1;
5933 }
5934 Py_DECREF(path);
5935 }
5936 else if (PyList_Check(path))
5937 {
5938 if (PyList_Append(path, vim_special_path_object))
5939 return -1;
5940 }
5941 else
5942 {
5943 VimTryStart();
5944 EMSG(_("Failed to set path: sys.path is not a list\n"
5945 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5946 VimTryEnd(); /* Discard the error */
5947 }
5948
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005949 return 0;
5950}
5951
5952static BufMapObject TheBufferMap =
5953{
5954 PyObject_HEAD_INIT(&BufMapType)
5955};
5956
5957static WinListObject TheWindowList =
5958{
5959 PyObject_HEAD_INIT(&WinListType)
5960 NULL
5961};
5962
5963static CurrentObject TheCurrent =
5964{
5965 PyObject_HEAD_INIT(&CurrentType)
5966};
5967
5968static TabListObject TheTabPageList =
5969{
5970 PyObject_HEAD_INIT(&TabListType)
5971};
5972
5973static struct numeric_constant {
5974 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005975 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005976} numeric_constants[] = {
5977 {"VAR_LOCKED", VAR_LOCKED},
5978 {"VAR_FIXED", VAR_FIXED},
5979 {"VAR_SCOPE", VAR_SCOPE},
5980 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5981};
5982
5983static struct object_constant {
5984 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005985 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005986} object_constants[] = {
5987 {"buffers", (PyObject *)(void *)&TheBufferMap},
5988 {"windows", (PyObject *)(void *)&TheWindowList},
5989 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5990 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005991
5992 {"Buffer", (PyObject *)&BufferType},
5993 {"Range", (PyObject *)&RangeType},
5994 {"Window", (PyObject *)&WindowType},
5995 {"TabPage", (PyObject *)&TabPageType},
5996 {"Dictionary", (PyObject *)&DictionaryType},
5997 {"List", (PyObject *)&ListType},
5998 {"Function", (PyObject *)&FunctionType},
5999 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006000 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006001};
6002
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006003#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006004 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006005 return -1;
6006
6007#define ADD_CHECKED_OBJECT(m, name, obj) \
6008 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006009 PyObject *valObject = obj; \
6010 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006011 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006012 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006013 }
6014
6015 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006016populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006017{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006018 int i;
6019 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006020 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006021 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006022
6023 for (i = 0; i < (int)(sizeof(numeric_constants)
6024 / sizeof(struct numeric_constant));
6025 ++i)
6026 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006027 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006028
6029 for (i = 0; i < (int)(sizeof(object_constants)
6030 / sizeof(struct object_constant));
6031 ++i)
6032 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006033 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006034
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006035 valObject = object_constants[i].valObject;
6036 Py_INCREF(valObject);
6037 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006038 }
6039
6040 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6041 return -1;
6042 ADD_OBJECT(m, "error", VimError);
6043
Bram Moolenaara9922d62013-05-30 13:01:18 +02006044 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6045 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006046 ADD_CHECKED_OBJECT(m, "options",
6047 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006048
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006049 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006050 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006051 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006052
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006053 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006054 return -1;
6055 ADD_OBJECT(m, "_getcwd", py_getcwd)
6056
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006057 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006058 return -1;
6059 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006060 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006061 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006062 if (PyObject_SetAttrString(other_module, "chdir", attr))
6063 {
6064 Py_DECREF(attr);
6065 return -1;
6066 }
6067 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006068
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006069 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006070 {
6071 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006072 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006073 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006074 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6075 {
6076 Py_DECREF(attr);
6077 return -1;
6078 }
6079 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006080 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006081 else
6082 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006083
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006084 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6085 return -1;
6086
6087 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6088
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006089 if (!(imp = PyImport_ImportModule("imp")))
6090 return -1;
6091
6092 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6093 {
6094 Py_DECREF(imp);
6095 return -1;
6096 }
6097
6098 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6099 {
6100 Py_DECREF(py_find_module);
6101 Py_DECREF(imp);
6102 return -1;
6103 }
6104
6105 Py_DECREF(imp);
6106
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006107 ADD_OBJECT(m, "_find_module", py_find_module);
6108 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006109
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006110 return 0;
6111}