blob: 748577981c71d75e48ec2348e581edb7d482f8c8 [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 Moolenaarba2d7ff2013-11-04 00:34:53 +01001627 if (rObj == NULL)
1628 return -1;
1629
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001630 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001631
Bram Moolenaardee2e312013-06-23 16:35:47 +02001632 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001633
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001634 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001635}
1636
1637typedef struct
1638{
1639 hashitem_T *ht_array;
1640 long_u ht_used;
1641 hashtab_T *ht;
1642 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001643 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001644} dictiterinfo_T;
1645
1646 static PyObject *
1647DictionaryIterNext(dictiterinfo_T **dii)
1648{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001649 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001650
1651 if (!(*dii)->todo)
1652 return NULL;
1653
1654 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1655 (*dii)->ht->ht_used != (*dii)->ht_used)
1656 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001657 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001658 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001659 return NULL;
1660 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001661
Bram Moolenaara9922d62013-05-30 13:01:18 +02001662 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1663 ++((*dii)->hi);
1664
1665 --((*dii)->todo);
1666
Bram Moolenaar41009372013-07-01 22:03:04 +02001667 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001668 return NULL;
1669
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001670 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001671}
1672
1673 static PyObject *
1674DictionaryIter(DictionaryObject *self)
1675{
1676 dictiterinfo_T *dii;
1677 hashtab_T *ht;
1678
1679 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1680 {
1681 PyErr_NoMemory();
1682 return NULL;
1683 }
1684
1685 ht = &self->dict->dv_hashtab;
1686 dii->ht_array = ht->ht_array;
1687 dii->ht_used = ht->ht_used;
1688 dii->ht = ht;
1689 dii->hi = dii->ht_array;
1690 dii->todo = dii->ht_used;
1691
1692 return IterNew(dii,
1693 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1694 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001695}
1696
1697 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001698DictionaryAssItem(
1699 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001700{
1701 char_u *key;
1702 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001703 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001704 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001705 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001706
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001707 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001708 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001709 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001710 return -1;
1711 }
1712
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001713 if (!(key = StringToChars(keyObject, &todecref)))
1714 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001715
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001716 if (*key == NUL)
1717 {
1718 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001719 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001720 return -1;
1721 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001722
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001723 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001724
1725 if (valObject == NULL)
1726 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001727 hashitem_T *hi;
1728
Bram Moolenaardb913952012-06-29 12:54:53 +02001729 if (di == NULL)
1730 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001731 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001732 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001733 return -1;
1734 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001735 hi = hash_find(&dict->dv_hashtab, di->di_key);
1736 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001737 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001738 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001739 return 0;
1740 }
1741
1742 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001743 {
1744 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001746 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001747
1748 if (di == NULL)
1749 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001750 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001751 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001752 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001753 PyErr_NoMemory();
1754 return -1;
1755 }
1756 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001757 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001758
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001759 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001760 {
1761 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001762 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001763 RAISE_KEY_ADD_FAIL(key);
1764 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001765 return -1;
1766 }
1767 }
1768 else
1769 clear_tv(&di->di_tv);
1770
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001771 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001772
1773 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001774 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001775 return 0;
1776}
1777
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778typedef PyObject *(*hi_to_py)(hashitem_T *);
1779
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001781DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001782{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001783 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001784 long_u todo = dict->dv_hashtab.ht_used;
1785 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001786 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001787 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001789
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001790 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001791 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1792 {
1793 if (!HASHITEM_EMPTY(hi))
1794 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001795 if (!(newObj = hiconvert(hi)))
1796 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001797 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798 return NULL;
1799 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001800 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001801 --todo;
1802 ++i;
1803 }
1804 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001805 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001806}
1807
Bram Moolenaara9922d62013-05-30 13:01:18 +02001808 static PyObject *
1809dict_key(hashitem_T *hi)
1810{
1811 return PyBytes_FromString((char *)(hi->hi_key));
1812}
1813
1814 static PyObject *
1815DictionaryListKeys(DictionaryObject *self)
1816{
1817 return DictionaryListObjects(self, dict_key);
1818}
1819
1820 static PyObject *
1821dict_val(hashitem_T *hi)
1822{
1823 dictitem_T *di;
1824
1825 di = dict_lookup(hi);
1826 return ConvertToPyObject(&di->di_tv);
1827}
1828
1829 static PyObject *
1830DictionaryListValues(DictionaryObject *self)
1831{
1832 return DictionaryListObjects(self, dict_val);
1833}
1834
1835 static PyObject *
1836dict_item(hashitem_T *hi)
1837{
1838 PyObject *keyObject;
1839 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001840 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001841
1842 if (!(keyObject = dict_key(hi)))
1843 return NULL;
1844
1845 if (!(valObject = dict_val(hi)))
1846 {
1847 Py_DECREF(keyObject);
1848 return NULL;
1849 }
1850
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001851 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001852
1853 Py_DECREF(keyObject);
1854 Py_DECREF(valObject);
1855
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001856 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857}
1858
1859 static PyObject *
1860DictionaryListItems(DictionaryObject *self)
1861{
1862 return DictionaryListObjects(self, dict_item);
1863}
1864
1865 static PyObject *
1866DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1867{
1868 dict_T *dict = self->dict;
1869
1870 if (dict->dv_lock)
1871 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001872 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001873 return NULL;
1874 }
1875
1876 if (kwargs)
1877 {
1878 typval_T tv;
1879
1880 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1881 return NULL;
1882
1883 VimTryStart();
1884 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1885 clear_tv(&tv);
1886 if (VimTryEnd())
1887 return NULL;
1888 }
1889 else
1890 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001891 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001892
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001893 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001894 return NULL;
1895
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001896 if (PyObject_HasAttrString(obj, "keys"))
1897 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001898 else
1899 {
1900 PyObject *iterator;
1901 PyObject *item;
1902
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001903 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001904 return NULL;
1905
1906 while ((item = PyIter_Next(iterator)))
1907 {
1908 PyObject *fast;
1909 PyObject *keyObject;
1910 PyObject *valObject;
1911 PyObject *todecref;
1912 char_u *key;
1913 dictitem_T *di;
1914
1915 if (!(fast = PySequence_Fast(item, "")))
1916 {
1917 Py_DECREF(iterator);
1918 Py_DECREF(item);
1919 return NULL;
1920 }
1921
1922 Py_DECREF(item);
1923
1924 if (PySequence_Fast_GET_SIZE(fast) != 2)
1925 {
1926 Py_DECREF(iterator);
1927 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001928 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001929 N_("expected sequence element of size 2, "
1930 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001931 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001932 return NULL;
1933 }
1934
1935 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1936
1937 if (!(key = StringToChars(keyObject, &todecref)))
1938 {
1939 Py_DECREF(iterator);
1940 Py_DECREF(fast);
1941 return NULL;
1942 }
1943
1944 di = dictitem_alloc(key);
1945
1946 Py_XDECREF(todecref);
1947
1948 if (di == NULL)
1949 {
1950 Py_DECREF(fast);
1951 Py_DECREF(iterator);
1952 PyErr_NoMemory();
1953 return NULL;
1954 }
1955 di->di_tv.v_lock = 0;
1956 di->di_tv.v_type = VAR_UNKNOWN;
1957
1958 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1959
1960 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1961 {
1962 Py_DECREF(iterator);
1963 Py_DECREF(fast);
1964 dictitem_free(di);
1965 return NULL;
1966 }
1967
1968 Py_DECREF(fast);
1969
1970 if (dict_add(dict, di) == FAIL)
1971 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001972 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001973 Py_DECREF(iterator);
1974 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001975 return NULL;
1976 }
1977 }
1978
1979 Py_DECREF(iterator);
1980
1981 /* Iterator may have finished due to an exception */
1982 if (PyErr_Occurred())
1983 return NULL;
1984 }
1985 }
1986 Py_INCREF(Py_None);
1987 return Py_None;
1988}
1989
1990 static PyObject *
1991DictionaryGet(DictionaryObject *self, PyObject *args)
1992{
1993 return _DictionaryItem(self, args,
1994 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1995}
1996
1997 static PyObject *
1998DictionaryPop(DictionaryObject *self, PyObject *args)
1999{
2000 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2001}
2002
2003 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002004DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002005{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002006 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002007 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002008 PyObject *valObject;
2009 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010
Bram Moolenaarde71b562013-06-02 17:41:54 +02002011 if (self->dict->dv_hashtab.ht_used == 0)
2012 {
2013 PyErr_SetNone(PyExc_KeyError);
2014 return NULL;
2015 }
2016
2017 hi = self->dict->dv_hashtab.ht_array;
2018 while (HASHITEM_EMPTY(hi))
2019 ++hi;
2020
2021 di = dict_lookup(hi);
2022
2023 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002024 return NULL;
2025
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002026 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002027 {
2028 Py_DECREF(valObject);
2029 return NULL;
2030 }
2031
2032 hash_remove(&self->dict->dv_hashtab, hi);
2033 dictitem_free(di);
2034
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002035 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002036}
2037
2038 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002039DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002040{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002041 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2042}
2043
2044static PySequenceMethods DictionaryAsSeq = {
2045 0, /* sq_length */
2046 0, /* sq_concat */
2047 0, /* sq_repeat */
2048 0, /* sq_item */
2049 0, /* sq_slice */
2050 0, /* sq_ass_item */
2051 0, /* sq_ass_slice */
2052 (objobjproc) DictionaryContains, /* sq_contains */
2053 0, /* sq_inplace_concat */
2054 0, /* sq_inplace_repeat */
2055};
2056
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002057static PyMappingMethods DictionaryAsMapping = {
2058 (lenfunc) DictionaryLength,
2059 (binaryfunc) DictionaryItem,
2060 (objobjargproc) DictionaryAssItem,
2061};
2062
Bram Moolenaardb913952012-06-29 12:54:53 +02002063static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002064 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002065 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2066 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2067 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2068 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2069 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002070 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002071 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002072 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2073 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002074};
2075
2076static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002077static PySequenceMethods ListAsSeq;
2078static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002079
2080typedef struct
2081{
2082 PyObject_HEAD
2083 list_T *list;
2084 pylinkedlist_T ref;
2085} ListObject;
2086
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002087#define NEW_LIST(list) ListNew(&ListType, list)
2088
Bram Moolenaardb913952012-06-29 12:54:53 +02002089 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002090ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002091{
2092 ListObject *self;
2093
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002094 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002095 if (self == NULL)
2096 return NULL;
2097 self->list = list;
2098 ++list->lv_refcount;
2099
2100 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2101
2102 return (PyObject *)(self);
2103}
2104
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002105 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002106py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002107{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002108 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002109
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002110 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002111 {
2112 PyErr_NoMemory();
2113 return NULL;
2114 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002115 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002116
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002117 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002118}
2119
2120 static int
2121list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2122{
2123 PyObject *iterator;
2124 PyObject *item;
2125 listitem_T *li;
2126
2127 if (!(iterator = PyObject_GetIter(obj)))
2128 return -1;
2129
2130 while ((item = PyIter_Next(iterator)))
2131 {
2132 if (!(li = listitem_alloc()))
2133 {
2134 PyErr_NoMemory();
2135 Py_DECREF(item);
2136 Py_DECREF(iterator);
2137 return -1;
2138 }
2139 li->li_tv.v_lock = 0;
2140 li->li_tv.v_type = VAR_UNKNOWN;
2141
2142 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2143 {
2144 Py_DECREF(item);
2145 Py_DECREF(iterator);
2146 listitem_free(li);
2147 return -1;
2148 }
2149
2150 Py_DECREF(item);
2151
2152 list_append(l, li);
2153 }
2154
2155 Py_DECREF(iterator);
2156
2157 /* Iterator may have finished due to an exception */
2158 if (PyErr_Occurred())
2159 return -1;
2160
2161 return 0;
2162}
2163
2164 static PyObject *
2165ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2166{
2167 list_T *list;
2168 PyObject *obj = NULL;
2169
2170 if (kwargs)
2171 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002172 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002173 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002174 return NULL;
2175 }
2176
2177 if (!PyArg_ParseTuple(args, "|O", &obj))
2178 return NULL;
2179
2180 if (!(list = py_list_alloc()))
2181 return NULL;
2182
2183 if (obj)
2184 {
2185 PyObject *lookup_dict;
2186
2187 if (!(lookup_dict = PyDict_New()))
2188 {
2189 list_unref(list);
2190 return NULL;
2191 }
2192
2193 if (list_py_concat(list, obj, lookup_dict) == -1)
2194 {
2195 Py_DECREF(lookup_dict);
2196 list_unref(list);
2197 return NULL;
2198 }
2199
2200 Py_DECREF(lookup_dict);
2201 }
2202
2203 return ListNew(subtype, list);
2204}
2205
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002206 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002207ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002208{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002209 pyll_remove(&self->ref, &lastlist);
2210 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002211
2212 DESTRUCTOR_FINISH(self);
2213}
2214
Bram Moolenaardb913952012-06-29 12:54:53 +02002215 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002216ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002217{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002218 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002219}
2220
2221 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002222ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002223{
2224 listitem_T *li;
2225
Bram Moolenaard6e39182013-05-21 18:30:34 +02002226 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002227 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002228 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002229 return NULL;
2230 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002231 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002232 if (li == NULL)
2233 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002234 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002235 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002236 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002237 return NULL;
2238 }
2239 return ConvertToPyObject(&li->li_tv);
2240}
2241
2242#define PROC_RANGE \
2243 if (last < 0) {\
2244 if (last < -size) \
2245 last = 0; \
2246 else \
2247 last += size; \
2248 } \
2249 if (first < 0) \
2250 first = 0; \
2251 if (first > size) \
2252 first = size; \
2253 if (last > size) \
2254 last = size;
2255
2256 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002257ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002258{
2259 PyInt i;
2260 PyInt size = ListLength(self);
2261 PyInt n;
2262 PyObject *list;
2263 int reversed = 0;
2264
2265 PROC_RANGE
2266 if (first >= last)
2267 first = last;
2268
2269 n = last-first;
2270 list = PyList_New(n);
2271 if (list == NULL)
2272 return NULL;
2273
2274 for (i = 0; i < n; ++i)
2275 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002276 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002277 if (item == NULL)
2278 {
2279 Py_DECREF(list);
2280 return NULL;
2281 }
2282
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002283 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002284 }
2285
2286 return list;
2287}
2288
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002289typedef struct
2290{
2291 listwatch_T lw;
2292 list_T *list;
2293} listiterinfo_T;
2294
2295 static void
2296ListIterDestruct(listiterinfo_T *lii)
2297{
2298 list_rem_watch(lii->list, &lii->lw);
2299 PyMem_Free(lii);
2300}
2301
2302 static PyObject *
2303ListIterNext(listiterinfo_T **lii)
2304{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002305 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002306
2307 if (!((*lii)->lw.lw_item))
2308 return NULL;
2309
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002310 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002311 return NULL;
2312
2313 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2314
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002315 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002316}
2317
2318 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002319ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002320{
2321 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002322 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002323
2324 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2325 {
2326 PyErr_NoMemory();
2327 return NULL;
2328 }
2329
2330 list_add_watch(l, &lii->lw);
2331 lii->lw.lw_item = l->lv_first;
2332 lii->list = l;
2333
2334 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002335 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2336 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002337}
2338
Bram Moolenaardb913952012-06-29 12:54:53 +02002339 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002340ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002341{
2342 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002343 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002344 listitem_T *li;
2345 Py_ssize_t length = ListLength(self);
2346
2347 if (l->lv_lock)
2348 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002349 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002350 return -1;
2351 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002352 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002353 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002354 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002355 return -1;
2356 }
2357
2358 if (obj == NULL)
2359 {
2360 li = list_find(l, (long) index);
2361 list_remove(l, li, li);
2362 clear_tv(&li->li_tv);
2363 vim_free(li);
2364 return 0;
2365 }
2366
2367 if (ConvertFromPyObject(obj, &tv) == -1)
2368 return -1;
2369
2370 if (index == length)
2371 {
2372 if (list_append_tv(l, &tv) == FAIL)
2373 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002374 clear_tv(&tv);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002375 PyErr_SET_VIM(N_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002376 return -1;
2377 }
2378 }
2379 else
2380 {
2381 li = list_find(l, (long) index);
2382 clear_tv(&li->li_tv);
2383 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002384 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002385 }
2386 return 0;
2387}
2388
2389 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002390ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002391{
2392 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002393 PyObject *iterator;
2394 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002395 listitem_T *li;
2396 listitem_T *next;
2397 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002398 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002399 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002400
2401 if (l->lv_lock)
2402 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002403 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002404 return -1;
2405 }
2406
2407 PROC_RANGE
2408
2409 if (first == size)
2410 li = NULL;
2411 else
2412 {
2413 li = list_find(l, (long) first);
2414 if (li == NULL)
2415 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002416 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2417 (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002418 return -1;
2419 }
2420 if (last > first)
2421 {
2422 i = last - first;
2423 while (i-- && li != NULL)
2424 {
2425 next = li->li_next;
2426 listitem_remove(l, li);
2427 li = next;
2428 }
2429 }
2430 }
2431
2432 if (obj == NULL)
2433 return 0;
2434
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002435 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002436 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002437
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002438 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002439 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002440 if (ConvertFromPyObject(item, &v) == -1)
2441 {
2442 Py_DECREF(iterator);
2443 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002444 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002445 }
2446 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002447 if (list_insert_tv(l, &v, li) == FAIL)
2448 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002449 clear_tv(&v);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002450 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002451 return -1;
2452 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002453 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002454 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002455 Py_DECREF(iterator);
Bram Moolenaardee2e312013-06-23 16:35:47 +02002456
2457 if (PyErr_Occurred())
2458 return -1;
2459
Bram Moolenaardb913952012-06-29 12:54:53 +02002460 return 0;
2461}
2462
2463 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002464ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002465{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002466 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002467 PyObject *lookup_dict;
2468
2469 if (l->lv_lock)
2470 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002471 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002472 return NULL;
2473 }
2474
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002475 if (!(lookup_dict = PyDict_New()))
2476 return NULL;
2477
Bram Moolenaardb913952012-06-29 12:54:53 +02002478 if (list_py_concat(l, obj, lookup_dict) == -1)
2479 {
2480 Py_DECREF(lookup_dict);
2481 return NULL;
2482 }
2483 Py_DECREF(lookup_dict);
2484
2485 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002486 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002487}
2488
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002489static char *ListAttrs[] = {
2490 "locked",
2491 NULL
2492};
2493
2494 static PyObject *
2495ListDir(PyObject *self)
2496{
2497 return ObjectDir(self, ListAttrs);
2498}
2499
Bram Moolenaar66b79852012-09-21 14:00:35 +02002500 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002501ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002502{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002503 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002504 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002505 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002506 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002507 return -1;
2508 }
2509
2510 if (strcmp(name, "locked") == 0)
2511 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002512 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002513 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002514 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002515 return -1;
2516 }
2517 else
2518 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002519 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002520 if (istrue == -1)
2521 return -1;
2522 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002523 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002524 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002525 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002526 }
2527 return 0;
2528 }
2529 else
2530 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002531 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002532 return -1;
2533 }
2534}
2535
Bram Moolenaardb913952012-06-29 12:54:53 +02002536static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002537 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2538 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2539 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002540};
2541
2542typedef struct
2543{
2544 PyObject_HEAD
2545 char_u *name;
2546} FunctionObject;
2547
2548static PyTypeObject FunctionType;
2549
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002550#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2551
Bram Moolenaardb913952012-06-29 12:54:53 +02002552 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002553FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002554{
2555 FunctionObject *self;
2556
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002557 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2558
Bram Moolenaardb913952012-06-29 12:54:53 +02002559 if (self == NULL)
2560 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002561
2562 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002563 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002564 if (!translated_function_exists(name))
2565 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002566 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002567 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002568 return NULL;
2569 }
2570 self->name = vim_strsave(name);
2571 func_ref(self->name);
2572 }
2573 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002574 if ((self->name = get_expanded_name(name,
2575 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2576 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002577 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002578 PyErr_FORMAT(PyExc_ValueError,
2579 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002580 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002581 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002582
2583 return (PyObject *)(self);
2584}
2585
2586 static PyObject *
2587FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2588{
2589 PyObject *self;
2590 char_u *name;
2591
2592 if (kwargs)
2593 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002594 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002595 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002596 return NULL;
2597 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002598
Bram Moolenaar389a1792013-06-23 13:00:44 +02002599 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002600 return NULL;
2601
2602 self = FunctionNew(subtype, name);
2603
Bram Moolenaar389a1792013-06-23 13:00:44 +02002604 PyMem_Free(name);
2605
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002606 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002607}
2608
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002609 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002610FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002611{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002612 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002613 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002614
2615 DESTRUCTOR_FINISH(self);
2616}
2617
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002618static char *FunctionAttrs[] = {
2619 "softspace",
2620 NULL
2621};
2622
2623 static PyObject *
2624FunctionDir(PyObject *self)
2625{
2626 return ObjectDir(self, FunctionAttrs);
2627}
2628
Bram Moolenaardb913952012-06-29 12:54:53 +02002629 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002630FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002631{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002632 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002633 typval_T args;
2634 typval_T selfdicttv;
2635 typval_T rettv;
2636 dict_T *selfdict = NULL;
2637 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002638 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002639 int error;
2640
2641 if (ConvertFromPyObject(argsObject, &args) == -1)
2642 return NULL;
2643
2644 if (kwargs != NULL)
2645 {
2646 selfdictObject = PyDict_GetItemString(kwargs, "self");
2647 if (selfdictObject != NULL)
2648 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002649 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002650 {
2651 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002652 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002653 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002654 selfdict = selfdicttv.vval.v_dict;
2655 }
2656 }
2657
Bram Moolenaar71700b82013-05-15 17:49:05 +02002658 Py_BEGIN_ALLOW_THREADS
2659 Python_Lock_Vim();
2660
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002661 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002662 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002663
2664 Python_Release_Vim();
2665 Py_END_ALLOW_THREADS
2666
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002667 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002668 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002669 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002670 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002671 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002672 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002673 }
2674 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002675 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002676
Bram Moolenaardb913952012-06-29 12:54:53 +02002677 clear_tv(&args);
2678 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002679 if (selfdict != NULL)
2680 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002681
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002682 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002683}
2684
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002685 static PyObject *
2686FunctionRepr(FunctionObject *self)
2687{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002688#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002689 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002690 * Finalize */
2691 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002692 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002693#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002694 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002695#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002696}
2697
Bram Moolenaardb913952012-06-29 12:54:53 +02002698static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002699 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2700 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002701};
2702
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002703/*
2704 * Options object
2705 */
2706
2707static PyTypeObject OptionsType;
2708
2709typedef int (*checkfun)(void *);
2710
2711typedef struct
2712{
2713 PyObject_HEAD
2714 int opt_type;
2715 void *from;
2716 checkfun Check;
2717 PyObject *fromObj;
2718} OptionsObject;
2719
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002720 static int
2721dummy_check(void *arg UNUSED)
2722{
2723 return 0;
2724}
2725
2726 static PyObject *
2727OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2728{
2729 OptionsObject *self;
2730
Bram Moolenaar774267b2013-05-21 20:51:59 +02002731 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002732 if (self == NULL)
2733 return NULL;
2734
2735 self->opt_type = opt_type;
2736 self->from = from;
2737 self->Check = Check;
2738 self->fromObj = fromObj;
2739 if (fromObj)
2740 Py_INCREF(fromObj);
2741
2742 return (PyObject *)(self);
2743}
2744
2745 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002747{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002748 PyObject_GC_UnTrack((void *)(self));
2749 Py_XDECREF(self->fromObj);
2750 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002751}
2752
2753 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002754OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002755{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002756 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002757 return 0;
2758}
2759
2760 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002761OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002762{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002763 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002764 return 0;
2765}
2766
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002767 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002768OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002769{
2770 char_u *key;
2771 int flags;
2772 long numval;
2773 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002774 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002775
Bram Moolenaard6e39182013-05-21 18:30:34 +02002776 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002777 return NULL;
2778
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002779 if (!(key = StringToChars(keyObject, &todecref)))
2780 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002781
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002782 if (*key == NUL)
2783 {
2784 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002785 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002786 return NULL;
2787 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002788
2789 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002790 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002791
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002792 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002793
2794 if (flags == 0)
2795 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002796 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002797 return NULL;
2798 }
2799
2800 if (flags & SOPT_UNSET)
2801 {
2802 Py_INCREF(Py_None);
2803 return Py_None;
2804 }
2805 else if (flags & SOPT_BOOL)
2806 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002807 PyObject *ret;
2808 ret = numval ? Py_True : Py_False;
2809 Py_INCREF(ret);
2810 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002811 }
2812 else if (flags & SOPT_NUM)
2813 return PyInt_FromLong(numval);
2814 else if (flags & SOPT_STRING)
2815 {
2816 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002817 {
Bram Moolenaar41009372013-07-01 22:03:04 +02002818 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002819 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002820 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002821 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002822 else
2823 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002824 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002825 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002826 return NULL;
2827 }
2828 }
2829 else
2830 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002831 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002832 return NULL;
2833 }
2834}
2835
2836 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002837set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002838{
2839 char_u *errmsg;
2840
2841 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2842 {
2843 if (VimTryEnd())
2844 return FAIL;
2845 PyErr_SetVim((char *)errmsg);
2846 return FAIL;
2847 }
2848 return OK;
2849}
2850
2851 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002852set_option_value_for(
2853 char_u *key,
2854 int numval,
2855 char_u *stringval,
2856 int opt_flags,
2857 int opt_type,
2858 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002859{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002860 win_T *save_curwin = NULL;
2861 tabpage_T *save_curtab = NULL;
2862 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002863 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002864
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002865 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002866 switch (opt_type)
2867 {
2868 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002869 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002870 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002871 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002872 if (VimTryEnd())
2873 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002874 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002875 return -1;
2876 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002877 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2878 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002879 break;
2880 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002881 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002882 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002883 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002884 break;
2885 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002886 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002887 break;
2888 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002889 if (set_ret == FAIL)
2890 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002891 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002892}
2893
2894 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002896{
2897 char_u *key;
2898 int flags;
2899 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002900 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002901 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002902
Bram Moolenaard6e39182013-05-21 18:30:34 +02002903 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002904 return -1;
2905
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002906 if (!(key = StringToChars(keyObject, &todecref)))
2907 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002908
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002909 if (*key == NUL)
2910 {
2911 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002912 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002913 return -1;
2914 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002915
2916 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002917 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002918
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002919 if (flags == 0)
2920 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002921 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002922 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002923 return -1;
2924 }
2925
2926 if (valObject == NULL)
2927 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002928 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002929 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002930 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002931 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002932 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002933 return -1;
2934 }
2935 else if (!(flags & SOPT_GLOBAL))
2936 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002937 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002938 N_("unable to unset option %s "
2939 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002940 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002941 return -1;
2942 }
2943 else
2944 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002945 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002946 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002947 return 0;
2948 }
2949 }
2950
Bram Moolenaard6e39182013-05-21 18:30:34 +02002951 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002952
2953 if (flags & SOPT_BOOL)
2954 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002955 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002956
Bram Moolenaarb983f752013-05-15 16:11:50 +02002957 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002958 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002959 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002960 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002961 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002962 }
2963 else if (flags & SOPT_NUM)
2964 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002965 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002966
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002967 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002968 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002969 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002970 return -1;
2971 }
2972
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002973 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002974 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002975 }
2976 else
2977 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002978 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002979 PyObject *todecref;
2980
2981 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002982 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002983 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002984 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002985 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002986 }
2987
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002988 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002989
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002990 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002991}
2992
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002993static PyMappingMethods OptionsAsMapping = {
2994 (lenfunc) NULL,
2995 (binaryfunc) OptionsItem,
2996 (objobjargproc) OptionsAssItem,
2997};
2998
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002999/* Tabpage object
3000 */
3001
3002typedef struct
3003{
3004 PyObject_HEAD
3005 tabpage_T *tab;
3006} TabPageObject;
3007
3008static PyObject *WinListNew(TabPageObject *tabObject);
3009
3010static PyTypeObject TabPageType;
3011
3012 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003013CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003014{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003015 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003016 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003017 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003018 return -1;
3019 }
3020
3021 return 0;
3022}
3023
3024 static PyObject *
3025TabPageNew(tabpage_T *tab)
3026{
3027 TabPageObject *self;
3028
3029 if (TAB_PYTHON_REF(tab))
3030 {
3031 self = TAB_PYTHON_REF(tab);
3032 Py_INCREF(self);
3033 }
3034 else
3035 {
3036 self = PyObject_NEW(TabPageObject, &TabPageType);
3037 if (self == NULL)
3038 return NULL;
3039 self->tab = tab;
3040 TAB_PYTHON_REF(tab) = self;
3041 }
3042
3043 return (PyObject *)(self);
3044}
3045
3046 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003047TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003048{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003049 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3050 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003051
3052 DESTRUCTOR_FINISH(self);
3053}
3054
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003055static char *TabPageAttrs[] = {
3056 "windows", "number", "vars", "window", "valid",
3057 NULL
3058};
3059
3060 static PyObject *
3061TabPageDir(PyObject *self)
3062{
3063 return ObjectDir(self, TabPageAttrs);
3064}
3065
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003066 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003067TabPageAttrValid(TabPageObject *self, char *name)
3068{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003069 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003070
3071 if (strcmp(name, "valid") != 0)
3072 return NULL;
3073
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003074 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3075 Py_INCREF(ret);
3076 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003077}
3078
3079 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003080TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003081{
3082 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003083 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003084 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003085 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003086 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003087 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003088 else if (strcmp(name, "window") == 0)
3089 {
3090 /* For current tab window.c does not bother to set or update tp_curwin
3091 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003092 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003093 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003094 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003095 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003096 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003097 else if (strcmp(name, "__members__") == 0)
3098 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003099 return NULL;
3100}
3101
3102 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003103TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003104{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003105 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003106 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003107 else
3108 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003109 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003110
3111 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003112 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3113 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003114 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003115 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003116 }
3117}
3118
3119static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003120 /* name, function, calling, documentation */
3121 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3122 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003123};
3124
3125/*
3126 * Window list object
3127 */
3128
3129static PyTypeObject TabListType;
3130static PySequenceMethods TabListAsSeq;
3131
3132typedef struct
3133{
3134 PyObject_HEAD
3135} TabListObject;
3136
3137 static PyInt
3138TabListLength(PyObject *self UNUSED)
3139{
3140 tabpage_T *tp = first_tabpage;
3141 PyInt n = 0;
3142
3143 while (tp != NULL)
3144 {
3145 ++n;
3146 tp = tp->tp_next;
3147 }
3148
3149 return n;
3150}
3151
3152 static PyObject *
3153TabListItem(PyObject *self UNUSED, PyInt n)
3154{
3155 tabpage_T *tp;
3156
3157 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3158 if (n == 0)
3159 return TabPageNew(tp);
3160
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003161 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003162 return NULL;
3163}
3164
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003165/*
3166 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003167 */
3168
3169typedef struct
3170{
3171 PyObject_HEAD
3172 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003173 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003174} WindowObject;
3175
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003176static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003177
3178 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003179CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003180{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003181 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003182 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003183 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003184 return -1;
3185 }
3186
3187 return 0;
3188}
3189
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003190 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003191WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003192{
3193 /* We need to handle deletion of windows underneath us.
3194 * If we add a "w_python*_ref" field to the win_T structure,
3195 * then we can get at it in win_free() in vim. We then
3196 * need to create only ONE Python object per window - if
3197 * we try to create a second, just INCREF the existing one
3198 * and return it. The (single) Python object referring to
3199 * the window is stored in "w_python*_ref".
3200 * On a win_free() we set the Python object's win_T* field
3201 * to an invalid value. We trap all uses of a window
3202 * object, and reject them if the win_T* field is invalid.
3203 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003204 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003205 * w_python_ref and w_python3_ref fields respectively.
3206 */
3207
3208 WindowObject *self;
3209
3210 if (WIN_PYTHON_REF(win))
3211 {
3212 self = WIN_PYTHON_REF(win);
3213 Py_INCREF(self);
3214 }
3215 else
3216 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003217 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003218 if (self == NULL)
3219 return NULL;
3220 self->win = win;
3221 WIN_PYTHON_REF(win) = self;
3222 }
3223
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003224 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3225
Bram Moolenaar971db462013-05-12 18:44:48 +02003226 return (PyObject *)(self);
3227}
3228
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003229 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003230WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003231{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003232 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003233 if (self->win && self->win != INVALID_WINDOW_VALUE)
3234 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003235 Py_XDECREF(((PyObject *)(self->tabObject)));
3236 PyObject_GC_Del((void *)(self));
3237}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003238
Bram Moolenaar774267b2013-05-21 20:51:59 +02003239 static int
3240WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3241{
3242 Py_VISIT(((PyObject *)(self->tabObject)));
3243 return 0;
3244}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003245
Bram Moolenaar774267b2013-05-21 20:51:59 +02003246 static int
3247WindowClear(WindowObject *self)
3248{
3249 Py_CLEAR(self->tabObject);
3250 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003251}
3252
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003253 static win_T *
3254get_firstwin(TabPageObject *tabObject)
3255{
3256 if (tabObject)
3257 {
3258 if (CheckTabPage(tabObject))
3259 return NULL;
3260 /* For current tab window.c does not bother to set or update tp_firstwin
3261 */
3262 else if (tabObject->tab == curtab)
3263 return firstwin;
3264 else
3265 return tabObject->tab->tp_firstwin;
3266 }
3267 else
3268 return firstwin;
3269}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003270static char *WindowAttrs[] = {
3271 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3272 "tabpage", "valid",
3273 NULL
3274};
3275
3276 static PyObject *
3277WindowDir(PyObject *self)
3278{
3279 return ObjectDir(self, WindowAttrs);
3280}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003281
Bram Moolenaar971db462013-05-12 18:44:48 +02003282 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003283WindowAttrValid(WindowObject *self, char *name)
3284{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003285 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003286
3287 if (strcmp(name, "valid") != 0)
3288 return NULL;
3289
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003290 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3291 Py_INCREF(ret);
3292 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003293}
3294
3295 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003296WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003297{
3298 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003299 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003300 else if (strcmp(name, "cursor") == 0)
3301 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003302 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003303
3304 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3305 }
3306 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003307 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003308#ifdef FEAT_WINDOWS
3309 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003310 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003311#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003312#ifdef FEAT_VERTSPLIT
3313 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003314 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003315 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003316 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003317#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003318 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003319 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003320 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003321 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3322 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003323 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003324 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003325 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003326 return NULL;
3327 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003328 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003329 }
3330 else if (strcmp(name, "tabpage") == 0)
3331 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003332 Py_INCREF(self->tabObject);
3333 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003334 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003335 else if (strcmp(name, "__members__") == 0)
3336 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003337 else
3338 return NULL;
3339}
3340
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003341 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003342WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003343{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003345 return -1;
3346
3347 if (strcmp(name, "buffer") == 0)
3348 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003349 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003350 return -1;
3351 }
3352 else if (strcmp(name, "cursor") == 0)
3353 {
3354 long lnum;
3355 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003356
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003357 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003358 return -1;
3359
Bram Moolenaard6e39182013-05-21 18:30:34 +02003360 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003361 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003362 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003363 return -1;
3364 }
3365
3366 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003367 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003368 return -1;
3369
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370 self->win->w_cursor.lnum = lnum;
3371 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003372#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003374#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003375 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003376 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003377
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003378 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003379 return 0;
3380 }
3381 else if (strcmp(name, "height") == 0)
3382 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003383 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003384 win_T *savewin;
3385
Bram Moolenaardee2e312013-06-23 16:35:47 +02003386 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003387 return -1;
3388
3389#ifdef FEAT_GUI
3390 need_mouse_correct = TRUE;
3391#endif
3392 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003393 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003394
3395 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003396 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003397 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003398 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003399 return -1;
3400
3401 return 0;
3402 }
3403#ifdef FEAT_VERTSPLIT
3404 else if (strcmp(name, "width") == 0)
3405 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003406 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003407 win_T *savewin;
3408
Bram Moolenaardee2e312013-06-23 16:35:47 +02003409 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003410 return -1;
3411
3412#ifdef FEAT_GUI
3413 need_mouse_correct = TRUE;
3414#endif
3415 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003416 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003417
3418 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003419 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003420 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003421 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003422 return -1;
3423
3424 return 0;
3425 }
3426#endif
3427 else
3428 {
3429 PyErr_SetString(PyExc_AttributeError, name);
3430 return -1;
3431 }
3432}
3433
3434 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003435WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003436{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003437 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003438 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003439 else
3440 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003441 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003442
Bram Moolenaar6d216452013-05-12 19:00:41 +02003443 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003444 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003445 (self));
3446 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003447 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003448 }
3449}
3450
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003451static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003452 /* name, function, calling, documentation */
3453 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3454 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003455};
3456
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003457/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003458 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003459 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003460
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003461static PyTypeObject WinListType;
3462static PySequenceMethods WinListAsSeq;
3463
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003464typedef struct
3465{
3466 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003467 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003468} WinListObject;
3469
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003470 static PyObject *
3471WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003472{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003473 WinListObject *self;
3474
3475 self = PyObject_NEW(WinListObject, &WinListType);
3476 self->tabObject = tabObject;
3477 Py_INCREF(tabObject);
3478
3479 return (PyObject *)(self);
3480}
3481
3482 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003483WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003484{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003485 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003486
3487 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003488 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003489 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003490 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003491
3492 DESTRUCTOR_FINISH(self);
3493}
3494
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003495 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003496WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003497{
3498 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003499 PyInt n = 0;
3500
Bram Moolenaard6e39182013-05-21 18:30:34 +02003501 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003502 return -1;
3503
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003504 while (w != NULL)
3505 {
3506 ++n;
3507 w = W_NEXT(w);
3508 }
3509
3510 return n;
3511}
3512
3513 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003514WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003515{
3516 win_T *w;
3517
Bram Moolenaard6e39182013-05-21 18:30:34 +02003518 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003519 return NULL;
3520
3521 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003522 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003523 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003524
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003525 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003526 return NULL;
3527}
3528
3529/* Convert a Python string into a Vim line.
3530 *
3531 * The result is in allocated memory. All internal nulls are replaced by
3532 * newline characters. It is an error for the string to contain newline
3533 * characters.
3534 *
3535 * On errors, the Python exception data is set, and NULL is returned.
3536 */
3537 static char *
3538StringToLine(PyObject *obj)
3539{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003540 char *str;
3541 char *save;
3542 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003543 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003544 PyInt i;
3545 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003546
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003547 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003548 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003549 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3550 || str == NULL)
3551 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003552 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003553 else if (PyUnicode_Check(obj))
3554 {
3555 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3556 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003557
Bram Moolenaardaa27022013-06-24 22:33:30 +02003558 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003559 || str == NULL)
3560 {
3561 Py_DECREF(bytes);
3562 return NULL;
3563 }
3564 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003565 else
3566 {
3567#if PY_MAJOR_VERSION < 3
3568 PyErr_FORMAT(PyExc_TypeError,
3569 N_("expected str() or unicode() instance, but got %s"),
3570 Py_TYPE_NAME(obj));
3571#else
3572 PyErr_FORMAT(PyExc_TypeError,
3573 N_("expected bytes() or str() instance, but got %s"),
3574 Py_TYPE_NAME(obj));
3575#endif
3576 return NULL;
3577 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003578
3579 /*
3580 * Error checking: String must not contain newlines, as we
3581 * are replacing a single line, and we must replace it with
3582 * a single line.
3583 * A trailing newline is removed, so that append(f.readlines()) works.
3584 */
3585 p = memchr(str, '\n', len);
3586 if (p != NULL)
3587 {
3588 if (p == str + len - 1)
3589 --len;
3590 else
3591 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003592 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003593 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003594 return NULL;
3595 }
3596 }
3597
3598 /* Create a copy of the string, with internal nulls replaced by
3599 * newline characters, as is the vim convention.
3600 */
3601 save = (char *)alloc((unsigned)(len+1));
3602 if (save == NULL)
3603 {
3604 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003605 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003606 return NULL;
3607 }
3608
3609 for (i = 0; i < len; ++i)
3610 {
3611 if (str[i] == '\0')
3612 save[i] = '\n';
3613 else
3614 save[i] = str[i];
3615 }
3616
3617 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003618 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003619
3620 return save;
3621}
3622
3623/* Get a line from the specified buffer. The line number is
3624 * in Vim format (1-based). The line is returned as a Python
3625 * string object.
3626 */
3627 static PyObject *
3628GetBufferLine(buf_T *buf, PyInt n)
3629{
3630 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3631}
3632
3633
3634/* Get a list of lines from the specified buffer. The line numbers
3635 * are in Vim format (1-based). The range is from lo up to, but not
3636 * including, hi. The list is returned as a Python list of string objects.
3637 */
3638 static PyObject *
3639GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3640{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003641 PyInt i;
3642 PyInt n = hi - lo;
3643 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003644
3645 if (list == NULL)
3646 return NULL;
3647
3648 for (i = 0; i < n; ++i)
3649 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003650 PyObject *string = LineToString(
3651 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003652
3653 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003654 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003655 {
3656 Py_DECREF(list);
3657 return NULL;
3658 }
3659
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003660 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003661 }
3662
3663 /* The ownership of the Python list is passed to the caller (ie,
3664 * the caller should Py_DECREF() the object when it is finished
3665 * with it).
3666 */
3667
3668 return list;
3669}
3670
3671/*
3672 * Check if deleting lines made the cursor position invalid.
3673 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3674 * deleted).
3675 */
3676 static void
3677py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3678{
3679 if (curwin->w_cursor.lnum >= lo)
3680 {
3681 /* Adjust the cursor position if it's in/after the changed
3682 * lines. */
3683 if (curwin->w_cursor.lnum >= hi)
3684 {
3685 curwin->w_cursor.lnum += extra;
3686 check_cursor_col();
3687 }
3688 else if (extra < 0)
3689 {
3690 curwin->w_cursor.lnum = lo;
3691 check_cursor();
3692 }
3693 else
3694 check_cursor_col();
3695 changed_cline_bef_curs();
3696 }
3697 invalidate_botline();
3698}
3699
Bram Moolenaar19e60942011-06-19 00:27:51 +02003700/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003701 * Find a window that contains "buf" and switch to it.
3702 * If there is no such window, use the current window and change "curbuf".
3703 * Caller must initialize save_curbuf to NULL.
3704 * restore_win_for_buf() MUST be called later!
3705 */
3706 static void
3707switch_to_win_for_buf(
3708 buf_T *buf,
3709 win_T **save_curwinp,
3710 tabpage_T **save_curtabp,
3711 buf_T **save_curbufp)
3712{
3713 win_T *wp;
3714 tabpage_T *tp;
3715
3716 if (find_win_for_buf(buf, &wp, &tp) == FAIL
3717 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
3718 switch_buffer(save_curbufp, buf);
3719}
3720
3721 static void
3722restore_win_for_buf(
3723 win_T *save_curwin,
3724 tabpage_T *save_curtab,
3725 buf_T *save_curbuf)
3726{
3727 if (save_curbuf == NULL)
3728 restore_win(save_curwin, save_curtab, TRUE);
3729 else
3730 restore_buffer(save_curbuf);
3731}
3732
3733/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02003734 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003735 * in Vim format (1-based). The replacement line is given as
3736 * a Python string object. The object is checked for validity
3737 * and correct format. Errors are returned as a value of FAIL.
3738 * The return value is OK on success.
3739 * If OK is returned and len_change is not NULL, *len_change
3740 * is set to the change in the buffer length.
3741 */
3742 static int
3743SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3744{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003745 buf_T *save_curbuf = NULL;
3746 win_T *save_curwin = NULL;
3747 tabpage_T *save_curtab = NULL;
3748
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003749 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003750 * There are three cases:
3751 * 1. NULL, or None - this is a deletion.
3752 * 2. A string - this is a replacement.
3753 * 3. Anything else - this is an error.
3754 */
3755 if (line == Py_None || line == NULL)
3756 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003757 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003758 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003759
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003760 VimTryStart();
3761
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003762 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003763 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003764 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003765 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003766 else
3767 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003768 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003769 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003770 if (save_curbuf == NULL)
3771 /* Only adjust marks if we managed to switch to a window that
3772 * holds the buffer, otherwise line numbers will be invalid. */
3773 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003774 }
3775
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003776 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003777
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 = -1;
3783
3784 return OK;
3785 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003786 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003787 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003788 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003789
3790 if (save == NULL)
3791 return FAIL;
3792
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003793 VimTryStart();
3794
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003795 /* We do not need to free "save" if ml_replace() consumes it. */
3796 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003797 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003798
3799 if (u_savesub((linenr_T)n) == FAIL)
3800 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003801 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003802 vim_free(save);
3803 }
3804 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3805 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003806 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003807 vim_free(save);
3808 }
3809 else
3810 changed_bytes((linenr_T)n, 0);
3811
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003812 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003813
3814 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003815 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003816 check_cursor_col();
3817
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003818 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003819 return FAIL;
3820
3821 if (len_change)
3822 *len_change = 0;
3823
3824 return OK;
3825 }
3826 else
3827 {
3828 PyErr_BadArgument();
3829 return FAIL;
3830 }
3831}
3832
Bram Moolenaar19e60942011-06-19 00:27:51 +02003833/* Replace a range of lines in the specified buffer. The line numbers are in
3834 * Vim format (1-based). The range is from lo up to, but not including, hi.
3835 * The replacement lines are given as a Python list of string objects. The
3836 * list is checked for validity and correct format. Errors are returned as a
3837 * value of FAIL. The return value is OK on success.
3838 * If OK is returned and len_change is not NULL, *len_change
3839 * is set to the change in the buffer length.
3840 */
3841 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003842SetBufferLineList(
3843 buf_T *buf,
3844 PyInt lo,
3845 PyInt hi,
3846 PyObject *list,
3847 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003848{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003849 buf_T *save_curbuf = NULL;
3850 win_T *save_curwin = NULL;
3851 tabpage_T *save_curtab = NULL;
3852
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003853 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003854 * There are three cases:
3855 * 1. NULL, or None - this is a deletion.
3856 * 2. A list - this is a replacement.
3857 * 3. Anything else - this is an error.
3858 */
3859 if (list == Py_None || list == NULL)
3860 {
3861 PyInt i;
3862 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003863
3864 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003865 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003866 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003867
3868 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003869 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003870 else
3871 {
3872 for (i = 0; i < n; ++i)
3873 {
3874 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3875 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003876 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003877 break;
3878 }
3879 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003880 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003881 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003882 if (save_curbuf == NULL)
3883 /* Only adjust marks if we managed to switch to a window that
3884 * holds the buffer, otherwise line numbers will be invalid. */
3885 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003886 }
3887
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003888 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003889
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003890 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003891 return FAIL;
3892
3893 if (len_change)
3894 *len_change = -n;
3895
3896 return OK;
3897 }
3898 else if (PyList_Check(list))
3899 {
3900 PyInt i;
3901 PyInt new_len = PyList_Size(list);
3902 PyInt old_len = hi - lo;
3903 PyInt extra = 0; /* lines added to text, can be negative */
3904 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003905
3906 if (new_len == 0) /* avoid allocating zero bytes */
3907 array = NULL;
3908 else
3909 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003910 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003911 if (array == NULL)
3912 {
3913 PyErr_NoMemory();
3914 return FAIL;
3915 }
3916 }
3917
3918 for (i = 0; i < new_len; ++i)
3919 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003920 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003921
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003922 if (!(line = PyList_GetItem(list, i)) ||
3923 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003924 {
3925 while (i)
3926 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003927 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003928 return FAIL;
3929 }
3930 }
3931
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003932 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003933 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003934
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003935 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003936 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003937
3938 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003939 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003940
3941 /* If the size of the range is reducing (ie, new_len < old_len) we
3942 * need to delete some old_len. We do this at the start, by
3943 * repeatedly deleting line "lo".
3944 */
3945 if (!PyErr_Occurred())
3946 {
3947 for (i = 0; i < old_len - new_len; ++i)
3948 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3949 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003950 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003951 break;
3952 }
3953 extra -= i;
3954 }
3955
3956 /* For as long as possible, replace the existing old_len with the
3957 * new old_len. This is a more efficient operation, as it requires
3958 * less memory allocation and freeing.
3959 */
3960 if (!PyErr_Occurred())
3961 {
3962 for (i = 0; i < old_len && i < new_len; ++i)
3963 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3964 == FAIL)
3965 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003966 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003967 break;
3968 }
3969 }
3970 else
3971 i = 0;
3972
3973 /* Now we may need to insert the remaining new old_len. If we do, we
3974 * must free the strings as we finish with them (we can't pass the
3975 * responsibility to vim in this case).
3976 */
3977 if (!PyErr_Occurred())
3978 {
3979 while (i < new_len)
3980 {
3981 if (ml_append((linenr_T)(lo + i - 1),
3982 (char_u *)array[i], 0, FALSE) == FAIL)
3983 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003984 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003985 break;
3986 }
3987 vim_free(array[i]);
3988 ++i;
3989 ++extra;
3990 }
3991 }
3992
3993 /* Free any left-over old_len, as a result of an error */
3994 while (i < new_len)
3995 {
3996 vim_free(array[i]);
3997 ++i;
3998 }
3999
4000 /* Free the array of old_len. All of its contents have now
4001 * been dealt with (either freed, or the responsibility passed
4002 * to vim.
4003 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004004 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004005
4006 /* Adjust marks. Invalidate any which lie in the
4007 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004008 * Only adjust marks if we managed to switch to a window that holds
4009 * the buffer, otherwise line numbers will be invalid. */
4010 if (save_curbuf == NULL)
4011 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004012 (long)MAXLNUM, (long)extra);
4013 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4014
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004015 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004016 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4017
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004018 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004019 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004020
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004021 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004022 return FAIL;
4023
4024 if (len_change)
4025 *len_change = new_len - old_len;
4026
4027 return OK;
4028 }
4029 else
4030 {
4031 PyErr_BadArgument();
4032 return FAIL;
4033 }
4034}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004036/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037 * The line number is in Vim format (1-based). The lines to be inserted are
4038 * given as a Python list of string objects or as a single string. The lines
4039 * to be added are checked for validity and correct format. Errors are
4040 * returned as a value of FAIL. The return value is OK on success.
4041 * If OK is returned and len_change is not NULL, *len_change
4042 * is set to the change in the buffer length.
4043 */
4044 static int
4045InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4046{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004047 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004048 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004049 tabpage_T *save_curtab = NULL;
4050
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 /* First of all, we check the type of the supplied Python object.
4052 * It must be a string or a list, or the call is in error.
4053 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004054 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004056 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057
4058 if (str == NULL)
4059 return FAIL;
4060
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004062 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004063 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004064
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004065 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004066 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004068 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004069 else if (save_curbuf == NULL)
4070 /* Only adjust marks if we managed to switch to a window that
4071 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072 appended_lines_mark((linenr_T)n, 1L);
4073
4074 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004075 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 update_screen(VALID);
4077
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004078 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 return FAIL;
4080
4081 if (len_change)
4082 *len_change = 1;
4083
4084 return OK;
4085 }
4086 else if (PyList_Check(lines))
4087 {
4088 PyInt i;
4089 PyInt size = PyList_Size(lines);
4090 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004092 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 if (array == NULL)
4094 {
4095 PyErr_NoMemory();
4096 return FAIL;
4097 }
4098
4099 for (i = 0; i < size; ++i)
4100 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004101 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004103 if (!(line = PyList_GetItem(lines, i)) ||
4104 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105 {
4106 while (i)
4107 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004108 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 return FAIL;
4110 }
4111 }
4112
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004114 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004115 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116
4117 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004118 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 else
4120 {
4121 for (i = 0; i < size; ++i)
4122 {
4123 if (ml_append((linenr_T)(n + i),
4124 (char_u *)array[i], 0, FALSE) == FAIL)
4125 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004126 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127
4128 /* Free the rest of the lines */
4129 while (i < size)
4130 vim_free(array[i++]);
4131
4132 break;
4133 }
4134 vim_free(array[i]);
4135 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004136 if (i > 0 && save_curbuf == NULL)
4137 /* Only adjust marks if we managed to switch to a window that
4138 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139 appended_lines_mark((linenr_T)n, (long)i);
4140 }
4141
4142 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004143 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004144 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004145 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147 update_screen(VALID);
4148
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004149 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150 return FAIL;
4151
4152 if (len_change)
4153 *len_change = size;
4154
4155 return OK;
4156 }
4157 else
4158 {
4159 PyErr_BadArgument();
4160 return FAIL;
4161 }
4162}
4163
4164/*
4165 * Common routines for buffers and line ranges
4166 * -------------------------------------------
4167 */
4168
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004169typedef struct
4170{
4171 PyObject_HEAD
4172 buf_T *buf;
4173} BufferObject;
4174
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004175 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004176CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004177{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004178 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004179 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004180 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004181 return -1;
4182 }
4183
4184 return 0;
4185}
4186
4187 static PyObject *
4188RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4189{
4190 if (CheckBuffer(self))
4191 return NULL;
4192
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004193 if (end == -1)
4194 end = self->buf->b_ml.ml_line_count;
4195
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004196 if (n < 0)
4197 n += end - start + 1;
4198
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004199 if (n < 0 || n > end - start)
4200 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004201 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004202 return NULL;
4203 }
4204
4205 return GetBufferLine(self->buf, n+start);
4206}
4207
4208 static PyObject *
4209RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4210{
4211 PyInt size;
4212
4213 if (CheckBuffer(self))
4214 return NULL;
4215
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004216 if (end == -1)
4217 end = self->buf->b_ml.ml_line_count;
4218
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004219 size = end - start + 1;
4220
4221 if (lo < 0)
4222 lo = 0;
4223 else if (lo > size)
4224 lo = size;
4225 if (hi < 0)
4226 hi = 0;
4227 if (hi < lo)
4228 hi = lo;
4229 else if (hi > size)
4230 hi = size;
4231
4232 return GetBufferLineList(self->buf, lo+start, hi+start);
4233}
4234
4235 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004236RBAsItem(
4237 BufferObject *self,
4238 PyInt n,
4239 PyObject *valObject,
4240 PyInt start,
4241 PyInt end,
4242 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004243{
4244 PyInt len_change;
4245
4246 if (CheckBuffer(self))
4247 return -1;
4248
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004249 if (end == -1)
4250 end = self->buf->b_ml.ml_line_count;
4251
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004252 if (n < 0)
4253 n += end - start + 1;
4254
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004255 if (n < 0 || n > end - start)
4256 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004257 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004258 return -1;
4259 }
4260
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004261 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004262 return -1;
4263
4264 if (new_end)
4265 *new_end = end + len_change;
4266
4267 return 0;
4268}
4269
Bram Moolenaar19e60942011-06-19 00:27:51 +02004270 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004271RBAsSlice(
4272 BufferObject *self,
4273 PyInt lo,
4274 PyInt hi,
4275 PyObject *valObject,
4276 PyInt start,
4277 PyInt end,
4278 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004279{
4280 PyInt size;
4281 PyInt len_change;
4282
4283 /* Self must be a valid buffer */
4284 if (CheckBuffer(self))
4285 return -1;
4286
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004287 if (end == -1)
4288 end = self->buf->b_ml.ml_line_count;
4289
Bram Moolenaar19e60942011-06-19 00:27:51 +02004290 /* Sort out the slice range */
4291 size = end - start + 1;
4292
4293 if (lo < 0)
4294 lo = 0;
4295 else if (lo > size)
4296 lo = size;
4297 if (hi < 0)
4298 hi = 0;
4299 if (hi < lo)
4300 hi = lo;
4301 else if (hi > size)
4302 hi = size;
4303
4304 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004305 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004306 return -1;
4307
4308 if (new_end)
4309 *new_end = end + len_change;
4310
4311 return 0;
4312}
4313
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004314
4315 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004316RBAppend(
4317 BufferObject *self,
4318 PyObject *args,
4319 PyInt start,
4320 PyInt end,
4321 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004322{
4323 PyObject *lines;
4324 PyInt len_change;
4325 PyInt max;
4326 PyInt n;
4327
4328 if (CheckBuffer(self))
4329 return NULL;
4330
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004331 if (end == -1)
4332 end = self->buf->b_ml.ml_line_count;
4333
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004334 max = n = end - start + 1;
4335
4336 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4337 return NULL;
4338
4339 if (n < 0 || n > max)
4340 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004341 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004342 return NULL;
4343 }
4344
4345 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4346 return NULL;
4347
4348 if (new_end)
4349 *new_end = end + len_change;
4350
4351 Py_INCREF(Py_None);
4352 return Py_None;
4353}
4354
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004355/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004356 */
4357
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004358static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004359static PySequenceMethods RangeAsSeq;
4360static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004361
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004362typedef struct
4363{
4364 PyObject_HEAD
4365 BufferObject *buf;
4366 PyInt start;
4367 PyInt end;
4368} RangeObject;
4369
4370 static PyObject *
4371RangeNew(buf_T *buf, PyInt start, PyInt end)
4372{
4373 BufferObject *bufr;
4374 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004375 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004376 if (self == NULL)
4377 return NULL;
4378
4379 bufr = (BufferObject *)BufferNew(buf);
4380 if (bufr == NULL)
4381 {
4382 Py_DECREF(self);
4383 return NULL;
4384 }
4385 Py_INCREF(bufr);
4386
4387 self->buf = bufr;
4388 self->start = start;
4389 self->end = end;
4390
4391 return (PyObject *)(self);
4392}
4393
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004394 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004395RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004396{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004397 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004398 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004399 PyObject_GC_Del((void *)(self));
4400}
4401
4402 static int
4403RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4404{
4405 Py_VISIT(((PyObject *)(self->buf)));
4406 return 0;
4407}
4408
4409 static int
4410RangeClear(RangeObject *self)
4411{
4412 Py_CLEAR(self->buf);
4413 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004414}
4415
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004416 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004417RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004418{
4419 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004420 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004421 return -1; /* ??? */
4422
Bram Moolenaard6e39182013-05-21 18:30:34 +02004423 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004424}
4425
4426 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004427RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004428{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004429 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004430}
4431
4432 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004433RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004434{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004435 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004436}
4437
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004438static char *RangeAttrs[] = {
4439 "start", "end",
4440 NULL
4441};
4442
4443 static PyObject *
4444RangeDir(PyObject *self)
4445{
4446 return ObjectDir(self, RangeAttrs);
4447}
4448
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004449 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004450RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004451{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004452 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004453}
4454
4455 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004456RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004457{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004458 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004459 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4460 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004461 else
4462 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004463 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004464
4465 if (name == NULL)
4466 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004467
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004468 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004469 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004470 }
4471}
4472
4473static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004474 /* name, function, calling, documentation */
4475 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004476 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4477 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004478};
4479
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004480static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004481static PySequenceMethods BufferAsSeq;
4482static PyMappingMethods BufferAsMapping;
4483
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004484 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004485BufferNew(buf_T *buf)
4486{
4487 /* We need to handle deletion of buffers underneath us.
4488 * If we add a "b_python*_ref" field to the buf_T structure,
4489 * then we can get at it in buf_freeall() in vim. We then
4490 * need to create only ONE Python object per buffer - if
4491 * we try to create a second, just INCREF the existing one
4492 * and return it. The (single) Python object referring to
4493 * the buffer is stored in "b_python*_ref".
4494 * Question: what to do on a buf_freeall(). We'll probably
4495 * have to either delete the Python object (DECREF it to
4496 * zero - a bad idea, as it leaves dangling refs!) or
4497 * set the buf_T * value to an invalid value (-1?), which
4498 * means we need checks in all access functions... Bah.
4499 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004500 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004501 * b_python_ref and b_python3_ref fields respectively.
4502 */
4503
4504 BufferObject *self;
4505
4506 if (BUF_PYTHON_REF(buf) != NULL)
4507 {
4508 self = BUF_PYTHON_REF(buf);
4509 Py_INCREF(self);
4510 }
4511 else
4512 {
4513 self = PyObject_NEW(BufferObject, &BufferType);
4514 if (self == NULL)
4515 return NULL;
4516 self->buf = buf;
4517 BUF_PYTHON_REF(buf) = self;
4518 }
4519
4520 return (PyObject *)(self);
4521}
4522
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004523 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004524BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004525{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004526 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4527 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004528
4529 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004530}
4531
Bram Moolenaar971db462013-05-12 18:44:48 +02004532 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004533BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004534{
4535 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004536 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004537 return -1; /* ??? */
4538
Bram Moolenaard6e39182013-05-21 18:30:34 +02004539 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004540}
4541
4542 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004543BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004544{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004545 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004546}
4547
4548 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004549BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004550{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004551 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004552}
4553
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004554static char *BufferAttrs[] = {
4555 "name", "number", "vars", "options", "valid",
4556 NULL
4557};
4558
4559 static PyObject *
4560BufferDir(PyObject *self)
4561{
4562 return ObjectDir(self, BufferAttrs);
4563}
4564
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004565 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004566BufferAttrValid(BufferObject *self, char *name)
4567{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004568 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004569
4570 if (strcmp(name, "valid") != 0)
4571 return NULL;
4572
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004573 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4574 Py_INCREF(ret);
4575 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004576}
4577
4578 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004579BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004580{
4581 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004582 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004583 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004584 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004585 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004586 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004587 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004588 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004589 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4590 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004591 else if (strcmp(name, "__members__") == 0)
4592 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004593 else
4594 return NULL;
4595}
4596
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004597 static int
4598BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4599{
4600 if (CheckBuffer(self))
4601 return -1;
4602
4603 if (strcmp(name, "name") == 0)
4604 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004605 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004606 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004607 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004608 PyObject *todecref;
4609
4610 if (!(val = StringToChars(valObject, &todecref)))
4611 return -1;
4612
4613 VimTryStart();
4614 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4615 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004616 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004617 aucmd_restbuf(&aco);
4618 Py_XDECREF(todecref);
4619 if (VimTryEnd())
4620 return -1;
4621
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004622 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004623 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004624 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004625 return -1;
4626 }
4627 return 0;
4628 }
4629 else
4630 {
4631 PyErr_SetString(PyExc_AttributeError, name);
4632 return -1;
4633 }
4634}
4635
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004636 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004637BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004638{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004639 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004640}
4641
4642 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004643BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004644{
4645 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004646 char_u *pmark;
4647 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004648 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004649 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004650
Bram Moolenaard6e39182013-05-21 18:30:34 +02004651 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652 return NULL;
4653
Bram Moolenaar389a1792013-06-23 13:00:44 +02004654 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004655 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004656
Bram Moolenaar389a1792013-06-23 13:00:44 +02004657 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004658 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004659 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004660 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004661 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004662 return NULL;
4663 }
4664
4665 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004666
4667 Py_XDECREF(todecref);
4668
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004669 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004670 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004671 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004672 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004673 if (VimTryEnd())
4674 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675
4676 if (posp == NULL)
4677 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004678 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004679 return NULL;
4680 }
4681
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004682 if (posp->lnum <= 0)
4683 {
4684 /* Or raise an error? */
4685 Py_INCREF(Py_None);
4686 return Py_None;
4687 }
4688
4689 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4690}
4691
4692 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004693BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694{
4695 PyInt start;
4696 PyInt end;
4697
Bram Moolenaard6e39182013-05-21 18:30:34 +02004698 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004699 return NULL;
4700
4701 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4702 return NULL;
4703
Bram Moolenaard6e39182013-05-21 18:30:34 +02004704 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004705}
4706
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004707 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004708BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004709{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004710 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004711 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004712 else
4713 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004714 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004715
4716 if (name == NULL)
4717 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004718
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004719 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004720 }
4721}
4722
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004723static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004724 /* name, function, calling, documentation */
4725 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004726 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004727 {"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 +02004728 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4729 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730};
4731
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004732/*
4733 * Buffer list object - Implementation
4734 */
4735
4736static PyTypeObject BufMapType;
4737
4738typedef struct
4739{
4740 PyObject_HEAD
4741} BufMapObject;
4742
4743 static PyInt
4744BufMapLength(PyObject *self UNUSED)
4745{
4746 buf_T *b = firstbuf;
4747 PyInt n = 0;
4748
4749 while (b)
4750 {
4751 ++n;
4752 b = b->b_next;
4753 }
4754
4755 return n;
4756}
4757
4758 static PyObject *
4759BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4760{
4761 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004762 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004763
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004764 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004765 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004766
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004767 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004768
4769 if (b)
4770 return BufferNew(b);
4771 else
4772 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004773 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004774 return NULL;
4775 }
4776}
4777
4778 static void
4779BufMapIterDestruct(PyObject *buffer)
4780{
4781 /* Iteration was stopped before all buffers were processed */
4782 if (buffer)
4783 {
4784 Py_DECREF(buffer);
4785 }
4786}
4787
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004788 static int
4789BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4790{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004791 if (buffer)
4792 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004793 return 0;
4794}
4795
4796 static int
4797BufMapIterClear(PyObject **buffer)
4798{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004799 if (*buffer)
4800 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004801 return 0;
4802}
4803
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004804 static PyObject *
4805BufMapIterNext(PyObject **buffer)
4806{
4807 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004808 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004809
4810 if (!*buffer)
4811 return NULL;
4812
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004813 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004814
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004815 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004816 {
4817 *buffer = NULL;
4818 return NULL;
4819 }
4820
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004821 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004822 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004823 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004824 return NULL;
4825 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004826 /* Do not increment reference: we no longer hold it (decref), but whoever
4827 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004828 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004829}
4830
4831 static PyObject *
4832BufMapIter(PyObject *self UNUSED)
4833{
4834 PyObject *buffer;
4835
4836 buffer = BufferNew(firstbuf);
4837 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004838 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4839 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004840}
4841
4842static PyMappingMethods BufMapAsMapping = {
4843 (lenfunc) BufMapLength,
4844 (binaryfunc) BufMapItem,
4845 (objobjargproc) 0,
4846};
4847
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004848/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004849 */
4850
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004851static char *CurrentAttrs[] = {
4852 "buffer", "window", "line", "range", "tabpage",
4853 NULL
4854};
4855
4856 static PyObject *
4857CurrentDir(PyObject *self)
4858{
4859 return ObjectDir(self, CurrentAttrs);
4860}
4861
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004862 static PyObject *
4863CurrentGetattr(PyObject *self UNUSED, char *name)
4864{
4865 if (strcmp(name, "buffer") == 0)
4866 return (PyObject *)BufferNew(curbuf);
4867 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004868 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004869 else if (strcmp(name, "tabpage") == 0)
4870 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004871 else if (strcmp(name, "line") == 0)
4872 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4873 else if (strcmp(name, "range") == 0)
4874 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004875 else if (strcmp(name, "__members__") == 0)
4876 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004877 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004878#if PY_MAJOR_VERSION < 3
4879 return Py_FindMethod(WindowMethods, self, name);
4880#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004881 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004882#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004883}
4884
4885 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004886CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004887{
4888 if (strcmp(name, "line") == 0)
4889 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004890 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4891 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004892 return -1;
4893
4894 return 0;
4895 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004896 else if (strcmp(name, "buffer") == 0)
4897 {
4898 int count;
4899
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004900 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004901 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004902 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004903 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004904 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004905 return -1;
4906 }
4907
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004908 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004909 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004910 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004911
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004912 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004913 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4914 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004915 if (VimTryEnd())
4916 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004917 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004918 return -1;
4919 }
4920
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004921 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004922 }
4923 else if (strcmp(name, "window") == 0)
4924 {
4925 int count;
4926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004927 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004928 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004929 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004930 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004931 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004932 return -1;
4933 }
4934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004935 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004936 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004937 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004938
4939 if (!count)
4940 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004941 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004942 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004943 return -1;
4944 }
4945
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004946 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004947 win_goto(((WindowObject *)(valObject))->win);
4948 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004949 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004950 if (VimTryEnd())
4951 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004952 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004953 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004954 return -1;
4955 }
4956
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004957 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004958 }
4959 else if (strcmp(name, "tabpage") == 0)
4960 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004961 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004962 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004963 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004964 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004965 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004966 return -1;
4967 }
4968
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004969 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004970 return -1;
4971
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004972 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004973 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4974 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004975 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004976 if (VimTryEnd())
4977 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004978 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004979 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004980 return -1;
4981 }
4982
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004983 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004984 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004985 else
4986 {
4987 PyErr_SetString(PyExc_AttributeError, name);
4988 return -1;
4989 }
4990}
4991
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004992static struct PyMethodDef CurrentMethods[] = {
4993 /* name, function, calling, documentation */
4994 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4995 { NULL, NULL, 0, NULL}
4996};
4997
Bram Moolenaardb913952012-06-29 12:54:53 +02004998 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004999init_range_cmd(exarg_T *eap)
5000{
5001 RangeStart = eap->line1;
5002 RangeEnd = eap->line2;
5003}
5004
5005 static void
5006init_range_eval(typval_T *rettv UNUSED)
5007{
5008 RangeStart = (PyInt) curwin->w_cursor.lnum;
5009 RangeEnd = RangeStart;
5010}
5011
5012 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005013run_cmd(const char *cmd, void *arg UNUSED
5014#ifdef PY_CAN_RECURSE
5015 , PyGILState_STATE *pygilstate UNUSED
5016#endif
5017 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005018{
Bram Moolenaar41009372013-07-01 22:03:04 +02005019 PyObject *run_ret;
5020 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5021 if (run_ret != NULL)
5022 {
5023 Py_DECREF(run_ret);
5024 }
5025 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5026 {
5027 EMSG2(_(e_py_systemexit), "python");
5028 PyErr_Clear();
5029 }
5030 else
5031 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005032}
5033
5034static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5035static int code_hdr_len = 30;
5036
5037 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005038run_do(const char *cmd, void *arg UNUSED
5039#ifdef PY_CAN_RECURSE
5040 , PyGILState_STATE *pygilstate
5041#endif
5042 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005043{
5044 PyInt lnum;
5045 size_t len;
5046 char *code;
5047 int status;
5048 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005049 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005050
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005051 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005052 {
5053 EMSG(_("cannot save undo information"));
5054 return;
5055 }
5056
5057 len = code_hdr_len + STRLEN(cmd);
5058 code = PyMem_New(char, len + 1);
5059 memcpy(code, code_hdr, code_hdr_len);
5060 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005061 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5062 status = -1;
5063 if (run_ret != NULL)
5064 {
5065 status = 0;
5066 Py_DECREF(run_ret);
5067 }
5068 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5069 {
5070 PyMem_Free(code);
5071 EMSG2(_(e_py_systemexit), "python");
5072 PyErr_Clear();
5073 return;
5074 }
5075 else
5076 PyErr_PrintEx(1);
5077
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005078 PyMem_Free(code);
5079
5080 if (status)
5081 {
5082 EMSG(_("failed to run the code"));
5083 return;
5084 }
5085
5086 status = 0;
5087 pymain = PyImport_AddModule("__main__");
5088 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005089#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005090 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005091#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005092
5093 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5094 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005095 PyObject *line;
5096 PyObject *linenr;
5097 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005098
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005099#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005100 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005101#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005102 if (!(line = GetBufferLine(curbuf, lnum)))
5103 goto err;
5104 if (!(linenr = PyInt_FromLong((long) lnum)))
5105 {
5106 Py_DECREF(line);
5107 goto err;
5108 }
5109 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5110 Py_DECREF(line);
5111 Py_DECREF(linenr);
5112 if (!ret)
5113 goto err;
5114
5115 if (ret != Py_None)
5116 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5117 goto err;
5118
5119 Py_XDECREF(ret);
5120 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005121#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005122 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005123#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005124 }
5125 goto out;
5126err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005127#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005128 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005129#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005130 PyErr_PrintEx(0);
5131 PythonIO_Flush();
5132 status = 1;
5133out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005134#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005135 if (!status)
5136 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005137#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005138 Py_DECREF(pyfunc);
5139 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5140 if (status)
5141 return;
5142 check_cursor();
5143 update_curbuf(NOT_VALID);
5144}
5145
5146 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005147run_eval(const char *cmd, typval_T *rettv
5148#ifdef PY_CAN_RECURSE
5149 , PyGILState_STATE *pygilstate UNUSED
5150#endif
5151 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005152{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005153 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005154
Bram Moolenaar41009372013-07-01 22:03:04 +02005155 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005156 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005157 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005158 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005159 {
5160 EMSG2(_(e_py_systemexit), "python");
5161 PyErr_Clear();
5162 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005163 else
5164 {
5165 if (PyErr_Occurred() && !msg_silent)
5166 PyErr_PrintEx(0);
5167 EMSG(_("E858: Eval did not return a valid python object"));
5168 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005169 }
5170 else
5171 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005172 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005173 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005174 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005175 }
5176 PyErr_Clear();
5177}
5178
5179 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005180set_ref_in_py(const int copyID)
5181{
5182 pylinkedlist_T *cur;
5183 dict_T *dd;
5184 list_T *ll;
5185
5186 if (lastdict != NULL)
5187 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5188 {
5189 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5190 if (dd->dv_copyID != copyID)
5191 {
5192 dd->dv_copyID = copyID;
5193 set_ref_in_ht(&dd->dv_hashtab, copyID);
5194 }
5195 }
5196
5197 if (lastlist != NULL)
5198 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5199 {
5200 ll = ((ListObject *) (cur->pll_obj))->list;
5201 if (ll->lv_copyID != copyID)
5202 {
5203 ll->lv_copyID = copyID;
5204 set_ref_in_list(ll, copyID);
5205 }
5206 }
5207}
5208
5209 static int
5210set_string_copy(char_u *str, typval_T *tv)
5211{
5212 tv->vval.v_string = vim_strsave(str);
5213 if (tv->vval.v_string == NULL)
5214 {
5215 PyErr_NoMemory();
5216 return -1;
5217 }
5218 return 0;
5219}
5220
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005221 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005222pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005223{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005224 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005225 char_u *key;
5226 dictitem_T *di;
5227 PyObject *keyObject;
5228 PyObject *valObject;
5229 Py_ssize_t iter = 0;
5230
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005231 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005233
5234 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005235 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005236
5237 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5238 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005239 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005240
Bram Moolenaara03e6312013-05-29 22:49:26 +02005241 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005242 {
5243 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005244 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005245 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005246
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005247 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005248 {
5249 dict_unref(dict);
5250 return -1;
5251 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005252
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005253 if (*key == NUL)
5254 {
5255 dict_unref(dict);
5256 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005257 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005258 return -1;
5259 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005260
5261 di = dictitem_alloc(key);
5262
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005263 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005264
5265 if (di == NULL)
5266 {
5267 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005268 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005269 return -1;
5270 }
5271 di->di_tv.v_lock = 0;
5272
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005273 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005274 {
5275 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005276 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005277 return -1;
5278 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005279
5280 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005281 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005282 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005283 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005284 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005285 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005286 return -1;
5287 }
5288 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005289
5290 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005291 return 0;
5292}
5293
5294 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005295pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005296{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005297 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005298 char_u *key;
5299 dictitem_T *di;
5300 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005301 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005302 PyObject *keyObject;
5303 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005304
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005305 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005306 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005307
5308 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005309 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005310
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005311 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005312 {
5313 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005314 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005315 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005316
5317 if (!(iterator = PyObject_GetIter(list)))
5318 {
5319 dict_unref(dict);
5320 Py_DECREF(list);
5321 return -1;
5322 }
5323 Py_DECREF(list);
5324
5325 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005326 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005327 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005328
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005329 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005330 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005331 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005332 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005333 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005334 return -1;
5335 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005336
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005337 if (*key == NUL)
5338 {
5339 Py_DECREF(keyObject);
5340 Py_DECREF(iterator);
5341 Py_XDECREF(todecref);
5342 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005343 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005344 return -1;
5345 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005346
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005347 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005348 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005349 Py_DECREF(keyObject);
5350 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005351 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005352 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005353 return -1;
5354 }
5355
5356 di = dictitem_alloc(key);
5357
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005358 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005359 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005360
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005361 if (di == NULL)
5362 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005363 Py_DECREF(iterator);
5364 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005365 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005366 PyErr_NoMemory();
5367 return -1;
5368 }
5369 di->di_tv.v_lock = 0;
5370
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005371 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005372 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005373 Py_DECREF(iterator);
5374 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005375 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005376 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005377 return -1;
5378 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005379
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005380 Py_DECREF(valObject);
5381
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005382 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005383 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005384 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005385 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005386 dictitem_free(di);
5387 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005388 return -1;
5389 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005390 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005391 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005392 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005393 return 0;
5394}
5395
5396 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005397pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005398{
5399 list_T *l;
5400
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005401 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005402 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005403
5404 tv->v_type = VAR_LIST;
5405 tv->vval.v_list = l;
5406
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005407 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005408 {
5409 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005410 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005411 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005412
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005413 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005414 return 0;
5415}
5416
Bram Moolenaardb913952012-06-29 12:54:53 +02005417typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5418
5419 static int
5420convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005421 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005422{
5423 PyObject *capsule;
5424 char hexBuf[sizeof(void *) * 2 + 3];
5425
5426 sprintf(hexBuf, "%p", obj);
5427
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005428# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005429 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005430# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005431 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005432# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005433 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005434 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005435# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005436 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005437# else
5438 capsule = PyCObject_FromVoidPtr(tv, NULL);
5439# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005440 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5441 {
5442 Py_DECREF(capsule);
5443 tv->v_type = VAR_UNKNOWN;
5444 return -1;
5445 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005446
5447 Py_DECREF(capsule);
5448
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005449 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005450 {
5451 tv->v_type = VAR_UNKNOWN;
5452 return -1;
5453 }
5454 /* As we are not using copy_tv which increments reference count we must
5455 * do it ourself. */
5456 switch(tv->v_type)
5457 {
5458 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5459 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5460 }
5461 }
5462 else
5463 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005464 typval_T *v;
5465
5466# ifdef PY_USE_CAPSULE
5467 v = PyCapsule_GetPointer(capsule, NULL);
5468# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005469 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005470# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005471 copy_tv(v, tv);
5472 }
5473 return 0;
5474}
5475
5476 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005477ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5478{
5479 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005480 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005481
5482 if (!(lookup_dict = PyDict_New()))
5483 return -1;
5484
5485 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5486 {
5487 tv->v_type = VAR_DICT;
5488 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5489 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005490 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005491 }
5492 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005493 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005494 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005495 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005496 else
5497 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005498 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005499 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005500 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005501 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005502 }
5503 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005504 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005505}
5506
5507 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005508ConvertFromPyObject(PyObject *obj, typval_T *tv)
5509{
5510 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005511 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005512
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005513 if (!(lookup_dict = PyDict_New()))
5514 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005515 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005516 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005517 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005518}
5519
5520 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005521_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005522{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005523 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005524 {
5525 tv->v_type = VAR_DICT;
5526 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5527 ++tv->vval.v_dict->dv_refcount;
5528 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005529 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005530 {
5531 tv->v_type = VAR_LIST;
5532 tv->vval.v_list = (((ListObject *)(obj))->list);
5533 ++tv->vval.v_list->lv_refcount;
5534 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005535 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005536 {
5537 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5538 return -1;
5539
5540 tv->v_type = VAR_FUNC;
5541 func_ref(tv->vval.v_string);
5542 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005543 else if (PyBytes_Check(obj))
5544 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005545 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005546
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005547 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005548 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005549 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005550 return -1;
5551
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005552 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005553 return -1;
5554
5555 tv->v_type = VAR_STRING;
5556 }
5557 else if (PyUnicode_Check(obj))
5558 {
5559 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005560 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005561
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005562 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005563 if (bytes == NULL)
5564 return -1;
5565
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005566 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005567 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005568 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005569 return -1;
5570
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005572 {
5573 Py_XDECREF(bytes);
5574 return -1;
5575 }
5576 Py_XDECREF(bytes);
5577
5578 tv->v_type = VAR_STRING;
5579 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005580#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005581 else if (PyInt_Check(obj))
5582 {
5583 tv->v_type = VAR_NUMBER;
5584 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005585 if (PyErr_Occurred())
5586 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005587 }
5588#endif
5589 else if (PyLong_Check(obj))
5590 {
5591 tv->v_type = VAR_NUMBER;
5592 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005593 if (PyErr_Occurred())
5594 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005595 }
5596 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005597 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005598#ifdef FEAT_FLOAT
5599 else if (PyFloat_Check(obj))
5600 {
5601 tv->v_type = VAR_FLOAT;
5602 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5603 }
5604#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005605 else if (PyObject_HasAttrString(obj, "keys"))
5606 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005607 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005608 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005609 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005610 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005611 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005612 else if (PyNumber_Check(obj))
5613 {
5614 PyObject *num;
5615
5616 if (!(num = PyNumber_Long(obj)))
5617 return -1;
5618
5619 tv->v_type = VAR_NUMBER;
5620 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5621
5622 Py_DECREF(num);
5623 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005624 else
5625 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005626 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005627 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005628 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005629 return -1;
5630 }
5631 return 0;
5632}
5633
5634 static PyObject *
5635ConvertToPyObject(typval_T *tv)
5636{
5637 if (tv == NULL)
5638 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005639 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005640 return NULL;
5641 }
5642 switch (tv->v_type)
5643 {
5644 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005645 return PyBytes_FromString(tv->vval.v_string == NULL
5646 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005647 case VAR_NUMBER:
5648 return PyLong_FromLong((long) tv->vval.v_number);
5649#ifdef FEAT_FLOAT
5650 case VAR_FLOAT:
5651 return PyFloat_FromDouble((double) tv->vval.v_float);
5652#endif
5653 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005654 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005655 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005656 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005657 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005658 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005659 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005660 case VAR_UNKNOWN:
5661 Py_INCREF(Py_None);
5662 return Py_None;
5663 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005664 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005665 return NULL;
5666 }
5667}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005668
5669typedef struct
5670{
5671 PyObject_HEAD
5672} CurrentObject;
5673static PyTypeObject CurrentType;
5674
5675 static void
5676init_structs(void)
5677{
5678 vim_memset(&OutputType, 0, sizeof(OutputType));
5679 OutputType.tp_name = "vim.message";
5680 OutputType.tp_basicsize = sizeof(OutputObject);
5681 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5682 OutputType.tp_doc = "vim message object";
5683 OutputType.tp_methods = OutputMethods;
5684#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005685 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5686 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005687 OutputType.tp_alloc = call_PyType_GenericAlloc;
5688 OutputType.tp_new = call_PyType_GenericNew;
5689 OutputType.tp_free = call_PyObject_Free;
5690#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005691 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5692 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005693#endif
5694
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005695 vim_memset(&IterType, 0, sizeof(IterType));
5696 IterType.tp_name = "vim.iter";
5697 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005698 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005699 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005700 IterType.tp_iter = (getiterfunc)IterIter;
5701 IterType.tp_iternext = (iternextfunc)IterNext;
5702 IterType.tp_dealloc = (destructor)IterDestructor;
5703 IterType.tp_traverse = (traverseproc)IterTraverse;
5704 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005705
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005706 vim_memset(&BufferType, 0, sizeof(BufferType));
5707 BufferType.tp_name = "vim.buffer";
5708 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005709 BufferType.tp_dealloc = (destructor)BufferDestructor;
5710 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005711 BufferType.tp_as_sequence = &BufferAsSeq;
5712 BufferType.tp_as_mapping = &BufferAsMapping;
5713 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5714 BufferType.tp_doc = "vim buffer object";
5715 BufferType.tp_methods = BufferMethods;
5716#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005717 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005718 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005719 BufferType.tp_alloc = call_PyType_GenericAlloc;
5720 BufferType.tp_new = call_PyType_GenericNew;
5721 BufferType.tp_free = call_PyObject_Free;
5722#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005723 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005724 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005725#endif
5726
5727 vim_memset(&WindowType, 0, sizeof(WindowType));
5728 WindowType.tp_name = "vim.window";
5729 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005730 WindowType.tp_dealloc = (destructor)WindowDestructor;
5731 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005732 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005733 WindowType.tp_doc = "vim Window object";
5734 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005735 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5736 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005737#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005738 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5739 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005740 WindowType.tp_alloc = call_PyType_GenericAlloc;
5741 WindowType.tp_new = call_PyType_GenericNew;
5742 WindowType.tp_free = call_PyObject_Free;
5743#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005744 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5745 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005746#endif
5747
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005748 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5749 TabPageType.tp_name = "vim.tabpage";
5750 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005751 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5752 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005753 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5754 TabPageType.tp_doc = "vim tab page object";
5755 TabPageType.tp_methods = TabPageMethods;
5756#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005757 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005758 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5759 TabPageType.tp_new = call_PyType_GenericNew;
5760 TabPageType.tp_free = call_PyObject_Free;
5761#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005762 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005763#endif
5764
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005765 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5766 BufMapType.tp_name = "vim.bufferlist";
5767 BufMapType.tp_basicsize = sizeof(BufMapObject);
5768 BufMapType.tp_as_mapping = &BufMapAsMapping;
5769 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005770 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005771 BufferType.tp_doc = "vim buffer list";
5772
5773 vim_memset(&WinListType, 0, sizeof(WinListType));
5774 WinListType.tp_name = "vim.windowlist";
5775 WinListType.tp_basicsize = sizeof(WinListType);
5776 WinListType.tp_as_sequence = &WinListAsSeq;
5777 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5778 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005779 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005780
5781 vim_memset(&TabListType, 0, sizeof(TabListType));
5782 TabListType.tp_name = "vim.tabpagelist";
5783 TabListType.tp_basicsize = sizeof(TabListType);
5784 TabListType.tp_as_sequence = &TabListAsSeq;
5785 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5786 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005787
5788 vim_memset(&RangeType, 0, sizeof(RangeType));
5789 RangeType.tp_name = "vim.range";
5790 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005791 RangeType.tp_dealloc = (destructor)RangeDestructor;
5792 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005793 RangeType.tp_as_sequence = &RangeAsSeq;
5794 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005795 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005796 RangeType.tp_doc = "vim Range object";
5797 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005798 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5799 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005800#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005801 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005802 RangeType.tp_alloc = call_PyType_GenericAlloc;
5803 RangeType.tp_new = call_PyType_GenericNew;
5804 RangeType.tp_free = call_PyObject_Free;
5805#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005806 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005807#endif
5808
5809 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5810 CurrentType.tp_name = "vim.currentdata";
5811 CurrentType.tp_basicsize = sizeof(CurrentObject);
5812 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5813 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005814 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005815#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005816 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5817 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005818#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005819 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5820 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005821#endif
5822
5823 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5824 DictionaryType.tp_name = "vim.dictionary";
5825 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005826 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005827 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005828 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005829 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005830 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5831 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005832 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5833 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5834 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005835#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005836 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5837 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005838#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005839 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5840 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005841#endif
5842
5843 vim_memset(&ListType, 0, sizeof(ListType));
5844 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005845 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005846 ListType.tp_basicsize = sizeof(ListObject);
5847 ListType.tp_as_sequence = &ListAsSeq;
5848 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005849 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005850 ListType.tp_doc = "list pushing modifications to vim structure";
5851 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005852 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005853 ListType.tp_new = (newfunc)ListConstructor;
5854 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005855#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005856 ListType.tp_getattro = (getattrofunc)ListGetattro;
5857 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005858#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005859 ListType.tp_getattr = (getattrfunc)ListGetattr;
5860 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005861#endif
5862
5863 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005864 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005865 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005866 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5867 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005868 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005869 FunctionType.tp_doc = "object that calls vim function";
5870 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005871 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005872 FunctionType.tp_new = (newfunc)FunctionConstructor;
5873 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005874#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005875 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005876#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005877 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005878#endif
5879
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005880 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5881 OptionsType.tp_name = "vim.options";
5882 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005883 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005884 OptionsType.tp_doc = "object for manipulating options";
5885 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005886 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5887 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5888 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005889
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005890 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5891 LoaderType.tp_name = "vim.Loader";
5892 LoaderType.tp_basicsize = sizeof(LoaderObject);
5893 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5894 LoaderType.tp_doc = "vim message object";
5895 LoaderType.tp_methods = LoaderMethods;
5896 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5897
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005898#if PY_MAJOR_VERSION >= 3
5899 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5900 vimmodule.m_name = "vim";
5901 vimmodule.m_doc = "Vim Python interface\n";
5902 vimmodule.m_size = -1;
5903 vimmodule.m_methods = VimMethods;
5904#endif
5905}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005906
5907#define PYTYPE_READY(type) \
5908 if (PyType_Ready(&type)) \
5909 return -1;
5910
5911 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02005912init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005913{
5914 PYTYPE_READY(IterType);
5915 PYTYPE_READY(BufferType);
5916 PYTYPE_READY(RangeType);
5917 PYTYPE_READY(WindowType);
5918 PYTYPE_READY(TabPageType);
5919 PYTYPE_READY(BufMapType);
5920 PYTYPE_READY(WinListType);
5921 PYTYPE_READY(TabListType);
5922 PYTYPE_READY(CurrentType);
5923 PYTYPE_READY(DictionaryType);
5924 PYTYPE_READY(ListType);
5925 PYTYPE_READY(FunctionType);
5926 PYTYPE_READY(OptionsType);
5927 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005928 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005929 return 0;
5930}
5931
5932 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005933init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005934{
5935 PyObject *path;
5936 PyObject *path_hook;
5937 PyObject *path_hooks;
5938
5939 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5940 return -1;
5941
5942 if (!(path_hooks = PySys_GetObject("path_hooks")))
5943 {
5944 PyErr_Clear();
5945 path_hooks = PyList_New(1);
5946 PyList_SET_ITEM(path_hooks, 0, path_hook);
5947 if (PySys_SetObject("path_hooks", path_hooks))
5948 {
5949 Py_DECREF(path_hooks);
5950 return -1;
5951 }
5952 Py_DECREF(path_hooks);
5953 }
5954 else if (PyList_Check(path_hooks))
5955 {
5956 if (PyList_Append(path_hooks, path_hook))
5957 {
5958 Py_DECREF(path_hook);
5959 return -1;
5960 }
5961 Py_DECREF(path_hook);
5962 }
5963 else
5964 {
5965 VimTryStart();
5966 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5967 "You should now do the following:\n"
5968 "- append vim.path_hook to sys.path_hooks\n"
5969 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5970 VimTryEnd(); /* Discard the error */
5971 Py_DECREF(path_hook);
5972 return 0;
5973 }
5974
5975 if (!(path = PySys_GetObject("path")))
5976 {
5977 PyErr_Clear();
5978 path = PyList_New(1);
5979 Py_INCREF(vim_special_path_object);
5980 PyList_SET_ITEM(path, 0, vim_special_path_object);
5981 if (PySys_SetObject("path", path))
5982 {
5983 Py_DECREF(path);
5984 return -1;
5985 }
5986 Py_DECREF(path);
5987 }
5988 else if (PyList_Check(path))
5989 {
5990 if (PyList_Append(path, vim_special_path_object))
5991 return -1;
5992 }
5993 else
5994 {
5995 VimTryStart();
5996 EMSG(_("Failed to set path: sys.path is not a list\n"
5997 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5998 VimTryEnd(); /* Discard the error */
5999 }
6000
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006001 return 0;
6002}
6003
6004static BufMapObject TheBufferMap =
6005{
6006 PyObject_HEAD_INIT(&BufMapType)
6007};
6008
6009static WinListObject TheWindowList =
6010{
6011 PyObject_HEAD_INIT(&WinListType)
6012 NULL
6013};
6014
6015static CurrentObject TheCurrent =
6016{
6017 PyObject_HEAD_INIT(&CurrentType)
6018};
6019
6020static TabListObject TheTabPageList =
6021{
6022 PyObject_HEAD_INIT(&TabListType)
6023};
6024
6025static struct numeric_constant {
6026 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006027 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006028} numeric_constants[] = {
6029 {"VAR_LOCKED", VAR_LOCKED},
6030 {"VAR_FIXED", VAR_FIXED},
6031 {"VAR_SCOPE", VAR_SCOPE},
6032 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6033};
6034
6035static struct object_constant {
6036 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006037 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006038} object_constants[] = {
6039 {"buffers", (PyObject *)(void *)&TheBufferMap},
6040 {"windows", (PyObject *)(void *)&TheWindowList},
6041 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6042 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006043
6044 {"Buffer", (PyObject *)&BufferType},
6045 {"Range", (PyObject *)&RangeType},
6046 {"Window", (PyObject *)&WindowType},
6047 {"TabPage", (PyObject *)&TabPageType},
6048 {"Dictionary", (PyObject *)&DictionaryType},
6049 {"List", (PyObject *)&ListType},
6050 {"Function", (PyObject *)&FunctionType},
6051 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006052 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006053};
6054
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006055#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006056 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006057 return -1;
6058
6059#define ADD_CHECKED_OBJECT(m, name, obj) \
6060 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006061 PyObject *valObject = obj; \
6062 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006063 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006064 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006065 }
6066
6067 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006068populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006069{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006070 int i;
6071 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006072 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006073 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006074
6075 for (i = 0; i < (int)(sizeof(numeric_constants)
6076 / sizeof(struct numeric_constant));
6077 ++i)
6078 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006079 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006080
6081 for (i = 0; i < (int)(sizeof(object_constants)
6082 / sizeof(struct object_constant));
6083 ++i)
6084 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006085 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006086
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006087 valObject = object_constants[i].valObject;
6088 Py_INCREF(valObject);
6089 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006090 }
6091
6092 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6093 return -1;
6094 ADD_OBJECT(m, "error", VimError);
6095
Bram Moolenaara9922d62013-05-30 13:01:18 +02006096 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6097 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006098 ADD_CHECKED_OBJECT(m, "options",
6099 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006100
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006101 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006102 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006103 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006104
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006105 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006106 return -1;
6107 ADD_OBJECT(m, "_getcwd", py_getcwd)
6108
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006109 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006110 return -1;
6111 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006112 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006113 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006114 if (PyObject_SetAttrString(other_module, "chdir", attr))
6115 {
6116 Py_DECREF(attr);
6117 return -1;
6118 }
6119 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006120
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006121 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006122 {
6123 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006124 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006125 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006126 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6127 {
6128 Py_DECREF(attr);
6129 return -1;
6130 }
6131 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006132 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006133 else
6134 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006135
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006136 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6137 return -1;
6138
6139 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6140
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006141 if (!(imp = PyImport_ImportModule("imp")))
6142 return -1;
6143
6144 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6145 {
6146 Py_DECREF(imp);
6147 return -1;
6148 }
6149
6150 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6151 {
6152 Py_DECREF(py_find_module);
6153 Py_DECREF(imp);
6154 return -1;
6155 }
6156
6157 Py_DECREF(imp);
6158
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006159 ADD_OBJECT(m, "_find_module", py_find_module);
6160 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006161
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006162 return 0;
6163}