blob: e6db4a3d0354d6fb7f9aec1f14a8c16104fecf0f [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 Moolenaard6b8a522013-11-11 01:05:48 +0100561 if (current_exception != NULL)
562 discard_current_exception();
563 else
564 need_rethrow = did_throw = FALSE;
565 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200566 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200567 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200568 }
569 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200570 return (PyErr_Occurred() ? -1 : 0);
571 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200573 {
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100574 if (current_exception != NULL)
575 discard_current_exception();
576 else
577 need_rethrow = did_throw = FALSE;
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200578 return -1;
579 }
580 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200581 else
582 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200583 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200584 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200585 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200586 }
587}
588
589 static int
590VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200591{
592 if (got_int)
593 {
594 PyErr_SetNone(PyExc_KeyboardInterrupt);
595 return 1;
596 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200597 return 0;
598}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200599
600/* Vim module - Implementation
601 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200602
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200603 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200604VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200605{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200606 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200607 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200608 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200609
Bram Moolenaar389a1792013-06-23 13:00:44 +0200610 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200611 return NULL;
612
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613 Py_BEGIN_ALLOW_THREADS
614 Python_Lock_Vim();
615
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200616 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200617 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200618 update_screen(VALID);
619
620 Python_Release_Vim();
621 Py_END_ALLOW_THREADS
622
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200623 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200624 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200625 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200626 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200627
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200628 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200629 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200630 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631}
632
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200633/*
634 * Function to translate a typval_T into a PyObject; this will recursively
635 * translate lists/dictionaries into their Python equivalents.
636 *
637 * The depth parameter is to avoid infinite recursion, set it to 1 when
638 * you call VimToPython.
639 */
640 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200641VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200642{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200643 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200644 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200645 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646
647 /* Avoid infinite recursion */
648 if (depth > 100)
649 {
650 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200651 ret = Py_None;
652 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200653 }
654
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200655 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656 * then and we can use it again. */
657 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
658 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
659 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200660 sprintf(ptrBuf, "%p",
661 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
662 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200663
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200664 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200666 Py_INCREF(ret);
667 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200668 }
669 }
670
671 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200672 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200673 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200674 else if (our_tv->v_type == VAR_NUMBER)
675 {
676 char buf[NUMBUFLEN];
677
678 /* For backwards compatibility numbers are stored as strings. */
679 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200680 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200681 }
682# ifdef FEAT_FLOAT
683 else if (our_tv->v_type == VAR_FLOAT)
684 {
685 char buf[NUMBUFLEN];
686
687 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200688 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689 }
690# endif
691 else if (our_tv->v_type == VAR_LIST)
692 {
693 list_T *list = our_tv->vval.v_list;
694 listitem_T *curr;
695
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200696 if (list == NULL)
697 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200698
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200699 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200700 return NULL;
701
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200702 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200703 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200704 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200705 return NULL;
706 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200707
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200708 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
709 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200710 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200713 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200714 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200715 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200716 {
717 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200718 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200719 return NULL;
720 }
721 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200722 }
723 }
724 else if (our_tv->v_type == VAR_DICT)
725 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200727 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
728 long_u todo = ht->ht_used;
729 hashitem_T *hi;
730 dictitem_T *di;
731 if (our_tv->vval.v_dict == NULL)
732 return NULL;
733
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200734 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200735 return NULL;
736
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200737 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200738 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200739 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200740 return NULL;
741 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200742
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200743 for (hi = ht->ht_array; todo > 0; ++hi)
744 {
745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200746 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200748
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200749 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200750 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200751 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200752 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200753 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200755 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200756 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200757 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200758 Py_DECREF(newObj);
759 return NULL;
760 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 }
762 }
763 }
764 else
765 {
766 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200767 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768 }
769
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200770 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200772
773 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200774VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200775{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200776 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200777 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200778 PyObject *string;
779 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200780 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200781 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200782
Bram Moolenaar389a1792013-06-23 13:00:44 +0200783 if (!PyArg_ParseTuple(args, "O", &string))
784 return NULL;
785
786 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787 return NULL;
788
789 Py_BEGIN_ALLOW_THREADS
790 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200791 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200792 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200793 Python_Release_Vim();
794 Py_END_ALLOW_THREADS
795
Bram Moolenaar389a1792013-06-23 13:00:44 +0200796 Py_XDECREF(todecref);
797
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200798 if (VimTryEnd())
799 return NULL;
800
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200801 if (our_tv == NULL)
802 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200803 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200804 return NULL;
805 }
806
807 /* Convert the Vim type into a Python type. Create a dictionary that's
808 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200809 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200810 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200811 else
812 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200813 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200814 Py_DECREF(lookup_dict);
815 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816
817
818 Py_BEGIN_ALLOW_THREADS
819 Python_Lock_Vim();
820 free_tv(our_tv);
821 Python_Release_Vim();
822 Py_END_ALLOW_THREADS
823
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200824 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200825}
826
Bram Moolenaardb913952012-06-29 12:54:53 +0200827static PyObject *ConvertToPyObject(typval_T *);
828
829 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200830VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200831{
Bram Moolenaardb913952012-06-29 12:54:53 +0200832 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200833 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200834 char_u *expr;
835 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200836
Bram Moolenaar389a1792013-06-23 13:00:44 +0200837 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200838 return NULL;
839
840 Py_BEGIN_ALLOW_THREADS
841 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200842 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200843 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200844 Python_Release_Vim();
845 Py_END_ALLOW_THREADS
846
Bram Moolenaar389a1792013-06-23 13:00:44 +0200847 Py_XDECREF(todecref);
848
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200849 if (VimTryEnd())
850 return NULL;
851
Bram Moolenaardb913952012-06-29 12:54:53 +0200852 if (our_tv == NULL)
853 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200854 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200855 return NULL;
856 }
857
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200858 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200859 Py_BEGIN_ALLOW_THREADS
860 Python_Lock_Vim();
861 free_tv(our_tv);
862 Python_Release_Vim();
863 Py_END_ALLOW_THREADS
864
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200865 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200866}
867
868 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200869VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200870{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200871 char_u *str;
872 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200873 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200874
Bram Moolenaar389a1792013-06-23 13:00:44 +0200875 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200876 return NULL;
877
Bram Moolenaara54bf402012-12-05 16:30:07 +0100878#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200879 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100880#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100882#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200883
884 Py_XDECREF(todecref);
885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200887}
888
Bram Moolenaarf4258302013-06-02 18:20:17 +0200889 static PyObject *
890_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
891{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200892 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200893 PyObject *newwd;
894 PyObject *todecref;
895 char_u *new_dir;
896
Bram Moolenaard4209d22013-06-05 20:34:15 +0200897 if (_chdir == NULL)
898 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200899 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200900 return NULL;
901
902 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
903 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200904 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200905 return NULL;
906 }
907
908 if (!(new_dir = StringToChars(newwd, &todecref)))
909 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200910 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200911 Py_DECREF(newwd);
912 return NULL;
913 }
914
915 VimTryStart();
916
917 if (vim_chdir(new_dir))
918 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200919 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200920 Py_DECREF(newwd);
921 Py_XDECREF(todecref);
922
923 if (VimTryEnd())
924 return NULL;
925
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200926 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200927 return NULL;
928 }
929
930 Py_DECREF(newwd);
931 Py_XDECREF(todecref);
932
933 post_chdir(FALSE);
934
935 if (VimTryEnd())
936 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200937 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200938 return NULL;
939 }
940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200941 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200942}
943
944 static PyObject *
945VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
946{
947 return _VimChdir(py_chdir, args, kwargs);
948}
949
950 static PyObject *
951VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
952{
953 return _VimChdir(py_fchdir, args, kwargs);
954}
955
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200956typedef struct {
957 PyObject *callable;
958 PyObject *result;
959} map_rtp_data;
960
961 static void
962map_rtp_callback(char_u *path, void *_data)
963{
964 void **data = (void **) _data;
965 PyObject *pathObject;
966 map_rtp_data *mr_data = *((map_rtp_data **) data);
967
Bram Moolenaar41009372013-07-01 22:03:04 +0200968 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200969 {
970 *data = NULL;
971 return;
972 }
973
974 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
975 pathObject, NULL);
976
977 Py_DECREF(pathObject);
978
979 if (!mr_data->result || mr_data->result != Py_None)
980 *data = NULL;
981 else
982 {
983 Py_DECREF(mr_data->result);
984 mr_data->result = NULL;
985 }
986}
987
988 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200989VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200990{
991 map_rtp_data data;
992
Bram Moolenaar389a1792013-06-23 13:00:44 +0200993 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200994 data.result = NULL;
995
996 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
997
998 if (data.result == NULL)
999 {
1000 if (PyErr_Occurred())
1001 return NULL;
1002 else
1003 {
1004 Py_INCREF(Py_None);
1005 return Py_None;
1006 }
1007 }
1008 return data.result;
1009}
1010
1011/*
1012 * _vim_runtimepath_ special path implementation.
1013 */
1014
1015 static void
1016map_finder_callback(char_u *path, void *_data)
1017{
1018 void **data = (void **) _data;
1019 PyObject *list = *((PyObject **) data);
1020 PyObject *pathObject1, *pathObject2;
1021 char *pathbuf;
1022 size_t pathlen;
1023
1024 pathlen = STRLEN(path);
1025
1026#if PY_MAJOR_VERSION < 3
1027# define PY_MAIN_DIR_STRING "python2"
1028#else
1029# define PY_MAIN_DIR_STRING "python3"
1030#endif
1031#define PY_ALTERNATE_DIR_STRING "pythonx"
1032
1033#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1034 if (!(pathbuf = PyMem_New(char,
1035 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1036 {
1037 PyErr_NoMemory();
1038 *data = NULL;
1039 return;
1040 }
1041
1042 mch_memmove(pathbuf, path, pathlen + 1);
1043 add_pathsep((char_u *) pathbuf);
1044
1045 pathlen = STRLEN(pathbuf);
1046 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1047 PYTHONX_STRING_LENGTH + 1);
1048
1049 if (!(pathObject1 = PyString_FromString(pathbuf)))
1050 {
1051 *data = NULL;
1052 PyMem_Free(pathbuf);
1053 return;
1054 }
1055
1056 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1057 PYTHONX_STRING_LENGTH + 1);
1058
1059 if (!(pathObject2 = PyString_FromString(pathbuf)))
1060 {
1061 Py_DECREF(pathObject1);
1062 PyMem_Free(pathbuf);
1063 *data = NULL;
1064 return;
1065 }
1066
1067 PyMem_Free(pathbuf);
1068
1069 if (PyList_Append(list, pathObject1)
1070 || PyList_Append(list, pathObject2))
1071 *data = NULL;
1072
1073 Py_DECREF(pathObject1);
1074 Py_DECREF(pathObject2);
1075}
1076
1077 static PyObject *
1078Vim_GetPaths(PyObject *self UNUSED)
1079{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001080 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001081
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001082 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001083 return NULL;
1084
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001085 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001086
1087 if (PyErr_Occurred())
1088 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001089 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001090 return NULL;
1091 }
1092
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001093 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001094}
1095
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001096 static PyObject *
1097call_load_module(char *name, int len, PyObject *find_module_result)
1098{
1099 PyObject *fd, *pathname, *description;
1100
Bram Moolenaarc476e522013-06-23 13:46:40 +02001101 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001102 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001103 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001104 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001105 Py_TYPE_NAME(find_module_result));
1106 return NULL;
1107 }
1108 if (PyTuple_GET_SIZE(find_module_result) != 3)
1109 {
1110 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001111 N_("expected 3-tuple as imp.find_module() result, but got "
1112 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001113 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001114 return NULL;
1115 }
1116
1117 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1118 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1119 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1120 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001121 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001122 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001123 return NULL;
1124 }
1125
1126 return PyObject_CallFunction(py_load_module,
1127 "s#OOO", name, len, fd, pathname, description);
1128}
1129
1130 static PyObject *
1131find_module(char *fullname, char *tail, PyObject *new_path)
1132{
1133 PyObject *find_module_result;
1134 PyObject *module;
1135 char *dot;
1136
Bram Moolenaar41009372013-07-01 22:03:04 +02001137 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001138 {
1139 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001140 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001141 * first component
1142 */
1143 PyObject *newest_path;
1144 int partlen = (int) (dot - 1 - tail);
1145
1146 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1147 "s#O", tail, partlen, new_path)))
1148 return NULL;
1149
1150 if (!(module = call_load_module(
1151 fullname,
1152 ((int) (tail - fullname)) + partlen,
1153 find_module_result)))
1154 {
1155 Py_DECREF(find_module_result);
1156 return NULL;
1157 }
1158
1159 Py_DECREF(find_module_result);
1160
1161 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1162 {
1163 Py_DECREF(module);
1164 return NULL;
1165 }
1166
1167 Py_DECREF(module);
1168
1169 module = find_module(fullname, dot + 1, newest_path);
1170
1171 Py_DECREF(newest_path);
1172
1173 return module;
1174 }
1175 else
1176 {
1177 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1178 "sO", tail, new_path)))
1179 return NULL;
1180
1181 if (!(module = call_load_module(
1182 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001183 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001184 find_module_result)))
1185 {
1186 Py_DECREF(find_module_result);
1187 return NULL;
1188 }
1189
1190 Py_DECREF(find_module_result);
1191
1192 return module;
1193 }
1194}
1195
1196 static PyObject *
1197FinderFindModule(PyObject *self, PyObject *args)
1198{
1199 char *fullname;
1200 PyObject *module;
1201 PyObject *new_path;
1202 LoaderObject *loader;
1203
1204 if (!PyArg_ParseTuple(args, "s", &fullname))
1205 return NULL;
1206
1207 if (!(new_path = Vim_GetPaths(self)))
1208 return NULL;
1209
1210 module = find_module(fullname, fullname, new_path);
1211
1212 Py_DECREF(new_path);
1213
1214 if (!module)
1215 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001216 if (PyErr_Occurred())
1217 {
1218 if (PyErr_ExceptionMatches(PyExc_ImportError))
1219 PyErr_Clear();
1220 else
1221 return NULL;
1222 }
1223
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001224 Py_INCREF(Py_None);
1225 return Py_None;
1226 }
1227
1228 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1229 {
1230 Py_DECREF(module);
1231 return NULL;
1232 }
1233
1234 loader->module = module;
1235
1236 return (PyObject *) loader;
1237}
1238
1239 static PyObject *
1240VimPathHook(PyObject *self UNUSED, PyObject *args)
1241{
1242 char *path;
1243
1244 if (PyArg_ParseTuple(args, "s", &path)
1245 && STRCMP(path, vim_special_path) == 0)
1246 {
1247 Py_INCREF(vim_module);
1248 return vim_module;
1249 }
1250
1251 PyErr_Clear();
1252 PyErr_SetNone(PyExc_ImportError);
1253 return NULL;
1254}
1255
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001256/*
1257 * Vim module - Definitions
1258 */
1259
1260static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001261 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001262 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001263 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001264 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1265 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001266 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1267 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001268 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001269 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001270 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1271 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1272 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001273};
1274
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001275/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001276 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001277 */
1278
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001279static PyTypeObject IterType;
1280
1281typedef PyObject *(*nextfun)(void **);
1282typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001283typedef int (*traversefun)(void *, visitproc, void *);
1284typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001285
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001286/* Main purpose of this object is removing the need for do python
1287 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1288 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001289
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001290typedef struct
1291{
1292 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001293 void *cur;
1294 nextfun next;
1295 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001296 traversefun traverse;
1297 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001298} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001299
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001300 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001301IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1302 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001303{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001304 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001305
Bram Moolenaar774267b2013-05-21 20:51:59 +02001306 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001307 self->cur = start;
1308 self->next = next;
1309 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001310 self->traverse = traverse;
1311 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001312
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001313 return (PyObject *)(self);
1314}
1315
1316 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001317IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001318{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001319 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001320 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001321 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001322}
1323
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001325IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001326{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001327 if (self->traverse != NULL)
1328 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001329 else
1330 return 0;
1331}
1332
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001333/* Mac OSX defines clear() somewhere. */
1334#ifdef clear
1335# undef clear
1336#endif
1337
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001338 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001339IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001340{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001341 if (self->clear != NULL)
1342 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001343 else
1344 return 0;
1345}
1346
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001347 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001349{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001350 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001351}
1352
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001353 static PyObject *
1354IterIter(PyObject *self)
1355{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001356 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001357 return self;
1358}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001359
Bram Moolenaardb913952012-06-29 12:54:53 +02001360typedef struct pylinkedlist_S {
1361 struct pylinkedlist_S *pll_next;
1362 struct pylinkedlist_S *pll_prev;
1363 PyObject *pll_obj;
1364} pylinkedlist_T;
1365
1366static pylinkedlist_T *lastdict = NULL;
1367static pylinkedlist_T *lastlist = NULL;
1368
1369 static void
1370pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1371{
1372 if (ref->pll_prev == NULL)
1373 {
1374 if (ref->pll_next == NULL)
1375 {
1376 *last = NULL;
1377 return;
1378 }
1379 }
1380 else
1381 ref->pll_prev->pll_next = ref->pll_next;
1382
1383 if (ref->pll_next == NULL)
1384 *last = ref->pll_prev;
1385 else
1386 ref->pll_next->pll_prev = ref->pll_prev;
1387}
1388
1389 static void
1390pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1391{
1392 if (*last == NULL)
1393 ref->pll_prev = NULL;
1394 else
1395 {
1396 (*last)->pll_next = ref;
1397 ref->pll_prev = *last;
1398 }
1399 ref->pll_next = NULL;
1400 ref->pll_obj = self;
1401 *last = ref;
1402}
1403
1404static PyTypeObject DictionaryType;
1405
1406typedef struct
1407{
1408 PyObject_HEAD
1409 dict_T *dict;
1410 pylinkedlist_T ref;
1411} DictionaryObject;
1412
Bram Moolenaara9922d62013-05-30 13:01:18 +02001413static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1414
1415#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1416
Bram Moolenaardb913952012-06-29 12:54:53 +02001417 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001418DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001419{
1420 DictionaryObject *self;
1421
Bram Moolenaara9922d62013-05-30 13:01:18 +02001422 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001423 if (self == NULL)
1424 return NULL;
1425 self->dict = dict;
1426 ++dict->dv_refcount;
1427
1428 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1429
1430 return (PyObject *)(self);
1431}
1432
Bram Moolenaara9922d62013-05-30 13:01:18 +02001433 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001434py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001435{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001436 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001437
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001438 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001439 {
1440 PyErr_NoMemory();
1441 return NULL;
1442 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001443 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001444
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001445 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001446}
1447
1448 static PyObject *
1449DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1450{
1451 DictionaryObject *self;
1452 dict_T *dict;
1453
1454 if (!(dict = py_dict_alloc()))
1455 return NULL;
1456
1457 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1458
1459 --dict->dv_refcount;
1460
1461 if (kwargs || PyTuple_Size(args))
1462 {
1463 PyObject *tmp;
1464 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1465 {
1466 Py_DECREF(self);
1467 return NULL;
1468 }
1469
1470 Py_DECREF(tmp);
1471 }
1472
1473 return (PyObject *)(self);
1474}
1475
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001476 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001477DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001478{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001479 pyll_remove(&self->ref, &lastdict);
1480 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001481
1482 DESTRUCTOR_FINISH(self);
1483}
1484
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001485static char *DictionaryAttrs[] = {
1486 "locked", "scope",
1487 NULL
1488};
1489
1490 static PyObject *
1491DictionaryDir(PyObject *self)
1492{
1493 return ObjectDir(self, DictionaryAttrs);
1494}
1495
Bram Moolenaardb913952012-06-29 12:54:53 +02001496 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001497DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001498{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001499 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001500 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001501 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001502 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001503 return -1;
1504 }
1505
1506 if (strcmp(name, "locked") == 0)
1507 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001508 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001509 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001510 PyErr_SET_STRING(PyExc_TypeError,
1511 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001512 return -1;
1513 }
1514 else
1515 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001516 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001517 if (istrue == -1)
1518 return -1;
1519 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001520 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001521 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001522 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001523 }
1524 return 0;
1525 }
1526 else
1527 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001528 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001529 return -1;
1530 }
1531}
1532
1533 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001534DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001535{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001536 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001537}
1538
Bram Moolenaara9922d62013-05-30 13:01:18 +02001539#define DICT_FLAG_HAS_DEFAULT 0x01
1540#define DICT_FLAG_POP 0x02
1541#define DICT_FLAG_NONE_DEFAULT 0x04
1542#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1543#define DICT_FLAG_RETURN_PAIR 0x10
1544
Bram Moolenaardb913952012-06-29 12:54:53 +02001545 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001546_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001547{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001548 PyObject *keyObject;
1549 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001550 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001551 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001552 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001553 dict_T *dict = self->dict;
1554 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001555 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001556
Bram Moolenaara9922d62013-05-30 13:01:18 +02001557 if (flags & DICT_FLAG_HAS_DEFAULT)
1558 {
1559 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1560 return NULL;
1561 }
1562 else
1563 keyObject = args;
1564
1565 if (flags & DICT_FLAG_RETURN_BOOL)
1566 defObject = Py_False;
1567
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001568 if (!(key = StringToChars(keyObject, &todecref)))
1569 return NULL;
1570
1571 if (*key == NUL)
1572 {
1573 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001574 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001575 return NULL;
1576 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001577
Bram Moolenaara9922d62013-05-30 13:01:18 +02001578 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001579
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001580 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001581
Bram Moolenaara9922d62013-05-30 13:01:18 +02001582 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001583 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001584 if (defObject)
1585 {
1586 Py_INCREF(defObject);
1587 return defObject;
1588 }
1589 else
1590 {
1591 PyErr_SetObject(PyExc_KeyError, keyObject);
1592 return NULL;
1593 }
1594 }
1595 else if (flags & DICT_FLAG_RETURN_BOOL)
1596 {
1597 Py_INCREF(Py_True);
1598 return Py_True;
1599 }
1600
1601 di = dict_lookup(hi);
1602
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001603 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001604 return NULL;
1605
1606 if (flags & DICT_FLAG_POP)
1607 {
1608 if (dict->dv_lock)
1609 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001610 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001611 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001612 return NULL;
1613 }
1614
1615 hash_remove(&dict->dv_hashtab, hi);
1616 dictitem_free(di);
1617 }
1618
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001619 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001620}
1621
1622 static PyObject *
1623DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1624{
1625 return _DictionaryItem(self, keyObject, 0);
1626}
1627
1628 static int
1629DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1630{
1631 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001632 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001633
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001634 if (rObj == NULL)
1635 return -1;
1636
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001637 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001638
Bram Moolenaardee2e312013-06-23 16:35:47 +02001639 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001640
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001641 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001642}
1643
1644typedef struct
1645{
1646 hashitem_T *ht_array;
1647 long_u ht_used;
1648 hashtab_T *ht;
1649 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001650 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001651} dictiterinfo_T;
1652
1653 static PyObject *
1654DictionaryIterNext(dictiterinfo_T **dii)
1655{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001656 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001657
1658 if (!(*dii)->todo)
1659 return NULL;
1660
1661 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1662 (*dii)->ht->ht_used != (*dii)->ht_used)
1663 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001664 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001665 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001666 return NULL;
1667 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001668
Bram Moolenaara9922d62013-05-30 13:01:18 +02001669 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1670 ++((*dii)->hi);
1671
1672 --((*dii)->todo);
1673
Bram Moolenaar41009372013-07-01 22:03:04 +02001674 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001675 return NULL;
1676
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001677 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001678}
1679
1680 static PyObject *
1681DictionaryIter(DictionaryObject *self)
1682{
1683 dictiterinfo_T *dii;
1684 hashtab_T *ht;
1685
1686 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1687 {
1688 PyErr_NoMemory();
1689 return NULL;
1690 }
1691
1692 ht = &self->dict->dv_hashtab;
1693 dii->ht_array = ht->ht_array;
1694 dii->ht_used = ht->ht_used;
1695 dii->ht = ht;
1696 dii->hi = dii->ht_array;
1697 dii->todo = dii->ht_used;
1698
1699 return IterNew(dii,
1700 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1701 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001702}
1703
1704 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001705DictionaryAssItem(
1706 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001707{
1708 char_u *key;
1709 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001710 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001711 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001712 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001713
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001714 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001715 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001716 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001717 return -1;
1718 }
1719
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001720 if (!(key = StringToChars(keyObject, &todecref)))
1721 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001722
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001723 if (*key == NUL)
1724 {
1725 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001726 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001727 return -1;
1728 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001729
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001730 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001731
1732 if (valObject == NULL)
1733 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001734 hashitem_T *hi;
1735
Bram Moolenaardb913952012-06-29 12:54:53 +02001736 if (di == NULL)
1737 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001738 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001739 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001740 return -1;
1741 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001742 hi = hash_find(&dict->dv_hashtab, di->di_key);
1743 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001744 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001745 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001746 return 0;
1747 }
1748
1749 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001750 {
1751 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001752 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001753 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001754
1755 if (di == NULL)
1756 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001757 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001758 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001759 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001760 PyErr_NoMemory();
1761 return -1;
1762 }
1763 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001764 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001765
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001766 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001767 {
1768 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001769 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001770 RAISE_KEY_ADD_FAIL(key);
1771 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001772 return -1;
1773 }
1774 }
1775 else
1776 clear_tv(&di->di_tv);
1777
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001778 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001779
1780 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001781 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001782 return 0;
1783}
1784
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785typedef PyObject *(*hi_to_py)(hashitem_T *);
1786
Bram Moolenaardb913952012-06-29 12:54:53 +02001787 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001789{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001790 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001791 long_u todo = dict->dv_hashtab.ht_used;
1792 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001793 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001794 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001795 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001796
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001797 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001798 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1799 {
1800 if (!HASHITEM_EMPTY(hi))
1801 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001802 if (!(newObj = hiconvert(hi)))
1803 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001804 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001805 return NULL;
1806 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001807 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001808 --todo;
1809 ++i;
1810 }
1811 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001812 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001813}
1814
Bram Moolenaara9922d62013-05-30 13:01:18 +02001815 static PyObject *
1816dict_key(hashitem_T *hi)
1817{
1818 return PyBytes_FromString((char *)(hi->hi_key));
1819}
1820
1821 static PyObject *
1822DictionaryListKeys(DictionaryObject *self)
1823{
1824 return DictionaryListObjects(self, dict_key);
1825}
1826
1827 static PyObject *
1828dict_val(hashitem_T *hi)
1829{
1830 dictitem_T *di;
1831
1832 di = dict_lookup(hi);
1833 return ConvertToPyObject(&di->di_tv);
1834}
1835
1836 static PyObject *
1837DictionaryListValues(DictionaryObject *self)
1838{
1839 return DictionaryListObjects(self, dict_val);
1840}
1841
1842 static PyObject *
1843dict_item(hashitem_T *hi)
1844{
1845 PyObject *keyObject;
1846 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001847 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001848
1849 if (!(keyObject = dict_key(hi)))
1850 return NULL;
1851
1852 if (!(valObject = dict_val(hi)))
1853 {
1854 Py_DECREF(keyObject);
1855 return NULL;
1856 }
1857
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001858 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001859
1860 Py_DECREF(keyObject);
1861 Py_DECREF(valObject);
1862
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001863 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001864}
1865
1866 static PyObject *
1867DictionaryListItems(DictionaryObject *self)
1868{
1869 return DictionaryListObjects(self, dict_item);
1870}
1871
1872 static PyObject *
1873DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1874{
1875 dict_T *dict = self->dict;
1876
1877 if (dict->dv_lock)
1878 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001879 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001880 return NULL;
1881 }
1882
1883 if (kwargs)
1884 {
1885 typval_T tv;
1886
1887 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1888 return NULL;
1889
1890 VimTryStart();
1891 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1892 clear_tv(&tv);
1893 if (VimTryEnd())
1894 return NULL;
1895 }
1896 else
1897 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001898 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001899
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001900 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001901 return NULL;
1902
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001903 if (PyObject_HasAttrString(obj, "keys"))
1904 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001905 else
1906 {
1907 PyObject *iterator;
1908 PyObject *item;
1909
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001910 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001911 return NULL;
1912
1913 while ((item = PyIter_Next(iterator)))
1914 {
1915 PyObject *fast;
1916 PyObject *keyObject;
1917 PyObject *valObject;
1918 PyObject *todecref;
1919 char_u *key;
1920 dictitem_T *di;
1921
1922 if (!(fast = PySequence_Fast(item, "")))
1923 {
1924 Py_DECREF(iterator);
1925 Py_DECREF(item);
1926 return NULL;
1927 }
1928
1929 Py_DECREF(item);
1930
1931 if (PySequence_Fast_GET_SIZE(fast) != 2)
1932 {
1933 Py_DECREF(iterator);
1934 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001935 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001936 N_("expected sequence element of size 2, "
1937 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001938 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939 return NULL;
1940 }
1941
1942 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1943
1944 if (!(key = StringToChars(keyObject, &todecref)))
1945 {
1946 Py_DECREF(iterator);
1947 Py_DECREF(fast);
1948 return NULL;
1949 }
1950
1951 di = dictitem_alloc(key);
1952
1953 Py_XDECREF(todecref);
1954
1955 if (di == NULL)
1956 {
1957 Py_DECREF(fast);
1958 Py_DECREF(iterator);
1959 PyErr_NoMemory();
1960 return NULL;
1961 }
1962 di->di_tv.v_lock = 0;
1963 di->di_tv.v_type = VAR_UNKNOWN;
1964
1965 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1966
1967 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1968 {
1969 Py_DECREF(iterator);
1970 Py_DECREF(fast);
1971 dictitem_free(di);
1972 return NULL;
1973 }
1974
1975 Py_DECREF(fast);
1976
1977 if (dict_add(dict, di) == FAIL)
1978 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001979 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001980 Py_DECREF(iterator);
1981 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001982 return NULL;
1983 }
1984 }
1985
1986 Py_DECREF(iterator);
1987
1988 /* Iterator may have finished due to an exception */
1989 if (PyErr_Occurred())
1990 return NULL;
1991 }
1992 }
1993 Py_INCREF(Py_None);
1994 return Py_None;
1995}
1996
1997 static PyObject *
1998DictionaryGet(DictionaryObject *self, PyObject *args)
1999{
2000 return _DictionaryItem(self, args,
2001 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2002}
2003
2004 static PyObject *
2005DictionaryPop(DictionaryObject *self, PyObject *args)
2006{
2007 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2008}
2009
2010 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002011DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002013 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002014 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002015 PyObject *valObject;
2016 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002017
Bram Moolenaarde71b562013-06-02 17:41:54 +02002018 if (self->dict->dv_hashtab.ht_used == 0)
2019 {
2020 PyErr_SetNone(PyExc_KeyError);
2021 return NULL;
2022 }
2023
2024 hi = self->dict->dv_hashtab.ht_array;
2025 while (HASHITEM_EMPTY(hi))
2026 ++hi;
2027
2028 di = dict_lookup(hi);
2029
2030 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002031 return NULL;
2032
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002033 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002034 {
2035 Py_DECREF(valObject);
2036 return NULL;
2037 }
2038
2039 hash_remove(&self->dict->dv_hashtab, hi);
2040 dictitem_free(di);
2041
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002042 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002043}
2044
2045 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002046DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002047{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002048 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2049}
2050
2051static PySequenceMethods DictionaryAsSeq = {
2052 0, /* sq_length */
2053 0, /* sq_concat */
2054 0, /* sq_repeat */
2055 0, /* sq_item */
2056 0, /* sq_slice */
2057 0, /* sq_ass_item */
2058 0, /* sq_ass_slice */
2059 (objobjproc) DictionaryContains, /* sq_contains */
2060 0, /* sq_inplace_concat */
2061 0, /* sq_inplace_repeat */
2062};
2063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002064static PyMappingMethods DictionaryAsMapping = {
2065 (lenfunc) DictionaryLength,
2066 (binaryfunc) DictionaryItem,
2067 (objobjargproc) DictionaryAssItem,
2068};
2069
Bram Moolenaardb913952012-06-29 12:54:53 +02002070static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002071 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002072 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2073 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2074 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2075 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2076 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002077 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002078 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002079 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2080 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002081};
2082
2083static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002084static PySequenceMethods ListAsSeq;
2085static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002086
2087typedef struct
2088{
2089 PyObject_HEAD
2090 list_T *list;
2091 pylinkedlist_T ref;
2092} ListObject;
2093
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002094#define NEW_LIST(list) ListNew(&ListType, list)
2095
Bram Moolenaardb913952012-06-29 12:54:53 +02002096 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002097ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002098{
2099 ListObject *self;
2100
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002101 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002102 if (self == NULL)
2103 return NULL;
2104 self->list = list;
2105 ++list->lv_refcount;
2106
2107 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2108
2109 return (PyObject *)(self);
2110}
2111
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002112 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002113py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002114{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002115 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002116
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002117 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002118 {
2119 PyErr_NoMemory();
2120 return NULL;
2121 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002122 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002123
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002124 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002125}
2126
2127 static int
2128list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2129{
2130 PyObject *iterator;
2131 PyObject *item;
2132 listitem_T *li;
2133
2134 if (!(iterator = PyObject_GetIter(obj)))
2135 return -1;
2136
2137 while ((item = PyIter_Next(iterator)))
2138 {
2139 if (!(li = listitem_alloc()))
2140 {
2141 PyErr_NoMemory();
2142 Py_DECREF(item);
2143 Py_DECREF(iterator);
2144 return -1;
2145 }
2146 li->li_tv.v_lock = 0;
2147 li->li_tv.v_type = VAR_UNKNOWN;
2148
2149 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2150 {
2151 Py_DECREF(item);
2152 Py_DECREF(iterator);
2153 listitem_free(li);
2154 return -1;
2155 }
2156
2157 Py_DECREF(item);
2158
2159 list_append(l, li);
2160 }
2161
2162 Py_DECREF(iterator);
2163
2164 /* Iterator may have finished due to an exception */
2165 if (PyErr_Occurred())
2166 return -1;
2167
2168 return 0;
2169}
2170
2171 static PyObject *
2172ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2173{
2174 list_T *list;
2175 PyObject *obj = NULL;
2176
2177 if (kwargs)
2178 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002179 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002180 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002181 return NULL;
2182 }
2183
2184 if (!PyArg_ParseTuple(args, "|O", &obj))
2185 return NULL;
2186
2187 if (!(list = py_list_alloc()))
2188 return NULL;
2189
2190 if (obj)
2191 {
2192 PyObject *lookup_dict;
2193
2194 if (!(lookup_dict = PyDict_New()))
2195 {
2196 list_unref(list);
2197 return NULL;
2198 }
2199
2200 if (list_py_concat(list, obj, lookup_dict) == -1)
2201 {
2202 Py_DECREF(lookup_dict);
2203 list_unref(list);
2204 return NULL;
2205 }
2206
2207 Py_DECREF(lookup_dict);
2208 }
2209
2210 return ListNew(subtype, list);
2211}
2212
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002213 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002214ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002215{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002216 pyll_remove(&self->ref, &lastlist);
2217 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002218
2219 DESTRUCTOR_FINISH(self);
2220}
2221
Bram Moolenaardb913952012-06-29 12:54:53 +02002222 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002223ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002224{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002225 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002226}
2227
2228 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002229ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002230{
2231 listitem_T *li;
2232
Bram Moolenaard6e39182013-05-21 18:30:34 +02002233 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002234 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002235 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002236 return NULL;
2237 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002238 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002239 if (li == NULL)
2240 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002241 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002242 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002243 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002244 return NULL;
2245 }
2246 return ConvertToPyObject(&li->li_tv);
2247}
2248
2249#define PROC_RANGE \
2250 if (last < 0) {\
2251 if (last < -size) \
2252 last = 0; \
2253 else \
2254 last += size; \
2255 } \
2256 if (first < 0) \
2257 first = 0; \
2258 if (first > size) \
2259 first = size; \
2260 if (last > size) \
2261 last = size;
2262
2263 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002264ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002265{
2266 PyInt i;
2267 PyInt size = ListLength(self);
2268 PyInt n;
2269 PyObject *list;
2270 int reversed = 0;
2271
2272 PROC_RANGE
2273 if (first >= last)
2274 first = last;
2275
2276 n = last-first;
2277 list = PyList_New(n);
2278 if (list == NULL)
2279 return NULL;
2280
2281 for (i = 0; i < n; ++i)
2282 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002283 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002284 if (item == NULL)
2285 {
2286 Py_DECREF(list);
2287 return NULL;
2288 }
2289
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002290 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002291 }
2292
2293 return list;
2294}
2295
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002296typedef struct
2297{
2298 listwatch_T lw;
2299 list_T *list;
2300} listiterinfo_T;
2301
2302 static void
2303ListIterDestruct(listiterinfo_T *lii)
2304{
2305 list_rem_watch(lii->list, &lii->lw);
2306 PyMem_Free(lii);
2307}
2308
2309 static PyObject *
2310ListIterNext(listiterinfo_T **lii)
2311{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002312 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002313
2314 if (!((*lii)->lw.lw_item))
2315 return NULL;
2316
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002317 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002318 return NULL;
2319
2320 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2321
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002322 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002323}
2324
2325 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002326ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002327{
2328 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002329 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002330
2331 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2332 {
2333 PyErr_NoMemory();
2334 return NULL;
2335 }
2336
2337 list_add_watch(l, &lii->lw);
2338 lii->lw.lw_item = l->lv_first;
2339 lii->list = l;
2340
2341 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002342 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2343 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002344}
2345
Bram Moolenaardb913952012-06-29 12:54:53 +02002346 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002347ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002348{
2349 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002350 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002351 listitem_T *li;
2352 Py_ssize_t length = ListLength(self);
2353
2354 if (l->lv_lock)
2355 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002356 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002357 return -1;
2358 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002359 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002360 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002361 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002362 return -1;
2363 }
2364
2365 if (obj == NULL)
2366 {
2367 li = list_find(l, (long) index);
2368 list_remove(l, li, li);
2369 clear_tv(&li->li_tv);
2370 vim_free(li);
2371 return 0;
2372 }
2373
2374 if (ConvertFromPyObject(obj, &tv) == -1)
2375 return -1;
2376
2377 if (index == length)
2378 {
2379 if (list_append_tv(l, &tv) == FAIL)
2380 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002381 clear_tv(&tv);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002382 PyErr_SET_VIM(N_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002383 return -1;
2384 }
2385 }
2386 else
2387 {
2388 li = list_find(l, (long) index);
2389 clear_tv(&li->li_tv);
2390 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002391 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002392 }
2393 return 0;
2394}
2395
2396 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002397ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002398{
2399 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002400 PyObject *iterator;
2401 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002402 listitem_T *li;
2403 listitem_T *next;
2404 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002405 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002406 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002407
2408 if (l->lv_lock)
2409 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002410 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002411 return -1;
2412 }
2413
2414 PROC_RANGE
2415
2416 if (first == size)
2417 li = NULL;
2418 else
2419 {
2420 li = list_find(l, (long) first);
2421 if (li == NULL)
2422 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002423 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2424 (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002425 return -1;
2426 }
2427 if (last > first)
2428 {
2429 i = last - first;
2430 while (i-- && li != NULL)
2431 {
2432 next = li->li_next;
2433 listitem_remove(l, li);
2434 li = next;
2435 }
2436 }
2437 }
2438
2439 if (obj == NULL)
2440 return 0;
2441
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002442 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002443 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002444
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002445 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002446 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002447 if (ConvertFromPyObject(item, &v) == -1)
2448 {
2449 Py_DECREF(iterator);
2450 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002451 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002452 }
2453 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002454 if (list_insert_tv(l, &v, li) == FAIL)
2455 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002456 clear_tv(&v);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002457 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002458 return -1;
2459 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002460 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002461 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002462 Py_DECREF(iterator);
Bram Moolenaardee2e312013-06-23 16:35:47 +02002463
2464 if (PyErr_Occurred())
2465 return -1;
2466
Bram Moolenaardb913952012-06-29 12:54:53 +02002467 return 0;
2468}
2469
2470 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002471ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002472{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002473 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002474 PyObject *lookup_dict;
2475
2476 if (l->lv_lock)
2477 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002478 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002479 return NULL;
2480 }
2481
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002482 if (!(lookup_dict = PyDict_New()))
2483 return NULL;
2484
Bram Moolenaardb913952012-06-29 12:54:53 +02002485 if (list_py_concat(l, obj, lookup_dict) == -1)
2486 {
2487 Py_DECREF(lookup_dict);
2488 return NULL;
2489 }
2490 Py_DECREF(lookup_dict);
2491
2492 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002493 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002494}
2495
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002496static char *ListAttrs[] = {
2497 "locked",
2498 NULL
2499};
2500
2501 static PyObject *
2502ListDir(PyObject *self)
2503{
2504 return ObjectDir(self, ListAttrs);
2505}
2506
Bram Moolenaar66b79852012-09-21 14:00:35 +02002507 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002508ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002509{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002510 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002511 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002512 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002513 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002514 return -1;
2515 }
2516
2517 if (strcmp(name, "locked") == 0)
2518 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002519 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002520 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002521 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002522 return -1;
2523 }
2524 else
2525 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002526 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002527 if (istrue == -1)
2528 return -1;
2529 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002530 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002531 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002532 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002533 }
2534 return 0;
2535 }
2536 else
2537 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002538 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002539 return -1;
2540 }
2541}
2542
Bram Moolenaardb913952012-06-29 12:54:53 +02002543static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002544 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2545 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2546 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002547};
2548
2549typedef struct
2550{
2551 PyObject_HEAD
2552 char_u *name;
2553} FunctionObject;
2554
2555static PyTypeObject FunctionType;
2556
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002557#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2558
Bram Moolenaardb913952012-06-29 12:54:53 +02002559 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002560FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002561{
2562 FunctionObject *self;
2563
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002564 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2565
Bram Moolenaardb913952012-06-29 12:54:53 +02002566 if (self == NULL)
2567 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002568
2569 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002570 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002571 if (!translated_function_exists(name))
2572 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002573 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002574 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002575 return NULL;
2576 }
2577 self->name = vim_strsave(name);
2578 func_ref(self->name);
2579 }
2580 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002581 if ((self->name = get_expanded_name(name,
2582 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2583 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002584 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002585 PyErr_FORMAT(PyExc_ValueError,
2586 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002587 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002588 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002589
2590 return (PyObject *)(self);
2591}
2592
2593 static PyObject *
2594FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2595{
2596 PyObject *self;
2597 char_u *name;
2598
2599 if (kwargs)
2600 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002601 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002602 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002603 return NULL;
2604 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002605
Bram Moolenaar389a1792013-06-23 13:00:44 +02002606 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002607 return NULL;
2608
2609 self = FunctionNew(subtype, name);
2610
Bram Moolenaar389a1792013-06-23 13:00:44 +02002611 PyMem_Free(name);
2612
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002613 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002614}
2615
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002616 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002617FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002618{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002619 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002620 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002621
2622 DESTRUCTOR_FINISH(self);
2623}
2624
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002625static char *FunctionAttrs[] = {
2626 "softspace",
2627 NULL
2628};
2629
2630 static PyObject *
2631FunctionDir(PyObject *self)
2632{
2633 return ObjectDir(self, FunctionAttrs);
2634}
2635
Bram Moolenaardb913952012-06-29 12:54:53 +02002636 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002637FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002638{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002639 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002640 typval_T args;
2641 typval_T selfdicttv;
2642 typval_T rettv;
2643 dict_T *selfdict = NULL;
2644 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002645 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002646 int error;
2647
2648 if (ConvertFromPyObject(argsObject, &args) == -1)
2649 return NULL;
2650
2651 if (kwargs != NULL)
2652 {
2653 selfdictObject = PyDict_GetItemString(kwargs, "self");
2654 if (selfdictObject != NULL)
2655 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002656 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002657 {
2658 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002659 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002660 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002661 selfdict = selfdicttv.vval.v_dict;
2662 }
2663 }
2664
Bram Moolenaar71700b82013-05-15 17:49:05 +02002665 Py_BEGIN_ALLOW_THREADS
2666 Python_Lock_Vim();
2667
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002668 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002669 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002670
2671 Python_Release_Vim();
2672 Py_END_ALLOW_THREADS
2673
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002674 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002675 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002676 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002677 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002678 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002679 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002680 }
2681 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002682 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002683
Bram Moolenaardb913952012-06-29 12:54:53 +02002684 clear_tv(&args);
2685 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002686 if (selfdict != NULL)
2687 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002688
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002689 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002690}
2691
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002692 static PyObject *
2693FunctionRepr(FunctionObject *self)
2694{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002695#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002696 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002697 * Finalize */
2698 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002699 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002700#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002701 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002702#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002703}
2704
Bram Moolenaardb913952012-06-29 12:54:53 +02002705static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002706 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2707 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002708};
2709
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002710/*
2711 * Options object
2712 */
2713
2714static PyTypeObject OptionsType;
2715
2716typedef int (*checkfun)(void *);
2717
2718typedef struct
2719{
2720 PyObject_HEAD
2721 int opt_type;
2722 void *from;
2723 checkfun Check;
2724 PyObject *fromObj;
2725} OptionsObject;
2726
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002727 static int
2728dummy_check(void *arg UNUSED)
2729{
2730 return 0;
2731}
2732
2733 static PyObject *
2734OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2735{
2736 OptionsObject *self;
2737
Bram Moolenaar774267b2013-05-21 20:51:59 +02002738 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002739 if (self == NULL)
2740 return NULL;
2741
2742 self->opt_type = opt_type;
2743 self->from = from;
2744 self->Check = Check;
2745 self->fromObj = fromObj;
2746 if (fromObj)
2747 Py_INCREF(fromObj);
2748
2749 return (PyObject *)(self);
2750}
2751
2752 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002753OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002754{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002755 PyObject_GC_UnTrack((void *)(self));
2756 Py_XDECREF(self->fromObj);
2757 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002758}
2759
2760 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002761OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002762{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002763 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002764 return 0;
2765}
2766
2767 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002768OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002769{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002770 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002771 return 0;
2772}
2773
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002774 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002775OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002776{
2777 char_u *key;
2778 int flags;
2779 long numval;
2780 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002781 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002782
Bram Moolenaard6e39182013-05-21 18:30:34 +02002783 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002784 return NULL;
2785
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002786 if (!(key = StringToChars(keyObject, &todecref)))
2787 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002788
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002789 if (*key == NUL)
2790 {
2791 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002792 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002793 return NULL;
2794 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002795
2796 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002797 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002798
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002799 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002800
2801 if (flags == 0)
2802 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002803 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002804 return NULL;
2805 }
2806
2807 if (flags & SOPT_UNSET)
2808 {
2809 Py_INCREF(Py_None);
2810 return Py_None;
2811 }
2812 else if (flags & SOPT_BOOL)
2813 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002814 PyObject *ret;
2815 ret = numval ? Py_True : Py_False;
2816 Py_INCREF(ret);
2817 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002818 }
2819 else if (flags & SOPT_NUM)
2820 return PyInt_FromLong(numval);
2821 else if (flags & SOPT_STRING)
2822 {
2823 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002824 {
Bram Moolenaar41009372013-07-01 22:03:04 +02002825 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002826 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002827 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002828 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002829 else
2830 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002831 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002832 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002833 return NULL;
2834 }
2835 }
2836 else
2837 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002838 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002839 return NULL;
2840 }
2841}
2842
2843 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002844set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002845{
2846 char_u *errmsg;
2847
2848 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2849 {
2850 if (VimTryEnd())
2851 return FAIL;
2852 PyErr_SetVim((char *)errmsg);
2853 return FAIL;
2854 }
2855 return OK;
2856}
2857
2858 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002859set_option_value_for(
2860 char_u *key,
2861 int numval,
2862 char_u *stringval,
2863 int opt_flags,
2864 int opt_type,
2865 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002866{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002867 win_T *save_curwin = NULL;
2868 tabpage_T *save_curtab = NULL;
2869 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002870 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002871
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002872 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002873 switch (opt_type)
2874 {
2875 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002876 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002877 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002878 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002879 if (VimTryEnd())
2880 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002881 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002882 return -1;
2883 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002884 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2885 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002886 break;
2887 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002888 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002889 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002890 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002891 break;
2892 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002893 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002894 break;
2895 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002896 if (set_ret == FAIL)
2897 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002898 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002899}
2900
2901 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002902OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002903{
2904 char_u *key;
2905 int flags;
2906 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002907 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002908 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002909
Bram Moolenaard6e39182013-05-21 18:30:34 +02002910 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002911 return -1;
2912
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002913 if (!(key = StringToChars(keyObject, &todecref)))
2914 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002915
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002916 if (*key == NUL)
2917 {
2918 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002919 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002920 return -1;
2921 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002922
2923 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002924 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002925
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002926 if (flags == 0)
2927 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002928 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002929 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002930 return -1;
2931 }
2932
2933 if (valObject == NULL)
2934 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002935 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002936 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002937 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002938 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002939 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002940 return -1;
2941 }
2942 else if (!(flags & SOPT_GLOBAL))
2943 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002944 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002945 N_("unable to unset option %s "
2946 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002947 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002948 return -1;
2949 }
2950 else
2951 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002952 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002953 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002954 return 0;
2955 }
2956 }
2957
Bram Moolenaard6e39182013-05-21 18:30:34 +02002958 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002959
2960 if (flags & SOPT_BOOL)
2961 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002962 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002963
Bram Moolenaarb983f752013-05-15 16:11:50 +02002964 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002965 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002966 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002967 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002968 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002969 }
2970 else if (flags & SOPT_NUM)
2971 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002972 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002973
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002974 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002975 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002976 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002977 return -1;
2978 }
2979
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002980 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002981 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002982 }
2983 else
2984 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002985 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002986 PyObject *todecref;
2987
2988 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002989 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002990 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002991 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002992 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002993 }
2994
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002995 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002996
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002997 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002998}
2999
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003000static PyMappingMethods OptionsAsMapping = {
3001 (lenfunc) NULL,
3002 (binaryfunc) OptionsItem,
3003 (objobjargproc) OptionsAssItem,
3004};
3005
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003006/* Tabpage object
3007 */
3008
3009typedef struct
3010{
3011 PyObject_HEAD
3012 tabpage_T *tab;
3013} TabPageObject;
3014
3015static PyObject *WinListNew(TabPageObject *tabObject);
3016
3017static PyTypeObject TabPageType;
3018
3019 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003020CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003021{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003022 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003023 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003024 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003025 return -1;
3026 }
3027
3028 return 0;
3029}
3030
3031 static PyObject *
3032TabPageNew(tabpage_T *tab)
3033{
3034 TabPageObject *self;
3035
3036 if (TAB_PYTHON_REF(tab))
3037 {
3038 self = TAB_PYTHON_REF(tab);
3039 Py_INCREF(self);
3040 }
3041 else
3042 {
3043 self = PyObject_NEW(TabPageObject, &TabPageType);
3044 if (self == NULL)
3045 return NULL;
3046 self->tab = tab;
3047 TAB_PYTHON_REF(tab) = self;
3048 }
3049
3050 return (PyObject *)(self);
3051}
3052
3053 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003054TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003055{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003056 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3057 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003058
3059 DESTRUCTOR_FINISH(self);
3060}
3061
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003062static char *TabPageAttrs[] = {
3063 "windows", "number", "vars", "window", "valid",
3064 NULL
3065};
3066
3067 static PyObject *
3068TabPageDir(PyObject *self)
3069{
3070 return ObjectDir(self, TabPageAttrs);
3071}
3072
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003073 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003074TabPageAttrValid(TabPageObject *self, char *name)
3075{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003076 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003077
3078 if (strcmp(name, "valid") != 0)
3079 return NULL;
3080
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003081 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3082 Py_INCREF(ret);
3083 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003084}
3085
3086 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003087TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003088{
3089 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003090 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003091 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003092 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003093 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003094 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003095 else if (strcmp(name, "window") == 0)
3096 {
3097 /* For current tab window.c does not bother to set or update tp_curwin
3098 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003099 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003100 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003101 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003102 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003103 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003104 else if (strcmp(name, "__members__") == 0)
3105 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003106 return NULL;
3107}
3108
3109 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003110TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003111{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003112 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003113 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003114 else
3115 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003116 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003117
3118 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003119 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3120 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003121 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003122 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003123 }
3124}
3125
3126static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003127 /* name, function, calling, documentation */
3128 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3129 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003130};
3131
3132/*
3133 * Window list object
3134 */
3135
3136static PyTypeObject TabListType;
3137static PySequenceMethods TabListAsSeq;
3138
3139typedef struct
3140{
3141 PyObject_HEAD
3142} TabListObject;
3143
3144 static PyInt
3145TabListLength(PyObject *self UNUSED)
3146{
3147 tabpage_T *tp = first_tabpage;
3148 PyInt n = 0;
3149
3150 while (tp != NULL)
3151 {
3152 ++n;
3153 tp = tp->tp_next;
3154 }
3155
3156 return n;
3157}
3158
3159 static PyObject *
3160TabListItem(PyObject *self UNUSED, PyInt n)
3161{
3162 tabpage_T *tp;
3163
3164 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3165 if (n == 0)
3166 return TabPageNew(tp);
3167
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003168 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003169 return NULL;
3170}
3171
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003172/*
3173 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003174 */
3175
3176typedef struct
3177{
3178 PyObject_HEAD
3179 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003180 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003181} WindowObject;
3182
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003183static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003184
3185 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003186CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003187{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003188 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003189 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003190 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003191 return -1;
3192 }
3193
3194 return 0;
3195}
3196
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003197 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003198WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003199{
3200 /* We need to handle deletion of windows underneath us.
3201 * If we add a "w_python*_ref" field to the win_T structure,
3202 * then we can get at it in win_free() in vim. We then
3203 * need to create only ONE Python object per window - if
3204 * we try to create a second, just INCREF the existing one
3205 * and return it. The (single) Python object referring to
3206 * the window is stored in "w_python*_ref".
3207 * On a win_free() we set the Python object's win_T* field
3208 * to an invalid value. We trap all uses of a window
3209 * object, and reject them if the win_T* field is invalid.
3210 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003211 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003212 * w_python_ref and w_python3_ref fields respectively.
3213 */
3214
3215 WindowObject *self;
3216
3217 if (WIN_PYTHON_REF(win))
3218 {
3219 self = WIN_PYTHON_REF(win);
3220 Py_INCREF(self);
3221 }
3222 else
3223 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003224 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003225 if (self == NULL)
3226 return NULL;
3227 self->win = win;
3228 WIN_PYTHON_REF(win) = self;
3229 }
3230
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003231 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3232
Bram Moolenaar971db462013-05-12 18:44:48 +02003233 return (PyObject *)(self);
3234}
3235
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003236 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003237WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003238{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003239 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003240 if (self->win && self->win != INVALID_WINDOW_VALUE)
3241 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003242 Py_XDECREF(((PyObject *)(self->tabObject)));
3243 PyObject_GC_Del((void *)(self));
3244}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003245
Bram Moolenaar774267b2013-05-21 20:51:59 +02003246 static int
3247WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3248{
3249 Py_VISIT(((PyObject *)(self->tabObject)));
3250 return 0;
3251}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003252
Bram Moolenaar774267b2013-05-21 20:51:59 +02003253 static int
3254WindowClear(WindowObject *self)
3255{
3256 Py_CLEAR(self->tabObject);
3257 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003258}
3259
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003260 static win_T *
3261get_firstwin(TabPageObject *tabObject)
3262{
3263 if (tabObject)
3264 {
3265 if (CheckTabPage(tabObject))
3266 return NULL;
3267 /* For current tab window.c does not bother to set or update tp_firstwin
3268 */
3269 else if (tabObject->tab == curtab)
3270 return firstwin;
3271 else
3272 return tabObject->tab->tp_firstwin;
3273 }
3274 else
3275 return firstwin;
3276}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003277static char *WindowAttrs[] = {
3278 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3279 "tabpage", "valid",
3280 NULL
3281};
3282
3283 static PyObject *
3284WindowDir(PyObject *self)
3285{
3286 return ObjectDir(self, WindowAttrs);
3287}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003288
Bram Moolenaar971db462013-05-12 18:44:48 +02003289 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003290WindowAttrValid(WindowObject *self, char *name)
3291{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003292 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003293
3294 if (strcmp(name, "valid") != 0)
3295 return NULL;
3296
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3298 Py_INCREF(ret);
3299 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003300}
3301
3302 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003303WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003304{
3305 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003306 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003307 else if (strcmp(name, "cursor") == 0)
3308 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003309 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003310
3311 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3312 }
3313 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003314 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003315#ifdef FEAT_WINDOWS
3316 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003317 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003318#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003319#ifdef FEAT_VERTSPLIT
3320 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003321 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003322 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003323 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003324#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003325 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003326 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003327 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003328 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3329 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003330 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003331 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003332 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003333 return NULL;
3334 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003335 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003336 }
3337 else if (strcmp(name, "tabpage") == 0)
3338 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003339 Py_INCREF(self->tabObject);
3340 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003341 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003342 else if (strcmp(name, "__members__") == 0)
3343 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003344 else
3345 return NULL;
3346}
3347
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003348 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003349WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003350{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003351 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003352 return -1;
3353
3354 if (strcmp(name, "buffer") == 0)
3355 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003356 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003357 return -1;
3358 }
3359 else if (strcmp(name, "cursor") == 0)
3360 {
3361 long lnum;
3362 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003363
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003364 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003365 return -1;
3366
Bram Moolenaard6e39182013-05-21 18:30:34 +02003367 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003368 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003369 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003370 return -1;
3371 }
3372
3373 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003374 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003375 return -1;
3376
Bram Moolenaard6e39182013-05-21 18:30:34 +02003377 self->win->w_cursor.lnum = lnum;
3378 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003379#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003380 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003381#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003382 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003383 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003384
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003385 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003386 return 0;
3387 }
3388 else if (strcmp(name, "height") == 0)
3389 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003390 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003391 win_T *savewin;
3392
Bram Moolenaardee2e312013-06-23 16:35:47 +02003393 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003394 return -1;
3395
3396#ifdef FEAT_GUI
3397 need_mouse_correct = TRUE;
3398#endif
3399 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003400 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003401
3402 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003403 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003404 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003405 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003406 return -1;
3407
3408 return 0;
3409 }
3410#ifdef FEAT_VERTSPLIT
3411 else if (strcmp(name, "width") == 0)
3412 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003413 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003414 win_T *savewin;
3415
Bram Moolenaardee2e312013-06-23 16:35:47 +02003416 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003417 return -1;
3418
3419#ifdef FEAT_GUI
3420 need_mouse_correct = TRUE;
3421#endif
3422 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003423 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003424
3425 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003426 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003427 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003428 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003429 return -1;
3430
3431 return 0;
3432 }
3433#endif
3434 else
3435 {
3436 PyErr_SetString(PyExc_AttributeError, name);
3437 return -1;
3438 }
3439}
3440
3441 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003442WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003443{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003444 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003445 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003446 else
3447 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003448 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003449
Bram Moolenaar6d216452013-05-12 19:00:41 +02003450 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003451 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003452 (self));
3453 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003454 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003455 }
3456}
3457
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003458static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003459 /* name, function, calling, documentation */
3460 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3461 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003462};
3463
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003464/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003465 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003466 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003467
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003468static PyTypeObject WinListType;
3469static PySequenceMethods WinListAsSeq;
3470
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003471typedef struct
3472{
3473 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003474 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003475} WinListObject;
3476
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003477 static PyObject *
3478WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003479{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003480 WinListObject *self;
3481
3482 self = PyObject_NEW(WinListObject, &WinListType);
3483 self->tabObject = tabObject;
3484 Py_INCREF(tabObject);
3485
3486 return (PyObject *)(self);
3487}
3488
3489 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003490WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003491{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003492 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003493
3494 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003495 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003496 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003497 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003498
3499 DESTRUCTOR_FINISH(self);
3500}
3501
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003502 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003503WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003504{
3505 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003506 PyInt n = 0;
3507
Bram Moolenaard6e39182013-05-21 18:30:34 +02003508 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003509 return -1;
3510
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003511 while (w != NULL)
3512 {
3513 ++n;
3514 w = W_NEXT(w);
3515 }
3516
3517 return n;
3518}
3519
3520 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003521WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003522{
3523 win_T *w;
3524
Bram Moolenaard6e39182013-05-21 18:30:34 +02003525 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003526 return NULL;
3527
3528 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003529 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003530 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003531
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003532 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003533 return NULL;
3534}
3535
3536/* Convert a Python string into a Vim line.
3537 *
3538 * The result is in allocated memory. All internal nulls are replaced by
3539 * newline characters. It is an error for the string to contain newline
3540 * characters.
3541 *
3542 * On errors, the Python exception data is set, and NULL is returned.
3543 */
3544 static char *
3545StringToLine(PyObject *obj)
3546{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003547 char *str;
3548 char *save;
3549 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003550 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003551 PyInt i;
3552 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003553
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003554 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003555 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003556 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3557 || str == NULL)
3558 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003559 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003560 else if (PyUnicode_Check(obj))
3561 {
3562 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3563 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003564
Bram Moolenaardaa27022013-06-24 22:33:30 +02003565 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003566 || str == NULL)
3567 {
3568 Py_DECREF(bytes);
3569 return NULL;
3570 }
3571 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003572 else
3573 {
3574#if PY_MAJOR_VERSION < 3
3575 PyErr_FORMAT(PyExc_TypeError,
3576 N_("expected str() or unicode() instance, but got %s"),
3577 Py_TYPE_NAME(obj));
3578#else
3579 PyErr_FORMAT(PyExc_TypeError,
3580 N_("expected bytes() or str() instance, but got %s"),
3581 Py_TYPE_NAME(obj));
3582#endif
3583 return NULL;
3584 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003585
3586 /*
3587 * Error checking: String must not contain newlines, as we
3588 * are replacing a single line, and we must replace it with
3589 * a single line.
3590 * A trailing newline is removed, so that append(f.readlines()) works.
3591 */
3592 p = memchr(str, '\n', len);
3593 if (p != NULL)
3594 {
3595 if (p == str + len - 1)
3596 --len;
3597 else
3598 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003599 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003600 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003601 return NULL;
3602 }
3603 }
3604
3605 /* Create a copy of the string, with internal nulls replaced by
3606 * newline characters, as is the vim convention.
3607 */
3608 save = (char *)alloc((unsigned)(len+1));
3609 if (save == NULL)
3610 {
3611 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003612 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003613 return NULL;
3614 }
3615
3616 for (i = 0; i < len; ++i)
3617 {
3618 if (str[i] == '\0')
3619 save[i] = '\n';
3620 else
3621 save[i] = str[i];
3622 }
3623
3624 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003625 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003626
3627 return save;
3628}
3629
3630/* Get a line from the specified buffer. The line number is
3631 * in Vim format (1-based). The line is returned as a Python
3632 * string object.
3633 */
3634 static PyObject *
3635GetBufferLine(buf_T *buf, PyInt n)
3636{
3637 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3638}
3639
3640
3641/* Get a list of lines from the specified buffer. The line numbers
3642 * are in Vim format (1-based). The range is from lo up to, but not
3643 * including, hi. The list is returned as a Python list of string objects.
3644 */
3645 static PyObject *
3646GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3647{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003648 PyInt i;
3649 PyInt n = hi - lo;
3650 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003651
3652 if (list == NULL)
3653 return NULL;
3654
3655 for (i = 0; i < n; ++i)
3656 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003657 PyObject *string = LineToString(
3658 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003659
3660 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003661 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003662 {
3663 Py_DECREF(list);
3664 return NULL;
3665 }
3666
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003667 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003668 }
3669
3670 /* The ownership of the Python list is passed to the caller (ie,
3671 * the caller should Py_DECREF() the object when it is finished
3672 * with it).
3673 */
3674
3675 return list;
3676}
3677
3678/*
3679 * Check if deleting lines made the cursor position invalid.
3680 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3681 * deleted).
3682 */
3683 static void
3684py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3685{
3686 if (curwin->w_cursor.lnum >= lo)
3687 {
3688 /* Adjust the cursor position if it's in/after the changed
3689 * lines. */
3690 if (curwin->w_cursor.lnum >= hi)
3691 {
3692 curwin->w_cursor.lnum += extra;
3693 check_cursor_col();
3694 }
3695 else if (extra < 0)
3696 {
3697 curwin->w_cursor.lnum = lo;
3698 check_cursor();
3699 }
3700 else
3701 check_cursor_col();
3702 changed_cline_bef_curs();
3703 }
3704 invalidate_botline();
3705}
3706
Bram Moolenaar19e60942011-06-19 00:27:51 +02003707/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003708 * Find a window that contains "buf" and switch to it.
3709 * If there is no such window, use the current window and change "curbuf".
3710 * Caller must initialize save_curbuf to NULL.
3711 * restore_win_for_buf() MUST be called later!
3712 */
3713 static void
3714switch_to_win_for_buf(
3715 buf_T *buf,
3716 win_T **save_curwinp,
3717 tabpage_T **save_curtabp,
3718 buf_T **save_curbufp)
3719{
3720 win_T *wp;
3721 tabpage_T *tp;
3722
3723 if (find_win_for_buf(buf, &wp, &tp) == FAIL
3724 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
3725 switch_buffer(save_curbufp, buf);
3726}
3727
3728 static void
3729restore_win_for_buf(
3730 win_T *save_curwin,
3731 tabpage_T *save_curtab,
3732 buf_T *save_curbuf)
3733{
3734 if (save_curbuf == NULL)
3735 restore_win(save_curwin, save_curtab, TRUE);
3736 else
3737 restore_buffer(save_curbuf);
3738}
3739
3740/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02003741 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742 * in Vim format (1-based). The replacement line is given as
3743 * a Python string object. The object is checked for validity
3744 * and correct format. Errors are returned as a value of FAIL.
3745 * The return value is OK on success.
3746 * If OK is returned and len_change is not NULL, *len_change
3747 * is set to the change in the buffer length.
3748 */
3749 static int
3750SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3751{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003752 buf_T *save_curbuf = NULL;
3753 win_T *save_curwin = NULL;
3754 tabpage_T *save_curtab = NULL;
3755
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003756 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003757 * There are three cases:
3758 * 1. NULL, or None - this is a deletion.
3759 * 2. A string - this is a replacement.
3760 * 3. Anything else - this is an error.
3761 */
3762 if (line == Py_None || line == NULL)
3763 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003764 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003765 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003766
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003767 VimTryStart();
3768
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003769 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003770 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003771 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003772 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003773 else
3774 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003775 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003777 if (save_curbuf == NULL)
3778 /* Only adjust marks if we managed to switch to a window that
3779 * holds the buffer, otherwise line numbers will be invalid. */
3780 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003781 }
3782
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003783 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003784
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003785 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003786 return FAIL;
3787
3788 if (len_change)
3789 *len_change = -1;
3790
3791 return OK;
3792 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003793 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003794 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003795 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003796
3797 if (save == NULL)
3798 return FAIL;
3799
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003800 VimTryStart();
3801
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003802 /* We do not need to free "save" if ml_replace() consumes it. */
3803 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003804 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003805
3806 if (u_savesub((linenr_T)n) == FAIL)
3807 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003808 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003809 vim_free(save);
3810 }
3811 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3812 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003813 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003814 vim_free(save);
3815 }
3816 else
3817 changed_bytes((linenr_T)n, 0);
3818
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003819 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003820
3821 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003822 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003823 check_cursor_col();
3824
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003825 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003826 return FAIL;
3827
3828 if (len_change)
3829 *len_change = 0;
3830
3831 return OK;
3832 }
3833 else
3834 {
3835 PyErr_BadArgument();
3836 return FAIL;
3837 }
3838}
3839
Bram Moolenaar19e60942011-06-19 00:27:51 +02003840/* Replace a range of lines in the specified buffer. The line numbers are in
3841 * Vim format (1-based). The range is from lo up to, but not including, hi.
3842 * The replacement lines are given as a Python list of string objects. The
3843 * list is checked for validity and correct format. Errors are returned as a
3844 * value of FAIL. The return value is OK on success.
3845 * If OK is returned and len_change is not NULL, *len_change
3846 * is set to the change in the buffer length.
3847 */
3848 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003849SetBufferLineList(
3850 buf_T *buf,
3851 PyInt lo,
3852 PyInt hi,
3853 PyObject *list,
3854 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003855{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003856 buf_T *save_curbuf = NULL;
3857 win_T *save_curwin = NULL;
3858 tabpage_T *save_curtab = NULL;
3859
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003860 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003861 * There are three cases:
3862 * 1. NULL, or None - this is a deletion.
3863 * 2. A list - this is a replacement.
3864 * 3. Anything else - this is an error.
3865 */
3866 if (list == Py_None || list == NULL)
3867 {
3868 PyInt i;
3869 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003870
3871 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003872 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003873 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003874
3875 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003876 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003877 else
3878 {
3879 for (i = 0; i < n; ++i)
3880 {
3881 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3882 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003883 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003884 break;
3885 }
3886 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003887 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003888 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003889 if (save_curbuf == NULL)
3890 /* Only adjust marks if we managed to switch to a window that
3891 * holds the buffer, otherwise line numbers will be invalid. */
3892 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003893 }
3894
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003895 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003896
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003897 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003898 return FAIL;
3899
3900 if (len_change)
3901 *len_change = -n;
3902
3903 return OK;
3904 }
3905 else if (PyList_Check(list))
3906 {
3907 PyInt i;
3908 PyInt new_len = PyList_Size(list);
3909 PyInt old_len = hi - lo;
3910 PyInt extra = 0; /* lines added to text, can be negative */
3911 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003912
3913 if (new_len == 0) /* avoid allocating zero bytes */
3914 array = NULL;
3915 else
3916 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003917 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003918 if (array == NULL)
3919 {
3920 PyErr_NoMemory();
3921 return FAIL;
3922 }
3923 }
3924
3925 for (i = 0; i < new_len; ++i)
3926 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003927 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003928
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003929 if (!(line = PyList_GetItem(list, i)) ||
3930 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003931 {
3932 while (i)
3933 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003934 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003935 return FAIL;
3936 }
3937 }
3938
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003939 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003940 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003941
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003942 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003943 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003944
3945 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003946 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003947
3948 /* If the size of the range is reducing (ie, new_len < old_len) we
3949 * need to delete some old_len. We do this at the start, by
3950 * repeatedly deleting line "lo".
3951 */
3952 if (!PyErr_Occurred())
3953 {
3954 for (i = 0; i < old_len - new_len; ++i)
3955 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3956 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003957 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003958 break;
3959 }
3960 extra -= i;
3961 }
3962
3963 /* For as long as possible, replace the existing old_len with the
3964 * new old_len. This is a more efficient operation, as it requires
3965 * less memory allocation and freeing.
3966 */
3967 if (!PyErr_Occurred())
3968 {
3969 for (i = 0; i < old_len && i < new_len; ++i)
3970 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3971 == FAIL)
3972 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003973 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003974 break;
3975 }
3976 }
3977 else
3978 i = 0;
3979
3980 /* Now we may need to insert the remaining new old_len. If we do, we
3981 * must free the strings as we finish with them (we can't pass the
3982 * responsibility to vim in this case).
3983 */
3984 if (!PyErr_Occurred())
3985 {
3986 while (i < new_len)
3987 {
3988 if (ml_append((linenr_T)(lo + i - 1),
3989 (char_u *)array[i], 0, FALSE) == FAIL)
3990 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003991 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003992 break;
3993 }
3994 vim_free(array[i]);
3995 ++i;
3996 ++extra;
3997 }
3998 }
3999
4000 /* Free any left-over old_len, as a result of an error */
4001 while (i < new_len)
4002 {
4003 vim_free(array[i]);
4004 ++i;
4005 }
4006
4007 /* Free the array of old_len. All of its contents have now
4008 * been dealt with (either freed, or the responsibility passed
4009 * to vim.
4010 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004011 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004012
4013 /* Adjust marks. Invalidate any which lie in the
4014 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004015 * Only adjust marks if we managed to switch to a window that holds
4016 * the buffer, otherwise line numbers will be invalid. */
4017 if (save_curbuf == NULL)
4018 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004019 (long)MAXLNUM, (long)extra);
4020 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4021
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004022 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004023 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4024
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004025 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004026 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004027
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004028 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004029 return FAIL;
4030
4031 if (len_change)
4032 *len_change = new_len - old_len;
4033
4034 return OK;
4035 }
4036 else
4037 {
4038 PyErr_BadArgument();
4039 return FAIL;
4040 }
4041}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004043/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044 * The line number is in Vim format (1-based). The lines to be inserted are
4045 * given as a Python list of string objects or as a single string. The lines
4046 * to be added are checked for validity and correct format. Errors are
4047 * returned as a value of FAIL. The return value is OK on success.
4048 * If OK is returned and len_change is not NULL, *len_change
4049 * is set to the change in the buffer length.
4050 */
4051 static int
4052InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4053{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004054 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004055 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004056 tabpage_T *save_curtab = NULL;
4057
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004058 /* First of all, we check the type of the supplied Python object.
4059 * It must be a string or a list, or the call is in error.
4060 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004061 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004063 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004064
4065 if (str == NULL)
4066 return FAIL;
4067
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004069 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004070 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004072 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004073 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004075 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004076 else if (save_curbuf == NULL)
4077 /* Only adjust marks if we managed to switch to a window that
4078 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 appended_lines_mark((linenr_T)n, 1L);
4080
4081 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004082 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 update_screen(VALID);
4084
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004085 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 return FAIL;
4087
4088 if (len_change)
4089 *len_change = 1;
4090
4091 return OK;
4092 }
4093 else if (PyList_Check(lines))
4094 {
4095 PyInt i;
4096 PyInt size = PyList_Size(lines);
4097 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004099 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004100 if (array == NULL)
4101 {
4102 PyErr_NoMemory();
4103 return FAIL;
4104 }
4105
4106 for (i = 0; i < size; ++i)
4107 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004108 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004110 if (!(line = PyList_GetItem(lines, i)) ||
4111 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 {
4113 while (i)
4114 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004115 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 return FAIL;
4117 }
4118 }
4119
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004120 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004121 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004122 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004123
4124 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004125 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126 else
4127 {
4128 for (i = 0; i < size; ++i)
4129 {
4130 if (ml_append((linenr_T)(n + i),
4131 (char_u *)array[i], 0, FALSE) == FAIL)
4132 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004133 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004134
4135 /* Free the rest of the lines */
4136 while (i < size)
4137 vim_free(array[i++]);
4138
4139 break;
4140 }
4141 vim_free(array[i]);
4142 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004143 if (i > 0 && save_curbuf == NULL)
4144 /* Only adjust marks if we managed to switch to a window that
4145 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146 appended_lines_mark((linenr_T)n, (long)i);
4147 }
4148
4149 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004150 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004151 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004152 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004153
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004154 update_screen(VALID);
4155
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004156 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004157 return FAIL;
4158
4159 if (len_change)
4160 *len_change = size;
4161
4162 return OK;
4163 }
4164 else
4165 {
4166 PyErr_BadArgument();
4167 return FAIL;
4168 }
4169}
4170
4171/*
4172 * Common routines for buffers and line ranges
4173 * -------------------------------------------
4174 */
4175
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004176typedef struct
4177{
4178 PyObject_HEAD
4179 buf_T *buf;
4180} BufferObject;
4181
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004182 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004183CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004184{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004185 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004187 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004188 return -1;
4189 }
4190
4191 return 0;
4192}
4193
4194 static PyObject *
4195RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4196{
4197 if (CheckBuffer(self))
4198 return NULL;
4199
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004200 if (end == -1)
4201 end = self->buf->b_ml.ml_line_count;
4202
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004203 if (n < 0)
4204 n += end - start + 1;
4205
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004206 if (n < 0 || n > end - start)
4207 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004208 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004209 return NULL;
4210 }
4211
4212 return GetBufferLine(self->buf, n+start);
4213}
4214
4215 static PyObject *
4216RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4217{
4218 PyInt size;
4219
4220 if (CheckBuffer(self))
4221 return NULL;
4222
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004223 if (end == -1)
4224 end = self->buf->b_ml.ml_line_count;
4225
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004226 size = end - start + 1;
4227
4228 if (lo < 0)
4229 lo = 0;
4230 else if (lo > size)
4231 lo = size;
4232 if (hi < 0)
4233 hi = 0;
4234 if (hi < lo)
4235 hi = lo;
4236 else if (hi > size)
4237 hi = size;
4238
4239 return GetBufferLineList(self->buf, lo+start, hi+start);
4240}
4241
4242 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004243RBAsItem(
4244 BufferObject *self,
4245 PyInt n,
4246 PyObject *valObject,
4247 PyInt start,
4248 PyInt end,
4249 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004250{
4251 PyInt len_change;
4252
4253 if (CheckBuffer(self))
4254 return -1;
4255
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004256 if (end == -1)
4257 end = self->buf->b_ml.ml_line_count;
4258
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004259 if (n < 0)
4260 n += end - start + 1;
4261
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004262 if (n < 0 || n > end - start)
4263 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004264 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004265 return -1;
4266 }
4267
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004268 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269 return -1;
4270
4271 if (new_end)
4272 *new_end = end + len_change;
4273
4274 return 0;
4275}
4276
Bram Moolenaar19e60942011-06-19 00:27:51 +02004277 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004278RBAsSlice(
4279 BufferObject *self,
4280 PyInt lo,
4281 PyInt hi,
4282 PyObject *valObject,
4283 PyInt start,
4284 PyInt end,
4285 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004286{
4287 PyInt size;
4288 PyInt len_change;
4289
4290 /* Self must be a valid buffer */
4291 if (CheckBuffer(self))
4292 return -1;
4293
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004294 if (end == -1)
4295 end = self->buf->b_ml.ml_line_count;
4296
Bram Moolenaar19e60942011-06-19 00:27:51 +02004297 /* Sort out the slice range */
4298 size = end - start + 1;
4299
4300 if (lo < 0)
4301 lo = 0;
4302 else if (lo > size)
4303 lo = size;
4304 if (hi < 0)
4305 hi = 0;
4306 if (hi < lo)
4307 hi = lo;
4308 else if (hi > size)
4309 hi = size;
4310
4311 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004312 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004313 return -1;
4314
4315 if (new_end)
4316 *new_end = end + len_change;
4317
4318 return 0;
4319}
4320
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321
4322 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004323RBAppend(
4324 BufferObject *self,
4325 PyObject *args,
4326 PyInt start,
4327 PyInt end,
4328 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004329{
4330 PyObject *lines;
4331 PyInt len_change;
4332 PyInt max;
4333 PyInt n;
4334
4335 if (CheckBuffer(self))
4336 return NULL;
4337
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004338 if (end == -1)
4339 end = self->buf->b_ml.ml_line_count;
4340
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004341 max = n = end - start + 1;
4342
4343 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4344 return NULL;
4345
4346 if (n < 0 || n > max)
4347 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004348 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004349 return NULL;
4350 }
4351
4352 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4353 return NULL;
4354
4355 if (new_end)
4356 *new_end = end + len_change;
4357
4358 Py_INCREF(Py_None);
4359 return Py_None;
4360}
4361
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004362/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004363 */
4364
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004365static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004366static PySequenceMethods RangeAsSeq;
4367static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004368
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004369typedef struct
4370{
4371 PyObject_HEAD
4372 BufferObject *buf;
4373 PyInt start;
4374 PyInt end;
4375} RangeObject;
4376
4377 static PyObject *
4378RangeNew(buf_T *buf, PyInt start, PyInt end)
4379{
4380 BufferObject *bufr;
4381 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004382 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383 if (self == NULL)
4384 return NULL;
4385
4386 bufr = (BufferObject *)BufferNew(buf);
4387 if (bufr == NULL)
4388 {
4389 Py_DECREF(self);
4390 return NULL;
4391 }
4392 Py_INCREF(bufr);
4393
4394 self->buf = bufr;
4395 self->start = start;
4396 self->end = end;
4397
4398 return (PyObject *)(self);
4399}
4400
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004401 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004402RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004403{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004404 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004405 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004406 PyObject_GC_Del((void *)(self));
4407}
4408
4409 static int
4410RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4411{
4412 Py_VISIT(((PyObject *)(self->buf)));
4413 return 0;
4414}
4415
4416 static int
4417RangeClear(RangeObject *self)
4418{
4419 Py_CLEAR(self->buf);
4420 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004421}
4422
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004423 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004424RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004425{
4426 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004427 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004428 return -1; /* ??? */
4429
Bram Moolenaard6e39182013-05-21 18:30:34 +02004430 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004431}
4432
4433 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004434RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004435{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004436 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004437}
4438
4439 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004440RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004441{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004442 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004443}
4444
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004445static char *RangeAttrs[] = {
4446 "start", "end",
4447 NULL
4448};
4449
4450 static PyObject *
4451RangeDir(PyObject *self)
4452{
4453 return ObjectDir(self, RangeAttrs);
4454}
4455
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004456 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004457RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004458{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004459 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004460}
4461
4462 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004463RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004464{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004465 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004466 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4467 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004468 else
4469 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004470 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004471
4472 if (name == NULL)
4473 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004474
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004475 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004476 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004477 }
4478}
4479
4480static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004481 /* name, function, calling, documentation */
4482 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004483 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4484 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004485};
4486
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004487static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004488static PySequenceMethods BufferAsSeq;
4489static PyMappingMethods BufferAsMapping;
4490
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004491 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004492BufferNew(buf_T *buf)
4493{
4494 /* We need to handle deletion of buffers underneath us.
4495 * If we add a "b_python*_ref" field to the buf_T structure,
4496 * then we can get at it in buf_freeall() in vim. We then
4497 * need to create only ONE Python object per buffer - if
4498 * we try to create a second, just INCREF the existing one
4499 * and return it. The (single) Python object referring to
4500 * the buffer is stored in "b_python*_ref".
4501 * Question: what to do on a buf_freeall(). We'll probably
4502 * have to either delete the Python object (DECREF it to
4503 * zero - a bad idea, as it leaves dangling refs!) or
4504 * set the buf_T * value to an invalid value (-1?), which
4505 * means we need checks in all access functions... Bah.
4506 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004507 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004508 * b_python_ref and b_python3_ref fields respectively.
4509 */
4510
4511 BufferObject *self;
4512
4513 if (BUF_PYTHON_REF(buf) != NULL)
4514 {
4515 self = BUF_PYTHON_REF(buf);
4516 Py_INCREF(self);
4517 }
4518 else
4519 {
4520 self = PyObject_NEW(BufferObject, &BufferType);
4521 if (self == NULL)
4522 return NULL;
4523 self->buf = buf;
4524 BUF_PYTHON_REF(buf) = self;
4525 }
4526
4527 return (PyObject *)(self);
4528}
4529
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004530 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004531BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004532{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004533 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4534 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004535
4536 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004537}
4538
Bram Moolenaar971db462013-05-12 18:44:48 +02004539 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004540BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004541{
4542 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004543 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004544 return -1; /* ??? */
4545
Bram Moolenaard6e39182013-05-21 18:30:34 +02004546 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004547}
4548
4549 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004550BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004551{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004552 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004553}
4554
4555 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004556BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004557{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004558 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004559}
4560
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004561static char *BufferAttrs[] = {
4562 "name", "number", "vars", "options", "valid",
4563 NULL
4564};
4565
4566 static PyObject *
4567BufferDir(PyObject *self)
4568{
4569 return ObjectDir(self, BufferAttrs);
4570}
4571
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004573BufferAttrValid(BufferObject *self, char *name)
4574{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004575 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004576
4577 if (strcmp(name, "valid") != 0)
4578 return NULL;
4579
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004580 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4581 Py_INCREF(ret);
4582 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004583}
4584
4585 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004586BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004587{
4588 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004589 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004590 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004591 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004592 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004593 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004594 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004595 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004596 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4597 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004598 else if (strcmp(name, "__members__") == 0)
4599 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004600 else
4601 return NULL;
4602}
4603
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004604 static int
4605BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4606{
4607 if (CheckBuffer(self))
4608 return -1;
4609
4610 if (strcmp(name, "name") == 0)
4611 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004612 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004613 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004614 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004615 PyObject *todecref;
4616
4617 if (!(val = StringToChars(valObject, &todecref)))
4618 return -1;
4619
4620 VimTryStart();
4621 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4622 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004623 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004624 aucmd_restbuf(&aco);
4625 Py_XDECREF(todecref);
4626 if (VimTryEnd())
4627 return -1;
4628
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004629 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004630 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004631 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004632 return -1;
4633 }
4634 return 0;
4635 }
4636 else
4637 {
4638 PyErr_SetString(PyExc_AttributeError, name);
4639 return -1;
4640 }
4641}
4642
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004643 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004644BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004645{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004646 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004647}
4648
4649 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004650BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004651{
4652 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004653 char_u *pmark;
4654 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004655 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004656 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004657
Bram Moolenaard6e39182013-05-21 18:30:34 +02004658 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004659 return NULL;
4660
Bram Moolenaar389a1792013-06-23 13:00:44 +02004661 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004662 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004663
Bram Moolenaar389a1792013-06-23 13:00:44 +02004664 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004665 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004666 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004667 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004668 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004669 return NULL;
4670 }
4671
4672 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004673
4674 Py_XDECREF(todecref);
4675
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004676 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004677 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004678 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004679 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004680 if (VimTryEnd())
4681 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004682
4683 if (posp == NULL)
4684 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004685 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004686 return NULL;
4687 }
4688
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004689 if (posp->lnum <= 0)
4690 {
4691 /* Or raise an error? */
4692 Py_INCREF(Py_None);
4693 return Py_None;
4694 }
4695
4696 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4697}
4698
4699 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004700BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004701{
4702 PyInt start;
4703 PyInt end;
4704
Bram Moolenaard6e39182013-05-21 18:30:34 +02004705 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706 return NULL;
4707
4708 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4709 return NULL;
4710
Bram Moolenaard6e39182013-05-21 18:30:34 +02004711 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712}
4713
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004714 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004715BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004716{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004717 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004718 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004719 else
4720 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004721 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004722
4723 if (name == NULL)
4724 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004725
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004726 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004727 }
4728}
4729
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004731 /* name, function, calling, documentation */
4732 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004733 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004734 {"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 +02004735 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4736 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004737};
4738
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004739/*
4740 * Buffer list object - Implementation
4741 */
4742
4743static PyTypeObject BufMapType;
4744
4745typedef struct
4746{
4747 PyObject_HEAD
4748} BufMapObject;
4749
4750 static PyInt
4751BufMapLength(PyObject *self UNUSED)
4752{
4753 buf_T *b = firstbuf;
4754 PyInt n = 0;
4755
4756 while (b)
4757 {
4758 ++n;
4759 b = b->b_next;
4760 }
4761
4762 return n;
4763}
4764
4765 static PyObject *
4766BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4767{
4768 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004769 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004770
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004771 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004772 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004773
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004774 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004775
4776 if (b)
4777 return BufferNew(b);
4778 else
4779 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004780 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004781 return NULL;
4782 }
4783}
4784
4785 static void
4786BufMapIterDestruct(PyObject *buffer)
4787{
4788 /* Iteration was stopped before all buffers were processed */
4789 if (buffer)
4790 {
4791 Py_DECREF(buffer);
4792 }
4793}
4794
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004795 static int
4796BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4797{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004798 if (buffer)
4799 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004800 return 0;
4801}
4802
4803 static int
4804BufMapIterClear(PyObject **buffer)
4805{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004806 if (*buffer)
4807 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004808 return 0;
4809}
4810
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004811 static PyObject *
4812BufMapIterNext(PyObject **buffer)
4813{
4814 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004815 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004816
4817 if (!*buffer)
4818 return NULL;
4819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004820 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004821
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004822 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004823 {
4824 *buffer = NULL;
4825 return NULL;
4826 }
4827
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004828 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004829 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004830 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004831 return NULL;
4832 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004833 /* Do not increment reference: we no longer hold it (decref), but whoever
4834 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004835 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004836}
4837
4838 static PyObject *
4839BufMapIter(PyObject *self UNUSED)
4840{
4841 PyObject *buffer;
4842
4843 buffer = BufferNew(firstbuf);
4844 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004845 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4846 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004847}
4848
4849static PyMappingMethods BufMapAsMapping = {
4850 (lenfunc) BufMapLength,
4851 (binaryfunc) BufMapItem,
4852 (objobjargproc) 0,
4853};
4854
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004855/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004856 */
4857
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004858static char *CurrentAttrs[] = {
4859 "buffer", "window", "line", "range", "tabpage",
4860 NULL
4861};
4862
4863 static PyObject *
4864CurrentDir(PyObject *self)
4865{
4866 return ObjectDir(self, CurrentAttrs);
4867}
4868
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004869 static PyObject *
4870CurrentGetattr(PyObject *self UNUSED, char *name)
4871{
4872 if (strcmp(name, "buffer") == 0)
4873 return (PyObject *)BufferNew(curbuf);
4874 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004875 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004876 else if (strcmp(name, "tabpage") == 0)
4877 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004878 else if (strcmp(name, "line") == 0)
4879 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4880 else if (strcmp(name, "range") == 0)
4881 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004882 else if (strcmp(name, "__members__") == 0)
4883 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004884 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004885#if PY_MAJOR_VERSION < 3
4886 return Py_FindMethod(WindowMethods, self, name);
4887#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004888 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004889#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004890}
4891
4892 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004893CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004894{
4895 if (strcmp(name, "line") == 0)
4896 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004897 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4898 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004899 return -1;
4900
4901 return 0;
4902 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004903 else if (strcmp(name, "buffer") == 0)
4904 {
4905 int count;
4906
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004907 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004908 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004909 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004910 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004911 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004912 return -1;
4913 }
4914
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004915 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004916 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004917 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004918
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004919 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004920 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4921 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004922 if (VimTryEnd())
4923 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004924 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004925 return -1;
4926 }
4927
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004928 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004929 }
4930 else if (strcmp(name, "window") == 0)
4931 {
4932 int count;
4933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004934 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004935 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004936 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004937 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004938 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004939 return -1;
4940 }
4941
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004942 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004943 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004944 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004945
4946 if (!count)
4947 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004948 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004949 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004950 return -1;
4951 }
4952
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004953 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004954 win_goto(((WindowObject *)(valObject))->win);
4955 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004956 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004957 if (VimTryEnd())
4958 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004959 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004960 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004961 return -1;
4962 }
4963
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004964 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004965 }
4966 else if (strcmp(name, "tabpage") == 0)
4967 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004968 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004969 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004970 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004971 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004972 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004973 return -1;
4974 }
4975
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004976 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004977 return -1;
4978
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004979 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004980 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4981 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004982 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004983 if (VimTryEnd())
4984 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004985 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004986 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004987 return -1;
4988 }
4989
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004990 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004991 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004992 else
4993 {
4994 PyErr_SetString(PyExc_AttributeError, name);
4995 return -1;
4996 }
4997}
4998
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004999static struct PyMethodDef CurrentMethods[] = {
5000 /* name, function, calling, documentation */
5001 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5002 { NULL, NULL, 0, NULL}
5003};
5004
Bram Moolenaardb913952012-06-29 12:54:53 +02005005 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005006init_range_cmd(exarg_T *eap)
5007{
5008 RangeStart = eap->line1;
5009 RangeEnd = eap->line2;
5010}
5011
5012 static void
5013init_range_eval(typval_T *rettv UNUSED)
5014{
5015 RangeStart = (PyInt) curwin->w_cursor.lnum;
5016 RangeEnd = RangeStart;
5017}
5018
5019 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005020run_cmd(const char *cmd, void *arg UNUSED
5021#ifdef PY_CAN_RECURSE
5022 , PyGILState_STATE *pygilstate UNUSED
5023#endif
5024 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005025{
Bram Moolenaar41009372013-07-01 22:03:04 +02005026 PyObject *run_ret;
5027 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5028 if (run_ret != NULL)
5029 {
5030 Py_DECREF(run_ret);
5031 }
5032 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5033 {
5034 EMSG2(_(e_py_systemexit), "python");
5035 PyErr_Clear();
5036 }
5037 else
5038 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005039}
5040
5041static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5042static int code_hdr_len = 30;
5043
5044 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005045run_do(const char *cmd, void *arg UNUSED
5046#ifdef PY_CAN_RECURSE
5047 , PyGILState_STATE *pygilstate
5048#endif
5049 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005050{
5051 PyInt lnum;
5052 size_t len;
5053 char *code;
5054 int status;
5055 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005056 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005057
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005058 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005059 {
5060 EMSG(_("cannot save undo information"));
5061 return;
5062 }
5063
5064 len = code_hdr_len + STRLEN(cmd);
5065 code = PyMem_New(char, len + 1);
5066 memcpy(code, code_hdr, code_hdr_len);
5067 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005068 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5069 status = -1;
5070 if (run_ret != NULL)
5071 {
5072 status = 0;
5073 Py_DECREF(run_ret);
5074 }
5075 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5076 {
5077 PyMem_Free(code);
5078 EMSG2(_(e_py_systemexit), "python");
5079 PyErr_Clear();
5080 return;
5081 }
5082 else
5083 PyErr_PrintEx(1);
5084
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005085 PyMem_Free(code);
5086
5087 if (status)
5088 {
5089 EMSG(_("failed to run the code"));
5090 return;
5091 }
5092
5093 status = 0;
5094 pymain = PyImport_AddModule("__main__");
5095 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005096#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005097 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005098#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005099
5100 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5101 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005102 PyObject *line;
5103 PyObject *linenr;
5104 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005105
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005106#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005107 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005108#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005109 if (!(line = GetBufferLine(curbuf, lnum)))
5110 goto err;
5111 if (!(linenr = PyInt_FromLong((long) lnum)))
5112 {
5113 Py_DECREF(line);
5114 goto err;
5115 }
5116 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5117 Py_DECREF(line);
5118 Py_DECREF(linenr);
5119 if (!ret)
5120 goto err;
5121
5122 if (ret != Py_None)
5123 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5124 goto err;
5125
5126 Py_XDECREF(ret);
5127 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005128#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005129 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005130#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005131 }
5132 goto out;
5133err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005134#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005135 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005136#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005137 PyErr_PrintEx(0);
5138 PythonIO_Flush();
5139 status = 1;
5140out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005141#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005142 if (!status)
5143 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005144#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005145 Py_DECREF(pyfunc);
5146 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5147 if (status)
5148 return;
5149 check_cursor();
5150 update_curbuf(NOT_VALID);
5151}
5152
5153 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005154run_eval(const char *cmd, typval_T *rettv
5155#ifdef PY_CAN_RECURSE
5156 , PyGILState_STATE *pygilstate UNUSED
5157#endif
5158 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005159{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005160 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005161
Bram Moolenaar41009372013-07-01 22:03:04 +02005162 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005163 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005164 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005165 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005166 {
5167 EMSG2(_(e_py_systemexit), "python");
5168 PyErr_Clear();
5169 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005170 else
5171 {
5172 if (PyErr_Occurred() && !msg_silent)
5173 PyErr_PrintEx(0);
5174 EMSG(_("E858: Eval did not return a valid python object"));
5175 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005176 }
5177 else
5178 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005179 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005180 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005181 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005182 }
5183 PyErr_Clear();
5184}
5185
5186 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005187set_ref_in_py(const int copyID)
5188{
5189 pylinkedlist_T *cur;
5190 dict_T *dd;
5191 list_T *ll;
5192
5193 if (lastdict != NULL)
5194 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5195 {
5196 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5197 if (dd->dv_copyID != copyID)
5198 {
5199 dd->dv_copyID = copyID;
5200 set_ref_in_ht(&dd->dv_hashtab, copyID);
5201 }
5202 }
5203
5204 if (lastlist != NULL)
5205 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5206 {
5207 ll = ((ListObject *) (cur->pll_obj))->list;
5208 if (ll->lv_copyID != copyID)
5209 {
5210 ll->lv_copyID = copyID;
5211 set_ref_in_list(ll, copyID);
5212 }
5213 }
5214}
5215
5216 static int
5217set_string_copy(char_u *str, typval_T *tv)
5218{
5219 tv->vval.v_string = vim_strsave(str);
5220 if (tv->vval.v_string == NULL)
5221 {
5222 PyErr_NoMemory();
5223 return -1;
5224 }
5225 return 0;
5226}
5227
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005228 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005229pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005230{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005231 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 char_u *key;
5233 dictitem_T *di;
5234 PyObject *keyObject;
5235 PyObject *valObject;
5236 Py_ssize_t iter = 0;
5237
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005238 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005239 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005240
5241 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005242 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005243
5244 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5245 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005246 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005247
Bram Moolenaara03e6312013-05-29 22:49:26 +02005248 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005249 {
5250 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005251 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005252 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005253
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005254 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005255 {
5256 dict_unref(dict);
5257 return -1;
5258 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005259
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005260 if (*key == NUL)
5261 {
5262 dict_unref(dict);
5263 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005264 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005265 return -1;
5266 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005267
5268 di = dictitem_alloc(key);
5269
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005270 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005271
5272 if (di == NULL)
5273 {
5274 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005275 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005276 return -1;
5277 }
5278 di->di_tv.v_lock = 0;
5279
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005280 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005281 {
5282 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005283 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005284 return -1;
5285 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005286
5287 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005288 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005289 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005290 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005291 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005292 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005293 return -1;
5294 }
5295 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005296
5297 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005298 return 0;
5299}
5300
5301 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005302pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005303{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005304 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005305 char_u *key;
5306 dictitem_T *di;
5307 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005308 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005309 PyObject *keyObject;
5310 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005311
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005312 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005313 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005314
5315 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005316 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005317
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005318 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005319 {
5320 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005321 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005322 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005323
5324 if (!(iterator = PyObject_GetIter(list)))
5325 {
5326 dict_unref(dict);
5327 Py_DECREF(list);
5328 return -1;
5329 }
5330 Py_DECREF(list);
5331
5332 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005333 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005334 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005335
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005336 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005337 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005338 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005339 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005340 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005341 return -1;
5342 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005343
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005344 if (*key == NUL)
5345 {
5346 Py_DECREF(keyObject);
5347 Py_DECREF(iterator);
5348 Py_XDECREF(todecref);
5349 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005350 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005351 return -1;
5352 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005353
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005354 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005355 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005356 Py_DECREF(keyObject);
5357 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005358 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005359 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005360 return -1;
5361 }
5362
5363 di = dictitem_alloc(key);
5364
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005365 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005366 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005367
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005368 if (di == NULL)
5369 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005370 Py_DECREF(iterator);
5371 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005372 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005373 PyErr_NoMemory();
5374 return -1;
5375 }
5376 di->di_tv.v_lock = 0;
5377
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005378 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005379 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005380 Py_DECREF(iterator);
5381 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005382 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005383 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005384 return -1;
5385 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005386
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005387 Py_DECREF(valObject);
5388
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005389 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005390 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005391 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005392 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005393 dictitem_free(di);
5394 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005395 return -1;
5396 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005397 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005398 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005399 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005400 return 0;
5401}
5402
5403 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005404pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005405{
5406 list_T *l;
5407
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005408 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005409 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005410
5411 tv->v_type = VAR_LIST;
5412 tv->vval.v_list = l;
5413
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005414 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005415 {
5416 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005417 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005418 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005419
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005420 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005421 return 0;
5422}
5423
Bram Moolenaardb913952012-06-29 12:54:53 +02005424typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5425
5426 static int
5427convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005428 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005429{
5430 PyObject *capsule;
5431 char hexBuf[sizeof(void *) * 2 + 3];
5432
5433 sprintf(hexBuf, "%p", obj);
5434
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005435# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005436 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005437# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005438 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005439# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005440 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005441 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005442# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005443 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005444# else
5445 capsule = PyCObject_FromVoidPtr(tv, NULL);
5446# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005447 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5448 {
5449 Py_DECREF(capsule);
5450 tv->v_type = VAR_UNKNOWN;
5451 return -1;
5452 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005453
5454 Py_DECREF(capsule);
5455
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005456 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005457 {
5458 tv->v_type = VAR_UNKNOWN;
5459 return -1;
5460 }
5461 /* As we are not using copy_tv which increments reference count we must
5462 * do it ourself. */
5463 switch(tv->v_type)
5464 {
5465 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5466 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5467 }
5468 }
5469 else
5470 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005471 typval_T *v;
5472
5473# ifdef PY_USE_CAPSULE
5474 v = PyCapsule_GetPointer(capsule, NULL);
5475# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005476 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005477# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005478 copy_tv(v, tv);
5479 }
5480 return 0;
5481}
5482
5483 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005484ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5485{
5486 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005487 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005488
5489 if (!(lookup_dict = PyDict_New()))
5490 return -1;
5491
5492 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5493 {
5494 tv->v_type = VAR_DICT;
5495 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5496 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005497 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005498 }
5499 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005500 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005501 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005502 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005503 else
5504 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005505 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005506 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005507 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005508 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005509 }
5510 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005511 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005512}
5513
5514 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005515ConvertFromPyObject(PyObject *obj, typval_T *tv)
5516{
5517 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005518 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005519
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005520 if (!(lookup_dict = PyDict_New()))
5521 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005522 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005523 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005524 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005525}
5526
5527 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005528_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005529{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005530 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005531 {
5532 tv->v_type = VAR_DICT;
5533 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5534 ++tv->vval.v_dict->dv_refcount;
5535 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005536 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005537 {
5538 tv->v_type = VAR_LIST;
5539 tv->vval.v_list = (((ListObject *)(obj))->list);
5540 ++tv->vval.v_list->lv_refcount;
5541 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005542 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005543 {
5544 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5545 return -1;
5546
5547 tv->v_type = VAR_FUNC;
5548 func_ref(tv->vval.v_string);
5549 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005550 else if (PyBytes_Check(obj))
5551 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005552 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005553
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005554 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005555 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005556 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005557 return -1;
5558
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005559 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005560 return -1;
5561
5562 tv->v_type = VAR_STRING;
5563 }
5564 else if (PyUnicode_Check(obj))
5565 {
5566 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005567 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005568
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005569 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005570 if (bytes == NULL)
5571 return -1;
5572
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005574 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005575 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005576 return -1;
5577
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005578 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005579 {
5580 Py_XDECREF(bytes);
5581 return -1;
5582 }
5583 Py_XDECREF(bytes);
5584
5585 tv->v_type = VAR_STRING;
5586 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005587#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005588 else if (PyInt_Check(obj))
5589 {
5590 tv->v_type = VAR_NUMBER;
5591 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005592 if (PyErr_Occurred())
5593 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005594 }
5595#endif
5596 else if (PyLong_Check(obj))
5597 {
5598 tv->v_type = VAR_NUMBER;
5599 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005600 if (PyErr_Occurred())
5601 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005602 }
5603 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005604 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005605#ifdef FEAT_FLOAT
5606 else if (PyFloat_Check(obj))
5607 {
5608 tv->v_type = VAR_FLOAT;
5609 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5610 }
5611#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005612 else if (PyObject_HasAttrString(obj, "keys"))
5613 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005614 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005615 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005616 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005617 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005618 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005619 else if (PyNumber_Check(obj))
5620 {
5621 PyObject *num;
5622
5623 if (!(num = PyNumber_Long(obj)))
5624 return -1;
5625
5626 tv->v_type = VAR_NUMBER;
5627 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5628
5629 Py_DECREF(num);
5630 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005631 else
5632 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005633 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005634 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005635 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005636 return -1;
5637 }
5638 return 0;
5639}
5640
5641 static PyObject *
5642ConvertToPyObject(typval_T *tv)
5643{
5644 if (tv == NULL)
5645 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005646 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005647 return NULL;
5648 }
5649 switch (tv->v_type)
5650 {
5651 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005652 return PyBytes_FromString(tv->vval.v_string == NULL
5653 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005654 case VAR_NUMBER:
5655 return PyLong_FromLong((long) tv->vval.v_number);
5656#ifdef FEAT_FLOAT
5657 case VAR_FLOAT:
5658 return PyFloat_FromDouble((double) tv->vval.v_float);
5659#endif
5660 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005661 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005662 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005663 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005664 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005665 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005666 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005667 case VAR_UNKNOWN:
5668 Py_INCREF(Py_None);
5669 return Py_None;
5670 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005671 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005672 return NULL;
5673 }
5674}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005675
5676typedef struct
5677{
5678 PyObject_HEAD
5679} CurrentObject;
5680static PyTypeObject CurrentType;
5681
5682 static void
5683init_structs(void)
5684{
5685 vim_memset(&OutputType, 0, sizeof(OutputType));
5686 OutputType.tp_name = "vim.message";
5687 OutputType.tp_basicsize = sizeof(OutputObject);
5688 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5689 OutputType.tp_doc = "vim message object";
5690 OutputType.tp_methods = OutputMethods;
5691#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005692 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5693 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005694 OutputType.tp_alloc = call_PyType_GenericAlloc;
5695 OutputType.tp_new = call_PyType_GenericNew;
5696 OutputType.tp_free = call_PyObject_Free;
5697#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005698 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5699 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005700#endif
5701
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005702 vim_memset(&IterType, 0, sizeof(IterType));
5703 IterType.tp_name = "vim.iter";
5704 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005705 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005706 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005707 IterType.tp_iter = (getiterfunc)IterIter;
5708 IterType.tp_iternext = (iternextfunc)IterNext;
5709 IterType.tp_dealloc = (destructor)IterDestructor;
5710 IterType.tp_traverse = (traverseproc)IterTraverse;
5711 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005712
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005713 vim_memset(&BufferType, 0, sizeof(BufferType));
5714 BufferType.tp_name = "vim.buffer";
5715 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005716 BufferType.tp_dealloc = (destructor)BufferDestructor;
5717 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005718 BufferType.tp_as_sequence = &BufferAsSeq;
5719 BufferType.tp_as_mapping = &BufferAsMapping;
5720 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5721 BufferType.tp_doc = "vim buffer object";
5722 BufferType.tp_methods = BufferMethods;
5723#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005724 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005725 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005726 BufferType.tp_alloc = call_PyType_GenericAlloc;
5727 BufferType.tp_new = call_PyType_GenericNew;
5728 BufferType.tp_free = call_PyObject_Free;
5729#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005730 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005731 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005732#endif
5733
5734 vim_memset(&WindowType, 0, sizeof(WindowType));
5735 WindowType.tp_name = "vim.window";
5736 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005737 WindowType.tp_dealloc = (destructor)WindowDestructor;
5738 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005739 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005740 WindowType.tp_doc = "vim Window object";
5741 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005742 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5743 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005744#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005745 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5746 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005747 WindowType.tp_alloc = call_PyType_GenericAlloc;
5748 WindowType.tp_new = call_PyType_GenericNew;
5749 WindowType.tp_free = call_PyObject_Free;
5750#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005751 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5752 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005753#endif
5754
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005755 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5756 TabPageType.tp_name = "vim.tabpage";
5757 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005758 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5759 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005760 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5761 TabPageType.tp_doc = "vim tab page object";
5762 TabPageType.tp_methods = TabPageMethods;
5763#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005764 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005765 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5766 TabPageType.tp_new = call_PyType_GenericNew;
5767 TabPageType.tp_free = call_PyObject_Free;
5768#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005769 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005770#endif
5771
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005772 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5773 BufMapType.tp_name = "vim.bufferlist";
5774 BufMapType.tp_basicsize = sizeof(BufMapObject);
5775 BufMapType.tp_as_mapping = &BufMapAsMapping;
5776 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005777 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005778 BufferType.tp_doc = "vim buffer list";
5779
5780 vim_memset(&WinListType, 0, sizeof(WinListType));
5781 WinListType.tp_name = "vim.windowlist";
5782 WinListType.tp_basicsize = sizeof(WinListType);
5783 WinListType.tp_as_sequence = &WinListAsSeq;
5784 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5785 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005786 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005787
5788 vim_memset(&TabListType, 0, sizeof(TabListType));
5789 TabListType.tp_name = "vim.tabpagelist";
5790 TabListType.tp_basicsize = sizeof(TabListType);
5791 TabListType.tp_as_sequence = &TabListAsSeq;
5792 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5793 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005794
5795 vim_memset(&RangeType, 0, sizeof(RangeType));
5796 RangeType.tp_name = "vim.range";
5797 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005798 RangeType.tp_dealloc = (destructor)RangeDestructor;
5799 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005800 RangeType.tp_as_sequence = &RangeAsSeq;
5801 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005802 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005803 RangeType.tp_doc = "vim Range object";
5804 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005805 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5806 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005807#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005808 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005809 RangeType.tp_alloc = call_PyType_GenericAlloc;
5810 RangeType.tp_new = call_PyType_GenericNew;
5811 RangeType.tp_free = call_PyObject_Free;
5812#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005813 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005814#endif
5815
5816 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5817 CurrentType.tp_name = "vim.currentdata";
5818 CurrentType.tp_basicsize = sizeof(CurrentObject);
5819 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5820 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005821 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005822#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005823 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5824 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005825#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005826 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5827 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005828#endif
5829
5830 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5831 DictionaryType.tp_name = "vim.dictionary";
5832 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005833 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005834 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005835 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005836 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005837 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5838 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005839 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5840 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5841 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005842#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005843 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5844 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005845#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005846 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5847 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005848#endif
5849
5850 vim_memset(&ListType, 0, sizeof(ListType));
5851 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005852 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005853 ListType.tp_basicsize = sizeof(ListObject);
5854 ListType.tp_as_sequence = &ListAsSeq;
5855 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005856 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005857 ListType.tp_doc = "list pushing modifications to vim structure";
5858 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005859 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005860 ListType.tp_new = (newfunc)ListConstructor;
5861 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005862#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005863 ListType.tp_getattro = (getattrofunc)ListGetattro;
5864 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005865#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005866 ListType.tp_getattr = (getattrfunc)ListGetattr;
5867 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005868#endif
5869
5870 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005871 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005872 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005873 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5874 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005875 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005876 FunctionType.tp_doc = "object that calls vim function";
5877 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005878 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005879 FunctionType.tp_new = (newfunc)FunctionConstructor;
5880 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005881#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005882 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005883#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005884 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005885#endif
5886
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005887 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5888 OptionsType.tp_name = "vim.options";
5889 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005890 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005891 OptionsType.tp_doc = "object for manipulating options";
5892 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005893 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5894 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5895 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005896
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005897 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5898 LoaderType.tp_name = "vim.Loader";
5899 LoaderType.tp_basicsize = sizeof(LoaderObject);
5900 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5901 LoaderType.tp_doc = "vim message object";
5902 LoaderType.tp_methods = LoaderMethods;
5903 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5904
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005905#if PY_MAJOR_VERSION >= 3
5906 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5907 vimmodule.m_name = "vim";
5908 vimmodule.m_doc = "Vim Python interface\n";
5909 vimmodule.m_size = -1;
5910 vimmodule.m_methods = VimMethods;
5911#endif
5912}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005913
5914#define PYTYPE_READY(type) \
5915 if (PyType_Ready(&type)) \
5916 return -1;
5917
5918 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02005919init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005920{
5921 PYTYPE_READY(IterType);
5922 PYTYPE_READY(BufferType);
5923 PYTYPE_READY(RangeType);
5924 PYTYPE_READY(WindowType);
5925 PYTYPE_READY(TabPageType);
5926 PYTYPE_READY(BufMapType);
5927 PYTYPE_READY(WinListType);
5928 PYTYPE_READY(TabListType);
5929 PYTYPE_READY(CurrentType);
5930 PYTYPE_READY(DictionaryType);
5931 PYTYPE_READY(ListType);
5932 PYTYPE_READY(FunctionType);
5933 PYTYPE_READY(OptionsType);
5934 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005935 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005936 return 0;
5937}
5938
5939 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005940init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005941{
5942 PyObject *path;
5943 PyObject *path_hook;
5944 PyObject *path_hooks;
5945
5946 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5947 return -1;
5948
5949 if (!(path_hooks = PySys_GetObject("path_hooks")))
5950 {
5951 PyErr_Clear();
5952 path_hooks = PyList_New(1);
5953 PyList_SET_ITEM(path_hooks, 0, path_hook);
5954 if (PySys_SetObject("path_hooks", path_hooks))
5955 {
5956 Py_DECREF(path_hooks);
5957 return -1;
5958 }
5959 Py_DECREF(path_hooks);
5960 }
5961 else if (PyList_Check(path_hooks))
5962 {
5963 if (PyList_Append(path_hooks, path_hook))
5964 {
5965 Py_DECREF(path_hook);
5966 return -1;
5967 }
5968 Py_DECREF(path_hook);
5969 }
5970 else
5971 {
5972 VimTryStart();
5973 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5974 "You should now do the following:\n"
5975 "- append vim.path_hook to sys.path_hooks\n"
5976 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5977 VimTryEnd(); /* Discard the error */
5978 Py_DECREF(path_hook);
5979 return 0;
5980 }
5981
5982 if (!(path = PySys_GetObject("path")))
5983 {
5984 PyErr_Clear();
5985 path = PyList_New(1);
5986 Py_INCREF(vim_special_path_object);
5987 PyList_SET_ITEM(path, 0, vim_special_path_object);
5988 if (PySys_SetObject("path", path))
5989 {
5990 Py_DECREF(path);
5991 return -1;
5992 }
5993 Py_DECREF(path);
5994 }
5995 else if (PyList_Check(path))
5996 {
5997 if (PyList_Append(path, vim_special_path_object))
5998 return -1;
5999 }
6000 else
6001 {
6002 VimTryStart();
6003 EMSG(_("Failed to set path: sys.path is not a list\n"
6004 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6005 VimTryEnd(); /* Discard the error */
6006 }
6007
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006008 return 0;
6009}
6010
6011static BufMapObject TheBufferMap =
6012{
6013 PyObject_HEAD_INIT(&BufMapType)
6014};
6015
6016static WinListObject TheWindowList =
6017{
6018 PyObject_HEAD_INIT(&WinListType)
6019 NULL
6020};
6021
6022static CurrentObject TheCurrent =
6023{
6024 PyObject_HEAD_INIT(&CurrentType)
6025};
6026
6027static TabListObject TheTabPageList =
6028{
6029 PyObject_HEAD_INIT(&TabListType)
6030};
6031
6032static struct numeric_constant {
6033 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006034 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006035} numeric_constants[] = {
6036 {"VAR_LOCKED", VAR_LOCKED},
6037 {"VAR_FIXED", VAR_FIXED},
6038 {"VAR_SCOPE", VAR_SCOPE},
6039 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6040};
6041
6042static struct object_constant {
6043 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006044 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006045} object_constants[] = {
6046 {"buffers", (PyObject *)(void *)&TheBufferMap},
6047 {"windows", (PyObject *)(void *)&TheWindowList},
6048 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6049 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006050
6051 {"Buffer", (PyObject *)&BufferType},
6052 {"Range", (PyObject *)&RangeType},
6053 {"Window", (PyObject *)&WindowType},
6054 {"TabPage", (PyObject *)&TabPageType},
6055 {"Dictionary", (PyObject *)&DictionaryType},
6056 {"List", (PyObject *)&ListType},
6057 {"Function", (PyObject *)&FunctionType},
6058 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006059 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006060};
6061
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006062#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006063 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006064 return -1;
6065
6066#define ADD_CHECKED_OBJECT(m, name, obj) \
6067 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006068 PyObject *valObject = obj; \
6069 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006070 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006071 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006072 }
6073
6074 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006075populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006076{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006077 int i;
6078 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006079 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006080 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006081
6082 for (i = 0; i < (int)(sizeof(numeric_constants)
6083 / sizeof(struct numeric_constant));
6084 ++i)
6085 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006086 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006087
6088 for (i = 0; i < (int)(sizeof(object_constants)
6089 / sizeof(struct object_constant));
6090 ++i)
6091 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006092 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006093
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006094 valObject = object_constants[i].valObject;
6095 Py_INCREF(valObject);
6096 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006097 }
6098
6099 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6100 return -1;
6101 ADD_OBJECT(m, "error", VimError);
6102
Bram Moolenaara9922d62013-05-30 13:01:18 +02006103 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6104 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006105 ADD_CHECKED_OBJECT(m, "options",
6106 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006107
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006108 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006109 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006110 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006111
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006112 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006113 return -1;
6114 ADD_OBJECT(m, "_getcwd", py_getcwd)
6115
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006116 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006117 return -1;
6118 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006119 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006120 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006121 if (PyObject_SetAttrString(other_module, "chdir", attr))
6122 {
6123 Py_DECREF(attr);
6124 return -1;
6125 }
6126 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006127
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006128 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006129 {
6130 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006131 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006132 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006133 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6134 {
6135 Py_DECREF(attr);
6136 return -1;
6137 }
6138 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006139 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006140 else
6141 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006142
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006143 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6144 return -1;
6145
6146 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6147
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006148 if (!(imp = PyImport_ImportModule("imp")))
6149 return -1;
6150
6151 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6152 {
6153 Py_DECREF(imp);
6154 return -1;
6155 }
6156
6157 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6158 {
6159 Py_DECREF(py_find_module);
6160 Py_DECREF(imp);
6161 return -1;
6162 }
6163
6164 Py_DECREF(imp);
6165
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006166 ADD_OBJECT(m, "_find_module", py_find_module);
6167 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006168
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006169 return 0;
6170}