blob: 4ac2ebfad92ca57ae3f72f4b56baa1d4eaa92cf5 [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{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004000 buf_T *save_curbuf = NULL;
4001 win_T *wp;
4002 win_T *save_curwin = NULL;
4003 tabpage_T *tp;
4004 tabpage_T *save_curtab = NULL;
4005
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004006 /* First of all, we check the type of the supplied Python object.
4007 * It must be a string or a list, or the call is in error.
4008 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004009 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004011 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004012
4013 if (str == NULL)
4014 return FAIL;
4015
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004016 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004017 VimTryStart();
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004018 if (find_win_for_buf(buf, &wp, &tp) == FAIL
4019 || switch_win(&save_curwin, &save_curtab, wp, tp, TRUE)
4020 == FAIL)
4021 switch_buffer(&save_curbuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004023 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004024 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004025 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004026 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004027 else if (save_curbuf == NULL)
4028 /* Only adjust marks if we managed to switch to a window that
4029 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030 appended_lines_mark((linenr_T)n, 1L);
4031
4032 vim_free(str);
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004033 if (save_curbuf == NULL)
4034 restore_win(save_curwin, save_curtab, TRUE);
4035 else
4036 restore_buffer(save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037 update_screen(VALID);
4038
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004039 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004040 return FAIL;
4041
4042 if (len_change)
4043 *len_change = 1;
4044
4045 return OK;
4046 }
4047 else if (PyList_Check(lines))
4048 {
4049 PyInt i;
4050 PyInt size = PyList_Size(lines);
4051 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004053 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 if (array == NULL)
4055 {
4056 PyErr_NoMemory();
4057 return FAIL;
4058 }
4059
4060 for (i = 0; i < size; ++i)
4061 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004062 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004063
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004064 if (!(line = PyList_GetItem(lines, i)) ||
4065 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066 {
4067 while (i)
4068 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004069 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070 return FAIL;
4071 }
4072 }
4073
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004075 VimTryStart();
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004076 if (find_win_for_buf(buf, &wp, &tp) == FAIL
4077 || switch_win(&save_curwin, &save_curtab, wp, tp, TRUE)
4078 == FAIL)
4079 switch_buffer(&save_curbuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004080
4081 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004082 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 else
4084 {
4085 for (i = 0; i < size; ++i)
4086 {
4087 if (ml_append((linenr_T)(n + i),
4088 (char_u *)array[i], 0, FALSE) == FAIL)
4089 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004090 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091
4092 /* Free the rest of the lines */
4093 while (i < size)
4094 vim_free(array[i++]);
4095
4096 break;
4097 }
4098 vim_free(array[i]);
4099 }
4100 if (i > 0)
4101 appended_lines_mark((linenr_T)n, (long)i);
4102 }
4103
4104 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004105 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004106 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004108 if (save_curbuf == NULL)
4109 restore_win(save_curwin, save_curtab, TRUE);
4110 else
4111 restore_buffer(save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 update_screen(VALID);
4113
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004114 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115 return FAIL;
4116
4117 if (len_change)
4118 *len_change = size;
4119
4120 return OK;
4121 }
4122 else
4123 {
4124 PyErr_BadArgument();
4125 return FAIL;
4126 }
4127}
4128
4129/*
4130 * Common routines for buffers and line ranges
4131 * -------------------------------------------
4132 */
4133
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004134typedef struct
4135{
4136 PyObject_HEAD
4137 buf_T *buf;
4138} BufferObject;
4139
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004140 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004141CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004142{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004143 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004144 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004145 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146 return -1;
4147 }
4148
4149 return 0;
4150}
4151
4152 static PyObject *
4153RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4154{
4155 if (CheckBuffer(self))
4156 return NULL;
4157
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004158 if (end == -1)
4159 end = self->buf->b_ml.ml_line_count;
4160
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004161 if (n < 0)
4162 n += end - start + 1;
4163
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164 if (n < 0 || n > end - start)
4165 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004166 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167 return NULL;
4168 }
4169
4170 return GetBufferLine(self->buf, n+start);
4171}
4172
4173 static PyObject *
4174RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4175{
4176 PyInt size;
4177
4178 if (CheckBuffer(self))
4179 return NULL;
4180
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004181 if (end == -1)
4182 end = self->buf->b_ml.ml_line_count;
4183
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004184 size = end - start + 1;
4185
4186 if (lo < 0)
4187 lo = 0;
4188 else if (lo > size)
4189 lo = size;
4190 if (hi < 0)
4191 hi = 0;
4192 if (hi < lo)
4193 hi = lo;
4194 else if (hi > size)
4195 hi = size;
4196
4197 return GetBufferLineList(self->buf, lo+start, hi+start);
4198}
4199
4200 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004201RBAsItem(
4202 BufferObject *self,
4203 PyInt n,
4204 PyObject *valObject,
4205 PyInt start,
4206 PyInt end,
4207 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004208{
4209 PyInt len_change;
4210
4211 if (CheckBuffer(self))
4212 return -1;
4213
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004214 if (end == -1)
4215 end = self->buf->b_ml.ml_line_count;
4216
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004217 if (n < 0)
4218 n += end - start + 1;
4219
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004220 if (n < 0 || n > end - start)
4221 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004222 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004223 return -1;
4224 }
4225
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004226 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004227 return -1;
4228
4229 if (new_end)
4230 *new_end = end + len_change;
4231
4232 return 0;
4233}
4234
Bram Moolenaar19e60942011-06-19 00:27:51 +02004235 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004236RBAsSlice(
4237 BufferObject *self,
4238 PyInt lo,
4239 PyInt hi,
4240 PyObject *valObject,
4241 PyInt start,
4242 PyInt end,
4243 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004244{
4245 PyInt size;
4246 PyInt len_change;
4247
4248 /* Self must be a valid buffer */
4249 if (CheckBuffer(self))
4250 return -1;
4251
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004252 if (end == -1)
4253 end = self->buf->b_ml.ml_line_count;
4254
Bram Moolenaar19e60942011-06-19 00:27:51 +02004255 /* Sort out the slice range */
4256 size = end - start + 1;
4257
4258 if (lo < 0)
4259 lo = 0;
4260 else if (lo > size)
4261 lo = size;
4262 if (hi < 0)
4263 hi = 0;
4264 if (hi < lo)
4265 hi = lo;
4266 else if (hi > size)
4267 hi = size;
4268
4269 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004270 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004271 return -1;
4272
4273 if (new_end)
4274 *new_end = end + len_change;
4275
4276 return 0;
4277}
4278
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004279
4280 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004281RBAppend(
4282 BufferObject *self,
4283 PyObject *args,
4284 PyInt start,
4285 PyInt end,
4286 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004287{
4288 PyObject *lines;
4289 PyInt len_change;
4290 PyInt max;
4291 PyInt n;
4292
4293 if (CheckBuffer(self))
4294 return NULL;
4295
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004296 if (end == -1)
4297 end = self->buf->b_ml.ml_line_count;
4298
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004299 max = n = end - start + 1;
4300
4301 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4302 return NULL;
4303
4304 if (n < 0 || n > max)
4305 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004306 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307 return NULL;
4308 }
4309
4310 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4311 return NULL;
4312
4313 if (new_end)
4314 *new_end = end + len_change;
4315
4316 Py_INCREF(Py_None);
4317 return Py_None;
4318}
4319
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004320/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321 */
4322
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004323static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004324static PySequenceMethods RangeAsSeq;
4325static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004326
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004327typedef struct
4328{
4329 PyObject_HEAD
4330 BufferObject *buf;
4331 PyInt start;
4332 PyInt end;
4333} RangeObject;
4334
4335 static PyObject *
4336RangeNew(buf_T *buf, PyInt start, PyInt end)
4337{
4338 BufferObject *bufr;
4339 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004340 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004341 if (self == NULL)
4342 return NULL;
4343
4344 bufr = (BufferObject *)BufferNew(buf);
4345 if (bufr == NULL)
4346 {
4347 Py_DECREF(self);
4348 return NULL;
4349 }
4350 Py_INCREF(bufr);
4351
4352 self->buf = bufr;
4353 self->start = start;
4354 self->end = end;
4355
4356 return (PyObject *)(self);
4357}
4358
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004359 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004360RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004361{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004362 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004363 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004364 PyObject_GC_Del((void *)(self));
4365}
4366
4367 static int
4368RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4369{
4370 Py_VISIT(((PyObject *)(self->buf)));
4371 return 0;
4372}
4373
4374 static int
4375RangeClear(RangeObject *self)
4376{
4377 Py_CLEAR(self->buf);
4378 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004379}
4380
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004381 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004382RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004383{
4384 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004385 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004386 return -1; /* ??? */
4387
Bram Moolenaard6e39182013-05-21 18:30:34 +02004388 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004389}
4390
4391 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004392RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004393{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004394 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004395}
4396
4397 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004398RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004399{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004400 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004401}
4402
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004403static char *RangeAttrs[] = {
4404 "start", "end",
4405 NULL
4406};
4407
4408 static PyObject *
4409RangeDir(PyObject *self)
4410{
4411 return ObjectDir(self, RangeAttrs);
4412}
4413
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004414 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004415RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004416{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004417 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004418}
4419
4420 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004421RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004422{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004423 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004424 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4425 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004426 else
4427 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004428 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004429
4430 if (name == NULL)
4431 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004432
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004433 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004434 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004435 }
4436}
4437
4438static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004439 /* name, function, calling, documentation */
4440 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004441 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4442 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004443};
4444
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004445static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004446static PySequenceMethods BufferAsSeq;
4447static PyMappingMethods BufferAsMapping;
4448
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004449 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004450BufferNew(buf_T *buf)
4451{
4452 /* We need to handle deletion of buffers underneath us.
4453 * If we add a "b_python*_ref" field to the buf_T structure,
4454 * then we can get at it in buf_freeall() in vim. We then
4455 * need to create only ONE Python object per buffer - if
4456 * we try to create a second, just INCREF the existing one
4457 * and return it. The (single) Python object referring to
4458 * the buffer is stored in "b_python*_ref".
4459 * Question: what to do on a buf_freeall(). We'll probably
4460 * have to either delete the Python object (DECREF it to
4461 * zero - a bad idea, as it leaves dangling refs!) or
4462 * set the buf_T * value to an invalid value (-1?), which
4463 * means we need checks in all access functions... Bah.
4464 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004465 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004466 * b_python_ref and b_python3_ref fields respectively.
4467 */
4468
4469 BufferObject *self;
4470
4471 if (BUF_PYTHON_REF(buf) != NULL)
4472 {
4473 self = BUF_PYTHON_REF(buf);
4474 Py_INCREF(self);
4475 }
4476 else
4477 {
4478 self = PyObject_NEW(BufferObject, &BufferType);
4479 if (self == NULL)
4480 return NULL;
4481 self->buf = buf;
4482 BUF_PYTHON_REF(buf) = self;
4483 }
4484
4485 return (PyObject *)(self);
4486}
4487
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004488 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004489BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004490{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004491 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4492 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004493
4494 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004495}
4496
Bram Moolenaar971db462013-05-12 18:44:48 +02004497 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004498BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004499{
4500 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004501 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004502 return -1; /* ??? */
4503
Bram Moolenaard6e39182013-05-21 18:30:34 +02004504 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004505}
4506
4507 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004508BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004509{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004510 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004511}
4512
4513 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004514BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004515{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004516 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004517}
4518
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004519static char *BufferAttrs[] = {
4520 "name", "number", "vars", "options", "valid",
4521 NULL
4522};
4523
4524 static PyObject *
4525BufferDir(PyObject *self)
4526{
4527 return ObjectDir(self, BufferAttrs);
4528}
4529
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004530 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004531BufferAttrValid(BufferObject *self, char *name)
4532{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004533 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004534
4535 if (strcmp(name, "valid") != 0)
4536 return NULL;
4537
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004538 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4539 Py_INCREF(ret);
4540 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004541}
4542
4543 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004544BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004545{
4546 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004547 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004548 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004549 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004550 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004551 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004552 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004553 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004554 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4555 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004556 else if (strcmp(name, "__members__") == 0)
4557 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004558 else
4559 return NULL;
4560}
4561
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004562 static int
4563BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4564{
4565 if (CheckBuffer(self))
4566 return -1;
4567
4568 if (strcmp(name, "name") == 0)
4569 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004570 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004571 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004572 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004573 PyObject *todecref;
4574
4575 if (!(val = StringToChars(valObject, &todecref)))
4576 return -1;
4577
4578 VimTryStart();
4579 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4580 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004581 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004582 aucmd_restbuf(&aco);
4583 Py_XDECREF(todecref);
4584 if (VimTryEnd())
4585 return -1;
4586
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004587 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004588 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004589 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004590 return -1;
4591 }
4592 return 0;
4593 }
4594 else
4595 {
4596 PyErr_SetString(PyExc_AttributeError, name);
4597 return -1;
4598 }
4599}
4600
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004601 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004602BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004603{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004604 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004605}
4606
4607 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004608BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004609{
4610 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004611 char_u *pmark;
4612 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004613 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004614 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004615
Bram Moolenaard6e39182013-05-21 18:30:34 +02004616 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004617 return NULL;
4618
Bram Moolenaar389a1792013-06-23 13:00:44 +02004619 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004620 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004621
Bram Moolenaar389a1792013-06-23 13:00:44 +02004622 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004623 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004624 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004625 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004626 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004627 return NULL;
4628 }
4629
4630 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004631
4632 Py_XDECREF(todecref);
4633
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004634 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004635 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004636 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004637 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004638 if (VimTryEnd())
4639 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004640
4641 if (posp == NULL)
4642 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004643 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004644 return NULL;
4645 }
4646
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004647 if (posp->lnum <= 0)
4648 {
4649 /* Or raise an error? */
4650 Py_INCREF(Py_None);
4651 return Py_None;
4652 }
4653
4654 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4655}
4656
4657 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004658BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004659{
4660 PyInt start;
4661 PyInt end;
4662
Bram Moolenaard6e39182013-05-21 18:30:34 +02004663 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004664 return NULL;
4665
4666 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4667 return NULL;
4668
Bram Moolenaard6e39182013-05-21 18:30:34 +02004669 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004670}
4671
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004672 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004673BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004674{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004675 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004676 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004677 else
4678 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004679 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004680
4681 if (name == NULL)
4682 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004683
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004684 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004685 }
4686}
4687
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004688static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004689 /* name, function, calling, documentation */
4690 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004691 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004692 {"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 +02004693 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4694 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004695};
4696
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004697/*
4698 * Buffer list object - Implementation
4699 */
4700
4701static PyTypeObject BufMapType;
4702
4703typedef struct
4704{
4705 PyObject_HEAD
4706} BufMapObject;
4707
4708 static PyInt
4709BufMapLength(PyObject *self UNUSED)
4710{
4711 buf_T *b = firstbuf;
4712 PyInt n = 0;
4713
4714 while (b)
4715 {
4716 ++n;
4717 b = b->b_next;
4718 }
4719
4720 return n;
4721}
4722
4723 static PyObject *
4724BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4725{
4726 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004727 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004728
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004729 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004730 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004731
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004732 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004733
4734 if (b)
4735 return BufferNew(b);
4736 else
4737 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004738 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004739 return NULL;
4740 }
4741}
4742
4743 static void
4744BufMapIterDestruct(PyObject *buffer)
4745{
4746 /* Iteration was stopped before all buffers were processed */
4747 if (buffer)
4748 {
4749 Py_DECREF(buffer);
4750 }
4751}
4752
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004753 static int
4754BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4755{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004756 if (buffer)
4757 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004758 return 0;
4759}
4760
4761 static int
4762BufMapIterClear(PyObject **buffer)
4763{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004764 if (*buffer)
4765 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004766 return 0;
4767}
4768
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004769 static PyObject *
4770BufMapIterNext(PyObject **buffer)
4771{
4772 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004773 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004774
4775 if (!*buffer)
4776 return NULL;
4777
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004778 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004779
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004780 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004781 {
4782 *buffer = NULL;
4783 return NULL;
4784 }
4785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004786 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004787 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004788 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004789 return NULL;
4790 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004791 /* Do not increment reference: we no longer hold it (decref), but whoever
4792 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004793 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004794}
4795
4796 static PyObject *
4797BufMapIter(PyObject *self UNUSED)
4798{
4799 PyObject *buffer;
4800
4801 buffer = BufferNew(firstbuf);
4802 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004803 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4804 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004805}
4806
4807static PyMappingMethods BufMapAsMapping = {
4808 (lenfunc) BufMapLength,
4809 (binaryfunc) BufMapItem,
4810 (objobjargproc) 0,
4811};
4812
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004813/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004814 */
4815
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004816static char *CurrentAttrs[] = {
4817 "buffer", "window", "line", "range", "tabpage",
4818 NULL
4819};
4820
4821 static PyObject *
4822CurrentDir(PyObject *self)
4823{
4824 return ObjectDir(self, CurrentAttrs);
4825}
4826
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004827 static PyObject *
4828CurrentGetattr(PyObject *self UNUSED, char *name)
4829{
4830 if (strcmp(name, "buffer") == 0)
4831 return (PyObject *)BufferNew(curbuf);
4832 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004833 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004834 else if (strcmp(name, "tabpage") == 0)
4835 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004836 else if (strcmp(name, "line") == 0)
4837 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4838 else if (strcmp(name, "range") == 0)
4839 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004840 else if (strcmp(name, "__members__") == 0)
4841 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004842 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004843#if PY_MAJOR_VERSION < 3
4844 return Py_FindMethod(WindowMethods, self, name);
4845#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004846 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004847#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004848}
4849
4850 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004851CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004852{
4853 if (strcmp(name, "line") == 0)
4854 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004855 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4856 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004857 return -1;
4858
4859 return 0;
4860 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004861 else if (strcmp(name, "buffer") == 0)
4862 {
4863 int count;
4864
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004865 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004866 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004867 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004868 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004869 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004870 return -1;
4871 }
4872
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004873 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004874 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004875 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004876
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004877 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004878 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4879 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004880 if (VimTryEnd())
4881 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004882 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004883 return -1;
4884 }
4885
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004886 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004887 }
4888 else if (strcmp(name, "window") == 0)
4889 {
4890 int count;
4891
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004892 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004893 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004894 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004895 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004896 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004897 return -1;
4898 }
4899
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004900 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004901 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004902 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004903
4904 if (!count)
4905 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004906 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004907 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004908 return -1;
4909 }
4910
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004911 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004912 win_goto(((WindowObject *)(valObject))->win);
4913 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004914 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004915 if (VimTryEnd())
4916 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004917 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004918 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004919 return -1;
4920 }
4921
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004922 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004923 }
4924 else if (strcmp(name, "tabpage") == 0)
4925 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004926 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004927 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004928 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004929 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004930 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004931 return -1;
4932 }
4933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004934 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004935 return -1;
4936
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004937 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004938 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4939 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004940 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004941 if (VimTryEnd())
4942 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004943 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004944 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004945 return -1;
4946 }
4947
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004948 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004949 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004950 else
4951 {
4952 PyErr_SetString(PyExc_AttributeError, name);
4953 return -1;
4954 }
4955}
4956
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004957static struct PyMethodDef CurrentMethods[] = {
4958 /* name, function, calling, documentation */
4959 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4960 { NULL, NULL, 0, NULL}
4961};
4962
Bram Moolenaardb913952012-06-29 12:54:53 +02004963 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004964init_range_cmd(exarg_T *eap)
4965{
4966 RangeStart = eap->line1;
4967 RangeEnd = eap->line2;
4968}
4969
4970 static void
4971init_range_eval(typval_T *rettv UNUSED)
4972{
4973 RangeStart = (PyInt) curwin->w_cursor.lnum;
4974 RangeEnd = RangeStart;
4975}
4976
4977 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004978run_cmd(const char *cmd, void *arg UNUSED
4979#ifdef PY_CAN_RECURSE
4980 , PyGILState_STATE *pygilstate UNUSED
4981#endif
4982 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004983{
Bram Moolenaar41009372013-07-01 22:03:04 +02004984 PyObject *run_ret;
4985 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
4986 if (run_ret != NULL)
4987 {
4988 Py_DECREF(run_ret);
4989 }
4990 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
4991 {
4992 EMSG2(_(e_py_systemexit), "python");
4993 PyErr_Clear();
4994 }
4995 else
4996 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004997}
4998
4999static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5000static int code_hdr_len = 30;
5001
5002 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005003run_do(const char *cmd, void *arg UNUSED
5004#ifdef PY_CAN_RECURSE
5005 , PyGILState_STATE *pygilstate
5006#endif
5007 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005008{
5009 PyInt lnum;
5010 size_t len;
5011 char *code;
5012 int status;
5013 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005014 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005015
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005016 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005017 {
5018 EMSG(_("cannot save undo information"));
5019 return;
5020 }
5021
5022 len = code_hdr_len + STRLEN(cmd);
5023 code = PyMem_New(char, len + 1);
5024 memcpy(code, code_hdr, code_hdr_len);
5025 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005026 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5027 status = -1;
5028 if (run_ret != NULL)
5029 {
5030 status = 0;
5031 Py_DECREF(run_ret);
5032 }
5033 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5034 {
5035 PyMem_Free(code);
5036 EMSG2(_(e_py_systemexit), "python");
5037 PyErr_Clear();
5038 return;
5039 }
5040 else
5041 PyErr_PrintEx(1);
5042
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005043 PyMem_Free(code);
5044
5045 if (status)
5046 {
5047 EMSG(_("failed to run the code"));
5048 return;
5049 }
5050
5051 status = 0;
5052 pymain = PyImport_AddModule("__main__");
5053 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005054#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005055 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005056#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005057
5058 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5059 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005060 PyObject *line;
5061 PyObject *linenr;
5062 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005063
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005064#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005065 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005066#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005067 if (!(line = GetBufferLine(curbuf, lnum)))
5068 goto err;
5069 if (!(linenr = PyInt_FromLong((long) lnum)))
5070 {
5071 Py_DECREF(line);
5072 goto err;
5073 }
5074 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5075 Py_DECREF(line);
5076 Py_DECREF(linenr);
5077 if (!ret)
5078 goto err;
5079
5080 if (ret != Py_None)
5081 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5082 goto err;
5083
5084 Py_XDECREF(ret);
5085 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005086#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005087 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005088#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005089 }
5090 goto out;
5091err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005092#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005093 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005094#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005095 PyErr_PrintEx(0);
5096 PythonIO_Flush();
5097 status = 1;
5098out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005099#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005100 if (!status)
5101 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005102#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005103 Py_DECREF(pyfunc);
5104 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5105 if (status)
5106 return;
5107 check_cursor();
5108 update_curbuf(NOT_VALID);
5109}
5110
5111 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005112run_eval(const char *cmd, typval_T *rettv
5113#ifdef PY_CAN_RECURSE
5114 , PyGILState_STATE *pygilstate UNUSED
5115#endif
5116 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005117{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005118 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005119
Bram Moolenaar41009372013-07-01 22:03:04 +02005120 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005121 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005122 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005123 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005124 {
5125 EMSG2(_(e_py_systemexit), "python");
5126 PyErr_Clear();
5127 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005128 else
5129 {
5130 if (PyErr_Occurred() && !msg_silent)
5131 PyErr_PrintEx(0);
5132 EMSG(_("E858: Eval did not return a valid python object"));
5133 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005134 }
5135 else
5136 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005137 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005138 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005139 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005140 }
5141 PyErr_Clear();
5142}
5143
5144 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005145set_ref_in_py(const int copyID)
5146{
5147 pylinkedlist_T *cur;
5148 dict_T *dd;
5149 list_T *ll;
5150
5151 if (lastdict != NULL)
5152 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5153 {
5154 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5155 if (dd->dv_copyID != copyID)
5156 {
5157 dd->dv_copyID = copyID;
5158 set_ref_in_ht(&dd->dv_hashtab, copyID);
5159 }
5160 }
5161
5162 if (lastlist != NULL)
5163 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5164 {
5165 ll = ((ListObject *) (cur->pll_obj))->list;
5166 if (ll->lv_copyID != copyID)
5167 {
5168 ll->lv_copyID = copyID;
5169 set_ref_in_list(ll, copyID);
5170 }
5171 }
5172}
5173
5174 static int
5175set_string_copy(char_u *str, typval_T *tv)
5176{
5177 tv->vval.v_string = vim_strsave(str);
5178 if (tv->vval.v_string == NULL)
5179 {
5180 PyErr_NoMemory();
5181 return -1;
5182 }
5183 return 0;
5184}
5185
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005186 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005187pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005188{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005189 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005190 char_u *key;
5191 dictitem_T *di;
5192 PyObject *keyObject;
5193 PyObject *valObject;
5194 Py_ssize_t iter = 0;
5195
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005196 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005197 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005198
5199 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005200 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005201
5202 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5203 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005204 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005205
Bram Moolenaara03e6312013-05-29 22:49:26 +02005206 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005207 {
5208 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005209 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005210 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005211
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005212 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005213 {
5214 dict_unref(dict);
5215 return -1;
5216 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005217
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005218 if (*key == NUL)
5219 {
5220 dict_unref(dict);
5221 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005222 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005223 return -1;
5224 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005225
5226 di = dictitem_alloc(key);
5227
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005228 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005229
5230 if (di == NULL)
5231 {
5232 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005233 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005234 return -1;
5235 }
5236 di->di_tv.v_lock = 0;
5237
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005238 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005239 {
5240 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005241 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005242 return -1;
5243 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005244
5245 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005246 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005247 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005248 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005249 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005250 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005251 return -1;
5252 }
5253 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005254
5255 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005256 return 0;
5257}
5258
5259 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005260pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005261{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005262 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005263 char_u *key;
5264 dictitem_T *di;
5265 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005266 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005267 PyObject *keyObject;
5268 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005269
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005270 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005271 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005272
5273 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005274 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005275
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005276 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005277 {
5278 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005279 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005280 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005281
5282 if (!(iterator = PyObject_GetIter(list)))
5283 {
5284 dict_unref(dict);
5285 Py_DECREF(list);
5286 return -1;
5287 }
5288 Py_DECREF(list);
5289
5290 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005291 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005292 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005293
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005294 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005295 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005296 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005297 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005298 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005299 return -1;
5300 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005301
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005302 if (*key == NUL)
5303 {
5304 Py_DECREF(keyObject);
5305 Py_DECREF(iterator);
5306 Py_XDECREF(todecref);
5307 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005308 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005309 return -1;
5310 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005311
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005312 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005313 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005314 Py_DECREF(keyObject);
5315 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005316 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005317 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005318 return -1;
5319 }
5320
5321 di = dictitem_alloc(key);
5322
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005323 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005324 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005325
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005326 if (di == NULL)
5327 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005328 Py_DECREF(iterator);
5329 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005330 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005331 PyErr_NoMemory();
5332 return -1;
5333 }
5334 di->di_tv.v_lock = 0;
5335
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005336 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005337 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005338 Py_DECREF(iterator);
5339 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005340 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005341 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005342 return -1;
5343 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005344
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005345 Py_DECREF(valObject);
5346
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005347 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005348 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005349 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005350 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005351 dictitem_free(di);
5352 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005353 return -1;
5354 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005355 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005356 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005357 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005358 return 0;
5359}
5360
5361 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005362pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005363{
5364 list_T *l;
5365
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005366 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005367 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005368
5369 tv->v_type = VAR_LIST;
5370 tv->vval.v_list = l;
5371
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005372 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005373 {
5374 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005375 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005376 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005377
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005378 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005379 return 0;
5380}
5381
Bram Moolenaardb913952012-06-29 12:54:53 +02005382typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5383
5384 static int
5385convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005386 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005387{
5388 PyObject *capsule;
5389 char hexBuf[sizeof(void *) * 2 + 3];
5390
5391 sprintf(hexBuf, "%p", obj);
5392
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005393# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005394 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005395# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005396 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005397# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005398 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005399 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005400# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005401 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005402# else
5403 capsule = PyCObject_FromVoidPtr(tv, NULL);
5404# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005405 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5406 {
5407 Py_DECREF(capsule);
5408 tv->v_type = VAR_UNKNOWN;
5409 return -1;
5410 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005411
5412 Py_DECREF(capsule);
5413
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005414 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005415 {
5416 tv->v_type = VAR_UNKNOWN;
5417 return -1;
5418 }
5419 /* As we are not using copy_tv which increments reference count we must
5420 * do it ourself. */
5421 switch(tv->v_type)
5422 {
5423 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5424 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5425 }
5426 }
5427 else
5428 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005429 typval_T *v;
5430
5431# ifdef PY_USE_CAPSULE
5432 v = PyCapsule_GetPointer(capsule, NULL);
5433# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005434 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005435# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005436 copy_tv(v, tv);
5437 }
5438 return 0;
5439}
5440
5441 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005442ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5443{
5444 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005445 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005446
5447 if (!(lookup_dict = PyDict_New()))
5448 return -1;
5449
5450 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5451 {
5452 tv->v_type = VAR_DICT;
5453 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5454 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005455 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005456 }
5457 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005458 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005459 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005460 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005461 else
5462 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005463 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005464 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005465 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005466 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005467 }
5468 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005469 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005470}
5471
5472 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005473ConvertFromPyObject(PyObject *obj, typval_T *tv)
5474{
5475 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005476 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005477
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005478 if (!(lookup_dict = PyDict_New()))
5479 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005480 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005481 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005482 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005483}
5484
5485 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005486_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005487{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005488 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005489 {
5490 tv->v_type = VAR_DICT;
5491 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5492 ++tv->vval.v_dict->dv_refcount;
5493 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005494 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005495 {
5496 tv->v_type = VAR_LIST;
5497 tv->vval.v_list = (((ListObject *)(obj))->list);
5498 ++tv->vval.v_list->lv_refcount;
5499 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005500 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005501 {
5502 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5503 return -1;
5504
5505 tv->v_type = VAR_FUNC;
5506 func_ref(tv->vval.v_string);
5507 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005508 else if (PyBytes_Check(obj))
5509 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005510 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005511
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005512 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005513 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005514 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005515 return -1;
5516
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005517 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005518 return -1;
5519
5520 tv->v_type = VAR_STRING;
5521 }
5522 else if (PyUnicode_Check(obj))
5523 {
5524 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005525 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005526
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005527 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005528 if (bytes == NULL)
5529 return -1;
5530
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005531 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005532 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005533 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005534 return -1;
5535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005536 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005537 {
5538 Py_XDECREF(bytes);
5539 return -1;
5540 }
5541 Py_XDECREF(bytes);
5542
5543 tv->v_type = VAR_STRING;
5544 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005545#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005546 else if (PyInt_Check(obj))
5547 {
5548 tv->v_type = VAR_NUMBER;
5549 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005550 if (PyErr_Occurred())
5551 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005552 }
5553#endif
5554 else if (PyLong_Check(obj))
5555 {
5556 tv->v_type = VAR_NUMBER;
5557 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005558 if (PyErr_Occurred())
5559 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005560 }
5561 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005562 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005563#ifdef FEAT_FLOAT
5564 else if (PyFloat_Check(obj))
5565 {
5566 tv->v_type = VAR_FLOAT;
5567 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5568 }
5569#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005570 else if (PyObject_HasAttrString(obj, "keys"))
5571 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005572 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005573 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005574 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005575 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005576 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005577 else if (PyNumber_Check(obj))
5578 {
5579 PyObject *num;
5580
5581 if (!(num = PyNumber_Long(obj)))
5582 return -1;
5583
5584 tv->v_type = VAR_NUMBER;
5585 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5586
5587 Py_DECREF(num);
5588 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005589 else
5590 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005591 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005592 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005593 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005594 return -1;
5595 }
5596 return 0;
5597}
5598
5599 static PyObject *
5600ConvertToPyObject(typval_T *tv)
5601{
5602 if (tv == NULL)
5603 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005604 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005605 return NULL;
5606 }
5607 switch (tv->v_type)
5608 {
5609 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005610 return PyBytes_FromString(tv->vval.v_string == NULL
5611 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005612 case VAR_NUMBER:
5613 return PyLong_FromLong((long) tv->vval.v_number);
5614#ifdef FEAT_FLOAT
5615 case VAR_FLOAT:
5616 return PyFloat_FromDouble((double) tv->vval.v_float);
5617#endif
5618 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005619 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005620 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005621 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005622 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005623 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005624 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005625 case VAR_UNKNOWN:
5626 Py_INCREF(Py_None);
5627 return Py_None;
5628 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005629 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005630 return NULL;
5631 }
5632}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005633
5634typedef struct
5635{
5636 PyObject_HEAD
5637} CurrentObject;
5638static PyTypeObject CurrentType;
5639
5640 static void
5641init_structs(void)
5642{
5643 vim_memset(&OutputType, 0, sizeof(OutputType));
5644 OutputType.tp_name = "vim.message";
5645 OutputType.tp_basicsize = sizeof(OutputObject);
5646 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5647 OutputType.tp_doc = "vim message object";
5648 OutputType.tp_methods = OutputMethods;
5649#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005650 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5651 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005652 OutputType.tp_alloc = call_PyType_GenericAlloc;
5653 OutputType.tp_new = call_PyType_GenericNew;
5654 OutputType.tp_free = call_PyObject_Free;
5655#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005656 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5657 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005658#endif
5659
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005660 vim_memset(&IterType, 0, sizeof(IterType));
5661 IterType.tp_name = "vim.iter";
5662 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005663 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005664 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005665 IterType.tp_iter = (getiterfunc)IterIter;
5666 IterType.tp_iternext = (iternextfunc)IterNext;
5667 IterType.tp_dealloc = (destructor)IterDestructor;
5668 IterType.tp_traverse = (traverseproc)IterTraverse;
5669 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005670
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005671 vim_memset(&BufferType, 0, sizeof(BufferType));
5672 BufferType.tp_name = "vim.buffer";
5673 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005674 BufferType.tp_dealloc = (destructor)BufferDestructor;
5675 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005676 BufferType.tp_as_sequence = &BufferAsSeq;
5677 BufferType.tp_as_mapping = &BufferAsMapping;
5678 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5679 BufferType.tp_doc = "vim buffer object";
5680 BufferType.tp_methods = BufferMethods;
5681#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005682 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005683 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005684 BufferType.tp_alloc = call_PyType_GenericAlloc;
5685 BufferType.tp_new = call_PyType_GenericNew;
5686 BufferType.tp_free = call_PyObject_Free;
5687#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005688 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005689 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005690#endif
5691
5692 vim_memset(&WindowType, 0, sizeof(WindowType));
5693 WindowType.tp_name = "vim.window";
5694 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005695 WindowType.tp_dealloc = (destructor)WindowDestructor;
5696 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005697 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005698 WindowType.tp_doc = "vim Window object";
5699 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005700 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5701 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005702#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005703 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5704 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005705 WindowType.tp_alloc = call_PyType_GenericAlloc;
5706 WindowType.tp_new = call_PyType_GenericNew;
5707 WindowType.tp_free = call_PyObject_Free;
5708#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005709 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5710 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005711#endif
5712
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005713 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5714 TabPageType.tp_name = "vim.tabpage";
5715 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005716 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5717 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005718 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5719 TabPageType.tp_doc = "vim tab page object";
5720 TabPageType.tp_methods = TabPageMethods;
5721#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005722 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005723 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5724 TabPageType.tp_new = call_PyType_GenericNew;
5725 TabPageType.tp_free = call_PyObject_Free;
5726#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005727 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005728#endif
5729
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005730 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5731 BufMapType.tp_name = "vim.bufferlist";
5732 BufMapType.tp_basicsize = sizeof(BufMapObject);
5733 BufMapType.tp_as_mapping = &BufMapAsMapping;
5734 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005735 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005736 BufferType.tp_doc = "vim buffer list";
5737
5738 vim_memset(&WinListType, 0, sizeof(WinListType));
5739 WinListType.tp_name = "vim.windowlist";
5740 WinListType.tp_basicsize = sizeof(WinListType);
5741 WinListType.tp_as_sequence = &WinListAsSeq;
5742 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5743 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005744 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005745
5746 vim_memset(&TabListType, 0, sizeof(TabListType));
5747 TabListType.tp_name = "vim.tabpagelist";
5748 TabListType.tp_basicsize = sizeof(TabListType);
5749 TabListType.tp_as_sequence = &TabListAsSeq;
5750 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5751 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005752
5753 vim_memset(&RangeType, 0, sizeof(RangeType));
5754 RangeType.tp_name = "vim.range";
5755 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005756 RangeType.tp_dealloc = (destructor)RangeDestructor;
5757 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005758 RangeType.tp_as_sequence = &RangeAsSeq;
5759 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005760 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005761 RangeType.tp_doc = "vim Range object";
5762 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005763 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5764 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005765#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005766 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005767 RangeType.tp_alloc = call_PyType_GenericAlloc;
5768 RangeType.tp_new = call_PyType_GenericNew;
5769 RangeType.tp_free = call_PyObject_Free;
5770#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005771 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005772#endif
5773
5774 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5775 CurrentType.tp_name = "vim.currentdata";
5776 CurrentType.tp_basicsize = sizeof(CurrentObject);
5777 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5778 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005779 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005780#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005781 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5782 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005783#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005784 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5785 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005786#endif
5787
5788 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5789 DictionaryType.tp_name = "vim.dictionary";
5790 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005791 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005792 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005793 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005794 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005795 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5796 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005797 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5798 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5799 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005800#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005801 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5802 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005803#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005804 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5805 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005806#endif
5807
5808 vim_memset(&ListType, 0, sizeof(ListType));
5809 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005810 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005811 ListType.tp_basicsize = sizeof(ListObject);
5812 ListType.tp_as_sequence = &ListAsSeq;
5813 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005814 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005815 ListType.tp_doc = "list pushing modifications to vim structure";
5816 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005817 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005818 ListType.tp_new = (newfunc)ListConstructor;
5819 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005820#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005821 ListType.tp_getattro = (getattrofunc)ListGetattro;
5822 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005823#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005824 ListType.tp_getattr = (getattrfunc)ListGetattr;
5825 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005826#endif
5827
5828 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005829 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005830 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005831 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5832 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005833 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005834 FunctionType.tp_doc = "object that calls vim function";
5835 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005836 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005837 FunctionType.tp_new = (newfunc)FunctionConstructor;
5838 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005839#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005840 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005841#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005842 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005843#endif
5844
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005845 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5846 OptionsType.tp_name = "vim.options";
5847 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005848 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005849 OptionsType.tp_doc = "object for manipulating options";
5850 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005851 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5852 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5853 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005854
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005855 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5856 LoaderType.tp_name = "vim.Loader";
5857 LoaderType.tp_basicsize = sizeof(LoaderObject);
5858 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5859 LoaderType.tp_doc = "vim message object";
5860 LoaderType.tp_methods = LoaderMethods;
5861 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5862
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005863#if PY_MAJOR_VERSION >= 3
5864 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5865 vimmodule.m_name = "vim";
5866 vimmodule.m_doc = "Vim Python interface\n";
5867 vimmodule.m_size = -1;
5868 vimmodule.m_methods = VimMethods;
5869#endif
5870}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005871
5872#define PYTYPE_READY(type) \
5873 if (PyType_Ready(&type)) \
5874 return -1;
5875
5876 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02005877init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005878{
5879 PYTYPE_READY(IterType);
5880 PYTYPE_READY(BufferType);
5881 PYTYPE_READY(RangeType);
5882 PYTYPE_READY(WindowType);
5883 PYTYPE_READY(TabPageType);
5884 PYTYPE_READY(BufMapType);
5885 PYTYPE_READY(WinListType);
5886 PYTYPE_READY(TabListType);
5887 PYTYPE_READY(CurrentType);
5888 PYTYPE_READY(DictionaryType);
5889 PYTYPE_READY(ListType);
5890 PYTYPE_READY(FunctionType);
5891 PYTYPE_READY(OptionsType);
5892 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005893 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005894 return 0;
5895}
5896
5897 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005898init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005899{
5900 PyObject *path;
5901 PyObject *path_hook;
5902 PyObject *path_hooks;
5903
5904 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5905 return -1;
5906
5907 if (!(path_hooks = PySys_GetObject("path_hooks")))
5908 {
5909 PyErr_Clear();
5910 path_hooks = PyList_New(1);
5911 PyList_SET_ITEM(path_hooks, 0, path_hook);
5912 if (PySys_SetObject("path_hooks", path_hooks))
5913 {
5914 Py_DECREF(path_hooks);
5915 return -1;
5916 }
5917 Py_DECREF(path_hooks);
5918 }
5919 else if (PyList_Check(path_hooks))
5920 {
5921 if (PyList_Append(path_hooks, path_hook))
5922 {
5923 Py_DECREF(path_hook);
5924 return -1;
5925 }
5926 Py_DECREF(path_hook);
5927 }
5928 else
5929 {
5930 VimTryStart();
5931 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5932 "You should now do the following:\n"
5933 "- append vim.path_hook to sys.path_hooks\n"
5934 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5935 VimTryEnd(); /* Discard the error */
5936 Py_DECREF(path_hook);
5937 return 0;
5938 }
5939
5940 if (!(path = PySys_GetObject("path")))
5941 {
5942 PyErr_Clear();
5943 path = PyList_New(1);
5944 Py_INCREF(vim_special_path_object);
5945 PyList_SET_ITEM(path, 0, vim_special_path_object);
5946 if (PySys_SetObject("path", path))
5947 {
5948 Py_DECREF(path);
5949 return -1;
5950 }
5951 Py_DECREF(path);
5952 }
5953 else if (PyList_Check(path))
5954 {
5955 if (PyList_Append(path, vim_special_path_object))
5956 return -1;
5957 }
5958 else
5959 {
5960 VimTryStart();
5961 EMSG(_("Failed to set path: sys.path is not a list\n"
5962 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5963 VimTryEnd(); /* Discard the error */
5964 }
5965
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005966 return 0;
5967}
5968
5969static BufMapObject TheBufferMap =
5970{
5971 PyObject_HEAD_INIT(&BufMapType)
5972};
5973
5974static WinListObject TheWindowList =
5975{
5976 PyObject_HEAD_INIT(&WinListType)
5977 NULL
5978};
5979
5980static CurrentObject TheCurrent =
5981{
5982 PyObject_HEAD_INIT(&CurrentType)
5983};
5984
5985static TabListObject TheTabPageList =
5986{
5987 PyObject_HEAD_INIT(&TabListType)
5988};
5989
5990static struct numeric_constant {
5991 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005992 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005993} numeric_constants[] = {
5994 {"VAR_LOCKED", VAR_LOCKED},
5995 {"VAR_FIXED", VAR_FIXED},
5996 {"VAR_SCOPE", VAR_SCOPE},
5997 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5998};
5999
6000static struct object_constant {
6001 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006002 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006003} object_constants[] = {
6004 {"buffers", (PyObject *)(void *)&TheBufferMap},
6005 {"windows", (PyObject *)(void *)&TheWindowList},
6006 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6007 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006008
6009 {"Buffer", (PyObject *)&BufferType},
6010 {"Range", (PyObject *)&RangeType},
6011 {"Window", (PyObject *)&WindowType},
6012 {"TabPage", (PyObject *)&TabPageType},
6013 {"Dictionary", (PyObject *)&DictionaryType},
6014 {"List", (PyObject *)&ListType},
6015 {"Function", (PyObject *)&FunctionType},
6016 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006017 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006018};
6019
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006020#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006021 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006022 return -1;
6023
6024#define ADD_CHECKED_OBJECT(m, name, obj) \
6025 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006026 PyObject *valObject = obj; \
6027 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006028 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006029 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006030 }
6031
6032 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006033populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006034{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006035 int i;
6036 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006037 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006038 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006039
6040 for (i = 0; i < (int)(sizeof(numeric_constants)
6041 / sizeof(struct numeric_constant));
6042 ++i)
6043 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006044 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006045
6046 for (i = 0; i < (int)(sizeof(object_constants)
6047 / sizeof(struct object_constant));
6048 ++i)
6049 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006050 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006051
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006052 valObject = object_constants[i].valObject;
6053 Py_INCREF(valObject);
6054 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006055 }
6056
6057 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6058 return -1;
6059 ADD_OBJECT(m, "error", VimError);
6060
Bram Moolenaara9922d62013-05-30 13:01:18 +02006061 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6062 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006063 ADD_CHECKED_OBJECT(m, "options",
6064 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006065
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006066 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006067 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006068 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006069
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006070 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006071 return -1;
6072 ADD_OBJECT(m, "_getcwd", py_getcwd)
6073
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006074 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006075 return -1;
6076 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006077 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006078 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006079 if (PyObject_SetAttrString(other_module, "chdir", attr))
6080 {
6081 Py_DECREF(attr);
6082 return -1;
6083 }
6084 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006085
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006086 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006087 {
6088 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006089 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006090 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006091 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6092 {
6093 Py_DECREF(attr);
6094 return -1;
6095 }
6096 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006097 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006098 else
6099 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006100
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006101 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6102 return -1;
6103
6104 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6105
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006106 if (!(imp = PyImport_ImportModule("imp")))
6107 return -1;
6108
6109 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6110 {
6111 Py_DECREF(imp);
6112 return -1;
6113 }
6114
6115 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6116 {
6117 Py_DECREF(py_find_module);
6118 Py_DECREF(imp);
6119 return -1;
6120 }
6121
6122 Py_DECREF(imp);
6123
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006124 ADD_OBJECT(m, "_find_module", py_find_module);
6125 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006126
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006127 return 0;
6128}