blob: 17c02a9ec2e31ea8796fd31f72bd96a2f1ba20ea [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 Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaarc476e522013-06-23 13:46:40 +020039#define PyErr_FORMAT(exc, str, tail) PyErr_Format(exc, _(str), tail)
40#define PyErr_VIM_FORMAT(str, tail) PyErr_FORMAT(VimError, str, tail)
41
42#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
43 ? "(NULL)" \
44 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020045
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020046#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020047 N_("empty keys are not allowed"))
48#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
49#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
50#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
51#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
52#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
53#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020054#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020055 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020056#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020057 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020058 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020059
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020060#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
61#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020062#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020063
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020064typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020065typedef void (*runner)(const char *, void *
66#ifdef PY_CAN_RECURSE
67 , PyGILState_STATE *
68#endif
69 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020070
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020071static int ConvertFromPyObject(PyObject *, typval_T *);
72static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020073static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020074static PyObject *WindowNew(win_T *, tabpage_T *);
75static PyObject *BufferNew (buf_T *);
76static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020077
78static PyInt RangeStart;
79static PyInt RangeEnd;
80
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020081static PyObject *globals;
82
Bram Moolenaarf4258302013-06-02 18:20:17 +020083static PyObject *py_chdir;
84static PyObject *py_fchdir;
85static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020086static PyObject *vim_module;
87static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020088
Bram Moolenaar81c40c52013-06-12 14:41:04 +020089static PyObject *py_find_module;
90static PyObject *py_load_module;
91
92static PyObject *VimError;
93
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020094/*
95 * obtain a lock on the Vim data structures
96 */
97 static void
98Python_Lock_Vim(void)
99{
100}
101
102/*
103 * release a lock on the Vim data structures
104 */
105 static void
106Python_Release_Vim(void)
107{
108}
109
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200110/*
111 * The "todecref" argument holds a pointer to PyObject * that must be
112 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
113 * was needed to generate returned value is object.
114 *
115 * Use Py_XDECREF to decrement reference count.
116 */
117 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200118StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200119{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200122 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200123 {
124
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200125 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
126 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200127 return NULL;
128
129 *todecref = NULL;
130 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200131 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200132 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200133 PyObject *bytes;
134
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200135 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200136 return NULL;
137
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200138 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
139 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200140 {
141 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200142 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200143 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200144
145 *todecref = bytes;
146 }
147 else
148 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200149#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200150 PyErr_FORMAT(PyExc_TypeError,
151 N_("expected str() or unicode() instance, but got %s"),
152 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200153#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200154 PyErr_FORMAT(PyExc_TypeError,
155 N_("expected bytes() or str() instance, but got %s"),
156 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200157#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200158 return NULL;
159 }
160
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200161 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200162}
163
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200164#define NUMBER_LONG 1
165#define NUMBER_INT 2
166#define NUMBER_NATURAL 4
167#define NUMBER_UNSIGNED 8
168
169 static int
170NumberToLong(PyObject *obj, long *result, int flags)
171{
172#if PY_MAJOR_VERSION < 3
173 if (PyInt_Check(obj))
174 {
175 *result = PyInt_AsLong(obj);
176 if (PyErr_Occurred())
177 return -1;
178 }
179 else
180#endif
181 if (PyLong_Check(obj))
182 {
183 *result = PyLong_AsLong(obj);
184 if (PyErr_Occurred())
185 return -1;
186 }
187 else if (PyNumber_Check(obj))
188 {
189 PyObject *num;
190
191 if (!(num = PyNumber_Long(obj)))
192 return -1;
193
194 *result = PyLong_AsLong(num);
195
196 Py_DECREF(num);
197
198 if (PyErr_Occurred())
199 return -1;
200 }
201 else
202 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200203#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200204 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200205 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 "coercing to long(), but got %s"),
207 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200208#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200209 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200210 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200211 "but got %s"),
212 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200213#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214 return -1;
215 }
216
217 if (flags & NUMBER_INT)
218 {
219 if (*result > INT_MAX)
220 {
221 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200222 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200223 return -1;
224 }
225 else if (*result < INT_MIN)
226 {
227 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200228 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200229 return -1;
230 }
231 }
232
233 if (flags & NUMBER_NATURAL)
234 {
235 if (*result <= 0)
236 {
237 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200238 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200239 return -1;
240 }
241 }
242 else if (flags & NUMBER_UNSIGNED)
243 {
244 if (*result < 0)
245 {
246 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200247 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200248 return -1;
249 }
250 }
251
252 return 0;
253}
254
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200255 static int
256add_string(PyObject *list, char *s)
257{
258 PyObject *string;
259
260 if (!(string = PyString_FromString(s)))
261 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200262
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200263 if (PyList_Append(list, string))
264 {
265 Py_DECREF(string);
266 return -1;
267 }
268
269 Py_DECREF(string);
270 return 0;
271}
272
273 static PyObject *
274ObjectDir(PyObject *self, char **attributes)
275{
276 PyMethodDef *method;
277 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200278 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281 return NULL;
282
283 if (self)
284 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200285 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200286 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200287 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 return NULL;
289 }
290
291 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200292 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200293 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 return NULL;
296 }
297
298#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200299 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200300 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200301 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200302 return NULL;
303 }
304#endif
305
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200306 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200307}
308
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200309/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200310 */
311
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200312/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200313typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200314
315static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200316
317typedef struct
318{
319 PyObject_HEAD
320 long softspace;
321 long error;
322} OutputObject;
323
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200324static char *OutputAttrs[] = {
325 "softspace",
326 NULL
327};
328
329 static PyObject *
330OutputDir(PyObject *self)
331{
332 return ObjectDir(self, OutputAttrs);
333}
334
Bram Moolenaar77045652012-09-21 13:46:06 +0200335 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200336OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200337{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200338 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200339 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200340 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200341 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200342 return -1;
343 }
344
345 if (strcmp(name, "softspace") == 0)
346 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200347 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200348 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return 0;
350 }
351
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200352 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200353 return -1;
354}
355
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200356/* Buffer IO, we write one whole line at a time. */
357static garray_T io_ga = {0, 0, 1, 80, NULL};
358static writefn old_fn = NULL;
359
360 static void
361PythonIO_Flush(void)
362{
363 if (old_fn != NULL && io_ga.ga_len > 0)
364 {
365 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
366 old_fn((char_u *)io_ga.ga_data);
367 }
368 io_ga.ga_len = 0;
369}
370
371 static void
372writer(writefn fn, char_u *str, PyInt n)
373{
374 char_u *ptr;
375
376 /* Flush when switching output function. */
377 if (fn != old_fn)
378 PythonIO_Flush();
379 old_fn = fn;
380
381 /* Write each NL separated line. Text after the last NL is kept for
382 * writing later. */
383 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
384 {
385 PyInt len = ptr - str;
386
387 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
388 break;
389
390 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
391 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
392 fn((char_u *)io_ga.ga_data);
393 str = ptr + 1;
394 n -= len + 1;
395 io_ga.ga_len = 0;
396 }
397
398 /* Put the remaining text into io_ga for later printing. */
399 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
400 {
401 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
402 io_ga.ga_len += (int)n;
403 }
404}
405
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200406 static int
407write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200408{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200409 Py_ssize_t len = 0;
410 char *str = NULL;
411 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200412
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200413 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
414 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200415
416 Py_BEGIN_ALLOW_THREADS
417 Python_Lock_Vim();
418 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
419 Python_Release_Vim();
420 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200421 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200422
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200423 return 0;
424}
425
426 static PyObject *
427OutputWrite(OutputObject *self, PyObject *string)
428{
429 if (write_output(self, string))
430 return NULL;
431
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200432 Py_INCREF(Py_None);
433 return Py_None;
434}
435
436 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200437OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200438{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200439 PyObject *iterator;
440 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200441
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200442 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200445 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200447 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200448 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200449 Py_DECREF(iterator);
450 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200451 return NULL;
452 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200453 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200454 }
455
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200456 Py_DECREF(iterator);
457
458 /* Iterator may have finished due to an exception */
459 if (PyErr_Occurred())
460 return NULL;
461
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200462 Py_INCREF(Py_None);
463 return Py_None;
464}
465
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100466 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200467OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100468{
469 /* do nothing */
470 Py_INCREF(Py_None);
471 return Py_None;
472}
473
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200474/***************/
475
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200476static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200477 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200478 {"write", (PyCFunction)OutputWrite, METH_O, ""},
479 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200480 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200481 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200482 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200483};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200484
485static OutputObject Output =
486{
487 PyObject_HEAD_INIT(&OutputType)
488 0,
489 0
490};
491
492static OutputObject Error =
493{
494 PyObject_HEAD_INIT(&OutputType)
495 0,
496 1
497};
498
499 static int
500PythonIO_Init_io(void)
501{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200502 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
503 return -1;
504 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
505 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200506
507 if (PyErr_Occurred())
508 {
509 EMSG(_("E264: Python: Error initialising I/O objects"));
510 return -1;
511 }
512
513 return 0;
514}
515
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200516typedef struct
517{
518 PyObject_HEAD
519 PyObject *module;
520} LoaderObject;
521static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200522
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200523 static void
524LoaderDestructor(LoaderObject *self)
525{
526 Py_DECREF(self->module);
527 DESTRUCTOR_FINISH(self);
528}
529
530 static PyObject *
531LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
532{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200533 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200534
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200535 Py_INCREF(ret);
536 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200537}
538
539static struct PyMethodDef LoaderMethods[] = {
540 /* name, function, calling, doc */
541 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
542 { NULL, NULL, 0, NULL}
543};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200544
545/* Check to see whether a Vim error has been reported, or a keyboard
546 * interrupt has been detected.
547 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200548
549 static void
550VimTryStart(void)
551{
552 ++trylevel;
553}
554
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200555 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200556VimTryEnd(void)
557{
558 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200559 /* Without this it stops processing all subsequent VimL commands and
560 * generates strange error messages if I e.g. try calling Test() in a
561 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200562 did_emsg = FALSE;
563 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200564 if (got_int)
565 {
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100566 if (current_exception != NULL)
567 discard_current_exception();
568 else
569 need_rethrow = did_throw = FALSE;
570 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200571 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200572 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200573 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100574 else if (msg_list != NULL && *msg_list != NULL)
575 {
576 int should_free;
577 char_u *msg;
578
579 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
580
581 if (msg == NULL)
582 {
583 PyErr_NoMemory();
584 return -1;
585 }
586
587 PyErr_SetVim((char *) msg);
588
589 free_global_msglist();
590
591 if (should_free)
592 vim_free(msg);
593
594 return -1;
595 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200596 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200597 return (PyErr_Occurred() ? -1 : 0);
598 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200599 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200600 {
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100601 if (current_exception != NULL)
602 discard_current_exception();
603 else
604 need_rethrow = did_throw = FALSE;
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200605 return -1;
606 }
607 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200608 else
609 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200610 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200611 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200612 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200613 }
614}
615
616 static int
617VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200618{
619 if (got_int)
620 {
621 PyErr_SetNone(PyExc_KeyboardInterrupt);
622 return 1;
623 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200624 return 0;
625}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626
627/* Vim module - Implementation
628 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200629
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200630 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200631VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200633 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200634 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200635 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200636
Bram Moolenaar389a1792013-06-23 13:00:44 +0200637 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200638 return NULL;
639
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200640 Py_BEGIN_ALLOW_THREADS
641 Python_Lock_Vim();
642
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200643 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200644 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200645 update_screen(VALID);
646
647 Python_Release_Vim();
648 Py_END_ALLOW_THREADS
649
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200650 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200651 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200652 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200653 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200655 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200656 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200657 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200658}
659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200660/*
661 * Function to translate a typval_T into a PyObject; this will recursively
662 * translate lists/dictionaries into their Python equivalents.
663 *
664 * The depth parameter is to avoid infinite recursion, set it to 1 when
665 * you call VimToPython.
666 */
667 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200668VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200669{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200670 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200671 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200672 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673
674 /* Avoid infinite recursion */
675 if (depth > 100)
676 {
677 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200678 ret = Py_None;
679 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200680 }
681
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200682 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683 * then and we can use it again. */
684 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
685 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
686 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200687 sprintf(ptrBuf, "%p",
688 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
689 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200690
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200691 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200693 Py_INCREF(ret);
694 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695 }
696 }
697
698 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200699 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200700 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701 else if (our_tv->v_type == VAR_NUMBER)
702 {
703 char buf[NUMBUFLEN];
704
705 /* For backwards compatibility numbers are stored as strings. */
706 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200707 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200708 }
709# ifdef FEAT_FLOAT
710 else if (our_tv->v_type == VAR_FLOAT)
711 {
712 char buf[NUMBUFLEN];
713
714 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200715 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200716 }
717# endif
718 else if (our_tv->v_type == VAR_LIST)
719 {
720 list_T *list = our_tv->vval.v_list;
721 listitem_T *curr;
722
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200723 if (list == NULL)
724 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200725
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200726 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200727 return NULL;
728
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200729 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200731 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200732 return NULL;
733 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200735 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
736 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200737 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
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;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200741 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200742 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200743 {
744 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200745 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200746 return NULL;
747 }
748 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749 }
750 }
751 else if (our_tv->v_type == VAR_DICT)
752 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200753
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200754 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
755 long_u todo = ht->ht_used;
756 hashitem_T *hi;
757 dictitem_T *di;
758 if (our_tv->vval.v_dict == NULL)
759 return NULL;
760
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200761 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200762 return NULL;
763
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200764 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200766 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200767 return NULL;
768 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200770 for (hi = ht->ht_array; todo > 0; ++hi)
771 {
772 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200774 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200775
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200777 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200778 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200779 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200781 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200783 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 Py_DECREF(newObj);
786 return NULL;
787 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200788 }
789 }
790 }
791 else
792 {
793 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200794 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 }
796
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200797 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200799
800 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200801VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200802{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200803 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200804 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200805 PyObject *string;
806 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200807 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200808 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200809
Bram Moolenaar389a1792013-06-23 13:00:44 +0200810 if (!PyArg_ParseTuple(args, "O", &string))
811 return NULL;
812
813 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200814 return NULL;
815
816 Py_BEGIN_ALLOW_THREADS
817 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200818 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200819 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200820 Python_Release_Vim();
821 Py_END_ALLOW_THREADS
822
Bram Moolenaar389a1792013-06-23 13:00:44 +0200823 Py_XDECREF(todecref);
824
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200825 if (VimTryEnd())
826 return NULL;
827
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200828 if (our_tv == NULL)
829 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200830 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200831 return NULL;
832 }
833
834 /* Convert the Vim type into a Python type. Create a dictionary that's
835 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200836 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200837 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200838 else
839 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200840 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200841 Py_DECREF(lookup_dict);
842 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200843
844
845 Py_BEGIN_ALLOW_THREADS
846 Python_Lock_Vim();
847 free_tv(our_tv);
848 Python_Release_Vim();
849 Py_END_ALLOW_THREADS
850
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200851 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200852}
853
Bram Moolenaardb913952012-06-29 12:54:53 +0200854static PyObject *ConvertToPyObject(typval_T *);
855
856 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200857VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200858{
Bram Moolenaardb913952012-06-29 12:54:53 +0200859 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200860 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200861 char_u *expr;
862 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200863
Bram Moolenaar389a1792013-06-23 13:00:44 +0200864 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200865 return NULL;
866
867 Py_BEGIN_ALLOW_THREADS
868 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200869 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200870 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200871 Python_Release_Vim();
872 Py_END_ALLOW_THREADS
873
Bram Moolenaar389a1792013-06-23 13:00:44 +0200874 Py_XDECREF(todecref);
875
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200876 if (VimTryEnd())
877 return NULL;
878
Bram Moolenaardb913952012-06-29 12:54:53 +0200879 if (our_tv == NULL)
880 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200881 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200882 return NULL;
883 }
884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200885 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200886 Py_BEGIN_ALLOW_THREADS
887 Python_Lock_Vim();
888 free_tv(our_tv);
889 Python_Release_Vim();
890 Py_END_ALLOW_THREADS
891
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200892 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200893}
894
895 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200896VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200897{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 char_u *str;
899 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200900 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200901
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200903 return NULL;
904
Bram Moolenaara54bf402012-12-05 16:30:07 +0100905#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200906 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100907#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200908 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100909#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200910
911 Py_XDECREF(todecref);
912
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200913 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200914}
915
Bram Moolenaarf4258302013-06-02 18:20:17 +0200916 static PyObject *
917_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
918{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200919 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200920 PyObject *newwd;
921 PyObject *todecref;
922 char_u *new_dir;
923
Bram Moolenaard4209d22013-06-05 20:34:15 +0200924 if (_chdir == NULL)
925 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200926 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200927 return NULL;
928
929 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
930 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200931 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200932 return NULL;
933 }
934
935 if (!(new_dir = StringToChars(newwd, &todecref)))
936 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200937 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200938 Py_DECREF(newwd);
939 return NULL;
940 }
941
942 VimTryStart();
943
944 if (vim_chdir(new_dir))
945 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200946 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200947 Py_DECREF(newwd);
948 Py_XDECREF(todecref);
949
950 if (VimTryEnd())
951 return NULL;
952
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200953 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200954 return NULL;
955 }
956
957 Py_DECREF(newwd);
958 Py_XDECREF(todecref);
959
960 post_chdir(FALSE);
961
962 if (VimTryEnd())
963 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200965 return NULL;
966 }
967
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200969}
970
971 static PyObject *
972VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
973{
974 return _VimChdir(py_chdir, args, kwargs);
975}
976
977 static PyObject *
978VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
979{
980 return _VimChdir(py_fchdir, args, kwargs);
981}
982
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200983typedef struct {
984 PyObject *callable;
985 PyObject *result;
986} map_rtp_data;
987
988 static void
989map_rtp_callback(char_u *path, void *_data)
990{
991 void **data = (void **) _data;
992 PyObject *pathObject;
993 map_rtp_data *mr_data = *((map_rtp_data **) data);
994
Bram Moolenaar41009372013-07-01 22:03:04 +0200995 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200996 {
997 *data = NULL;
998 return;
999 }
1000
1001 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1002 pathObject, NULL);
1003
1004 Py_DECREF(pathObject);
1005
1006 if (!mr_data->result || mr_data->result != Py_None)
1007 *data = NULL;
1008 else
1009 {
1010 Py_DECREF(mr_data->result);
1011 mr_data->result = NULL;
1012 }
1013}
1014
1015 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001016VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001017{
1018 map_rtp_data data;
1019
Bram Moolenaar389a1792013-06-23 13:00:44 +02001020 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001021 data.result = NULL;
1022
1023 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1024
1025 if (data.result == NULL)
1026 {
1027 if (PyErr_Occurred())
1028 return NULL;
1029 else
1030 {
1031 Py_INCREF(Py_None);
1032 return Py_None;
1033 }
1034 }
1035 return data.result;
1036}
1037
1038/*
1039 * _vim_runtimepath_ special path implementation.
1040 */
1041
1042 static void
1043map_finder_callback(char_u *path, void *_data)
1044{
1045 void **data = (void **) _data;
1046 PyObject *list = *((PyObject **) data);
1047 PyObject *pathObject1, *pathObject2;
1048 char *pathbuf;
1049 size_t pathlen;
1050
1051 pathlen = STRLEN(path);
1052
1053#if PY_MAJOR_VERSION < 3
1054# define PY_MAIN_DIR_STRING "python2"
1055#else
1056# define PY_MAIN_DIR_STRING "python3"
1057#endif
1058#define PY_ALTERNATE_DIR_STRING "pythonx"
1059
1060#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1061 if (!(pathbuf = PyMem_New(char,
1062 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1063 {
1064 PyErr_NoMemory();
1065 *data = NULL;
1066 return;
1067 }
1068
1069 mch_memmove(pathbuf, path, pathlen + 1);
1070 add_pathsep((char_u *) pathbuf);
1071
1072 pathlen = STRLEN(pathbuf);
1073 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1074 PYTHONX_STRING_LENGTH + 1);
1075
1076 if (!(pathObject1 = PyString_FromString(pathbuf)))
1077 {
1078 *data = NULL;
1079 PyMem_Free(pathbuf);
1080 return;
1081 }
1082
1083 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1084 PYTHONX_STRING_LENGTH + 1);
1085
1086 if (!(pathObject2 = PyString_FromString(pathbuf)))
1087 {
1088 Py_DECREF(pathObject1);
1089 PyMem_Free(pathbuf);
1090 *data = NULL;
1091 return;
1092 }
1093
1094 PyMem_Free(pathbuf);
1095
1096 if (PyList_Append(list, pathObject1)
1097 || PyList_Append(list, pathObject2))
1098 *data = NULL;
1099
1100 Py_DECREF(pathObject1);
1101 Py_DECREF(pathObject2);
1102}
1103
1104 static PyObject *
1105Vim_GetPaths(PyObject *self UNUSED)
1106{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001107 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001108
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001109 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001110 return NULL;
1111
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001112 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001113
1114 if (PyErr_Occurred())
1115 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001116 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117 return NULL;
1118 }
1119
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001120 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001121}
1122
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001123 static PyObject *
1124call_load_module(char *name, int len, PyObject *find_module_result)
1125{
1126 PyObject *fd, *pathname, *description;
1127
Bram Moolenaarc476e522013-06-23 13:46:40 +02001128 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001129 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001130 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001131 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001132 Py_TYPE_NAME(find_module_result));
1133 return NULL;
1134 }
1135 if (PyTuple_GET_SIZE(find_module_result) != 3)
1136 {
1137 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001138 N_("expected 3-tuple as imp.find_module() result, but got "
1139 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001140 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001141 return NULL;
1142 }
1143
1144 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1145 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1146 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1147 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001148 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001149 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001150 return NULL;
1151 }
1152
1153 return PyObject_CallFunction(py_load_module,
1154 "s#OOO", name, len, fd, pathname, description);
1155}
1156
1157 static PyObject *
1158find_module(char *fullname, char *tail, PyObject *new_path)
1159{
1160 PyObject *find_module_result;
1161 PyObject *module;
1162 char *dot;
1163
Bram Moolenaar41009372013-07-01 22:03:04 +02001164 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001165 {
1166 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001167 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001168 * first component
1169 */
1170 PyObject *newest_path;
1171 int partlen = (int) (dot - 1 - tail);
1172
1173 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1174 "s#O", tail, partlen, new_path)))
1175 return NULL;
1176
1177 if (!(module = call_load_module(
1178 fullname,
1179 ((int) (tail - fullname)) + partlen,
1180 find_module_result)))
1181 {
1182 Py_DECREF(find_module_result);
1183 return NULL;
1184 }
1185
1186 Py_DECREF(find_module_result);
1187
1188 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1189 {
1190 Py_DECREF(module);
1191 return NULL;
1192 }
1193
1194 Py_DECREF(module);
1195
1196 module = find_module(fullname, dot + 1, newest_path);
1197
1198 Py_DECREF(newest_path);
1199
1200 return module;
1201 }
1202 else
1203 {
1204 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1205 "sO", tail, new_path)))
1206 return NULL;
1207
1208 if (!(module = call_load_module(
1209 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001210 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001211 find_module_result)))
1212 {
1213 Py_DECREF(find_module_result);
1214 return NULL;
1215 }
1216
1217 Py_DECREF(find_module_result);
1218
1219 return module;
1220 }
1221}
1222
1223 static PyObject *
1224FinderFindModule(PyObject *self, PyObject *args)
1225{
1226 char *fullname;
1227 PyObject *module;
1228 PyObject *new_path;
1229 LoaderObject *loader;
1230
1231 if (!PyArg_ParseTuple(args, "s", &fullname))
1232 return NULL;
1233
1234 if (!(new_path = Vim_GetPaths(self)))
1235 return NULL;
1236
1237 module = find_module(fullname, fullname, new_path);
1238
1239 Py_DECREF(new_path);
1240
1241 if (!module)
1242 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001243 if (PyErr_Occurred())
1244 {
1245 if (PyErr_ExceptionMatches(PyExc_ImportError))
1246 PyErr_Clear();
1247 else
1248 return NULL;
1249 }
1250
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001251 Py_INCREF(Py_None);
1252 return Py_None;
1253 }
1254
1255 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1256 {
1257 Py_DECREF(module);
1258 return NULL;
1259 }
1260
1261 loader->module = module;
1262
1263 return (PyObject *) loader;
1264}
1265
1266 static PyObject *
1267VimPathHook(PyObject *self UNUSED, PyObject *args)
1268{
1269 char *path;
1270
1271 if (PyArg_ParseTuple(args, "s", &path)
1272 && STRCMP(path, vim_special_path) == 0)
1273 {
1274 Py_INCREF(vim_module);
1275 return vim_module;
1276 }
1277
1278 PyErr_Clear();
1279 PyErr_SetNone(PyExc_ImportError);
1280 return NULL;
1281}
1282
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001283/*
1284 * Vim module - Definitions
1285 */
1286
1287static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001288 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001289 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001290 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001291 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1292 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001293 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1294 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001295 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001296 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001297 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1298 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1299 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001300};
1301
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001302/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001303 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001304 */
1305
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001306static PyTypeObject IterType;
1307
1308typedef PyObject *(*nextfun)(void **);
1309typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001310typedef int (*traversefun)(void *, visitproc, void *);
1311typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001312
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001313/* Main purpose of this object is removing the need for do python
1314 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1315 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001316
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001317typedef struct
1318{
1319 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001320 void *cur;
1321 nextfun next;
1322 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001323 traversefun traverse;
1324 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001325} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001326
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001327 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001328IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1329 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001330{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001331 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001332
Bram Moolenaar774267b2013-05-21 20:51:59 +02001333 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001334 self->cur = start;
1335 self->next = next;
1336 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001337 self->traverse = traverse;
1338 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001339
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001340 return (PyObject *)(self);
1341}
1342
1343 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001344IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001345{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001346 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001347 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001348 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001349}
1350
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001351 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001352IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001353{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001354 if (self->traverse != NULL)
1355 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001356 else
1357 return 0;
1358}
1359
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001360/* Mac OSX defines clear() somewhere. */
1361#ifdef clear
1362# undef clear
1363#endif
1364
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001365 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001366IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001367{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001368 if (self->clear != NULL)
1369 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001370 else
1371 return 0;
1372}
1373
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001374 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001375IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001376{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001377 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001378}
1379
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001380 static PyObject *
1381IterIter(PyObject *self)
1382{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001383 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001384 return self;
1385}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001386
Bram Moolenaardb913952012-06-29 12:54:53 +02001387typedef struct pylinkedlist_S {
1388 struct pylinkedlist_S *pll_next;
1389 struct pylinkedlist_S *pll_prev;
1390 PyObject *pll_obj;
1391} pylinkedlist_T;
1392
1393static pylinkedlist_T *lastdict = NULL;
1394static pylinkedlist_T *lastlist = NULL;
1395
1396 static void
1397pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1398{
1399 if (ref->pll_prev == NULL)
1400 {
1401 if (ref->pll_next == NULL)
1402 {
1403 *last = NULL;
1404 return;
1405 }
1406 }
1407 else
1408 ref->pll_prev->pll_next = ref->pll_next;
1409
1410 if (ref->pll_next == NULL)
1411 *last = ref->pll_prev;
1412 else
1413 ref->pll_next->pll_prev = ref->pll_prev;
1414}
1415
1416 static void
1417pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1418{
1419 if (*last == NULL)
1420 ref->pll_prev = NULL;
1421 else
1422 {
1423 (*last)->pll_next = ref;
1424 ref->pll_prev = *last;
1425 }
1426 ref->pll_next = NULL;
1427 ref->pll_obj = self;
1428 *last = ref;
1429}
1430
1431static PyTypeObject DictionaryType;
1432
1433typedef struct
1434{
1435 PyObject_HEAD
1436 dict_T *dict;
1437 pylinkedlist_T ref;
1438} DictionaryObject;
1439
Bram Moolenaara9922d62013-05-30 13:01:18 +02001440static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1441
1442#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1443
Bram Moolenaardb913952012-06-29 12:54:53 +02001444 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001445DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001446{
1447 DictionaryObject *self;
1448
Bram Moolenaara9922d62013-05-30 13:01:18 +02001449 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001450 if (self == NULL)
1451 return NULL;
1452 self->dict = dict;
1453 ++dict->dv_refcount;
1454
1455 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1456
1457 return (PyObject *)(self);
1458}
1459
Bram Moolenaara9922d62013-05-30 13:01:18 +02001460 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001461py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001462{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001463 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001464
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001465 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001466 {
1467 PyErr_NoMemory();
1468 return NULL;
1469 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001470 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001471
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001472 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001473}
1474
1475 static PyObject *
1476DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1477{
1478 DictionaryObject *self;
1479 dict_T *dict;
1480
1481 if (!(dict = py_dict_alloc()))
1482 return NULL;
1483
1484 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1485
1486 --dict->dv_refcount;
1487
1488 if (kwargs || PyTuple_Size(args))
1489 {
1490 PyObject *tmp;
1491 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1492 {
1493 Py_DECREF(self);
1494 return NULL;
1495 }
1496
1497 Py_DECREF(tmp);
1498 }
1499
1500 return (PyObject *)(self);
1501}
1502
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001503 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001504DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001505{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001506 pyll_remove(&self->ref, &lastdict);
1507 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001508
1509 DESTRUCTOR_FINISH(self);
1510}
1511
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001512static char *DictionaryAttrs[] = {
1513 "locked", "scope",
1514 NULL
1515};
1516
1517 static PyObject *
1518DictionaryDir(PyObject *self)
1519{
1520 return ObjectDir(self, DictionaryAttrs);
1521}
1522
Bram Moolenaardb913952012-06-29 12:54:53 +02001523 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001524DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001525{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001526 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001527 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001528 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001529 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001530 return -1;
1531 }
1532
1533 if (strcmp(name, "locked") == 0)
1534 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001535 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001536 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001537 PyErr_SET_STRING(PyExc_TypeError,
1538 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001539 return -1;
1540 }
1541 else
1542 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001543 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001544 if (istrue == -1)
1545 return -1;
1546 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001547 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001548 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001549 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001550 }
1551 return 0;
1552 }
1553 else
1554 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001555 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001556 return -1;
1557 }
1558}
1559
1560 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001561DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001562{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001563 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001564}
1565
Bram Moolenaara9922d62013-05-30 13:01:18 +02001566#define DICT_FLAG_HAS_DEFAULT 0x01
1567#define DICT_FLAG_POP 0x02
1568#define DICT_FLAG_NONE_DEFAULT 0x04
1569#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1570#define DICT_FLAG_RETURN_PAIR 0x10
1571
Bram Moolenaardb913952012-06-29 12:54:53 +02001572 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001573_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001574{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001575 PyObject *keyObject;
1576 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001577 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001578 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001579 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001580 dict_T *dict = self->dict;
1581 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001582 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001583
Bram Moolenaara9922d62013-05-30 13:01:18 +02001584 if (flags & DICT_FLAG_HAS_DEFAULT)
1585 {
1586 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1587 return NULL;
1588 }
1589 else
1590 keyObject = args;
1591
1592 if (flags & DICT_FLAG_RETURN_BOOL)
1593 defObject = Py_False;
1594
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001595 if (!(key = StringToChars(keyObject, &todecref)))
1596 return NULL;
1597
1598 if (*key == NUL)
1599 {
1600 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001601 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001602 return NULL;
1603 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001604
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001606
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001607 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001608
Bram Moolenaara9922d62013-05-30 13:01:18 +02001609 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001610 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001611 if (defObject)
1612 {
1613 Py_INCREF(defObject);
1614 return defObject;
1615 }
1616 else
1617 {
1618 PyErr_SetObject(PyExc_KeyError, keyObject);
1619 return NULL;
1620 }
1621 }
1622 else if (flags & DICT_FLAG_RETURN_BOOL)
1623 {
1624 Py_INCREF(Py_True);
1625 return Py_True;
1626 }
1627
1628 di = dict_lookup(hi);
1629
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001630 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001631 return NULL;
1632
1633 if (flags & DICT_FLAG_POP)
1634 {
1635 if (dict->dv_lock)
1636 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001637 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001638 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001639 return NULL;
1640 }
1641
1642 hash_remove(&dict->dv_hashtab, hi);
1643 dictitem_free(di);
1644 }
1645
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001646 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001647}
1648
1649 static PyObject *
1650DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1651{
1652 return _DictionaryItem(self, keyObject, 0);
1653}
1654
1655 static int
1656DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1657{
1658 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001659 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001660
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001661 if (rObj == NULL)
1662 return -1;
1663
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001665
Bram Moolenaardee2e312013-06-23 16:35:47 +02001666 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001667
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001668 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001669}
1670
1671typedef struct
1672{
1673 hashitem_T *ht_array;
1674 long_u ht_used;
1675 hashtab_T *ht;
1676 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001677 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001678} dictiterinfo_T;
1679
1680 static PyObject *
1681DictionaryIterNext(dictiterinfo_T **dii)
1682{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001683 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001684
1685 if (!(*dii)->todo)
1686 return NULL;
1687
1688 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1689 (*dii)->ht->ht_used != (*dii)->ht_used)
1690 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001691 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001692 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001693 return NULL;
1694 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001695
Bram Moolenaara9922d62013-05-30 13:01:18 +02001696 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1697 ++((*dii)->hi);
1698
1699 --((*dii)->todo);
1700
Bram Moolenaar41009372013-07-01 22:03:04 +02001701 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702 return NULL;
1703
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001704 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705}
1706
1707 static PyObject *
1708DictionaryIter(DictionaryObject *self)
1709{
1710 dictiterinfo_T *dii;
1711 hashtab_T *ht;
1712
1713 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1714 {
1715 PyErr_NoMemory();
1716 return NULL;
1717 }
1718
1719 ht = &self->dict->dv_hashtab;
1720 dii->ht_array = ht->ht_array;
1721 dii->ht_used = ht->ht_used;
1722 dii->ht = ht;
1723 dii->hi = dii->ht_array;
1724 dii->todo = dii->ht_used;
1725
1726 return IterNew(dii,
1727 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1728 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001729}
1730
1731 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001732DictionaryAssItem(
1733 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001734{
1735 char_u *key;
1736 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001737 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001738 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001739 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001740
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001741 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001742 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001743 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001744 return -1;
1745 }
1746
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001747 if (!(key = StringToChars(keyObject, &todecref)))
1748 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001749
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001750 if (*key == NUL)
1751 {
1752 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001753 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001754 return -1;
1755 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001756
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001757 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001758
1759 if (valObject == NULL)
1760 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001761 hashitem_T *hi;
1762
Bram Moolenaardb913952012-06-29 12:54:53 +02001763 if (di == NULL)
1764 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001765 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001766 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001767 return -1;
1768 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001769 hi = hash_find(&dict->dv_hashtab, di->di_key);
1770 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001771 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001772 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001773 return 0;
1774 }
1775
1776 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001777 {
1778 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001779 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001780 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001781
1782 if (di == NULL)
1783 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001784 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001785 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001786 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001787 PyErr_NoMemory();
1788 return -1;
1789 }
1790 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001791 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001792
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001793 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001794 {
1795 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001797 RAISE_KEY_ADD_FAIL(key);
1798 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001799 return -1;
1800 }
1801 }
1802 else
1803 clear_tv(&di->di_tv);
1804
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001805 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001806
1807 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001808 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001809 return 0;
1810}
1811
Bram Moolenaara9922d62013-05-30 13:01:18 +02001812typedef PyObject *(*hi_to_py)(hashitem_T *);
1813
Bram Moolenaardb913952012-06-29 12:54:53 +02001814 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001815DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001816{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001817 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001818 long_u todo = dict->dv_hashtab.ht_used;
1819 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001820 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001821 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001822 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001823
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001824 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001825 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1826 {
1827 if (!HASHITEM_EMPTY(hi))
1828 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 if (!(newObj = hiconvert(hi)))
1830 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001831 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001832 return NULL;
1833 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001834 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001835 --todo;
1836 ++i;
1837 }
1838 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001839 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001840}
1841
Bram Moolenaara9922d62013-05-30 13:01:18 +02001842 static PyObject *
1843dict_key(hashitem_T *hi)
1844{
1845 return PyBytes_FromString((char *)(hi->hi_key));
1846}
1847
1848 static PyObject *
1849DictionaryListKeys(DictionaryObject *self)
1850{
1851 return DictionaryListObjects(self, dict_key);
1852}
1853
1854 static PyObject *
1855dict_val(hashitem_T *hi)
1856{
1857 dictitem_T *di;
1858
1859 di = dict_lookup(hi);
1860 return ConvertToPyObject(&di->di_tv);
1861}
1862
1863 static PyObject *
1864DictionaryListValues(DictionaryObject *self)
1865{
1866 return DictionaryListObjects(self, dict_val);
1867}
1868
1869 static PyObject *
1870dict_item(hashitem_T *hi)
1871{
1872 PyObject *keyObject;
1873 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001874 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001875
1876 if (!(keyObject = dict_key(hi)))
1877 return NULL;
1878
1879 if (!(valObject = dict_val(hi)))
1880 {
1881 Py_DECREF(keyObject);
1882 return NULL;
1883 }
1884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001885 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001886
1887 Py_DECREF(keyObject);
1888 Py_DECREF(valObject);
1889
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001890 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001891}
1892
1893 static PyObject *
1894DictionaryListItems(DictionaryObject *self)
1895{
1896 return DictionaryListObjects(self, dict_item);
1897}
1898
1899 static PyObject *
1900DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1901{
1902 dict_T *dict = self->dict;
1903
1904 if (dict->dv_lock)
1905 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001906 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001907 return NULL;
1908 }
1909
1910 if (kwargs)
1911 {
1912 typval_T tv;
1913
1914 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1915 return NULL;
1916
1917 VimTryStart();
1918 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1919 clear_tv(&tv);
1920 if (VimTryEnd())
1921 return NULL;
1922 }
1923 else
1924 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001925 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001927 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001928 return NULL;
1929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001930 if (PyObject_HasAttrString(obj, "keys"))
1931 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001932 else
1933 {
1934 PyObject *iterator;
1935 PyObject *item;
1936
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001937 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001938 return NULL;
1939
1940 while ((item = PyIter_Next(iterator)))
1941 {
1942 PyObject *fast;
1943 PyObject *keyObject;
1944 PyObject *valObject;
1945 PyObject *todecref;
1946 char_u *key;
1947 dictitem_T *di;
1948
1949 if (!(fast = PySequence_Fast(item, "")))
1950 {
1951 Py_DECREF(iterator);
1952 Py_DECREF(item);
1953 return NULL;
1954 }
1955
1956 Py_DECREF(item);
1957
1958 if (PySequence_Fast_GET_SIZE(fast) != 2)
1959 {
1960 Py_DECREF(iterator);
1961 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001962 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001963 N_("expected sequence element of size 2, "
1964 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001965 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001966 return NULL;
1967 }
1968
1969 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1970
1971 if (!(key = StringToChars(keyObject, &todecref)))
1972 {
1973 Py_DECREF(iterator);
1974 Py_DECREF(fast);
1975 return NULL;
1976 }
1977
1978 di = dictitem_alloc(key);
1979
1980 Py_XDECREF(todecref);
1981
1982 if (di == NULL)
1983 {
1984 Py_DECREF(fast);
1985 Py_DECREF(iterator);
1986 PyErr_NoMemory();
1987 return NULL;
1988 }
1989 di->di_tv.v_lock = 0;
1990 di->di_tv.v_type = VAR_UNKNOWN;
1991
1992 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1993
1994 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1995 {
1996 Py_DECREF(iterator);
1997 Py_DECREF(fast);
1998 dictitem_free(di);
1999 return NULL;
2000 }
2001
2002 Py_DECREF(fast);
2003
2004 if (dict_add(dict, di) == FAIL)
2005 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002006 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002007 Py_DECREF(iterator);
2008 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002009 return NULL;
2010 }
2011 }
2012
2013 Py_DECREF(iterator);
2014
2015 /* Iterator may have finished due to an exception */
2016 if (PyErr_Occurred())
2017 return NULL;
2018 }
2019 }
2020 Py_INCREF(Py_None);
2021 return Py_None;
2022}
2023
2024 static PyObject *
2025DictionaryGet(DictionaryObject *self, PyObject *args)
2026{
2027 return _DictionaryItem(self, args,
2028 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2029}
2030
2031 static PyObject *
2032DictionaryPop(DictionaryObject *self, PyObject *args)
2033{
2034 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2035}
2036
2037 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002038DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002039{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002040 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002041 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002042 PyObject *valObject;
2043 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002044
Bram Moolenaarde71b562013-06-02 17:41:54 +02002045 if (self->dict->dv_hashtab.ht_used == 0)
2046 {
2047 PyErr_SetNone(PyExc_KeyError);
2048 return NULL;
2049 }
2050
2051 hi = self->dict->dv_hashtab.ht_array;
2052 while (HASHITEM_EMPTY(hi))
2053 ++hi;
2054
2055 di = dict_lookup(hi);
2056
2057 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002058 return NULL;
2059
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002060 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002061 {
2062 Py_DECREF(valObject);
2063 return NULL;
2064 }
2065
2066 hash_remove(&self->dict->dv_hashtab, hi);
2067 dictitem_free(di);
2068
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002069 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002070}
2071
2072 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002073DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002074{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002075 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2076}
2077
2078static PySequenceMethods DictionaryAsSeq = {
2079 0, /* sq_length */
2080 0, /* sq_concat */
2081 0, /* sq_repeat */
2082 0, /* sq_item */
2083 0, /* sq_slice */
2084 0, /* sq_ass_item */
2085 0, /* sq_ass_slice */
2086 (objobjproc) DictionaryContains, /* sq_contains */
2087 0, /* sq_inplace_concat */
2088 0, /* sq_inplace_repeat */
2089};
2090
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002091static PyMappingMethods DictionaryAsMapping = {
2092 (lenfunc) DictionaryLength,
2093 (binaryfunc) DictionaryItem,
2094 (objobjargproc) DictionaryAssItem,
2095};
2096
Bram Moolenaardb913952012-06-29 12:54:53 +02002097static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002098 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002099 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2100 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2101 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2102 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2103 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002104 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002105 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002106 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2107 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002108};
2109
2110static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002111static PySequenceMethods ListAsSeq;
2112static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002113
2114typedef struct
2115{
2116 PyObject_HEAD
2117 list_T *list;
2118 pylinkedlist_T ref;
2119} ListObject;
2120
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002121#define NEW_LIST(list) ListNew(&ListType, list)
2122
Bram Moolenaardb913952012-06-29 12:54:53 +02002123 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002124ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002125{
2126 ListObject *self;
2127
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002128 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002129 if (self == NULL)
2130 return NULL;
2131 self->list = list;
2132 ++list->lv_refcount;
2133
2134 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2135
2136 return (PyObject *)(self);
2137}
2138
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002139 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002140py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002141{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002142 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002143
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002144 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002145 {
2146 PyErr_NoMemory();
2147 return NULL;
2148 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002149 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002150
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002151 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002152}
2153
2154 static int
2155list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2156{
2157 PyObject *iterator;
2158 PyObject *item;
2159 listitem_T *li;
2160
2161 if (!(iterator = PyObject_GetIter(obj)))
2162 return -1;
2163
2164 while ((item = PyIter_Next(iterator)))
2165 {
2166 if (!(li = listitem_alloc()))
2167 {
2168 PyErr_NoMemory();
2169 Py_DECREF(item);
2170 Py_DECREF(iterator);
2171 return -1;
2172 }
2173 li->li_tv.v_lock = 0;
2174 li->li_tv.v_type = VAR_UNKNOWN;
2175
2176 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2177 {
2178 Py_DECREF(item);
2179 Py_DECREF(iterator);
2180 listitem_free(li);
2181 return -1;
2182 }
2183
2184 Py_DECREF(item);
2185
2186 list_append(l, li);
2187 }
2188
2189 Py_DECREF(iterator);
2190
2191 /* Iterator may have finished due to an exception */
2192 if (PyErr_Occurred())
2193 return -1;
2194
2195 return 0;
2196}
2197
2198 static PyObject *
2199ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2200{
2201 list_T *list;
2202 PyObject *obj = NULL;
2203
2204 if (kwargs)
2205 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002206 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002207 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002208 return NULL;
2209 }
2210
2211 if (!PyArg_ParseTuple(args, "|O", &obj))
2212 return NULL;
2213
2214 if (!(list = py_list_alloc()))
2215 return NULL;
2216
2217 if (obj)
2218 {
2219 PyObject *lookup_dict;
2220
2221 if (!(lookup_dict = PyDict_New()))
2222 {
2223 list_unref(list);
2224 return NULL;
2225 }
2226
2227 if (list_py_concat(list, obj, lookup_dict) == -1)
2228 {
2229 Py_DECREF(lookup_dict);
2230 list_unref(list);
2231 return NULL;
2232 }
2233
2234 Py_DECREF(lookup_dict);
2235 }
2236
2237 return ListNew(subtype, list);
2238}
2239
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002240 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002241ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002242{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002243 pyll_remove(&self->ref, &lastlist);
2244 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002245
2246 DESTRUCTOR_FINISH(self);
2247}
2248
Bram Moolenaardb913952012-06-29 12:54:53 +02002249 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002250ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002251{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002252 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002253}
2254
2255 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002256ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002257{
2258 listitem_T *li;
2259
Bram Moolenaard6e39182013-05-21 18:30:34 +02002260 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002261 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002262 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002263 return NULL;
2264 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002265 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002266 if (li == NULL)
2267 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002268 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002269 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002270 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002271 return NULL;
2272 }
2273 return ConvertToPyObject(&li->li_tv);
2274}
2275
2276#define PROC_RANGE \
2277 if (last < 0) {\
2278 if (last < -size) \
2279 last = 0; \
2280 else \
2281 last += size; \
2282 } \
2283 if (first < 0) \
2284 first = 0; \
2285 if (first > size) \
2286 first = size; \
2287 if (last > size) \
2288 last = size;
2289
2290 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002291ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002292{
2293 PyInt i;
2294 PyInt size = ListLength(self);
2295 PyInt n;
2296 PyObject *list;
2297 int reversed = 0;
2298
2299 PROC_RANGE
2300 if (first >= last)
2301 first = last;
2302
2303 n = last-first;
2304 list = PyList_New(n);
2305 if (list == NULL)
2306 return NULL;
2307
2308 for (i = 0; i < n; ++i)
2309 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002310 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002311 if (item == NULL)
2312 {
2313 Py_DECREF(list);
2314 return NULL;
2315 }
2316
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002317 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002318 }
2319
2320 return list;
2321}
2322
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002323typedef struct
2324{
2325 listwatch_T lw;
2326 list_T *list;
2327} listiterinfo_T;
2328
2329 static void
2330ListIterDestruct(listiterinfo_T *lii)
2331{
2332 list_rem_watch(lii->list, &lii->lw);
2333 PyMem_Free(lii);
2334}
2335
2336 static PyObject *
2337ListIterNext(listiterinfo_T **lii)
2338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002339 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002340
2341 if (!((*lii)->lw.lw_item))
2342 return NULL;
2343
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002344 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002345 return NULL;
2346
2347 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2348
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002349 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002350}
2351
2352 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002353ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002354{
2355 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002356 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002357
2358 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2359 {
2360 PyErr_NoMemory();
2361 return NULL;
2362 }
2363
2364 list_add_watch(l, &lii->lw);
2365 lii->lw.lw_item = l->lv_first;
2366 lii->list = l;
2367
2368 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002369 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2370 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002371}
2372
Bram Moolenaardb913952012-06-29 12:54:53 +02002373 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002374ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002375{
2376 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002377 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002378 listitem_T *li;
2379 Py_ssize_t length = ListLength(self);
2380
2381 if (l->lv_lock)
2382 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002383 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002384 return -1;
2385 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002386 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002387 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002388 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002389 return -1;
2390 }
2391
2392 if (obj == NULL)
2393 {
2394 li = list_find(l, (long) index);
2395 list_remove(l, li, li);
2396 clear_tv(&li->li_tv);
2397 vim_free(li);
2398 return 0;
2399 }
2400
2401 if (ConvertFromPyObject(obj, &tv) == -1)
2402 return -1;
2403
2404 if (index == length)
2405 {
2406 if (list_append_tv(l, &tv) == FAIL)
2407 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002408 clear_tv(&tv);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002409 PyErr_SET_VIM(N_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002410 return -1;
2411 }
2412 }
2413 else
2414 {
2415 li = list_find(l, (long) index);
2416 clear_tv(&li->li_tv);
2417 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002418 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002419 }
2420 return 0;
2421}
2422
2423 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002424ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002425{
2426 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002427 PyObject *iterator;
2428 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002429 listitem_T *li;
2430 listitem_T *next;
2431 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002432 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002433 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002434
2435 if (l->lv_lock)
2436 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002437 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002438 return -1;
2439 }
2440
2441 PROC_RANGE
2442
2443 if (first == size)
2444 li = NULL;
2445 else
2446 {
2447 li = list_find(l, (long) first);
2448 if (li == NULL)
2449 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002450 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2451 (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002452 return -1;
2453 }
2454 if (last > first)
2455 {
2456 i = last - first;
2457 while (i-- && li != NULL)
2458 {
2459 next = li->li_next;
2460 listitem_remove(l, li);
2461 li = next;
2462 }
2463 }
2464 }
2465
2466 if (obj == NULL)
2467 return 0;
2468
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002469 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002470 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002471
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002472 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002473 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002474 if (ConvertFromPyObject(item, &v) == -1)
2475 {
2476 Py_DECREF(iterator);
2477 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002478 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002479 }
2480 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002481 if (list_insert_tv(l, &v, li) == FAIL)
2482 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002483 clear_tv(&v);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002484 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002485 return -1;
2486 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002487 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002488 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002489 Py_DECREF(iterator);
Bram Moolenaardee2e312013-06-23 16:35:47 +02002490
2491 if (PyErr_Occurred())
2492 return -1;
2493
Bram Moolenaardb913952012-06-29 12:54:53 +02002494 return 0;
2495}
2496
2497 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002498ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002499{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002500 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002501 PyObject *lookup_dict;
2502
2503 if (l->lv_lock)
2504 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002505 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002506 return NULL;
2507 }
2508
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002509 if (!(lookup_dict = PyDict_New()))
2510 return NULL;
2511
Bram Moolenaardb913952012-06-29 12:54:53 +02002512 if (list_py_concat(l, obj, lookup_dict) == -1)
2513 {
2514 Py_DECREF(lookup_dict);
2515 return NULL;
2516 }
2517 Py_DECREF(lookup_dict);
2518
2519 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002520 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002521}
2522
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002523static char *ListAttrs[] = {
2524 "locked",
2525 NULL
2526};
2527
2528 static PyObject *
2529ListDir(PyObject *self)
2530{
2531 return ObjectDir(self, ListAttrs);
2532}
2533
Bram Moolenaar66b79852012-09-21 14:00:35 +02002534 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002535ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002536{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002537 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002538 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002539 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002540 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002541 return -1;
2542 }
2543
2544 if (strcmp(name, "locked") == 0)
2545 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002546 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002547 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002548 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002549 return -1;
2550 }
2551 else
2552 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002553 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002554 if (istrue == -1)
2555 return -1;
2556 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002557 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002558 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002559 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002560 }
2561 return 0;
2562 }
2563 else
2564 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002565 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002566 return -1;
2567 }
2568}
2569
Bram Moolenaardb913952012-06-29 12:54:53 +02002570static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002571 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2572 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2573 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002574};
2575
2576typedef struct
2577{
2578 PyObject_HEAD
2579 char_u *name;
2580} FunctionObject;
2581
2582static PyTypeObject FunctionType;
2583
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002584#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2585
Bram Moolenaardb913952012-06-29 12:54:53 +02002586 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002587FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002588{
2589 FunctionObject *self;
2590
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002591 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2592
Bram Moolenaardb913952012-06-29 12:54:53 +02002593 if (self == NULL)
2594 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002595
2596 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002597 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002598 if (!translated_function_exists(name))
2599 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002600 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002601 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002602 return NULL;
2603 }
2604 self->name = vim_strsave(name);
2605 func_ref(self->name);
2606 }
2607 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002608 if ((self->name = get_expanded_name(name,
2609 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2610 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002611 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002612 PyErr_FORMAT(PyExc_ValueError,
2613 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002614 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002615 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002616
2617 return (PyObject *)(self);
2618}
2619
2620 static PyObject *
2621FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2622{
2623 PyObject *self;
2624 char_u *name;
2625
2626 if (kwargs)
2627 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002628 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002629 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002630 return NULL;
2631 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002632
Bram Moolenaar389a1792013-06-23 13:00:44 +02002633 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002634 return NULL;
2635
2636 self = FunctionNew(subtype, name);
2637
Bram Moolenaar389a1792013-06-23 13:00:44 +02002638 PyMem_Free(name);
2639
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002640 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002641}
2642
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002643 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002644FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002645{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002646 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002647 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002648
2649 DESTRUCTOR_FINISH(self);
2650}
2651
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002652static char *FunctionAttrs[] = {
2653 "softspace",
2654 NULL
2655};
2656
2657 static PyObject *
2658FunctionDir(PyObject *self)
2659{
2660 return ObjectDir(self, FunctionAttrs);
2661}
2662
Bram Moolenaardb913952012-06-29 12:54:53 +02002663 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002664FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002665{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002666 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002667 typval_T args;
2668 typval_T selfdicttv;
2669 typval_T rettv;
2670 dict_T *selfdict = NULL;
2671 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002672 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002673 int error;
2674
2675 if (ConvertFromPyObject(argsObject, &args) == -1)
2676 return NULL;
2677
2678 if (kwargs != NULL)
2679 {
2680 selfdictObject = PyDict_GetItemString(kwargs, "self");
2681 if (selfdictObject != NULL)
2682 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002683 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002684 {
2685 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002686 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002687 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002688 selfdict = selfdicttv.vval.v_dict;
2689 }
2690 }
2691
Bram Moolenaar71700b82013-05-15 17:49:05 +02002692 Py_BEGIN_ALLOW_THREADS
2693 Python_Lock_Vim();
2694
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002695 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002696 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002697
2698 Python_Release_Vim();
2699 Py_END_ALLOW_THREADS
2700
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002701 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002702 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002703 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002704 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002705 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002706 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002707 }
2708 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002709 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002710
Bram Moolenaardb913952012-06-29 12:54:53 +02002711 clear_tv(&args);
2712 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002713 if (selfdict != NULL)
2714 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002715
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002716 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002717}
2718
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002719 static PyObject *
2720FunctionRepr(FunctionObject *self)
2721{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002722#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002723 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002724 * Finalize */
2725 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002726 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002727#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002728 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002729#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002730}
2731
Bram Moolenaardb913952012-06-29 12:54:53 +02002732static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002733 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2734 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002735};
2736
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002737/*
2738 * Options object
2739 */
2740
2741static PyTypeObject OptionsType;
2742
2743typedef int (*checkfun)(void *);
2744
2745typedef struct
2746{
2747 PyObject_HEAD
2748 int opt_type;
2749 void *from;
2750 checkfun Check;
2751 PyObject *fromObj;
2752} OptionsObject;
2753
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002754 static int
2755dummy_check(void *arg UNUSED)
2756{
2757 return 0;
2758}
2759
2760 static PyObject *
2761OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2762{
2763 OptionsObject *self;
2764
Bram Moolenaar774267b2013-05-21 20:51:59 +02002765 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002766 if (self == NULL)
2767 return NULL;
2768
2769 self->opt_type = opt_type;
2770 self->from = from;
2771 self->Check = Check;
2772 self->fromObj = fromObj;
2773 if (fromObj)
2774 Py_INCREF(fromObj);
2775
2776 return (PyObject *)(self);
2777}
2778
2779 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002780OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002781{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002782 PyObject_GC_UnTrack((void *)(self));
2783 Py_XDECREF(self->fromObj);
2784 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002785}
2786
2787 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002788OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002789{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002790 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002791 return 0;
2792}
2793
2794 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002795OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002796{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002797 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002798 return 0;
2799}
2800
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002801 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002802OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002803{
2804 char_u *key;
2805 int flags;
2806 long numval;
2807 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002808 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002809
Bram Moolenaard6e39182013-05-21 18:30:34 +02002810 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002811 return NULL;
2812
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002813 if (!(key = StringToChars(keyObject, &todecref)))
2814 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002815
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002816 if (*key == NUL)
2817 {
2818 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002819 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002820 return NULL;
2821 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002822
2823 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002824 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002825
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002826 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002827
2828 if (flags == 0)
2829 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002830 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002831 return NULL;
2832 }
2833
2834 if (flags & SOPT_UNSET)
2835 {
2836 Py_INCREF(Py_None);
2837 return Py_None;
2838 }
2839 else if (flags & SOPT_BOOL)
2840 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002841 PyObject *ret;
2842 ret = numval ? Py_True : Py_False;
2843 Py_INCREF(ret);
2844 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002845 }
2846 else if (flags & SOPT_NUM)
2847 return PyInt_FromLong(numval);
2848 else if (flags & SOPT_STRING)
2849 {
2850 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002851 {
Bram Moolenaar41009372013-07-01 22:03:04 +02002852 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002853 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002854 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002855 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002856 else
2857 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002858 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002859 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002860 return NULL;
2861 }
2862 }
2863 else
2864 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002865 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002866 return NULL;
2867 }
2868}
2869
2870 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002871set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002872{
2873 char_u *errmsg;
2874
2875 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2876 {
2877 if (VimTryEnd())
2878 return FAIL;
2879 PyErr_SetVim((char *)errmsg);
2880 return FAIL;
2881 }
2882 return OK;
2883}
2884
2885 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002886set_option_value_for(
2887 char_u *key,
2888 int numval,
2889 char_u *stringval,
2890 int opt_flags,
2891 int opt_type,
2892 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002893{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002894 win_T *save_curwin = NULL;
2895 tabpage_T *save_curtab = NULL;
2896 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002897 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002898
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002899 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002900 switch (opt_type)
2901 {
2902 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002903 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002904 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002905 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002906 if (VimTryEnd())
2907 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002908 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002909 return -1;
2910 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002911 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2912 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002913 break;
2914 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002915 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002916 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002917 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002918 break;
2919 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002920 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002921 break;
2922 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002923 if (set_ret == FAIL)
2924 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002925 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002926}
2927
2928 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002929OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002930{
2931 char_u *key;
2932 int flags;
2933 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002934 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002935 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002936
Bram Moolenaard6e39182013-05-21 18:30:34 +02002937 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002938 return -1;
2939
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002940 if (!(key = StringToChars(keyObject, &todecref)))
2941 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002942
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002943 if (*key == NUL)
2944 {
2945 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002946 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002947 return -1;
2948 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002949
2950 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002951 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002952
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002953 if (flags == 0)
2954 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002955 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002956 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002957 return -1;
2958 }
2959
2960 if (valObject == NULL)
2961 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002962 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002963 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002964 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002965 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002966 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002967 return -1;
2968 }
2969 else if (!(flags & SOPT_GLOBAL))
2970 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002971 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002972 N_("unable to unset option %s "
2973 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002974 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002975 return -1;
2976 }
2977 else
2978 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002979 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002980 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002981 return 0;
2982 }
2983 }
2984
Bram Moolenaard6e39182013-05-21 18:30:34 +02002985 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002986
2987 if (flags & SOPT_BOOL)
2988 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002989 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002990
Bram Moolenaarb983f752013-05-15 16:11:50 +02002991 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002992 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002993 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002994 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002995 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002996 }
2997 else if (flags & SOPT_NUM)
2998 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002999 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003000
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003001 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003002 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003003 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003004 return -1;
3005 }
3006
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003007 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003008 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003009 }
3010 else
3011 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003012 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003013 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003014
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003015 if ((val = StringToChars(valObject, &todecref2)))
3016 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003017 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003018 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003019 Py_XDECREF(todecref2);
3020 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003021 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003022 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003023 }
3024
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003025 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003026
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003027 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003028}
3029
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003030static PyMappingMethods OptionsAsMapping = {
3031 (lenfunc) NULL,
3032 (binaryfunc) OptionsItem,
3033 (objobjargproc) OptionsAssItem,
3034};
3035
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003036/* Tabpage object
3037 */
3038
3039typedef struct
3040{
3041 PyObject_HEAD
3042 tabpage_T *tab;
3043} TabPageObject;
3044
3045static PyObject *WinListNew(TabPageObject *tabObject);
3046
3047static PyTypeObject TabPageType;
3048
3049 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003050CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003051{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003052 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003053 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003054 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003055 return -1;
3056 }
3057
3058 return 0;
3059}
3060
3061 static PyObject *
3062TabPageNew(tabpage_T *tab)
3063{
3064 TabPageObject *self;
3065
3066 if (TAB_PYTHON_REF(tab))
3067 {
3068 self = TAB_PYTHON_REF(tab);
3069 Py_INCREF(self);
3070 }
3071 else
3072 {
3073 self = PyObject_NEW(TabPageObject, &TabPageType);
3074 if (self == NULL)
3075 return NULL;
3076 self->tab = tab;
3077 TAB_PYTHON_REF(tab) = self;
3078 }
3079
3080 return (PyObject *)(self);
3081}
3082
3083 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003084TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003085{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003086 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3087 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003088
3089 DESTRUCTOR_FINISH(self);
3090}
3091
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003092static char *TabPageAttrs[] = {
3093 "windows", "number", "vars", "window", "valid",
3094 NULL
3095};
3096
3097 static PyObject *
3098TabPageDir(PyObject *self)
3099{
3100 return ObjectDir(self, TabPageAttrs);
3101}
3102
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003103 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003104TabPageAttrValid(TabPageObject *self, char *name)
3105{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003106 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003107
3108 if (strcmp(name, "valid") != 0)
3109 return NULL;
3110
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003111 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3112 Py_INCREF(ret);
3113 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003114}
3115
3116 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003117TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003118{
3119 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003120 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003121 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003122 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003123 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003124 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003125 else if (strcmp(name, "window") == 0)
3126 {
3127 /* For current tab window.c does not bother to set or update tp_curwin
3128 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003129 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003130 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003131 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003132 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003133 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003134 else if (strcmp(name, "__members__") == 0)
3135 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003136 return NULL;
3137}
3138
3139 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003140TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003141{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003142 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003143 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003144 else
3145 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003146 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003147
3148 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003149 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3150 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003151 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003152 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003153 }
3154}
3155
3156static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003157 /* name, function, calling, documentation */
3158 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3159 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003160};
3161
3162/*
3163 * Window list object
3164 */
3165
3166static PyTypeObject TabListType;
3167static PySequenceMethods TabListAsSeq;
3168
3169typedef struct
3170{
3171 PyObject_HEAD
3172} TabListObject;
3173
3174 static PyInt
3175TabListLength(PyObject *self UNUSED)
3176{
3177 tabpage_T *tp = first_tabpage;
3178 PyInt n = 0;
3179
3180 while (tp != NULL)
3181 {
3182 ++n;
3183 tp = tp->tp_next;
3184 }
3185
3186 return n;
3187}
3188
3189 static PyObject *
3190TabListItem(PyObject *self UNUSED, PyInt n)
3191{
3192 tabpage_T *tp;
3193
3194 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3195 if (n == 0)
3196 return TabPageNew(tp);
3197
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003198 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003199 return NULL;
3200}
3201
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003202/*
3203 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003204 */
3205
3206typedef struct
3207{
3208 PyObject_HEAD
3209 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003210 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003211} WindowObject;
3212
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003213static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003214
3215 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003216CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003217{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003218 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003219 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003220 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003221 return -1;
3222 }
3223
3224 return 0;
3225}
3226
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003227 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003228WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003229{
3230 /* We need to handle deletion of windows underneath us.
3231 * If we add a "w_python*_ref" field to the win_T structure,
3232 * then we can get at it in win_free() in vim. We then
3233 * need to create only ONE Python object per window - if
3234 * we try to create a second, just INCREF the existing one
3235 * and return it. The (single) Python object referring to
3236 * the window is stored in "w_python*_ref".
3237 * On a win_free() we set the Python object's win_T* field
3238 * to an invalid value. We trap all uses of a window
3239 * object, and reject them if the win_T* field is invalid.
3240 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003241 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003242 * w_python_ref and w_python3_ref fields respectively.
3243 */
3244
3245 WindowObject *self;
3246
3247 if (WIN_PYTHON_REF(win))
3248 {
3249 self = WIN_PYTHON_REF(win);
3250 Py_INCREF(self);
3251 }
3252 else
3253 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003254 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003255 if (self == NULL)
3256 return NULL;
3257 self->win = win;
3258 WIN_PYTHON_REF(win) = self;
3259 }
3260
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003261 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3262
Bram Moolenaar971db462013-05-12 18:44:48 +02003263 return (PyObject *)(self);
3264}
3265
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003266 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003267WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003268{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003269 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003270 if (self->win && self->win != INVALID_WINDOW_VALUE)
3271 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003272 Py_XDECREF(((PyObject *)(self->tabObject)));
3273 PyObject_GC_Del((void *)(self));
3274}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003275
Bram Moolenaar774267b2013-05-21 20:51:59 +02003276 static int
3277WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3278{
3279 Py_VISIT(((PyObject *)(self->tabObject)));
3280 return 0;
3281}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003282
Bram Moolenaar774267b2013-05-21 20:51:59 +02003283 static int
3284WindowClear(WindowObject *self)
3285{
3286 Py_CLEAR(self->tabObject);
3287 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003288}
3289
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003290 static win_T *
3291get_firstwin(TabPageObject *tabObject)
3292{
3293 if (tabObject)
3294 {
3295 if (CheckTabPage(tabObject))
3296 return NULL;
3297 /* For current tab window.c does not bother to set or update tp_firstwin
3298 */
3299 else if (tabObject->tab == curtab)
3300 return firstwin;
3301 else
3302 return tabObject->tab->tp_firstwin;
3303 }
3304 else
3305 return firstwin;
3306}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003307static char *WindowAttrs[] = {
3308 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3309 "tabpage", "valid",
3310 NULL
3311};
3312
3313 static PyObject *
3314WindowDir(PyObject *self)
3315{
3316 return ObjectDir(self, WindowAttrs);
3317}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003318
Bram Moolenaar971db462013-05-12 18:44:48 +02003319 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003320WindowAttrValid(WindowObject *self, char *name)
3321{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003322 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003323
3324 if (strcmp(name, "valid") != 0)
3325 return NULL;
3326
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003327 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3328 Py_INCREF(ret);
3329 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003330}
3331
3332 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003334{
3335 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003337 else if (strcmp(name, "cursor") == 0)
3338 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003339 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003340
3341 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3342 }
3343 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003345#ifdef FEAT_WINDOWS
3346 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003347 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003348#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003349#ifdef FEAT_VERTSPLIT
3350 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003351 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003352 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003353 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003354#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003355 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003356 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003357 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003358 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3359 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003360 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003361 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003362 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003363 return NULL;
3364 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003365 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003366 }
3367 else if (strcmp(name, "tabpage") == 0)
3368 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003369 Py_INCREF(self->tabObject);
3370 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003371 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003372 else if (strcmp(name, "__members__") == 0)
3373 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003374 else
3375 return NULL;
3376}
3377
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003378 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003379WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003380{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003381 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003382 return -1;
3383
3384 if (strcmp(name, "buffer") == 0)
3385 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003386 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003387 return -1;
3388 }
3389 else if (strcmp(name, "cursor") == 0)
3390 {
3391 long lnum;
3392 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003393
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003394 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003395 return -1;
3396
Bram Moolenaard6e39182013-05-21 18:30:34 +02003397 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003398 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003399 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003400 return -1;
3401 }
3402
3403 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003404 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003405 return -1;
3406
Bram Moolenaard6e39182013-05-21 18:30:34 +02003407 self->win->w_cursor.lnum = lnum;
3408 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003409#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003410 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003411#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003412 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003413 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003414
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003415 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003416 return 0;
3417 }
3418 else if (strcmp(name, "height") == 0)
3419 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003420 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003421 win_T *savewin;
3422
Bram Moolenaardee2e312013-06-23 16:35:47 +02003423 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003424 return -1;
3425
3426#ifdef FEAT_GUI
3427 need_mouse_correct = TRUE;
3428#endif
3429 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003430 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003431
3432 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003433 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003434 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003435 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003436 return -1;
3437
3438 return 0;
3439 }
3440#ifdef FEAT_VERTSPLIT
3441 else if (strcmp(name, "width") == 0)
3442 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003443 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003444 win_T *savewin;
3445
Bram Moolenaardee2e312013-06-23 16:35:47 +02003446 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003447 return -1;
3448
3449#ifdef FEAT_GUI
3450 need_mouse_correct = TRUE;
3451#endif
3452 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003453 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003454
3455 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003456 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003457 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003458 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003459 return -1;
3460
3461 return 0;
3462 }
3463#endif
3464 else
3465 {
3466 PyErr_SetString(PyExc_AttributeError, name);
3467 return -1;
3468 }
3469}
3470
3471 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003472WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003473{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003474 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003475 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003476 else
3477 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003478 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003479
Bram Moolenaar6d216452013-05-12 19:00:41 +02003480 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003481 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003482 (self));
3483 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003484 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003485 }
3486}
3487
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003488static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003489 /* name, function, calling, documentation */
3490 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3491 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003492};
3493
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003494/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003495 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003496 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003497
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003498static PyTypeObject WinListType;
3499static PySequenceMethods WinListAsSeq;
3500
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003501typedef struct
3502{
3503 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003504 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003505} WinListObject;
3506
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003507 static PyObject *
3508WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003509{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003510 WinListObject *self;
3511
3512 self = PyObject_NEW(WinListObject, &WinListType);
3513 self->tabObject = tabObject;
3514 Py_INCREF(tabObject);
3515
3516 return (PyObject *)(self);
3517}
3518
3519 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003520WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003521{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003522 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003523
3524 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003525 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003526 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003527 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003528
3529 DESTRUCTOR_FINISH(self);
3530}
3531
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003532 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003533WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003534{
3535 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003536 PyInt n = 0;
3537
Bram Moolenaard6e39182013-05-21 18:30:34 +02003538 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003539 return -1;
3540
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003541 while (w != NULL)
3542 {
3543 ++n;
3544 w = W_NEXT(w);
3545 }
3546
3547 return n;
3548}
3549
3550 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003551WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003552{
3553 win_T *w;
3554
Bram Moolenaard6e39182013-05-21 18:30:34 +02003555 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003556 return NULL;
3557
3558 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003559 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003560 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003561
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003562 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003563 return NULL;
3564}
3565
3566/* Convert a Python string into a Vim line.
3567 *
3568 * The result is in allocated memory. All internal nulls are replaced by
3569 * newline characters. It is an error for the string to contain newline
3570 * characters.
3571 *
3572 * On errors, the Python exception data is set, and NULL is returned.
3573 */
3574 static char *
3575StringToLine(PyObject *obj)
3576{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003577 char *str;
3578 char *save;
3579 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003580 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003581 PyInt i;
3582 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003583
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003584 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003585 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003586 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3587 || str == NULL)
3588 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003589 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003590 else if (PyUnicode_Check(obj))
3591 {
3592 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3593 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003594
Bram Moolenaardaa27022013-06-24 22:33:30 +02003595 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003596 || str == NULL)
3597 {
3598 Py_DECREF(bytes);
3599 return NULL;
3600 }
3601 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003602 else
3603 {
3604#if PY_MAJOR_VERSION < 3
3605 PyErr_FORMAT(PyExc_TypeError,
3606 N_("expected str() or unicode() instance, but got %s"),
3607 Py_TYPE_NAME(obj));
3608#else
3609 PyErr_FORMAT(PyExc_TypeError,
3610 N_("expected bytes() or str() instance, but got %s"),
3611 Py_TYPE_NAME(obj));
3612#endif
3613 return NULL;
3614 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003615
3616 /*
3617 * Error checking: String must not contain newlines, as we
3618 * are replacing a single line, and we must replace it with
3619 * a single line.
3620 * A trailing newline is removed, so that append(f.readlines()) works.
3621 */
3622 p = memchr(str, '\n', len);
3623 if (p != NULL)
3624 {
3625 if (p == str + len - 1)
3626 --len;
3627 else
3628 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003629 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003630 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003631 return NULL;
3632 }
3633 }
3634
3635 /* Create a copy of the string, with internal nulls replaced by
3636 * newline characters, as is the vim convention.
3637 */
3638 save = (char *)alloc((unsigned)(len+1));
3639 if (save == NULL)
3640 {
3641 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003642 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003643 return NULL;
3644 }
3645
3646 for (i = 0; i < len; ++i)
3647 {
3648 if (str[i] == '\0')
3649 save[i] = '\n';
3650 else
3651 save[i] = str[i];
3652 }
3653
3654 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003655 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003656
3657 return save;
3658}
3659
3660/* Get a line from the specified buffer. The line number is
3661 * in Vim format (1-based). The line is returned as a Python
3662 * string object.
3663 */
3664 static PyObject *
3665GetBufferLine(buf_T *buf, PyInt n)
3666{
3667 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3668}
3669
3670
3671/* Get a list of lines from the specified buffer. The line numbers
3672 * are in Vim format (1-based). The range is from lo up to, but not
3673 * including, hi. The list is returned as a Python list of string objects.
3674 */
3675 static PyObject *
3676GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3677{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003678 PyInt i;
3679 PyInt n = hi - lo;
3680 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003681
3682 if (list == NULL)
3683 return NULL;
3684
3685 for (i = 0; i < n; ++i)
3686 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003687 PyObject *string = LineToString(
3688 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003689
3690 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003691 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003692 {
3693 Py_DECREF(list);
3694 return NULL;
3695 }
3696
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003697 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003698 }
3699
3700 /* The ownership of the Python list is passed to the caller (ie,
3701 * the caller should Py_DECREF() the object when it is finished
3702 * with it).
3703 */
3704
3705 return list;
3706}
3707
3708/*
3709 * Check if deleting lines made the cursor position invalid.
3710 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3711 * deleted).
3712 */
3713 static void
3714py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3715{
3716 if (curwin->w_cursor.lnum >= lo)
3717 {
3718 /* Adjust the cursor position if it's in/after the changed
3719 * lines. */
3720 if (curwin->w_cursor.lnum >= hi)
3721 {
3722 curwin->w_cursor.lnum += extra;
3723 check_cursor_col();
3724 }
3725 else if (extra < 0)
3726 {
3727 curwin->w_cursor.lnum = lo;
3728 check_cursor();
3729 }
3730 else
3731 check_cursor_col();
3732 changed_cline_bef_curs();
3733 }
3734 invalidate_botline();
3735}
3736
Bram Moolenaar19e60942011-06-19 00:27:51 +02003737/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003738 * Find a window that contains "buf" and switch to it.
3739 * If there is no such window, use the current window and change "curbuf".
3740 * Caller must initialize save_curbuf to NULL.
3741 * restore_win_for_buf() MUST be called later!
3742 */
3743 static void
3744switch_to_win_for_buf(
3745 buf_T *buf,
3746 win_T **save_curwinp,
3747 tabpage_T **save_curtabp,
3748 buf_T **save_curbufp)
3749{
3750 win_T *wp;
3751 tabpage_T *tp;
3752
3753 if (find_win_for_buf(buf, &wp, &tp) == FAIL
3754 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
3755 switch_buffer(save_curbufp, buf);
3756}
3757
3758 static void
3759restore_win_for_buf(
3760 win_T *save_curwin,
3761 tabpage_T *save_curtab,
3762 buf_T *save_curbuf)
3763{
3764 if (save_curbuf == NULL)
3765 restore_win(save_curwin, save_curtab, TRUE);
3766 else
3767 restore_buffer(save_curbuf);
3768}
3769
3770/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02003771 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003772 * in Vim format (1-based). The replacement line is given as
3773 * a Python string object. The object is checked for validity
3774 * and correct format. Errors are returned as a value of FAIL.
3775 * The return value is OK on success.
3776 * If OK is returned and len_change is not NULL, *len_change
3777 * is set to the change in the buffer length.
3778 */
3779 static int
3780SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3781{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003782 buf_T *save_curbuf = NULL;
3783 win_T *save_curwin = NULL;
3784 tabpage_T *save_curtab = NULL;
3785
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003786 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003787 * There are three cases:
3788 * 1. NULL, or None - this is a deletion.
3789 * 2. A string - this is a replacement.
3790 * 3. Anything else - this is an error.
3791 */
3792 if (line == Py_None || line == NULL)
3793 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003794 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003795 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003796
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003797 VimTryStart();
3798
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003799 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003800 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003801 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003802 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003803 else
3804 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003805 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003806 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003807 if (save_curbuf == NULL)
3808 /* Only adjust marks if we managed to switch to a window that
3809 * holds the buffer, otherwise line numbers will be invalid. */
3810 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003811 }
3812
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003813 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003814
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003815 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003816 return FAIL;
3817
3818 if (len_change)
3819 *len_change = -1;
3820
3821 return OK;
3822 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003823 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003824 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003825 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003826
3827 if (save == NULL)
3828 return FAIL;
3829
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003830 VimTryStart();
3831
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003832 /* We do not need to free "save" if ml_replace() consumes it. */
3833 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003834 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003835
3836 if (u_savesub((linenr_T)n) == FAIL)
3837 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003838 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003839 vim_free(save);
3840 }
3841 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3842 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003843 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844 vim_free(save);
3845 }
3846 else
3847 changed_bytes((linenr_T)n, 0);
3848
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003849 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003850
3851 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003852 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003853 check_cursor_col();
3854
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003855 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003856 return FAIL;
3857
3858 if (len_change)
3859 *len_change = 0;
3860
3861 return OK;
3862 }
3863 else
3864 {
3865 PyErr_BadArgument();
3866 return FAIL;
3867 }
3868}
3869
Bram Moolenaar19e60942011-06-19 00:27:51 +02003870/* Replace a range of lines in the specified buffer. The line numbers are in
3871 * Vim format (1-based). The range is from lo up to, but not including, hi.
3872 * The replacement lines are given as a Python list of string objects. The
3873 * list is checked for validity and correct format. Errors are returned as a
3874 * value of FAIL. The return value is OK on success.
3875 * If OK is returned and len_change is not NULL, *len_change
3876 * is set to the change in the buffer length.
3877 */
3878 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003879SetBufferLineList(
3880 buf_T *buf,
3881 PyInt lo,
3882 PyInt hi,
3883 PyObject *list,
3884 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003885{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003886 buf_T *save_curbuf = NULL;
3887 win_T *save_curwin = NULL;
3888 tabpage_T *save_curtab = NULL;
3889
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003890 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003891 * There are three cases:
3892 * 1. NULL, or None - this is a deletion.
3893 * 2. A list - this is a replacement.
3894 * 3. Anything else - this is an error.
3895 */
3896 if (list == Py_None || list == NULL)
3897 {
3898 PyInt i;
3899 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003900
3901 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003902 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003903 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003904
3905 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003906 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003907 else
3908 {
3909 for (i = 0; i < n; ++i)
3910 {
3911 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3912 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003913 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003914 break;
3915 }
3916 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003917 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003918 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003919 if (save_curbuf == NULL)
3920 /* Only adjust marks if we managed to switch to a window that
3921 * holds the buffer, otherwise line numbers will be invalid. */
3922 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003923 }
3924
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003925 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003926
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003927 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003928 return FAIL;
3929
3930 if (len_change)
3931 *len_change = -n;
3932
3933 return OK;
3934 }
3935 else if (PyList_Check(list))
3936 {
3937 PyInt i;
3938 PyInt new_len = PyList_Size(list);
3939 PyInt old_len = hi - lo;
3940 PyInt extra = 0; /* lines added to text, can be negative */
3941 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003942
3943 if (new_len == 0) /* avoid allocating zero bytes */
3944 array = NULL;
3945 else
3946 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003947 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003948 if (array == NULL)
3949 {
3950 PyErr_NoMemory();
3951 return FAIL;
3952 }
3953 }
3954
3955 for (i = 0; i < new_len; ++i)
3956 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003957 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003958
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003959 if (!(line = PyList_GetItem(list, i)) ||
3960 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003961 {
3962 while (i)
3963 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003964 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003965 return FAIL;
3966 }
3967 }
3968
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003969 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003970 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003971
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003972 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003973 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003974
3975 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003976 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003977
3978 /* If the size of the range is reducing (ie, new_len < old_len) we
3979 * need to delete some old_len. We do this at the start, by
3980 * repeatedly deleting line "lo".
3981 */
3982 if (!PyErr_Occurred())
3983 {
3984 for (i = 0; i < old_len - new_len; ++i)
3985 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3986 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003987 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003988 break;
3989 }
3990 extra -= i;
3991 }
3992
3993 /* For as long as possible, replace the existing old_len with the
3994 * new old_len. This is a more efficient operation, as it requires
3995 * less memory allocation and freeing.
3996 */
3997 if (!PyErr_Occurred())
3998 {
3999 for (i = 0; i < old_len && i < new_len; ++i)
4000 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4001 == FAIL)
4002 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004003 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004004 break;
4005 }
4006 }
4007 else
4008 i = 0;
4009
4010 /* Now we may need to insert the remaining new old_len. If we do, we
4011 * must free the strings as we finish with them (we can't pass the
4012 * responsibility to vim in this case).
4013 */
4014 if (!PyErr_Occurred())
4015 {
4016 while (i < new_len)
4017 {
4018 if (ml_append((linenr_T)(lo + i - 1),
4019 (char_u *)array[i], 0, FALSE) == FAIL)
4020 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004021 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004022 break;
4023 }
4024 vim_free(array[i]);
4025 ++i;
4026 ++extra;
4027 }
4028 }
4029
4030 /* Free any left-over old_len, as a result of an error */
4031 while (i < new_len)
4032 {
4033 vim_free(array[i]);
4034 ++i;
4035 }
4036
4037 /* Free the array of old_len. All of its contents have now
4038 * been dealt with (either freed, or the responsibility passed
4039 * to vim.
4040 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004041 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004042
4043 /* Adjust marks. Invalidate any which lie in the
4044 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004045 * Only adjust marks if we managed to switch to a window that holds
4046 * the buffer, otherwise line numbers will be invalid. */
4047 if (save_curbuf == NULL)
4048 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004049 (long)MAXLNUM, (long)extra);
4050 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4051
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004052 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004053 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4054
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004055 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004056 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004057
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004058 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004059 return FAIL;
4060
4061 if (len_change)
4062 *len_change = new_len - old_len;
4063
4064 return OK;
4065 }
4066 else
4067 {
4068 PyErr_BadArgument();
4069 return FAIL;
4070 }
4071}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004073/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 * The line number is in Vim format (1-based). The lines to be inserted are
4075 * given as a Python list of string objects or as a single string. The lines
4076 * to be added are checked for validity and correct format. Errors are
4077 * returned as a value of FAIL. The return value is OK on success.
4078 * If OK is returned and len_change is not NULL, *len_change
4079 * is set to the change in the buffer length.
4080 */
4081 static int
4082InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4083{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004084 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004085 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004086 tabpage_T *save_curtab = NULL;
4087
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 /* First of all, we check the type of the supplied Python object.
4089 * It must be a string or a list, or the call is in error.
4090 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004091 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004093 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094
4095 if (str == NULL)
4096 return FAIL;
4097
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004099 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004100 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004102 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004103 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004104 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004105 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004106 else if (save_curbuf == NULL)
4107 /* Only adjust marks if we managed to switch to a window that
4108 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 appended_lines_mark((linenr_T)n, 1L);
4110
4111 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004112 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 update_screen(VALID);
4114
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004115 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 return FAIL;
4117
4118 if (len_change)
4119 *len_change = 1;
4120
4121 return OK;
4122 }
4123 else if (PyList_Check(lines))
4124 {
4125 PyInt i;
4126 PyInt size = PyList_Size(lines);
4127 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004128
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004129 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130 if (array == NULL)
4131 {
4132 PyErr_NoMemory();
4133 return FAIL;
4134 }
4135
4136 for (i = 0; i < size; ++i)
4137 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004138 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004140 if (!(line = PyList_GetItem(lines, i)) ||
4141 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004142 {
4143 while (i)
4144 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004145 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146 return FAIL;
4147 }
4148 }
4149
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004151 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004152 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004153
4154 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004155 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004156 else
4157 {
4158 for (i = 0; i < size; ++i)
4159 {
4160 if (ml_append((linenr_T)(n + i),
4161 (char_u *)array[i], 0, FALSE) == FAIL)
4162 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004163 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164
4165 /* Free the rest of the lines */
4166 while (i < size)
4167 vim_free(array[i++]);
4168
4169 break;
4170 }
4171 vim_free(array[i]);
4172 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004173 if (i > 0 && save_curbuf == NULL)
4174 /* Only adjust marks if we managed to switch to a window that
4175 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004176 appended_lines_mark((linenr_T)n, (long)i);
4177 }
4178
4179 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004180 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004181 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004182 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004183
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004184 update_screen(VALID);
4185
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004186 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004187 return FAIL;
4188
4189 if (len_change)
4190 *len_change = size;
4191
4192 return OK;
4193 }
4194 else
4195 {
4196 PyErr_BadArgument();
4197 return FAIL;
4198 }
4199}
4200
4201/*
4202 * Common routines for buffers and line ranges
4203 * -------------------------------------------
4204 */
4205
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004206typedef struct
4207{
4208 PyObject_HEAD
4209 buf_T *buf;
4210} BufferObject;
4211
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004213CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004215 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004217 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218 return -1;
4219 }
4220
4221 return 0;
4222}
4223
4224 static PyObject *
4225RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4226{
4227 if (CheckBuffer(self))
4228 return NULL;
4229
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004230 if (end == -1)
4231 end = self->buf->b_ml.ml_line_count;
4232
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004233 if (n < 0)
4234 n += end - start + 1;
4235
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004236 if (n < 0 || n > end - start)
4237 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004238 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004239 return NULL;
4240 }
4241
4242 return GetBufferLine(self->buf, n+start);
4243}
4244
4245 static PyObject *
4246RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4247{
4248 PyInt size;
4249
4250 if (CheckBuffer(self))
4251 return NULL;
4252
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004253 if (end == -1)
4254 end = self->buf->b_ml.ml_line_count;
4255
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004256 size = end - start + 1;
4257
4258 if (lo < 0)
4259 lo = 0;
4260 else if (lo > size)
4261 lo = size;
4262 if (hi < 0)
4263 hi = 0;
4264 if (hi < lo)
4265 hi = lo;
4266 else if (hi > size)
4267 hi = size;
4268
4269 return GetBufferLineList(self->buf, lo+start, hi+start);
4270}
4271
4272 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004273RBAsItem(
4274 BufferObject *self,
4275 PyInt n,
4276 PyObject *valObject,
4277 PyInt start,
4278 PyInt end,
4279 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004280{
4281 PyInt len_change;
4282
4283 if (CheckBuffer(self))
4284 return -1;
4285
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004286 if (end == -1)
4287 end = self->buf->b_ml.ml_line_count;
4288
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004289 if (n < 0)
4290 n += end - start + 1;
4291
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 if (n < 0 || n > end - start)
4293 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004294 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004295 return -1;
4296 }
4297
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004298 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004299 return -1;
4300
4301 if (new_end)
4302 *new_end = end + len_change;
4303
4304 return 0;
4305}
4306
Bram Moolenaar19e60942011-06-19 00:27:51 +02004307 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004308RBAsSlice(
4309 BufferObject *self,
4310 PyInt lo,
4311 PyInt hi,
4312 PyObject *valObject,
4313 PyInt start,
4314 PyInt end,
4315 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004316{
4317 PyInt size;
4318 PyInt len_change;
4319
4320 /* Self must be a valid buffer */
4321 if (CheckBuffer(self))
4322 return -1;
4323
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004324 if (end == -1)
4325 end = self->buf->b_ml.ml_line_count;
4326
Bram Moolenaar19e60942011-06-19 00:27:51 +02004327 /* Sort out the slice range */
4328 size = end - start + 1;
4329
4330 if (lo < 0)
4331 lo = 0;
4332 else if (lo > size)
4333 lo = size;
4334 if (hi < 0)
4335 hi = 0;
4336 if (hi < lo)
4337 hi = lo;
4338 else if (hi > size)
4339 hi = size;
4340
4341 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004342 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004343 return -1;
4344
4345 if (new_end)
4346 *new_end = end + len_change;
4347
4348 return 0;
4349}
4350
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004351
4352 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004353RBAppend(
4354 BufferObject *self,
4355 PyObject *args,
4356 PyInt start,
4357 PyInt end,
4358 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004359{
4360 PyObject *lines;
4361 PyInt len_change;
4362 PyInt max;
4363 PyInt n;
4364
4365 if (CheckBuffer(self))
4366 return NULL;
4367
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004368 if (end == -1)
4369 end = self->buf->b_ml.ml_line_count;
4370
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004371 max = n = end - start + 1;
4372
4373 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4374 return NULL;
4375
4376 if (n < 0 || n > max)
4377 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004378 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004379 return NULL;
4380 }
4381
4382 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4383 return NULL;
4384
4385 if (new_end)
4386 *new_end = end + len_change;
4387
4388 Py_INCREF(Py_None);
4389 return Py_None;
4390}
4391
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004392/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 */
4394
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004395static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004396static PySequenceMethods RangeAsSeq;
4397static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004398
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004399typedef struct
4400{
4401 PyObject_HEAD
4402 BufferObject *buf;
4403 PyInt start;
4404 PyInt end;
4405} RangeObject;
4406
4407 static PyObject *
4408RangeNew(buf_T *buf, PyInt start, PyInt end)
4409{
4410 BufferObject *bufr;
4411 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004412 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004413 if (self == NULL)
4414 return NULL;
4415
4416 bufr = (BufferObject *)BufferNew(buf);
4417 if (bufr == NULL)
4418 {
4419 Py_DECREF(self);
4420 return NULL;
4421 }
4422 Py_INCREF(bufr);
4423
4424 self->buf = bufr;
4425 self->start = start;
4426 self->end = end;
4427
4428 return (PyObject *)(self);
4429}
4430
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004431 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004432RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004433{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004434 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004435 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004436 PyObject_GC_Del((void *)(self));
4437}
4438
4439 static int
4440RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4441{
4442 Py_VISIT(((PyObject *)(self->buf)));
4443 return 0;
4444}
4445
4446 static int
4447RangeClear(RangeObject *self)
4448{
4449 Py_CLEAR(self->buf);
4450 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004451}
4452
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004453 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004454RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004455{
4456 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004457 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004458 return -1; /* ??? */
4459
Bram Moolenaard6e39182013-05-21 18:30:34 +02004460 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004461}
4462
4463 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004464RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004465{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004466 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004467}
4468
4469 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004470RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004471{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004472 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004473}
4474
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004475static char *RangeAttrs[] = {
4476 "start", "end",
4477 NULL
4478};
4479
4480 static PyObject *
4481RangeDir(PyObject *self)
4482{
4483 return ObjectDir(self, RangeAttrs);
4484}
4485
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004486 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004487RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004488{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004489 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004490}
4491
4492 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004493RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004494{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004495 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004496 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4497 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004498 else
4499 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004500 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004501
4502 if (name == NULL)
4503 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004504
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004505 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004506 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004507 }
4508}
4509
4510static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004511 /* name, function, calling, documentation */
4512 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004513 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4514 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004515};
4516
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004517static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004518static PySequenceMethods BufferAsSeq;
4519static PyMappingMethods BufferAsMapping;
4520
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004521 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004522BufferNew(buf_T *buf)
4523{
4524 /* We need to handle deletion of buffers underneath us.
4525 * If we add a "b_python*_ref" field to the buf_T structure,
4526 * then we can get at it in buf_freeall() in vim. We then
4527 * need to create only ONE Python object per buffer - if
4528 * we try to create a second, just INCREF the existing one
4529 * and return it. The (single) Python object referring to
4530 * the buffer is stored in "b_python*_ref".
4531 * Question: what to do on a buf_freeall(). We'll probably
4532 * have to either delete the Python object (DECREF it to
4533 * zero - a bad idea, as it leaves dangling refs!) or
4534 * set the buf_T * value to an invalid value (-1?), which
4535 * means we need checks in all access functions... Bah.
4536 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004537 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004538 * b_python_ref and b_python3_ref fields respectively.
4539 */
4540
4541 BufferObject *self;
4542
4543 if (BUF_PYTHON_REF(buf) != NULL)
4544 {
4545 self = BUF_PYTHON_REF(buf);
4546 Py_INCREF(self);
4547 }
4548 else
4549 {
4550 self = PyObject_NEW(BufferObject, &BufferType);
4551 if (self == NULL)
4552 return NULL;
4553 self->buf = buf;
4554 BUF_PYTHON_REF(buf) = self;
4555 }
4556
4557 return (PyObject *)(self);
4558}
4559
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004560 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004561BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004562{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004563 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4564 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004565
4566 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004567}
4568
Bram Moolenaar971db462013-05-12 18:44:48 +02004569 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004570BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004571{
4572 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004573 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004574 return -1; /* ??? */
4575
Bram Moolenaard6e39182013-05-21 18:30:34 +02004576 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004577}
4578
4579 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004580BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004581{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004582 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004583}
4584
4585 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004586BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004587{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004588 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004589}
4590
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004591static char *BufferAttrs[] = {
4592 "name", "number", "vars", "options", "valid",
4593 NULL
4594};
4595
4596 static PyObject *
4597BufferDir(PyObject *self)
4598{
4599 return ObjectDir(self, BufferAttrs);
4600}
4601
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004602 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004603BufferAttrValid(BufferObject *self, char *name)
4604{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004605 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004606
4607 if (strcmp(name, "valid") != 0)
4608 return NULL;
4609
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004610 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4611 Py_INCREF(ret);
4612 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004613}
4614
4615 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004616BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004617{
4618 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004619 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004620 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004621 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004622 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004623 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004624 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004625 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004626 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4627 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004628 else if (strcmp(name, "__members__") == 0)
4629 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004630 else
4631 return NULL;
4632}
4633
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004634 static int
4635BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4636{
4637 if (CheckBuffer(self))
4638 return -1;
4639
4640 if (strcmp(name, "name") == 0)
4641 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004642 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004643 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004644 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004645 PyObject *todecref;
4646
4647 if (!(val = StringToChars(valObject, &todecref)))
4648 return -1;
4649
4650 VimTryStart();
4651 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4652 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004653 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004654 aucmd_restbuf(&aco);
4655 Py_XDECREF(todecref);
4656 if (VimTryEnd())
4657 return -1;
4658
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004659 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004660 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004661 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004662 return -1;
4663 }
4664 return 0;
4665 }
4666 else
4667 {
4668 PyErr_SetString(PyExc_AttributeError, name);
4669 return -1;
4670 }
4671}
4672
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004673 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004674BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004676 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677}
4678
4679 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004680BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004681{
4682 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004683 char_u *pmark;
4684 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004685 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004686 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004687
Bram Moolenaard6e39182013-05-21 18:30:34 +02004688 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004689 return NULL;
4690
Bram Moolenaar389a1792013-06-23 13:00:44 +02004691 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004692 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693
Bram Moolenaar389a1792013-06-23 13:00:44 +02004694 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004695 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004696 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004697 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004698 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004699 return NULL;
4700 }
4701
4702 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004703
4704 Py_XDECREF(todecref);
4705
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004706 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004707 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004709 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004710 if (VimTryEnd())
4711 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712
4713 if (posp == NULL)
4714 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004715 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004716 return NULL;
4717 }
4718
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004719 if (posp->lnum <= 0)
4720 {
4721 /* Or raise an error? */
4722 Py_INCREF(Py_None);
4723 return Py_None;
4724 }
4725
4726 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4727}
4728
4729 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004730BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731{
4732 PyInt start;
4733 PyInt end;
4734
Bram Moolenaard6e39182013-05-21 18:30:34 +02004735 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004736 return NULL;
4737
4738 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4739 return NULL;
4740
Bram Moolenaard6e39182013-05-21 18:30:34 +02004741 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742}
4743
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004744 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004745BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004746{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004747 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004748 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004749 else
4750 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004751 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004752
4753 if (name == NULL)
4754 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004755
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004756 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004757 }
4758}
4759
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004760static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004761 /* name, function, calling, documentation */
4762 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004763 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004764 {"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 +02004765 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4766 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004767};
4768
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004769/*
4770 * Buffer list object - Implementation
4771 */
4772
4773static PyTypeObject BufMapType;
4774
4775typedef struct
4776{
4777 PyObject_HEAD
4778} BufMapObject;
4779
4780 static PyInt
4781BufMapLength(PyObject *self UNUSED)
4782{
4783 buf_T *b = firstbuf;
4784 PyInt n = 0;
4785
4786 while (b)
4787 {
4788 ++n;
4789 b = b->b_next;
4790 }
4791
4792 return n;
4793}
4794
4795 static PyObject *
4796BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4797{
4798 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004799 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004800
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004801 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004802 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004803
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004804 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004805
4806 if (b)
4807 return BufferNew(b);
4808 else
4809 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004810 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004811 return NULL;
4812 }
4813}
4814
4815 static void
4816BufMapIterDestruct(PyObject *buffer)
4817{
4818 /* Iteration was stopped before all buffers were processed */
4819 if (buffer)
4820 {
4821 Py_DECREF(buffer);
4822 }
4823}
4824
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004825 static int
4826BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4827{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004828 if (buffer)
4829 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004830 return 0;
4831}
4832
4833 static int
4834BufMapIterClear(PyObject **buffer)
4835{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004836 if (*buffer)
4837 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004838 return 0;
4839}
4840
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004841 static PyObject *
4842BufMapIterNext(PyObject **buffer)
4843{
4844 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004845 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004846
4847 if (!*buffer)
4848 return NULL;
4849
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004850 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004851
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004852 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004853 {
4854 *buffer = NULL;
4855 return NULL;
4856 }
4857
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004858 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004859 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004860 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004861 return NULL;
4862 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004863 /* Do not increment reference: we no longer hold it (decref), but whoever
4864 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004865 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004866}
4867
4868 static PyObject *
4869BufMapIter(PyObject *self UNUSED)
4870{
4871 PyObject *buffer;
4872
4873 buffer = BufferNew(firstbuf);
4874 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004875 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4876 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004877}
4878
4879static PyMappingMethods BufMapAsMapping = {
4880 (lenfunc) BufMapLength,
4881 (binaryfunc) BufMapItem,
4882 (objobjargproc) 0,
4883};
4884
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004885/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004886 */
4887
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004888static char *CurrentAttrs[] = {
4889 "buffer", "window", "line", "range", "tabpage",
4890 NULL
4891};
4892
4893 static PyObject *
4894CurrentDir(PyObject *self)
4895{
4896 return ObjectDir(self, CurrentAttrs);
4897}
4898
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004899 static PyObject *
4900CurrentGetattr(PyObject *self UNUSED, char *name)
4901{
4902 if (strcmp(name, "buffer") == 0)
4903 return (PyObject *)BufferNew(curbuf);
4904 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004905 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004906 else if (strcmp(name, "tabpage") == 0)
4907 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004908 else if (strcmp(name, "line") == 0)
4909 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4910 else if (strcmp(name, "range") == 0)
4911 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004912 else if (strcmp(name, "__members__") == 0)
4913 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004914 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004915#if PY_MAJOR_VERSION < 3
4916 return Py_FindMethod(WindowMethods, self, name);
4917#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004918 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004919#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004920}
4921
4922 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004923CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004924{
4925 if (strcmp(name, "line") == 0)
4926 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004927 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4928 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004929 return -1;
4930
4931 return 0;
4932 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004933 else if (strcmp(name, "buffer") == 0)
4934 {
4935 int count;
4936
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004937 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004938 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004939 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004940 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004941 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004942 return -1;
4943 }
4944
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004945 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004946 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004947 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004948
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004949 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004950 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4951 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004952 if (VimTryEnd())
4953 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004954 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004955 return -1;
4956 }
4957
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004958 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004959 }
4960 else if (strcmp(name, "window") == 0)
4961 {
4962 int count;
4963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004964 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004965 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004966 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004967 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004968 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004969 return -1;
4970 }
4971
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004972 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004973 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004974 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004975
4976 if (!count)
4977 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004978 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004979 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004980 return -1;
4981 }
4982
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004983 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004984 win_goto(((WindowObject *)(valObject))->win);
4985 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004986 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004987 if (VimTryEnd())
4988 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004989 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004990 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004991 return -1;
4992 }
4993
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004994 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004995 }
4996 else if (strcmp(name, "tabpage") == 0)
4997 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004998 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004999 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005000 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005001 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005002 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005003 return -1;
5004 }
5005
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005006 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005007 return -1;
5008
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005009 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005010 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5011 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005012 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005013 if (VimTryEnd())
5014 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005015 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005016 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005017 return -1;
5018 }
5019
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005020 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005021 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005022 else
5023 {
5024 PyErr_SetString(PyExc_AttributeError, name);
5025 return -1;
5026 }
5027}
5028
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005029static struct PyMethodDef CurrentMethods[] = {
5030 /* name, function, calling, documentation */
5031 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5032 { NULL, NULL, 0, NULL}
5033};
5034
Bram Moolenaardb913952012-06-29 12:54:53 +02005035 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005036init_range_cmd(exarg_T *eap)
5037{
5038 RangeStart = eap->line1;
5039 RangeEnd = eap->line2;
5040}
5041
5042 static void
5043init_range_eval(typval_T *rettv UNUSED)
5044{
5045 RangeStart = (PyInt) curwin->w_cursor.lnum;
5046 RangeEnd = RangeStart;
5047}
5048
5049 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005050run_cmd(const char *cmd, void *arg UNUSED
5051#ifdef PY_CAN_RECURSE
5052 , PyGILState_STATE *pygilstate UNUSED
5053#endif
5054 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005055{
Bram Moolenaar41009372013-07-01 22:03:04 +02005056 PyObject *run_ret;
5057 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5058 if (run_ret != NULL)
5059 {
5060 Py_DECREF(run_ret);
5061 }
5062 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5063 {
5064 EMSG2(_(e_py_systemexit), "python");
5065 PyErr_Clear();
5066 }
5067 else
5068 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005069}
5070
5071static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5072static int code_hdr_len = 30;
5073
5074 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005075run_do(const char *cmd, void *arg UNUSED
5076#ifdef PY_CAN_RECURSE
5077 , PyGILState_STATE *pygilstate
5078#endif
5079 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005080{
5081 PyInt lnum;
5082 size_t len;
5083 char *code;
5084 int status;
5085 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005086 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005087
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005088 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005089 {
5090 EMSG(_("cannot save undo information"));
5091 return;
5092 }
5093
5094 len = code_hdr_len + STRLEN(cmd);
5095 code = PyMem_New(char, len + 1);
5096 memcpy(code, code_hdr, code_hdr_len);
5097 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005098 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5099 status = -1;
5100 if (run_ret != NULL)
5101 {
5102 status = 0;
5103 Py_DECREF(run_ret);
5104 }
5105 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5106 {
5107 PyMem_Free(code);
5108 EMSG2(_(e_py_systemexit), "python");
5109 PyErr_Clear();
5110 return;
5111 }
5112 else
5113 PyErr_PrintEx(1);
5114
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005115 PyMem_Free(code);
5116
5117 if (status)
5118 {
5119 EMSG(_("failed to run the code"));
5120 return;
5121 }
5122
5123 status = 0;
5124 pymain = PyImport_AddModule("__main__");
5125 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005126#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005127 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005128#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005129
5130 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5131 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005132 PyObject *line;
5133 PyObject *linenr;
5134 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005135
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005136#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005137 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005138#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005139 if (!(line = GetBufferLine(curbuf, lnum)))
5140 goto err;
5141 if (!(linenr = PyInt_FromLong((long) lnum)))
5142 {
5143 Py_DECREF(line);
5144 goto err;
5145 }
5146 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5147 Py_DECREF(line);
5148 Py_DECREF(linenr);
5149 if (!ret)
5150 goto err;
5151
5152 if (ret != Py_None)
5153 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5154 goto err;
5155
5156 Py_XDECREF(ret);
5157 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005158#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005159 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005160#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005161 }
5162 goto out;
5163err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005164#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005165 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005166#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005167 PyErr_PrintEx(0);
5168 PythonIO_Flush();
5169 status = 1;
5170out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005171#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005172 if (!status)
5173 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005174#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005175 Py_DECREF(pyfunc);
5176 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5177 if (status)
5178 return;
5179 check_cursor();
5180 update_curbuf(NOT_VALID);
5181}
5182
5183 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005184run_eval(const char *cmd, typval_T *rettv
5185#ifdef PY_CAN_RECURSE
5186 , PyGILState_STATE *pygilstate UNUSED
5187#endif
5188 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005189{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005190 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005191
Bram Moolenaar41009372013-07-01 22:03:04 +02005192 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005193 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005194 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005195 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005196 {
5197 EMSG2(_(e_py_systemexit), "python");
5198 PyErr_Clear();
5199 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005200 else
5201 {
5202 if (PyErr_Occurred() && !msg_silent)
5203 PyErr_PrintEx(0);
5204 EMSG(_("E858: Eval did not return a valid python object"));
5205 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005206 }
5207 else
5208 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005209 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005210 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005211 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005212 }
5213 PyErr_Clear();
5214}
5215
5216 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005217set_ref_in_py(const int copyID)
5218{
5219 pylinkedlist_T *cur;
5220 dict_T *dd;
5221 list_T *ll;
5222
5223 if (lastdict != NULL)
5224 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5225 {
5226 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5227 if (dd->dv_copyID != copyID)
5228 {
5229 dd->dv_copyID = copyID;
5230 set_ref_in_ht(&dd->dv_hashtab, copyID);
5231 }
5232 }
5233
5234 if (lastlist != NULL)
5235 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5236 {
5237 ll = ((ListObject *) (cur->pll_obj))->list;
5238 if (ll->lv_copyID != copyID)
5239 {
5240 ll->lv_copyID = copyID;
5241 set_ref_in_list(ll, copyID);
5242 }
5243 }
5244}
5245
5246 static int
5247set_string_copy(char_u *str, typval_T *tv)
5248{
5249 tv->vval.v_string = vim_strsave(str);
5250 if (tv->vval.v_string == NULL)
5251 {
5252 PyErr_NoMemory();
5253 return -1;
5254 }
5255 return 0;
5256}
5257
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005259pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005260{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005261 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005262 char_u *key;
5263 dictitem_T *di;
5264 PyObject *keyObject;
5265 PyObject *valObject;
5266 Py_ssize_t iter = 0;
5267
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005268 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005269 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005270
5271 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005272 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005273
5274 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5275 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005276 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005277
Bram Moolenaara03e6312013-05-29 22:49:26 +02005278 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005279 {
5280 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005281 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005282 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005283
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005284 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005285 {
5286 dict_unref(dict);
5287 return -1;
5288 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005289
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005290 if (*key == NUL)
5291 {
5292 dict_unref(dict);
5293 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005294 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005295 return -1;
5296 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005297
5298 di = dictitem_alloc(key);
5299
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005300 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005301
5302 if (di == NULL)
5303 {
5304 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005305 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005306 return -1;
5307 }
5308 di->di_tv.v_lock = 0;
5309
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005310 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005311 {
5312 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005313 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005314 return -1;
5315 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005316
5317 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005318 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005319 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005320 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005321 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005322 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005323 return -1;
5324 }
5325 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005326
5327 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005328 return 0;
5329}
5330
5331 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005332pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005333{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005334 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005335 char_u *key;
5336 dictitem_T *di;
5337 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005338 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005339 PyObject *keyObject;
5340 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005341
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005342 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005343 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005344
5345 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005346 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005347
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005348 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005349 {
5350 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005351 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005352 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005353
5354 if (!(iterator = PyObject_GetIter(list)))
5355 {
5356 dict_unref(dict);
5357 Py_DECREF(list);
5358 return -1;
5359 }
5360 Py_DECREF(list);
5361
5362 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005363 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005364 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005365
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005366 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005367 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005368 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005369 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005370 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005371 return -1;
5372 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005373
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005374 if (*key == NUL)
5375 {
5376 Py_DECREF(keyObject);
5377 Py_DECREF(iterator);
5378 Py_XDECREF(todecref);
5379 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005380 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005381 return -1;
5382 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005383
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005384 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005385 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005386 Py_DECREF(keyObject);
5387 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005388 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005389 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005390 return -1;
5391 }
5392
5393 di = dictitem_alloc(key);
5394
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005395 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005396 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005397
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005398 if (di == NULL)
5399 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005400 Py_DECREF(iterator);
5401 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005402 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005403 PyErr_NoMemory();
5404 return -1;
5405 }
5406 di->di_tv.v_lock = 0;
5407
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005408 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005409 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005410 Py_DECREF(iterator);
5411 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005412 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005413 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005414 return -1;
5415 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005416
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005417 Py_DECREF(valObject);
5418
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005419 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005420 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005421 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005422 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005423 dictitem_free(di);
5424 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005425 return -1;
5426 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005427 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005428 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005429 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005430 return 0;
5431}
5432
5433 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005434pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005435{
5436 list_T *l;
5437
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005438 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005439 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005440
5441 tv->v_type = VAR_LIST;
5442 tv->vval.v_list = l;
5443
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005444 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005445 {
5446 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005447 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005448 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005449
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005450 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005451 return 0;
5452}
5453
Bram Moolenaardb913952012-06-29 12:54:53 +02005454typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5455
5456 static int
5457convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005458 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005459{
5460 PyObject *capsule;
5461 char hexBuf[sizeof(void *) * 2 + 3];
5462
5463 sprintf(hexBuf, "%p", obj);
5464
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005465# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005466 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005467# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005468 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005469# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005470 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005471 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005472# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005473 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005474# else
5475 capsule = PyCObject_FromVoidPtr(tv, NULL);
5476# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005477 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5478 {
5479 Py_DECREF(capsule);
5480 tv->v_type = VAR_UNKNOWN;
5481 return -1;
5482 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005483
5484 Py_DECREF(capsule);
5485
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005486 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005487 {
5488 tv->v_type = VAR_UNKNOWN;
5489 return -1;
5490 }
5491 /* As we are not using copy_tv which increments reference count we must
5492 * do it ourself. */
5493 switch(tv->v_type)
5494 {
5495 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5496 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5497 }
5498 }
5499 else
5500 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005501 typval_T *v;
5502
5503# ifdef PY_USE_CAPSULE
5504 v = PyCapsule_GetPointer(capsule, NULL);
5505# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005506 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005507# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005508 copy_tv(v, tv);
5509 }
5510 return 0;
5511}
5512
5513 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005514ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5515{
5516 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005517 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005518
5519 if (!(lookup_dict = PyDict_New()))
5520 return -1;
5521
5522 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5523 {
5524 tv->v_type = VAR_DICT;
5525 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5526 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005527 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005528 }
5529 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005530 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005531 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005532 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005533 else
5534 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005535 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005536 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005537 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005538 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005539 }
5540 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005541 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005542}
5543
5544 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005545ConvertFromPyObject(PyObject *obj, typval_T *tv)
5546{
5547 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005548 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005549
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005550 if (!(lookup_dict = PyDict_New()))
5551 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005552 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005553 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005554 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005555}
5556
5557 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005558_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005559{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005560 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005561 {
5562 tv->v_type = VAR_DICT;
5563 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5564 ++tv->vval.v_dict->dv_refcount;
5565 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005566 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005567 {
5568 tv->v_type = VAR_LIST;
5569 tv->vval.v_list = (((ListObject *)(obj))->list);
5570 ++tv->vval.v_list->lv_refcount;
5571 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005572 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005573 {
5574 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5575 return -1;
5576
5577 tv->v_type = VAR_FUNC;
5578 func_ref(tv->vval.v_string);
5579 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005580 else if (PyBytes_Check(obj))
5581 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005582 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005583
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005584 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005585 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005586 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005587 return -1;
5588
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005589 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005590 return -1;
5591
5592 tv->v_type = VAR_STRING;
5593 }
5594 else if (PyUnicode_Check(obj))
5595 {
5596 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005597 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005598
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005599 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005600 if (bytes == NULL)
5601 return -1;
5602
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005603 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005604 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005606 return -1;
5607
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005608 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005609 {
5610 Py_XDECREF(bytes);
5611 return -1;
5612 }
5613 Py_XDECREF(bytes);
5614
5615 tv->v_type = VAR_STRING;
5616 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005617#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005618 else if (PyInt_Check(obj))
5619 {
5620 tv->v_type = VAR_NUMBER;
5621 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005622 if (PyErr_Occurred())
5623 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005624 }
5625#endif
5626 else if (PyLong_Check(obj))
5627 {
5628 tv->v_type = VAR_NUMBER;
5629 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005630 if (PyErr_Occurred())
5631 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005632 }
5633 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005634 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005635#ifdef FEAT_FLOAT
5636 else if (PyFloat_Check(obj))
5637 {
5638 tv->v_type = VAR_FLOAT;
5639 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5640 }
5641#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005642 else if (PyObject_HasAttrString(obj, "keys"))
5643 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005644 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005645 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005646 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005647 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005648 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005649 else if (PyNumber_Check(obj))
5650 {
5651 PyObject *num;
5652
5653 if (!(num = PyNumber_Long(obj)))
5654 return -1;
5655
5656 tv->v_type = VAR_NUMBER;
5657 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5658
5659 Py_DECREF(num);
5660 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005661 else
5662 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005663 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005664 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005665 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005666 return -1;
5667 }
5668 return 0;
5669}
5670
5671 static PyObject *
5672ConvertToPyObject(typval_T *tv)
5673{
5674 if (tv == NULL)
5675 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005676 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005677 return NULL;
5678 }
5679 switch (tv->v_type)
5680 {
5681 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005682 return PyBytes_FromString(tv->vval.v_string == NULL
5683 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005684 case VAR_NUMBER:
5685 return PyLong_FromLong((long) tv->vval.v_number);
5686#ifdef FEAT_FLOAT
5687 case VAR_FLOAT:
5688 return PyFloat_FromDouble((double) tv->vval.v_float);
5689#endif
5690 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005691 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005692 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005693 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005694 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005695 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005696 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005697 case VAR_UNKNOWN:
5698 Py_INCREF(Py_None);
5699 return Py_None;
5700 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005701 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005702 return NULL;
5703 }
5704}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005705
5706typedef struct
5707{
5708 PyObject_HEAD
5709} CurrentObject;
5710static PyTypeObject CurrentType;
5711
5712 static void
5713init_structs(void)
5714{
5715 vim_memset(&OutputType, 0, sizeof(OutputType));
5716 OutputType.tp_name = "vim.message";
5717 OutputType.tp_basicsize = sizeof(OutputObject);
5718 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5719 OutputType.tp_doc = "vim message object";
5720 OutputType.tp_methods = OutputMethods;
5721#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005722 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5723 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005724 OutputType.tp_alloc = call_PyType_GenericAlloc;
5725 OutputType.tp_new = call_PyType_GenericNew;
5726 OutputType.tp_free = call_PyObject_Free;
5727#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005728 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5729 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005730#endif
5731
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005732 vim_memset(&IterType, 0, sizeof(IterType));
5733 IterType.tp_name = "vim.iter";
5734 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005735 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005736 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005737 IterType.tp_iter = (getiterfunc)IterIter;
5738 IterType.tp_iternext = (iternextfunc)IterNext;
5739 IterType.tp_dealloc = (destructor)IterDestructor;
5740 IterType.tp_traverse = (traverseproc)IterTraverse;
5741 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005742
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005743 vim_memset(&BufferType, 0, sizeof(BufferType));
5744 BufferType.tp_name = "vim.buffer";
5745 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005746 BufferType.tp_dealloc = (destructor)BufferDestructor;
5747 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005748 BufferType.tp_as_sequence = &BufferAsSeq;
5749 BufferType.tp_as_mapping = &BufferAsMapping;
5750 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5751 BufferType.tp_doc = "vim buffer object";
5752 BufferType.tp_methods = BufferMethods;
5753#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005754 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005755 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005756 BufferType.tp_alloc = call_PyType_GenericAlloc;
5757 BufferType.tp_new = call_PyType_GenericNew;
5758 BufferType.tp_free = call_PyObject_Free;
5759#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005760 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005761 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005762#endif
5763
5764 vim_memset(&WindowType, 0, sizeof(WindowType));
5765 WindowType.tp_name = "vim.window";
5766 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005767 WindowType.tp_dealloc = (destructor)WindowDestructor;
5768 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005769 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005770 WindowType.tp_doc = "vim Window object";
5771 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005772 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5773 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005774#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005775 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5776 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005777 WindowType.tp_alloc = call_PyType_GenericAlloc;
5778 WindowType.tp_new = call_PyType_GenericNew;
5779 WindowType.tp_free = call_PyObject_Free;
5780#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005781 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5782 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005783#endif
5784
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005785 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5786 TabPageType.tp_name = "vim.tabpage";
5787 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005788 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5789 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005790 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5791 TabPageType.tp_doc = "vim tab page object";
5792 TabPageType.tp_methods = TabPageMethods;
5793#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005794 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005795 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5796 TabPageType.tp_new = call_PyType_GenericNew;
5797 TabPageType.tp_free = call_PyObject_Free;
5798#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005799 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005800#endif
5801
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005802 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5803 BufMapType.tp_name = "vim.bufferlist";
5804 BufMapType.tp_basicsize = sizeof(BufMapObject);
5805 BufMapType.tp_as_mapping = &BufMapAsMapping;
5806 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005807 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005808 BufferType.tp_doc = "vim buffer list";
5809
5810 vim_memset(&WinListType, 0, sizeof(WinListType));
5811 WinListType.tp_name = "vim.windowlist";
5812 WinListType.tp_basicsize = sizeof(WinListType);
5813 WinListType.tp_as_sequence = &WinListAsSeq;
5814 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5815 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005816 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005817
5818 vim_memset(&TabListType, 0, sizeof(TabListType));
5819 TabListType.tp_name = "vim.tabpagelist";
5820 TabListType.tp_basicsize = sizeof(TabListType);
5821 TabListType.tp_as_sequence = &TabListAsSeq;
5822 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5823 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005824
5825 vim_memset(&RangeType, 0, sizeof(RangeType));
5826 RangeType.tp_name = "vim.range";
5827 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005828 RangeType.tp_dealloc = (destructor)RangeDestructor;
5829 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005830 RangeType.tp_as_sequence = &RangeAsSeq;
5831 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005832 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005833 RangeType.tp_doc = "vim Range object";
5834 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005835 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5836 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005837#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005838 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005839 RangeType.tp_alloc = call_PyType_GenericAlloc;
5840 RangeType.tp_new = call_PyType_GenericNew;
5841 RangeType.tp_free = call_PyObject_Free;
5842#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005843 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005844#endif
5845
5846 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5847 CurrentType.tp_name = "vim.currentdata";
5848 CurrentType.tp_basicsize = sizeof(CurrentObject);
5849 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5850 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005851 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005852#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005853 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5854 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005855#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005856 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5857 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005858#endif
5859
5860 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5861 DictionaryType.tp_name = "vim.dictionary";
5862 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005863 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005864 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005865 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005866 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005867 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5868 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005869 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5870 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5871 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005872#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005873 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5874 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005875#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005876 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5877 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005878#endif
5879
5880 vim_memset(&ListType, 0, sizeof(ListType));
5881 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005882 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005883 ListType.tp_basicsize = sizeof(ListObject);
5884 ListType.tp_as_sequence = &ListAsSeq;
5885 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005886 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005887 ListType.tp_doc = "list pushing modifications to vim structure";
5888 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005889 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005890 ListType.tp_new = (newfunc)ListConstructor;
5891 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005892#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005893 ListType.tp_getattro = (getattrofunc)ListGetattro;
5894 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005895#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005896 ListType.tp_getattr = (getattrfunc)ListGetattr;
5897 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005898#endif
5899
5900 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005901 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005902 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005903 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5904 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005905 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005906 FunctionType.tp_doc = "object that calls vim function";
5907 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005908 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005909 FunctionType.tp_new = (newfunc)FunctionConstructor;
5910 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005911#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005912 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005913#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005914 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005915#endif
5916
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005917 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5918 OptionsType.tp_name = "vim.options";
5919 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005920 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005921 OptionsType.tp_doc = "object for manipulating options";
5922 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005923 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5924 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5925 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005926
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005927 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5928 LoaderType.tp_name = "vim.Loader";
5929 LoaderType.tp_basicsize = sizeof(LoaderObject);
5930 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5931 LoaderType.tp_doc = "vim message object";
5932 LoaderType.tp_methods = LoaderMethods;
5933 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5934
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005935#if PY_MAJOR_VERSION >= 3
5936 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5937 vimmodule.m_name = "vim";
5938 vimmodule.m_doc = "Vim Python interface\n";
5939 vimmodule.m_size = -1;
5940 vimmodule.m_methods = VimMethods;
5941#endif
5942}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005943
5944#define PYTYPE_READY(type) \
5945 if (PyType_Ready(&type)) \
5946 return -1;
5947
5948 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02005949init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005950{
5951 PYTYPE_READY(IterType);
5952 PYTYPE_READY(BufferType);
5953 PYTYPE_READY(RangeType);
5954 PYTYPE_READY(WindowType);
5955 PYTYPE_READY(TabPageType);
5956 PYTYPE_READY(BufMapType);
5957 PYTYPE_READY(WinListType);
5958 PYTYPE_READY(TabListType);
5959 PYTYPE_READY(CurrentType);
5960 PYTYPE_READY(DictionaryType);
5961 PYTYPE_READY(ListType);
5962 PYTYPE_READY(FunctionType);
5963 PYTYPE_READY(OptionsType);
5964 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005965 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005966 return 0;
5967}
5968
5969 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005970init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005971{
5972 PyObject *path;
5973 PyObject *path_hook;
5974 PyObject *path_hooks;
5975
5976 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5977 return -1;
5978
5979 if (!(path_hooks = PySys_GetObject("path_hooks")))
5980 {
5981 PyErr_Clear();
5982 path_hooks = PyList_New(1);
5983 PyList_SET_ITEM(path_hooks, 0, path_hook);
5984 if (PySys_SetObject("path_hooks", path_hooks))
5985 {
5986 Py_DECREF(path_hooks);
5987 return -1;
5988 }
5989 Py_DECREF(path_hooks);
5990 }
5991 else if (PyList_Check(path_hooks))
5992 {
5993 if (PyList_Append(path_hooks, path_hook))
5994 {
5995 Py_DECREF(path_hook);
5996 return -1;
5997 }
5998 Py_DECREF(path_hook);
5999 }
6000 else
6001 {
6002 VimTryStart();
6003 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6004 "You should now do the following:\n"
6005 "- append vim.path_hook to sys.path_hooks\n"
6006 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6007 VimTryEnd(); /* Discard the error */
6008 Py_DECREF(path_hook);
6009 return 0;
6010 }
6011
6012 if (!(path = PySys_GetObject("path")))
6013 {
6014 PyErr_Clear();
6015 path = PyList_New(1);
6016 Py_INCREF(vim_special_path_object);
6017 PyList_SET_ITEM(path, 0, vim_special_path_object);
6018 if (PySys_SetObject("path", path))
6019 {
6020 Py_DECREF(path);
6021 return -1;
6022 }
6023 Py_DECREF(path);
6024 }
6025 else if (PyList_Check(path))
6026 {
6027 if (PyList_Append(path, vim_special_path_object))
6028 return -1;
6029 }
6030 else
6031 {
6032 VimTryStart();
6033 EMSG(_("Failed to set path: sys.path is not a list\n"
6034 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6035 VimTryEnd(); /* Discard the error */
6036 }
6037
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006038 return 0;
6039}
6040
6041static BufMapObject TheBufferMap =
6042{
6043 PyObject_HEAD_INIT(&BufMapType)
6044};
6045
6046static WinListObject TheWindowList =
6047{
6048 PyObject_HEAD_INIT(&WinListType)
6049 NULL
6050};
6051
6052static CurrentObject TheCurrent =
6053{
6054 PyObject_HEAD_INIT(&CurrentType)
6055};
6056
6057static TabListObject TheTabPageList =
6058{
6059 PyObject_HEAD_INIT(&TabListType)
6060};
6061
6062static struct numeric_constant {
6063 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006064 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006065} numeric_constants[] = {
6066 {"VAR_LOCKED", VAR_LOCKED},
6067 {"VAR_FIXED", VAR_FIXED},
6068 {"VAR_SCOPE", VAR_SCOPE},
6069 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6070};
6071
6072static struct object_constant {
6073 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006074 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006075} object_constants[] = {
6076 {"buffers", (PyObject *)(void *)&TheBufferMap},
6077 {"windows", (PyObject *)(void *)&TheWindowList},
6078 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6079 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006080
6081 {"Buffer", (PyObject *)&BufferType},
6082 {"Range", (PyObject *)&RangeType},
6083 {"Window", (PyObject *)&WindowType},
6084 {"TabPage", (PyObject *)&TabPageType},
6085 {"Dictionary", (PyObject *)&DictionaryType},
6086 {"List", (PyObject *)&ListType},
6087 {"Function", (PyObject *)&FunctionType},
6088 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006089 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006090};
6091
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006092#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006093 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006094 return -1;
6095
6096#define ADD_CHECKED_OBJECT(m, name, obj) \
6097 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006098 PyObject *valObject = obj; \
6099 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006100 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006101 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006102 }
6103
6104 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006105populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006106{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006107 int i;
6108 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006109 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006110 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006111
6112 for (i = 0; i < (int)(sizeof(numeric_constants)
6113 / sizeof(struct numeric_constant));
6114 ++i)
6115 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006116 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006117
6118 for (i = 0; i < (int)(sizeof(object_constants)
6119 / sizeof(struct object_constant));
6120 ++i)
6121 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006122 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006123
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006124 valObject = object_constants[i].valObject;
6125 Py_INCREF(valObject);
6126 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006127 }
6128
6129 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6130 return -1;
6131 ADD_OBJECT(m, "error", VimError);
6132
Bram Moolenaara9922d62013-05-30 13:01:18 +02006133 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6134 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006135 ADD_CHECKED_OBJECT(m, "options",
6136 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006137
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006138 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006139 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006140 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006141
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006142 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006143 return -1;
6144 ADD_OBJECT(m, "_getcwd", py_getcwd)
6145
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006146 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006147 return -1;
6148 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006149 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006150 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006151 if (PyObject_SetAttrString(other_module, "chdir", attr))
6152 {
6153 Py_DECREF(attr);
6154 return -1;
6155 }
6156 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006157
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006158 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006159 {
6160 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006161 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006162 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006163 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6164 {
6165 Py_DECREF(attr);
6166 return -1;
6167 }
6168 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006169 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006170 else
6171 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006172
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006173 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6174 return -1;
6175
6176 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6177
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006178 if (!(imp = PyImport_ImportModule("imp")))
6179 return -1;
6180
6181 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6182 {
6183 Py_DECREF(imp);
6184 return -1;
6185 }
6186
6187 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6188 {
6189 Py_DECREF(py_find_module);
6190 Py_DECREF(imp);
6191 return -1;
6192 }
6193
6194 Py_DECREF(imp);
6195
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006196 ADD_OBJECT(m, "_find_module", py_find_module);
6197 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006198
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006199 return 0;
6200}