blob: 497db86651e2da10a12655ce23b0a973e7dd9357 [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 Moolenaar4de6a212014-03-08 16:13:44 +0100239 N_("number must be greater than 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 Moolenaar24a6ff82015-02-10 18:41:58 +0100750 hashtab_T *ht;
751 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200752 hashitem_T *hi;
753 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100754
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200755 if (our_tv->vval.v_dict == NULL)
756 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100757 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200758
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200759 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200760 return NULL;
761
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200764 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200765 return NULL;
766 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200767
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100768 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200769 for (hi = ht->ht_array; todo > 0; ++hi)
770 {
771 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200772 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200773 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200774
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200775 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200776 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200777 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200778 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200780 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200781 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200782 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200784 Py_DECREF(newObj);
785 return NULL;
786 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787 }
788 }
789 }
790 else
791 {
792 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794 }
795
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200796 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798
799 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200800VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200801{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200802 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200804 PyObject *string;
805 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200806 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200807 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200808
Bram Moolenaar389a1792013-06-23 13:00:44 +0200809 if (!PyArg_ParseTuple(args, "O", &string))
810 return NULL;
811
812 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200813 return NULL;
814
815 Py_BEGIN_ALLOW_THREADS
816 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200817 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200818 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200819 Python_Release_Vim();
820 Py_END_ALLOW_THREADS
821
Bram Moolenaar389a1792013-06-23 13:00:44 +0200822 Py_XDECREF(todecref);
823
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200824 if (VimTryEnd())
825 return NULL;
826
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827 if (our_tv == NULL)
828 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200829 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200830 return NULL;
831 }
832
833 /* Convert the Vim type into a Python type. Create a dictionary that's
834 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200835 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200837 else
838 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200840 Py_DECREF(lookup_dict);
841 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200842
843
844 Py_BEGIN_ALLOW_THREADS
845 Python_Lock_Vim();
846 free_tv(our_tv);
847 Python_Release_Vim();
848 Py_END_ALLOW_THREADS
849
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200850 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200851}
852
Bram Moolenaardb913952012-06-29 12:54:53 +0200853static PyObject *ConvertToPyObject(typval_T *);
854
855 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200856VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200857{
Bram Moolenaardb913952012-06-29 12:54:53 +0200858 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200859 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 char_u *expr;
861 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200862
Bram Moolenaar389a1792013-06-23 13:00:44 +0200863 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200864 return NULL;
865
866 Py_BEGIN_ALLOW_THREADS
867 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200868 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200869 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200870 Python_Release_Vim();
871 Py_END_ALLOW_THREADS
872
Bram Moolenaar389a1792013-06-23 13:00:44 +0200873 Py_XDECREF(todecref);
874
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200875 if (VimTryEnd())
876 return NULL;
877
Bram Moolenaardb913952012-06-29 12:54:53 +0200878 if (our_tv == NULL)
879 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200880 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200881 return NULL;
882 }
883
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200884 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200885 Py_BEGIN_ALLOW_THREADS
886 Python_Lock_Vim();
887 free_tv(our_tv);
888 Python_Release_Vim();
889 Py_END_ALLOW_THREADS
890
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200891 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200892}
893
894 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200895VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200896{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200897 char_u *str;
898 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200899 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200900
Bram Moolenaar389a1792013-06-23 13:00:44 +0200901 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200902 return NULL;
903
Bram Moolenaara54bf402012-12-05 16:30:07 +0100904#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200905 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100906#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200907 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100908#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200909
910 Py_XDECREF(todecref);
911
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200912 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200913}
914
Bram Moolenaarf4258302013-06-02 18:20:17 +0200915 static PyObject *
916_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
917{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200918 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200919 PyObject *newwd;
920 PyObject *todecref;
921 char_u *new_dir;
922
Bram Moolenaard4209d22013-06-05 20:34:15 +0200923 if (_chdir == NULL)
924 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200925 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200926 return NULL;
927
928 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
929 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200930 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200931 return NULL;
932 }
933
934 if (!(new_dir = StringToChars(newwd, &todecref)))
935 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200936 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200937 Py_DECREF(newwd);
938 return NULL;
939 }
940
941 VimTryStart();
942
943 if (vim_chdir(new_dir))
944 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200945 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200946 Py_DECREF(newwd);
947 Py_XDECREF(todecref);
948
949 if (VimTryEnd())
950 return NULL;
951
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200952 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200953 return NULL;
954 }
955
956 Py_DECREF(newwd);
957 Py_XDECREF(todecref);
958
959 post_chdir(FALSE);
960
961 if (VimTryEnd())
962 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200963 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200964 return NULL;
965 }
966
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200967 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200968}
969
970 static PyObject *
971VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
972{
973 return _VimChdir(py_chdir, args, kwargs);
974}
975
976 static PyObject *
977VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
978{
979 return _VimChdir(py_fchdir, args, kwargs);
980}
981
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200982typedef struct {
983 PyObject *callable;
984 PyObject *result;
985} map_rtp_data;
986
987 static void
988map_rtp_callback(char_u *path, void *_data)
989{
990 void **data = (void **) _data;
991 PyObject *pathObject;
992 map_rtp_data *mr_data = *((map_rtp_data **) data);
993
Bram Moolenaar41009372013-07-01 22:03:04 +0200994 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200995 {
996 *data = NULL;
997 return;
998 }
999
1000 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1001 pathObject, NULL);
1002
1003 Py_DECREF(pathObject);
1004
1005 if (!mr_data->result || mr_data->result != Py_None)
1006 *data = NULL;
1007 else
1008 {
1009 Py_DECREF(mr_data->result);
1010 mr_data->result = NULL;
1011 }
1012}
1013
1014 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001015VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001016{
1017 map_rtp_data data;
1018
Bram Moolenaar389a1792013-06-23 13:00:44 +02001019 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001020 data.result = NULL;
1021
1022 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1023
1024 if (data.result == NULL)
1025 {
1026 if (PyErr_Occurred())
1027 return NULL;
1028 else
1029 {
1030 Py_INCREF(Py_None);
1031 return Py_None;
1032 }
1033 }
1034 return data.result;
1035}
1036
1037/*
1038 * _vim_runtimepath_ special path implementation.
1039 */
1040
1041 static void
1042map_finder_callback(char_u *path, void *_data)
1043{
1044 void **data = (void **) _data;
1045 PyObject *list = *((PyObject **) data);
1046 PyObject *pathObject1, *pathObject2;
1047 char *pathbuf;
1048 size_t pathlen;
1049
1050 pathlen = STRLEN(path);
1051
1052#if PY_MAJOR_VERSION < 3
1053# define PY_MAIN_DIR_STRING "python2"
1054#else
1055# define PY_MAIN_DIR_STRING "python3"
1056#endif
1057#define PY_ALTERNATE_DIR_STRING "pythonx"
1058
1059#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1060 if (!(pathbuf = PyMem_New(char,
1061 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1062 {
1063 PyErr_NoMemory();
1064 *data = NULL;
1065 return;
1066 }
1067
1068 mch_memmove(pathbuf, path, pathlen + 1);
1069 add_pathsep((char_u *) pathbuf);
1070
1071 pathlen = STRLEN(pathbuf);
1072 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1073 PYTHONX_STRING_LENGTH + 1);
1074
1075 if (!(pathObject1 = PyString_FromString(pathbuf)))
1076 {
1077 *data = NULL;
1078 PyMem_Free(pathbuf);
1079 return;
1080 }
1081
1082 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1083 PYTHONX_STRING_LENGTH + 1);
1084
1085 if (!(pathObject2 = PyString_FromString(pathbuf)))
1086 {
1087 Py_DECREF(pathObject1);
1088 PyMem_Free(pathbuf);
1089 *data = NULL;
1090 return;
1091 }
1092
1093 PyMem_Free(pathbuf);
1094
1095 if (PyList_Append(list, pathObject1)
1096 || PyList_Append(list, pathObject2))
1097 *data = NULL;
1098
1099 Py_DECREF(pathObject1);
1100 Py_DECREF(pathObject2);
1101}
1102
1103 static PyObject *
1104Vim_GetPaths(PyObject *self UNUSED)
1105{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001106 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001107
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001108 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001109 return NULL;
1110
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001111 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001112
1113 if (PyErr_Occurred())
1114 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001115 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001116 return NULL;
1117 }
1118
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001119 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001120}
1121
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001122 static PyObject *
1123call_load_module(char *name, int len, PyObject *find_module_result)
1124{
1125 PyObject *fd, *pathname, *description;
1126
Bram Moolenaarc476e522013-06-23 13:46:40 +02001127 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001128 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001129 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001130 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001131 Py_TYPE_NAME(find_module_result));
1132 return NULL;
1133 }
1134 if (PyTuple_GET_SIZE(find_module_result) != 3)
1135 {
1136 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001137 N_("expected 3-tuple as imp.find_module() result, but got "
1138 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001139 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001140 return NULL;
1141 }
1142
1143 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1144 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1145 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1146 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001147 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001148 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001149 return NULL;
1150 }
1151
1152 return PyObject_CallFunction(py_load_module,
1153 "s#OOO", name, len, fd, pathname, description);
1154}
1155
1156 static PyObject *
1157find_module(char *fullname, char *tail, PyObject *new_path)
1158{
1159 PyObject *find_module_result;
1160 PyObject *module;
1161 char *dot;
1162
Bram Moolenaar41009372013-07-01 22:03:04 +02001163 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001164 {
1165 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001166 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001167 * first component
1168 */
1169 PyObject *newest_path;
1170 int partlen = (int) (dot - 1 - tail);
1171
1172 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1173 "s#O", tail, partlen, new_path)))
1174 return NULL;
1175
1176 if (!(module = call_load_module(
1177 fullname,
1178 ((int) (tail - fullname)) + partlen,
1179 find_module_result)))
1180 {
1181 Py_DECREF(find_module_result);
1182 return NULL;
1183 }
1184
1185 Py_DECREF(find_module_result);
1186
1187 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1188 {
1189 Py_DECREF(module);
1190 return NULL;
1191 }
1192
1193 Py_DECREF(module);
1194
1195 module = find_module(fullname, dot + 1, newest_path);
1196
1197 Py_DECREF(newest_path);
1198
1199 return module;
1200 }
1201 else
1202 {
1203 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1204 "sO", tail, new_path)))
1205 return NULL;
1206
1207 if (!(module = call_load_module(
1208 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001209 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001210 find_module_result)))
1211 {
1212 Py_DECREF(find_module_result);
1213 return NULL;
1214 }
1215
1216 Py_DECREF(find_module_result);
1217
1218 return module;
1219 }
1220}
1221
1222 static PyObject *
1223FinderFindModule(PyObject *self, PyObject *args)
1224{
1225 char *fullname;
1226 PyObject *module;
1227 PyObject *new_path;
1228 LoaderObject *loader;
1229
1230 if (!PyArg_ParseTuple(args, "s", &fullname))
1231 return NULL;
1232
1233 if (!(new_path = Vim_GetPaths(self)))
1234 return NULL;
1235
1236 module = find_module(fullname, fullname, new_path);
1237
1238 Py_DECREF(new_path);
1239
1240 if (!module)
1241 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001242 if (PyErr_Occurred())
1243 {
1244 if (PyErr_ExceptionMatches(PyExc_ImportError))
1245 PyErr_Clear();
1246 else
1247 return NULL;
1248 }
1249
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001250 Py_INCREF(Py_None);
1251 return Py_None;
1252 }
1253
1254 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1255 {
1256 Py_DECREF(module);
1257 return NULL;
1258 }
1259
1260 loader->module = module;
1261
1262 return (PyObject *) loader;
1263}
1264
1265 static PyObject *
1266VimPathHook(PyObject *self UNUSED, PyObject *args)
1267{
1268 char *path;
1269
1270 if (PyArg_ParseTuple(args, "s", &path)
1271 && STRCMP(path, vim_special_path) == 0)
1272 {
1273 Py_INCREF(vim_module);
1274 return vim_module;
1275 }
1276
1277 PyErr_Clear();
1278 PyErr_SetNone(PyExc_ImportError);
1279 return NULL;
1280}
1281
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001282/*
1283 * Vim module - Definitions
1284 */
1285
1286static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001287 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001288 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001289 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001290 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1291 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001292 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1293 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001294 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001295 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001296 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1297 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1298 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001299};
1300
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001301/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001302 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001303 */
1304
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001305static PyTypeObject IterType;
1306
1307typedef PyObject *(*nextfun)(void **);
1308typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001309typedef int (*traversefun)(void *, visitproc, void *);
1310typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001311
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001312/* Main purpose of this object is removing the need for do python
1313 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1314 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001315
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001316typedef struct
1317{
1318 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001319 void *cur;
1320 nextfun next;
1321 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001322 traversefun traverse;
1323 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001324} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001325
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001326 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001327IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1328 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001329{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001330 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001331
Bram Moolenaar774267b2013-05-21 20:51:59 +02001332 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001333 self->cur = start;
1334 self->next = next;
1335 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001336 self->traverse = traverse;
1337 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001338
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001339 return (PyObject *)(self);
1340}
1341
1342 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001343IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001344{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001345 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001346 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001347 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001348}
1349
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001350 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001351IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001353 if (self->traverse != NULL)
1354 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001355 else
1356 return 0;
1357}
1358
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001359/* Mac OSX defines clear() somewhere. */
1360#ifdef clear
1361# undef clear
1362#endif
1363
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001364 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001365IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001367 if (self->clear != NULL)
1368 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001369 else
1370 return 0;
1371}
1372
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001373 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001374IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001375{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001376 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001377}
1378
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001379 static PyObject *
1380IterIter(PyObject *self)
1381{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001382 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001383 return self;
1384}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001385
Bram Moolenaardb913952012-06-29 12:54:53 +02001386typedef struct pylinkedlist_S {
1387 struct pylinkedlist_S *pll_next;
1388 struct pylinkedlist_S *pll_prev;
1389 PyObject *pll_obj;
1390} pylinkedlist_T;
1391
1392static pylinkedlist_T *lastdict = NULL;
1393static pylinkedlist_T *lastlist = NULL;
1394
1395 static void
1396pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1397{
1398 if (ref->pll_prev == NULL)
1399 {
1400 if (ref->pll_next == NULL)
1401 {
1402 *last = NULL;
1403 return;
1404 }
1405 }
1406 else
1407 ref->pll_prev->pll_next = ref->pll_next;
1408
1409 if (ref->pll_next == NULL)
1410 *last = ref->pll_prev;
1411 else
1412 ref->pll_next->pll_prev = ref->pll_prev;
1413}
1414
1415 static void
1416pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1417{
1418 if (*last == NULL)
1419 ref->pll_prev = NULL;
1420 else
1421 {
1422 (*last)->pll_next = ref;
1423 ref->pll_prev = *last;
1424 }
1425 ref->pll_next = NULL;
1426 ref->pll_obj = self;
1427 *last = ref;
1428}
1429
1430static PyTypeObject DictionaryType;
1431
1432typedef struct
1433{
1434 PyObject_HEAD
1435 dict_T *dict;
1436 pylinkedlist_T ref;
1437} DictionaryObject;
1438
Bram Moolenaara9922d62013-05-30 13:01:18 +02001439static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1440
1441#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1442
Bram Moolenaardb913952012-06-29 12:54:53 +02001443 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001444DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001445{
1446 DictionaryObject *self;
1447
Bram Moolenaara9922d62013-05-30 13:01:18 +02001448 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001449 if (self == NULL)
1450 return NULL;
1451 self->dict = dict;
1452 ++dict->dv_refcount;
1453
1454 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1455
1456 return (PyObject *)(self);
1457}
1458
Bram Moolenaara9922d62013-05-30 13:01:18 +02001459 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001460py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001461{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001462 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001463
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001464 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001465 {
1466 PyErr_NoMemory();
1467 return NULL;
1468 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001469 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001470
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001471 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001472}
1473
1474 static PyObject *
1475DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1476{
1477 DictionaryObject *self;
1478 dict_T *dict;
1479
1480 if (!(dict = py_dict_alloc()))
1481 return NULL;
1482
1483 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1484
1485 --dict->dv_refcount;
1486
1487 if (kwargs || PyTuple_Size(args))
1488 {
1489 PyObject *tmp;
1490 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1491 {
1492 Py_DECREF(self);
1493 return NULL;
1494 }
1495
1496 Py_DECREF(tmp);
1497 }
1498
1499 return (PyObject *)(self);
1500}
1501
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001502 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001503DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001505 pyll_remove(&self->ref, &lastdict);
1506 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001507
1508 DESTRUCTOR_FINISH(self);
1509}
1510
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001511static char *DictionaryAttrs[] = {
1512 "locked", "scope",
1513 NULL
1514};
1515
1516 static PyObject *
1517DictionaryDir(PyObject *self)
1518{
1519 return ObjectDir(self, DictionaryAttrs);
1520}
1521
Bram Moolenaardb913952012-06-29 12:54:53 +02001522 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001523DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001524{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001525 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001526 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001527 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001528 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001529 return -1;
1530 }
1531
1532 if (strcmp(name, "locked") == 0)
1533 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001534 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001535 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001536 PyErr_SET_STRING(PyExc_TypeError,
1537 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001538 return -1;
1539 }
1540 else
1541 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001542 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001543 if (istrue == -1)
1544 return -1;
1545 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001546 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001547 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001548 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001549 }
1550 return 0;
1551 }
1552 else
1553 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001554 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001555 return -1;
1556 }
1557}
1558
1559 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001560DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001561{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001562 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001563}
1564
Bram Moolenaara9922d62013-05-30 13:01:18 +02001565#define DICT_FLAG_HAS_DEFAULT 0x01
1566#define DICT_FLAG_POP 0x02
1567#define DICT_FLAG_NONE_DEFAULT 0x04
1568#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1569#define DICT_FLAG_RETURN_PAIR 0x10
1570
Bram Moolenaardb913952012-06-29 12:54:53 +02001571 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001572_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001573{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001574 PyObject *keyObject;
1575 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001576 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001577 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001578 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001579 dict_T *dict = self->dict;
1580 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001581 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001582
Bram Moolenaara9922d62013-05-30 13:01:18 +02001583 if (flags & DICT_FLAG_HAS_DEFAULT)
1584 {
1585 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1586 return NULL;
1587 }
1588 else
1589 keyObject = args;
1590
1591 if (flags & DICT_FLAG_RETURN_BOOL)
1592 defObject = Py_False;
1593
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001594 if (!(key = StringToChars(keyObject, &todecref)))
1595 return NULL;
1596
1597 if (*key == NUL)
1598 {
1599 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001600 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001601 return NULL;
1602 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001603
Bram Moolenaara9922d62013-05-30 13:01:18 +02001604 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001605
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001606 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001607
Bram Moolenaara9922d62013-05-30 13:01:18 +02001608 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001609 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001610 if (defObject)
1611 {
1612 Py_INCREF(defObject);
1613 return defObject;
1614 }
1615 else
1616 {
1617 PyErr_SetObject(PyExc_KeyError, keyObject);
1618 return NULL;
1619 }
1620 }
1621 else if (flags & DICT_FLAG_RETURN_BOOL)
1622 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001623 ret = Py_True;
1624 Py_INCREF(ret);
1625 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001626 }
1627
1628 di = dict_lookup(hi);
1629
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001630 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001631 return NULL;
1632
1633 if (flags & DICT_FLAG_POP)
1634 {
1635 if (dict->dv_lock)
1636 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001637 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001638 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001639 return NULL;
1640 }
1641
1642 hash_remove(&dict->dv_hashtab, hi);
1643 dictitem_free(di);
1644 }
1645
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001646 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001647}
1648
1649 static PyObject *
1650DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1651{
1652 return _DictionaryItem(self, keyObject, 0);
1653}
1654
1655 static int
1656DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1657{
1658 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001659 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001660
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001661 if (rObj == NULL)
1662 return -1;
1663
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001665
Bram Moolenaardee2e312013-06-23 16:35:47 +02001666 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001667
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001668 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001669}
1670
1671typedef struct
1672{
1673 hashitem_T *ht_array;
1674 long_u ht_used;
1675 hashtab_T *ht;
1676 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001677 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001678} dictiterinfo_T;
1679
1680 static PyObject *
1681DictionaryIterNext(dictiterinfo_T **dii)
1682{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001683 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001684
1685 if (!(*dii)->todo)
1686 return NULL;
1687
1688 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1689 (*dii)->ht->ht_used != (*dii)->ht_used)
1690 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001691 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001692 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001693 return NULL;
1694 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001695
Bram Moolenaara9922d62013-05-30 13:01:18 +02001696 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1697 ++((*dii)->hi);
1698
1699 --((*dii)->todo);
1700
Bram Moolenaar41009372013-07-01 22:03:04 +02001701 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702 return NULL;
1703
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001704 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705}
1706
1707 static PyObject *
1708DictionaryIter(DictionaryObject *self)
1709{
1710 dictiterinfo_T *dii;
1711 hashtab_T *ht;
1712
1713 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1714 {
1715 PyErr_NoMemory();
1716 return NULL;
1717 }
1718
1719 ht = &self->dict->dv_hashtab;
1720 dii->ht_array = ht->ht_array;
1721 dii->ht_used = ht->ht_used;
1722 dii->ht = ht;
1723 dii->hi = dii->ht_array;
1724 dii->todo = dii->ht_used;
1725
1726 return IterNew(dii,
1727 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1728 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001729}
1730
1731 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001732DictionaryAssItem(
1733 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001734{
1735 char_u *key;
1736 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001737 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001738 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001739 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001740
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001741 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001742 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001743 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001744 return -1;
1745 }
1746
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001747 if (!(key = StringToChars(keyObject, &todecref)))
1748 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001749
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001750 if (*key == NUL)
1751 {
1752 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001753 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001754 return -1;
1755 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001756
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001757 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001758
1759 if (valObject == NULL)
1760 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001761 hashitem_T *hi;
1762
Bram Moolenaardb913952012-06-29 12:54:53 +02001763 if (di == NULL)
1764 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001765 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001766 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001767 return -1;
1768 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001769 hi = hash_find(&dict->dv_hashtab, di->di_key);
1770 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001771 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001772 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001773 return 0;
1774 }
1775
1776 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001777 {
1778 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001779 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001780 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001781
1782 if (di == NULL)
1783 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001784 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001785 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001786 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001787 PyErr_NoMemory();
1788 return -1;
1789 }
1790 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001791 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001792
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001793 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001794 {
1795 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001797 RAISE_KEY_ADD_FAIL(key);
1798 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001799 return -1;
1800 }
1801 }
1802 else
1803 clear_tv(&di->di_tv);
1804
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001805 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001806
1807 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001808 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001809 return 0;
1810}
1811
Bram Moolenaara9922d62013-05-30 13:01:18 +02001812typedef PyObject *(*hi_to_py)(hashitem_T *);
1813
Bram Moolenaardb913952012-06-29 12:54:53 +02001814 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001815DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001816{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001817 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001818 long_u todo = dict->dv_hashtab.ht_used;
1819 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001820 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001821 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001822 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001823
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001824 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001825 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1826 {
1827 if (!HASHITEM_EMPTY(hi))
1828 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 if (!(newObj = hiconvert(hi)))
1830 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001831 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001832 return NULL;
1833 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001834 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001835 --todo;
1836 ++i;
1837 }
1838 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001839 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001840}
1841
Bram Moolenaara9922d62013-05-30 13:01:18 +02001842 static PyObject *
1843dict_key(hashitem_T *hi)
1844{
1845 return PyBytes_FromString((char *)(hi->hi_key));
1846}
1847
1848 static PyObject *
1849DictionaryListKeys(DictionaryObject *self)
1850{
1851 return DictionaryListObjects(self, dict_key);
1852}
1853
1854 static PyObject *
1855dict_val(hashitem_T *hi)
1856{
1857 dictitem_T *di;
1858
1859 di = dict_lookup(hi);
1860 return ConvertToPyObject(&di->di_tv);
1861}
1862
1863 static PyObject *
1864DictionaryListValues(DictionaryObject *self)
1865{
1866 return DictionaryListObjects(self, dict_val);
1867}
1868
1869 static PyObject *
1870dict_item(hashitem_T *hi)
1871{
1872 PyObject *keyObject;
1873 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001874 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001875
1876 if (!(keyObject = dict_key(hi)))
1877 return NULL;
1878
1879 if (!(valObject = dict_val(hi)))
1880 {
1881 Py_DECREF(keyObject);
1882 return NULL;
1883 }
1884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001885 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001886
1887 Py_DECREF(keyObject);
1888 Py_DECREF(valObject);
1889
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001890 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001891}
1892
1893 static PyObject *
1894DictionaryListItems(DictionaryObject *self)
1895{
1896 return DictionaryListObjects(self, dict_item);
1897}
1898
1899 static PyObject *
1900DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1901{
1902 dict_T *dict = self->dict;
1903
1904 if (dict->dv_lock)
1905 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001906 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001907 return NULL;
1908 }
1909
1910 if (kwargs)
1911 {
1912 typval_T tv;
1913
1914 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1915 return NULL;
1916
1917 VimTryStart();
1918 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1919 clear_tv(&tv);
1920 if (VimTryEnd())
1921 return NULL;
1922 }
1923 else
1924 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001925 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001926
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001927 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001928 return NULL;
1929
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001930 if (obj == NULL)
1931 {
1932 Py_INCREF(Py_None);
1933 return Py_None;
1934 }
1935
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001936 if (PyObject_HasAttrString(obj, "keys"))
1937 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001938 else
1939 {
1940 PyObject *iterator;
1941 PyObject *item;
1942
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001943 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001944 return NULL;
1945
1946 while ((item = PyIter_Next(iterator)))
1947 {
1948 PyObject *fast;
1949 PyObject *keyObject;
1950 PyObject *valObject;
1951 PyObject *todecref;
1952 char_u *key;
1953 dictitem_T *di;
1954
1955 if (!(fast = PySequence_Fast(item, "")))
1956 {
1957 Py_DECREF(iterator);
1958 Py_DECREF(item);
1959 return NULL;
1960 }
1961
1962 Py_DECREF(item);
1963
1964 if (PySequence_Fast_GET_SIZE(fast) != 2)
1965 {
1966 Py_DECREF(iterator);
1967 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001968 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001969 N_("expected sequence element of size 2, "
1970 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001971 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001972 return NULL;
1973 }
1974
1975 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1976
1977 if (!(key = StringToChars(keyObject, &todecref)))
1978 {
1979 Py_DECREF(iterator);
1980 Py_DECREF(fast);
1981 return NULL;
1982 }
1983
1984 di = dictitem_alloc(key);
1985
1986 Py_XDECREF(todecref);
1987
1988 if (di == NULL)
1989 {
1990 Py_DECREF(fast);
1991 Py_DECREF(iterator);
1992 PyErr_NoMemory();
1993 return NULL;
1994 }
1995 di->di_tv.v_lock = 0;
1996 di->di_tv.v_type = VAR_UNKNOWN;
1997
1998 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1999
2000 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2001 {
2002 Py_DECREF(iterator);
2003 Py_DECREF(fast);
2004 dictitem_free(di);
2005 return NULL;
2006 }
2007
2008 Py_DECREF(fast);
2009
2010 if (dict_add(dict, di) == FAIL)
2011 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002012 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002013 Py_DECREF(iterator);
2014 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002015 return NULL;
2016 }
2017 }
2018
2019 Py_DECREF(iterator);
2020
2021 /* Iterator may have finished due to an exception */
2022 if (PyErr_Occurred())
2023 return NULL;
2024 }
2025 }
2026 Py_INCREF(Py_None);
2027 return Py_None;
2028}
2029
2030 static PyObject *
2031DictionaryGet(DictionaryObject *self, PyObject *args)
2032{
2033 return _DictionaryItem(self, args,
2034 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2035}
2036
2037 static PyObject *
2038DictionaryPop(DictionaryObject *self, PyObject *args)
2039{
2040 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2041}
2042
2043 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002044DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002046 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002047 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002048 PyObject *valObject;
2049 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002050
Bram Moolenaarde71b562013-06-02 17:41:54 +02002051 if (self->dict->dv_hashtab.ht_used == 0)
2052 {
2053 PyErr_SetNone(PyExc_KeyError);
2054 return NULL;
2055 }
2056
2057 hi = self->dict->dv_hashtab.ht_array;
2058 while (HASHITEM_EMPTY(hi))
2059 ++hi;
2060
2061 di = dict_lookup(hi);
2062
2063 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002064 return NULL;
2065
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002066 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002067 {
2068 Py_DECREF(valObject);
2069 return NULL;
2070 }
2071
2072 hash_remove(&self->dict->dv_hashtab, hi);
2073 dictitem_free(di);
2074
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002075 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002076}
2077
2078 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002079DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002080{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002081 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2082}
2083
2084static PySequenceMethods DictionaryAsSeq = {
2085 0, /* sq_length */
2086 0, /* sq_concat */
2087 0, /* sq_repeat */
2088 0, /* sq_item */
2089 0, /* sq_slice */
2090 0, /* sq_ass_item */
2091 0, /* sq_ass_slice */
2092 (objobjproc) DictionaryContains, /* sq_contains */
2093 0, /* sq_inplace_concat */
2094 0, /* sq_inplace_repeat */
2095};
2096
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002097static PyMappingMethods DictionaryAsMapping = {
2098 (lenfunc) DictionaryLength,
2099 (binaryfunc) DictionaryItem,
2100 (objobjargproc) DictionaryAssItem,
2101};
2102
Bram Moolenaardb913952012-06-29 12:54:53 +02002103static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002104 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002105 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2106 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2107 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2108 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2109 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002110 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002111 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002112 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2113 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002114};
2115
2116static PyTypeObject ListType;
2117
2118typedef struct
2119{
2120 PyObject_HEAD
2121 list_T *list;
2122 pylinkedlist_T ref;
2123} ListObject;
2124
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002125#define NEW_LIST(list) ListNew(&ListType, list)
2126
Bram Moolenaardb913952012-06-29 12:54:53 +02002127 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002128ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002129{
2130 ListObject *self;
2131
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002132 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002133 if (self == NULL)
2134 return NULL;
2135 self->list = list;
2136 ++list->lv_refcount;
2137
2138 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2139
2140 return (PyObject *)(self);
2141}
2142
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002143 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002144py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002145{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002146 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002147
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002148 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002149 {
2150 PyErr_NoMemory();
2151 return NULL;
2152 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002153 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002154
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002155 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002156}
2157
2158 static int
2159list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2160{
2161 PyObject *iterator;
2162 PyObject *item;
2163 listitem_T *li;
2164
2165 if (!(iterator = PyObject_GetIter(obj)))
2166 return -1;
2167
2168 while ((item = PyIter_Next(iterator)))
2169 {
2170 if (!(li = listitem_alloc()))
2171 {
2172 PyErr_NoMemory();
2173 Py_DECREF(item);
2174 Py_DECREF(iterator);
2175 return -1;
2176 }
2177 li->li_tv.v_lock = 0;
2178 li->li_tv.v_type = VAR_UNKNOWN;
2179
2180 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2181 {
2182 Py_DECREF(item);
2183 Py_DECREF(iterator);
2184 listitem_free(li);
2185 return -1;
2186 }
2187
2188 Py_DECREF(item);
2189
2190 list_append(l, li);
2191 }
2192
2193 Py_DECREF(iterator);
2194
2195 /* Iterator may have finished due to an exception */
2196 if (PyErr_Occurred())
2197 return -1;
2198
2199 return 0;
2200}
2201
2202 static PyObject *
2203ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2204{
2205 list_T *list;
2206 PyObject *obj = NULL;
2207
2208 if (kwargs)
2209 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002210 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002211 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002212 return NULL;
2213 }
2214
2215 if (!PyArg_ParseTuple(args, "|O", &obj))
2216 return NULL;
2217
2218 if (!(list = py_list_alloc()))
2219 return NULL;
2220
2221 if (obj)
2222 {
2223 PyObject *lookup_dict;
2224
2225 if (!(lookup_dict = PyDict_New()))
2226 {
2227 list_unref(list);
2228 return NULL;
2229 }
2230
2231 if (list_py_concat(list, obj, lookup_dict) == -1)
2232 {
2233 Py_DECREF(lookup_dict);
2234 list_unref(list);
2235 return NULL;
2236 }
2237
2238 Py_DECREF(lookup_dict);
2239 }
2240
2241 return ListNew(subtype, list);
2242}
2243
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002244 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002245ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002246{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002247 pyll_remove(&self->ref, &lastlist);
2248 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002249
2250 DESTRUCTOR_FINISH(self);
2251}
2252
Bram Moolenaardb913952012-06-29 12:54:53 +02002253 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002254ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002255{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002256 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002257}
2258
2259 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002260ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002261{
2262 listitem_T *li;
2263
Bram Moolenaard6e39182013-05-21 18:30:34 +02002264 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002265 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002266 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002267 return NULL;
2268 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002269 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002270 if (li == NULL)
2271 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002272 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002273 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002274 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002275 return NULL;
2276 }
2277 return ConvertToPyObject(&li->li_tv);
2278}
2279
Bram Moolenaardb913952012-06-29 12:54:53 +02002280 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002281ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2282 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002283{
2284 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002285 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002286
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002287 if (step == 0)
2288 {
2289 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2290 return NULL;
2291 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002292
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002293 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002294 if (list == NULL)
2295 return NULL;
2296
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002297 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002298 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002299 PyObject *item;
2300
2301 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002302 if (item == NULL)
2303 {
2304 Py_DECREF(list);
2305 return NULL;
2306 }
2307
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002308 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002309 }
2310
2311 return list;
2312}
2313
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002314 static PyObject *
2315ListItem(ListObject *self, PyObject* idx)
2316{
2317#if PY_MAJOR_VERSION < 3
2318 if (PyInt_Check(idx))
2319 {
2320 long _idx = PyInt_AsLong(idx);
2321 return ListIndex(self, _idx);
2322 }
2323 else
2324#endif
2325 if (PyLong_Check(idx))
2326 {
2327 long _idx = PyLong_AsLong(idx);
2328 return ListIndex(self, _idx);
2329 }
2330 else if (PySlice_Check(idx))
2331 {
2332 Py_ssize_t start, stop, step, slicelen;
2333
Bram Moolenaar922a4662014-03-30 16:11:43 +02002334 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002335 &start, &stop, &step, &slicelen) < 0)
2336 return NULL;
2337 return ListSlice(self, start, step, slicelen);
2338 }
2339 else
2340 {
2341 RAISE_INVALID_INDEX_TYPE(idx);
2342 return NULL;
2343 }
2344}
2345
2346 static void
2347list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2348 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2349{
2350 while (numreplaced--)
2351 {
2352 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2353 listitem_remove(l, lis[slicelen + numreplaced]);
2354 }
2355 while (numadded--)
2356 {
2357 listitem_T *next;
2358
2359 next = lastaddedli->li_prev;
2360 listitem_remove(l, lastaddedli);
2361 lastaddedli = next;
2362 }
2363}
2364
2365 static int
2366ListAssSlice(ListObject *self, Py_ssize_t first,
2367 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2368{
2369 PyObject *iterator;
2370 PyObject *item;
2371 listitem_T *li;
2372 listitem_T *lastaddedli = NULL;
2373 listitem_T *next;
2374 typval_T v;
2375 list_T *l = self->list;
2376 PyInt i;
2377 PyInt j;
2378 PyInt numreplaced = 0;
2379 PyInt numadded = 0;
2380 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002381 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002382
2383 size = ListLength(self);
2384
2385 if (l->lv_lock)
2386 {
2387 RAISE_LOCKED_LIST;
2388 return -1;
2389 }
2390
2391 if (step == 0)
2392 {
2393 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2394 return -1;
2395 }
2396
2397 if (step != 1 && slicelen == 0)
2398 {
2399 /* Nothing to do. Only error out if obj has some items. */
2400 int ret = 0;
2401
2402 if (obj == NULL)
2403 return 0;
2404
2405 if (!(iterator = PyObject_GetIter(obj)))
2406 return -1;
2407
2408 if ((item = PyIter_Next(iterator)))
2409 {
2410 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002411 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002412 "to extended slice"), 0);
2413 Py_DECREF(item);
2414 ret = -1;
2415 }
2416 Py_DECREF(iterator);
2417 return ret;
2418 }
2419
2420 if (obj != NULL)
2421 /* XXX May allocate zero bytes. */
2422 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2423 {
2424 PyErr_NoMemory();
2425 return -1;
2426 }
2427
2428 if (first == size)
2429 li = NULL;
2430 else
2431 {
2432 li = list_find(l, (long) first);
2433 if (li == NULL)
2434 {
2435 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2436 (int)first);
2437 if (obj != NULL)
2438 PyMem_Free(lis);
2439 return -1;
2440 }
2441 i = slicelen;
2442 while (i-- && li != NULL)
2443 {
2444 j = step;
2445 next = li;
2446 if (step > 0)
2447 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2448 else
2449 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2450
2451 if (obj == NULL)
2452 listitem_remove(l, li);
2453 else
2454 lis[slicelen - i - 1] = li;
2455
2456 li = next;
2457 }
2458 if (li == NULL && i != -1)
2459 {
2460 PyErr_SET_VIM(N_("internal error: not enough list items"));
2461 if (obj != NULL)
2462 PyMem_Free(lis);
2463 return -1;
2464 }
2465 }
2466
2467 if (obj == NULL)
2468 return 0;
2469
2470 if (!(iterator = PyObject_GetIter(obj)))
2471 {
2472 PyMem_Free(lis);
2473 return -1;
2474 }
2475
2476 i = 0;
2477 while ((item = PyIter_Next(iterator)))
2478 {
2479 if (ConvertFromPyObject(item, &v) == -1)
2480 {
2481 Py_DECREF(iterator);
2482 Py_DECREF(item);
2483 PyMem_Free(lis);
2484 return -1;
2485 }
2486 Py_DECREF(item);
2487 if (list_insert_tv(l, &v, numreplaced < slicelen
2488 ? lis[numreplaced]
2489 : li) == FAIL)
2490 {
2491 clear_tv(&v);
2492 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2493 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2494 PyMem_Free(lis);
2495 return -1;
2496 }
2497 if (numreplaced < slicelen)
2498 {
2499 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002500 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002501 numreplaced++;
2502 }
2503 else
2504 {
2505 if (li)
2506 lastaddedli = li->li_prev;
2507 else
2508 lastaddedli = l->lv_last;
2509 numadded++;
2510 }
2511 clear_tv(&v);
2512 if (step != 1 && i >= slicelen)
2513 {
2514 Py_DECREF(iterator);
2515 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002516 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002517 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002518 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2519 PyMem_Free(lis);
2520 return -1;
2521 }
2522 ++i;
2523 }
2524 Py_DECREF(iterator);
2525
2526 if (step != 1 && i != slicelen)
2527 {
2528 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002529 N_("attempt to assign sequence of size %d to extended slice "
2530 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002531 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2532 PyMem_Free(lis);
2533 return -1;
2534 }
2535
2536 if (PyErr_Occurred())
2537 {
2538 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2539 PyMem_Free(lis);
2540 return -1;
2541 }
2542
2543 for (i = 0; i < numreplaced; i++)
2544 listitem_free(lis[i]);
2545 if (step == 1)
2546 for (i = numreplaced; i < slicelen; i++)
2547 listitem_remove(l, lis[i]);
2548
2549 PyMem_Free(lis);
2550
2551 return 0;
2552}
2553
2554 static int
2555ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2556{
2557 typval_T tv;
2558 list_T *l = self->list;
2559 listitem_T *li;
2560 Py_ssize_t length = ListLength(self);
2561
2562 if (l->lv_lock)
2563 {
2564 RAISE_LOCKED_LIST;
2565 return -1;
2566 }
2567 if (index > length || (index == length && obj == NULL))
2568 {
2569 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2570 return -1;
2571 }
2572
2573 if (obj == NULL)
2574 {
2575 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002576 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002577 clear_tv(&li->li_tv);
2578 vim_free(li);
2579 return 0;
2580 }
2581
2582 if (ConvertFromPyObject(obj, &tv) == -1)
2583 return -1;
2584
2585 if (index == length)
2586 {
2587 if (list_append_tv(l, &tv) == FAIL)
2588 {
2589 clear_tv(&tv);
2590 PyErr_SET_VIM(N_("failed to add item to list"));
2591 return -1;
2592 }
2593 }
2594 else
2595 {
2596 li = list_find(l, (long) index);
2597 clear_tv(&li->li_tv);
2598 copy_tv(&tv, &li->li_tv);
2599 clear_tv(&tv);
2600 }
2601 return 0;
2602}
2603
2604 static Py_ssize_t
2605ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2606{
2607#if PY_MAJOR_VERSION < 3
2608 if (PyInt_Check(idx))
2609 {
2610 long _idx = PyInt_AsLong(idx);
2611 return ListAssIndex(self, _idx, obj);
2612 }
2613 else
2614#endif
2615 if (PyLong_Check(idx))
2616 {
2617 long _idx = PyLong_AsLong(idx);
2618 return ListAssIndex(self, _idx, obj);
2619 }
2620 else if (PySlice_Check(idx))
2621 {
2622 Py_ssize_t start, stop, step, slicelen;
2623
Bram Moolenaar922a4662014-03-30 16:11:43 +02002624 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002625 &start, &stop, &step, &slicelen) < 0)
2626 return -1;
2627 return ListAssSlice(self, start, step, slicelen,
2628 obj);
2629 }
2630 else
2631 {
2632 RAISE_INVALID_INDEX_TYPE(idx);
2633 return -1;
2634 }
2635}
2636
2637 static PyObject *
2638ListConcatInPlace(ListObject *self, PyObject *obj)
2639{
2640 list_T *l = self->list;
2641 PyObject *lookup_dict;
2642
2643 if (l->lv_lock)
2644 {
2645 RAISE_LOCKED_LIST;
2646 return NULL;
2647 }
2648
2649 if (!(lookup_dict = PyDict_New()))
2650 return NULL;
2651
2652 if (list_py_concat(l, obj, lookup_dict) == -1)
2653 {
2654 Py_DECREF(lookup_dict);
2655 return NULL;
2656 }
2657 Py_DECREF(lookup_dict);
2658
2659 Py_INCREF(self);
2660 return (PyObject *)(self);
2661}
2662
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002663typedef struct
2664{
2665 listwatch_T lw;
2666 list_T *list;
2667} listiterinfo_T;
2668
2669 static void
2670ListIterDestruct(listiterinfo_T *lii)
2671{
2672 list_rem_watch(lii->list, &lii->lw);
2673 PyMem_Free(lii);
2674}
2675
2676 static PyObject *
2677ListIterNext(listiterinfo_T **lii)
2678{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002679 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002680
2681 if (!((*lii)->lw.lw_item))
2682 return NULL;
2683
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002684 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002685 return NULL;
2686
2687 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2688
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002689 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002690}
2691
2692 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002693ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002694{
2695 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002696 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002697
2698 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2699 {
2700 PyErr_NoMemory();
2701 return NULL;
2702 }
2703
2704 list_add_watch(l, &lii->lw);
2705 lii->lw.lw_item = l->lv_first;
2706 lii->list = l;
2707
2708 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002709 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2710 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002711}
2712
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002713static char *ListAttrs[] = {
2714 "locked",
2715 NULL
2716};
2717
2718 static PyObject *
2719ListDir(PyObject *self)
2720{
2721 return ObjectDir(self, ListAttrs);
2722}
2723
Bram Moolenaar66b79852012-09-21 14:00:35 +02002724 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002725ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002726{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002727 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002728 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002729 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002730 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002731 return -1;
2732 }
2733
2734 if (strcmp(name, "locked") == 0)
2735 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002736 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002737 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002738 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002739 return -1;
2740 }
2741 else
2742 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002743 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002744 if (istrue == -1)
2745 return -1;
2746 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002747 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002748 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002749 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002750 }
2751 return 0;
2752 }
2753 else
2754 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002755 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002756 return -1;
2757 }
2758}
2759
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002760static PySequenceMethods ListAsSeq = {
2761 (lenfunc) ListLength, /* sq_length, len(x) */
2762 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2763 0, /* RangeRepeat, sq_repeat, x*n */
2764 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2765 0, /* was_sq_slice, x[i:j] */
2766 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2767 0, /* was_sq_ass_slice, x[i:j]=v */
2768 0, /* sq_contains */
2769 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2770 0, /* sq_inplace_repeat */
2771};
2772
2773static PyMappingMethods ListAsMapping = {
2774 /* mp_length */ (lenfunc) ListLength,
2775 /* mp_subscript */ (binaryfunc) ListItem,
2776 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2777};
2778
Bram Moolenaardb913952012-06-29 12:54:53 +02002779static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002780 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2781 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2782 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002783};
2784
2785typedef struct
2786{
2787 PyObject_HEAD
2788 char_u *name;
2789} FunctionObject;
2790
2791static PyTypeObject FunctionType;
2792
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002793#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2794
Bram Moolenaardb913952012-06-29 12:54:53 +02002795 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002796FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002797{
2798 FunctionObject *self;
2799
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002800 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2801
Bram Moolenaardb913952012-06-29 12:54:53 +02002802 if (self == NULL)
2803 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002804
2805 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002806 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002807 if (!translated_function_exists(name))
2808 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002809 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002810 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002811 return NULL;
2812 }
2813 self->name = vim_strsave(name);
2814 func_ref(self->name);
2815 }
2816 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002817 if ((self->name = get_expanded_name(name,
2818 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2819 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002820 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002821 PyErr_FORMAT(PyExc_ValueError,
2822 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002823 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002824 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002825
2826 return (PyObject *)(self);
2827}
2828
2829 static PyObject *
2830FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2831{
2832 PyObject *self;
2833 char_u *name;
2834
2835 if (kwargs)
2836 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002837 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002838 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002839 return NULL;
2840 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002841
Bram Moolenaar389a1792013-06-23 13:00:44 +02002842 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002843 return NULL;
2844
2845 self = FunctionNew(subtype, name);
2846
Bram Moolenaar389a1792013-06-23 13:00:44 +02002847 PyMem_Free(name);
2848
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002849 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002850}
2851
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002852 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002853FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002854{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002855 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002856 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002857
2858 DESTRUCTOR_FINISH(self);
2859}
2860
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002861static char *FunctionAttrs[] = {
2862 "softspace",
2863 NULL
2864};
2865
2866 static PyObject *
2867FunctionDir(PyObject *self)
2868{
2869 return ObjectDir(self, FunctionAttrs);
2870}
2871
Bram Moolenaardb913952012-06-29 12:54:53 +02002872 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002873FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002874{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002875 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002876 typval_T args;
2877 typval_T selfdicttv;
2878 typval_T rettv;
2879 dict_T *selfdict = NULL;
2880 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002881 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002882 int error;
2883
2884 if (ConvertFromPyObject(argsObject, &args) == -1)
2885 return NULL;
2886
2887 if (kwargs != NULL)
2888 {
2889 selfdictObject = PyDict_GetItemString(kwargs, "self");
2890 if (selfdictObject != NULL)
2891 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002892 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002893 {
2894 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002895 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002896 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002897 selfdict = selfdicttv.vval.v_dict;
2898 }
2899 }
2900
Bram Moolenaar71700b82013-05-15 17:49:05 +02002901 Py_BEGIN_ALLOW_THREADS
2902 Python_Lock_Vim();
2903
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002904 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002905 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002906
2907 Python_Release_Vim();
2908 Py_END_ALLOW_THREADS
2909
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002910 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002911 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002912 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002913 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002914 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002915 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002916 }
2917 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002918 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002919
Bram Moolenaardb913952012-06-29 12:54:53 +02002920 clear_tv(&args);
2921 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002922 if (selfdict != NULL)
2923 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002924
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002925 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002926}
2927
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002928 static PyObject *
2929FunctionRepr(FunctionObject *self)
2930{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002931#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002932 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002933 * Finalize */
2934 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002935 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002936#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002937 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002938#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002939}
2940
Bram Moolenaardb913952012-06-29 12:54:53 +02002941static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002942 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2943 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002944};
2945
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002946/*
2947 * Options object
2948 */
2949
2950static PyTypeObject OptionsType;
2951
2952typedef int (*checkfun)(void *);
2953
2954typedef struct
2955{
2956 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002957 int opt_type;
2958 void *from;
2959 checkfun Check;
2960 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002961} OptionsObject;
2962
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002963 static int
2964dummy_check(void *arg UNUSED)
2965{
2966 return 0;
2967}
2968
2969 static PyObject *
2970OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2971{
2972 OptionsObject *self;
2973
Bram Moolenaar774267b2013-05-21 20:51:59 +02002974 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002975 if (self == NULL)
2976 return NULL;
2977
2978 self->opt_type = opt_type;
2979 self->from = from;
2980 self->Check = Check;
2981 self->fromObj = fromObj;
2982 if (fromObj)
2983 Py_INCREF(fromObj);
2984
2985 return (PyObject *)(self);
2986}
2987
2988 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002989OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002990{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002991 PyObject_GC_UnTrack((void *)(self));
2992 Py_XDECREF(self->fromObj);
2993 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002994}
2995
2996 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002997OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002998{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002999 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003000 return 0;
3001}
3002
3003 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003004OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003005{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003006 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003007 return 0;
3008}
3009
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003010 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003011OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003012{
3013 char_u *key;
3014 int flags;
3015 long numval;
3016 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003017 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003018
Bram Moolenaard6e39182013-05-21 18:30:34 +02003019 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003020 return NULL;
3021
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003022 if (!(key = StringToChars(keyObject, &todecref)))
3023 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003024
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003025 if (*key == NUL)
3026 {
3027 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003028 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003029 return NULL;
3030 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003031
3032 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003033 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003034
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003035 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003036
3037 if (flags == 0)
3038 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003039 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003040 return NULL;
3041 }
3042
3043 if (flags & SOPT_UNSET)
3044 {
3045 Py_INCREF(Py_None);
3046 return Py_None;
3047 }
3048 else if (flags & SOPT_BOOL)
3049 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003050 PyObject *ret;
3051 ret = numval ? Py_True : Py_False;
3052 Py_INCREF(ret);
3053 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003054 }
3055 else if (flags & SOPT_NUM)
3056 return PyInt_FromLong(numval);
3057 else if (flags & SOPT_STRING)
3058 {
3059 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003060 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003061 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003062 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003063 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003064 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003065 else
3066 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003067 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003068 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003069 return NULL;
3070 }
3071 }
3072 else
3073 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003074 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003075 return NULL;
3076 }
3077}
3078
3079 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003080OptionsContains(OptionsObject *self, PyObject *keyObject)
3081{
3082 char_u *key;
3083 PyObject *todecref;
3084
3085 if (!(key = StringToChars(keyObject, &todecref)))
3086 return -1;
3087
3088 if (*key == NUL)
3089 {
3090 Py_XDECREF(todecref);
3091 return 0;
3092 }
3093
3094 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3095 {
3096 Py_XDECREF(todecref);
3097 return 1;
3098 }
3099 else
3100 {
3101 Py_XDECREF(todecref);
3102 return 0;
3103 }
3104}
3105
3106typedef struct
3107{
3108 void *lastoption;
3109 int opt_type;
3110} optiterinfo_T;
3111
3112 static PyObject *
3113OptionsIterNext(optiterinfo_T **oii)
3114{
3115 char_u *name;
3116
3117 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3118 return PyString_FromString((char *)name);
3119
3120 return NULL;
3121}
3122
3123 static PyObject *
3124OptionsIter(OptionsObject *self)
3125{
3126 optiterinfo_T *oii;
3127
3128 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3129 {
3130 PyErr_NoMemory();
3131 return NULL;
3132 }
3133
3134 oii->opt_type = self->opt_type;
3135 oii->lastoption = NULL;
3136
3137 return IterNew(oii,
3138 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3139 NULL, NULL);
3140}
3141
3142 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003143set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003144{
3145 char_u *errmsg;
3146
3147 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3148 {
3149 if (VimTryEnd())
3150 return FAIL;
3151 PyErr_SetVim((char *)errmsg);
3152 return FAIL;
3153 }
3154 return OK;
3155}
3156
3157 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003158set_option_value_for(
3159 char_u *key,
3160 int numval,
3161 char_u *stringval,
3162 int opt_flags,
3163 int opt_type,
3164 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003165{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003166 win_T *save_curwin = NULL;
3167 tabpage_T *save_curtab = NULL;
3168 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003169 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003170
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003171 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003172 switch (opt_type)
3173 {
3174 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003175 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003176 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003177 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003178 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003179 if (VimTryEnd())
3180 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003181 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003182 return -1;
3183 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003184 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3185 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003186 break;
3187 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003188 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003189 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003190 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003191 break;
3192 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003193 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003194 break;
3195 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003196 if (set_ret == FAIL)
3197 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003198 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003199}
3200
3201 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003202OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003203{
3204 char_u *key;
3205 int flags;
3206 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003207 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003208 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003209
Bram Moolenaard6e39182013-05-21 18:30:34 +02003210 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003211 return -1;
3212
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003213 if (!(key = StringToChars(keyObject, &todecref)))
3214 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003215
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003216 if (*key == NUL)
3217 {
3218 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003219 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003220 return -1;
3221 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003222
3223 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003224 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003225
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003226 if (flags == 0)
3227 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003228 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003229 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003230 return -1;
3231 }
3232
3233 if (valObject == NULL)
3234 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003235 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003236 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003237 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003238 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003239 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003240 return -1;
3241 }
3242 else if (!(flags & SOPT_GLOBAL))
3243 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003244 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003245 N_("unable to unset option %s "
3246 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003247 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003248 return -1;
3249 }
3250 else
3251 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003252 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003253 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003254 return 0;
3255 }
3256 }
3257
Bram Moolenaard6e39182013-05-21 18:30:34 +02003258 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003259
3260 if (flags & SOPT_BOOL)
3261 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003262 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003263
Bram Moolenaarb983f752013-05-15 16:11:50 +02003264 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003265 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003266 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003267 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003268 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003269 }
3270 else if (flags & SOPT_NUM)
3271 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003272 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003273
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003274 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003275 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003276 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003277 return -1;
3278 }
3279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003280 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003281 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003282 }
3283 else
3284 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003285 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003286 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003287
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003288 if ((val = StringToChars(valObject, &todecref2)))
3289 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003290 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003291 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003292 Py_XDECREF(todecref2);
3293 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003294 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003295 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003296 }
3297
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003298 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003299
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003300 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003301}
3302
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003303static PySequenceMethods OptionsAsSeq = {
3304 0, /* sq_length */
3305 0, /* sq_concat */
3306 0, /* sq_repeat */
3307 0, /* sq_item */
3308 0, /* sq_slice */
3309 0, /* sq_ass_item */
3310 0, /* sq_ass_slice */
3311 (objobjproc) OptionsContains, /* sq_contains */
3312 0, /* sq_inplace_concat */
3313 0, /* sq_inplace_repeat */
3314};
3315
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003316static PyMappingMethods OptionsAsMapping = {
3317 (lenfunc) NULL,
3318 (binaryfunc) OptionsItem,
3319 (objobjargproc) OptionsAssItem,
3320};
3321
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003322/* Tabpage object
3323 */
3324
3325typedef struct
3326{
3327 PyObject_HEAD
3328 tabpage_T *tab;
3329} TabPageObject;
3330
3331static PyObject *WinListNew(TabPageObject *tabObject);
3332
3333static PyTypeObject TabPageType;
3334
3335 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003337{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003338 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003339 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003340 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003341 return -1;
3342 }
3343
3344 return 0;
3345}
3346
3347 static PyObject *
3348TabPageNew(tabpage_T *tab)
3349{
3350 TabPageObject *self;
3351
3352 if (TAB_PYTHON_REF(tab))
3353 {
3354 self = TAB_PYTHON_REF(tab);
3355 Py_INCREF(self);
3356 }
3357 else
3358 {
3359 self = PyObject_NEW(TabPageObject, &TabPageType);
3360 if (self == NULL)
3361 return NULL;
3362 self->tab = tab;
3363 TAB_PYTHON_REF(tab) = self;
3364 }
3365
3366 return (PyObject *)(self);
3367}
3368
3369 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003371{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003372 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3373 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003374
3375 DESTRUCTOR_FINISH(self);
3376}
3377
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003378static char *TabPageAttrs[] = {
3379 "windows", "number", "vars", "window", "valid",
3380 NULL
3381};
3382
3383 static PyObject *
3384TabPageDir(PyObject *self)
3385{
3386 return ObjectDir(self, TabPageAttrs);
3387}
3388
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003389 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003390TabPageAttrValid(TabPageObject *self, char *name)
3391{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003392 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003393
3394 if (strcmp(name, "valid") != 0)
3395 return NULL;
3396
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003397 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3398 Py_INCREF(ret);
3399 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003400}
3401
3402 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003403TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003404{
3405 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003406 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003407 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003408 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003409 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003410 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003411 else if (strcmp(name, "window") == 0)
3412 {
3413 /* For current tab window.c does not bother to set or update tp_curwin
3414 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003415 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003416 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003417 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003418 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003419 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003420 else if (strcmp(name, "__members__") == 0)
3421 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003422 return NULL;
3423}
3424
3425 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003426TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003427{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003428 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003429 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003430 else
3431 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003432 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003433
3434 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003435 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3436 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003437 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003438 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003439 }
3440}
3441
3442static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003443 /* name, function, calling, documentation */
3444 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3445 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003446};
3447
3448/*
3449 * Window list object
3450 */
3451
3452static PyTypeObject TabListType;
3453static PySequenceMethods TabListAsSeq;
3454
3455typedef struct
3456{
3457 PyObject_HEAD
3458} TabListObject;
3459
3460 static PyInt
3461TabListLength(PyObject *self UNUSED)
3462{
3463 tabpage_T *tp = first_tabpage;
3464 PyInt n = 0;
3465
3466 while (tp != NULL)
3467 {
3468 ++n;
3469 tp = tp->tp_next;
3470 }
3471
3472 return n;
3473}
3474
3475 static PyObject *
3476TabListItem(PyObject *self UNUSED, PyInt n)
3477{
3478 tabpage_T *tp;
3479
3480 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3481 if (n == 0)
3482 return TabPageNew(tp);
3483
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003484 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003485 return NULL;
3486}
3487
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003488/*
3489 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003490 */
3491
3492typedef struct
3493{
3494 PyObject_HEAD
3495 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003496 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003497} WindowObject;
3498
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003499static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003500
3501 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003502CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003503{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003504 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003505 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003506 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003507 return -1;
3508 }
3509
3510 return 0;
3511}
3512
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003513 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003514WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003515{
3516 /* We need to handle deletion of windows underneath us.
3517 * If we add a "w_python*_ref" field to the win_T structure,
3518 * then we can get at it in win_free() in vim. We then
3519 * need to create only ONE Python object per window - if
3520 * we try to create a second, just INCREF the existing one
3521 * and return it. The (single) Python object referring to
3522 * the window is stored in "w_python*_ref".
3523 * On a win_free() we set the Python object's win_T* field
3524 * to an invalid value. We trap all uses of a window
3525 * object, and reject them if the win_T* field is invalid.
3526 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003527 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003528 * w_python_ref and w_python3_ref fields respectively.
3529 */
3530
3531 WindowObject *self;
3532
3533 if (WIN_PYTHON_REF(win))
3534 {
3535 self = WIN_PYTHON_REF(win);
3536 Py_INCREF(self);
3537 }
3538 else
3539 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003540 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003541 if (self == NULL)
3542 return NULL;
3543 self->win = win;
3544 WIN_PYTHON_REF(win) = self;
3545 }
3546
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003547 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3548
Bram Moolenaar971db462013-05-12 18:44:48 +02003549 return (PyObject *)(self);
3550}
3551
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003552 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003553WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003554{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003555 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003556 if (self->win && self->win != INVALID_WINDOW_VALUE)
3557 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003558 Py_XDECREF(((PyObject *)(self->tabObject)));
3559 PyObject_GC_Del((void *)(self));
3560}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003561
Bram Moolenaar774267b2013-05-21 20:51:59 +02003562 static int
3563WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3564{
3565 Py_VISIT(((PyObject *)(self->tabObject)));
3566 return 0;
3567}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003568
Bram Moolenaar774267b2013-05-21 20:51:59 +02003569 static int
3570WindowClear(WindowObject *self)
3571{
3572 Py_CLEAR(self->tabObject);
3573 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003574}
3575
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003576 static win_T *
3577get_firstwin(TabPageObject *tabObject)
3578{
3579 if (tabObject)
3580 {
3581 if (CheckTabPage(tabObject))
3582 return NULL;
3583 /* For current tab window.c does not bother to set or update tp_firstwin
3584 */
3585 else if (tabObject->tab == curtab)
3586 return firstwin;
3587 else
3588 return tabObject->tab->tp_firstwin;
3589 }
3590 else
3591 return firstwin;
3592}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003593static char *WindowAttrs[] = {
3594 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3595 "tabpage", "valid",
3596 NULL
3597};
3598
3599 static PyObject *
3600WindowDir(PyObject *self)
3601{
3602 return ObjectDir(self, WindowAttrs);
3603}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003604
Bram Moolenaar971db462013-05-12 18:44:48 +02003605 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003606WindowAttrValid(WindowObject *self, char *name)
3607{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003608 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003609
3610 if (strcmp(name, "valid") != 0)
3611 return NULL;
3612
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003613 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3614 Py_INCREF(ret);
3615 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003616}
3617
3618 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003619WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003620{
3621 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003622 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003623 else if (strcmp(name, "cursor") == 0)
3624 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003625 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003626
3627 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3628 }
3629 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003630 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003631#ifdef FEAT_WINDOWS
3632 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003633 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003634#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003635#ifdef FEAT_VERTSPLIT
3636 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003637 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003638 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003639 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003640#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003641 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003642 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003643 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003644 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3645 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003646 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003647 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003648 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003649 return NULL;
3650 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003651 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003652 }
3653 else if (strcmp(name, "tabpage") == 0)
3654 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003655 Py_INCREF(self->tabObject);
3656 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003657 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003658 else if (strcmp(name, "__members__") == 0)
3659 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003660 else
3661 return NULL;
3662}
3663
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003664 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003665WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003666{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003667 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003668 return -1;
3669
3670 if (strcmp(name, "buffer") == 0)
3671 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003672 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003673 return -1;
3674 }
3675 else if (strcmp(name, "cursor") == 0)
3676 {
3677 long lnum;
3678 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003679
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003680 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003681 return -1;
3682
Bram Moolenaard6e39182013-05-21 18:30:34 +02003683 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003684 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003685 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003686 return -1;
3687 }
3688
3689 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003690 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691 return -1;
3692
Bram Moolenaard6e39182013-05-21 18:30:34 +02003693 self->win->w_cursor.lnum = lnum;
3694 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003695#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003696 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003697#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003698 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003699 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003700
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003701 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003702 return 0;
3703 }
3704 else if (strcmp(name, "height") == 0)
3705 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003706 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003707 win_T *savewin;
3708
Bram Moolenaardee2e312013-06-23 16:35:47 +02003709 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710 return -1;
3711
3712#ifdef FEAT_GUI
3713 need_mouse_correct = TRUE;
3714#endif
3715 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003716 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003717
3718 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003719 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003720 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003721 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003722 return -1;
3723
3724 return 0;
3725 }
3726#ifdef FEAT_VERTSPLIT
3727 else if (strcmp(name, "width") == 0)
3728 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003729 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003730 win_T *savewin;
3731
Bram Moolenaardee2e312013-06-23 16:35:47 +02003732 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003733 return -1;
3734
3735#ifdef FEAT_GUI
3736 need_mouse_correct = TRUE;
3737#endif
3738 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003739 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003740
3741 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003742 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003743 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003744 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003745 return -1;
3746
3747 return 0;
3748 }
3749#endif
3750 else
3751 {
3752 PyErr_SetString(PyExc_AttributeError, name);
3753 return -1;
3754 }
3755}
3756
3757 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003758WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003759{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003760 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003761 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003762 else
3763 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003764 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003765
Bram Moolenaar6d216452013-05-12 19:00:41 +02003766 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003767 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003768 (self));
3769 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003770 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003771 }
3772}
3773
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003774static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003775 /* name, function, calling, documentation */
3776 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3777 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003778};
3779
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003780/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003781 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003782 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003783
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003784static PyTypeObject WinListType;
3785static PySequenceMethods WinListAsSeq;
3786
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003787typedef struct
3788{
3789 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003790 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003791} WinListObject;
3792
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003793 static PyObject *
3794WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003795{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003796 WinListObject *self;
3797
3798 self = PyObject_NEW(WinListObject, &WinListType);
3799 self->tabObject = tabObject;
3800 Py_INCREF(tabObject);
3801
3802 return (PyObject *)(self);
3803}
3804
3805 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003806WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003807{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003808 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003809
3810 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003811 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003812 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003813 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003814
3815 DESTRUCTOR_FINISH(self);
3816}
3817
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003818 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003819WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003820{
3821 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003822 PyInt n = 0;
3823
Bram Moolenaard6e39182013-05-21 18:30:34 +02003824 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003825 return -1;
3826
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003827 while (w != NULL)
3828 {
3829 ++n;
3830 w = W_NEXT(w);
3831 }
3832
3833 return n;
3834}
3835
3836 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003837WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003838{
3839 win_T *w;
3840
Bram Moolenaard6e39182013-05-21 18:30:34 +02003841 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003842 return NULL;
3843
3844 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003845 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003846 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003847
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003848 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003849 return NULL;
3850}
3851
3852/* Convert a Python string into a Vim line.
3853 *
3854 * The result is in allocated memory. All internal nulls are replaced by
3855 * newline characters. It is an error for the string to contain newline
3856 * characters.
3857 *
3858 * On errors, the Python exception data is set, and NULL is returned.
3859 */
3860 static char *
3861StringToLine(PyObject *obj)
3862{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003863 char *str;
3864 char *save;
3865 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003866 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003867 PyInt i;
3868 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003869
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003870 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003871 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003872 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3873 || str == NULL)
3874 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003875 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003876 else if (PyUnicode_Check(obj))
3877 {
3878 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3879 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003880
Bram Moolenaardaa27022013-06-24 22:33:30 +02003881 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003882 || str == NULL)
3883 {
3884 Py_DECREF(bytes);
3885 return NULL;
3886 }
3887 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003888 else
3889 {
3890#if PY_MAJOR_VERSION < 3
3891 PyErr_FORMAT(PyExc_TypeError,
3892 N_("expected str() or unicode() instance, but got %s"),
3893 Py_TYPE_NAME(obj));
3894#else
3895 PyErr_FORMAT(PyExc_TypeError,
3896 N_("expected bytes() or str() instance, but got %s"),
3897 Py_TYPE_NAME(obj));
3898#endif
3899 return NULL;
3900 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003901
3902 /*
3903 * Error checking: String must not contain newlines, as we
3904 * are replacing a single line, and we must replace it with
3905 * a single line.
3906 * A trailing newline is removed, so that append(f.readlines()) works.
3907 */
3908 p = memchr(str, '\n', len);
3909 if (p != NULL)
3910 {
3911 if (p == str + len - 1)
3912 --len;
3913 else
3914 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003915 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003916 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003917 return NULL;
3918 }
3919 }
3920
3921 /* Create a copy of the string, with internal nulls replaced by
3922 * newline characters, as is the vim convention.
3923 */
3924 save = (char *)alloc((unsigned)(len+1));
3925 if (save == NULL)
3926 {
3927 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003928 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003929 return NULL;
3930 }
3931
3932 for (i = 0; i < len; ++i)
3933 {
3934 if (str[i] == '\0')
3935 save[i] = '\n';
3936 else
3937 save[i] = str[i];
3938 }
3939
3940 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003941 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003942
3943 return save;
3944}
3945
3946/* Get a line from the specified buffer. The line number is
3947 * in Vim format (1-based). The line is returned as a Python
3948 * string object.
3949 */
3950 static PyObject *
3951GetBufferLine(buf_T *buf, PyInt n)
3952{
3953 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3954}
3955
3956
3957/* Get a list of lines from the specified buffer. The line numbers
3958 * are in Vim format (1-based). The range is from lo up to, but not
3959 * including, hi. The list is returned as a Python list of string objects.
3960 */
3961 static PyObject *
3962GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3963{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003964 PyInt i;
3965 PyInt n = hi - lo;
3966 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003967
3968 if (list == NULL)
3969 return NULL;
3970
3971 for (i = 0; i < n; ++i)
3972 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003973 PyObject *string = LineToString(
3974 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003975
3976 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003977 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003978 {
3979 Py_DECREF(list);
3980 return NULL;
3981 }
3982
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003983 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003984 }
3985
3986 /* The ownership of the Python list is passed to the caller (ie,
3987 * the caller should Py_DECREF() the object when it is finished
3988 * with it).
3989 */
3990
3991 return list;
3992}
3993
3994/*
3995 * Check if deleting lines made the cursor position invalid.
3996 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3997 * deleted).
3998 */
3999 static void
4000py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4001{
4002 if (curwin->w_cursor.lnum >= lo)
4003 {
4004 /* Adjust the cursor position if it's in/after the changed
4005 * lines. */
4006 if (curwin->w_cursor.lnum >= hi)
4007 {
4008 curwin->w_cursor.lnum += extra;
4009 check_cursor_col();
4010 }
4011 else if (extra < 0)
4012 {
4013 curwin->w_cursor.lnum = lo;
4014 check_cursor();
4015 }
4016 else
4017 check_cursor_col();
4018 changed_cline_bef_curs();
4019 }
4020 invalidate_botline();
4021}
4022
Bram Moolenaar19e60942011-06-19 00:27:51 +02004023/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004024 * Find a window that contains "buf" and switch to it.
4025 * If there is no such window, use the current window and change "curbuf".
4026 * Caller must initialize save_curbuf to NULL.
4027 * restore_win_for_buf() MUST be called later!
4028 */
4029 static void
4030switch_to_win_for_buf(
4031 buf_T *buf,
4032 win_T **save_curwinp,
4033 tabpage_T **save_curtabp,
4034 buf_T **save_curbufp)
4035{
4036 win_T *wp;
4037 tabpage_T *tp;
4038
Bram Moolenaarae38d052014-12-17 14:46:09 +01004039 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004040 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004041 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4042 {
4043 restore_win(*save_curwinp, *save_curtabp, TRUE);
4044 switch_buffer(save_curbufp, buf);
4045 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004046}
4047
4048 static void
4049restore_win_for_buf(
4050 win_T *save_curwin,
4051 tabpage_T *save_curtab,
4052 buf_T *save_curbuf)
4053{
4054 if (save_curbuf == NULL)
4055 restore_win(save_curwin, save_curtab, TRUE);
4056 else
4057 restore_buffer(save_curbuf);
4058}
4059
4060/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004061 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004062 * in Vim format (1-based). The replacement line is given as
4063 * a Python string object. The object is checked for validity
4064 * and correct format. Errors are returned as a value of FAIL.
4065 * The return value is OK on success.
4066 * If OK is returned and len_change is not NULL, *len_change
4067 * is set to the change in the buffer length.
4068 */
4069 static int
4070SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4071{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004072 buf_T *save_curbuf = NULL;
4073 win_T *save_curwin = NULL;
4074 tabpage_T *save_curtab = NULL;
4075
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004076 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077 * There are three cases:
4078 * 1. NULL, or None - this is a deletion.
4079 * 2. A string - this is a replacement.
4080 * 3. Anything else - this is an error.
4081 */
4082 if (line == Py_None || line == NULL)
4083 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004084 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004085 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004087 VimTryStart();
4088
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004090 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004092 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 else
4094 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004095 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004097 if (save_curbuf == NULL)
4098 /* Only adjust marks if we managed to switch to a window that
4099 * holds the buffer, otherwise line numbers will be invalid. */
4100 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 }
4102
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004103 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004104
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004105 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106 return FAIL;
4107
4108 if (len_change)
4109 *len_change = -1;
4110
4111 return OK;
4112 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004113 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004115 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116
4117 if (save == NULL)
4118 return FAIL;
4119
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004120 VimTryStart();
4121
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122 /* We do not need to free "save" if ml_replace() consumes it. */
4123 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004124 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125
4126 if (u_savesub((linenr_T)n) == FAIL)
4127 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004128 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129 vim_free(save);
4130 }
4131 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4132 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004133 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004134 vim_free(save);
4135 }
4136 else
4137 changed_bytes((linenr_T)n, 0);
4138
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004139 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004140
4141 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004142 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004143 check_cursor_col();
4144
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004145 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146 return FAIL;
4147
4148 if (len_change)
4149 *len_change = 0;
4150
4151 return OK;
4152 }
4153 else
4154 {
4155 PyErr_BadArgument();
4156 return FAIL;
4157 }
4158}
4159
Bram Moolenaar19e60942011-06-19 00:27:51 +02004160/* Replace a range of lines in the specified buffer. The line numbers are in
4161 * Vim format (1-based). The range is from lo up to, but not including, hi.
4162 * The replacement lines are given as a Python list of string objects. The
4163 * list is checked for validity and correct format. Errors are returned as a
4164 * value of FAIL. The return value is OK on success.
4165 * If OK is returned and len_change is not NULL, *len_change
4166 * is set to the change in the buffer length.
4167 */
4168 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004169SetBufferLineList(
4170 buf_T *buf,
4171 PyInt lo,
4172 PyInt hi,
4173 PyObject *list,
4174 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004175{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004176 buf_T *save_curbuf = NULL;
4177 win_T *save_curwin = NULL;
4178 tabpage_T *save_curtab = NULL;
4179
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004180 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004181 * There are three cases:
4182 * 1. NULL, or None - this is a deletion.
4183 * 2. A list - this is a replacement.
4184 * 3. Anything else - this is an error.
4185 */
4186 if (list == Py_None || list == NULL)
4187 {
4188 PyInt i;
4189 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004190
4191 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004192 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004193 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004194
4195 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004196 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004197 else
4198 {
4199 for (i = 0; i < n; ++i)
4200 {
4201 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4202 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004203 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004204 break;
4205 }
4206 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004207 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4208 /* Using an existing window for the buffer, adjust the cursor
4209 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004210 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004211 if (save_curbuf == NULL)
4212 /* Only adjust marks if we managed to switch to a window that
4213 * holds the buffer, otherwise line numbers will be invalid. */
4214 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004215 }
4216
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004217 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004218
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004219 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004220 return FAIL;
4221
4222 if (len_change)
4223 *len_change = -n;
4224
4225 return OK;
4226 }
4227 else if (PyList_Check(list))
4228 {
4229 PyInt i;
4230 PyInt new_len = PyList_Size(list);
4231 PyInt old_len = hi - lo;
4232 PyInt extra = 0; /* lines added to text, can be negative */
4233 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004234
4235 if (new_len == 0) /* avoid allocating zero bytes */
4236 array = NULL;
4237 else
4238 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004239 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004240 if (array == NULL)
4241 {
4242 PyErr_NoMemory();
4243 return FAIL;
4244 }
4245 }
4246
4247 for (i = 0; i < new_len; ++i)
4248 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004249 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004250
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004251 if (!(line = PyList_GetItem(list, i)) ||
4252 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004253 {
4254 while (i)
4255 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004256 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004257 return FAIL;
4258 }
4259 }
4260
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004261 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004262 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004263
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004264 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004265 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004266
4267 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004268 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004269
4270 /* If the size of the range is reducing (ie, new_len < old_len) we
4271 * need to delete some old_len. We do this at the start, by
4272 * repeatedly deleting line "lo".
4273 */
4274 if (!PyErr_Occurred())
4275 {
4276 for (i = 0; i < old_len - new_len; ++i)
4277 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4278 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004279 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004280 break;
4281 }
4282 extra -= i;
4283 }
4284
4285 /* For as long as possible, replace the existing old_len with the
4286 * new old_len. This is a more efficient operation, as it requires
4287 * less memory allocation and freeing.
4288 */
4289 if (!PyErr_Occurred())
4290 {
4291 for (i = 0; i < old_len && i < new_len; ++i)
4292 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4293 == FAIL)
4294 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004295 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004296 break;
4297 }
4298 }
4299 else
4300 i = 0;
4301
4302 /* Now we may need to insert the remaining new old_len. If we do, we
4303 * must free the strings as we finish with them (we can't pass the
4304 * responsibility to vim in this case).
4305 */
4306 if (!PyErr_Occurred())
4307 {
4308 while (i < new_len)
4309 {
4310 if (ml_append((linenr_T)(lo + i - 1),
4311 (char_u *)array[i], 0, FALSE) == FAIL)
4312 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004313 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004314 break;
4315 }
4316 vim_free(array[i]);
4317 ++i;
4318 ++extra;
4319 }
4320 }
4321
4322 /* Free any left-over old_len, as a result of an error */
4323 while (i < new_len)
4324 {
4325 vim_free(array[i]);
4326 ++i;
4327 }
4328
4329 /* Free the array of old_len. All of its contents have now
4330 * been dealt with (either freed, or the responsibility passed
4331 * to vim.
4332 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004333 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004334
4335 /* Adjust marks. Invalidate any which lie in the
4336 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004337 * Only adjust marks if we managed to switch to a window that holds
4338 * the buffer, otherwise line numbers will be invalid. */
4339 if (save_curbuf == NULL)
4340 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004341 (long)MAXLNUM, (long)extra);
4342 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4343
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004344 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004345 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4346
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004347 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004348 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004349
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004350 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004351 return FAIL;
4352
4353 if (len_change)
4354 *len_change = new_len - old_len;
4355
4356 return OK;
4357 }
4358 else
4359 {
4360 PyErr_BadArgument();
4361 return FAIL;
4362 }
4363}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004364
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004365/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004366 * The line number is in Vim format (1-based). The lines to be inserted are
4367 * given as a Python list of string objects or as a single string. The lines
4368 * to be added are checked for validity and correct format. Errors are
4369 * returned as a value of FAIL. The return value is OK on success.
4370 * If OK is returned and len_change is not NULL, *len_change
4371 * is set to the change in the buffer length.
4372 */
4373 static int
4374InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4375{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004376 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004377 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004378 tabpage_T *save_curtab = NULL;
4379
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004380 /* First of all, we check the type of the supplied Python object.
4381 * It must be a string or a list, or the call is in error.
4382 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004383 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004384 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004385 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004386
4387 if (str == NULL)
4388 return FAIL;
4389
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004391 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004392 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004394 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004395 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004397 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004398 else if (save_curbuf == NULL)
4399 /* Only adjust marks if we managed to switch to a window that
4400 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004401 appended_lines_mark((linenr_T)n, 1L);
4402
4403 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004404 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004405 update_screen(VALID);
4406
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004407 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004408 return FAIL;
4409
4410 if (len_change)
4411 *len_change = 1;
4412
4413 return OK;
4414 }
4415 else if (PyList_Check(lines))
4416 {
4417 PyInt i;
4418 PyInt size = PyList_Size(lines);
4419 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004421 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422 if (array == NULL)
4423 {
4424 PyErr_NoMemory();
4425 return FAIL;
4426 }
4427
4428 for (i = 0; i < size; ++i)
4429 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004430 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004432 if (!(line = PyList_GetItem(lines, i)) ||
4433 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434 {
4435 while (i)
4436 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004437 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 return FAIL;
4439 }
4440 }
4441
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004442 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004443 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004444 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445
4446 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004447 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448 else
4449 {
4450 for (i = 0; i < size; ++i)
4451 {
4452 if (ml_append((linenr_T)(n + i),
4453 (char_u *)array[i], 0, FALSE) == FAIL)
4454 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004455 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456
4457 /* Free the rest of the lines */
4458 while (i < size)
4459 vim_free(array[i++]);
4460
4461 break;
4462 }
4463 vim_free(array[i]);
4464 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004465 if (i > 0 && save_curbuf == NULL)
4466 /* Only adjust marks if we managed to switch to a window that
4467 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004468 appended_lines_mark((linenr_T)n, (long)i);
4469 }
4470
4471 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004472 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004473 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004474 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004475
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004476 update_screen(VALID);
4477
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004478 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004479 return FAIL;
4480
4481 if (len_change)
4482 *len_change = size;
4483
4484 return OK;
4485 }
4486 else
4487 {
4488 PyErr_BadArgument();
4489 return FAIL;
4490 }
4491}
4492
4493/*
4494 * Common routines for buffers and line ranges
4495 * -------------------------------------------
4496 */
4497
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004498typedef struct
4499{
4500 PyObject_HEAD
4501 buf_T *buf;
4502} BufferObject;
4503
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004504 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004505CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004506{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004507 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004508 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004509 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004510 return -1;
4511 }
4512
4513 return 0;
4514}
4515
4516 static PyObject *
4517RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4518{
4519 if (CheckBuffer(self))
4520 return NULL;
4521
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004522 if (end == -1)
4523 end = self->buf->b_ml.ml_line_count;
4524
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004525 if (n < 0)
4526 n += end - start + 1;
4527
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004528 if (n < 0 || n > end - start)
4529 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004530 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004531 return NULL;
4532 }
4533
4534 return GetBufferLine(self->buf, n+start);
4535}
4536
4537 static PyObject *
4538RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4539{
4540 PyInt size;
4541
4542 if (CheckBuffer(self))
4543 return NULL;
4544
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004545 if (end == -1)
4546 end = self->buf->b_ml.ml_line_count;
4547
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004548 size = end - start + 1;
4549
4550 if (lo < 0)
4551 lo = 0;
4552 else if (lo > size)
4553 lo = size;
4554 if (hi < 0)
4555 hi = 0;
4556 if (hi < lo)
4557 hi = lo;
4558 else if (hi > size)
4559 hi = size;
4560
4561 return GetBufferLineList(self->buf, lo+start, hi+start);
4562}
4563
4564 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004565RBAsItem(
4566 BufferObject *self,
4567 PyInt n,
4568 PyObject *valObject,
4569 PyInt start,
4570 PyInt end,
4571 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572{
4573 PyInt len_change;
4574
4575 if (CheckBuffer(self))
4576 return -1;
4577
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004578 if (end == -1)
4579 end = self->buf->b_ml.ml_line_count;
4580
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004581 if (n < 0)
4582 n += end - start + 1;
4583
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004584 if (n < 0 || n > end - start)
4585 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004586 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004587 return -1;
4588 }
4589
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004590 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004591 return -1;
4592
4593 if (new_end)
4594 *new_end = end + len_change;
4595
4596 return 0;
4597}
4598
Bram Moolenaar19e60942011-06-19 00:27:51 +02004599 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004600RBAsSlice(
4601 BufferObject *self,
4602 PyInt lo,
4603 PyInt hi,
4604 PyObject *valObject,
4605 PyInt start,
4606 PyInt end,
4607 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004608{
4609 PyInt size;
4610 PyInt len_change;
4611
4612 /* Self must be a valid buffer */
4613 if (CheckBuffer(self))
4614 return -1;
4615
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004616 if (end == -1)
4617 end = self->buf->b_ml.ml_line_count;
4618
Bram Moolenaar19e60942011-06-19 00:27:51 +02004619 /* Sort out the slice range */
4620 size = end - start + 1;
4621
4622 if (lo < 0)
4623 lo = 0;
4624 else if (lo > size)
4625 lo = size;
4626 if (hi < 0)
4627 hi = 0;
4628 if (hi < lo)
4629 hi = lo;
4630 else if (hi > size)
4631 hi = size;
4632
4633 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004634 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004635 return -1;
4636
4637 if (new_end)
4638 *new_end = end + len_change;
4639
4640 return 0;
4641}
4642
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004643
4644 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004645RBAppend(
4646 BufferObject *self,
4647 PyObject *args,
4648 PyInt start,
4649 PyInt end,
4650 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004651{
4652 PyObject *lines;
4653 PyInt len_change;
4654 PyInt max;
4655 PyInt n;
4656
4657 if (CheckBuffer(self))
4658 return NULL;
4659
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004660 if (end == -1)
4661 end = self->buf->b_ml.ml_line_count;
4662
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004663 max = n = end - start + 1;
4664
4665 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4666 return NULL;
4667
4668 if (n < 0 || n > max)
4669 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004670 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004671 return NULL;
4672 }
4673
4674 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4675 return NULL;
4676
4677 if (new_end)
4678 *new_end = end + len_change;
4679
4680 Py_INCREF(Py_None);
4681 return Py_None;
4682}
4683
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004684/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004685 */
4686
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004687static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004688static PySequenceMethods RangeAsSeq;
4689static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004690
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691typedef struct
4692{
4693 PyObject_HEAD
4694 BufferObject *buf;
4695 PyInt start;
4696 PyInt end;
4697} RangeObject;
4698
4699 static PyObject *
4700RangeNew(buf_T *buf, PyInt start, PyInt end)
4701{
4702 BufferObject *bufr;
4703 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004704 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004705 if (self == NULL)
4706 return NULL;
4707
4708 bufr = (BufferObject *)BufferNew(buf);
4709 if (bufr == NULL)
4710 {
4711 Py_DECREF(self);
4712 return NULL;
4713 }
4714 Py_INCREF(bufr);
4715
4716 self->buf = bufr;
4717 self->start = start;
4718 self->end = end;
4719
4720 return (PyObject *)(self);
4721}
4722
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004723 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004724RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004725{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004726 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004727 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004728 PyObject_GC_Del((void *)(self));
4729}
4730
4731 static int
4732RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4733{
4734 Py_VISIT(((PyObject *)(self->buf)));
4735 return 0;
4736}
4737
4738 static int
4739RangeClear(RangeObject *self)
4740{
4741 Py_CLEAR(self->buf);
4742 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004743}
4744
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004745 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004746RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004747{
4748 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004749 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004750 return -1; /* ??? */
4751
Bram Moolenaard6e39182013-05-21 18:30:34 +02004752 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004753}
4754
4755 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004756RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004757{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004758 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004759}
4760
4761 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004762RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004763{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004764 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004765}
4766
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004767static char *RangeAttrs[] = {
4768 "start", "end",
4769 NULL
4770};
4771
4772 static PyObject *
4773RangeDir(PyObject *self)
4774{
4775 return ObjectDir(self, RangeAttrs);
4776}
4777
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004778 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004779RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004780{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004781 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004782}
4783
4784 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004785RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004786{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004787 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004788 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4789 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004790 else
4791 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004792 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004793
4794 if (name == NULL)
4795 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004796
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004797 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004798 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004799 }
4800}
4801
4802static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004803 /* name, function, calling, documentation */
4804 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004805 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4806 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004807};
4808
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004809static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004810static PySequenceMethods BufferAsSeq;
4811static PyMappingMethods BufferAsMapping;
4812
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004813 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004814BufferNew(buf_T *buf)
4815{
4816 /* We need to handle deletion of buffers underneath us.
4817 * If we add a "b_python*_ref" field to the buf_T structure,
4818 * then we can get at it in buf_freeall() in vim. We then
4819 * need to create only ONE Python object per buffer - if
4820 * we try to create a second, just INCREF the existing one
4821 * and return it. The (single) Python object referring to
4822 * the buffer is stored in "b_python*_ref".
4823 * Question: what to do on a buf_freeall(). We'll probably
4824 * have to either delete the Python object (DECREF it to
4825 * zero - a bad idea, as it leaves dangling refs!) or
4826 * set the buf_T * value to an invalid value (-1?), which
4827 * means we need checks in all access functions... Bah.
4828 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004829 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004830 * b_python_ref and b_python3_ref fields respectively.
4831 */
4832
4833 BufferObject *self;
4834
4835 if (BUF_PYTHON_REF(buf) != NULL)
4836 {
4837 self = BUF_PYTHON_REF(buf);
4838 Py_INCREF(self);
4839 }
4840 else
4841 {
4842 self = PyObject_NEW(BufferObject, &BufferType);
4843 if (self == NULL)
4844 return NULL;
4845 self->buf = buf;
4846 BUF_PYTHON_REF(buf) = self;
4847 }
4848
4849 return (PyObject *)(self);
4850}
4851
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004852 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004853BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004854{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004855 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4856 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004857
4858 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004859}
4860
Bram Moolenaar971db462013-05-12 18:44:48 +02004861 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004862BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004863{
4864 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004865 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004866 return -1; /* ??? */
4867
Bram Moolenaard6e39182013-05-21 18:30:34 +02004868 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004869}
4870
4871 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004872BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004873{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004874 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004875}
4876
4877 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004878BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004879{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004880 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004881}
4882
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004883static char *BufferAttrs[] = {
4884 "name", "number", "vars", "options", "valid",
4885 NULL
4886};
4887
4888 static PyObject *
4889BufferDir(PyObject *self)
4890{
4891 return ObjectDir(self, BufferAttrs);
4892}
4893
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004894 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004895BufferAttrValid(BufferObject *self, char *name)
4896{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004897 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004898
4899 if (strcmp(name, "valid") != 0)
4900 return NULL;
4901
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004902 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4903 Py_INCREF(ret);
4904 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004905}
4906
4907 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004908BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004909{
4910 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004911 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004912 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004913 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004914 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004915 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004916 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004917 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004918 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4919 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004920 else if (strcmp(name, "__members__") == 0)
4921 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004922 else
4923 return NULL;
4924}
4925
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004926 static int
4927BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4928{
4929 if (CheckBuffer(self))
4930 return -1;
4931
4932 if (strcmp(name, "name") == 0)
4933 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004934 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004935 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004936 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004937 PyObject *todecref;
4938
4939 if (!(val = StringToChars(valObject, &todecref)))
4940 return -1;
4941
4942 VimTryStart();
4943 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4944 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004945 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004946 aucmd_restbuf(&aco);
4947 Py_XDECREF(todecref);
4948 if (VimTryEnd())
4949 return -1;
4950
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004951 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004952 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004953 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004954 return -1;
4955 }
4956 return 0;
4957 }
4958 else
4959 {
4960 PyErr_SetString(PyExc_AttributeError, name);
4961 return -1;
4962 }
4963}
4964
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004965 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004966BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004967{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004968 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004969}
4970
4971 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004972BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004973{
4974 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004975 char_u *pmark;
4976 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004977 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004978 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004979
Bram Moolenaard6e39182013-05-21 18:30:34 +02004980 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004981 return NULL;
4982
Bram Moolenaar389a1792013-06-23 13:00:44 +02004983 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004984 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004985
Bram Moolenaar389a1792013-06-23 13:00:44 +02004986 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004987 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004988 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004989 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004990 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004991 return NULL;
4992 }
4993
4994 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004995
4996 Py_XDECREF(todecref);
4997
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004998 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004999 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005000 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02005001 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005002 if (VimTryEnd())
5003 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005004
5005 if (posp == NULL)
5006 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005007 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005008 return NULL;
5009 }
5010
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005011 if (posp->lnum <= 0)
5012 {
5013 /* Or raise an error? */
5014 Py_INCREF(Py_None);
5015 return Py_None;
5016 }
5017
5018 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5019}
5020
5021 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005022BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005023{
5024 PyInt start;
5025 PyInt end;
5026
Bram Moolenaard6e39182013-05-21 18:30:34 +02005027 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005028 return NULL;
5029
5030 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5031 return NULL;
5032
Bram Moolenaard6e39182013-05-21 18:30:34 +02005033 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005034}
5035
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005036 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005037BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005038{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005039 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005040 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005041 else
5042 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005043 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005044
5045 if (name == NULL)
5046 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005047
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005048 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005049 }
5050}
5051
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005052static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005053 /* name, function, calling, documentation */
5054 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005055 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005056 {"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 +02005057 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5058 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005059};
5060
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005061/*
5062 * Buffer list object - Implementation
5063 */
5064
5065static PyTypeObject BufMapType;
5066
5067typedef struct
5068{
5069 PyObject_HEAD
5070} BufMapObject;
5071
5072 static PyInt
5073BufMapLength(PyObject *self UNUSED)
5074{
5075 buf_T *b = firstbuf;
5076 PyInt n = 0;
5077
5078 while (b)
5079 {
5080 ++n;
5081 b = b->b_next;
5082 }
5083
5084 return n;
5085}
5086
5087 static PyObject *
5088BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5089{
5090 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005091 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005092
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005093 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005094 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005095
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005096 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005097
5098 if (b)
5099 return BufferNew(b);
5100 else
5101 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005102 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005103 return NULL;
5104 }
5105}
5106
5107 static void
5108BufMapIterDestruct(PyObject *buffer)
5109{
5110 /* Iteration was stopped before all buffers were processed */
5111 if (buffer)
5112 {
5113 Py_DECREF(buffer);
5114 }
5115}
5116
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005117 static int
5118BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5119{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005120 if (buffer)
5121 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005122 return 0;
5123}
5124
5125 static int
5126BufMapIterClear(PyObject **buffer)
5127{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005128 if (*buffer)
5129 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005130 return 0;
5131}
5132
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005133 static PyObject *
5134BufMapIterNext(PyObject **buffer)
5135{
5136 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005137 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005138
5139 if (!*buffer)
5140 return NULL;
5141
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005142 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005143
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005144 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005145 {
5146 *buffer = NULL;
5147 return NULL;
5148 }
5149
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005150 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005151 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005152 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005153 return NULL;
5154 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005155 /* Do not increment reference: we no longer hold it (decref), but whoever
5156 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005157 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005158}
5159
5160 static PyObject *
5161BufMapIter(PyObject *self UNUSED)
5162{
5163 PyObject *buffer;
5164
5165 buffer = BufferNew(firstbuf);
5166 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005167 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5168 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005169}
5170
5171static PyMappingMethods BufMapAsMapping = {
5172 (lenfunc) BufMapLength,
5173 (binaryfunc) BufMapItem,
5174 (objobjargproc) 0,
5175};
5176
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005177/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005178 */
5179
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005180static char *CurrentAttrs[] = {
5181 "buffer", "window", "line", "range", "tabpage",
5182 NULL
5183};
5184
5185 static PyObject *
5186CurrentDir(PyObject *self)
5187{
5188 return ObjectDir(self, CurrentAttrs);
5189}
5190
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005191 static PyObject *
5192CurrentGetattr(PyObject *self UNUSED, char *name)
5193{
5194 if (strcmp(name, "buffer") == 0)
5195 return (PyObject *)BufferNew(curbuf);
5196 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005197 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005198 else if (strcmp(name, "tabpage") == 0)
5199 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005200 else if (strcmp(name, "line") == 0)
5201 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5202 else if (strcmp(name, "range") == 0)
5203 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005204 else if (strcmp(name, "__members__") == 0)
5205 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005206 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005207#if PY_MAJOR_VERSION < 3
5208 return Py_FindMethod(WindowMethods, self, name);
5209#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005210 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005211#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005212}
5213
5214 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005215CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005216{
5217 if (strcmp(name, "line") == 0)
5218 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005219 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5220 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005221 return -1;
5222
5223 return 0;
5224 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005225 else if (strcmp(name, "buffer") == 0)
5226 {
5227 int count;
5228
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005229 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005230 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005231 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005232 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005233 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005234 return -1;
5235 }
5236
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005237 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005238 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005239 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005240
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005241 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005242 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5243 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005244 if (VimTryEnd())
5245 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005246 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005247 return -1;
5248 }
5249
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005250 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005251 }
5252 else if (strcmp(name, "window") == 0)
5253 {
5254 int count;
5255
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005256 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005257 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005258 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005259 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005260 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005261 return -1;
5262 }
5263
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005264 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005265 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005266 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005267
5268 if (!count)
5269 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005270 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005271 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005272 return -1;
5273 }
5274
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005275 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005276 win_goto(((WindowObject *)(valObject))->win);
5277 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005278 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005279 if (VimTryEnd())
5280 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005281 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005282 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005283 return -1;
5284 }
5285
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005286 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005287 }
5288 else if (strcmp(name, "tabpage") == 0)
5289 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005290 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005291 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005292 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005293 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005294 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005295 return -1;
5296 }
5297
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005298 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005299 return -1;
5300
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005301 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005302 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5303 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005304 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005305 if (VimTryEnd())
5306 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005307 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005308 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005309 return -1;
5310 }
5311
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005312 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005313 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005314 else
5315 {
5316 PyErr_SetString(PyExc_AttributeError, name);
5317 return -1;
5318 }
5319}
5320
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005321static struct PyMethodDef CurrentMethods[] = {
5322 /* name, function, calling, documentation */
5323 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5324 { NULL, NULL, 0, NULL}
5325};
5326
Bram Moolenaardb913952012-06-29 12:54:53 +02005327 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005328init_range_cmd(exarg_T *eap)
5329{
5330 RangeStart = eap->line1;
5331 RangeEnd = eap->line2;
5332}
5333
5334 static void
5335init_range_eval(typval_T *rettv UNUSED)
5336{
5337 RangeStart = (PyInt) curwin->w_cursor.lnum;
5338 RangeEnd = RangeStart;
5339}
5340
5341 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005342run_cmd(const char *cmd, void *arg UNUSED
5343#ifdef PY_CAN_RECURSE
5344 , PyGILState_STATE *pygilstate UNUSED
5345#endif
5346 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005347{
Bram Moolenaar41009372013-07-01 22:03:04 +02005348 PyObject *run_ret;
5349 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5350 if (run_ret != NULL)
5351 {
5352 Py_DECREF(run_ret);
5353 }
5354 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5355 {
5356 EMSG2(_(e_py_systemexit), "python");
5357 PyErr_Clear();
5358 }
5359 else
5360 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005361}
5362
5363static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5364static int code_hdr_len = 30;
5365
5366 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005367run_do(const char *cmd, void *arg UNUSED
5368#ifdef PY_CAN_RECURSE
5369 , PyGILState_STATE *pygilstate
5370#endif
5371 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005372{
5373 PyInt lnum;
5374 size_t len;
5375 char *code;
5376 int status;
5377 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005378 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005379
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005380 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005381 {
5382 EMSG(_("cannot save undo information"));
5383 return;
5384 }
5385
5386 len = code_hdr_len + STRLEN(cmd);
5387 code = PyMem_New(char, len + 1);
5388 memcpy(code, code_hdr, code_hdr_len);
5389 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005390 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5391 status = -1;
5392 if (run_ret != NULL)
5393 {
5394 status = 0;
5395 Py_DECREF(run_ret);
5396 }
5397 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5398 {
5399 PyMem_Free(code);
5400 EMSG2(_(e_py_systemexit), "python");
5401 PyErr_Clear();
5402 return;
5403 }
5404 else
5405 PyErr_PrintEx(1);
5406
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005407 PyMem_Free(code);
5408
5409 if (status)
5410 {
5411 EMSG(_("failed to run the code"));
5412 return;
5413 }
5414
5415 status = 0;
5416 pymain = PyImport_AddModule("__main__");
5417 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005418#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005419 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005420#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005421
5422 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5423 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005424 PyObject *line;
5425 PyObject *linenr;
5426 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005427
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005428#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005429 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005430#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005431 if (!(line = GetBufferLine(curbuf, lnum)))
5432 goto err;
5433 if (!(linenr = PyInt_FromLong((long) lnum)))
5434 {
5435 Py_DECREF(line);
5436 goto err;
5437 }
5438 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5439 Py_DECREF(line);
5440 Py_DECREF(linenr);
5441 if (!ret)
5442 goto err;
5443
5444 if (ret != Py_None)
5445 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5446 goto err;
5447
5448 Py_XDECREF(ret);
5449 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005450#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005451 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005452#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005453 }
5454 goto out;
5455err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005456#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005457 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005458#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005459 PyErr_PrintEx(0);
5460 PythonIO_Flush();
5461 status = 1;
5462out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005463#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005464 if (!status)
5465 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005466#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005467 Py_DECREF(pyfunc);
5468 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5469 if (status)
5470 return;
5471 check_cursor();
5472 update_curbuf(NOT_VALID);
5473}
5474
5475 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005476run_eval(const char *cmd, typval_T *rettv
5477#ifdef PY_CAN_RECURSE
5478 , PyGILState_STATE *pygilstate UNUSED
5479#endif
5480 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005481{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005482 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005483
Bram Moolenaar41009372013-07-01 22:03:04 +02005484 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005485 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005486 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005487 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005488 {
5489 EMSG2(_(e_py_systemexit), "python");
5490 PyErr_Clear();
5491 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005492 else
5493 {
5494 if (PyErr_Occurred() && !msg_silent)
5495 PyErr_PrintEx(0);
5496 EMSG(_("E858: Eval did not return a valid python object"));
5497 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005498 }
5499 else
5500 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005501 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005502 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005503 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005504 }
5505 PyErr_Clear();
5506}
5507
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005508 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005509set_ref_in_py(const int copyID)
5510{
5511 pylinkedlist_T *cur;
5512 dict_T *dd;
5513 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005514 int abort = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02005515
5516 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005517 {
5518 for(cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005519 {
5520 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5521 if (dd->dv_copyID != copyID)
5522 {
5523 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005524 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005525 }
5526 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005527 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005528
5529 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005530 {
5531 for(cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005532 {
5533 ll = ((ListObject *) (cur->pll_obj))->list;
5534 if (ll->lv_copyID != copyID)
5535 {
5536 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005537 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005538 }
5539 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005540 }
5541
5542 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005543}
5544
5545 static int
5546set_string_copy(char_u *str, typval_T *tv)
5547{
5548 tv->vval.v_string = vim_strsave(str);
5549 if (tv->vval.v_string == NULL)
5550 {
5551 PyErr_NoMemory();
5552 return -1;
5553 }
5554 return 0;
5555}
5556
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005557 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005558pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005559{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005560 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005561 char_u *key;
5562 dictitem_T *di;
5563 PyObject *keyObject;
5564 PyObject *valObject;
5565 Py_ssize_t iter = 0;
5566
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005567 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005568 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005569
5570 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005571 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005572
5573 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5574 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005575 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005576
Bram Moolenaara03e6312013-05-29 22:49:26 +02005577 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005578 {
5579 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005580 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005581 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005582
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005583 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005584 {
5585 dict_unref(dict);
5586 return -1;
5587 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005588
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005589 if (*key == NUL)
5590 {
5591 dict_unref(dict);
5592 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005593 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005594 return -1;
5595 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005596
5597 di = dictitem_alloc(key);
5598
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005599 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005600
5601 if (di == NULL)
5602 {
5603 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005604 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005605 return -1;
5606 }
5607 di->di_tv.v_lock = 0;
5608
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005609 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005610 {
5611 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005612 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005613 return -1;
5614 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005615
5616 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005617 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005618 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005619 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005620 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005621 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005622 return -1;
5623 }
5624 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005625
5626 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005627 return 0;
5628}
5629
5630 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005631pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005632{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005633 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005634 char_u *key;
5635 dictitem_T *di;
5636 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005637 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005638 PyObject *keyObject;
5639 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005640
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005641 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005642 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005643
5644 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005645 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005646
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005647 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005648 {
5649 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005650 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005651 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005652
5653 if (!(iterator = PyObject_GetIter(list)))
5654 {
5655 dict_unref(dict);
5656 Py_DECREF(list);
5657 return -1;
5658 }
5659 Py_DECREF(list);
5660
5661 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005662 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005663 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005664
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005665 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005666 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005667 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005668 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005669 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005670 return -1;
5671 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005672
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005673 if (*key == NUL)
5674 {
5675 Py_DECREF(keyObject);
5676 Py_DECREF(iterator);
5677 Py_XDECREF(todecref);
5678 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005679 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005680 return -1;
5681 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005682
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005683 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005684 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005685 Py_DECREF(keyObject);
5686 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005687 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005688 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005689 return -1;
5690 }
5691
5692 di = dictitem_alloc(key);
5693
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005694 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005695 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005696
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005697 if (di == NULL)
5698 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005699 Py_DECREF(iterator);
5700 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005701 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005702 PyErr_NoMemory();
5703 return -1;
5704 }
5705 di->di_tv.v_lock = 0;
5706
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005707 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005708 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005709 Py_DECREF(iterator);
5710 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005711 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005712 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005713 return -1;
5714 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005715
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005716 Py_DECREF(valObject);
5717
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005718 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005719 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005720 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005721 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005722 dictitem_free(di);
5723 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005724 return -1;
5725 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005726 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005727 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005728 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005729 return 0;
5730}
5731
5732 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005733pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005734{
5735 list_T *l;
5736
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005737 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005738 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005739
5740 tv->v_type = VAR_LIST;
5741 tv->vval.v_list = l;
5742
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005743 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005744 {
5745 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005746 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005747 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005748
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005749 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005750 return 0;
5751}
5752
Bram Moolenaardb913952012-06-29 12:54:53 +02005753typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5754
5755 static int
5756convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005757 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005758{
5759 PyObject *capsule;
5760 char hexBuf[sizeof(void *) * 2 + 3];
5761
5762 sprintf(hexBuf, "%p", obj);
5763
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005764# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005765 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005766# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005767 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005768# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005769 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005770 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005771# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005772 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005773# else
5774 capsule = PyCObject_FromVoidPtr(tv, NULL);
5775# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005776 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5777 {
5778 Py_DECREF(capsule);
5779 tv->v_type = VAR_UNKNOWN;
5780 return -1;
5781 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005782
5783 Py_DECREF(capsule);
5784
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005785 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005786 {
5787 tv->v_type = VAR_UNKNOWN;
5788 return -1;
5789 }
5790 /* As we are not using copy_tv which increments reference count we must
5791 * do it ourself. */
5792 switch(tv->v_type)
5793 {
5794 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5795 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5796 }
5797 }
5798 else
5799 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005800 typval_T *v;
5801
5802# ifdef PY_USE_CAPSULE
5803 v = PyCapsule_GetPointer(capsule, NULL);
5804# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005805 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005806# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005807 copy_tv(v, tv);
5808 }
5809 return 0;
5810}
5811
5812 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005813ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5814{
5815 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005816 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005817
5818 if (!(lookup_dict = PyDict_New()))
5819 return -1;
5820
5821 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5822 {
5823 tv->v_type = VAR_DICT;
5824 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5825 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005826 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005827 }
5828 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005829 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005830 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005831 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005832 else
5833 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005834 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005835 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005836 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005837 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005838 }
5839 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005840 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005841}
5842
5843 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005844ConvertFromPyObject(PyObject *obj, typval_T *tv)
5845{
5846 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005847 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005848
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005849 if (!(lookup_dict = PyDict_New()))
5850 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005851 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005852 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005853 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005854}
5855
5856 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005857_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005858{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005859 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005860 {
5861 tv->v_type = VAR_DICT;
5862 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5863 ++tv->vval.v_dict->dv_refcount;
5864 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005865 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005866 {
5867 tv->v_type = VAR_LIST;
5868 tv->vval.v_list = (((ListObject *)(obj))->list);
5869 ++tv->vval.v_list->lv_refcount;
5870 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005871 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005872 {
5873 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5874 return -1;
5875
5876 tv->v_type = VAR_FUNC;
5877 func_ref(tv->vval.v_string);
5878 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005879 else if (PyBytes_Check(obj))
5880 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005881 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005882
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005883 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005884 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005885 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005886 return -1;
5887
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005888 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005889 return -1;
5890
5891 tv->v_type = VAR_STRING;
5892 }
5893 else if (PyUnicode_Check(obj))
5894 {
5895 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005896 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005897
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005898 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005899 if (bytes == NULL)
5900 return -1;
5901
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005902 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005903 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005904 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005905 return -1;
5906
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005907 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005908 {
5909 Py_XDECREF(bytes);
5910 return -1;
5911 }
5912 Py_XDECREF(bytes);
5913
5914 tv->v_type = VAR_STRING;
5915 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005916#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005917 else if (PyInt_Check(obj))
5918 {
5919 tv->v_type = VAR_NUMBER;
5920 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005921 if (PyErr_Occurred())
5922 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005923 }
5924#endif
5925 else if (PyLong_Check(obj))
5926 {
5927 tv->v_type = VAR_NUMBER;
5928 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005929 if (PyErr_Occurred())
5930 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005931 }
5932 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005933 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005934#ifdef FEAT_FLOAT
5935 else if (PyFloat_Check(obj))
5936 {
5937 tv->v_type = VAR_FLOAT;
5938 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5939 }
5940#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005941 else if (PyObject_HasAttrString(obj, "keys"))
5942 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005943 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005944 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005945 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005946 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005947 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005948 else if (PyNumber_Check(obj))
5949 {
5950 PyObject *num;
5951
5952 if (!(num = PyNumber_Long(obj)))
5953 return -1;
5954
5955 tv->v_type = VAR_NUMBER;
5956 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5957
5958 Py_DECREF(num);
5959 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005960 else
5961 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005962 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005963 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005964 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005965 return -1;
5966 }
5967 return 0;
5968}
5969
5970 static PyObject *
5971ConvertToPyObject(typval_T *tv)
5972{
5973 if (tv == NULL)
5974 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005975 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 return NULL;
5977 }
5978 switch (tv->v_type)
5979 {
5980 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005981 return PyBytes_FromString(tv->vval.v_string == NULL
5982 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005983 case VAR_NUMBER:
5984 return PyLong_FromLong((long) tv->vval.v_number);
5985#ifdef FEAT_FLOAT
5986 case VAR_FLOAT:
5987 return PyFloat_FromDouble((double) tv->vval.v_float);
5988#endif
5989 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005990 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005991 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005992 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005993 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005994 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005995 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005996 case VAR_UNKNOWN:
5997 Py_INCREF(Py_None);
5998 return Py_None;
5999 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006000 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006001 return NULL;
6002 }
6003}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006004
6005typedef struct
6006{
6007 PyObject_HEAD
6008} CurrentObject;
6009static PyTypeObject CurrentType;
6010
6011 static void
6012init_structs(void)
6013{
6014 vim_memset(&OutputType, 0, sizeof(OutputType));
6015 OutputType.tp_name = "vim.message";
6016 OutputType.tp_basicsize = sizeof(OutputObject);
6017 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6018 OutputType.tp_doc = "vim message object";
6019 OutputType.tp_methods = OutputMethods;
6020#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006021 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6022 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006023 OutputType.tp_alloc = call_PyType_GenericAlloc;
6024 OutputType.tp_new = call_PyType_GenericNew;
6025 OutputType.tp_free = call_PyObject_Free;
6026#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006027 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6028 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006029#endif
6030
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006031 vim_memset(&IterType, 0, sizeof(IterType));
6032 IterType.tp_name = "vim.iter";
6033 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006034 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006035 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006036 IterType.tp_iter = (getiterfunc)IterIter;
6037 IterType.tp_iternext = (iternextfunc)IterNext;
6038 IterType.tp_dealloc = (destructor)IterDestructor;
6039 IterType.tp_traverse = (traverseproc)IterTraverse;
6040 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006041
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006042 vim_memset(&BufferType, 0, sizeof(BufferType));
6043 BufferType.tp_name = "vim.buffer";
6044 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006045 BufferType.tp_dealloc = (destructor)BufferDestructor;
6046 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006047 BufferType.tp_as_sequence = &BufferAsSeq;
6048 BufferType.tp_as_mapping = &BufferAsMapping;
6049 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6050 BufferType.tp_doc = "vim buffer object";
6051 BufferType.tp_methods = BufferMethods;
6052#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006053 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006054 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006055 BufferType.tp_alloc = call_PyType_GenericAlloc;
6056 BufferType.tp_new = call_PyType_GenericNew;
6057 BufferType.tp_free = call_PyObject_Free;
6058#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006059 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006060 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006061#endif
6062
6063 vim_memset(&WindowType, 0, sizeof(WindowType));
6064 WindowType.tp_name = "vim.window";
6065 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006066 WindowType.tp_dealloc = (destructor)WindowDestructor;
6067 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006068 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006069 WindowType.tp_doc = "vim Window object";
6070 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006071 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6072 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006073#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006074 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6075 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006076 WindowType.tp_alloc = call_PyType_GenericAlloc;
6077 WindowType.tp_new = call_PyType_GenericNew;
6078 WindowType.tp_free = call_PyObject_Free;
6079#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006080 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6081 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006082#endif
6083
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006084 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6085 TabPageType.tp_name = "vim.tabpage";
6086 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006087 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6088 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006089 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6090 TabPageType.tp_doc = "vim tab page object";
6091 TabPageType.tp_methods = TabPageMethods;
6092#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006093 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006094 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6095 TabPageType.tp_new = call_PyType_GenericNew;
6096 TabPageType.tp_free = call_PyObject_Free;
6097#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006098 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006099#endif
6100
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006101 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6102 BufMapType.tp_name = "vim.bufferlist";
6103 BufMapType.tp_basicsize = sizeof(BufMapObject);
6104 BufMapType.tp_as_mapping = &BufMapAsMapping;
6105 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006106 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006107 BufferType.tp_doc = "vim buffer list";
6108
6109 vim_memset(&WinListType, 0, sizeof(WinListType));
6110 WinListType.tp_name = "vim.windowlist";
6111 WinListType.tp_basicsize = sizeof(WinListType);
6112 WinListType.tp_as_sequence = &WinListAsSeq;
6113 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6114 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006115 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006116
6117 vim_memset(&TabListType, 0, sizeof(TabListType));
6118 TabListType.tp_name = "vim.tabpagelist";
6119 TabListType.tp_basicsize = sizeof(TabListType);
6120 TabListType.tp_as_sequence = &TabListAsSeq;
6121 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6122 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006123
6124 vim_memset(&RangeType, 0, sizeof(RangeType));
6125 RangeType.tp_name = "vim.range";
6126 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006127 RangeType.tp_dealloc = (destructor)RangeDestructor;
6128 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006129 RangeType.tp_as_sequence = &RangeAsSeq;
6130 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006131 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006132 RangeType.tp_doc = "vim Range object";
6133 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006134 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6135 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006136#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006137 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006138 RangeType.tp_alloc = call_PyType_GenericAlloc;
6139 RangeType.tp_new = call_PyType_GenericNew;
6140 RangeType.tp_free = call_PyObject_Free;
6141#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006142 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006143#endif
6144
6145 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6146 CurrentType.tp_name = "vim.currentdata";
6147 CurrentType.tp_basicsize = sizeof(CurrentObject);
6148 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6149 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006150 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006151#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006152 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6153 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006154#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006155 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6156 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006157#endif
6158
6159 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6160 DictionaryType.tp_name = "vim.dictionary";
6161 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006162 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006163 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006164 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006165 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006166 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6167 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006168 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6169 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6170 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006171#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006172 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6173 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006174#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006175 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6176 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006177#endif
6178
6179 vim_memset(&ListType, 0, sizeof(ListType));
6180 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006181 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006182 ListType.tp_basicsize = sizeof(ListObject);
6183 ListType.tp_as_sequence = &ListAsSeq;
6184 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006185 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006186 ListType.tp_doc = "list pushing modifications to vim structure";
6187 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006188 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006189 ListType.tp_new = (newfunc)ListConstructor;
6190 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006191#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006192 ListType.tp_getattro = (getattrofunc)ListGetattro;
6193 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006194#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006195 ListType.tp_getattr = (getattrfunc)ListGetattr;
6196 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006197#endif
6198
6199 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006200 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006201 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006202 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6203 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006204 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006205 FunctionType.tp_doc = "object that calls vim function";
6206 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006207 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006208 FunctionType.tp_new = (newfunc)FunctionConstructor;
6209 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006210#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006211 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006212#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006213 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006214#endif
6215
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006216 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6217 OptionsType.tp_name = "vim.options";
6218 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006219 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006220 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006221 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006222 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006223 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006224 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6225 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6226 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006227
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006228 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6229 LoaderType.tp_name = "vim.Loader";
6230 LoaderType.tp_basicsize = sizeof(LoaderObject);
6231 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6232 LoaderType.tp_doc = "vim message object";
6233 LoaderType.tp_methods = LoaderMethods;
6234 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6235
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006236#if PY_MAJOR_VERSION >= 3
6237 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6238 vimmodule.m_name = "vim";
6239 vimmodule.m_doc = "Vim Python interface\n";
6240 vimmodule.m_size = -1;
6241 vimmodule.m_methods = VimMethods;
6242#endif
6243}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006244
6245#define PYTYPE_READY(type) \
6246 if (PyType_Ready(&type)) \
6247 return -1;
6248
6249 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006250init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006251{
6252 PYTYPE_READY(IterType);
6253 PYTYPE_READY(BufferType);
6254 PYTYPE_READY(RangeType);
6255 PYTYPE_READY(WindowType);
6256 PYTYPE_READY(TabPageType);
6257 PYTYPE_READY(BufMapType);
6258 PYTYPE_READY(WinListType);
6259 PYTYPE_READY(TabListType);
6260 PYTYPE_READY(CurrentType);
6261 PYTYPE_READY(DictionaryType);
6262 PYTYPE_READY(ListType);
6263 PYTYPE_READY(FunctionType);
6264 PYTYPE_READY(OptionsType);
6265 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006266 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006267 return 0;
6268}
6269
6270 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006271init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006272{
6273 PyObject *path;
6274 PyObject *path_hook;
6275 PyObject *path_hooks;
6276
6277 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6278 return -1;
6279
6280 if (!(path_hooks = PySys_GetObject("path_hooks")))
6281 {
6282 PyErr_Clear();
6283 path_hooks = PyList_New(1);
6284 PyList_SET_ITEM(path_hooks, 0, path_hook);
6285 if (PySys_SetObject("path_hooks", path_hooks))
6286 {
6287 Py_DECREF(path_hooks);
6288 return -1;
6289 }
6290 Py_DECREF(path_hooks);
6291 }
6292 else if (PyList_Check(path_hooks))
6293 {
6294 if (PyList_Append(path_hooks, path_hook))
6295 {
6296 Py_DECREF(path_hook);
6297 return -1;
6298 }
6299 Py_DECREF(path_hook);
6300 }
6301 else
6302 {
6303 VimTryStart();
6304 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6305 "You should now do the following:\n"
6306 "- append vim.path_hook to sys.path_hooks\n"
6307 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6308 VimTryEnd(); /* Discard the error */
6309 Py_DECREF(path_hook);
6310 return 0;
6311 }
6312
6313 if (!(path = PySys_GetObject("path")))
6314 {
6315 PyErr_Clear();
6316 path = PyList_New(1);
6317 Py_INCREF(vim_special_path_object);
6318 PyList_SET_ITEM(path, 0, vim_special_path_object);
6319 if (PySys_SetObject("path", path))
6320 {
6321 Py_DECREF(path);
6322 return -1;
6323 }
6324 Py_DECREF(path);
6325 }
6326 else if (PyList_Check(path))
6327 {
6328 if (PyList_Append(path, vim_special_path_object))
6329 return -1;
6330 }
6331 else
6332 {
6333 VimTryStart();
6334 EMSG(_("Failed to set path: sys.path is not a list\n"
6335 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6336 VimTryEnd(); /* Discard the error */
6337 }
6338
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006339 return 0;
6340}
6341
6342static BufMapObject TheBufferMap =
6343{
6344 PyObject_HEAD_INIT(&BufMapType)
6345};
6346
6347static WinListObject TheWindowList =
6348{
6349 PyObject_HEAD_INIT(&WinListType)
6350 NULL
6351};
6352
6353static CurrentObject TheCurrent =
6354{
6355 PyObject_HEAD_INIT(&CurrentType)
6356};
6357
6358static TabListObject TheTabPageList =
6359{
6360 PyObject_HEAD_INIT(&TabListType)
6361};
6362
6363static struct numeric_constant {
6364 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006365 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006366} numeric_constants[] = {
6367 {"VAR_LOCKED", VAR_LOCKED},
6368 {"VAR_FIXED", VAR_FIXED},
6369 {"VAR_SCOPE", VAR_SCOPE},
6370 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6371};
6372
6373static struct object_constant {
6374 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006375 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006376} object_constants[] = {
6377 {"buffers", (PyObject *)(void *)&TheBufferMap},
6378 {"windows", (PyObject *)(void *)&TheWindowList},
6379 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6380 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006381
6382 {"Buffer", (PyObject *)&BufferType},
6383 {"Range", (PyObject *)&RangeType},
6384 {"Window", (PyObject *)&WindowType},
6385 {"TabPage", (PyObject *)&TabPageType},
6386 {"Dictionary", (PyObject *)&DictionaryType},
6387 {"List", (PyObject *)&ListType},
6388 {"Function", (PyObject *)&FunctionType},
6389 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006390 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006391};
6392
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006393#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006394 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006395 return -1;
6396
6397#define ADD_CHECKED_OBJECT(m, name, obj) \
6398 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006399 PyObject *valObject = obj; \
6400 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006401 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006402 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006403 }
6404
6405 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006406populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006407{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006408 int i;
6409 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006410 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006411 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006412
6413 for (i = 0; i < (int)(sizeof(numeric_constants)
6414 / sizeof(struct numeric_constant));
6415 ++i)
6416 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006417 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006418
6419 for (i = 0; i < (int)(sizeof(object_constants)
6420 / sizeof(struct object_constant));
6421 ++i)
6422 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006423 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006424
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006425 valObject = object_constants[i].valObject;
6426 Py_INCREF(valObject);
6427 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006428 }
6429
6430 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6431 return -1;
6432 ADD_OBJECT(m, "error", VimError);
6433
Bram Moolenaara9922d62013-05-30 13:01:18 +02006434 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6435 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006436 ADD_CHECKED_OBJECT(m, "options",
6437 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006438
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006439 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006440 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006441 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006442
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006443 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006444 return -1;
6445 ADD_OBJECT(m, "_getcwd", py_getcwd)
6446
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006447 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006448 return -1;
6449 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006450 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006451 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006452 if (PyObject_SetAttrString(other_module, "chdir", attr))
6453 {
6454 Py_DECREF(attr);
6455 return -1;
6456 }
6457 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006458
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006459 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006460 {
6461 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006462 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006463 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006464 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6465 {
6466 Py_DECREF(attr);
6467 return -1;
6468 }
6469 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006470 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006471 else
6472 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006473
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006474 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6475 return -1;
6476
6477 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6478
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006479 if (!(imp = PyImport_ImportModule("imp")))
6480 return -1;
6481
6482 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6483 {
6484 Py_DECREF(imp);
6485 return -1;
6486 }
6487
6488 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6489 {
6490 Py_DECREF(py_find_module);
6491 Py_DECREF(imp);
6492 return -1;
6493 }
6494
6495 Py_DECREF(imp);
6496
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006497 ADD_OBJECT(m, "_find_module", py_find_module);
6498 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006499
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006500 return 0;
6501}