blob: a0902b21761edee9d1823f596eb014e5cea62a18 [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 Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020075static PyObject *WindowNew(win_T *, tabpage_T *);
76static PyObject *BufferNew (buf_T *);
77static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020078
79static PyInt RangeStart;
80static PyInt RangeEnd;
81
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020082static PyObject *globals;
83
Bram Moolenaarf4258302013-06-02 18:20:17 +020084static PyObject *py_chdir;
85static PyObject *py_fchdir;
86static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020087static PyObject *vim_module;
88static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020089
Bram Moolenaar81c40c52013-06-12 14:41:04 +020090static PyObject *py_find_module;
91static PyObject *py_load_module;
92
93static PyObject *VimError;
94
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020095/*
96 * obtain a lock on the Vim data structures
97 */
98 static void
99Python_Lock_Vim(void)
100{
101}
102
103/*
104 * release a lock on the Vim data structures
105 */
106 static void
107Python_Release_Vim(void)
108{
109}
110
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200111/*
112 * The "todecref" argument holds a pointer to PyObject * that must be
113 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
114 * was needed to generate returned value is object.
115 *
116 * Use Py_XDECREF to decrement reference count.
117 */
118 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200119StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200121 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200124 {
125
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
127 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 return NULL;
129
130 *todecref = NULL;
131 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200132 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200134 PyObject *bytes;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137 return NULL;
138
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200139 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
140 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 {
142 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200143 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200144 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200145
146 *todecref = bytes;
147 }
148 else
149 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200151 PyErr_FORMAT(PyExc_TypeError,
152 N_("expected str() or unicode() instance, but got %s"),
153 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200154#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200155 PyErr_FORMAT(PyExc_TypeError,
156 N_("expected bytes() or str() instance, but got %s"),
157 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200158#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200159 return NULL;
160 }
161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200162 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200163}
164
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200165#define NUMBER_LONG 1
166#define NUMBER_INT 2
167#define NUMBER_NATURAL 4
168#define NUMBER_UNSIGNED 8
169
170 static int
171NumberToLong(PyObject *obj, long *result, int flags)
172{
173#if PY_MAJOR_VERSION < 3
174 if (PyInt_Check(obj))
175 {
176 *result = PyInt_AsLong(obj);
177 if (PyErr_Occurred())
178 return -1;
179 }
180 else
181#endif
182 if (PyLong_Check(obj))
183 {
184 *result = PyLong_AsLong(obj);
185 if (PyErr_Occurred())
186 return -1;
187 }
188 else if (PyNumber_Check(obj))
189 {
190 PyObject *num;
191
192 if (!(num = PyNumber_Long(obj)))
193 return -1;
194
195 *result = PyLong_AsLong(num);
196
197 Py_DECREF(num);
198
199 if (PyErr_Occurred())
200 return -1;
201 }
202 else
203 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200204#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200205 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200206 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 "coercing to long(), but got %s"),
208 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200212 "but got %s"),
213 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215 return -1;
216 }
217
218 if (flags & NUMBER_INT)
219 {
220 if (*result > INT_MAX)
221 {
222 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200223 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200224 return -1;
225 }
226 else if (*result < INT_MIN)
227 {
228 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200229 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200230 return -1;
231 }
232 }
233
234 if (flags & NUMBER_NATURAL)
235 {
236 if (*result <= 0)
237 {
238 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200239 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200240 return -1;
241 }
242 }
243 else if (flags & NUMBER_UNSIGNED)
244 {
245 if (*result < 0)
246 {
247 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200248 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200249 return -1;
250 }
251 }
252
253 return 0;
254}
255
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200256 static int
257add_string(PyObject *list, char *s)
258{
259 PyObject *string;
260
261 if (!(string = PyString_FromString(s)))
262 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200263
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200264 if (PyList_Append(list, string))
265 {
266 Py_DECREF(string);
267 return -1;
268 }
269
270 Py_DECREF(string);
271 return 0;
272}
273
274 static PyObject *
275ObjectDir(PyObject *self, char **attributes)
276{
277 PyMethodDef *method;
278 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200279 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200281 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200282 return NULL;
283
284 if (self)
285 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200286 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200288 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200289 return NULL;
290 }
291
292 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200295 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200296 return NULL;
297 }
298
299#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200302 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200303 return NULL;
304 }
305#endif
306
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308}
309
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200310/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 */
312
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200313/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315
316static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317
318typedef struct
319{
320 PyObject_HEAD
321 long softspace;
322 long error;
323} OutputObject;
324
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200325static char *OutputAttrs[] = {
326 "softspace",
327 NULL
328};
329
330 static PyObject *
331OutputDir(PyObject *self)
332{
333 return ObjectDir(self, OutputAttrs);
334}
335
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200337OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200339 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200341 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200342 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200343 return -1;
344 }
345
346 if (strcmp(name, "softspace") == 0)
347 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200348 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return 0;
351 }
352
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200353 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
355}
356
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200357/* Buffer IO, we write one whole line at a time. */
358static garray_T io_ga = {0, 0, 1, 80, NULL};
359static writefn old_fn = NULL;
360
361 static void
362PythonIO_Flush(void)
363{
364 if (old_fn != NULL && io_ga.ga_len > 0)
365 {
366 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
367 old_fn((char_u *)io_ga.ga_data);
368 }
369 io_ga.ga_len = 0;
370}
371
372 static void
373writer(writefn fn, char_u *str, PyInt n)
374{
375 char_u *ptr;
376
377 /* Flush when switching output function. */
378 if (fn != old_fn)
379 PythonIO_Flush();
380 old_fn = fn;
381
382 /* Write each NL separated line. Text after the last NL is kept for
383 * writing later. */
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
399 /* Put the remaining text into io_ga for later printing. */
400 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
401 {
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 io_ga.ga_len += (int)n;
404 }
405}
406
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200407 static int
408write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 Py_ssize_t len = 0;
411 char *str = NULL;
412 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200413
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200416
417 Py_BEGIN_ALLOW_THREADS
418 Python_Lock_Vim();
419 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
420 Python_Release_Vim();
421 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200422 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200424 return 0;
425}
426
427 static PyObject *
428OutputWrite(OutputObject *self, PyObject *string)
429{
430 if (write_output(self, string))
431 return NULL;
432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433 Py_INCREF(Py_None);
434 return Py_None;
435}
436
437 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200438OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200440 PyObject *iterator;
441 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200448 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200450 Py_DECREF(iterator);
451 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 return NULL;
453 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 }
456
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(iterator);
458
459 /* Iterator may have finished due to an exception */
460 if (PyErr_Occurred())
461 return NULL;
462
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463 Py_INCREF(Py_None);
464 return Py_None;
465}
466
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100467 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200468OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100469{
470 /* do nothing */
471 Py_INCREF(Py_None);
472 return Py_None;
473}
474
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200475/***************/
476
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200477static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200478 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200479 {"write", (PyCFunction)OutputWrite, METH_O, ""},
480 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200481 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200482 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200483 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200484};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200485
486static OutputObject Output =
487{
488 PyObject_HEAD_INIT(&OutputType)
489 0,
490 0
491};
492
493static OutputObject Error =
494{
495 PyObject_HEAD_INIT(&OutputType)
496 0,
497 1
498};
499
500 static int
501PythonIO_Init_io(void)
502{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200503 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
504 return -1;
505 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
506 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200507
508 if (PyErr_Occurred())
509 {
510 EMSG(_("E264: Python: Error initialising I/O objects"));
511 return -1;
512 }
513
514 return 0;
515}
516
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200517typedef struct
518{
519 PyObject_HEAD
520 PyObject *module;
521} LoaderObject;
522static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200523
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200524 static void
525LoaderDestructor(LoaderObject *self)
526{
527 Py_DECREF(self->module);
528 DESTRUCTOR_FINISH(self);
529}
530
531 static PyObject *
532LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
533{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200534 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200536 Py_INCREF(ret);
537 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200538}
539
540static struct PyMethodDef LoaderMethods[] = {
541 /* name, function, calling, doc */
542 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
543 { NULL, NULL, 0, NULL}
544};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200545
546/* Check to see whether a Vim error has been reported, or a keyboard
547 * interrupt has been detected.
548 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200549
550 static void
551VimTryStart(void)
552{
553 ++trylevel;
554}
555
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200557VimTryEnd(void)
558{
559 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200560 /* Without this it stops processing all subsequent VimL commands and
561 * generates strange error messages if I e.g. try calling Test() in a
562 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563 did_emsg = FALSE;
564 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200565 if (got_int)
566 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100567 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100568 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100569 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200570 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200571 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100573 else if (msg_list != NULL && *msg_list != NULL)
574 {
575 int should_free;
576 char_u *msg;
577
578 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
579
580 if (msg == NULL)
581 {
582 PyErr_NoMemory();
583 return -1;
584 }
585
586 PyErr_SetVim((char *) msg);
587
588 free_global_msglist();
589
590 if (should_free)
591 vim_free(msg);
592
593 return -1;
594 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200596 return (PyErr_Occurred() ? -1 : 0);
597 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200598 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200599 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100600 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200601 return -1;
602 }
603 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200604 else
605 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200606 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200607 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200608 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 }
610}
611
612 static int
613VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200614{
615 if (got_int)
616 {
617 PyErr_SetNone(PyExc_KeyboardInterrupt);
618 return 1;
619 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200620 return 0;
621}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200622
623/* Vim module - Implementation
624 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200627VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200629 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200630 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200631 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632
Bram Moolenaar389a1792013-06-23 13:00:44 +0200633 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200634 return NULL;
635
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200636 Py_BEGIN_ALLOW_THREADS
637 Python_Lock_Vim();
638
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200639 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200640 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200641 update_screen(VALID);
642
643 Python_Release_Vim();
644 Py_END_ALLOW_THREADS
645
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200646 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200647 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200648 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200649 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200651 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200653 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654}
655
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656/*
657 * Function to translate a typval_T into a PyObject; this will recursively
658 * translate lists/dictionaries into their Python equivalents.
659 *
660 * The depth parameter is to avoid infinite recursion, set it to 1 when
661 * you call VimToPython.
662 */
663 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200664VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200666 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200667 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200668 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200669
670 /* Avoid infinite recursion */
671 if (depth > 100)
672 {
673 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 ret = Py_None;
675 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200676 }
677
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200678 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679 * then and we can use it again. */
680 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
681 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
682 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200683 sprintf(ptrBuf, "%p",
684 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
685 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200686
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200687 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 Py_INCREF(ret);
690 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 }
692 }
693
694 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200695 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200696 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 else if (our_tv->v_type == VAR_NUMBER)
698 {
699 char buf[NUMBUFLEN];
700
701 /* For backwards compatibility numbers are stored as strings. */
702 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200703 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 }
705# ifdef FEAT_FLOAT
706 else if (our_tv->v_type == VAR_FLOAT)
707 {
708 char buf[NUMBUFLEN];
709
710 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200711 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712 }
713# endif
714 else if (our_tv->v_type == VAR_LIST)
715 {
716 list_T *list = our_tv->vval.v_list;
717 listitem_T *curr;
718
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200719 if (list == NULL)
720 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200722 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200723 return NULL;
724
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
729 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200731 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
732 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200733 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200735 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200736 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200738 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200739 {
740 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200741 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 return NULL;
743 }
744 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 }
746 }
747 else if (our_tv->v_type == VAR_DICT)
748 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200750 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
751 long_u todo = ht->ht_used;
752 hashitem_T *hi;
753 dictitem_T *di;
754 if (our_tv->vval.v_dict == NULL)
755 return NULL;
756
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200757 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200758 return NULL;
759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 return NULL;
764 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200766 for (hi = ht->ht_array; todo > 0; ++hi)
767 {
768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200770 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200772 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200773 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200774 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200775 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200777 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200778 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200780 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781 Py_DECREF(newObj);
782 return NULL;
783 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200784 }
785 }
786 }
787 else
788 {
789 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200790 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 }
792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795
796 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200797VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200799 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200801 PyObject *string;
802 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200804 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805
Bram Moolenaar389a1792013-06-23 13:00:44 +0200806 if (!PyArg_ParseTuple(args, "O", &string))
807 return NULL;
808
809 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810 return NULL;
811
812 Py_BEGIN_ALLOW_THREADS
813 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200814 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200815 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816 Python_Release_Vim();
817 Py_END_ALLOW_THREADS
818
Bram Moolenaar389a1792013-06-23 13:00:44 +0200819 Py_XDECREF(todecref);
820
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200821 if (VimTryEnd())
822 return NULL;
823
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824 if (our_tv == NULL)
825 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200826 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827 return NULL;
828 }
829
830 /* Convert the Vim type into a Python type. Create a dictionary that's
831 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200832 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200833 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200834 else
835 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200837 Py_DECREF(lookup_dict);
838 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839
840
841 Py_BEGIN_ALLOW_THREADS
842 Python_Lock_Vim();
843 free_tv(our_tv);
844 Python_Release_Vim();
845 Py_END_ALLOW_THREADS
846
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200847 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848}
849
Bram Moolenaardb913952012-06-29 12:54:53 +0200850static PyObject *ConvertToPyObject(typval_T *);
851
852 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200853VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200854{
Bram Moolenaardb913952012-06-29 12:54:53 +0200855 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200856 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200857 char_u *expr;
858 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200859
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200861 return NULL;
862
863 Py_BEGIN_ALLOW_THREADS
864 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200865 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200866 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200867 Python_Release_Vim();
868 Py_END_ALLOW_THREADS
869
Bram Moolenaar389a1792013-06-23 13:00:44 +0200870 Py_XDECREF(todecref);
871
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200872 if (VimTryEnd())
873 return NULL;
874
Bram Moolenaardb913952012-06-29 12:54:53 +0200875 if (our_tv == NULL)
876 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200877 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200878 return NULL;
879 }
880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200882 Py_BEGIN_ALLOW_THREADS
883 Python_Lock_Vim();
884 free_tv(our_tv);
885 Python_Release_Vim();
886 Py_END_ALLOW_THREADS
887
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200888 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200889}
890
891 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200892VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200893{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200894 char_u *str;
895 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200896 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200897
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200899 return NULL;
900
Bram Moolenaara54bf402012-12-05 16:30:07 +0100901#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200902 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100903#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200904 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100905#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906
907 Py_XDECREF(todecref);
908
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200910}
911
Bram Moolenaarf4258302013-06-02 18:20:17 +0200912 static PyObject *
913_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
914{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200915 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200916 PyObject *newwd;
917 PyObject *todecref;
918 char_u *new_dir;
919
Bram Moolenaard4209d22013-06-05 20:34:15 +0200920 if (_chdir == NULL)
921 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200922 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200923 return NULL;
924
925 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
926 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928 return NULL;
929 }
930
931 if (!(new_dir = StringToChars(newwd, &todecref)))
932 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200933 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200934 Py_DECREF(newwd);
935 return NULL;
936 }
937
938 VimTryStart();
939
940 if (vim_chdir(new_dir))
941 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200942 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200943 Py_DECREF(newwd);
944 Py_XDECREF(todecref);
945
946 if (VimTryEnd())
947 return NULL;
948
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200949 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200950 return NULL;
951 }
952
953 Py_DECREF(newwd);
954 Py_XDECREF(todecref);
955
956 post_chdir(FALSE);
957
958 if (VimTryEnd())
959 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200960 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200961 return NULL;
962 }
963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200965}
966
967 static PyObject *
968VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
969{
970 return _VimChdir(py_chdir, args, kwargs);
971}
972
973 static PyObject *
974VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
975{
976 return _VimChdir(py_fchdir, args, kwargs);
977}
978
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200979typedef struct {
980 PyObject *callable;
981 PyObject *result;
982} map_rtp_data;
983
984 static void
985map_rtp_callback(char_u *path, void *_data)
986{
987 void **data = (void **) _data;
988 PyObject *pathObject;
989 map_rtp_data *mr_data = *((map_rtp_data **) data);
990
Bram Moolenaar41009372013-07-01 22:03:04 +0200991 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200992 {
993 *data = NULL;
994 return;
995 }
996
997 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
998 pathObject, NULL);
999
1000 Py_DECREF(pathObject);
1001
1002 if (!mr_data->result || mr_data->result != Py_None)
1003 *data = NULL;
1004 else
1005 {
1006 Py_DECREF(mr_data->result);
1007 mr_data->result = NULL;
1008 }
1009}
1010
1011 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001012VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001013{
1014 map_rtp_data data;
1015
Bram Moolenaar389a1792013-06-23 13:00:44 +02001016 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001017 data.result = NULL;
1018
1019 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1020
1021 if (data.result == NULL)
1022 {
1023 if (PyErr_Occurred())
1024 return NULL;
1025 else
1026 {
1027 Py_INCREF(Py_None);
1028 return Py_None;
1029 }
1030 }
1031 return data.result;
1032}
1033
1034/*
1035 * _vim_runtimepath_ special path implementation.
1036 */
1037
1038 static void
1039map_finder_callback(char_u *path, void *_data)
1040{
1041 void **data = (void **) _data;
1042 PyObject *list = *((PyObject **) data);
1043 PyObject *pathObject1, *pathObject2;
1044 char *pathbuf;
1045 size_t pathlen;
1046
1047 pathlen = STRLEN(path);
1048
1049#if PY_MAJOR_VERSION < 3
1050# define PY_MAIN_DIR_STRING "python2"
1051#else
1052# define PY_MAIN_DIR_STRING "python3"
1053#endif
1054#define PY_ALTERNATE_DIR_STRING "pythonx"
1055
1056#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1057 if (!(pathbuf = PyMem_New(char,
1058 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1059 {
1060 PyErr_NoMemory();
1061 *data = NULL;
1062 return;
1063 }
1064
1065 mch_memmove(pathbuf, path, pathlen + 1);
1066 add_pathsep((char_u *) pathbuf);
1067
1068 pathlen = STRLEN(pathbuf);
1069 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1070 PYTHONX_STRING_LENGTH + 1);
1071
1072 if (!(pathObject1 = PyString_FromString(pathbuf)))
1073 {
1074 *data = NULL;
1075 PyMem_Free(pathbuf);
1076 return;
1077 }
1078
1079 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1080 PYTHONX_STRING_LENGTH + 1);
1081
1082 if (!(pathObject2 = PyString_FromString(pathbuf)))
1083 {
1084 Py_DECREF(pathObject1);
1085 PyMem_Free(pathbuf);
1086 *data = NULL;
1087 return;
1088 }
1089
1090 PyMem_Free(pathbuf);
1091
1092 if (PyList_Append(list, pathObject1)
1093 || PyList_Append(list, pathObject2))
1094 *data = NULL;
1095
1096 Py_DECREF(pathObject1);
1097 Py_DECREF(pathObject2);
1098}
1099
1100 static PyObject *
1101Vim_GetPaths(PyObject *self UNUSED)
1102{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001103 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001104
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001105 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001106 return NULL;
1107
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001108 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001109
1110 if (PyErr_Occurred())
1111 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001112 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001113 return NULL;
1114 }
1115
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001116 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117}
1118
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001119 static PyObject *
1120call_load_module(char *name, int len, PyObject *find_module_result)
1121{
1122 PyObject *fd, *pathname, *description;
1123
Bram Moolenaarc476e522013-06-23 13:46:40 +02001124 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001125 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001126 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001127 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001128 Py_TYPE_NAME(find_module_result));
1129 return NULL;
1130 }
1131 if (PyTuple_GET_SIZE(find_module_result) != 3)
1132 {
1133 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001134 N_("expected 3-tuple as imp.find_module() result, but got "
1135 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001136 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001137 return NULL;
1138 }
1139
1140 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1141 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1142 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1143 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001144 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001145 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001146 return NULL;
1147 }
1148
1149 return PyObject_CallFunction(py_load_module,
1150 "s#OOO", name, len, fd, pathname, description);
1151}
1152
1153 static PyObject *
1154find_module(char *fullname, char *tail, PyObject *new_path)
1155{
1156 PyObject *find_module_result;
1157 PyObject *module;
1158 char *dot;
1159
Bram Moolenaar41009372013-07-01 22:03:04 +02001160 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001161 {
1162 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001163 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001164 * first component
1165 */
1166 PyObject *newest_path;
1167 int partlen = (int) (dot - 1 - tail);
1168
1169 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1170 "s#O", tail, partlen, new_path)))
1171 return NULL;
1172
1173 if (!(module = call_load_module(
1174 fullname,
1175 ((int) (tail - fullname)) + partlen,
1176 find_module_result)))
1177 {
1178 Py_DECREF(find_module_result);
1179 return NULL;
1180 }
1181
1182 Py_DECREF(find_module_result);
1183
1184 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1185 {
1186 Py_DECREF(module);
1187 return NULL;
1188 }
1189
1190 Py_DECREF(module);
1191
1192 module = find_module(fullname, dot + 1, newest_path);
1193
1194 Py_DECREF(newest_path);
1195
1196 return module;
1197 }
1198 else
1199 {
1200 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1201 "sO", tail, new_path)))
1202 return NULL;
1203
1204 if (!(module = call_load_module(
1205 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001206 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001207 find_module_result)))
1208 {
1209 Py_DECREF(find_module_result);
1210 return NULL;
1211 }
1212
1213 Py_DECREF(find_module_result);
1214
1215 return module;
1216 }
1217}
1218
1219 static PyObject *
1220FinderFindModule(PyObject *self, PyObject *args)
1221{
1222 char *fullname;
1223 PyObject *module;
1224 PyObject *new_path;
1225 LoaderObject *loader;
1226
1227 if (!PyArg_ParseTuple(args, "s", &fullname))
1228 return NULL;
1229
1230 if (!(new_path = Vim_GetPaths(self)))
1231 return NULL;
1232
1233 module = find_module(fullname, fullname, new_path);
1234
1235 Py_DECREF(new_path);
1236
1237 if (!module)
1238 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001239 if (PyErr_Occurred())
1240 {
1241 if (PyErr_ExceptionMatches(PyExc_ImportError))
1242 PyErr_Clear();
1243 else
1244 return NULL;
1245 }
1246
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001247 Py_INCREF(Py_None);
1248 return Py_None;
1249 }
1250
1251 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1252 {
1253 Py_DECREF(module);
1254 return NULL;
1255 }
1256
1257 loader->module = module;
1258
1259 return (PyObject *) loader;
1260}
1261
1262 static PyObject *
1263VimPathHook(PyObject *self UNUSED, PyObject *args)
1264{
1265 char *path;
1266
1267 if (PyArg_ParseTuple(args, "s", &path)
1268 && STRCMP(path, vim_special_path) == 0)
1269 {
1270 Py_INCREF(vim_module);
1271 return vim_module;
1272 }
1273
1274 PyErr_Clear();
1275 PyErr_SetNone(PyExc_ImportError);
1276 return NULL;
1277}
1278
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001279/*
1280 * Vim module - Definitions
1281 */
1282
1283static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001284 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001285 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001286 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001287 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1288 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001289 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1290 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001291 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001292 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001293 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1294 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1295 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001296};
1297
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001298/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001299 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001300 */
1301
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001302static PyTypeObject IterType;
1303
1304typedef PyObject *(*nextfun)(void **);
1305typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001306typedef int (*traversefun)(void *, visitproc, void *);
1307typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001308
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001309/* Main purpose of this object is removing the need for do python
1310 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1311 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001312
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001313typedef struct
1314{
1315 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001316 void *cur;
1317 nextfun next;
1318 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001319 traversefun traverse;
1320 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001321} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001322
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001323 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1325 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001327 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001328
Bram Moolenaar774267b2013-05-21 20:51:59 +02001329 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001330 self->cur = start;
1331 self->next = next;
1332 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001333 self->traverse = traverse;
1334 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001335
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001336 return (PyObject *)(self);
1337}
1338
1339 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001340IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001341{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001342 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001343 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001344 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345}
1346
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001347 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001349{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001350 if (self->traverse != NULL)
1351 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352 else
1353 return 0;
1354}
1355
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001356/* Mac OSX defines clear() somewhere. */
1357#ifdef clear
1358# undef clear
1359#endif
1360
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001361 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001362IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001363{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001364 if (self->clear != NULL)
1365 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 else
1367 return 0;
1368}
1369
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001370 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001371IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001372{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001373 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001374}
1375
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001376 static PyObject *
1377IterIter(PyObject *self)
1378{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001379 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001380 return self;
1381}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001382
Bram Moolenaardb913952012-06-29 12:54:53 +02001383typedef struct pylinkedlist_S {
1384 struct pylinkedlist_S *pll_next;
1385 struct pylinkedlist_S *pll_prev;
1386 PyObject *pll_obj;
1387} pylinkedlist_T;
1388
1389static pylinkedlist_T *lastdict = NULL;
1390static pylinkedlist_T *lastlist = NULL;
1391
1392 static void
1393pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1394{
1395 if (ref->pll_prev == NULL)
1396 {
1397 if (ref->pll_next == NULL)
1398 {
1399 *last = NULL;
1400 return;
1401 }
1402 }
1403 else
1404 ref->pll_prev->pll_next = ref->pll_next;
1405
1406 if (ref->pll_next == NULL)
1407 *last = ref->pll_prev;
1408 else
1409 ref->pll_next->pll_prev = ref->pll_prev;
1410}
1411
1412 static void
1413pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1414{
1415 if (*last == NULL)
1416 ref->pll_prev = NULL;
1417 else
1418 {
1419 (*last)->pll_next = ref;
1420 ref->pll_prev = *last;
1421 }
1422 ref->pll_next = NULL;
1423 ref->pll_obj = self;
1424 *last = ref;
1425}
1426
1427static PyTypeObject DictionaryType;
1428
1429typedef struct
1430{
1431 PyObject_HEAD
1432 dict_T *dict;
1433 pylinkedlist_T ref;
1434} DictionaryObject;
1435
Bram Moolenaara9922d62013-05-30 13:01:18 +02001436static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1437
1438#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1439
Bram Moolenaardb913952012-06-29 12:54:53 +02001440 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001441DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001442{
1443 DictionaryObject *self;
1444
Bram Moolenaara9922d62013-05-30 13:01:18 +02001445 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001446 if (self == NULL)
1447 return NULL;
1448 self->dict = dict;
1449 ++dict->dv_refcount;
1450
1451 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1452
1453 return (PyObject *)(self);
1454}
1455
Bram Moolenaara9922d62013-05-30 13:01:18 +02001456 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001457py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001458{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001459 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001461 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001462 {
1463 PyErr_NoMemory();
1464 return NULL;
1465 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001467
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001468 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001469}
1470
1471 static PyObject *
1472DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1473{
1474 DictionaryObject *self;
1475 dict_T *dict;
1476
1477 if (!(dict = py_dict_alloc()))
1478 return NULL;
1479
1480 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1481
1482 --dict->dv_refcount;
1483
1484 if (kwargs || PyTuple_Size(args))
1485 {
1486 PyObject *tmp;
1487 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1488 {
1489 Py_DECREF(self);
1490 return NULL;
1491 }
1492
1493 Py_DECREF(tmp);
1494 }
1495
1496 return (PyObject *)(self);
1497}
1498
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001499 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001500DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001501{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502 pyll_remove(&self->ref, &lastdict);
1503 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504
1505 DESTRUCTOR_FINISH(self);
1506}
1507
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001508static char *DictionaryAttrs[] = {
1509 "locked", "scope",
1510 NULL
1511};
1512
1513 static PyObject *
1514DictionaryDir(PyObject *self)
1515{
1516 return ObjectDir(self, DictionaryAttrs);
1517}
1518
Bram Moolenaardb913952012-06-29 12:54:53 +02001519 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001520DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001521{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001522 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001523 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001524 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001525 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001526 return -1;
1527 }
1528
1529 if (strcmp(name, "locked") == 0)
1530 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001531 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001532 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001533 PyErr_SET_STRING(PyExc_TypeError,
1534 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001535 return -1;
1536 }
1537 else
1538 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001539 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001540 if (istrue == -1)
1541 return -1;
1542 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001543 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001544 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001545 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001546 }
1547 return 0;
1548 }
1549 else
1550 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001551 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001552 return -1;
1553 }
1554}
1555
1556 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001557DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001558{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001559 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001560}
1561
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562#define DICT_FLAG_HAS_DEFAULT 0x01
1563#define DICT_FLAG_POP 0x02
1564#define DICT_FLAG_NONE_DEFAULT 0x04
1565#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1566#define DICT_FLAG_RETURN_PAIR 0x10
1567
Bram Moolenaardb913952012-06-29 12:54:53 +02001568 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001569_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001570{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001571 PyObject *keyObject;
1572 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001573 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001574 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001575 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 dict_T *dict = self->dict;
1577 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001578 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001579
Bram Moolenaara9922d62013-05-30 13:01:18 +02001580 if (flags & DICT_FLAG_HAS_DEFAULT)
1581 {
1582 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1583 return NULL;
1584 }
1585 else
1586 keyObject = args;
1587
1588 if (flags & DICT_FLAG_RETURN_BOOL)
1589 defObject = Py_False;
1590
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001591 if (!(key = StringToChars(keyObject, &todecref)))
1592 return NULL;
1593
1594 if (*key == NUL)
1595 {
1596 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001597 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001598 return NULL;
1599 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001600
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001602
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001603 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001604
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001606 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001607 if (defObject)
1608 {
1609 Py_INCREF(defObject);
1610 return defObject;
1611 }
1612 else
1613 {
1614 PyErr_SetObject(PyExc_KeyError, keyObject);
1615 return NULL;
1616 }
1617 }
1618 else if (flags & DICT_FLAG_RETURN_BOOL)
1619 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001620 ret = Py_True;
1621 Py_INCREF(ret);
1622 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001623 }
1624
1625 di = dict_lookup(hi);
1626
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001627 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001628 return NULL;
1629
1630 if (flags & DICT_FLAG_POP)
1631 {
1632 if (dict->dv_lock)
1633 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001634 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001635 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001636 return NULL;
1637 }
1638
1639 hash_remove(&dict->dv_hashtab, hi);
1640 dictitem_free(di);
1641 }
1642
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001643 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001644}
1645
1646 static PyObject *
1647DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1648{
1649 return _DictionaryItem(self, keyObject, 0);
1650}
1651
1652 static int
1653DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1654{
1655 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001656 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001657
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001658 if (rObj == NULL)
1659 return -1;
1660
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001661 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001662
Bram Moolenaardee2e312013-06-23 16:35:47 +02001663 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001664
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001665 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001666}
1667
1668typedef struct
1669{
1670 hashitem_T *ht_array;
1671 long_u ht_used;
1672 hashtab_T *ht;
1673 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001674 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001675} dictiterinfo_T;
1676
1677 static PyObject *
1678DictionaryIterNext(dictiterinfo_T **dii)
1679{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001680 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001681
1682 if (!(*dii)->todo)
1683 return NULL;
1684
1685 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1686 (*dii)->ht->ht_used != (*dii)->ht_used)
1687 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001688 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001689 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001690 return NULL;
1691 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001692
Bram Moolenaara9922d62013-05-30 13:01:18 +02001693 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1694 ++((*dii)->hi);
1695
1696 --((*dii)->todo);
1697
Bram Moolenaar41009372013-07-01 22:03:04 +02001698 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001699 return NULL;
1700
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001701 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702}
1703
1704 static PyObject *
1705DictionaryIter(DictionaryObject *self)
1706{
1707 dictiterinfo_T *dii;
1708 hashtab_T *ht;
1709
1710 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1711 {
1712 PyErr_NoMemory();
1713 return NULL;
1714 }
1715
1716 ht = &self->dict->dv_hashtab;
1717 dii->ht_array = ht->ht_array;
1718 dii->ht_used = ht->ht_used;
1719 dii->ht = ht;
1720 dii->hi = dii->ht_array;
1721 dii->todo = dii->ht_used;
1722
1723 return IterNew(dii,
1724 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1725 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001726}
1727
1728 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001729DictionaryAssItem(
1730 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001731{
1732 char_u *key;
1733 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001734 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001735 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001736 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001737
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001738 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001739 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001740 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001741 return -1;
1742 }
1743
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001744 if (!(key = StringToChars(keyObject, &todecref)))
1745 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001746
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001747 if (*key == NUL)
1748 {
1749 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001750 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001751 return -1;
1752 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001753
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001754 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001755
1756 if (valObject == NULL)
1757 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001758 hashitem_T *hi;
1759
Bram Moolenaardb913952012-06-29 12:54:53 +02001760 if (di == NULL)
1761 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001762 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001763 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001764 return -1;
1765 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001766 hi = hash_find(&dict->dv_hashtab, di->di_key);
1767 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001768 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001769 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001770 return 0;
1771 }
1772
1773 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001774 {
1775 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001776 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001777 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001778
1779 if (di == NULL)
1780 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001781 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001782 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001783 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001784 PyErr_NoMemory();
1785 return -1;
1786 }
1787 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001789
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001790 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001791 {
1792 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001793 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001794 RAISE_KEY_ADD_FAIL(key);
1795 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001796 return -1;
1797 }
1798 }
1799 else
1800 clear_tv(&di->di_tv);
1801
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001802 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001803
1804 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001805 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001806 return 0;
1807}
1808
Bram Moolenaara9922d62013-05-30 13:01:18 +02001809typedef PyObject *(*hi_to_py)(hashitem_T *);
1810
Bram Moolenaardb913952012-06-29 12:54:53 +02001811 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001812DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001813{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001814 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 long_u todo = dict->dv_hashtab.ht_used;
1816 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001817 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001818 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001819 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001820
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001821 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001822 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1823 {
1824 if (!HASHITEM_EMPTY(hi))
1825 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001826 if (!(newObj = hiconvert(hi)))
1827 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001828 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 return NULL;
1830 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001831 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001832 --todo;
1833 ++i;
1834 }
1835 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001836 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001837}
1838
Bram Moolenaara9922d62013-05-30 13:01:18 +02001839 static PyObject *
1840dict_key(hashitem_T *hi)
1841{
1842 return PyBytes_FromString((char *)(hi->hi_key));
1843}
1844
1845 static PyObject *
1846DictionaryListKeys(DictionaryObject *self)
1847{
1848 return DictionaryListObjects(self, dict_key);
1849}
1850
1851 static PyObject *
1852dict_val(hashitem_T *hi)
1853{
1854 dictitem_T *di;
1855
1856 di = dict_lookup(hi);
1857 return ConvertToPyObject(&di->di_tv);
1858}
1859
1860 static PyObject *
1861DictionaryListValues(DictionaryObject *self)
1862{
1863 return DictionaryListObjects(self, dict_val);
1864}
1865
1866 static PyObject *
1867dict_item(hashitem_T *hi)
1868{
1869 PyObject *keyObject;
1870 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001871 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001872
1873 if (!(keyObject = dict_key(hi)))
1874 return NULL;
1875
1876 if (!(valObject = dict_val(hi)))
1877 {
1878 Py_DECREF(keyObject);
1879 return NULL;
1880 }
1881
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001882 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001883
1884 Py_DECREF(keyObject);
1885 Py_DECREF(valObject);
1886
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001887 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001888}
1889
1890 static PyObject *
1891DictionaryListItems(DictionaryObject *self)
1892{
1893 return DictionaryListObjects(self, dict_item);
1894}
1895
1896 static PyObject *
1897DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1898{
1899 dict_T *dict = self->dict;
1900
1901 if (dict->dv_lock)
1902 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001903 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001904 return NULL;
1905 }
1906
1907 if (kwargs)
1908 {
1909 typval_T tv;
1910
1911 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1912 return NULL;
1913
1914 VimTryStart();
1915 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1916 clear_tv(&tv);
1917 if (VimTryEnd())
1918 return NULL;
1919 }
1920 else
1921 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001922 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001923
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001924 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001925 return NULL;
1926
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001927 if (obj == NULL)
1928 {
1929 Py_INCREF(Py_None);
1930 return Py_None;
1931 }
1932
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001933 if (PyObject_HasAttrString(obj, "keys"))
1934 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001935 else
1936 {
1937 PyObject *iterator;
1938 PyObject *item;
1939
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001940 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001941 return NULL;
1942
1943 while ((item = PyIter_Next(iterator)))
1944 {
1945 PyObject *fast;
1946 PyObject *keyObject;
1947 PyObject *valObject;
1948 PyObject *todecref;
1949 char_u *key;
1950 dictitem_T *di;
1951
1952 if (!(fast = PySequence_Fast(item, "")))
1953 {
1954 Py_DECREF(iterator);
1955 Py_DECREF(item);
1956 return NULL;
1957 }
1958
1959 Py_DECREF(item);
1960
1961 if (PySequence_Fast_GET_SIZE(fast) != 2)
1962 {
1963 Py_DECREF(iterator);
1964 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001965 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001966 N_("expected sequence element of size 2, "
1967 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001968 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001969 return NULL;
1970 }
1971
1972 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1973
1974 if (!(key = StringToChars(keyObject, &todecref)))
1975 {
1976 Py_DECREF(iterator);
1977 Py_DECREF(fast);
1978 return NULL;
1979 }
1980
1981 di = dictitem_alloc(key);
1982
1983 Py_XDECREF(todecref);
1984
1985 if (di == NULL)
1986 {
1987 Py_DECREF(fast);
1988 Py_DECREF(iterator);
1989 PyErr_NoMemory();
1990 return NULL;
1991 }
1992 di->di_tv.v_lock = 0;
1993 di->di_tv.v_type = VAR_UNKNOWN;
1994
1995 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1996
1997 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1998 {
1999 Py_DECREF(iterator);
2000 Py_DECREF(fast);
2001 dictitem_free(di);
2002 return NULL;
2003 }
2004
2005 Py_DECREF(fast);
2006
2007 if (dict_add(dict, di) == FAIL)
2008 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002009 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010 Py_DECREF(iterator);
2011 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012 return NULL;
2013 }
2014 }
2015
2016 Py_DECREF(iterator);
2017
2018 /* Iterator may have finished due to an exception */
2019 if (PyErr_Occurred())
2020 return NULL;
2021 }
2022 }
2023 Py_INCREF(Py_None);
2024 return Py_None;
2025}
2026
2027 static PyObject *
2028DictionaryGet(DictionaryObject *self, PyObject *args)
2029{
2030 return _DictionaryItem(self, args,
2031 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2032}
2033
2034 static PyObject *
2035DictionaryPop(DictionaryObject *self, PyObject *args)
2036{
2037 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2038}
2039
2040 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002041DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002042{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002043 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002044 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002045 PyObject *valObject;
2046 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002047
Bram Moolenaarde71b562013-06-02 17:41:54 +02002048 if (self->dict->dv_hashtab.ht_used == 0)
2049 {
2050 PyErr_SetNone(PyExc_KeyError);
2051 return NULL;
2052 }
2053
2054 hi = self->dict->dv_hashtab.ht_array;
2055 while (HASHITEM_EMPTY(hi))
2056 ++hi;
2057
2058 di = dict_lookup(hi);
2059
2060 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002061 return NULL;
2062
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002063 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002064 {
2065 Py_DECREF(valObject);
2066 return NULL;
2067 }
2068
2069 hash_remove(&self->dict->dv_hashtab, hi);
2070 dictitem_free(di);
2071
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002072 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002073}
2074
2075 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002076DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002077{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002078 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2079}
2080
2081static PySequenceMethods DictionaryAsSeq = {
2082 0, /* sq_length */
2083 0, /* sq_concat */
2084 0, /* sq_repeat */
2085 0, /* sq_item */
2086 0, /* sq_slice */
2087 0, /* sq_ass_item */
2088 0, /* sq_ass_slice */
2089 (objobjproc) DictionaryContains, /* sq_contains */
2090 0, /* sq_inplace_concat */
2091 0, /* sq_inplace_repeat */
2092};
2093
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002094static PyMappingMethods DictionaryAsMapping = {
2095 (lenfunc) DictionaryLength,
2096 (binaryfunc) DictionaryItem,
2097 (objobjargproc) DictionaryAssItem,
2098};
2099
Bram Moolenaardb913952012-06-29 12:54:53 +02002100static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002101 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002102 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2103 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2104 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2105 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2106 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002107 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002108 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002109 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2110 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002111};
2112
2113static PyTypeObject ListType;
2114
2115typedef struct
2116{
2117 PyObject_HEAD
2118 list_T *list;
2119 pylinkedlist_T ref;
2120} ListObject;
2121
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002122#define NEW_LIST(list) ListNew(&ListType, list)
2123
Bram Moolenaardb913952012-06-29 12:54:53 +02002124 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002125ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002126{
2127 ListObject *self;
2128
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002129 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002130 if (self == NULL)
2131 return NULL;
2132 self->list = list;
2133 ++list->lv_refcount;
2134
2135 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2136
2137 return (PyObject *)(self);
2138}
2139
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002140 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002141py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002142{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002143 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002144
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002145 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002146 {
2147 PyErr_NoMemory();
2148 return NULL;
2149 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002150 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002151
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002152 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002153}
2154
2155 static int
2156list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2157{
2158 PyObject *iterator;
2159 PyObject *item;
2160 listitem_T *li;
2161
2162 if (!(iterator = PyObject_GetIter(obj)))
2163 return -1;
2164
2165 while ((item = PyIter_Next(iterator)))
2166 {
2167 if (!(li = listitem_alloc()))
2168 {
2169 PyErr_NoMemory();
2170 Py_DECREF(item);
2171 Py_DECREF(iterator);
2172 return -1;
2173 }
2174 li->li_tv.v_lock = 0;
2175 li->li_tv.v_type = VAR_UNKNOWN;
2176
2177 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2178 {
2179 Py_DECREF(item);
2180 Py_DECREF(iterator);
2181 listitem_free(li);
2182 return -1;
2183 }
2184
2185 Py_DECREF(item);
2186
2187 list_append(l, li);
2188 }
2189
2190 Py_DECREF(iterator);
2191
2192 /* Iterator may have finished due to an exception */
2193 if (PyErr_Occurred())
2194 return -1;
2195
2196 return 0;
2197}
2198
2199 static PyObject *
2200ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2201{
2202 list_T *list;
2203 PyObject *obj = NULL;
2204
2205 if (kwargs)
2206 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002207 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002208 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002209 return NULL;
2210 }
2211
2212 if (!PyArg_ParseTuple(args, "|O", &obj))
2213 return NULL;
2214
2215 if (!(list = py_list_alloc()))
2216 return NULL;
2217
2218 if (obj)
2219 {
2220 PyObject *lookup_dict;
2221
2222 if (!(lookup_dict = PyDict_New()))
2223 {
2224 list_unref(list);
2225 return NULL;
2226 }
2227
2228 if (list_py_concat(list, obj, lookup_dict) == -1)
2229 {
2230 Py_DECREF(lookup_dict);
2231 list_unref(list);
2232 return NULL;
2233 }
2234
2235 Py_DECREF(lookup_dict);
2236 }
2237
2238 return ListNew(subtype, list);
2239}
2240
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002241 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002242ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002243{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002244 pyll_remove(&self->ref, &lastlist);
2245 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002246
2247 DESTRUCTOR_FINISH(self);
2248}
2249
Bram Moolenaardb913952012-06-29 12:54:53 +02002250 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002251ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002252{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002253 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002254}
2255
2256 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002257ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002258{
2259 listitem_T *li;
2260
Bram Moolenaard6e39182013-05-21 18:30:34 +02002261 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002262 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002263 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002264 return NULL;
2265 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002266 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002267 if (li == NULL)
2268 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002269 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002270 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002271 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002272 return NULL;
2273 }
2274 return ConvertToPyObject(&li->li_tv);
2275}
2276
Bram Moolenaardb913952012-06-29 12:54:53 +02002277 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002278ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2279 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002280{
2281 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002282 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002283
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002284 if (step == 0)
2285 {
2286 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2287 return NULL;
2288 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002289
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002290 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002291 if (list == NULL)
2292 return NULL;
2293
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002294 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002295 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002296 PyObject *item;
2297
2298 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002299 if (item == NULL)
2300 {
2301 Py_DECREF(list);
2302 return NULL;
2303 }
2304
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002305 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002306 }
2307
2308 return list;
2309}
2310
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002311 static PyObject *
2312ListItem(ListObject *self, PyObject* idx)
2313{
2314#if PY_MAJOR_VERSION < 3
2315 if (PyInt_Check(idx))
2316 {
2317 long _idx = PyInt_AsLong(idx);
2318 return ListIndex(self, _idx);
2319 }
2320 else
2321#endif
2322 if (PyLong_Check(idx))
2323 {
2324 long _idx = PyLong_AsLong(idx);
2325 return ListIndex(self, _idx);
2326 }
2327 else if (PySlice_Check(idx))
2328 {
2329 Py_ssize_t start, stop, step, slicelen;
2330
Bram Moolenaar5395e7a2014-01-14 19:35:56 +01002331 if (PySlice_GetIndicesEx((PySliceObject *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002332 &start, &stop, &step, &slicelen) < 0)
2333 return NULL;
2334 return ListSlice(self, start, step, slicelen);
2335 }
2336 else
2337 {
2338 RAISE_INVALID_INDEX_TYPE(idx);
2339 return NULL;
2340 }
2341}
2342
2343 static void
2344list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2345 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2346{
2347 while (numreplaced--)
2348 {
2349 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2350 listitem_remove(l, lis[slicelen + numreplaced]);
2351 }
2352 while (numadded--)
2353 {
2354 listitem_T *next;
2355
2356 next = lastaddedli->li_prev;
2357 listitem_remove(l, lastaddedli);
2358 lastaddedli = next;
2359 }
2360}
2361
2362 static int
2363ListAssSlice(ListObject *self, Py_ssize_t first,
2364 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2365{
2366 PyObject *iterator;
2367 PyObject *item;
2368 listitem_T *li;
2369 listitem_T *lastaddedli = NULL;
2370 listitem_T *next;
2371 typval_T v;
2372 list_T *l = self->list;
2373 PyInt i;
2374 PyInt j;
2375 PyInt numreplaced = 0;
2376 PyInt numadded = 0;
2377 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002378 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002379
2380 size = ListLength(self);
2381
2382 if (l->lv_lock)
2383 {
2384 RAISE_LOCKED_LIST;
2385 return -1;
2386 }
2387
2388 if (step == 0)
2389 {
2390 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2391 return -1;
2392 }
2393
2394 if (step != 1 && slicelen == 0)
2395 {
2396 /* Nothing to do. Only error out if obj has some items. */
2397 int ret = 0;
2398
2399 if (obj == NULL)
2400 return 0;
2401
2402 if (!(iterator = PyObject_GetIter(obj)))
2403 return -1;
2404
2405 if ((item = PyIter_Next(iterator)))
2406 {
2407 PyErr_FORMAT(PyExc_ValueError,
2408 N_("attempt to assign sequence of size greater then %d "
2409 "to extended slice"), 0);
2410 Py_DECREF(item);
2411 ret = -1;
2412 }
2413 Py_DECREF(iterator);
2414 return ret;
2415 }
2416
2417 if (obj != NULL)
2418 /* XXX May allocate zero bytes. */
2419 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2420 {
2421 PyErr_NoMemory();
2422 return -1;
2423 }
2424
2425 if (first == size)
2426 li = NULL;
2427 else
2428 {
2429 li = list_find(l, (long) first);
2430 if (li == NULL)
2431 {
2432 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2433 (int)first);
2434 if (obj != NULL)
2435 PyMem_Free(lis);
2436 return -1;
2437 }
2438 i = slicelen;
2439 while (i-- && li != NULL)
2440 {
2441 j = step;
2442 next = li;
2443 if (step > 0)
2444 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2445 else
2446 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2447
2448 if (obj == NULL)
2449 listitem_remove(l, li);
2450 else
2451 lis[slicelen - i - 1] = li;
2452
2453 li = next;
2454 }
2455 if (li == NULL && i != -1)
2456 {
2457 PyErr_SET_VIM(N_("internal error: not enough list items"));
2458 if (obj != NULL)
2459 PyMem_Free(lis);
2460 return -1;
2461 }
2462 }
2463
2464 if (obj == NULL)
2465 return 0;
2466
2467 if (!(iterator = PyObject_GetIter(obj)))
2468 {
2469 PyMem_Free(lis);
2470 return -1;
2471 }
2472
2473 i = 0;
2474 while ((item = PyIter_Next(iterator)))
2475 {
2476 if (ConvertFromPyObject(item, &v) == -1)
2477 {
2478 Py_DECREF(iterator);
2479 Py_DECREF(item);
2480 PyMem_Free(lis);
2481 return -1;
2482 }
2483 Py_DECREF(item);
2484 if (list_insert_tv(l, &v, numreplaced < slicelen
2485 ? lis[numreplaced]
2486 : li) == FAIL)
2487 {
2488 clear_tv(&v);
2489 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2490 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2491 PyMem_Free(lis);
2492 return -1;
2493 }
2494 if (numreplaced < slicelen)
2495 {
2496 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
2497 list_remove(l, lis[numreplaced], lis[numreplaced]);
2498 numreplaced++;
2499 }
2500 else
2501 {
2502 if (li)
2503 lastaddedli = li->li_prev;
2504 else
2505 lastaddedli = l->lv_last;
2506 numadded++;
2507 }
2508 clear_tv(&v);
2509 if (step != 1 && i >= slicelen)
2510 {
2511 Py_DECREF(iterator);
2512 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar3b522612014-02-11 16:00:35 +01002513 N_("attempt to assign sequence of size greater then %ld "
2514 "to extended slice"), (long)slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002515 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2516 PyMem_Free(lis);
2517 return -1;
2518 }
2519 ++i;
2520 }
2521 Py_DECREF(iterator);
2522
2523 if (step != 1 && i != slicelen)
2524 {
2525 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar3b522612014-02-11 16:00:35 +01002526 N_("attempt to assign sequence of size %ld to extended slice "
2527 "of size %ld"), (long)i, (long)slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002528 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2529 PyMem_Free(lis);
2530 return -1;
2531 }
2532
2533 if (PyErr_Occurred())
2534 {
2535 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2536 PyMem_Free(lis);
2537 return -1;
2538 }
2539
2540 for (i = 0; i < numreplaced; i++)
2541 listitem_free(lis[i]);
2542 if (step == 1)
2543 for (i = numreplaced; i < slicelen; i++)
2544 listitem_remove(l, lis[i]);
2545
2546 PyMem_Free(lis);
2547
2548 return 0;
2549}
2550
2551 static int
2552ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2553{
2554 typval_T tv;
2555 list_T *l = self->list;
2556 listitem_T *li;
2557 Py_ssize_t length = ListLength(self);
2558
2559 if (l->lv_lock)
2560 {
2561 RAISE_LOCKED_LIST;
2562 return -1;
2563 }
2564 if (index > length || (index == length && obj == NULL))
2565 {
2566 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2567 return -1;
2568 }
2569
2570 if (obj == NULL)
2571 {
2572 li = list_find(l, (long) index);
2573 list_remove(l, li, li);
2574 clear_tv(&li->li_tv);
2575 vim_free(li);
2576 return 0;
2577 }
2578
2579 if (ConvertFromPyObject(obj, &tv) == -1)
2580 return -1;
2581
2582 if (index == length)
2583 {
2584 if (list_append_tv(l, &tv) == FAIL)
2585 {
2586 clear_tv(&tv);
2587 PyErr_SET_VIM(N_("failed to add item to list"));
2588 return -1;
2589 }
2590 }
2591 else
2592 {
2593 li = list_find(l, (long) index);
2594 clear_tv(&li->li_tv);
2595 copy_tv(&tv, &li->li_tv);
2596 clear_tv(&tv);
2597 }
2598 return 0;
2599}
2600
2601 static Py_ssize_t
2602ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2603{
2604#if PY_MAJOR_VERSION < 3
2605 if (PyInt_Check(idx))
2606 {
2607 long _idx = PyInt_AsLong(idx);
2608 return ListAssIndex(self, _idx, obj);
2609 }
2610 else
2611#endif
2612 if (PyLong_Check(idx))
2613 {
2614 long _idx = PyLong_AsLong(idx);
2615 return ListAssIndex(self, _idx, obj);
2616 }
2617 else if (PySlice_Check(idx))
2618 {
2619 Py_ssize_t start, stop, step, slicelen;
2620
Bram Moolenaar5395e7a2014-01-14 19:35:56 +01002621 if (PySlice_GetIndicesEx((PySliceObject *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002622 &start, &stop, &step, &slicelen) < 0)
2623 return -1;
2624 return ListAssSlice(self, start, step, slicelen,
2625 obj);
2626 }
2627 else
2628 {
2629 RAISE_INVALID_INDEX_TYPE(idx);
2630 return -1;
2631 }
2632}
2633
2634 static PyObject *
2635ListConcatInPlace(ListObject *self, PyObject *obj)
2636{
2637 list_T *l = self->list;
2638 PyObject *lookup_dict;
2639
2640 if (l->lv_lock)
2641 {
2642 RAISE_LOCKED_LIST;
2643 return NULL;
2644 }
2645
2646 if (!(lookup_dict = PyDict_New()))
2647 return NULL;
2648
2649 if (list_py_concat(l, obj, lookup_dict) == -1)
2650 {
2651 Py_DECREF(lookup_dict);
2652 return NULL;
2653 }
2654 Py_DECREF(lookup_dict);
2655
2656 Py_INCREF(self);
2657 return (PyObject *)(self);
2658}
2659
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002660typedef struct
2661{
2662 listwatch_T lw;
2663 list_T *list;
2664} listiterinfo_T;
2665
2666 static void
2667ListIterDestruct(listiterinfo_T *lii)
2668{
2669 list_rem_watch(lii->list, &lii->lw);
2670 PyMem_Free(lii);
2671}
2672
2673 static PyObject *
2674ListIterNext(listiterinfo_T **lii)
2675{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002676 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002677
2678 if (!((*lii)->lw.lw_item))
2679 return NULL;
2680
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002681 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002682 return NULL;
2683
2684 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2685
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002686 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002687}
2688
2689 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002690ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002691{
2692 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002693 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002694
2695 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2696 {
2697 PyErr_NoMemory();
2698 return NULL;
2699 }
2700
2701 list_add_watch(l, &lii->lw);
2702 lii->lw.lw_item = l->lv_first;
2703 lii->list = l;
2704
2705 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002706 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2707 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002708}
2709
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002710static char *ListAttrs[] = {
2711 "locked",
2712 NULL
2713};
2714
2715 static PyObject *
2716ListDir(PyObject *self)
2717{
2718 return ObjectDir(self, ListAttrs);
2719}
2720
Bram Moolenaar66b79852012-09-21 14:00:35 +02002721 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002722ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002723{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002724 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002725 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002726 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002727 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002728 return -1;
2729 }
2730
2731 if (strcmp(name, "locked") == 0)
2732 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002733 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002734 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002735 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002736 return -1;
2737 }
2738 else
2739 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002740 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002741 if (istrue == -1)
2742 return -1;
2743 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002744 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002745 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002747 }
2748 return 0;
2749 }
2750 else
2751 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002752 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002753 return -1;
2754 }
2755}
2756
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002757static PySequenceMethods ListAsSeq = {
2758 (lenfunc) ListLength, /* sq_length, len(x) */
2759 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2760 0, /* RangeRepeat, sq_repeat, x*n */
2761 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2762 0, /* was_sq_slice, x[i:j] */
2763 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2764 0, /* was_sq_ass_slice, x[i:j]=v */
2765 0, /* sq_contains */
2766 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2767 0, /* sq_inplace_repeat */
2768};
2769
2770static PyMappingMethods ListAsMapping = {
2771 /* mp_length */ (lenfunc) ListLength,
2772 /* mp_subscript */ (binaryfunc) ListItem,
2773 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2774};
2775
Bram Moolenaardb913952012-06-29 12:54:53 +02002776static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002777 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2778 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2779 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002780};
2781
2782typedef struct
2783{
2784 PyObject_HEAD
2785 char_u *name;
2786} FunctionObject;
2787
2788static PyTypeObject FunctionType;
2789
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002790#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2791
Bram Moolenaardb913952012-06-29 12:54:53 +02002792 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002793FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002794{
2795 FunctionObject *self;
2796
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002797 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2798
Bram Moolenaardb913952012-06-29 12:54:53 +02002799 if (self == NULL)
2800 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002801
2802 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002803 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002804 if (!translated_function_exists(name))
2805 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002806 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002807 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002808 return NULL;
2809 }
2810 self->name = vim_strsave(name);
2811 func_ref(self->name);
2812 }
2813 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002814 if ((self->name = get_expanded_name(name,
2815 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2816 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002817 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002818 PyErr_FORMAT(PyExc_ValueError,
2819 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002820 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002821 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002822
2823 return (PyObject *)(self);
2824}
2825
2826 static PyObject *
2827FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2828{
2829 PyObject *self;
2830 char_u *name;
2831
2832 if (kwargs)
2833 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002834 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002835 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002836 return NULL;
2837 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002838
Bram Moolenaar389a1792013-06-23 13:00:44 +02002839 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002840 return NULL;
2841
2842 self = FunctionNew(subtype, name);
2843
Bram Moolenaar389a1792013-06-23 13:00:44 +02002844 PyMem_Free(name);
2845
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002846 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002847}
2848
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002849 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002850FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002851{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002852 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002853 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002854
2855 DESTRUCTOR_FINISH(self);
2856}
2857
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002858static char *FunctionAttrs[] = {
2859 "softspace",
2860 NULL
2861};
2862
2863 static PyObject *
2864FunctionDir(PyObject *self)
2865{
2866 return ObjectDir(self, FunctionAttrs);
2867}
2868
Bram Moolenaardb913952012-06-29 12:54:53 +02002869 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002870FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002871{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002872 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002873 typval_T args;
2874 typval_T selfdicttv;
2875 typval_T rettv;
2876 dict_T *selfdict = NULL;
2877 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002878 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002879 int error;
2880
2881 if (ConvertFromPyObject(argsObject, &args) == -1)
2882 return NULL;
2883
2884 if (kwargs != NULL)
2885 {
2886 selfdictObject = PyDict_GetItemString(kwargs, "self");
2887 if (selfdictObject != NULL)
2888 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002889 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002890 {
2891 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002892 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002893 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002894 selfdict = selfdicttv.vval.v_dict;
2895 }
2896 }
2897
Bram Moolenaar71700b82013-05-15 17:49:05 +02002898 Py_BEGIN_ALLOW_THREADS
2899 Python_Lock_Vim();
2900
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002901 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002902 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002903
2904 Python_Release_Vim();
2905 Py_END_ALLOW_THREADS
2906
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002907 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002908 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002909 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002910 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002911 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002912 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002913 }
2914 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002915 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002916
Bram Moolenaardb913952012-06-29 12:54:53 +02002917 clear_tv(&args);
2918 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002919 if (selfdict != NULL)
2920 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002921
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002922 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002923}
2924
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002925 static PyObject *
2926FunctionRepr(FunctionObject *self)
2927{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002928#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002929 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002930 * Finalize */
2931 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002932 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002933#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002934 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002935#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002936}
2937
Bram Moolenaardb913952012-06-29 12:54:53 +02002938static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002939 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2940 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002941};
2942
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002943/*
2944 * Options object
2945 */
2946
2947static PyTypeObject OptionsType;
2948
2949typedef int (*checkfun)(void *);
2950
2951typedef struct
2952{
2953 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002954 int opt_type;
2955 void *from;
2956 checkfun Check;
2957 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002958} OptionsObject;
2959
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002960 static int
2961dummy_check(void *arg UNUSED)
2962{
2963 return 0;
2964}
2965
2966 static PyObject *
2967OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2968{
2969 OptionsObject *self;
2970
Bram Moolenaar774267b2013-05-21 20:51:59 +02002971 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002972 if (self == NULL)
2973 return NULL;
2974
2975 self->opt_type = opt_type;
2976 self->from = from;
2977 self->Check = Check;
2978 self->fromObj = fromObj;
2979 if (fromObj)
2980 Py_INCREF(fromObj);
2981
2982 return (PyObject *)(self);
2983}
2984
2985 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002986OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002987{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002988 PyObject_GC_UnTrack((void *)(self));
2989 Py_XDECREF(self->fromObj);
2990 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002991}
2992
2993 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002994OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002995{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002996 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002997 return 0;
2998}
2999
3000 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003001OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003002{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003003 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003004 return 0;
3005}
3006
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003007 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003008OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003009{
3010 char_u *key;
3011 int flags;
3012 long numval;
3013 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003014 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003015
Bram Moolenaard6e39182013-05-21 18:30:34 +02003016 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003017 return NULL;
3018
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003019 if (!(key = StringToChars(keyObject, &todecref)))
3020 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003021
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003022 if (*key == NUL)
3023 {
3024 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003025 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003026 return NULL;
3027 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003028
3029 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003030 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003031
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003032 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003033
3034 if (flags == 0)
3035 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003036 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003037 return NULL;
3038 }
3039
3040 if (flags & SOPT_UNSET)
3041 {
3042 Py_INCREF(Py_None);
3043 return Py_None;
3044 }
3045 else if (flags & SOPT_BOOL)
3046 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003047 PyObject *ret;
3048 ret = numval ? Py_True : Py_False;
3049 Py_INCREF(ret);
3050 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003051 }
3052 else if (flags & SOPT_NUM)
3053 return PyInt_FromLong(numval);
3054 else if (flags & SOPT_STRING)
3055 {
3056 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003057 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003058 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003059 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003060 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003061 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003062 else
3063 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003064 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003065 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003066 return NULL;
3067 }
3068 }
3069 else
3070 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003071 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003072 return NULL;
3073 }
3074}
3075
3076 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003077OptionsContains(OptionsObject *self, PyObject *keyObject)
3078{
3079 char_u *key;
3080 PyObject *todecref;
3081
3082 if (!(key = StringToChars(keyObject, &todecref)))
3083 return -1;
3084
3085 if (*key == NUL)
3086 {
3087 Py_XDECREF(todecref);
3088 return 0;
3089 }
3090
3091 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3092 {
3093 Py_XDECREF(todecref);
3094 return 1;
3095 }
3096 else
3097 {
3098 Py_XDECREF(todecref);
3099 return 0;
3100 }
3101}
3102
3103typedef struct
3104{
3105 void *lastoption;
3106 int opt_type;
3107} optiterinfo_T;
3108
3109 static PyObject *
3110OptionsIterNext(optiterinfo_T **oii)
3111{
3112 char_u *name;
3113
3114 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3115 return PyString_FromString((char *)name);
3116
3117 return NULL;
3118}
3119
3120 static PyObject *
3121OptionsIter(OptionsObject *self)
3122{
3123 optiterinfo_T *oii;
3124
3125 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3126 {
3127 PyErr_NoMemory();
3128 return NULL;
3129 }
3130
3131 oii->opt_type = self->opt_type;
3132 oii->lastoption = NULL;
3133
3134 return IterNew(oii,
3135 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3136 NULL, NULL);
3137}
3138
3139 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003140set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003141{
3142 char_u *errmsg;
3143
3144 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3145 {
3146 if (VimTryEnd())
3147 return FAIL;
3148 PyErr_SetVim((char *)errmsg);
3149 return FAIL;
3150 }
3151 return OK;
3152}
3153
3154 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003155set_option_value_for(
3156 char_u *key,
3157 int numval,
3158 char_u *stringval,
3159 int opt_flags,
3160 int opt_type,
3161 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003162{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003163 win_T *save_curwin = NULL;
3164 tabpage_T *save_curtab = NULL;
3165 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003166 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003167
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003168 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003169 switch (opt_type)
3170 {
3171 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003172 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003173 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003174 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003175 if (VimTryEnd())
3176 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003177 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003178 return -1;
3179 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003180 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3181 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003182 break;
3183 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003184 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003185 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003186 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003187 break;
3188 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003189 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003190 break;
3191 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003192 if (set_ret == FAIL)
3193 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003194 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003195}
3196
3197 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003198OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003199{
3200 char_u *key;
3201 int flags;
3202 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003203 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003204 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003205
Bram Moolenaard6e39182013-05-21 18:30:34 +02003206 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003207 return -1;
3208
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003209 if (!(key = StringToChars(keyObject, &todecref)))
3210 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003211
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003212 if (*key == NUL)
3213 {
3214 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003215 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003216 return -1;
3217 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003218
3219 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003220 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003221
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003222 if (flags == 0)
3223 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003224 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003225 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003226 return -1;
3227 }
3228
3229 if (valObject == NULL)
3230 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003231 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003232 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003233 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003234 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003235 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003236 return -1;
3237 }
3238 else if (!(flags & SOPT_GLOBAL))
3239 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003240 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003241 N_("unable to unset option %s "
3242 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003243 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003244 return -1;
3245 }
3246 else
3247 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003248 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003249 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003250 return 0;
3251 }
3252 }
3253
Bram Moolenaard6e39182013-05-21 18:30:34 +02003254 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003255
3256 if (flags & SOPT_BOOL)
3257 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003258 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003259
Bram Moolenaarb983f752013-05-15 16:11:50 +02003260 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003261 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003262 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003263 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003264 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003265 }
3266 else if (flags & SOPT_NUM)
3267 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003268 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003269
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003270 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003271 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003272 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003273 return -1;
3274 }
3275
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003276 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003278 }
3279 else
3280 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003281 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003282 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003283
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003284 if ((val = StringToChars(valObject, &todecref2)))
3285 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003286 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003287 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003288 Py_XDECREF(todecref2);
3289 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003290 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003291 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003292 }
3293
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003294 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003295
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003296 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003297}
3298
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003299static PySequenceMethods OptionsAsSeq = {
3300 0, /* sq_length */
3301 0, /* sq_concat */
3302 0, /* sq_repeat */
3303 0, /* sq_item */
3304 0, /* sq_slice */
3305 0, /* sq_ass_item */
3306 0, /* sq_ass_slice */
3307 (objobjproc) OptionsContains, /* sq_contains */
3308 0, /* sq_inplace_concat */
3309 0, /* sq_inplace_repeat */
3310};
3311
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003312static PyMappingMethods OptionsAsMapping = {
3313 (lenfunc) NULL,
3314 (binaryfunc) OptionsItem,
3315 (objobjargproc) OptionsAssItem,
3316};
3317
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003318/* Tabpage object
3319 */
3320
3321typedef struct
3322{
3323 PyObject_HEAD
3324 tabpage_T *tab;
3325} TabPageObject;
3326
3327static PyObject *WinListNew(TabPageObject *tabObject);
3328
3329static PyTypeObject TabPageType;
3330
3331 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003332CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003333{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003335 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003336 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003337 return -1;
3338 }
3339
3340 return 0;
3341}
3342
3343 static PyObject *
3344TabPageNew(tabpage_T *tab)
3345{
3346 TabPageObject *self;
3347
3348 if (TAB_PYTHON_REF(tab))
3349 {
3350 self = TAB_PYTHON_REF(tab);
3351 Py_INCREF(self);
3352 }
3353 else
3354 {
3355 self = PyObject_NEW(TabPageObject, &TabPageType);
3356 if (self == NULL)
3357 return NULL;
3358 self->tab = tab;
3359 TAB_PYTHON_REF(tab) = self;
3360 }
3361
3362 return (PyObject *)(self);
3363}
3364
3365 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003367{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003368 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3369 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003370
3371 DESTRUCTOR_FINISH(self);
3372}
3373
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003374static char *TabPageAttrs[] = {
3375 "windows", "number", "vars", "window", "valid",
3376 NULL
3377};
3378
3379 static PyObject *
3380TabPageDir(PyObject *self)
3381{
3382 return ObjectDir(self, TabPageAttrs);
3383}
3384
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003385 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003386TabPageAttrValid(TabPageObject *self, char *name)
3387{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003388 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003389
3390 if (strcmp(name, "valid") != 0)
3391 return NULL;
3392
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003393 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3394 Py_INCREF(ret);
3395 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003396}
3397
3398 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003399TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003400{
3401 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003402 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003403 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003404 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003405 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003406 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003407 else if (strcmp(name, "window") == 0)
3408 {
3409 /* For current tab window.c does not bother to set or update tp_curwin
3410 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003411 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003412 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003413 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003414 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003415 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003416 else if (strcmp(name, "__members__") == 0)
3417 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003418 return NULL;
3419}
3420
3421 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003422TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003423{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003424 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003425 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003426 else
3427 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003428 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003429
3430 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003431 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3432 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003433 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003434 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003435 }
3436}
3437
3438static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003439 /* name, function, calling, documentation */
3440 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3441 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003442};
3443
3444/*
3445 * Window list object
3446 */
3447
3448static PyTypeObject TabListType;
3449static PySequenceMethods TabListAsSeq;
3450
3451typedef struct
3452{
3453 PyObject_HEAD
3454} TabListObject;
3455
3456 static PyInt
3457TabListLength(PyObject *self UNUSED)
3458{
3459 tabpage_T *tp = first_tabpage;
3460 PyInt n = 0;
3461
3462 while (tp != NULL)
3463 {
3464 ++n;
3465 tp = tp->tp_next;
3466 }
3467
3468 return n;
3469}
3470
3471 static PyObject *
3472TabListItem(PyObject *self UNUSED, PyInt n)
3473{
3474 tabpage_T *tp;
3475
3476 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3477 if (n == 0)
3478 return TabPageNew(tp);
3479
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003480 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003481 return NULL;
3482}
3483
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003484/*
3485 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003486 */
3487
3488typedef struct
3489{
3490 PyObject_HEAD
3491 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003492 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003493} WindowObject;
3494
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003495static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003496
3497 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003498CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003499{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003500 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003501 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003502 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003503 return -1;
3504 }
3505
3506 return 0;
3507}
3508
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003509 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003510WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003511{
3512 /* We need to handle deletion of windows underneath us.
3513 * If we add a "w_python*_ref" field to the win_T structure,
3514 * then we can get at it in win_free() in vim. We then
3515 * need to create only ONE Python object per window - if
3516 * we try to create a second, just INCREF the existing one
3517 * and return it. The (single) Python object referring to
3518 * the window is stored in "w_python*_ref".
3519 * On a win_free() we set the Python object's win_T* field
3520 * to an invalid value. We trap all uses of a window
3521 * object, and reject them if the win_T* field is invalid.
3522 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003523 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003524 * w_python_ref and w_python3_ref fields respectively.
3525 */
3526
3527 WindowObject *self;
3528
3529 if (WIN_PYTHON_REF(win))
3530 {
3531 self = WIN_PYTHON_REF(win);
3532 Py_INCREF(self);
3533 }
3534 else
3535 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003536 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003537 if (self == NULL)
3538 return NULL;
3539 self->win = win;
3540 WIN_PYTHON_REF(win) = self;
3541 }
3542
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003543 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3544
Bram Moolenaar971db462013-05-12 18:44:48 +02003545 return (PyObject *)(self);
3546}
3547
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003548 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003549WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003550{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003551 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003552 if (self->win && self->win != INVALID_WINDOW_VALUE)
3553 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003554 Py_XDECREF(((PyObject *)(self->tabObject)));
3555 PyObject_GC_Del((void *)(self));
3556}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003557
Bram Moolenaar774267b2013-05-21 20:51:59 +02003558 static int
3559WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3560{
3561 Py_VISIT(((PyObject *)(self->tabObject)));
3562 return 0;
3563}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003564
Bram Moolenaar774267b2013-05-21 20:51:59 +02003565 static int
3566WindowClear(WindowObject *self)
3567{
3568 Py_CLEAR(self->tabObject);
3569 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003570}
3571
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003572 static win_T *
3573get_firstwin(TabPageObject *tabObject)
3574{
3575 if (tabObject)
3576 {
3577 if (CheckTabPage(tabObject))
3578 return NULL;
3579 /* For current tab window.c does not bother to set or update tp_firstwin
3580 */
3581 else if (tabObject->tab == curtab)
3582 return firstwin;
3583 else
3584 return tabObject->tab->tp_firstwin;
3585 }
3586 else
3587 return firstwin;
3588}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003589static char *WindowAttrs[] = {
3590 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3591 "tabpage", "valid",
3592 NULL
3593};
3594
3595 static PyObject *
3596WindowDir(PyObject *self)
3597{
3598 return ObjectDir(self, WindowAttrs);
3599}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003600
Bram Moolenaar971db462013-05-12 18:44:48 +02003601 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003602WindowAttrValid(WindowObject *self, char *name)
3603{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003604 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003605
3606 if (strcmp(name, "valid") != 0)
3607 return NULL;
3608
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003609 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3610 Py_INCREF(ret);
3611 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003612}
3613
3614 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003615WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003616{
3617 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003619 else if (strcmp(name, "cursor") == 0)
3620 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003621 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003622
3623 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3624 }
3625 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003626 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003627#ifdef FEAT_WINDOWS
3628 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003629 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003630#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003631#ifdef FEAT_VERTSPLIT
3632 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003633 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003634 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003635 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003636#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003637 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003638 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003639 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003640 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3641 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003642 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003643 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003644 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003645 return NULL;
3646 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003647 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003648 }
3649 else if (strcmp(name, "tabpage") == 0)
3650 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003651 Py_INCREF(self->tabObject);
3652 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003653 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003654 else if (strcmp(name, "__members__") == 0)
3655 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003656 else
3657 return NULL;
3658}
3659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003660 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003661WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003662{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003663 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003664 return -1;
3665
3666 if (strcmp(name, "buffer") == 0)
3667 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003668 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003669 return -1;
3670 }
3671 else if (strcmp(name, "cursor") == 0)
3672 {
3673 long lnum;
3674 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003675
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003676 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003677 return -1;
3678
Bram Moolenaard6e39182013-05-21 18:30:34 +02003679 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003680 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003681 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003682 return -1;
3683 }
3684
3685 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003686 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003687 return -1;
3688
Bram Moolenaard6e39182013-05-21 18:30:34 +02003689 self->win->w_cursor.lnum = lnum;
3690 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003692 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003693#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003694 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003695 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003697 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003698 return 0;
3699 }
3700 else if (strcmp(name, "height") == 0)
3701 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003702 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003703 win_T *savewin;
3704
Bram Moolenaardee2e312013-06-23 16:35:47 +02003705 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706 return -1;
3707
3708#ifdef FEAT_GUI
3709 need_mouse_correct = TRUE;
3710#endif
3711 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003712 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003713
3714 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003715 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003716 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003717 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718 return -1;
3719
3720 return 0;
3721 }
3722#ifdef FEAT_VERTSPLIT
3723 else if (strcmp(name, "width") == 0)
3724 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003725 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003726 win_T *savewin;
3727
Bram Moolenaardee2e312013-06-23 16:35:47 +02003728 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003729 return -1;
3730
3731#ifdef FEAT_GUI
3732 need_mouse_correct = TRUE;
3733#endif
3734 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003735 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003736
3737 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003738 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003739 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003740 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003741 return -1;
3742
3743 return 0;
3744 }
3745#endif
3746 else
3747 {
3748 PyErr_SetString(PyExc_AttributeError, name);
3749 return -1;
3750 }
3751}
3752
3753 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003754WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003755{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003756 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003757 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003758 else
3759 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003760 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003761
Bram Moolenaar6d216452013-05-12 19:00:41 +02003762 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003763 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003764 (self));
3765 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003766 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003767 }
3768}
3769
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003770static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003771 /* name, function, calling, documentation */
3772 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3773 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003774};
3775
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003777 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003778 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003779
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003780static PyTypeObject WinListType;
3781static PySequenceMethods WinListAsSeq;
3782
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003783typedef struct
3784{
3785 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003786 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003787} WinListObject;
3788
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003789 static PyObject *
3790WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003791{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003792 WinListObject *self;
3793
3794 self = PyObject_NEW(WinListObject, &WinListType);
3795 self->tabObject = tabObject;
3796 Py_INCREF(tabObject);
3797
3798 return (PyObject *)(self);
3799}
3800
3801 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003802WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003803{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003804 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003805
3806 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003807 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003808 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003809 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003810
3811 DESTRUCTOR_FINISH(self);
3812}
3813
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003814 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003815WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003816{
3817 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003818 PyInt n = 0;
3819
Bram Moolenaard6e39182013-05-21 18:30:34 +02003820 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003821 return -1;
3822
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003823 while (w != NULL)
3824 {
3825 ++n;
3826 w = W_NEXT(w);
3827 }
3828
3829 return n;
3830}
3831
3832 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003833WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003834{
3835 win_T *w;
3836
Bram Moolenaard6e39182013-05-21 18:30:34 +02003837 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003838 return NULL;
3839
3840 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003842 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003843
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003844 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003845 return NULL;
3846}
3847
3848/* Convert a Python string into a Vim line.
3849 *
3850 * The result is in allocated memory. All internal nulls are replaced by
3851 * newline characters. It is an error for the string to contain newline
3852 * characters.
3853 *
3854 * On errors, the Python exception data is set, and NULL is returned.
3855 */
3856 static char *
3857StringToLine(PyObject *obj)
3858{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003859 char *str;
3860 char *save;
3861 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003862 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003863 PyInt i;
3864 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003865
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003866 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003867 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003868 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3869 || str == NULL)
3870 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003871 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003872 else if (PyUnicode_Check(obj))
3873 {
3874 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3875 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003876
Bram Moolenaardaa27022013-06-24 22:33:30 +02003877 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003878 || str == NULL)
3879 {
3880 Py_DECREF(bytes);
3881 return NULL;
3882 }
3883 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003884 else
3885 {
3886#if PY_MAJOR_VERSION < 3
3887 PyErr_FORMAT(PyExc_TypeError,
3888 N_("expected str() or unicode() instance, but got %s"),
3889 Py_TYPE_NAME(obj));
3890#else
3891 PyErr_FORMAT(PyExc_TypeError,
3892 N_("expected bytes() or str() instance, but got %s"),
3893 Py_TYPE_NAME(obj));
3894#endif
3895 return NULL;
3896 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003897
3898 /*
3899 * Error checking: String must not contain newlines, as we
3900 * are replacing a single line, and we must replace it with
3901 * a single line.
3902 * A trailing newline is removed, so that append(f.readlines()) works.
3903 */
3904 p = memchr(str, '\n', len);
3905 if (p != NULL)
3906 {
3907 if (p == str + len - 1)
3908 --len;
3909 else
3910 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003911 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003912 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003913 return NULL;
3914 }
3915 }
3916
3917 /* Create a copy of the string, with internal nulls replaced by
3918 * newline characters, as is the vim convention.
3919 */
3920 save = (char *)alloc((unsigned)(len+1));
3921 if (save == NULL)
3922 {
3923 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003924 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003925 return NULL;
3926 }
3927
3928 for (i = 0; i < len; ++i)
3929 {
3930 if (str[i] == '\0')
3931 save[i] = '\n';
3932 else
3933 save[i] = str[i];
3934 }
3935
3936 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003937 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003938
3939 return save;
3940}
3941
3942/* Get a line from the specified buffer. The line number is
3943 * in Vim format (1-based). The line is returned as a Python
3944 * string object.
3945 */
3946 static PyObject *
3947GetBufferLine(buf_T *buf, PyInt n)
3948{
3949 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3950}
3951
3952
3953/* Get a list of lines from the specified buffer. The line numbers
3954 * are in Vim format (1-based). The range is from lo up to, but not
3955 * including, hi. The list is returned as a Python list of string objects.
3956 */
3957 static PyObject *
3958GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3959{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003960 PyInt i;
3961 PyInt n = hi - lo;
3962 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003963
3964 if (list == NULL)
3965 return NULL;
3966
3967 for (i = 0; i < n; ++i)
3968 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003969 PyObject *string = LineToString(
3970 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003971
3972 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003973 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003974 {
3975 Py_DECREF(list);
3976 return NULL;
3977 }
3978
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003979 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003980 }
3981
3982 /* The ownership of the Python list is passed to the caller (ie,
3983 * the caller should Py_DECREF() the object when it is finished
3984 * with it).
3985 */
3986
3987 return list;
3988}
3989
3990/*
3991 * Check if deleting lines made the cursor position invalid.
3992 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3993 * deleted).
3994 */
3995 static void
3996py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3997{
3998 if (curwin->w_cursor.lnum >= lo)
3999 {
4000 /* Adjust the cursor position if it's in/after the changed
4001 * lines. */
4002 if (curwin->w_cursor.lnum >= hi)
4003 {
4004 curwin->w_cursor.lnum += extra;
4005 check_cursor_col();
4006 }
4007 else if (extra < 0)
4008 {
4009 curwin->w_cursor.lnum = lo;
4010 check_cursor();
4011 }
4012 else
4013 check_cursor_col();
4014 changed_cline_bef_curs();
4015 }
4016 invalidate_botline();
4017}
4018
Bram Moolenaar19e60942011-06-19 00:27:51 +02004019/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004020 * Find a window that contains "buf" and switch to it.
4021 * If there is no such window, use the current window and change "curbuf".
4022 * Caller must initialize save_curbuf to NULL.
4023 * restore_win_for_buf() MUST be called later!
4024 */
4025 static void
4026switch_to_win_for_buf(
4027 buf_T *buf,
4028 win_T **save_curwinp,
4029 tabpage_T **save_curtabp,
4030 buf_T **save_curbufp)
4031{
4032 win_T *wp;
4033 tabpage_T *tp;
4034
4035 if (find_win_for_buf(buf, &wp, &tp) == FAIL
4036 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4037 switch_buffer(save_curbufp, buf);
4038}
4039
4040 static void
4041restore_win_for_buf(
4042 win_T *save_curwin,
4043 tabpage_T *save_curtab,
4044 buf_T *save_curbuf)
4045{
4046 if (save_curbuf == NULL)
4047 restore_win(save_curwin, save_curtab, TRUE);
4048 else
4049 restore_buffer(save_curbuf);
4050}
4051
4052/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004053 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 * in Vim format (1-based). The replacement line is given as
4055 * a Python string object. The object is checked for validity
4056 * and correct format. Errors are returned as a value of FAIL.
4057 * The return value is OK on success.
4058 * If OK is returned and len_change is not NULL, *len_change
4059 * is set to the change in the buffer length.
4060 */
4061 static int
4062SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4063{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004064 buf_T *save_curbuf = NULL;
4065 win_T *save_curwin = NULL;
4066 tabpage_T *save_curtab = NULL;
4067
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004068 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 * There are three cases:
4070 * 1. NULL, or None - this is a deletion.
4071 * 2. A string - this is a replacement.
4072 * 3. Anything else - this is an error.
4073 */
4074 if (line == Py_None || line == NULL)
4075 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004077 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004079 VimTryStart();
4080
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004082 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004084 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 else
4086 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004087 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004089 if (save_curbuf == NULL)
4090 /* Only adjust marks if we managed to switch to a window that
4091 * holds the buffer, otherwise line numbers will be invalid. */
4092 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 }
4094
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004095 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004097 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 return FAIL;
4099
4100 if (len_change)
4101 *len_change = -1;
4102
4103 return OK;
4104 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004105 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004107 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108
4109 if (save == NULL)
4110 return FAIL;
4111
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004112 VimTryStart();
4113
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 /* We do not need to free "save" if ml_replace() consumes it. */
4115 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004116 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004117
4118 if (u_savesub((linenr_T)n) == FAIL)
4119 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004120 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004121 vim_free(save);
4122 }
4123 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4124 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004125 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126 vim_free(save);
4127 }
4128 else
4129 changed_bytes((linenr_T)n, 0);
4130
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004131 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004132
4133 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004134 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 check_cursor_col();
4136
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004137 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138 return FAIL;
4139
4140 if (len_change)
4141 *len_change = 0;
4142
4143 return OK;
4144 }
4145 else
4146 {
4147 PyErr_BadArgument();
4148 return FAIL;
4149 }
4150}
4151
Bram Moolenaar19e60942011-06-19 00:27:51 +02004152/* Replace a range of lines in the specified buffer. The line numbers are in
4153 * Vim format (1-based). The range is from lo up to, but not including, hi.
4154 * The replacement lines are given as a Python list of string objects. The
4155 * list is checked for validity and correct format. Errors are returned as a
4156 * value of FAIL. The return value is OK on success.
4157 * If OK is returned and len_change is not NULL, *len_change
4158 * is set to the change in the buffer length.
4159 */
4160 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004161SetBufferLineList(
4162 buf_T *buf,
4163 PyInt lo,
4164 PyInt hi,
4165 PyObject *list,
4166 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004167{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004168 buf_T *save_curbuf = NULL;
4169 win_T *save_curwin = NULL;
4170 tabpage_T *save_curtab = NULL;
4171
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004172 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004173 * There are three cases:
4174 * 1. NULL, or None - this is a deletion.
4175 * 2. A list - this is a replacement.
4176 * 3. Anything else - this is an error.
4177 */
4178 if (list == Py_None || list == NULL)
4179 {
4180 PyInt i;
4181 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004182
4183 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004184 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004185 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004186
4187 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004188 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004189 else
4190 {
4191 for (i = 0; i < n; ++i)
4192 {
4193 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4194 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004195 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004196 break;
4197 }
4198 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004199 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004200 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004201 if (save_curbuf == NULL)
4202 /* Only adjust marks if we managed to switch to a window that
4203 * holds the buffer, otherwise line numbers will be invalid. */
4204 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004205 }
4206
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004207 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004208
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004209 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004210 return FAIL;
4211
4212 if (len_change)
4213 *len_change = -n;
4214
4215 return OK;
4216 }
4217 else if (PyList_Check(list))
4218 {
4219 PyInt i;
4220 PyInt new_len = PyList_Size(list);
4221 PyInt old_len = hi - lo;
4222 PyInt extra = 0; /* lines added to text, can be negative */
4223 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004224
4225 if (new_len == 0) /* avoid allocating zero bytes */
4226 array = NULL;
4227 else
4228 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004229 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004230 if (array == NULL)
4231 {
4232 PyErr_NoMemory();
4233 return FAIL;
4234 }
4235 }
4236
4237 for (i = 0; i < new_len; ++i)
4238 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004239 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004240
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004241 if (!(line = PyList_GetItem(list, i)) ||
4242 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004243 {
4244 while (i)
4245 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004246 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004247 return FAIL;
4248 }
4249 }
4250
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004251 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004252 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004253
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004254 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004255 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004256
4257 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004258 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004259
4260 /* If the size of the range is reducing (ie, new_len < old_len) we
4261 * need to delete some old_len. We do this at the start, by
4262 * repeatedly deleting line "lo".
4263 */
4264 if (!PyErr_Occurred())
4265 {
4266 for (i = 0; i < old_len - new_len; ++i)
4267 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4268 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004269 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004270 break;
4271 }
4272 extra -= i;
4273 }
4274
4275 /* For as long as possible, replace the existing old_len with the
4276 * new old_len. This is a more efficient operation, as it requires
4277 * less memory allocation and freeing.
4278 */
4279 if (!PyErr_Occurred())
4280 {
4281 for (i = 0; i < old_len && i < new_len; ++i)
4282 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4283 == FAIL)
4284 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004285 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004286 break;
4287 }
4288 }
4289 else
4290 i = 0;
4291
4292 /* Now we may need to insert the remaining new old_len. If we do, we
4293 * must free the strings as we finish with them (we can't pass the
4294 * responsibility to vim in this case).
4295 */
4296 if (!PyErr_Occurred())
4297 {
4298 while (i < new_len)
4299 {
4300 if (ml_append((linenr_T)(lo + i - 1),
4301 (char_u *)array[i], 0, FALSE) == FAIL)
4302 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004303 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004304 break;
4305 }
4306 vim_free(array[i]);
4307 ++i;
4308 ++extra;
4309 }
4310 }
4311
4312 /* Free any left-over old_len, as a result of an error */
4313 while (i < new_len)
4314 {
4315 vim_free(array[i]);
4316 ++i;
4317 }
4318
4319 /* Free the array of old_len. All of its contents have now
4320 * been dealt with (either freed, or the responsibility passed
4321 * to vim.
4322 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004323 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004324
4325 /* Adjust marks. Invalidate any which lie in the
4326 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004327 * Only adjust marks if we managed to switch to a window that holds
4328 * the buffer, otherwise line numbers will be invalid. */
4329 if (save_curbuf == NULL)
4330 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004331 (long)MAXLNUM, (long)extra);
4332 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4333
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004334 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004335 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4336
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004337 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004338 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004339
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004340 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004341 return FAIL;
4342
4343 if (len_change)
4344 *len_change = new_len - old_len;
4345
4346 return OK;
4347 }
4348 else
4349 {
4350 PyErr_BadArgument();
4351 return FAIL;
4352 }
4353}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004354
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004355/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004356 * The line number is in Vim format (1-based). The lines to be inserted are
4357 * given as a Python list of string objects or as a single string. The lines
4358 * to be added are checked for validity and correct format. Errors are
4359 * returned as a value of FAIL. The return value is OK on success.
4360 * If OK is returned and len_change is not NULL, *len_change
4361 * is set to the change in the buffer length.
4362 */
4363 static int
4364InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4365{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004366 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004367 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004368 tabpage_T *save_curtab = NULL;
4369
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004370 /* First of all, we check the type of the supplied Python object.
4371 * It must be a string or a list, or the call is in error.
4372 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004373 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004374 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004375 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004376
4377 if (str == NULL)
4378 return FAIL;
4379
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004380 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004381 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004382 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004384 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004385 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004386 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004387 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004388 else if (save_curbuf == NULL)
4389 /* Only adjust marks if we managed to switch to a window that
4390 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004391 appended_lines_mark((linenr_T)n, 1L);
4392
4393 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004394 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004395 update_screen(VALID);
4396
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004397 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004398 return FAIL;
4399
4400 if (len_change)
4401 *len_change = 1;
4402
4403 return OK;
4404 }
4405 else if (PyList_Check(lines))
4406 {
4407 PyInt i;
4408 PyInt size = PyList_Size(lines);
4409 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004410
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004411 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412 if (array == NULL)
4413 {
4414 PyErr_NoMemory();
4415 return FAIL;
4416 }
4417
4418 for (i = 0; i < size; ++i)
4419 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004420 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004421
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004422 if (!(line = PyList_GetItem(lines, i)) ||
4423 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004424 {
4425 while (i)
4426 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004427 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428 return FAIL;
4429 }
4430 }
4431
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004433 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004434 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435
4436 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004437 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 else
4439 {
4440 for (i = 0; i < size; ++i)
4441 {
4442 if (ml_append((linenr_T)(n + i),
4443 (char_u *)array[i], 0, FALSE) == FAIL)
4444 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004445 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446
4447 /* Free the rest of the lines */
4448 while (i < size)
4449 vim_free(array[i++]);
4450
4451 break;
4452 }
4453 vim_free(array[i]);
4454 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004455 if (i > 0 && save_curbuf == NULL)
4456 /* Only adjust marks if we managed to switch to a window that
4457 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004458 appended_lines_mark((linenr_T)n, (long)i);
4459 }
4460
4461 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004462 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004463 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004464 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004466 update_screen(VALID);
4467
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004468 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004469 return FAIL;
4470
4471 if (len_change)
4472 *len_change = size;
4473
4474 return OK;
4475 }
4476 else
4477 {
4478 PyErr_BadArgument();
4479 return FAIL;
4480 }
4481}
4482
4483/*
4484 * Common routines for buffers and line ranges
4485 * -------------------------------------------
4486 */
4487
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004488typedef struct
4489{
4490 PyObject_HEAD
4491 buf_T *buf;
4492} BufferObject;
4493
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004494 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004495CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004496{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004497 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004498 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004499 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004500 return -1;
4501 }
4502
4503 return 0;
4504}
4505
4506 static PyObject *
4507RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4508{
4509 if (CheckBuffer(self))
4510 return NULL;
4511
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004512 if (end == -1)
4513 end = self->buf->b_ml.ml_line_count;
4514
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004515 if (n < 0)
4516 n += end - start + 1;
4517
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004518 if (n < 0 || n > end - start)
4519 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004520 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004521 return NULL;
4522 }
4523
4524 return GetBufferLine(self->buf, n+start);
4525}
4526
4527 static PyObject *
4528RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4529{
4530 PyInt size;
4531
4532 if (CheckBuffer(self))
4533 return NULL;
4534
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004535 if (end == -1)
4536 end = self->buf->b_ml.ml_line_count;
4537
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004538 size = end - start + 1;
4539
4540 if (lo < 0)
4541 lo = 0;
4542 else if (lo > size)
4543 lo = size;
4544 if (hi < 0)
4545 hi = 0;
4546 if (hi < lo)
4547 hi = lo;
4548 else if (hi > size)
4549 hi = size;
4550
4551 return GetBufferLineList(self->buf, lo+start, hi+start);
4552}
4553
4554 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004555RBAsItem(
4556 BufferObject *self,
4557 PyInt n,
4558 PyObject *valObject,
4559 PyInt start,
4560 PyInt end,
4561 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004562{
4563 PyInt len_change;
4564
4565 if (CheckBuffer(self))
4566 return -1;
4567
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004568 if (end == -1)
4569 end = self->buf->b_ml.ml_line_count;
4570
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004571 if (n < 0)
4572 n += end - start + 1;
4573
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004574 if (n < 0 || n > end - start)
4575 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004576 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004577 return -1;
4578 }
4579
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004580 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004581 return -1;
4582
4583 if (new_end)
4584 *new_end = end + len_change;
4585
4586 return 0;
4587}
4588
Bram Moolenaar19e60942011-06-19 00:27:51 +02004589 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004590RBAsSlice(
4591 BufferObject *self,
4592 PyInt lo,
4593 PyInt hi,
4594 PyObject *valObject,
4595 PyInt start,
4596 PyInt end,
4597 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004598{
4599 PyInt size;
4600 PyInt len_change;
4601
4602 /* Self must be a valid buffer */
4603 if (CheckBuffer(self))
4604 return -1;
4605
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004606 if (end == -1)
4607 end = self->buf->b_ml.ml_line_count;
4608
Bram Moolenaar19e60942011-06-19 00:27:51 +02004609 /* Sort out the slice range */
4610 size = end - start + 1;
4611
4612 if (lo < 0)
4613 lo = 0;
4614 else if (lo > size)
4615 lo = size;
4616 if (hi < 0)
4617 hi = 0;
4618 if (hi < lo)
4619 hi = lo;
4620 else if (hi > size)
4621 hi = size;
4622
4623 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004624 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004625 return -1;
4626
4627 if (new_end)
4628 *new_end = end + len_change;
4629
4630 return 0;
4631}
4632
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004633
4634 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004635RBAppend(
4636 BufferObject *self,
4637 PyObject *args,
4638 PyInt start,
4639 PyInt end,
4640 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004641{
4642 PyObject *lines;
4643 PyInt len_change;
4644 PyInt max;
4645 PyInt n;
4646
4647 if (CheckBuffer(self))
4648 return NULL;
4649
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004650 if (end == -1)
4651 end = self->buf->b_ml.ml_line_count;
4652
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004653 max = n = end - start + 1;
4654
4655 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4656 return NULL;
4657
4658 if (n < 0 || n > max)
4659 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004660 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004661 return NULL;
4662 }
4663
4664 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4665 return NULL;
4666
4667 if (new_end)
4668 *new_end = end + len_change;
4669
4670 Py_INCREF(Py_None);
4671 return Py_None;
4672}
4673
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004674/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675 */
4676
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004677static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004678static PySequenceMethods RangeAsSeq;
4679static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004680
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004681typedef struct
4682{
4683 PyObject_HEAD
4684 BufferObject *buf;
4685 PyInt start;
4686 PyInt end;
4687} RangeObject;
4688
4689 static PyObject *
4690RangeNew(buf_T *buf, PyInt start, PyInt end)
4691{
4692 BufferObject *bufr;
4693 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004694 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004695 if (self == NULL)
4696 return NULL;
4697
4698 bufr = (BufferObject *)BufferNew(buf);
4699 if (bufr == NULL)
4700 {
4701 Py_DECREF(self);
4702 return NULL;
4703 }
4704 Py_INCREF(bufr);
4705
4706 self->buf = bufr;
4707 self->start = start;
4708 self->end = end;
4709
4710 return (PyObject *)(self);
4711}
4712
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004713 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004714RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004715{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004716 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004717 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004718 PyObject_GC_Del((void *)(self));
4719}
4720
4721 static int
4722RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4723{
4724 Py_VISIT(((PyObject *)(self->buf)));
4725 return 0;
4726}
4727
4728 static int
4729RangeClear(RangeObject *self)
4730{
4731 Py_CLEAR(self->buf);
4732 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004733}
4734
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004735 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004736RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004737{
4738 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004739 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004740 return -1; /* ??? */
4741
Bram Moolenaard6e39182013-05-21 18:30:34 +02004742 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004743}
4744
4745 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004746RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004747{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004748 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004749}
4750
4751 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004752RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004753{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004754 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004755}
4756
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004757static char *RangeAttrs[] = {
4758 "start", "end",
4759 NULL
4760};
4761
4762 static PyObject *
4763RangeDir(PyObject *self)
4764{
4765 return ObjectDir(self, RangeAttrs);
4766}
4767
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004768 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004769RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004770{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004771 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004772}
4773
4774 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004775RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004776{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004777 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004778 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4779 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004780 else
4781 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004782 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004783
4784 if (name == NULL)
4785 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004786
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004787 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004788 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004789 }
4790}
4791
4792static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004793 /* name, function, calling, documentation */
4794 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004795 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4796 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004797};
4798
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004799static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004800static PySequenceMethods BufferAsSeq;
4801static PyMappingMethods BufferAsMapping;
4802
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004803 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004804BufferNew(buf_T *buf)
4805{
4806 /* We need to handle deletion of buffers underneath us.
4807 * If we add a "b_python*_ref" field to the buf_T structure,
4808 * then we can get at it in buf_freeall() in vim. We then
4809 * need to create only ONE Python object per buffer - if
4810 * we try to create a second, just INCREF the existing one
4811 * and return it. The (single) Python object referring to
4812 * the buffer is stored in "b_python*_ref".
4813 * Question: what to do on a buf_freeall(). We'll probably
4814 * have to either delete the Python object (DECREF it to
4815 * zero - a bad idea, as it leaves dangling refs!) or
4816 * set the buf_T * value to an invalid value (-1?), which
4817 * means we need checks in all access functions... Bah.
4818 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004819 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004820 * b_python_ref and b_python3_ref fields respectively.
4821 */
4822
4823 BufferObject *self;
4824
4825 if (BUF_PYTHON_REF(buf) != NULL)
4826 {
4827 self = BUF_PYTHON_REF(buf);
4828 Py_INCREF(self);
4829 }
4830 else
4831 {
4832 self = PyObject_NEW(BufferObject, &BufferType);
4833 if (self == NULL)
4834 return NULL;
4835 self->buf = buf;
4836 BUF_PYTHON_REF(buf) = self;
4837 }
4838
4839 return (PyObject *)(self);
4840}
4841
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004842 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004843BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004844{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004845 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4846 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004847
4848 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004849}
4850
Bram Moolenaar971db462013-05-12 18:44:48 +02004851 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004852BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004853{
4854 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004855 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004856 return -1; /* ??? */
4857
Bram Moolenaard6e39182013-05-21 18:30:34 +02004858 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004859}
4860
4861 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004862BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004863{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004864 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004865}
4866
4867 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004868BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004869{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004870 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004871}
4872
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004873static char *BufferAttrs[] = {
4874 "name", "number", "vars", "options", "valid",
4875 NULL
4876};
4877
4878 static PyObject *
4879BufferDir(PyObject *self)
4880{
4881 return ObjectDir(self, BufferAttrs);
4882}
4883
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004884 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004885BufferAttrValid(BufferObject *self, char *name)
4886{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004887 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004888
4889 if (strcmp(name, "valid") != 0)
4890 return NULL;
4891
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004892 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4893 Py_INCREF(ret);
4894 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004895}
4896
4897 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004898BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004899{
4900 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004901 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004902 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004903 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004904 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004905 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004906 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004907 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004908 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4909 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004910 else if (strcmp(name, "__members__") == 0)
4911 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004912 else
4913 return NULL;
4914}
4915
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004916 static int
4917BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4918{
4919 if (CheckBuffer(self))
4920 return -1;
4921
4922 if (strcmp(name, "name") == 0)
4923 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004924 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004925 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004926 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004927 PyObject *todecref;
4928
4929 if (!(val = StringToChars(valObject, &todecref)))
4930 return -1;
4931
4932 VimTryStart();
4933 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4934 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004935 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004936 aucmd_restbuf(&aco);
4937 Py_XDECREF(todecref);
4938 if (VimTryEnd())
4939 return -1;
4940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004941 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004942 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004943 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004944 return -1;
4945 }
4946 return 0;
4947 }
4948 else
4949 {
4950 PyErr_SetString(PyExc_AttributeError, name);
4951 return -1;
4952 }
4953}
4954
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004955 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004956BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004957{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004958 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004959}
4960
4961 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004962BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004963{
4964 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004965 char_u *pmark;
4966 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004967 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004968 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004969
Bram Moolenaard6e39182013-05-21 18:30:34 +02004970 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004971 return NULL;
4972
Bram Moolenaar389a1792013-06-23 13:00:44 +02004973 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004974 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004975
Bram Moolenaar389a1792013-06-23 13:00:44 +02004976 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004977 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004978 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004979 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004980 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004981 return NULL;
4982 }
4983
4984 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004985
4986 Py_XDECREF(todecref);
4987
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004988 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004989 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004990 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004991 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004992 if (VimTryEnd())
4993 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004994
4995 if (posp == NULL)
4996 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004997 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004998 return NULL;
4999 }
5000
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005001 if (posp->lnum <= 0)
5002 {
5003 /* Or raise an error? */
5004 Py_INCREF(Py_None);
5005 return Py_None;
5006 }
5007
5008 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5009}
5010
5011 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005012BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005013{
5014 PyInt start;
5015 PyInt end;
5016
Bram Moolenaard6e39182013-05-21 18:30:34 +02005017 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005018 return NULL;
5019
5020 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5021 return NULL;
5022
Bram Moolenaard6e39182013-05-21 18:30:34 +02005023 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005024}
5025
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005026 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005027BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005028{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005029 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005030 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005031 else
5032 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005033 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005034
5035 if (name == NULL)
5036 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005037
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005038 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005039 }
5040}
5041
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005042static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005043 /* name, function, calling, documentation */
5044 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005045 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005046 {"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 +02005047 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5048 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005049};
5050
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005051/*
5052 * Buffer list object - Implementation
5053 */
5054
5055static PyTypeObject BufMapType;
5056
5057typedef struct
5058{
5059 PyObject_HEAD
5060} BufMapObject;
5061
5062 static PyInt
5063BufMapLength(PyObject *self UNUSED)
5064{
5065 buf_T *b = firstbuf;
5066 PyInt n = 0;
5067
5068 while (b)
5069 {
5070 ++n;
5071 b = b->b_next;
5072 }
5073
5074 return n;
5075}
5076
5077 static PyObject *
5078BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5079{
5080 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005081 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005082
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005083 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005084 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005085
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005086 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005087
5088 if (b)
5089 return BufferNew(b);
5090 else
5091 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005092 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005093 return NULL;
5094 }
5095}
5096
5097 static void
5098BufMapIterDestruct(PyObject *buffer)
5099{
5100 /* Iteration was stopped before all buffers were processed */
5101 if (buffer)
5102 {
5103 Py_DECREF(buffer);
5104 }
5105}
5106
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005107 static int
5108BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5109{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005110 if (buffer)
5111 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005112 return 0;
5113}
5114
5115 static int
5116BufMapIterClear(PyObject **buffer)
5117{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005118 if (*buffer)
5119 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005120 return 0;
5121}
5122
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005123 static PyObject *
5124BufMapIterNext(PyObject **buffer)
5125{
5126 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005127 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005128
5129 if (!*buffer)
5130 return NULL;
5131
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005132 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005133
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005134 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005135 {
5136 *buffer = NULL;
5137 return NULL;
5138 }
5139
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005140 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005141 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005142 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005143 return NULL;
5144 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005145 /* Do not increment reference: we no longer hold it (decref), but whoever
5146 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005147 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005148}
5149
5150 static PyObject *
5151BufMapIter(PyObject *self UNUSED)
5152{
5153 PyObject *buffer;
5154
5155 buffer = BufferNew(firstbuf);
5156 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005157 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5158 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005159}
5160
5161static PyMappingMethods BufMapAsMapping = {
5162 (lenfunc) BufMapLength,
5163 (binaryfunc) BufMapItem,
5164 (objobjargproc) 0,
5165};
5166
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005167/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005168 */
5169
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005170static char *CurrentAttrs[] = {
5171 "buffer", "window", "line", "range", "tabpage",
5172 NULL
5173};
5174
5175 static PyObject *
5176CurrentDir(PyObject *self)
5177{
5178 return ObjectDir(self, CurrentAttrs);
5179}
5180
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005181 static PyObject *
5182CurrentGetattr(PyObject *self UNUSED, char *name)
5183{
5184 if (strcmp(name, "buffer") == 0)
5185 return (PyObject *)BufferNew(curbuf);
5186 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005187 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005188 else if (strcmp(name, "tabpage") == 0)
5189 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005190 else if (strcmp(name, "line") == 0)
5191 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5192 else if (strcmp(name, "range") == 0)
5193 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005194 else if (strcmp(name, "__members__") == 0)
5195 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005196 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005197#if PY_MAJOR_VERSION < 3
5198 return Py_FindMethod(WindowMethods, self, name);
5199#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005200 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005201#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005202}
5203
5204 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005205CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005206{
5207 if (strcmp(name, "line") == 0)
5208 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005209 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5210 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005211 return -1;
5212
5213 return 0;
5214 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005215 else if (strcmp(name, "buffer") == 0)
5216 {
5217 int count;
5218
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005219 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005220 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005221 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005222 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005223 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005224 return -1;
5225 }
5226
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005227 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005228 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005229 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005230
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005231 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005232 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5233 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005234 if (VimTryEnd())
5235 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005236 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005237 return -1;
5238 }
5239
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005240 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005241 }
5242 else if (strcmp(name, "window") == 0)
5243 {
5244 int count;
5245
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005246 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005247 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005248 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005249 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005250 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005251 return -1;
5252 }
5253
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005254 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005255 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005256 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005257
5258 if (!count)
5259 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005260 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005261 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005262 return -1;
5263 }
5264
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005265 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005266 win_goto(((WindowObject *)(valObject))->win);
5267 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005268 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005269 if (VimTryEnd())
5270 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005271 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005272 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005273 return -1;
5274 }
5275
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005276 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005277 }
5278 else if (strcmp(name, "tabpage") == 0)
5279 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005280 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005281 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005282 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005283 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005284 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005285 return -1;
5286 }
5287
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005288 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005289 return -1;
5290
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005291 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005292 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5293 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005294 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005295 if (VimTryEnd())
5296 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005297 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005298 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005299 return -1;
5300 }
5301
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005302 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005303 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005304 else
5305 {
5306 PyErr_SetString(PyExc_AttributeError, name);
5307 return -1;
5308 }
5309}
5310
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005311static struct PyMethodDef CurrentMethods[] = {
5312 /* name, function, calling, documentation */
5313 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5314 { NULL, NULL, 0, NULL}
5315};
5316
Bram Moolenaardb913952012-06-29 12:54:53 +02005317 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005318init_range_cmd(exarg_T *eap)
5319{
5320 RangeStart = eap->line1;
5321 RangeEnd = eap->line2;
5322}
5323
5324 static void
5325init_range_eval(typval_T *rettv UNUSED)
5326{
5327 RangeStart = (PyInt) curwin->w_cursor.lnum;
5328 RangeEnd = RangeStart;
5329}
5330
5331 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005332run_cmd(const char *cmd, void *arg UNUSED
5333#ifdef PY_CAN_RECURSE
5334 , PyGILState_STATE *pygilstate UNUSED
5335#endif
5336 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005337{
Bram Moolenaar41009372013-07-01 22:03:04 +02005338 PyObject *run_ret;
5339 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5340 if (run_ret != NULL)
5341 {
5342 Py_DECREF(run_ret);
5343 }
5344 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5345 {
5346 EMSG2(_(e_py_systemexit), "python");
5347 PyErr_Clear();
5348 }
5349 else
5350 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005351}
5352
5353static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5354static int code_hdr_len = 30;
5355
5356 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005357run_do(const char *cmd, void *arg UNUSED
5358#ifdef PY_CAN_RECURSE
5359 , PyGILState_STATE *pygilstate
5360#endif
5361 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005362{
5363 PyInt lnum;
5364 size_t len;
5365 char *code;
5366 int status;
5367 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005368 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005369
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005370 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005371 {
5372 EMSG(_("cannot save undo information"));
5373 return;
5374 }
5375
5376 len = code_hdr_len + STRLEN(cmd);
5377 code = PyMem_New(char, len + 1);
5378 memcpy(code, code_hdr, code_hdr_len);
5379 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005380 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5381 status = -1;
5382 if (run_ret != NULL)
5383 {
5384 status = 0;
5385 Py_DECREF(run_ret);
5386 }
5387 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5388 {
5389 PyMem_Free(code);
5390 EMSG2(_(e_py_systemexit), "python");
5391 PyErr_Clear();
5392 return;
5393 }
5394 else
5395 PyErr_PrintEx(1);
5396
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005397 PyMem_Free(code);
5398
5399 if (status)
5400 {
5401 EMSG(_("failed to run the code"));
5402 return;
5403 }
5404
5405 status = 0;
5406 pymain = PyImport_AddModule("__main__");
5407 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005408#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005409 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005410#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005411
5412 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5413 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005414 PyObject *line;
5415 PyObject *linenr;
5416 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005417
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005418#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005419 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005420#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005421 if (!(line = GetBufferLine(curbuf, lnum)))
5422 goto err;
5423 if (!(linenr = PyInt_FromLong((long) lnum)))
5424 {
5425 Py_DECREF(line);
5426 goto err;
5427 }
5428 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5429 Py_DECREF(line);
5430 Py_DECREF(linenr);
5431 if (!ret)
5432 goto err;
5433
5434 if (ret != Py_None)
5435 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5436 goto err;
5437
5438 Py_XDECREF(ret);
5439 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005440#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005441 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005442#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005443 }
5444 goto out;
5445err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005446#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005447 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005448#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005449 PyErr_PrintEx(0);
5450 PythonIO_Flush();
5451 status = 1;
5452out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005453#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005454 if (!status)
5455 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005456#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005457 Py_DECREF(pyfunc);
5458 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5459 if (status)
5460 return;
5461 check_cursor();
5462 update_curbuf(NOT_VALID);
5463}
5464
5465 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005466run_eval(const char *cmd, typval_T *rettv
5467#ifdef PY_CAN_RECURSE
5468 , PyGILState_STATE *pygilstate UNUSED
5469#endif
5470 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005471{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005472 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005473
Bram Moolenaar41009372013-07-01 22:03:04 +02005474 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005475 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005476 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005477 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005478 {
5479 EMSG2(_(e_py_systemexit), "python");
5480 PyErr_Clear();
5481 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005482 else
5483 {
5484 if (PyErr_Occurred() && !msg_silent)
5485 PyErr_PrintEx(0);
5486 EMSG(_("E858: Eval did not return a valid python object"));
5487 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005488 }
5489 else
5490 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005491 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005492 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005493 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005494 }
5495 PyErr_Clear();
5496}
5497
5498 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005499set_ref_in_py(const int copyID)
5500{
5501 pylinkedlist_T *cur;
5502 dict_T *dd;
5503 list_T *ll;
5504
5505 if (lastdict != NULL)
5506 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5507 {
5508 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5509 if (dd->dv_copyID != copyID)
5510 {
5511 dd->dv_copyID = copyID;
5512 set_ref_in_ht(&dd->dv_hashtab, copyID);
5513 }
5514 }
5515
5516 if (lastlist != NULL)
5517 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5518 {
5519 ll = ((ListObject *) (cur->pll_obj))->list;
5520 if (ll->lv_copyID != copyID)
5521 {
5522 ll->lv_copyID = copyID;
5523 set_ref_in_list(ll, copyID);
5524 }
5525 }
5526}
5527
5528 static int
5529set_string_copy(char_u *str, typval_T *tv)
5530{
5531 tv->vval.v_string = vim_strsave(str);
5532 if (tv->vval.v_string == NULL)
5533 {
5534 PyErr_NoMemory();
5535 return -1;
5536 }
5537 return 0;
5538}
5539
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005540 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005541pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005542{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005543 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005544 char_u *key;
5545 dictitem_T *di;
5546 PyObject *keyObject;
5547 PyObject *valObject;
5548 Py_ssize_t iter = 0;
5549
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005550 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005551 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005552
5553 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005554 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005555
5556 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5557 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005558 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005559
Bram Moolenaara03e6312013-05-29 22:49:26 +02005560 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005561 {
5562 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005563 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005564 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005565
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005566 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005567 {
5568 dict_unref(dict);
5569 return -1;
5570 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005571
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005572 if (*key == NUL)
5573 {
5574 dict_unref(dict);
5575 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005576 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005577 return -1;
5578 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005579
5580 di = dictitem_alloc(key);
5581
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005582 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005583
5584 if (di == NULL)
5585 {
5586 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005587 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005588 return -1;
5589 }
5590 di->di_tv.v_lock = 0;
5591
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005592 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005593 {
5594 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005595 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005596 return -1;
5597 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005598
5599 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005600 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005601 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005602 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005603 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005604 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005605 return -1;
5606 }
5607 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005608
5609 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005610 return 0;
5611}
5612
5613 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005614pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005615{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005616 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005617 char_u *key;
5618 dictitem_T *di;
5619 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005620 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005621 PyObject *keyObject;
5622 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005623
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005624 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005625 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005626
5627 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005628 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005629
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005630 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005631 {
5632 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005633 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005634 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005635
5636 if (!(iterator = PyObject_GetIter(list)))
5637 {
5638 dict_unref(dict);
5639 Py_DECREF(list);
5640 return -1;
5641 }
5642 Py_DECREF(list);
5643
5644 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005645 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005646 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005647
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005648 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005649 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005650 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005651 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005652 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005653 return -1;
5654 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005655
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005656 if (*key == NUL)
5657 {
5658 Py_DECREF(keyObject);
5659 Py_DECREF(iterator);
5660 Py_XDECREF(todecref);
5661 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005662 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005663 return -1;
5664 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005665
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005666 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005667 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005668 Py_DECREF(keyObject);
5669 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005670 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005671 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005672 return -1;
5673 }
5674
5675 di = dictitem_alloc(key);
5676
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005677 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005678 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005679
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005680 if (di == NULL)
5681 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005682 Py_DECREF(iterator);
5683 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005684 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005685 PyErr_NoMemory();
5686 return -1;
5687 }
5688 di->di_tv.v_lock = 0;
5689
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005690 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005691 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005692 Py_DECREF(iterator);
5693 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005694 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005695 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005696 return -1;
5697 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005698
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005699 Py_DECREF(valObject);
5700
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005701 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005702 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005703 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005704 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005705 dictitem_free(di);
5706 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005707 return -1;
5708 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005709 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005710 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005711 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005712 return 0;
5713}
5714
5715 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005716pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005717{
5718 list_T *l;
5719
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005720 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005721 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005722
5723 tv->v_type = VAR_LIST;
5724 tv->vval.v_list = l;
5725
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005726 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005727 {
5728 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005729 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005730 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005731
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005732 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005733 return 0;
5734}
5735
Bram Moolenaardb913952012-06-29 12:54:53 +02005736typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5737
5738 static int
5739convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005740 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005741{
5742 PyObject *capsule;
5743 char hexBuf[sizeof(void *) * 2 + 3];
5744
5745 sprintf(hexBuf, "%p", obj);
5746
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005747# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005748 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005749# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005750 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005751# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005752 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005753 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005754# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005755 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005756# else
5757 capsule = PyCObject_FromVoidPtr(tv, NULL);
5758# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005759 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5760 {
5761 Py_DECREF(capsule);
5762 tv->v_type = VAR_UNKNOWN;
5763 return -1;
5764 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005765
5766 Py_DECREF(capsule);
5767
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005768 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005769 {
5770 tv->v_type = VAR_UNKNOWN;
5771 return -1;
5772 }
5773 /* As we are not using copy_tv which increments reference count we must
5774 * do it ourself. */
5775 switch(tv->v_type)
5776 {
5777 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5778 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5779 }
5780 }
5781 else
5782 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005783 typval_T *v;
5784
5785# ifdef PY_USE_CAPSULE
5786 v = PyCapsule_GetPointer(capsule, NULL);
5787# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005788 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005789# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005790 copy_tv(v, tv);
5791 }
5792 return 0;
5793}
5794
5795 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005796ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5797{
5798 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005799 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005800
5801 if (!(lookup_dict = PyDict_New()))
5802 return -1;
5803
5804 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5805 {
5806 tv->v_type = VAR_DICT;
5807 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5808 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005809 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005810 }
5811 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005812 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005813 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005814 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005815 else
5816 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005817 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005818 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005819 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005820 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005821 }
5822 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005823 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005824}
5825
5826 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005827ConvertFromPyObject(PyObject *obj, typval_T *tv)
5828{
5829 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005830 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005831
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005832 if (!(lookup_dict = PyDict_New()))
5833 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005834 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005835 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005836 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005837}
5838
5839 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005840_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005841{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005842 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005843 {
5844 tv->v_type = VAR_DICT;
5845 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5846 ++tv->vval.v_dict->dv_refcount;
5847 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005848 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005849 {
5850 tv->v_type = VAR_LIST;
5851 tv->vval.v_list = (((ListObject *)(obj))->list);
5852 ++tv->vval.v_list->lv_refcount;
5853 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005854 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005855 {
5856 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5857 return -1;
5858
5859 tv->v_type = VAR_FUNC;
5860 func_ref(tv->vval.v_string);
5861 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005862 else if (PyBytes_Check(obj))
5863 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005864 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005865
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005866 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005867 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005868 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005869 return -1;
5870
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005871 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005872 return -1;
5873
5874 tv->v_type = VAR_STRING;
5875 }
5876 else if (PyUnicode_Check(obj))
5877 {
5878 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005879 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005880
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005881 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005882 if (bytes == NULL)
5883 return -1;
5884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005885 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005886 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005887 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005888 return -1;
5889
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005890 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005891 {
5892 Py_XDECREF(bytes);
5893 return -1;
5894 }
5895 Py_XDECREF(bytes);
5896
5897 tv->v_type = VAR_STRING;
5898 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005899#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005900 else if (PyInt_Check(obj))
5901 {
5902 tv->v_type = VAR_NUMBER;
5903 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005904 if (PyErr_Occurred())
5905 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005906 }
5907#endif
5908 else if (PyLong_Check(obj))
5909 {
5910 tv->v_type = VAR_NUMBER;
5911 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005912 if (PyErr_Occurred())
5913 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005914 }
5915 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005916 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005917#ifdef FEAT_FLOAT
5918 else if (PyFloat_Check(obj))
5919 {
5920 tv->v_type = VAR_FLOAT;
5921 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5922 }
5923#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005924 else if (PyObject_HasAttrString(obj, "keys"))
5925 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005926 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005927 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005928 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005929 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005930 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005931 else if (PyNumber_Check(obj))
5932 {
5933 PyObject *num;
5934
5935 if (!(num = PyNumber_Long(obj)))
5936 return -1;
5937
5938 tv->v_type = VAR_NUMBER;
5939 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5940
5941 Py_DECREF(num);
5942 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005943 else
5944 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005945 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005946 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005947 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005948 return -1;
5949 }
5950 return 0;
5951}
5952
5953 static PyObject *
5954ConvertToPyObject(typval_T *tv)
5955{
5956 if (tv == NULL)
5957 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005958 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005959 return NULL;
5960 }
5961 switch (tv->v_type)
5962 {
5963 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005964 return PyBytes_FromString(tv->vval.v_string == NULL
5965 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005966 case VAR_NUMBER:
5967 return PyLong_FromLong((long) tv->vval.v_number);
5968#ifdef FEAT_FLOAT
5969 case VAR_FLOAT:
5970 return PyFloat_FromDouble((double) tv->vval.v_float);
5971#endif
5972 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005973 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005974 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005975 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005977 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005978 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005979 case VAR_UNKNOWN:
5980 Py_INCREF(Py_None);
5981 return Py_None;
5982 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005983 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005984 return NULL;
5985 }
5986}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005987
5988typedef struct
5989{
5990 PyObject_HEAD
5991} CurrentObject;
5992static PyTypeObject CurrentType;
5993
5994 static void
5995init_structs(void)
5996{
5997 vim_memset(&OutputType, 0, sizeof(OutputType));
5998 OutputType.tp_name = "vim.message";
5999 OutputType.tp_basicsize = sizeof(OutputObject);
6000 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6001 OutputType.tp_doc = "vim message object";
6002 OutputType.tp_methods = OutputMethods;
6003#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006004 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6005 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006006 OutputType.tp_alloc = call_PyType_GenericAlloc;
6007 OutputType.tp_new = call_PyType_GenericNew;
6008 OutputType.tp_free = call_PyObject_Free;
6009#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006010 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6011 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006012#endif
6013
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006014 vim_memset(&IterType, 0, sizeof(IterType));
6015 IterType.tp_name = "vim.iter";
6016 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006017 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006018 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006019 IterType.tp_iter = (getiterfunc)IterIter;
6020 IterType.tp_iternext = (iternextfunc)IterNext;
6021 IterType.tp_dealloc = (destructor)IterDestructor;
6022 IterType.tp_traverse = (traverseproc)IterTraverse;
6023 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006024
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006025 vim_memset(&BufferType, 0, sizeof(BufferType));
6026 BufferType.tp_name = "vim.buffer";
6027 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006028 BufferType.tp_dealloc = (destructor)BufferDestructor;
6029 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006030 BufferType.tp_as_sequence = &BufferAsSeq;
6031 BufferType.tp_as_mapping = &BufferAsMapping;
6032 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6033 BufferType.tp_doc = "vim buffer object";
6034 BufferType.tp_methods = BufferMethods;
6035#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006036 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006037 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006038 BufferType.tp_alloc = call_PyType_GenericAlloc;
6039 BufferType.tp_new = call_PyType_GenericNew;
6040 BufferType.tp_free = call_PyObject_Free;
6041#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006042 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006043 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006044#endif
6045
6046 vim_memset(&WindowType, 0, sizeof(WindowType));
6047 WindowType.tp_name = "vim.window";
6048 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006049 WindowType.tp_dealloc = (destructor)WindowDestructor;
6050 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006051 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006052 WindowType.tp_doc = "vim Window object";
6053 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006054 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6055 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006056#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006057 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6058 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006059 WindowType.tp_alloc = call_PyType_GenericAlloc;
6060 WindowType.tp_new = call_PyType_GenericNew;
6061 WindowType.tp_free = call_PyObject_Free;
6062#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006063 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6064 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006065#endif
6066
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006067 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6068 TabPageType.tp_name = "vim.tabpage";
6069 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006070 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6071 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006072 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6073 TabPageType.tp_doc = "vim tab page object";
6074 TabPageType.tp_methods = TabPageMethods;
6075#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006076 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006077 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6078 TabPageType.tp_new = call_PyType_GenericNew;
6079 TabPageType.tp_free = call_PyObject_Free;
6080#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006081 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006082#endif
6083
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006084 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6085 BufMapType.tp_name = "vim.bufferlist";
6086 BufMapType.tp_basicsize = sizeof(BufMapObject);
6087 BufMapType.tp_as_mapping = &BufMapAsMapping;
6088 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006089 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006090 BufferType.tp_doc = "vim buffer list";
6091
6092 vim_memset(&WinListType, 0, sizeof(WinListType));
6093 WinListType.tp_name = "vim.windowlist";
6094 WinListType.tp_basicsize = sizeof(WinListType);
6095 WinListType.tp_as_sequence = &WinListAsSeq;
6096 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6097 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006098 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006099
6100 vim_memset(&TabListType, 0, sizeof(TabListType));
6101 TabListType.tp_name = "vim.tabpagelist";
6102 TabListType.tp_basicsize = sizeof(TabListType);
6103 TabListType.tp_as_sequence = &TabListAsSeq;
6104 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6105 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006106
6107 vim_memset(&RangeType, 0, sizeof(RangeType));
6108 RangeType.tp_name = "vim.range";
6109 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006110 RangeType.tp_dealloc = (destructor)RangeDestructor;
6111 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006112 RangeType.tp_as_sequence = &RangeAsSeq;
6113 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006114 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006115 RangeType.tp_doc = "vim Range object";
6116 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006117 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6118 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006119#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006120 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006121 RangeType.tp_alloc = call_PyType_GenericAlloc;
6122 RangeType.tp_new = call_PyType_GenericNew;
6123 RangeType.tp_free = call_PyObject_Free;
6124#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006125 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006126#endif
6127
6128 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6129 CurrentType.tp_name = "vim.currentdata";
6130 CurrentType.tp_basicsize = sizeof(CurrentObject);
6131 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6132 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006133 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006134#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006135 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6136 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006137#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006138 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6139 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006140#endif
6141
6142 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6143 DictionaryType.tp_name = "vim.dictionary";
6144 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006145 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006146 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006147 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006148 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006149 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6150 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006151 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6152 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6153 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006154#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006155 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6156 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006157#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006158 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6159 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006160#endif
6161
6162 vim_memset(&ListType, 0, sizeof(ListType));
6163 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006164 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006165 ListType.tp_basicsize = sizeof(ListObject);
6166 ListType.tp_as_sequence = &ListAsSeq;
6167 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006168 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006169 ListType.tp_doc = "list pushing modifications to vim structure";
6170 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006171 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006172 ListType.tp_new = (newfunc)ListConstructor;
6173 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006174#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006175 ListType.tp_getattro = (getattrofunc)ListGetattro;
6176 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006177#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006178 ListType.tp_getattr = (getattrfunc)ListGetattr;
6179 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006180#endif
6181
6182 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006183 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006184 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006185 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6186 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006187 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006188 FunctionType.tp_doc = "object that calls vim function";
6189 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006190 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006191 FunctionType.tp_new = (newfunc)FunctionConstructor;
6192 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006193#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006194 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006195#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006196 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006197#endif
6198
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006199 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6200 OptionsType.tp_name = "vim.options";
6201 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006202 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006203 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006204 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006205 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006206 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006207 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6208 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6209 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006210
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006211 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6212 LoaderType.tp_name = "vim.Loader";
6213 LoaderType.tp_basicsize = sizeof(LoaderObject);
6214 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6215 LoaderType.tp_doc = "vim message object";
6216 LoaderType.tp_methods = LoaderMethods;
6217 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6218
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006219#if PY_MAJOR_VERSION >= 3
6220 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6221 vimmodule.m_name = "vim";
6222 vimmodule.m_doc = "Vim Python interface\n";
6223 vimmodule.m_size = -1;
6224 vimmodule.m_methods = VimMethods;
6225#endif
6226}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006227
6228#define PYTYPE_READY(type) \
6229 if (PyType_Ready(&type)) \
6230 return -1;
6231
6232 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006233init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006234{
6235 PYTYPE_READY(IterType);
6236 PYTYPE_READY(BufferType);
6237 PYTYPE_READY(RangeType);
6238 PYTYPE_READY(WindowType);
6239 PYTYPE_READY(TabPageType);
6240 PYTYPE_READY(BufMapType);
6241 PYTYPE_READY(WinListType);
6242 PYTYPE_READY(TabListType);
6243 PYTYPE_READY(CurrentType);
6244 PYTYPE_READY(DictionaryType);
6245 PYTYPE_READY(ListType);
6246 PYTYPE_READY(FunctionType);
6247 PYTYPE_READY(OptionsType);
6248 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006249 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006250 return 0;
6251}
6252
6253 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006254init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006255{
6256 PyObject *path;
6257 PyObject *path_hook;
6258 PyObject *path_hooks;
6259
6260 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6261 return -1;
6262
6263 if (!(path_hooks = PySys_GetObject("path_hooks")))
6264 {
6265 PyErr_Clear();
6266 path_hooks = PyList_New(1);
6267 PyList_SET_ITEM(path_hooks, 0, path_hook);
6268 if (PySys_SetObject("path_hooks", path_hooks))
6269 {
6270 Py_DECREF(path_hooks);
6271 return -1;
6272 }
6273 Py_DECREF(path_hooks);
6274 }
6275 else if (PyList_Check(path_hooks))
6276 {
6277 if (PyList_Append(path_hooks, path_hook))
6278 {
6279 Py_DECREF(path_hook);
6280 return -1;
6281 }
6282 Py_DECREF(path_hook);
6283 }
6284 else
6285 {
6286 VimTryStart();
6287 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6288 "You should now do the following:\n"
6289 "- append vim.path_hook to sys.path_hooks\n"
6290 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6291 VimTryEnd(); /* Discard the error */
6292 Py_DECREF(path_hook);
6293 return 0;
6294 }
6295
6296 if (!(path = PySys_GetObject("path")))
6297 {
6298 PyErr_Clear();
6299 path = PyList_New(1);
6300 Py_INCREF(vim_special_path_object);
6301 PyList_SET_ITEM(path, 0, vim_special_path_object);
6302 if (PySys_SetObject("path", path))
6303 {
6304 Py_DECREF(path);
6305 return -1;
6306 }
6307 Py_DECREF(path);
6308 }
6309 else if (PyList_Check(path))
6310 {
6311 if (PyList_Append(path, vim_special_path_object))
6312 return -1;
6313 }
6314 else
6315 {
6316 VimTryStart();
6317 EMSG(_("Failed to set path: sys.path is not a list\n"
6318 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6319 VimTryEnd(); /* Discard the error */
6320 }
6321
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006322 return 0;
6323}
6324
6325static BufMapObject TheBufferMap =
6326{
6327 PyObject_HEAD_INIT(&BufMapType)
6328};
6329
6330static WinListObject TheWindowList =
6331{
6332 PyObject_HEAD_INIT(&WinListType)
6333 NULL
6334};
6335
6336static CurrentObject TheCurrent =
6337{
6338 PyObject_HEAD_INIT(&CurrentType)
6339};
6340
6341static TabListObject TheTabPageList =
6342{
6343 PyObject_HEAD_INIT(&TabListType)
6344};
6345
6346static struct numeric_constant {
6347 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006348 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006349} numeric_constants[] = {
6350 {"VAR_LOCKED", VAR_LOCKED},
6351 {"VAR_FIXED", VAR_FIXED},
6352 {"VAR_SCOPE", VAR_SCOPE},
6353 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6354};
6355
6356static struct object_constant {
6357 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006358 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006359} object_constants[] = {
6360 {"buffers", (PyObject *)(void *)&TheBufferMap},
6361 {"windows", (PyObject *)(void *)&TheWindowList},
6362 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6363 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006364
6365 {"Buffer", (PyObject *)&BufferType},
6366 {"Range", (PyObject *)&RangeType},
6367 {"Window", (PyObject *)&WindowType},
6368 {"TabPage", (PyObject *)&TabPageType},
6369 {"Dictionary", (PyObject *)&DictionaryType},
6370 {"List", (PyObject *)&ListType},
6371 {"Function", (PyObject *)&FunctionType},
6372 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006373 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006374};
6375
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006376#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006377 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006378 return -1;
6379
6380#define ADD_CHECKED_OBJECT(m, name, obj) \
6381 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006382 PyObject *valObject = obj; \
6383 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006384 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006385 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006386 }
6387
6388 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006389populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006390{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006391 int i;
6392 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006393 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006394 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006395
6396 for (i = 0; i < (int)(sizeof(numeric_constants)
6397 / sizeof(struct numeric_constant));
6398 ++i)
6399 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006400 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006401
6402 for (i = 0; i < (int)(sizeof(object_constants)
6403 / sizeof(struct object_constant));
6404 ++i)
6405 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006406 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006407
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006408 valObject = object_constants[i].valObject;
6409 Py_INCREF(valObject);
6410 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006411 }
6412
6413 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6414 return -1;
6415 ADD_OBJECT(m, "error", VimError);
6416
Bram Moolenaara9922d62013-05-30 13:01:18 +02006417 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6418 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006419 ADD_CHECKED_OBJECT(m, "options",
6420 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006421
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006422 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006423 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006424 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006425
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006426 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006427 return -1;
6428 ADD_OBJECT(m, "_getcwd", py_getcwd)
6429
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006430 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006431 return -1;
6432 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006433 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006434 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006435 if (PyObject_SetAttrString(other_module, "chdir", attr))
6436 {
6437 Py_DECREF(attr);
6438 return -1;
6439 }
6440 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006441
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006442 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006443 {
6444 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006445 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006446 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006447 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6448 {
6449 Py_DECREF(attr);
6450 return -1;
6451 }
6452 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006453 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006454 else
6455 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006456
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006457 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6458 return -1;
6459
6460 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6461
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006462 if (!(imp = PyImport_ImportModule("imp")))
6463 return -1;
6464
6465 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6466 {
6467 Py_DECREF(imp);
6468 return -1;
6469 }
6470
6471 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6472 {
6473 Py_DECREF(py_find_module);
6474 Py_DECREF(imp);
6475 return -1;
6476 }
6477
6478 Py_DECREF(imp);
6479
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006480 ADD_OBJECT(m, "_find_module", py_find_module);
6481 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006482
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006483 return 0;
6484}