blob: 01cbe418c8e1e64b09073d73323fab47dedbf6db [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020075static PyObject *WindowNew(win_T *, tabpage_T *);
76static PyObject *BufferNew (buf_T *);
77static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020078
79static PyInt RangeStart;
80static PyInt RangeEnd;
81
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020082static PyObject *globals;
83
Bram Moolenaarf4258302013-06-02 18:20:17 +020084static PyObject *py_chdir;
85static PyObject *py_fchdir;
86static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020087static PyObject *vim_module;
88static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020089
Bram Moolenaar81c40c52013-06-12 14:41:04 +020090static PyObject *py_find_module;
91static PyObject *py_load_module;
92
93static PyObject *VimError;
94
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020095/*
96 * obtain a lock on the Vim data structures
97 */
98 static void
99Python_Lock_Vim(void)
100{
101}
102
103/*
104 * release a lock on the Vim data structures
105 */
106 static void
107Python_Release_Vim(void)
108{
109}
110
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200111/*
112 * The "todecref" argument holds a pointer to PyObject * that must be
113 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
114 * was needed to generate returned value is object.
115 *
116 * Use Py_XDECREF to decrement reference count.
117 */
118 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200119StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200121 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200124 {
125
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
127 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 return NULL;
129
130 *todecref = NULL;
131 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200132 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200134 PyObject *bytes;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137 return NULL;
138
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200139 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
140 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 {
142 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200143 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200144 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200145
146 *todecref = bytes;
147 }
148 else
149 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200151 PyErr_FORMAT(PyExc_TypeError,
152 N_("expected str() or unicode() instance, but got %s"),
153 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200154#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200155 PyErr_FORMAT(PyExc_TypeError,
156 N_("expected bytes() or str() instance, but got %s"),
157 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200158#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200159 return NULL;
160 }
161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200162 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200163}
164
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200165#define NUMBER_LONG 1
166#define NUMBER_INT 2
167#define NUMBER_NATURAL 4
168#define NUMBER_UNSIGNED 8
169
170 static int
171NumberToLong(PyObject *obj, long *result, int flags)
172{
173#if PY_MAJOR_VERSION < 3
174 if (PyInt_Check(obj))
175 {
176 *result = PyInt_AsLong(obj);
177 if (PyErr_Occurred())
178 return -1;
179 }
180 else
181#endif
182 if (PyLong_Check(obj))
183 {
184 *result = PyLong_AsLong(obj);
185 if (PyErr_Occurred())
186 return -1;
187 }
188 else if (PyNumber_Check(obj))
189 {
190 PyObject *num;
191
192 if (!(num = PyNumber_Long(obj)))
193 return -1;
194
195 *result = PyLong_AsLong(num);
196
197 Py_DECREF(num);
198
199 if (PyErr_Occurred())
200 return -1;
201 }
202 else
203 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200204#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200205 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200206 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 "coercing to long(), but got %s"),
208 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200212 "but got %s"),
213 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215 return -1;
216 }
217
218 if (flags & NUMBER_INT)
219 {
220 if (*result > INT_MAX)
221 {
222 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200223 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200224 return -1;
225 }
226 else if (*result < INT_MIN)
227 {
228 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200229 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200230 return -1;
231 }
232 }
233
234 if (flags & NUMBER_NATURAL)
235 {
236 if (*result <= 0)
237 {
238 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200239 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200240 return -1;
241 }
242 }
243 else if (flags & NUMBER_UNSIGNED)
244 {
245 if (*result < 0)
246 {
247 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200248 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200249 return -1;
250 }
251 }
252
253 return 0;
254}
255
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200256 static int
257add_string(PyObject *list, char *s)
258{
259 PyObject *string;
260
261 if (!(string = PyString_FromString(s)))
262 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200263
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200264 if (PyList_Append(list, string))
265 {
266 Py_DECREF(string);
267 return -1;
268 }
269
270 Py_DECREF(string);
271 return 0;
272}
273
274 static PyObject *
275ObjectDir(PyObject *self, char **attributes)
276{
277 PyMethodDef *method;
278 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200279 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200281 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200282 return NULL;
283
284 if (self)
285 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200286 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200288 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200289 return NULL;
290 }
291
292 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200295 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200296 return NULL;
297 }
298
299#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200302 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200303 return NULL;
304 }
305#endif
306
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308}
309
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200310/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 */
312
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200313/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315
316static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317
318typedef struct
319{
320 PyObject_HEAD
321 long softspace;
322 long error;
323} OutputObject;
324
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200325static char *OutputAttrs[] = {
326 "softspace",
327 NULL
328};
329
330 static PyObject *
331OutputDir(PyObject *self)
332{
333 return ObjectDir(self, OutputAttrs);
334}
335
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200337OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200339 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200341 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200342 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200343 return -1;
344 }
345
346 if (strcmp(name, "softspace") == 0)
347 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200348 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return 0;
351 }
352
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200353 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
355}
356
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200357/* Buffer IO, we write one whole line at a time. */
358static garray_T io_ga = {0, 0, 1, 80, NULL};
359static writefn old_fn = NULL;
360
361 static void
362PythonIO_Flush(void)
363{
364 if (old_fn != NULL && io_ga.ga_len > 0)
365 {
366 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
367 old_fn((char_u *)io_ga.ga_data);
368 }
369 io_ga.ga_len = 0;
370}
371
372 static void
373writer(writefn fn, char_u *str, PyInt n)
374{
375 char_u *ptr;
376
377 /* Flush when switching output function. */
378 if (fn != old_fn)
379 PythonIO_Flush();
380 old_fn = fn;
381
382 /* Write each NL separated line. Text after the last NL is kept for
383 * writing later. */
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
399 /* Put the remaining text into io_ga for later printing. */
400 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
401 {
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 io_ga.ga_len += (int)n;
404 }
405}
406
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200407 static int
408write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 Py_ssize_t len = 0;
411 char *str = NULL;
412 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200413
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200416
417 Py_BEGIN_ALLOW_THREADS
418 Python_Lock_Vim();
419 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
420 Python_Release_Vim();
421 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200422 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200424 return 0;
425}
426
427 static PyObject *
428OutputWrite(OutputObject *self, PyObject *string)
429{
430 if (write_output(self, string))
431 return NULL;
432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433 Py_INCREF(Py_None);
434 return Py_None;
435}
436
437 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200438OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200440 PyObject *iterator;
441 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200448 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200450 Py_DECREF(iterator);
451 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 return NULL;
453 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 }
456
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(iterator);
458
459 /* Iterator may have finished due to an exception */
460 if (PyErr_Occurred())
461 return NULL;
462
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463 Py_INCREF(Py_None);
464 return Py_None;
465}
466
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100467 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200468OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100469{
470 /* do nothing */
471 Py_INCREF(Py_None);
472 return Py_None;
473}
474
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200475/***************/
476
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200477static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200478 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200479 {"write", (PyCFunction)OutputWrite, METH_O, ""},
480 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200481 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200482 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200483 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200484};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200485
486static OutputObject Output =
487{
488 PyObject_HEAD_INIT(&OutputType)
489 0,
490 0
491};
492
493static OutputObject Error =
494{
495 PyObject_HEAD_INIT(&OutputType)
496 0,
497 1
498};
499
500 static int
501PythonIO_Init_io(void)
502{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200503 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
504 return -1;
505 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
506 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200507
508 if (PyErr_Occurred())
509 {
510 EMSG(_("E264: Python: Error initialising I/O objects"));
511 return -1;
512 }
513
514 return 0;
515}
516
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200517typedef struct
518{
519 PyObject_HEAD
520 PyObject *module;
521} LoaderObject;
522static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200523
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200524 static void
525LoaderDestructor(LoaderObject *self)
526{
527 Py_DECREF(self->module);
528 DESTRUCTOR_FINISH(self);
529}
530
531 static PyObject *
532LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
533{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200534 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200536 Py_INCREF(ret);
537 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200538}
539
540static struct PyMethodDef LoaderMethods[] = {
541 /* name, function, calling, doc */
542 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
543 { NULL, NULL, 0, NULL}
544};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200545
546/* Check to see whether a Vim error has been reported, or a keyboard
547 * interrupt has been detected.
548 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200549
550 static void
551VimTryStart(void)
552{
553 ++trylevel;
554}
555
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200557VimTryEnd(void)
558{
559 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200560 /* Without this it stops processing all subsequent VimL commands and
561 * generates strange error messages if I e.g. try calling Test() in a
562 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563 did_emsg = FALSE;
564 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200565 if (got_int)
566 {
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100567 if (current_exception != NULL)
568 discard_current_exception();
569 else
570 need_rethrow = did_throw = FALSE;
571 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200573 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200574 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100575 else if (msg_list != NULL && *msg_list != NULL)
576 {
577 int should_free;
578 char_u *msg;
579
580 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
581
582 if (msg == NULL)
583 {
584 PyErr_NoMemory();
585 return -1;
586 }
587
588 PyErr_SetVim((char *) msg);
589
590 free_global_msglist();
591
592 if (should_free)
593 vim_free(msg);
594
595 return -1;
596 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200597 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200598 return (PyErr_Occurred() ? -1 : 0);
599 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200600 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200601 {
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100602 if (current_exception != NULL)
603 discard_current_exception();
604 else
605 need_rethrow = did_throw = FALSE;
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200606 return -1;
607 }
608 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 else
610 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200611 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200612 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200613 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200614 }
615}
616
617 static int
618VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200619{
620 if (got_int)
621 {
622 PyErr_SetNone(PyExc_KeyboardInterrupt);
623 return 1;
624 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200625 return 0;
626}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200627
628/* Vim module - Implementation
629 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200630
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200632VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200633{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200634 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200635 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200636 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200637
Bram Moolenaar389a1792013-06-23 13:00:44 +0200638 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639 return NULL;
640
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200641 Py_BEGIN_ALLOW_THREADS
642 Python_Lock_Vim();
643
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200644 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200645 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646 update_screen(VALID);
647
648 Python_Release_Vim();
649 Py_END_ALLOW_THREADS
650
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200651 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200652 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200653 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200654 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200655
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200656 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200657 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200658 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659}
660
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200661/*
662 * Function to translate a typval_T into a PyObject; this will recursively
663 * translate lists/dictionaries into their Python equivalents.
664 *
665 * The depth parameter is to avoid infinite recursion, set it to 1 when
666 * you call VimToPython.
667 */
668 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200669VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200670{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200671 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200672 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200673 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200674
675 /* Avoid infinite recursion */
676 if (depth > 100)
677 {
678 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200679 ret = Py_None;
680 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200681 }
682
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200683 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200684 * then and we can use it again. */
685 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
686 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
687 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200688 sprintf(ptrBuf, "%p",
689 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
690 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200691
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200692 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200694 Py_INCREF(ret);
695 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200696 }
697 }
698
699 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200700 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200701 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 else if (our_tv->v_type == VAR_NUMBER)
703 {
704 char buf[NUMBUFLEN];
705
706 /* For backwards compatibility numbers are stored as strings. */
707 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200708 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709 }
710# ifdef FEAT_FLOAT
711 else if (our_tv->v_type == VAR_FLOAT)
712 {
713 char buf[NUMBUFLEN];
714
715 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200716 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717 }
718# endif
719 else if (our_tv->v_type == VAR_LIST)
720 {
721 list_T *list = our_tv->vval.v_list;
722 listitem_T *curr;
723
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200724 if (list == NULL)
725 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
729
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200730 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200731 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200732 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200733 return NULL;
734 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200736 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
737 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200738 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200739 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200740 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200741 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200742 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200743 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200744 {
745 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747 return NULL;
748 }
749 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750 }
751 }
752 else if (our_tv->v_type == VAR_DICT)
753 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200755 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
756 long_u todo = ht->ht_used;
757 hashitem_T *hi;
758 dictitem_T *di;
759 if (our_tv->vval.v_dict == NULL)
760 return NULL;
761
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 return NULL;
764
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200765 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200766 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200767 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200768 return NULL;
769 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200771 for (hi = ht->ht_array; todo > 0; ++hi)
772 {
773 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200774 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200775 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200776
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200777 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200778 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200780 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200782 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200784 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200785 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200786 Py_DECREF(newObj);
787 return NULL;
788 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200789 }
790 }
791 }
792 else
793 {
794 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200796 }
797
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200798 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200799}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800
801 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200802VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200804 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200806 PyObject *string;
807 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200808 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200809 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810
Bram Moolenaar389a1792013-06-23 13:00:44 +0200811 if (!PyArg_ParseTuple(args, "O", &string))
812 return NULL;
813
814 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200815 return NULL;
816
817 Py_BEGIN_ALLOW_THREADS
818 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200819 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200820 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200821 Python_Release_Vim();
822 Py_END_ALLOW_THREADS
823
Bram Moolenaar389a1792013-06-23 13:00:44 +0200824 Py_XDECREF(todecref);
825
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200826 if (VimTryEnd())
827 return NULL;
828
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200829 if (our_tv == NULL)
830 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200831 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832 return NULL;
833 }
834
835 /* Convert the Vim type into a Python type. Create a dictionary that's
836 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200837 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200839 else
840 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200841 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200842 Py_DECREF(lookup_dict);
843 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200844
845
846 Py_BEGIN_ALLOW_THREADS
847 Python_Lock_Vim();
848 free_tv(our_tv);
849 Python_Release_Vim();
850 Py_END_ALLOW_THREADS
851
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200852 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200853}
854
Bram Moolenaardb913952012-06-29 12:54:53 +0200855static PyObject *ConvertToPyObject(typval_T *);
856
857 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200858VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200859{
Bram Moolenaardb913952012-06-29 12:54:53 +0200860 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200861 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200862 char_u *expr;
863 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200864
Bram Moolenaar389a1792013-06-23 13:00:44 +0200865 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200866 return NULL;
867
868 Py_BEGIN_ALLOW_THREADS
869 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200870 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200871 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200872 Python_Release_Vim();
873 Py_END_ALLOW_THREADS
874
Bram Moolenaar389a1792013-06-23 13:00:44 +0200875 Py_XDECREF(todecref);
876
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200877 if (VimTryEnd())
878 return NULL;
879
Bram Moolenaardb913952012-06-29 12:54:53 +0200880 if (our_tv == NULL)
881 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200882 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200883 return NULL;
884 }
885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200887 Py_BEGIN_ALLOW_THREADS
888 Python_Lock_Vim();
889 free_tv(our_tv);
890 Python_Release_Vim();
891 Py_END_ALLOW_THREADS
892
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200893 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200894}
895
896 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200897VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200898{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200899 char_u *str;
900 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200901 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200902
Bram Moolenaar389a1792013-06-23 13:00:44 +0200903 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200904 return NULL;
905
Bram Moolenaara54bf402012-12-05 16:30:07 +0100906#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200907 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100908#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100910#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200911
912 Py_XDECREF(todecref);
913
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200914 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200915}
916
Bram Moolenaarf4258302013-06-02 18:20:17 +0200917 static PyObject *
918_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
919{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200920 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200921 PyObject *newwd;
922 PyObject *todecref;
923 char_u *new_dir;
924
Bram Moolenaard4209d22013-06-05 20:34:15 +0200925 if (_chdir == NULL)
926 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928 return NULL;
929
930 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
931 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200932 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200933 return NULL;
934 }
935
936 if (!(new_dir = StringToChars(newwd, &todecref)))
937 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200938 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200939 Py_DECREF(newwd);
940 return NULL;
941 }
942
943 VimTryStart();
944
945 if (vim_chdir(new_dir))
946 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200947 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200948 Py_DECREF(newwd);
949 Py_XDECREF(todecref);
950
951 if (VimTryEnd())
952 return NULL;
953
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200954 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200955 return NULL;
956 }
957
958 Py_DECREF(newwd);
959 Py_XDECREF(todecref);
960
961 post_chdir(FALSE);
962
963 if (VimTryEnd())
964 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200965 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200966 return NULL;
967 }
968
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200969 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200970}
971
972 static PyObject *
973VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
974{
975 return _VimChdir(py_chdir, args, kwargs);
976}
977
978 static PyObject *
979VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
980{
981 return _VimChdir(py_fchdir, args, kwargs);
982}
983
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200984typedef struct {
985 PyObject *callable;
986 PyObject *result;
987} map_rtp_data;
988
989 static void
990map_rtp_callback(char_u *path, void *_data)
991{
992 void **data = (void **) _data;
993 PyObject *pathObject;
994 map_rtp_data *mr_data = *((map_rtp_data **) data);
995
Bram Moolenaar41009372013-07-01 22:03:04 +0200996 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200997 {
998 *data = NULL;
999 return;
1000 }
1001
1002 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1003 pathObject, NULL);
1004
1005 Py_DECREF(pathObject);
1006
1007 if (!mr_data->result || mr_data->result != Py_None)
1008 *data = NULL;
1009 else
1010 {
1011 Py_DECREF(mr_data->result);
1012 mr_data->result = NULL;
1013 }
1014}
1015
1016 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001017VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001018{
1019 map_rtp_data data;
1020
Bram Moolenaar389a1792013-06-23 13:00:44 +02001021 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001022 data.result = NULL;
1023
1024 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1025
1026 if (data.result == NULL)
1027 {
1028 if (PyErr_Occurred())
1029 return NULL;
1030 else
1031 {
1032 Py_INCREF(Py_None);
1033 return Py_None;
1034 }
1035 }
1036 return data.result;
1037}
1038
1039/*
1040 * _vim_runtimepath_ special path implementation.
1041 */
1042
1043 static void
1044map_finder_callback(char_u *path, void *_data)
1045{
1046 void **data = (void **) _data;
1047 PyObject *list = *((PyObject **) data);
1048 PyObject *pathObject1, *pathObject2;
1049 char *pathbuf;
1050 size_t pathlen;
1051
1052 pathlen = STRLEN(path);
1053
1054#if PY_MAJOR_VERSION < 3
1055# define PY_MAIN_DIR_STRING "python2"
1056#else
1057# define PY_MAIN_DIR_STRING "python3"
1058#endif
1059#define PY_ALTERNATE_DIR_STRING "pythonx"
1060
1061#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1062 if (!(pathbuf = PyMem_New(char,
1063 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1064 {
1065 PyErr_NoMemory();
1066 *data = NULL;
1067 return;
1068 }
1069
1070 mch_memmove(pathbuf, path, pathlen + 1);
1071 add_pathsep((char_u *) pathbuf);
1072
1073 pathlen = STRLEN(pathbuf);
1074 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1075 PYTHONX_STRING_LENGTH + 1);
1076
1077 if (!(pathObject1 = PyString_FromString(pathbuf)))
1078 {
1079 *data = NULL;
1080 PyMem_Free(pathbuf);
1081 return;
1082 }
1083
1084 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1085 PYTHONX_STRING_LENGTH + 1);
1086
1087 if (!(pathObject2 = PyString_FromString(pathbuf)))
1088 {
1089 Py_DECREF(pathObject1);
1090 PyMem_Free(pathbuf);
1091 *data = NULL;
1092 return;
1093 }
1094
1095 PyMem_Free(pathbuf);
1096
1097 if (PyList_Append(list, pathObject1)
1098 || PyList_Append(list, pathObject2))
1099 *data = NULL;
1100
1101 Py_DECREF(pathObject1);
1102 Py_DECREF(pathObject2);
1103}
1104
1105 static PyObject *
1106Vim_GetPaths(PyObject *self UNUSED)
1107{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001108 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001109
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001110 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001111 return NULL;
1112
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001113 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001114
1115 if (PyErr_Occurred())
1116 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001117 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001118 return NULL;
1119 }
1120
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001121 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001122}
1123
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001124 static PyObject *
1125call_load_module(char *name, int len, PyObject *find_module_result)
1126{
1127 PyObject *fd, *pathname, *description;
1128
Bram Moolenaarc476e522013-06-23 13:46:40 +02001129 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001130 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001131 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001132 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001133 Py_TYPE_NAME(find_module_result));
1134 return NULL;
1135 }
1136 if (PyTuple_GET_SIZE(find_module_result) != 3)
1137 {
1138 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001139 N_("expected 3-tuple as imp.find_module() result, but got "
1140 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001141 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001142 return NULL;
1143 }
1144
1145 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1146 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1147 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1148 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001149 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001150 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001151 return NULL;
1152 }
1153
1154 return PyObject_CallFunction(py_load_module,
1155 "s#OOO", name, len, fd, pathname, description);
1156}
1157
1158 static PyObject *
1159find_module(char *fullname, char *tail, PyObject *new_path)
1160{
1161 PyObject *find_module_result;
1162 PyObject *module;
1163 char *dot;
1164
Bram Moolenaar41009372013-07-01 22:03:04 +02001165 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001166 {
1167 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001168 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001169 * first component
1170 */
1171 PyObject *newest_path;
1172 int partlen = (int) (dot - 1 - tail);
1173
1174 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1175 "s#O", tail, partlen, new_path)))
1176 return NULL;
1177
1178 if (!(module = call_load_module(
1179 fullname,
1180 ((int) (tail - fullname)) + partlen,
1181 find_module_result)))
1182 {
1183 Py_DECREF(find_module_result);
1184 return NULL;
1185 }
1186
1187 Py_DECREF(find_module_result);
1188
1189 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1190 {
1191 Py_DECREF(module);
1192 return NULL;
1193 }
1194
1195 Py_DECREF(module);
1196
1197 module = find_module(fullname, dot + 1, newest_path);
1198
1199 Py_DECREF(newest_path);
1200
1201 return module;
1202 }
1203 else
1204 {
1205 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1206 "sO", tail, new_path)))
1207 return NULL;
1208
1209 if (!(module = call_load_module(
1210 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001211 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001212 find_module_result)))
1213 {
1214 Py_DECREF(find_module_result);
1215 return NULL;
1216 }
1217
1218 Py_DECREF(find_module_result);
1219
1220 return module;
1221 }
1222}
1223
1224 static PyObject *
1225FinderFindModule(PyObject *self, PyObject *args)
1226{
1227 char *fullname;
1228 PyObject *module;
1229 PyObject *new_path;
1230 LoaderObject *loader;
1231
1232 if (!PyArg_ParseTuple(args, "s", &fullname))
1233 return NULL;
1234
1235 if (!(new_path = Vim_GetPaths(self)))
1236 return NULL;
1237
1238 module = find_module(fullname, fullname, new_path);
1239
1240 Py_DECREF(new_path);
1241
1242 if (!module)
1243 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001244 if (PyErr_Occurred())
1245 {
1246 if (PyErr_ExceptionMatches(PyExc_ImportError))
1247 PyErr_Clear();
1248 else
1249 return NULL;
1250 }
1251
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001252 Py_INCREF(Py_None);
1253 return Py_None;
1254 }
1255
1256 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1257 {
1258 Py_DECREF(module);
1259 return NULL;
1260 }
1261
1262 loader->module = module;
1263
1264 return (PyObject *) loader;
1265}
1266
1267 static PyObject *
1268VimPathHook(PyObject *self UNUSED, PyObject *args)
1269{
1270 char *path;
1271
1272 if (PyArg_ParseTuple(args, "s", &path)
1273 && STRCMP(path, vim_special_path) == 0)
1274 {
1275 Py_INCREF(vim_module);
1276 return vim_module;
1277 }
1278
1279 PyErr_Clear();
1280 PyErr_SetNone(PyExc_ImportError);
1281 return NULL;
1282}
1283
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001284/*
1285 * Vim module - Definitions
1286 */
1287
1288static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001289 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001290 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001291 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001292 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1293 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001294 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1295 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001296 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001297 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001298 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1299 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1300 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001301};
1302
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001303/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001304 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001305 */
1306
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001307static PyTypeObject IterType;
1308
1309typedef PyObject *(*nextfun)(void **);
1310typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001311typedef int (*traversefun)(void *, visitproc, void *);
1312typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001313
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001314/* Main purpose of this object is removing the need for do python
1315 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1316 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001317
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001318typedef struct
1319{
1320 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001321 void *cur;
1322 nextfun next;
1323 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324 traversefun traverse;
1325 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001326} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001327
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001328 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001329IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1330 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001331{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001332 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001333
Bram Moolenaar774267b2013-05-21 20:51:59 +02001334 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001335 self->cur = start;
1336 self->next = next;
1337 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001338 self->traverse = traverse;
1339 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001340
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001341 return (PyObject *)(self);
1342}
1343
1344 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001345IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001346{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001347 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001349 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001350}
1351
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001353IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001354{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001355 if (self->traverse != NULL)
1356 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001357 else
1358 return 0;
1359}
1360
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001361/* Mac OSX defines clear() somewhere. */
1362#ifdef clear
1363# undef clear
1364#endif
1365
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001367IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001368{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001369 if (self->clear != NULL)
1370 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001371 else
1372 return 0;
1373}
1374
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001375 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001376IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001377{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001378 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001379}
1380
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001381 static PyObject *
1382IterIter(PyObject *self)
1383{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001384 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001385 return self;
1386}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001387
Bram Moolenaardb913952012-06-29 12:54:53 +02001388typedef struct pylinkedlist_S {
1389 struct pylinkedlist_S *pll_next;
1390 struct pylinkedlist_S *pll_prev;
1391 PyObject *pll_obj;
1392} pylinkedlist_T;
1393
1394static pylinkedlist_T *lastdict = NULL;
1395static pylinkedlist_T *lastlist = NULL;
1396
1397 static void
1398pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1399{
1400 if (ref->pll_prev == NULL)
1401 {
1402 if (ref->pll_next == NULL)
1403 {
1404 *last = NULL;
1405 return;
1406 }
1407 }
1408 else
1409 ref->pll_prev->pll_next = ref->pll_next;
1410
1411 if (ref->pll_next == NULL)
1412 *last = ref->pll_prev;
1413 else
1414 ref->pll_next->pll_prev = ref->pll_prev;
1415}
1416
1417 static void
1418pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1419{
1420 if (*last == NULL)
1421 ref->pll_prev = NULL;
1422 else
1423 {
1424 (*last)->pll_next = ref;
1425 ref->pll_prev = *last;
1426 }
1427 ref->pll_next = NULL;
1428 ref->pll_obj = self;
1429 *last = ref;
1430}
1431
1432static PyTypeObject DictionaryType;
1433
1434typedef struct
1435{
1436 PyObject_HEAD
1437 dict_T *dict;
1438 pylinkedlist_T ref;
1439} DictionaryObject;
1440
Bram Moolenaara9922d62013-05-30 13:01:18 +02001441static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1442
1443#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1444
Bram Moolenaardb913952012-06-29 12:54:53 +02001445 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001446DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001447{
1448 DictionaryObject *self;
1449
Bram Moolenaara9922d62013-05-30 13:01:18 +02001450 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001451 if (self == NULL)
1452 return NULL;
1453 self->dict = dict;
1454 ++dict->dv_refcount;
1455
1456 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1457
1458 return (PyObject *)(self);
1459}
1460
Bram Moolenaara9922d62013-05-30 13:01:18 +02001461 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001462py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001463{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001464 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001465
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001467 {
1468 PyErr_NoMemory();
1469 return NULL;
1470 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001471 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001472
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001473 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001474}
1475
1476 static PyObject *
1477DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1478{
1479 DictionaryObject *self;
1480 dict_T *dict;
1481
1482 if (!(dict = py_dict_alloc()))
1483 return NULL;
1484
1485 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1486
1487 --dict->dv_refcount;
1488
1489 if (kwargs || PyTuple_Size(args))
1490 {
1491 PyObject *tmp;
1492 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1493 {
1494 Py_DECREF(self);
1495 return NULL;
1496 }
1497
1498 Py_DECREF(tmp);
1499 }
1500
1501 return (PyObject *)(self);
1502}
1503
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001505DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001506{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001507 pyll_remove(&self->ref, &lastdict);
1508 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001509
1510 DESTRUCTOR_FINISH(self);
1511}
1512
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001513static char *DictionaryAttrs[] = {
1514 "locked", "scope",
1515 NULL
1516};
1517
1518 static PyObject *
1519DictionaryDir(PyObject *self)
1520{
1521 return ObjectDir(self, DictionaryAttrs);
1522}
1523
Bram Moolenaardb913952012-06-29 12:54:53 +02001524 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001525DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001526{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001527 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001528 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001529 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001530 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001531 return -1;
1532 }
1533
1534 if (strcmp(name, "locked") == 0)
1535 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001536 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001537 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001538 PyErr_SET_STRING(PyExc_TypeError,
1539 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001540 return -1;
1541 }
1542 else
1543 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001544 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001545 if (istrue == -1)
1546 return -1;
1547 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001548 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001549 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001550 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001551 }
1552 return 0;
1553 }
1554 else
1555 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001556 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001557 return -1;
1558 }
1559}
1560
1561 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001562DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001563{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001564 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001565}
1566
Bram Moolenaara9922d62013-05-30 13:01:18 +02001567#define DICT_FLAG_HAS_DEFAULT 0x01
1568#define DICT_FLAG_POP 0x02
1569#define DICT_FLAG_NONE_DEFAULT 0x04
1570#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1571#define DICT_FLAG_RETURN_PAIR 0x10
1572
Bram Moolenaardb913952012-06-29 12:54:53 +02001573 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001574_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001575{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 PyObject *keyObject;
1577 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001578 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001579 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001580 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581 dict_T *dict = self->dict;
1582 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001583 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001584
Bram Moolenaara9922d62013-05-30 13:01:18 +02001585 if (flags & DICT_FLAG_HAS_DEFAULT)
1586 {
1587 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1588 return NULL;
1589 }
1590 else
1591 keyObject = args;
1592
1593 if (flags & DICT_FLAG_RETURN_BOOL)
1594 defObject = Py_False;
1595
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001596 if (!(key = StringToChars(keyObject, &todecref)))
1597 return NULL;
1598
1599 if (*key == NUL)
1600 {
1601 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001602 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001603 return NULL;
1604 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001605
Bram Moolenaara9922d62013-05-30 13:01:18 +02001606 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001607
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001608 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001609
Bram Moolenaara9922d62013-05-30 13:01:18 +02001610 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001611 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001612 if (defObject)
1613 {
1614 Py_INCREF(defObject);
1615 return defObject;
1616 }
1617 else
1618 {
1619 PyErr_SetObject(PyExc_KeyError, keyObject);
1620 return NULL;
1621 }
1622 }
1623 else if (flags & DICT_FLAG_RETURN_BOOL)
1624 {
1625 Py_INCREF(Py_True);
1626 return Py_True;
1627 }
1628
1629 di = dict_lookup(hi);
1630
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001631 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632 return NULL;
1633
1634 if (flags & DICT_FLAG_POP)
1635 {
1636 if (dict->dv_lock)
1637 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001638 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001639 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001640 return NULL;
1641 }
1642
1643 hash_remove(&dict->dv_hashtab, hi);
1644 dictitem_free(di);
1645 }
1646
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001647 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001648}
1649
1650 static PyObject *
1651DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1652{
1653 return _DictionaryItem(self, keyObject, 0);
1654}
1655
1656 static int
1657DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1658{
1659 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001660 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001661
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001662 if (rObj == NULL)
1663 return -1;
1664
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001665 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001666
Bram Moolenaardee2e312013-06-23 16:35:47 +02001667 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001668
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001669 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001670}
1671
1672typedef struct
1673{
1674 hashitem_T *ht_array;
1675 long_u ht_used;
1676 hashtab_T *ht;
1677 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001678 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001679} dictiterinfo_T;
1680
1681 static PyObject *
1682DictionaryIterNext(dictiterinfo_T **dii)
1683{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001684 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001685
1686 if (!(*dii)->todo)
1687 return NULL;
1688
1689 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1690 (*dii)->ht->ht_used != (*dii)->ht_used)
1691 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001692 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001693 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001694 return NULL;
1695 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001696
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1698 ++((*dii)->hi);
1699
1700 --((*dii)->todo);
1701
Bram Moolenaar41009372013-07-01 22:03:04 +02001702 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001703 return NULL;
1704
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001705 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001706}
1707
1708 static PyObject *
1709DictionaryIter(DictionaryObject *self)
1710{
1711 dictiterinfo_T *dii;
1712 hashtab_T *ht;
1713
1714 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1715 {
1716 PyErr_NoMemory();
1717 return NULL;
1718 }
1719
1720 ht = &self->dict->dv_hashtab;
1721 dii->ht_array = ht->ht_array;
1722 dii->ht_used = ht->ht_used;
1723 dii->ht = ht;
1724 dii->hi = dii->ht_array;
1725 dii->todo = dii->ht_used;
1726
1727 return IterNew(dii,
1728 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1729 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001730}
1731
1732 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001733DictionaryAssItem(
1734 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001735{
1736 char_u *key;
1737 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001738 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001739 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001740 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001741
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001742 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001743 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001744 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 return -1;
1746 }
1747
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001748 if (!(key = StringToChars(keyObject, &todecref)))
1749 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001750
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001751 if (*key == NUL)
1752 {
1753 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001754 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001755 return -1;
1756 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001757
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001758 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001759
1760 if (valObject == NULL)
1761 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001762 hashitem_T *hi;
1763
Bram Moolenaardb913952012-06-29 12:54:53 +02001764 if (di == NULL)
1765 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001766 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001767 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001768 return -1;
1769 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001770 hi = hash_find(&dict->dv_hashtab, di->di_key);
1771 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001772 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001773 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001774 return 0;
1775 }
1776
1777 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001778 {
1779 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001781 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001782
1783 if (di == NULL)
1784 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001787 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001788 PyErr_NoMemory();
1789 return -1;
1790 }
1791 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001793
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001794 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001795 {
1796 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001797 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001798 RAISE_KEY_ADD_FAIL(key);
1799 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001800 return -1;
1801 }
1802 }
1803 else
1804 clear_tv(&di->di_tv);
1805
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001806 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001807
1808 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001809 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001810 return 0;
1811}
1812
Bram Moolenaara9922d62013-05-30 13:01:18 +02001813typedef PyObject *(*hi_to_py)(hashitem_T *);
1814
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001817{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001818 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001819 long_u todo = dict->dv_hashtab.ht_used;
1820 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001821 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001822 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001823 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001824
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001825 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001826 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1827 {
1828 if (!HASHITEM_EMPTY(hi))
1829 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001830 if (!(newObj = hiconvert(hi)))
1831 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001832 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001833 return NULL;
1834 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001835 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001836 --todo;
1837 ++i;
1838 }
1839 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001840 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001841}
1842
Bram Moolenaara9922d62013-05-30 13:01:18 +02001843 static PyObject *
1844dict_key(hashitem_T *hi)
1845{
1846 return PyBytes_FromString((char *)(hi->hi_key));
1847}
1848
1849 static PyObject *
1850DictionaryListKeys(DictionaryObject *self)
1851{
1852 return DictionaryListObjects(self, dict_key);
1853}
1854
1855 static PyObject *
1856dict_val(hashitem_T *hi)
1857{
1858 dictitem_T *di;
1859
1860 di = dict_lookup(hi);
1861 return ConvertToPyObject(&di->di_tv);
1862}
1863
1864 static PyObject *
1865DictionaryListValues(DictionaryObject *self)
1866{
1867 return DictionaryListObjects(self, dict_val);
1868}
1869
1870 static PyObject *
1871dict_item(hashitem_T *hi)
1872{
1873 PyObject *keyObject;
1874 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001875 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001876
1877 if (!(keyObject = dict_key(hi)))
1878 return NULL;
1879
1880 if (!(valObject = dict_val(hi)))
1881 {
1882 Py_DECREF(keyObject);
1883 return NULL;
1884 }
1885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001886 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001887
1888 Py_DECREF(keyObject);
1889 Py_DECREF(valObject);
1890
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001891 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001892}
1893
1894 static PyObject *
1895DictionaryListItems(DictionaryObject *self)
1896{
1897 return DictionaryListObjects(self, dict_item);
1898}
1899
1900 static PyObject *
1901DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1902{
1903 dict_T *dict = self->dict;
1904
1905 if (dict->dv_lock)
1906 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001907 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001908 return NULL;
1909 }
1910
1911 if (kwargs)
1912 {
1913 typval_T tv;
1914
1915 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1916 return NULL;
1917
1918 VimTryStart();
1919 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1920 clear_tv(&tv);
1921 if (VimTryEnd())
1922 return NULL;
1923 }
1924 else
1925 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001926 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001927
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001928 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001929 return NULL;
1930
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001931 if (PyObject_HasAttrString(obj, "keys"))
1932 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001933 else
1934 {
1935 PyObject *iterator;
1936 PyObject *item;
1937
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001938 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939 return NULL;
1940
1941 while ((item = PyIter_Next(iterator)))
1942 {
1943 PyObject *fast;
1944 PyObject *keyObject;
1945 PyObject *valObject;
1946 PyObject *todecref;
1947 char_u *key;
1948 dictitem_T *di;
1949
1950 if (!(fast = PySequence_Fast(item, "")))
1951 {
1952 Py_DECREF(iterator);
1953 Py_DECREF(item);
1954 return NULL;
1955 }
1956
1957 Py_DECREF(item);
1958
1959 if (PySequence_Fast_GET_SIZE(fast) != 2)
1960 {
1961 Py_DECREF(iterator);
1962 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001963 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001964 N_("expected sequence element of size 2, "
1965 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001966 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001967 return NULL;
1968 }
1969
1970 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1971
1972 if (!(key = StringToChars(keyObject, &todecref)))
1973 {
1974 Py_DECREF(iterator);
1975 Py_DECREF(fast);
1976 return NULL;
1977 }
1978
1979 di = dictitem_alloc(key);
1980
1981 Py_XDECREF(todecref);
1982
1983 if (di == NULL)
1984 {
1985 Py_DECREF(fast);
1986 Py_DECREF(iterator);
1987 PyErr_NoMemory();
1988 return NULL;
1989 }
1990 di->di_tv.v_lock = 0;
1991 di->di_tv.v_type = VAR_UNKNOWN;
1992
1993 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1994
1995 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1996 {
1997 Py_DECREF(iterator);
1998 Py_DECREF(fast);
1999 dictitem_free(di);
2000 return NULL;
2001 }
2002
2003 Py_DECREF(fast);
2004
2005 if (dict_add(dict, di) == FAIL)
2006 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002007 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002008 Py_DECREF(iterator);
2009 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010 return NULL;
2011 }
2012 }
2013
2014 Py_DECREF(iterator);
2015
2016 /* Iterator may have finished due to an exception */
2017 if (PyErr_Occurred())
2018 return NULL;
2019 }
2020 }
2021 Py_INCREF(Py_None);
2022 return Py_None;
2023}
2024
2025 static PyObject *
2026DictionaryGet(DictionaryObject *self, PyObject *args)
2027{
2028 return _DictionaryItem(self, args,
2029 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2030}
2031
2032 static PyObject *
2033DictionaryPop(DictionaryObject *self, PyObject *args)
2034{
2035 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2036}
2037
2038 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002039DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002040{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002041 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002042 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002043 PyObject *valObject;
2044 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045
Bram Moolenaarde71b562013-06-02 17:41:54 +02002046 if (self->dict->dv_hashtab.ht_used == 0)
2047 {
2048 PyErr_SetNone(PyExc_KeyError);
2049 return NULL;
2050 }
2051
2052 hi = self->dict->dv_hashtab.ht_array;
2053 while (HASHITEM_EMPTY(hi))
2054 ++hi;
2055
2056 di = dict_lookup(hi);
2057
2058 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002059 return NULL;
2060
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002061 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002062 {
2063 Py_DECREF(valObject);
2064 return NULL;
2065 }
2066
2067 hash_remove(&self->dict->dv_hashtab, hi);
2068 dictitem_free(di);
2069
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002070 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002071}
2072
2073 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002074DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002075{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002076 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2077}
2078
2079static PySequenceMethods DictionaryAsSeq = {
2080 0, /* sq_length */
2081 0, /* sq_concat */
2082 0, /* sq_repeat */
2083 0, /* sq_item */
2084 0, /* sq_slice */
2085 0, /* sq_ass_item */
2086 0, /* sq_ass_slice */
2087 (objobjproc) DictionaryContains, /* sq_contains */
2088 0, /* sq_inplace_concat */
2089 0, /* sq_inplace_repeat */
2090};
2091
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002092static PyMappingMethods DictionaryAsMapping = {
2093 (lenfunc) DictionaryLength,
2094 (binaryfunc) DictionaryItem,
2095 (objobjargproc) DictionaryAssItem,
2096};
2097
Bram Moolenaardb913952012-06-29 12:54:53 +02002098static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002099 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002100 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2101 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2102 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2103 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2104 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002105 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002106 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002107 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2108 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002109};
2110
2111static PyTypeObject ListType;
2112
2113typedef struct
2114{
2115 PyObject_HEAD
2116 list_T *list;
2117 pylinkedlist_T ref;
2118} ListObject;
2119
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002120#define NEW_LIST(list) ListNew(&ListType, list)
2121
Bram Moolenaardb913952012-06-29 12:54:53 +02002122 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002123ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002124{
2125 ListObject *self;
2126
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002127 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002128 if (self == NULL)
2129 return NULL;
2130 self->list = list;
2131 ++list->lv_refcount;
2132
2133 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2134
2135 return (PyObject *)(self);
2136}
2137
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002138 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002139py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002140{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002141 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002142
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002143 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002144 {
2145 PyErr_NoMemory();
2146 return NULL;
2147 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002148 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002149
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002150 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002151}
2152
2153 static int
2154list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2155{
2156 PyObject *iterator;
2157 PyObject *item;
2158 listitem_T *li;
2159
2160 if (!(iterator = PyObject_GetIter(obj)))
2161 return -1;
2162
2163 while ((item = PyIter_Next(iterator)))
2164 {
2165 if (!(li = listitem_alloc()))
2166 {
2167 PyErr_NoMemory();
2168 Py_DECREF(item);
2169 Py_DECREF(iterator);
2170 return -1;
2171 }
2172 li->li_tv.v_lock = 0;
2173 li->li_tv.v_type = VAR_UNKNOWN;
2174
2175 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2176 {
2177 Py_DECREF(item);
2178 Py_DECREF(iterator);
2179 listitem_free(li);
2180 return -1;
2181 }
2182
2183 Py_DECREF(item);
2184
2185 list_append(l, li);
2186 }
2187
2188 Py_DECREF(iterator);
2189
2190 /* Iterator may have finished due to an exception */
2191 if (PyErr_Occurred())
2192 return -1;
2193
2194 return 0;
2195}
2196
2197 static PyObject *
2198ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2199{
2200 list_T *list;
2201 PyObject *obj = NULL;
2202
2203 if (kwargs)
2204 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002205 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002206 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002207 return NULL;
2208 }
2209
2210 if (!PyArg_ParseTuple(args, "|O", &obj))
2211 return NULL;
2212
2213 if (!(list = py_list_alloc()))
2214 return NULL;
2215
2216 if (obj)
2217 {
2218 PyObject *lookup_dict;
2219
2220 if (!(lookup_dict = PyDict_New()))
2221 {
2222 list_unref(list);
2223 return NULL;
2224 }
2225
2226 if (list_py_concat(list, obj, lookup_dict) == -1)
2227 {
2228 Py_DECREF(lookup_dict);
2229 list_unref(list);
2230 return NULL;
2231 }
2232
2233 Py_DECREF(lookup_dict);
2234 }
2235
2236 return ListNew(subtype, list);
2237}
2238
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002239 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002240ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002241{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002242 pyll_remove(&self->ref, &lastlist);
2243 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002244
2245 DESTRUCTOR_FINISH(self);
2246}
2247
Bram Moolenaardb913952012-06-29 12:54:53 +02002248 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002249ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002250{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002251 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002252}
2253
2254 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002255ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002256{
2257 listitem_T *li;
2258
Bram Moolenaard6e39182013-05-21 18:30:34 +02002259 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002260 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002261 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002262 return NULL;
2263 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002264 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002265 if (li == NULL)
2266 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002267 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002268 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002269 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002270 return NULL;
2271 }
2272 return ConvertToPyObject(&li->li_tv);
2273}
2274
Bram Moolenaardb913952012-06-29 12:54:53 +02002275 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002276ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2277 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002278{
2279 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002280 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002281
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002282 if (step == 0)
2283 {
2284 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2285 return NULL;
2286 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002287
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002288 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002289 if (list == NULL)
2290 return NULL;
2291
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002292 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002293 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002294 PyObject *item;
2295
2296 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002297 if (item == NULL)
2298 {
2299 Py_DECREF(list);
2300 return NULL;
2301 }
2302
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002303 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002304 }
2305
2306 return list;
2307}
2308
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002309 static PyObject *
2310ListItem(ListObject *self, PyObject* idx)
2311{
2312#if PY_MAJOR_VERSION < 3
2313 if (PyInt_Check(idx))
2314 {
2315 long _idx = PyInt_AsLong(idx);
2316 return ListIndex(self, _idx);
2317 }
2318 else
2319#endif
2320 if (PyLong_Check(idx))
2321 {
2322 long _idx = PyLong_AsLong(idx);
2323 return ListIndex(self, _idx);
2324 }
2325 else if (PySlice_Check(idx))
2326 {
2327 Py_ssize_t start, stop, step, slicelen;
2328
2329 if (PySlice_GetIndicesEx(idx, ListLength(self),
2330 &start, &stop, &step, &slicelen) < 0)
2331 return NULL;
2332 return ListSlice(self, start, step, slicelen);
2333 }
2334 else
2335 {
2336 RAISE_INVALID_INDEX_TYPE(idx);
2337 return NULL;
2338 }
2339}
2340
2341 static void
2342list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2343 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2344{
2345 while (numreplaced--)
2346 {
2347 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2348 listitem_remove(l, lis[slicelen + numreplaced]);
2349 }
2350 while (numadded--)
2351 {
2352 listitem_T *next;
2353
2354 next = lastaddedli->li_prev;
2355 listitem_remove(l, lastaddedli);
2356 lastaddedli = next;
2357 }
2358}
2359
2360 static int
2361ListAssSlice(ListObject *self, Py_ssize_t first,
2362 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2363{
2364 PyObject *iterator;
2365 PyObject *item;
2366 listitem_T *li;
2367 listitem_T *lastaddedli = NULL;
2368 listitem_T *next;
2369 typval_T v;
2370 list_T *l = self->list;
2371 PyInt i;
2372 PyInt j;
2373 PyInt numreplaced = 0;
2374 PyInt numadded = 0;
2375 PyInt size;
2376 listitem_T **lis;
2377
2378 size = ListLength(self);
2379
2380 if (l->lv_lock)
2381 {
2382 RAISE_LOCKED_LIST;
2383 return -1;
2384 }
2385
2386 if (step == 0)
2387 {
2388 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2389 return -1;
2390 }
2391
2392 if (step != 1 && slicelen == 0)
2393 {
2394 /* Nothing to do. Only error out if obj has some items. */
2395 int ret = 0;
2396
2397 if (obj == NULL)
2398 return 0;
2399
2400 if (!(iterator = PyObject_GetIter(obj)))
2401 return -1;
2402
2403 if ((item = PyIter_Next(iterator)))
2404 {
2405 PyErr_FORMAT(PyExc_ValueError,
2406 N_("attempt to assign sequence of size greater then %d "
2407 "to extended slice"), 0);
2408 Py_DECREF(item);
2409 ret = -1;
2410 }
2411 Py_DECREF(iterator);
2412 return ret;
2413 }
2414
2415 if (obj != NULL)
2416 /* XXX May allocate zero bytes. */
2417 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2418 {
2419 PyErr_NoMemory();
2420 return -1;
2421 }
2422
2423 if (first == size)
2424 li = NULL;
2425 else
2426 {
2427 li = list_find(l, (long) first);
2428 if (li == NULL)
2429 {
2430 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2431 (int)first);
2432 if (obj != NULL)
2433 PyMem_Free(lis);
2434 return -1;
2435 }
2436 i = slicelen;
2437 while (i-- && li != NULL)
2438 {
2439 j = step;
2440 next = li;
2441 if (step > 0)
2442 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2443 else
2444 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2445
2446 if (obj == NULL)
2447 listitem_remove(l, li);
2448 else
2449 lis[slicelen - i - 1] = li;
2450
2451 li = next;
2452 }
2453 if (li == NULL && i != -1)
2454 {
2455 PyErr_SET_VIM(N_("internal error: not enough list items"));
2456 if (obj != NULL)
2457 PyMem_Free(lis);
2458 return -1;
2459 }
2460 }
2461
2462 if (obj == NULL)
2463 return 0;
2464
2465 if (!(iterator = PyObject_GetIter(obj)))
2466 {
2467 PyMem_Free(lis);
2468 return -1;
2469 }
2470
2471 i = 0;
2472 while ((item = PyIter_Next(iterator)))
2473 {
2474 if (ConvertFromPyObject(item, &v) == -1)
2475 {
2476 Py_DECREF(iterator);
2477 Py_DECREF(item);
2478 PyMem_Free(lis);
2479 return -1;
2480 }
2481 Py_DECREF(item);
2482 if (list_insert_tv(l, &v, numreplaced < slicelen
2483 ? lis[numreplaced]
2484 : li) == FAIL)
2485 {
2486 clear_tv(&v);
2487 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2488 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2489 PyMem_Free(lis);
2490 return -1;
2491 }
2492 if (numreplaced < slicelen)
2493 {
2494 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
2495 list_remove(l, lis[numreplaced], lis[numreplaced]);
2496 numreplaced++;
2497 }
2498 else
2499 {
2500 if (li)
2501 lastaddedli = li->li_prev;
2502 else
2503 lastaddedli = l->lv_last;
2504 numadded++;
2505 }
2506 clear_tv(&v);
2507 if (step != 1 && i >= slicelen)
2508 {
2509 Py_DECREF(iterator);
2510 PyErr_FORMAT(PyExc_ValueError,
2511 N_("attempt to assign sequence of size greater then %d "
2512 "to extended slice"), slicelen);
2513 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2514 PyMem_Free(lis);
2515 return -1;
2516 }
2517 ++i;
2518 }
2519 Py_DECREF(iterator);
2520
2521 if (step != 1 && i != slicelen)
2522 {
2523 PyErr_FORMAT2(PyExc_ValueError,
2524 N_("attempt to assign sequence of size %d to extended slice "
2525 "of size %d"), i, slicelen);
2526 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2527 PyMem_Free(lis);
2528 return -1;
2529 }
2530
2531 if (PyErr_Occurred())
2532 {
2533 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2534 PyMem_Free(lis);
2535 return -1;
2536 }
2537
2538 for (i = 0; i < numreplaced; i++)
2539 listitem_free(lis[i]);
2540 if (step == 1)
2541 for (i = numreplaced; i < slicelen; i++)
2542 listitem_remove(l, lis[i]);
2543
2544 PyMem_Free(lis);
2545
2546 return 0;
2547}
2548
2549 static int
2550ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2551{
2552 typval_T tv;
2553 list_T *l = self->list;
2554 listitem_T *li;
2555 Py_ssize_t length = ListLength(self);
2556
2557 if (l->lv_lock)
2558 {
2559 RAISE_LOCKED_LIST;
2560 return -1;
2561 }
2562 if (index > length || (index == length && obj == NULL))
2563 {
2564 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2565 return -1;
2566 }
2567
2568 if (obj == NULL)
2569 {
2570 li = list_find(l, (long) index);
2571 list_remove(l, li, li);
2572 clear_tv(&li->li_tv);
2573 vim_free(li);
2574 return 0;
2575 }
2576
2577 if (ConvertFromPyObject(obj, &tv) == -1)
2578 return -1;
2579
2580 if (index == length)
2581 {
2582 if (list_append_tv(l, &tv) == FAIL)
2583 {
2584 clear_tv(&tv);
2585 PyErr_SET_VIM(N_("failed to add item to list"));
2586 return -1;
2587 }
2588 }
2589 else
2590 {
2591 li = list_find(l, (long) index);
2592 clear_tv(&li->li_tv);
2593 copy_tv(&tv, &li->li_tv);
2594 clear_tv(&tv);
2595 }
2596 return 0;
2597}
2598
2599 static Py_ssize_t
2600ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2601{
2602#if PY_MAJOR_VERSION < 3
2603 if (PyInt_Check(idx))
2604 {
2605 long _idx = PyInt_AsLong(idx);
2606 return ListAssIndex(self, _idx, obj);
2607 }
2608 else
2609#endif
2610 if (PyLong_Check(idx))
2611 {
2612 long _idx = PyLong_AsLong(idx);
2613 return ListAssIndex(self, _idx, obj);
2614 }
2615 else if (PySlice_Check(idx))
2616 {
2617 Py_ssize_t start, stop, step, slicelen;
2618
2619 if (PySlice_GetIndicesEx(idx, ListLength(self),
2620 &start, &stop, &step, &slicelen) < 0)
2621 return -1;
2622 return ListAssSlice(self, start, step, slicelen,
2623 obj);
2624 }
2625 else
2626 {
2627 RAISE_INVALID_INDEX_TYPE(idx);
2628 return -1;
2629 }
2630}
2631
2632 static PyObject *
2633ListConcatInPlace(ListObject *self, PyObject *obj)
2634{
2635 list_T *l = self->list;
2636 PyObject *lookup_dict;
2637
2638 if (l->lv_lock)
2639 {
2640 RAISE_LOCKED_LIST;
2641 return NULL;
2642 }
2643
2644 if (!(lookup_dict = PyDict_New()))
2645 return NULL;
2646
2647 if (list_py_concat(l, obj, lookup_dict) == -1)
2648 {
2649 Py_DECREF(lookup_dict);
2650 return NULL;
2651 }
2652 Py_DECREF(lookup_dict);
2653
2654 Py_INCREF(self);
2655 return (PyObject *)(self);
2656}
2657
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002658typedef struct
2659{
2660 listwatch_T lw;
2661 list_T *list;
2662} listiterinfo_T;
2663
2664 static void
2665ListIterDestruct(listiterinfo_T *lii)
2666{
2667 list_rem_watch(lii->list, &lii->lw);
2668 PyMem_Free(lii);
2669}
2670
2671 static PyObject *
2672ListIterNext(listiterinfo_T **lii)
2673{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002674 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002675
2676 if (!((*lii)->lw.lw_item))
2677 return NULL;
2678
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002679 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002680 return NULL;
2681
2682 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2683
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002684 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002685}
2686
2687 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002688ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002689{
2690 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002691 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002692
2693 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2694 {
2695 PyErr_NoMemory();
2696 return NULL;
2697 }
2698
2699 list_add_watch(l, &lii->lw);
2700 lii->lw.lw_item = l->lv_first;
2701 lii->list = l;
2702
2703 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002704 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2705 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002706}
2707
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002708static char *ListAttrs[] = {
2709 "locked",
2710 NULL
2711};
2712
2713 static PyObject *
2714ListDir(PyObject *self)
2715{
2716 return ObjectDir(self, ListAttrs);
2717}
2718
Bram Moolenaar66b79852012-09-21 14:00:35 +02002719 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002720ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002721{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002722 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002723 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002724 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002725 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002726 return -1;
2727 }
2728
2729 if (strcmp(name, "locked") == 0)
2730 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002731 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002732 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002733 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002734 return -1;
2735 }
2736 else
2737 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002738 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002739 if (istrue == -1)
2740 return -1;
2741 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002742 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002743 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002744 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002745 }
2746 return 0;
2747 }
2748 else
2749 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002750 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002751 return -1;
2752 }
2753}
2754
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002755static PySequenceMethods ListAsSeq = {
2756 (lenfunc) ListLength, /* sq_length, len(x) */
2757 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2758 0, /* RangeRepeat, sq_repeat, x*n */
2759 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2760 0, /* was_sq_slice, x[i:j] */
2761 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2762 0, /* was_sq_ass_slice, x[i:j]=v */
2763 0, /* sq_contains */
2764 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2765 0, /* sq_inplace_repeat */
2766};
2767
2768static PyMappingMethods ListAsMapping = {
2769 /* mp_length */ (lenfunc) ListLength,
2770 /* mp_subscript */ (binaryfunc) ListItem,
2771 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2772};
2773
Bram Moolenaardb913952012-06-29 12:54:53 +02002774static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002775 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2776 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2777 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002778};
2779
2780typedef struct
2781{
2782 PyObject_HEAD
2783 char_u *name;
2784} FunctionObject;
2785
2786static PyTypeObject FunctionType;
2787
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002788#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2789
Bram Moolenaardb913952012-06-29 12:54:53 +02002790 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002791FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002792{
2793 FunctionObject *self;
2794
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002795 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2796
Bram Moolenaardb913952012-06-29 12:54:53 +02002797 if (self == NULL)
2798 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002799
2800 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002801 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002802 if (!translated_function_exists(name))
2803 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002804 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002805 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002806 return NULL;
2807 }
2808 self->name = vim_strsave(name);
2809 func_ref(self->name);
2810 }
2811 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002812 if ((self->name = get_expanded_name(name,
2813 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2814 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002815 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002816 PyErr_FORMAT(PyExc_ValueError,
2817 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002818 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002819 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002820
2821 return (PyObject *)(self);
2822}
2823
2824 static PyObject *
2825FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2826{
2827 PyObject *self;
2828 char_u *name;
2829
2830 if (kwargs)
2831 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002832 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002833 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002834 return NULL;
2835 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002836
Bram Moolenaar389a1792013-06-23 13:00:44 +02002837 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002838 return NULL;
2839
2840 self = FunctionNew(subtype, name);
2841
Bram Moolenaar389a1792013-06-23 13:00:44 +02002842 PyMem_Free(name);
2843
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002844 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002845}
2846
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002847 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002848FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002849{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002850 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002851 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002852
2853 DESTRUCTOR_FINISH(self);
2854}
2855
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002856static char *FunctionAttrs[] = {
2857 "softspace",
2858 NULL
2859};
2860
2861 static PyObject *
2862FunctionDir(PyObject *self)
2863{
2864 return ObjectDir(self, FunctionAttrs);
2865}
2866
Bram Moolenaardb913952012-06-29 12:54:53 +02002867 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002868FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002869{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002870 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002871 typval_T args;
2872 typval_T selfdicttv;
2873 typval_T rettv;
2874 dict_T *selfdict = NULL;
2875 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002876 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002877 int error;
2878
2879 if (ConvertFromPyObject(argsObject, &args) == -1)
2880 return NULL;
2881
2882 if (kwargs != NULL)
2883 {
2884 selfdictObject = PyDict_GetItemString(kwargs, "self");
2885 if (selfdictObject != NULL)
2886 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002887 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002888 {
2889 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002890 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002891 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002892 selfdict = selfdicttv.vval.v_dict;
2893 }
2894 }
2895
Bram Moolenaar71700b82013-05-15 17:49:05 +02002896 Py_BEGIN_ALLOW_THREADS
2897 Python_Lock_Vim();
2898
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002899 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002900 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002901
2902 Python_Release_Vim();
2903 Py_END_ALLOW_THREADS
2904
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002905 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002906 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002907 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002908 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002909 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002910 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002911 }
2912 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002913 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002914
Bram Moolenaardb913952012-06-29 12:54:53 +02002915 clear_tv(&args);
2916 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002917 if (selfdict != NULL)
2918 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002919
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002920 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002921}
2922
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002923 static PyObject *
2924FunctionRepr(FunctionObject *self)
2925{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002926#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002927 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002928 * Finalize */
2929 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002930 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002931#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002932 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002933#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002934}
2935
Bram Moolenaardb913952012-06-29 12:54:53 +02002936static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002937 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2938 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002939};
2940
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002941/*
2942 * Options object
2943 */
2944
2945static PyTypeObject OptionsType;
2946
2947typedef int (*checkfun)(void *);
2948
2949typedef struct
2950{
2951 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002952 int opt_type;
2953 void *from;
2954 checkfun Check;
2955 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002956} OptionsObject;
2957
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002958 static int
2959dummy_check(void *arg UNUSED)
2960{
2961 return 0;
2962}
2963
2964 static PyObject *
2965OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2966{
2967 OptionsObject *self;
2968
Bram Moolenaar774267b2013-05-21 20:51:59 +02002969 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002970 if (self == NULL)
2971 return NULL;
2972
2973 self->opt_type = opt_type;
2974 self->from = from;
2975 self->Check = Check;
2976 self->fromObj = fromObj;
2977 if (fromObj)
2978 Py_INCREF(fromObj);
2979
2980 return (PyObject *)(self);
2981}
2982
2983 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002984OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002985{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002986 PyObject_GC_UnTrack((void *)(self));
2987 Py_XDECREF(self->fromObj);
2988 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002989}
2990
2991 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002992OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002993{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002994 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002995 return 0;
2996}
2997
2998 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002999OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003000{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003001 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003002 return 0;
3003}
3004
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003005 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003006OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003007{
3008 char_u *key;
3009 int flags;
3010 long numval;
3011 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003012 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003013
Bram Moolenaard6e39182013-05-21 18:30:34 +02003014 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003015 return NULL;
3016
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003017 if (!(key = StringToChars(keyObject, &todecref)))
3018 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003019
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003020 if (*key == NUL)
3021 {
3022 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003023 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003024 return NULL;
3025 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003026
3027 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003028 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003029
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003030 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003031
3032 if (flags == 0)
3033 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003034 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003035 return NULL;
3036 }
3037
3038 if (flags & SOPT_UNSET)
3039 {
3040 Py_INCREF(Py_None);
3041 return Py_None;
3042 }
3043 else if (flags & SOPT_BOOL)
3044 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003045 PyObject *ret;
3046 ret = numval ? Py_True : Py_False;
3047 Py_INCREF(ret);
3048 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003049 }
3050 else if (flags & SOPT_NUM)
3051 return PyInt_FromLong(numval);
3052 else if (flags & SOPT_STRING)
3053 {
3054 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003055 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003056 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003057 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003058 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003059 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003060 else
3061 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003062 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003063 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003064 return NULL;
3065 }
3066 }
3067 else
3068 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003069 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003070 return NULL;
3071 }
3072}
3073
3074 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003075OptionsContains(OptionsObject *self, PyObject *keyObject)
3076{
3077 char_u *key;
3078 PyObject *todecref;
3079
3080 if (!(key = StringToChars(keyObject, &todecref)))
3081 return -1;
3082
3083 if (*key == NUL)
3084 {
3085 Py_XDECREF(todecref);
3086 return 0;
3087 }
3088
3089 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3090 {
3091 Py_XDECREF(todecref);
3092 return 1;
3093 }
3094 else
3095 {
3096 Py_XDECREF(todecref);
3097 return 0;
3098 }
3099}
3100
3101typedef struct
3102{
3103 void *lastoption;
3104 int opt_type;
3105} optiterinfo_T;
3106
3107 static PyObject *
3108OptionsIterNext(optiterinfo_T **oii)
3109{
3110 char_u *name;
3111
3112 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3113 return PyString_FromString((char *)name);
3114
3115 return NULL;
3116}
3117
3118 static PyObject *
3119OptionsIter(OptionsObject *self)
3120{
3121 optiterinfo_T *oii;
3122
3123 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3124 {
3125 PyErr_NoMemory();
3126 return NULL;
3127 }
3128
3129 oii->opt_type = self->opt_type;
3130 oii->lastoption = NULL;
3131
3132 return IterNew(oii,
3133 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3134 NULL, NULL);
3135}
3136
3137 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003138set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003139{
3140 char_u *errmsg;
3141
3142 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3143 {
3144 if (VimTryEnd())
3145 return FAIL;
3146 PyErr_SetVim((char *)errmsg);
3147 return FAIL;
3148 }
3149 return OK;
3150}
3151
3152 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003153set_option_value_for(
3154 char_u *key,
3155 int numval,
3156 char_u *stringval,
3157 int opt_flags,
3158 int opt_type,
3159 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003160{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003161 win_T *save_curwin = NULL;
3162 tabpage_T *save_curtab = NULL;
3163 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003164 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003165
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003166 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003167 switch (opt_type)
3168 {
3169 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003170 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003171 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003172 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003173 if (VimTryEnd())
3174 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003175 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003176 return -1;
3177 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003178 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3179 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003180 break;
3181 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003182 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003183 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003184 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003185 break;
3186 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003187 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003188 break;
3189 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003190 if (set_ret == FAIL)
3191 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003192 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003193}
3194
3195 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003196OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003197{
3198 char_u *key;
3199 int flags;
3200 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003201 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003202 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003203
Bram Moolenaard6e39182013-05-21 18:30:34 +02003204 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003205 return -1;
3206
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003207 if (!(key = StringToChars(keyObject, &todecref)))
3208 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003209
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003210 if (*key == NUL)
3211 {
3212 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003213 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003214 return -1;
3215 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003216
3217 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003218 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003219
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003220 if (flags == 0)
3221 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003222 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003223 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003224 return -1;
3225 }
3226
3227 if (valObject == NULL)
3228 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003229 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003230 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003231 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003232 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003233 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003234 return -1;
3235 }
3236 else if (!(flags & SOPT_GLOBAL))
3237 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003238 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003239 N_("unable to unset option %s "
3240 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003241 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003242 return -1;
3243 }
3244 else
3245 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003246 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003247 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003248 return 0;
3249 }
3250 }
3251
Bram Moolenaard6e39182013-05-21 18:30:34 +02003252 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003253
3254 if (flags & SOPT_BOOL)
3255 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003256 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003257
Bram Moolenaarb983f752013-05-15 16:11:50 +02003258 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003259 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003260 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003261 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003262 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003263 }
3264 else if (flags & SOPT_NUM)
3265 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003266 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003267
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003268 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003269 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003270 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003271 return -1;
3272 }
3273
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003274 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003275 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003276 }
3277 else
3278 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003279 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003280 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003281
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003282 if ((val = StringToChars(valObject, &todecref2)))
3283 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003284 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003285 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003286 Py_XDECREF(todecref2);
3287 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003288 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003289 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003290 }
3291
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003292 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003293
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003294 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003295}
3296
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003297static PySequenceMethods OptionsAsSeq = {
3298 0, /* sq_length */
3299 0, /* sq_concat */
3300 0, /* sq_repeat */
3301 0, /* sq_item */
3302 0, /* sq_slice */
3303 0, /* sq_ass_item */
3304 0, /* sq_ass_slice */
3305 (objobjproc) OptionsContains, /* sq_contains */
3306 0, /* sq_inplace_concat */
3307 0, /* sq_inplace_repeat */
3308};
3309
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003310static PyMappingMethods OptionsAsMapping = {
3311 (lenfunc) NULL,
3312 (binaryfunc) OptionsItem,
3313 (objobjargproc) OptionsAssItem,
3314};
3315
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003316/* Tabpage object
3317 */
3318
3319typedef struct
3320{
3321 PyObject_HEAD
3322 tabpage_T *tab;
3323} TabPageObject;
3324
3325static PyObject *WinListNew(TabPageObject *tabObject);
3326
3327static PyTypeObject TabPageType;
3328
3329 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003330CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003331{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003332 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003333 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003334 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003335 return -1;
3336 }
3337
3338 return 0;
3339}
3340
3341 static PyObject *
3342TabPageNew(tabpage_T *tab)
3343{
3344 TabPageObject *self;
3345
3346 if (TAB_PYTHON_REF(tab))
3347 {
3348 self = TAB_PYTHON_REF(tab);
3349 Py_INCREF(self);
3350 }
3351 else
3352 {
3353 self = PyObject_NEW(TabPageObject, &TabPageType);
3354 if (self == NULL)
3355 return NULL;
3356 self->tab = tab;
3357 TAB_PYTHON_REF(tab) = self;
3358 }
3359
3360 return (PyObject *)(self);
3361}
3362
3363 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003364TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003365{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3367 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003368
3369 DESTRUCTOR_FINISH(self);
3370}
3371
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003372static char *TabPageAttrs[] = {
3373 "windows", "number", "vars", "window", "valid",
3374 NULL
3375};
3376
3377 static PyObject *
3378TabPageDir(PyObject *self)
3379{
3380 return ObjectDir(self, TabPageAttrs);
3381}
3382
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003383 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003384TabPageAttrValid(TabPageObject *self, char *name)
3385{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003386 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003387
3388 if (strcmp(name, "valid") != 0)
3389 return NULL;
3390
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003391 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3392 Py_INCREF(ret);
3393 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003394}
3395
3396 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003397TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003398{
3399 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003400 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003401 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003402 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003403 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003404 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003405 else if (strcmp(name, "window") == 0)
3406 {
3407 /* For current tab window.c does not bother to set or update tp_curwin
3408 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003409 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003410 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003411 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003412 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003413 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003414 else if (strcmp(name, "__members__") == 0)
3415 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003416 return NULL;
3417}
3418
3419 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003420TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003421{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003422 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003423 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003424 else
3425 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003426 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003427
3428 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003429 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3430 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003431 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003432 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003433 }
3434}
3435
3436static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003437 /* name, function, calling, documentation */
3438 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3439 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003440};
3441
3442/*
3443 * Window list object
3444 */
3445
3446static PyTypeObject TabListType;
3447static PySequenceMethods TabListAsSeq;
3448
3449typedef struct
3450{
3451 PyObject_HEAD
3452} TabListObject;
3453
3454 static PyInt
3455TabListLength(PyObject *self UNUSED)
3456{
3457 tabpage_T *tp = first_tabpage;
3458 PyInt n = 0;
3459
3460 while (tp != NULL)
3461 {
3462 ++n;
3463 tp = tp->tp_next;
3464 }
3465
3466 return n;
3467}
3468
3469 static PyObject *
3470TabListItem(PyObject *self UNUSED, PyInt n)
3471{
3472 tabpage_T *tp;
3473
3474 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3475 if (n == 0)
3476 return TabPageNew(tp);
3477
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003478 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003479 return NULL;
3480}
3481
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003482/*
3483 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003484 */
3485
3486typedef struct
3487{
3488 PyObject_HEAD
3489 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003490 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003491} WindowObject;
3492
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003493static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003494
3495 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003496CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003497{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003498 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003499 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003500 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003501 return -1;
3502 }
3503
3504 return 0;
3505}
3506
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003507 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003508WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003509{
3510 /* We need to handle deletion of windows underneath us.
3511 * If we add a "w_python*_ref" field to the win_T structure,
3512 * then we can get at it in win_free() in vim. We then
3513 * need to create only ONE Python object per window - if
3514 * we try to create a second, just INCREF the existing one
3515 * and return it. The (single) Python object referring to
3516 * the window is stored in "w_python*_ref".
3517 * On a win_free() we set the Python object's win_T* field
3518 * to an invalid value. We trap all uses of a window
3519 * object, and reject them if the win_T* field is invalid.
3520 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003521 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003522 * w_python_ref and w_python3_ref fields respectively.
3523 */
3524
3525 WindowObject *self;
3526
3527 if (WIN_PYTHON_REF(win))
3528 {
3529 self = WIN_PYTHON_REF(win);
3530 Py_INCREF(self);
3531 }
3532 else
3533 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003534 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003535 if (self == NULL)
3536 return NULL;
3537 self->win = win;
3538 WIN_PYTHON_REF(win) = self;
3539 }
3540
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003541 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3542
Bram Moolenaar971db462013-05-12 18:44:48 +02003543 return (PyObject *)(self);
3544}
3545
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003546 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003547WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003548{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003549 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003550 if (self->win && self->win != INVALID_WINDOW_VALUE)
3551 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003552 Py_XDECREF(((PyObject *)(self->tabObject)));
3553 PyObject_GC_Del((void *)(self));
3554}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003555
Bram Moolenaar774267b2013-05-21 20:51:59 +02003556 static int
3557WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3558{
3559 Py_VISIT(((PyObject *)(self->tabObject)));
3560 return 0;
3561}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003562
Bram Moolenaar774267b2013-05-21 20:51:59 +02003563 static int
3564WindowClear(WindowObject *self)
3565{
3566 Py_CLEAR(self->tabObject);
3567 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003568}
3569
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003570 static win_T *
3571get_firstwin(TabPageObject *tabObject)
3572{
3573 if (tabObject)
3574 {
3575 if (CheckTabPage(tabObject))
3576 return NULL;
3577 /* For current tab window.c does not bother to set or update tp_firstwin
3578 */
3579 else if (tabObject->tab == curtab)
3580 return firstwin;
3581 else
3582 return tabObject->tab->tp_firstwin;
3583 }
3584 else
3585 return firstwin;
3586}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003587static char *WindowAttrs[] = {
3588 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3589 "tabpage", "valid",
3590 NULL
3591};
3592
3593 static PyObject *
3594WindowDir(PyObject *self)
3595{
3596 return ObjectDir(self, WindowAttrs);
3597}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003598
Bram Moolenaar971db462013-05-12 18:44:48 +02003599 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003600WindowAttrValid(WindowObject *self, char *name)
3601{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003602 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003603
3604 if (strcmp(name, "valid") != 0)
3605 return NULL;
3606
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003607 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3608 Py_INCREF(ret);
3609 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003610}
3611
3612 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003613WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003614{
3615 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003616 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003617 else if (strcmp(name, "cursor") == 0)
3618 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003619 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003620
3621 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3622 }
3623 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003624 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003625#ifdef FEAT_WINDOWS
3626 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003627 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003628#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003629#ifdef FEAT_VERTSPLIT
3630 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003631 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003632 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003633 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003634#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003635 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003636 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003637 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003638 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3639 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003640 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003641 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003642 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003643 return NULL;
3644 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003645 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003646 }
3647 else if (strcmp(name, "tabpage") == 0)
3648 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003649 Py_INCREF(self->tabObject);
3650 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003651 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003652 else if (strcmp(name, "__members__") == 0)
3653 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003654 else
3655 return NULL;
3656}
3657
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003658 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003659WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003660{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003661 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003662 return -1;
3663
3664 if (strcmp(name, "buffer") == 0)
3665 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003666 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003667 return -1;
3668 }
3669 else if (strcmp(name, "cursor") == 0)
3670 {
3671 long lnum;
3672 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003673
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003674 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003675 return -1;
3676
Bram Moolenaard6e39182013-05-21 18:30:34 +02003677 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003678 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003679 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003680 return -1;
3681 }
3682
3683 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003684 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003685 return -1;
3686
Bram Moolenaard6e39182013-05-21 18:30:34 +02003687 self->win->w_cursor.lnum = lnum;
3688 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003689#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003690 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003692 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003693 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003694
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003695 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696 return 0;
3697 }
3698 else if (strcmp(name, "height") == 0)
3699 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003700 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003701 win_T *savewin;
3702
Bram Moolenaardee2e312013-06-23 16:35:47 +02003703 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 return -1;
3705
3706#ifdef FEAT_GUI
3707 need_mouse_correct = TRUE;
3708#endif
3709 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003710 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003711
3712 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003713 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003714 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003715 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003716 return -1;
3717
3718 return 0;
3719 }
3720#ifdef FEAT_VERTSPLIT
3721 else if (strcmp(name, "width") == 0)
3722 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003723 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003724 win_T *savewin;
3725
Bram Moolenaardee2e312013-06-23 16:35:47 +02003726 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003727 return -1;
3728
3729#ifdef FEAT_GUI
3730 need_mouse_correct = TRUE;
3731#endif
3732 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003733 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003734
3735 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003736 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003737 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003738 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003739 return -1;
3740
3741 return 0;
3742 }
3743#endif
3744 else
3745 {
3746 PyErr_SetString(PyExc_AttributeError, name);
3747 return -1;
3748 }
3749}
3750
3751 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003752WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003753{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003754 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003755 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003756 else
3757 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003758 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003759
Bram Moolenaar6d216452013-05-12 19:00:41 +02003760 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003761 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003762 (self));
3763 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003764 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003765 }
3766}
3767
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003768static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003769 /* name, function, calling, documentation */
3770 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3771 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003772};
3773
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003774/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003775 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003777
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003778static PyTypeObject WinListType;
3779static PySequenceMethods WinListAsSeq;
3780
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003781typedef struct
3782{
3783 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003784 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003785} WinListObject;
3786
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003787 static PyObject *
3788WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003789{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003790 WinListObject *self;
3791
3792 self = PyObject_NEW(WinListObject, &WinListType);
3793 self->tabObject = tabObject;
3794 Py_INCREF(tabObject);
3795
3796 return (PyObject *)(self);
3797}
3798
3799 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003800WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003801{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003802 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003803
3804 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003805 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003806 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003807 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003808
3809 DESTRUCTOR_FINISH(self);
3810}
3811
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003812 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003813WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003814{
3815 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003816 PyInt n = 0;
3817
Bram Moolenaard6e39182013-05-21 18:30:34 +02003818 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003819 return -1;
3820
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003821 while (w != NULL)
3822 {
3823 ++n;
3824 w = W_NEXT(w);
3825 }
3826
3827 return n;
3828}
3829
3830 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003831WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003832{
3833 win_T *w;
3834
Bram Moolenaard6e39182013-05-21 18:30:34 +02003835 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003836 return NULL;
3837
3838 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003839 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003840 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003842 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003843 return NULL;
3844}
3845
3846/* Convert a Python string into a Vim line.
3847 *
3848 * The result is in allocated memory. All internal nulls are replaced by
3849 * newline characters. It is an error for the string to contain newline
3850 * characters.
3851 *
3852 * On errors, the Python exception data is set, and NULL is returned.
3853 */
3854 static char *
3855StringToLine(PyObject *obj)
3856{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003857 char *str;
3858 char *save;
3859 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003860 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003861 PyInt i;
3862 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003863
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003864 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003865 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003866 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3867 || str == NULL)
3868 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003869 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003870 else if (PyUnicode_Check(obj))
3871 {
3872 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3873 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003874
Bram Moolenaardaa27022013-06-24 22:33:30 +02003875 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003876 || str == NULL)
3877 {
3878 Py_DECREF(bytes);
3879 return NULL;
3880 }
3881 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003882 else
3883 {
3884#if PY_MAJOR_VERSION < 3
3885 PyErr_FORMAT(PyExc_TypeError,
3886 N_("expected str() or unicode() instance, but got %s"),
3887 Py_TYPE_NAME(obj));
3888#else
3889 PyErr_FORMAT(PyExc_TypeError,
3890 N_("expected bytes() or str() instance, but got %s"),
3891 Py_TYPE_NAME(obj));
3892#endif
3893 return NULL;
3894 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003895
3896 /*
3897 * Error checking: String must not contain newlines, as we
3898 * are replacing a single line, and we must replace it with
3899 * a single line.
3900 * A trailing newline is removed, so that append(f.readlines()) works.
3901 */
3902 p = memchr(str, '\n', len);
3903 if (p != NULL)
3904 {
3905 if (p == str + len - 1)
3906 --len;
3907 else
3908 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003909 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003910 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003911 return NULL;
3912 }
3913 }
3914
3915 /* Create a copy of the string, with internal nulls replaced by
3916 * newline characters, as is the vim convention.
3917 */
3918 save = (char *)alloc((unsigned)(len+1));
3919 if (save == NULL)
3920 {
3921 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003922 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003923 return NULL;
3924 }
3925
3926 for (i = 0; i < len; ++i)
3927 {
3928 if (str[i] == '\0')
3929 save[i] = '\n';
3930 else
3931 save[i] = str[i];
3932 }
3933
3934 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003935 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003936
3937 return save;
3938}
3939
3940/* Get a line from the specified buffer. The line number is
3941 * in Vim format (1-based). The line is returned as a Python
3942 * string object.
3943 */
3944 static PyObject *
3945GetBufferLine(buf_T *buf, PyInt n)
3946{
3947 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3948}
3949
3950
3951/* Get a list of lines from the specified buffer. The line numbers
3952 * are in Vim format (1-based). The range is from lo up to, but not
3953 * including, hi. The list is returned as a Python list of string objects.
3954 */
3955 static PyObject *
3956GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3957{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003958 PyInt i;
3959 PyInt n = hi - lo;
3960 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003961
3962 if (list == NULL)
3963 return NULL;
3964
3965 for (i = 0; i < n; ++i)
3966 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003967 PyObject *string = LineToString(
3968 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003969
3970 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003971 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972 {
3973 Py_DECREF(list);
3974 return NULL;
3975 }
3976
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003977 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003978 }
3979
3980 /* The ownership of the Python list is passed to the caller (ie,
3981 * the caller should Py_DECREF() the object when it is finished
3982 * with it).
3983 */
3984
3985 return list;
3986}
3987
3988/*
3989 * Check if deleting lines made the cursor position invalid.
3990 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3991 * deleted).
3992 */
3993 static void
3994py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3995{
3996 if (curwin->w_cursor.lnum >= lo)
3997 {
3998 /* Adjust the cursor position if it's in/after the changed
3999 * lines. */
4000 if (curwin->w_cursor.lnum >= hi)
4001 {
4002 curwin->w_cursor.lnum += extra;
4003 check_cursor_col();
4004 }
4005 else if (extra < 0)
4006 {
4007 curwin->w_cursor.lnum = lo;
4008 check_cursor();
4009 }
4010 else
4011 check_cursor_col();
4012 changed_cline_bef_curs();
4013 }
4014 invalidate_botline();
4015}
4016
Bram Moolenaar19e60942011-06-19 00:27:51 +02004017/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004018 * Find a window that contains "buf" and switch to it.
4019 * If there is no such window, use the current window and change "curbuf".
4020 * Caller must initialize save_curbuf to NULL.
4021 * restore_win_for_buf() MUST be called later!
4022 */
4023 static void
4024switch_to_win_for_buf(
4025 buf_T *buf,
4026 win_T **save_curwinp,
4027 tabpage_T **save_curtabp,
4028 buf_T **save_curbufp)
4029{
4030 win_T *wp;
4031 tabpage_T *tp;
4032
4033 if (find_win_for_buf(buf, &wp, &tp) == FAIL
4034 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4035 switch_buffer(save_curbufp, buf);
4036}
4037
4038 static void
4039restore_win_for_buf(
4040 win_T *save_curwin,
4041 tabpage_T *save_curtab,
4042 buf_T *save_curbuf)
4043{
4044 if (save_curbuf == NULL)
4045 restore_win(save_curwin, save_curtab, TRUE);
4046 else
4047 restore_buffer(save_curbuf);
4048}
4049
4050/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004051 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 * in Vim format (1-based). The replacement line is given as
4053 * a Python string object. The object is checked for validity
4054 * and correct format. Errors are returned as a value of FAIL.
4055 * The return value is OK on success.
4056 * If OK is returned and len_change is not NULL, *len_change
4057 * is set to the change in the buffer length.
4058 */
4059 static int
4060SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4061{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004062 buf_T *save_curbuf = NULL;
4063 win_T *save_curwin = NULL;
4064 tabpage_T *save_curtab = NULL;
4065
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004066 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067 * There are three cases:
4068 * 1. NULL, or None - this is a deletion.
4069 * 2. A string - this is a replacement.
4070 * 3. Anything else - this is an error.
4071 */
4072 if (line == Py_None || line == NULL)
4073 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004075 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004077 VimTryStart();
4078
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004080 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004082 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 else
4084 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004085 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004087 if (save_curbuf == NULL)
4088 /* Only adjust marks if we managed to switch to a window that
4089 * holds the buffer, otherwise line numbers will be invalid. */
4090 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 }
4092
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004093 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004095 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096 return FAIL;
4097
4098 if (len_change)
4099 *len_change = -1;
4100
4101 return OK;
4102 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004103 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004104 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004105 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106
4107 if (save == NULL)
4108 return FAIL;
4109
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004110 VimTryStart();
4111
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 /* We do not need to free "save" if ml_replace() consumes it. */
4113 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004114 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115
4116 if (u_savesub((linenr_T)n) == FAIL)
4117 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004118 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 vim_free(save);
4120 }
4121 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4122 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004123 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124 vim_free(save);
4125 }
4126 else
4127 changed_bytes((linenr_T)n, 0);
4128
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004129 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130
4131 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004132 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004133 check_cursor_col();
4134
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004135 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 return FAIL;
4137
4138 if (len_change)
4139 *len_change = 0;
4140
4141 return OK;
4142 }
4143 else
4144 {
4145 PyErr_BadArgument();
4146 return FAIL;
4147 }
4148}
4149
Bram Moolenaar19e60942011-06-19 00:27:51 +02004150/* Replace a range of lines in the specified buffer. The line numbers are in
4151 * Vim format (1-based). The range is from lo up to, but not including, hi.
4152 * The replacement lines are given as a Python list of string objects. The
4153 * list is checked for validity and correct format. Errors are returned as a
4154 * value of FAIL. The return value is OK on success.
4155 * If OK is returned and len_change is not NULL, *len_change
4156 * is set to the change in the buffer length.
4157 */
4158 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004159SetBufferLineList(
4160 buf_T *buf,
4161 PyInt lo,
4162 PyInt hi,
4163 PyObject *list,
4164 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004165{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004166 buf_T *save_curbuf = NULL;
4167 win_T *save_curwin = NULL;
4168 tabpage_T *save_curtab = NULL;
4169
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004170 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004171 * There are three cases:
4172 * 1. NULL, or None - this is a deletion.
4173 * 2. A list - this is a replacement.
4174 * 3. Anything else - this is an error.
4175 */
4176 if (list == Py_None || list == NULL)
4177 {
4178 PyInt i;
4179 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004180
4181 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004182 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004183 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004184
4185 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004186 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004187 else
4188 {
4189 for (i = 0; i < n; ++i)
4190 {
4191 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4192 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004193 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004194 break;
4195 }
4196 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004197 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004198 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004199 if (save_curbuf == NULL)
4200 /* Only adjust marks if we managed to switch to a window that
4201 * holds the buffer, otherwise line numbers will be invalid. */
4202 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004203 }
4204
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004205 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004206
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004207 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004208 return FAIL;
4209
4210 if (len_change)
4211 *len_change = -n;
4212
4213 return OK;
4214 }
4215 else if (PyList_Check(list))
4216 {
4217 PyInt i;
4218 PyInt new_len = PyList_Size(list);
4219 PyInt old_len = hi - lo;
4220 PyInt extra = 0; /* lines added to text, can be negative */
4221 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004222
4223 if (new_len == 0) /* avoid allocating zero bytes */
4224 array = NULL;
4225 else
4226 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004227 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004228 if (array == NULL)
4229 {
4230 PyErr_NoMemory();
4231 return FAIL;
4232 }
4233 }
4234
4235 for (i = 0; i < new_len; ++i)
4236 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004237 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004238
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004239 if (!(line = PyList_GetItem(list, i)) ||
4240 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004241 {
4242 while (i)
4243 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004244 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004245 return FAIL;
4246 }
4247 }
4248
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004249 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004250 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004251
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004252 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004253 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004254
4255 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004256 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004257
4258 /* If the size of the range is reducing (ie, new_len < old_len) we
4259 * need to delete some old_len. We do this at the start, by
4260 * repeatedly deleting line "lo".
4261 */
4262 if (!PyErr_Occurred())
4263 {
4264 for (i = 0; i < old_len - new_len; ++i)
4265 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4266 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004267 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004268 break;
4269 }
4270 extra -= i;
4271 }
4272
4273 /* For as long as possible, replace the existing old_len with the
4274 * new old_len. This is a more efficient operation, as it requires
4275 * less memory allocation and freeing.
4276 */
4277 if (!PyErr_Occurred())
4278 {
4279 for (i = 0; i < old_len && i < new_len; ++i)
4280 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4281 == FAIL)
4282 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004283 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004284 break;
4285 }
4286 }
4287 else
4288 i = 0;
4289
4290 /* Now we may need to insert the remaining new old_len. If we do, we
4291 * must free the strings as we finish with them (we can't pass the
4292 * responsibility to vim in this case).
4293 */
4294 if (!PyErr_Occurred())
4295 {
4296 while (i < new_len)
4297 {
4298 if (ml_append((linenr_T)(lo + i - 1),
4299 (char_u *)array[i], 0, FALSE) == FAIL)
4300 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004301 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004302 break;
4303 }
4304 vim_free(array[i]);
4305 ++i;
4306 ++extra;
4307 }
4308 }
4309
4310 /* Free any left-over old_len, as a result of an error */
4311 while (i < new_len)
4312 {
4313 vim_free(array[i]);
4314 ++i;
4315 }
4316
4317 /* Free the array of old_len. All of its contents have now
4318 * been dealt with (either freed, or the responsibility passed
4319 * to vim.
4320 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004321 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004322
4323 /* Adjust marks. Invalidate any which lie in the
4324 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004325 * Only adjust marks if we managed to switch to a window that holds
4326 * the buffer, otherwise line numbers will be invalid. */
4327 if (save_curbuf == NULL)
4328 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004329 (long)MAXLNUM, (long)extra);
4330 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4331
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004332 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004333 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4334
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004335 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004336 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004337
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004338 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004339 return FAIL;
4340
4341 if (len_change)
4342 *len_change = new_len - old_len;
4343
4344 return OK;
4345 }
4346 else
4347 {
4348 PyErr_BadArgument();
4349 return FAIL;
4350 }
4351}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004352
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004353/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004354 * The line number is in Vim format (1-based). The lines to be inserted are
4355 * given as a Python list of string objects or as a single string. The lines
4356 * to be added are checked for validity and correct format. Errors are
4357 * returned as a value of FAIL. The return value is OK on success.
4358 * If OK is returned and len_change is not NULL, *len_change
4359 * is set to the change in the buffer length.
4360 */
4361 static int
4362InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4363{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004364 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004365 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004366 tabpage_T *save_curtab = NULL;
4367
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004368 /* First of all, we check the type of the supplied Python object.
4369 * It must be a string or a list, or the call is in error.
4370 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004371 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004372 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004373 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004374
4375 if (str == NULL)
4376 return FAIL;
4377
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004378 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004379 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004380 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004381
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004382 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004383 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004384 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004385 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004386 else if (save_curbuf == NULL)
4387 /* Only adjust marks if we managed to switch to a window that
4388 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004389 appended_lines_mark((linenr_T)n, 1L);
4390
4391 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004392 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 update_screen(VALID);
4394
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004395 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004396 return FAIL;
4397
4398 if (len_change)
4399 *len_change = 1;
4400
4401 return OK;
4402 }
4403 else if (PyList_Check(lines))
4404 {
4405 PyInt i;
4406 PyInt size = PyList_Size(lines);
4407 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004408
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004409 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004410 if (array == NULL)
4411 {
4412 PyErr_NoMemory();
4413 return FAIL;
4414 }
4415
4416 for (i = 0; i < size; ++i)
4417 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004418 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004419
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004420 if (!(line = PyList_GetItem(lines, i)) ||
4421 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422 {
4423 while (i)
4424 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004425 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426 return FAIL;
4427 }
4428 }
4429
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004430 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004431 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004432 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004433
4434 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004435 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004436 else
4437 {
4438 for (i = 0; i < size; ++i)
4439 {
4440 if (ml_append((linenr_T)(n + i),
4441 (char_u *)array[i], 0, FALSE) == FAIL)
4442 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004443 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004444
4445 /* Free the rest of the lines */
4446 while (i < size)
4447 vim_free(array[i++]);
4448
4449 break;
4450 }
4451 vim_free(array[i]);
4452 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004453 if (i > 0 && save_curbuf == NULL)
4454 /* Only adjust marks if we managed to switch to a window that
4455 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004456 appended_lines_mark((linenr_T)n, (long)i);
4457 }
4458
4459 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004460 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004461 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004462 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004463
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004464 update_screen(VALID);
4465
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004466 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004467 return FAIL;
4468
4469 if (len_change)
4470 *len_change = size;
4471
4472 return OK;
4473 }
4474 else
4475 {
4476 PyErr_BadArgument();
4477 return FAIL;
4478 }
4479}
4480
4481/*
4482 * Common routines for buffers and line ranges
4483 * -------------------------------------------
4484 */
4485
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004486typedef struct
4487{
4488 PyObject_HEAD
4489 buf_T *buf;
4490} BufferObject;
4491
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004492 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004493CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004494{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004495 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004496 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004497 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004498 return -1;
4499 }
4500
4501 return 0;
4502}
4503
4504 static PyObject *
4505RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4506{
4507 if (CheckBuffer(self))
4508 return NULL;
4509
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004510 if (end == -1)
4511 end = self->buf->b_ml.ml_line_count;
4512
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004513 if (n < 0)
4514 n += end - start + 1;
4515
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004516 if (n < 0 || n > end - start)
4517 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004518 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004519 return NULL;
4520 }
4521
4522 return GetBufferLine(self->buf, n+start);
4523}
4524
4525 static PyObject *
4526RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4527{
4528 PyInt size;
4529
4530 if (CheckBuffer(self))
4531 return NULL;
4532
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004533 if (end == -1)
4534 end = self->buf->b_ml.ml_line_count;
4535
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004536 size = end - start + 1;
4537
4538 if (lo < 0)
4539 lo = 0;
4540 else if (lo > size)
4541 lo = size;
4542 if (hi < 0)
4543 hi = 0;
4544 if (hi < lo)
4545 hi = lo;
4546 else if (hi > size)
4547 hi = size;
4548
4549 return GetBufferLineList(self->buf, lo+start, hi+start);
4550}
4551
4552 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004553RBAsItem(
4554 BufferObject *self,
4555 PyInt n,
4556 PyObject *valObject,
4557 PyInt start,
4558 PyInt end,
4559 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004560{
4561 PyInt len_change;
4562
4563 if (CheckBuffer(self))
4564 return -1;
4565
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004566 if (end == -1)
4567 end = self->buf->b_ml.ml_line_count;
4568
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004569 if (n < 0)
4570 n += end - start + 1;
4571
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572 if (n < 0 || n > end - start)
4573 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004574 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004575 return -1;
4576 }
4577
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004578 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004579 return -1;
4580
4581 if (new_end)
4582 *new_end = end + len_change;
4583
4584 return 0;
4585}
4586
Bram Moolenaar19e60942011-06-19 00:27:51 +02004587 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004588RBAsSlice(
4589 BufferObject *self,
4590 PyInt lo,
4591 PyInt hi,
4592 PyObject *valObject,
4593 PyInt start,
4594 PyInt end,
4595 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004596{
4597 PyInt size;
4598 PyInt len_change;
4599
4600 /* Self must be a valid buffer */
4601 if (CheckBuffer(self))
4602 return -1;
4603
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004604 if (end == -1)
4605 end = self->buf->b_ml.ml_line_count;
4606
Bram Moolenaar19e60942011-06-19 00:27:51 +02004607 /* Sort out the slice range */
4608 size = end - start + 1;
4609
4610 if (lo < 0)
4611 lo = 0;
4612 else if (lo > size)
4613 lo = size;
4614 if (hi < 0)
4615 hi = 0;
4616 if (hi < lo)
4617 hi = lo;
4618 else if (hi > size)
4619 hi = size;
4620
4621 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004622 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004623 return -1;
4624
4625 if (new_end)
4626 *new_end = end + len_change;
4627
4628 return 0;
4629}
4630
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004631
4632 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004633RBAppend(
4634 BufferObject *self,
4635 PyObject *args,
4636 PyInt start,
4637 PyInt end,
4638 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004639{
4640 PyObject *lines;
4641 PyInt len_change;
4642 PyInt max;
4643 PyInt n;
4644
4645 if (CheckBuffer(self))
4646 return NULL;
4647
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004648 if (end == -1)
4649 end = self->buf->b_ml.ml_line_count;
4650
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004651 max = n = end - start + 1;
4652
4653 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4654 return NULL;
4655
4656 if (n < 0 || n > max)
4657 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004658 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004659 return NULL;
4660 }
4661
4662 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4663 return NULL;
4664
4665 if (new_end)
4666 *new_end = end + len_change;
4667
4668 Py_INCREF(Py_None);
4669 return Py_None;
4670}
4671
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004672/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004673 */
4674
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004675static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004676static PySequenceMethods RangeAsSeq;
4677static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004678
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004679typedef struct
4680{
4681 PyObject_HEAD
4682 BufferObject *buf;
4683 PyInt start;
4684 PyInt end;
4685} RangeObject;
4686
4687 static PyObject *
4688RangeNew(buf_T *buf, PyInt start, PyInt end)
4689{
4690 BufferObject *bufr;
4691 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004692 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693 if (self == NULL)
4694 return NULL;
4695
4696 bufr = (BufferObject *)BufferNew(buf);
4697 if (bufr == NULL)
4698 {
4699 Py_DECREF(self);
4700 return NULL;
4701 }
4702 Py_INCREF(bufr);
4703
4704 self->buf = bufr;
4705 self->start = start;
4706 self->end = end;
4707
4708 return (PyObject *)(self);
4709}
4710
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004711 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004712RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004713{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004714 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004715 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004716 PyObject_GC_Del((void *)(self));
4717}
4718
4719 static int
4720RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4721{
4722 Py_VISIT(((PyObject *)(self->buf)));
4723 return 0;
4724}
4725
4726 static int
4727RangeClear(RangeObject *self)
4728{
4729 Py_CLEAR(self->buf);
4730 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004731}
4732
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004733 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004734RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004735{
4736 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004737 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004738 return -1; /* ??? */
4739
Bram Moolenaard6e39182013-05-21 18:30:34 +02004740 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004741}
4742
4743 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004744RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004745{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004746 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004747}
4748
4749 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004750RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004751{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004752 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004753}
4754
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004755static char *RangeAttrs[] = {
4756 "start", "end",
4757 NULL
4758};
4759
4760 static PyObject *
4761RangeDir(PyObject *self)
4762{
4763 return ObjectDir(self, RangeAttrs);
4764}
4765
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004766 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004767RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004768{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004769 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004770}
4771
4772 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004773RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004774{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004775 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004776 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4777 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004778 else
4779 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004780 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004781
4782 if (name == NULL)
4783 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004784
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004785 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004786 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004787 }
4788}
4789
4790static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004791 /* name, function, calling, documentation */
4792 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004793 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4794 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004795};
4796
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004797static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004798static PySequenceMethods BufferAsSeq;
4799static PyMappingMethods BufferAsMapping;
4800
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004801 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004802BufferNew(buf_T *buf)
4803{
4804 /* We need to handle deletion of buffers underneath us.
4805 * If we add a "b_python*_ref" field to the buf_T structure,
4806 * then we can get at it in buf_freeall() in vim. We then
4807 * need to create only ONE Python object per buffer - if
4808 * we try to create a second, just INCREF the existing one
4809 * and return it. The (single) Python object referring to
4810 * the buffer is stored in "b_python*_ref".
4811 * Question: what to do on a buf_freeall(). We'll probably
4812 * have to either delete the Python object (DECREF it to
4813 * zero - a bad idea, as it leaves dangling refs!) or
4814 * set the buf_T * value to an invalid value (-1?), which
4815 * means we need checks in all access functions... Bah.
4816 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004817 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004818 * b_python_ref and b_python3_ref fields respectively.
4819 */
4820
4821 BufferObject *self;
4822
4823 if (BUF_PYTHON_REF(buf) != NULL)
4824 {
4825 self = BUF_PYTHON_REF(buf);
4826 Py_INCREF(self);
4827 }
4828 else
4829 {
4830 self = PyObject_NEW(BufferObject, &BufferType);
4831 if (self == NULL)
4832 return NULL;
4833 self->buf = buf;
4834 BUF_PYTHON_REF(buf) = self;
4835 }
4836
4837 return (PyObject *)(self);
4838}
4839
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004840 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004841BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004842{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004843 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4844 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004845
4846 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004847}
4848
Bram Moolenaar971db462013-05-12 18:44:48 +02004849 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004850BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004851{
4852 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004853 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004854 return -1; /* ??? */
4855
Bram Moolenaard6e39182013-05-21 18:30:34 +02004856 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004857}
4858
4859 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004860BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004861{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004862 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004863}
4864
4865 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004866BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004867{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004868 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004869}
4870
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004871static char *BufferAttrs[] = {
4872 "name", "number", "vars", "options", "valid",
4873 NULL
4874};
4875
4876 static PyObject *
4877BufferDir(PyObject *self)
4878{
4879 return ObjectDir(self, BufferAttrs);
4880}
4881
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004882 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004883BufferAttrValid(BufferObject *self, char *name)
4884{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004885 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004886
4887 if (strcmp(name, "valid") != 0)
4888 return NULL;
4889
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004890 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4891 Py_INCREF(ret);
4892 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004893}
4894
4895 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004896BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004897{
4898 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004899 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004900 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004901 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004902 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004903 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004904 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004905 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004906 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4907 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004908 else if (strcmp(name, "__members__") == 0)
4909 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004910 else
4911 return NULL;
4912}
4913
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004914 static int
4915BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4916{
4917 if (CheckBuffer(self))
4918 return -1;
4919
4920 if (strcmp(name, "name") == 0)
4921 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004922 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004923 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004924 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004925 PyObject *todecref;
4926
4927 if (!(val = StringToChars(valObject, &todecref)))
4928 return -1;
4929
4930 VimTryStart();
4931 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4932 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004933 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004934 aucmd_restbuf(&aco);
4935 Py_XDECREF(todecref);
4936 if (VimTryEnd())
4937 return -1;
4938
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004939 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004940 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004941 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004942 return -1;
4943 }
4944 return 0;
4945 }
4946 else
4947 {
4948 PyErr_SetString(PyExc_AttributeError, name);
4949 return -1;
4950 }
4951}
4952
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004953 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004954BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004955{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004956 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004957}
4958
4959 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004960BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004961{
4962 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004963 char_u *pmark;
4964 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004965 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004966 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004967
Bram Moolenaard6e39182013-05-21 18:30:34 +02004968 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004969 return NULL;
4970
Bram Moolenaar389a1792013-06-23 13:00:44 +02004971 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004972 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004973
Bram Moolenaar389a1792013-06-23 13:00:44 +02004974 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004975 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004976 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004977 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004978 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004979 return NULL;
4980 }
4981
4982 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004983
4984 Py_XDECREF(todecref);
4985
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004986 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004987 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004988 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004989 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004990 if (VimTryEnd())
4991 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992
4993 if (posp == NULL)
4994 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004995 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004996 return NULL;
4997 }
4998
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004999 if (posp->lnum <= 0)
5000 {
5001 /* Or raise an error? */
5002 Py_INCREF(Py_None);
5003 return Py_None;
5004 }
5005
5006 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5007}
5008
5009 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005010BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005011{
5012 PyInt start;
5013 PyInt end;
5014
Bram Moolenaard6e39182013-05-21 18:30:34 +02005015 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005016 return NULL;
5017
5018 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5019 return NULL;
5020
Bram Moolenaard6e39182013-05-21 18:30:34 +02005021 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005022}
5023
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005024 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005025BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005026{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005027 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005028 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005029 else
5030 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005031 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005032
5033 if (name == NULL)
5034 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005035
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005036 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005037 }
5038}
5039
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005040static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005041 /* name, function, calling, documentation */
5042 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005043 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005044 {"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 +02005045 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5046 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005047};
5048
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005049/*
5050 * Buffer list object - Implementation
5051 */
5052
5053static PyTypeObject BufMapType;
5054
5055typedef struct
5056{
5057 PyObject_HEAD
5058} BufMapObject;
5059
5060 static PyInt
5061BufMapLength(PyObject *self UNUSED)
5062{
5063 buf_T *b = firstbuf;
5064 PyInt n = 0;
5065
5066 while (b)
5067 {
5068 ++n;
5069 b = b->b_next;
5070 }
5071
5072 return n;
5073}
5074
5075 static PyObject *
5076BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5077{
5078 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005079 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005080
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005081 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005082 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005083
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005084 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005085
5086 if (b)
5087 return BufferNew(b);
5088 else
5089 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005090 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005091 return NULL;
5092 }
5093}
5094
5095 static void
5096BufMapIterDestruct(PyObject *buffer)
5097{
5098 /* Iteration was stopped before all buffers were processed */
5099 if (buffer)
5100 {
5101 Py_DECREF(buffer);
5102 }
5103}
5104
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005105 static int
5106BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5107{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005108 if (buffer)
5109 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005110 return 0;
5111}
5112
5113 static int
5114BufMapIterClear(PyObject **buffer)
5115{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005116 if (*buffer)
5117 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005118 return 0;
5119}
5120
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005121 static PyObject *
5122BufMapIterNext(PyObject **buffer)
5123{
5124 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005125 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005126
5127 if (!*buffer)
5128 return NULL;
5129
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005130 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005131
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005132 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005133 {
5134 *buffer = NULL;
5135 return NULL;
5136 }
5137
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005138 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005139 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005140 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005141 return NULL;
5142 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005143 /* Do not increment reference: we no longer hold it (decref), but whoever
5144 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005145 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005146}
5147
5148 static PyObject *
5149BufMapIter(PyObject *self UNUSED)
5150{
5151 PyObject *buffer;
5152
5153 buffer = BufferNew(firstbuf);
5154 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005155 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5156 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005157}
5158
5159static PyMappingMethods BufMapAsMapping = {
5160 (lenfunc) BufMapLength,
5161 (binaryfunc) BufMapItem,
5162 (objobjargproc) 0,
5163};
5164
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005165/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005166 */
5167
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005168static char *CurrentAttrs[] = {
5169 "buffer", "window", "line", "range", "tabpage",
5170 NULL
5171};
5172
5173 static PyObject *
5174CurrentDir(PyObject *self)
5175{
5176 return ObjectDir(self, CurrentAttrs);
5177}
5178
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005179 static PyObject *
5180CurrentGetattr(PyObject *self UNUSED, char *name)
5181{
5182 if (strcmp(name, "buffer") == 0)
5183 return (PyObject *)BufferNew(curbuf);
5184 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005185 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005186 else if (strcmp(name, "tabpage") == 0)
5187 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005188 else if (strcmp(name, "line") == 0)
5189 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5190 else if (strcmp(name, "range") == 0)
5191 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005192 else if (strcmp(name, "__members__") == 0)
5193 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005194 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005195#if PY_MAJOR_VERSION < 3
5196 return Py_FindMethod(WindowMethods, self, name);
5197#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005198 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005199#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005200}
5201
5202 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005203CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005204{
5205 if (strcmp(name, "line") == 0)
5206 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005207 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5208 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005209 return -1;
5210
5211 return 0;
5212 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005213 else if (strcmp(name, "buffer") == 0)
5214 {
5215 int count;
5216
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005217 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005218 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005219 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005220 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005221 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005222 return -1;
5223 }
5224
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005225 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005226 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005227 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005228
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005229 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005230 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5231 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005232 if (VimTryEnd())
5233 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005234 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005235 return -1;
5236 }
5237
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005238 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005239 }
5240 else if (strcmp(name, "window") == 0)
5241 {
5242 int count;
5243
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005244 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005245 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005246 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005247 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005248 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005249 return -1;
5250 }
5251
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005252 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005253 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005254 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005255
5256 if (!count)
5257 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005258 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005259 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005260 return -1;
5261 }
5262
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005263 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005264 win_goto(((WindowObject *)(valObject))->win);
5265 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005266 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005267 if (VimTryEnd())
5268 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005269 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005270 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005271 return -1;
5272 }
5273
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005274 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005275 }
5276 else if (strcmp(name, "tabpage") == 0)
5277 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005278 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005279 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005280 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005281 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005282 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005283 return -1;
5284 }
5285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005286 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005287 return -1;
5288
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005289 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005290 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5291 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005292 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005293 if (VimTryEnd())
5294 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005295 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005296 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005297 return -1;
5298 }
5299
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005300 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005301 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005302 else
5303 {
5304 PyErr_SetString(PyExc_AttributeError, name);
5305 return -1;
5306 }
5307}
5308
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005309static struct PyMethodDef CurrentMethods[] = {
5310 /* name, function, calling, documentation */
5311 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5312 { NULL, NULL, 0, NULL}
5313};
5314
Bram Moolenaardb913952012-06-29 12:54:53 +02005315 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005316init_range_cmd(exarg_T *eap)
5317{
5318 RangeStart = eap->line1;
5319 RangeEnd = eap->line2;
5320}
5321
5322 static void
5323init_range_eval(typval_T *rettv UNUSED)
5324{
5325 RangeStart = (PyInt) curwin->w_cursor.lnum;
5326 RangeEnd = RangeStart;
5327}
5328
5329 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005330run_cmd(const char *cmd, void *arg UNUSED
5331#ifdef PY_CAN_RECURSE
5332 , PyGILState_STATE *pygilstate UNUSED
5333#endif
5334 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005335{
Bram Moolenaar41009372013-07-01 22:03:04 +02005336 PyObject *run_ret;
5337 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5338 if (run_ret != NULL)
5339 {
5340 Py_DECREF(run_ret);
5341 }
5342 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5343 {
5344 EMSG2(_(e_py_systemexit), "python");
5345 PyErr_Clear();
5346 }
5347 else
5348 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005349}
5350
5351static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5352static int code_hdr_len = 30;
5353
5354 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005355run_do(const char *cmd, void *arg UNUSED
5356#ifdef PY_CAN_RECURSE
5357 , PyGILState_STATE *pygilstate
5358#endif
5359 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005360{
5361 PyInt lnum;
5362 size_t len;
5363 char *code;
5364 int status;
5365 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005366 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005367
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005368 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005369 {
5370 EMSG(_("cannot save undo information"));
5371 return;
5372 }
5373
5374 len = code_hdr_len + STRLEN(cmd);
5375 code = PyMem_New(char, len + 1);
5376 memcpy(code, code_hdr, code_hdr_len);
5377 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005378 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5379 status = -1;
5380 if (run_ret != NULL)
5381 {
5382 status = 0;
5383 Py_DECREF(run_ret);
5384 }
5385 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5386 {
5387 PyMem_Free(code);
5388 EMSG2(_(e_py_systemexit), "python");
5389 PyErr_Clear();
5390 return;
5391 }
5392 else
5393 PyErr_PrintEx(1);
5394
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005395 PyMem_Free(code);
5396
5397 if (status)
5398 {
5399 EMSG(_("failed to run the code"));
5400 return;
5401 }
5402
5403 status = 0;
5404 pymain = PyImport_AddModule("__main__");
5405 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005406#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005407 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005408#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005409
5410 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5411 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005412 PyObject *line;
5413 PyObject *linenr;
5414 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005415
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005416#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005417 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005418#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005419 if (!(line = GetBufferLine(curbuf, lnum)))
5420 goto err;
5421 if (!(linenr = PyInt_FromLong((long) lnum)))
5422 {
5423 Py_DECREF(line);
5424 goto err;
5425 }
5426 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5427 Py_DECREF(line);
5428 Py_DECREF(linenr);
5429 if (!ret)
5430 goto err;
5431
5432 if (ret != Py_None)
5433 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5434 goto err;
5435
5436 Py_XDECREF(ret);
5437 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005438#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005439 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005440#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005441 }
5442 goto out;
5443err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005444#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005445 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005446#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005447 PyErr_PrintEx(0);
5448 PythonIO_Flush();
5449 status = 1;
5450out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005451#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005452 if (!status)
5453 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005454#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005455 Py_DECREF(pyfunc);
5456 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5457 if (status)
5458 return;
5459 check_cursor();
5460 update_curbuf(NOT_VALID);
5461}
5462
5463 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005464run_eval(const char *cmd, typval_T *rettv
5465#ifdef PY_CAN_RECURSE
5466 , PyGILState_STATE *pygilstate UNUSED
5467#endif
5468 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005469{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005470 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005471
Bram Moolenaar41009372013-07-01 22:03:04 +02005472 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005473 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005474 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005475 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005476 {
5477 EMSG2(_(e_py_systemexit), "python");
5478 PyErr_Clear();
5479 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005480 else
5481 {
5482 if (PyErr_Occurred() && !msg_silent)
5483 PyErr_PrintEx(0);
5484 EMSG(_("E858: Eval did not return a valid python object"));
5485 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005486 }
5487 else
5488 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005489 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005490 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005491 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005492 }
5493 PyErr_Clear();
5494}
5495
5496 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005497set_ref_in_py(const int copyID)
5498{
5499 pylinkedlist_T *cur;
5500 dict_T *dd;
5501 list_T *ll;
5502
5503 if (lastdict != NULL)
5504 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5505 {
5506 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5507 if (dd->dv_copyID != copyID)
5508 {
5509 dd->dv_copyID = copyID;
5510 set_ref_in_ht(&dd->dv_hashtab, copyID);
5511 }
5512 }
5513
5514 if (lastlist != NULL)
5515 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5516 {
5517 ll = ((ListObject *) (cur->pll_obj))->list;
5518 if (ll->lv_copyID != copyID)
5519 {
5520 ll->lv_copyID = copyID;
5521 set_ref_in_list(ll, copyID);
5522 }
5523 }
5524}
5525
5526 static int
5527set_string_copy(char_u *str, typval_T *tv)
5528{
5529 tv->vval.v_string = vim_strsave(str);
5530 if (tv->vval.v_string == NULL)
5531 {
5532 PyErr_NoMemory();
5533 return -1;
5534 }
5535 return 0;
5536}
5537
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005538 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005539pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005540{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005541 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005542 char_u *key;
5543 dictitem_T *di;
5544 PyObject *keyObject;
5545 PyObject *valObject;
5546 Py_ssize_t iter = 0;
5547
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005548 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005549 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005550
5551 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005552 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005553
5554 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5555 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005556 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005557
Bram Moolenaara03e6312013-05-29 22:49:26 +02005558 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005559 {
5560 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005561 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005562 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005563
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005564 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005565 {
5566 dict_unref(dict);
5567 return -1;
5568 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005569
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005570 if (*key == NUL)
5571 {
5572 dict_unref(dict);
5573 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005574 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005575 return -1;
5576 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005577
5578 di = dictitem_alloc(key);
5579
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005580 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005581
5582 if (di == NULL)
5583 {
5584 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005585 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005586 return -1;
5587 }
5588 di->di_tv.v_lock = 0;
5589
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005590 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005591 {
5592 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005593 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005594 return -1;
5595 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005596
5597 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005598 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005599 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005600 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005601 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005602 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005603 return -1;
5604 }
5605 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005606
5607 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005608 return 0;
5609}
5610
5611 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005612pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005613{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005614 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005615 char_u *key;
5616 dictitem_T *di;
5617 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005618 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005619 PyObject *keyObject;
5620 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005621
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005622 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005623 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005624
5625 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005626 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005627
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005628 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005629 {
5630 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005631 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005632 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005633
5634 if (!(iterator = PyObject_GetIter(list)))
5635 {
5636 dict_unref(dict);
5637 Py_DECREF(list);
5638 return -1;
5639 }
5640 Py_DECREF(list);
5641
5642 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005643 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005644 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005645
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005646 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005647 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005648 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005649 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005650 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005651 return -1;
5652 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005653
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005654 if (*key == NUL)
5655 {
5656 Py_DECREF(keyObject);
5657 Py_DECREF(iterator);
5658 Py_XDECREF(todecref);
5659 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005660 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005661 return -1;
5662 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005663
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005664 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005665 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005666 Py_DECREF(keyObject);
5667 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005668 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005669 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005670 return -1;
5671 }
5672
5673 di = dictitem_alloc(key);
5674
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005675 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005676 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005677
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005678 if (di == NULL)
5679 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005680 Py_DECREF(iterator);
5681 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005682 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005683 PyErr_NoMemory();
5684 return -1;
5685 }
5686 di->di_tv.v_lock = 0;
5687
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005688 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005689 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005690 Py_DECREF(iterator);
5691 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005692 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005693 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005694 return -1;
5695 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005696
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005697 Py_DECREF(valObject);
5698
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005699 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005700 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005701 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005702 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005703 dictitem_free(di);
5704 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005705 return -1;
5706 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005707 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005708 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005709 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005710 return 0;
5711}
5712
5713 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005714pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005715{
5716 list_T *l;
5717
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005718 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005719 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005720
5721 tv->v_type = VAR_LIST;
5722 tv->vval.v_list = l;
5723
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005724 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005725 {
5726 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005727 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005728 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005729
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005730 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005731 return 0;
5732}
5733
Bram Moolenaardb913952012-06-29 12:54:53 +02005734typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5735
5736 static int
5737convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005738 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005739{
5740 PyObject *capsule;
5741 char hexBuf[sizeof(void *) * 2 + 3];
5742
5743 sprintf(hexBuf, "%p", obj);
5744
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005745# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005746 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005747# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005748 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005749# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005750 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005751 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005752# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005753 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005754# else
5755 capsule = PyCObject_FromVoidPtr(tv, NULL);
5756# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005757 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5758 {
5759 Py_DECREF(capsule);
5760 tv->v_type = VAR_UNKNOWN;
5761 return -1;
5762 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005763
5764 Py_DECREF(capsule);
5765
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005766 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005767 {
5768 tv->v_type = VAR_UNKNOWN;
5769 return -1;
5770 }
5771 /* As we are not using copy_tv which increments reference count we must
5772 * do it ourself. */
5773 switch(tv->v_type)
5774 {
5775 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5776 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5777 }
5778 }
5779 else
5780 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005781 typval_T *v;
5782
5783# ifdef PY_USE_CAPSULE
5784 v = PyCapsule_GetPointer(capsule, NULL);
5785# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005786 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005787# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005788 copy_tv(v, tv);
5789 }
5790 return 0;
5791}
5792
5793 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005794ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5795{
5796 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005797 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005798
5799 if (!(lookup_dict = PyDict_New()))
5800 return -1;
5801
5802 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5803 {
5804 tv->v_type = VAR_DICT;
5805 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5806 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005807 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005808 }
5809 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005810 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005811 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005812 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005813 else
5814 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005815 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005816 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005817 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005818 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005819 }
5820 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005821 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005822}
5823
5824 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005825ConvertFromPyObject(PyObject *obj, typval_T *tv)
5826{
5827 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005828 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005829
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005830 if (!(lookup_dict = PyDict_New()))
5831 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005832 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005833 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005834 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005835}
5836
5837 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005838_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005839{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005840 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005841 {
5842 tv->v_type = VAR_DICT;
5843 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5844 ++tv->vval.v_dict->dv_refcount;
5845 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005846 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005847 {
5848 tv->v_type = VAR_LIST;
5849 tv->vval.v_list = (((ListObject *)(obj))->list);
5850 ++tv->vval.v_list->lv_refcount;
5851 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005852 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005853 {
5854 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5855 return -1;
5856
5857 tv->v_type = VAR_FUNC;
5858 func_ref(tv->vval.v_string);
5859 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005860 else if (PyBytes_Check(obj))
5861 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005862 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005863
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005864 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005865 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005866 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005867 return -1;
5868
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005869 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005870 return -1;
5871
5872 tv->v_type = VAR_STRING;
5873 }
5874 else if (PyUnicode_Check(obj))
5875 {
5876 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005877 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005878
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005879 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005880 if (bytes == NULL)
5881 return -1;
5882
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005883 if(PyBytes_AsStringAndSize(bytes, (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))
Bram Moolenaardb913952012-06-29 12:54:53 +02005889 {
5890 Py_XDECREF(bytes);
5891 return -1;
5892 }
5893 Py_XDECREF(bytes);
5894
5895 tv->v_type = VAR_STRING;
5896 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005897#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005898 else if (PyInt_Check(obj))
5899 {
5900 tv->v_type = VAR_NUMBER;
5901 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005902 if (PyErr_Occurred())
5903 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005904 }
5905#endif
5906 else if (PyLong_Check(obj))
5907 {
5908 tv->v_type = VAR_NUMBER;
5909 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005910 if (PyErr_Occurred())
5911 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005912 }
5913 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005914 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005915#ifdef FEAT_FLOAT
5916 else if (PyFloat_Check(obj))
5917 {
5918 tv->v_type = VAR_FLOAT;
5919 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5920 }
5921#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005922 else if (PyObject_HasAttrString(obj, "keys"))
5923 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005924 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005925 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005926 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005927 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005928 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005929 else if (PyNumber_Check(obj))
5930 {
5931 PyObject *num;
5932
5933 if (!(num = PyNumber_Long(obj)))
5934 return -1;
5935
5936 tv->v_type = VAR_NUMBER;
5937 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5938
5939 Py_DECREF(num);
5940 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005941 else
5942 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005943 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005944 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005945 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005946 return -1;
5947 }
5948 return 0;
5949}
5950
5951 static PyObject *
5952ConvertToPyObject(typval_T *tv)
5953{
5954 if (tv == NULL)
5955 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005956 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005957 return NULL;
5958 }
5959 switch (tv->v_type)
5960 {
5961 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005962 return PyBytes_FromString(tv->vval.v_string == NULL
5963 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005964 case VAR_NUMBER:
5965 return PyLong_FromLong((long) tv->vval.v_number);
5966#ifdef FEAT_FLOAT
5967 case VAR_FLOAT:
5968 return PyFloat_FromDouble((double) tv->vval.v_float);
5969#endif
5970 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005971 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005972 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005973 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005974 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005975 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005976 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005977 case VAR_UNKNOWN:
5978 Py_INCREF(Py_None);
5979 return Py_None;
5980 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005981 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005982 return NULL;
5983 }
5984}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005985
5986typedef struct
5987{
5988 PyObject_HEAD
5989} CurrentObject;
5990static PyTypeObject CurrentType;
5991
5992 static void
5993init_structs(void)
5994{
5995 vim_memset(&OutputType, 0, sizeof(OutputType));
5996 OutputType.tp_name = "vim.message";
5997 OutputType.tp_basicsize = sizeof(OutputObject);
5998 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5999 OutputType.tp_doc = "vim message object";
6000 OutputType.tp_methods = OutputMethods;
6001#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006002 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6003 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006004 OutputType.tp_alloc = call_PyType_GenericAlloc;
6005 OutputType.tp_new = call_PyType_GenericNew;
6006 OutputType.tp_free = call_PyObject_Free;
6007#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006008 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6009 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006010#endif
6011
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006012 vim_memset(&IterType, 0, sizeof(IterType));
6013 IterType.tp_name = "vim.iter";
6014 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006015 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006016 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006017 IterType.tp_iter = (getiterfunc)IterIter;
6018 IterType.tp_iternext = (iternextfunc)IterNext;
6019 IterType.tp_dealloc = (destructor)IterDestructor;
6020 IterType.tp_traverse = (traverseproc)IterTraverse;
6021 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006022
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006023 vim_memset(&BufferType, 0, sizeof(BufferType));
6024 BufferType.tp_name = "vim.buffer";
6025 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006026 BufferType.tp_dealloc = (destructor)BufferDestructor;
6027 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006028 BufferType.tp_as_sequence = &BufferAsSeq;
6029 BufferType.tp_as_mapping = &BufferAsMapping;
6030 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6031 BufferType.tp_doc = "vim buffer object";
6032 BufferType.tp_methods = BufferMethods;
6033#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006034 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006035 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006036 BufferType.tp_alloc = call_PyType_GenericAlloc;
6037 BufferType.tp_new = call_PyType_GenericNew;
6038 BufferType.tp_free = call_PyObject_Free;
6039#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006040 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006041 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006042#endif
6043
6044 vim_memset(&WindowType, 0, sizeof(WindowType));
6045 WindowType.tp_name = "vim.window";
6046 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006047 WindowType.tp_dealloc = (destructor)WindowDestructor;
6048 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006049 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006050 WindowType.tp_doc = "vim Window object";
6051 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006052 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6053 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006054#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006055 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6056 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006057 WindowType.tp_alloc = call_PyType_GenericAlloc;
6058 WindowType.tp_new = call_PyType_GenericNew;
6059 WindowType.tp_free = call_PyObject_Free;
6060#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006061 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6062 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006063#endif
6064
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006065 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6066 TabPageType.tp_name = "vim.tabpage";
6067 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006068 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6069 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006070 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6071 TabPageType.tp_doc = "vim tab page object";
6072 TabPageType.tp_methods = TabPageMethods;
6073#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006074 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006075 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6076 TabPageType.tp_new = call_PyType_GenericNew;
6077 TabPageType.tp_free = call_PyObject_Free;
6078#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006079 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006080#endif
6081
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006082 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6083 BufMapType.tp_name = "vim.bufferlist";
6084 BufMapType.tp_basicsize = sizeof(BufMapObject);
6085 BufMapType.tp_as_mapping = &BufMapAsMapping;
6086 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006087 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006088 BufferType.tp_doc = "vim buffer list";
6089
6090 vim_memset(&WinListType, 0, sizeof(WinListType));
6091 WinListType.tp_name = "vim.windowlist";
6092 WinListType.tp_basicsize = sizeof(WinListType);
6093 WinListType.tp_as_sequence = &WinListAsSeq;
6094 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6095 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006096 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006097
6098 vim_memset(&TabListType, 0, sizeof(TabListType));
6099 TabListType.tp_name = "vim.tabpagelist";
6100 TabListType.tp_basicsize = sizeof(TabListType);
6101 TabListType.tp_as_sequence = &TabListAsSeq;
6102 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6103 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006104
6105 vim_memset(&RangeType, 0, sizeof(RangeType));
6106 RangeType.tp_name = "vim.range";
6107 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006108 RangeType.tp_dealloc = (destructor)RangeDestructor;
6109 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006110 RangeType.tp_as_sequence = &RangeAsSeq;
6111 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006112 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006113 RangeType.tp_doc = "vim Range object";
6114 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006115 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6116 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006117#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006118 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006119 RangeType.tp_alloc = call_PyType_GenericAlloc;
6120 RangeType.tp_new = call_PyType_GenericNew;
6121 RangeType.tp_free = call_PyObject_Free;
6122#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006123 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006124#endif
6125
6126 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6127 CurrentType.tp_name = "vim.currentdata";
6128 CurrentType.tp_basicsize = sizeof(CurrentObject);
6129 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6130 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006131 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006132#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006133 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6134 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006135#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006136 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6137 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006138#endif
6139
6140 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6141 DictionaryType.tp_name = "vim.dictionary";
6142 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006143 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006144 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006145 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006146 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006147 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6148 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006149 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6150 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6151 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006152#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006153 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6154 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006155#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006156 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6157 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006158#endif
6159
6160 vim_memset(&ListType, 0, sizeof(ListType));
6161 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006162 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006163 ListType.tp_basicsize = sizeof(ListObject);
6164 ListType.tp_as_sequence = &ListAsSeq;
6165 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006166 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006167 ListType.tp_doc = "list pushing modifications to vim structure";
6168 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006169 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006170 ListType.tp_new = (newfunc)ListConstructor;
6171 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006172#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006173 ListType.tp_getattro = (getattrofunc)ListGetattro;
6174 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006175#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006176 ListType.tp_getattr = (getattrfunc)ListGetattr;
6177 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006178#endif
6179
6180 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006181 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006182 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006183 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6184 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006185 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006186 FunctionType.tp_doc = "object that calls vim function";
6187 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006188 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006189 FunctionType.tp_new = (newfunc)FunctionConstructor;
6190 FunctionType.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 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006193#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006194 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006195#endif
6196
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006197 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6198 OptionsType.tp_name = "vim.options";
6199 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006200 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006201 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006202 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006203 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006204 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006205 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6206 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6207 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006208
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006209 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6210 LoaderType.tp_name = "vim.Loader";
6211 LoaderType.tp_basicsize = sizeof(LoaderObject);
6212 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6213 LoaderType.tp_doc = "vim message object";
6214 LoaderType.tp_methods = LoaderMethods;
6215 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6216
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006217#if PY_MAJOR_VERSION >= 3
6218 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6219 vimmodule.m_name = "vim";
6220 vimmodule.m_doc = "Vim Python interface\n";
6221 vimmodule.m_size = -1;
6222 vimmodule.m_methods = VimMethods;
6223#endif
6224}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006225
6226#define PYTYPE_READY(type) \
6227 if (PyType_Ready(&type)) \
6228 return -1;
6229
6230 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006231init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006232{
6233 PYTYPE_READY(IterType);
6234 PYTYPE_READY(BufferType);
6235 PYTYPE_READY(RangeType);
6236 PYTYPE_READY(WindowType);
6237 PYTYPE_READY(TabPageType);
6238 PYTYPE_READY(BufMapType);
6239 PYTYPE_READY(WinListType);
6240 PYTYPE_READY(TabListType);
6241 PYTYPE_READY(CurrentType);
6242 PYTYPE_READY(DictionaryType);
6243 PYTYPE_READY(ListType);
6244 PYTYPE_READY(FunctionType);
6245 PYTYPE_READY(OptionsType);
6246 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006247 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006248 return 0;
6249}
6250
6251 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006252init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006253{
6254 PyObject *path;
6255 PyObject *path_hook;
6256 PyObject *path_hooks;
6257
6258 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6259 return -1;
6260
6261 if (!(path_hooks = PySys_GetObject("path_hooks")))
6262 {
6263 PyErr_Clear();
6264 path_hooks = PyList_New(1);
6265 PyList_SET_ITEM(path_hooks, 0, path_hook);
6266 if (PySys_SetObject("path_hooks", path_hooks))
6267 {
6268 Py_DECREF(path_hooks);
6269 return -1;
6270 }
6271 Py_DECREF(path_hooks);
6272 }
6273 else if (PyList_Check(path_hooks))
6274 {
6275 if (PyList_Append(path_hooks, path_hook))
6276 {
6277 Py_DECREF(path_hook);
6278 return -1;
6279 }
6280 Py_DECREF(path_hook);
6281 }
6282 else
6283 {
6284 VimTryStart();
6285 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6286 "You should now do the following:\n"
6287 "- append vim.path_hook to sys.path_hooks\n"
6288 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6289 VimTryEnd(); /* Discard the error */
6290 Py_DECREF(path_hook);
6291 return 0;
6292 }
6293
6294 if (!(path = PySys_GetObject("path")))
6295 {
6296 PyErr_Clear();
6297 path = PyList_New(1);
6298 Py_INCREF(vim_special_path_object);
6299 PyList_SET_ITEM(path, 0, vim_special_path_object);
6300 if (PySys_SetObject("path", path))
6301 {
6302 Py_DECREF(path);
6303 return -1;
6304 }
6305 Py_DECREF(path);
6306 }
6307 else if (PyList_Check(path))
6308 {
6309 if (PyList_Append(path, vim_special_path_object))
6310 return -1;
6311 }
6312 else
6313 {
6314 VimTryStart();
6315 EMSG(_("Failed to set path: sys.path is not a list\n"
6316 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6317 VimTryEnd(); /* Discard the error */
6318 }
6319
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006320 return 0;
6321}
6322
6323static BufMapObject TheBufferMap =
6324{
6325 PyObject_HEAD_INIT(&BufMapType)
6326};
6327
6328static WinListObject TheWindowList =
6329{
6330 PyObject_HEAD_INIT(&WinListType)
6331 NULL
6332};
6333
6334static CurrentObject TheCurrent =
6335{
6336 PyObject_HEAD_INIT(&CurrentType)
6337};
6338
6339static TabListObject TheTabPageList =
6340{
6341 PyObject_HEAD_INIT(&TabListType)
6342};
6343
6344static struct numeric_constant {
6345 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006346 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006347} numeric_constants[] = {
6348 {"VAR_LOCKED", VAR_LOCKED},
6349 {"VAR_FIXED", VAR_FIXED},
6350 {"VAR_SCOPE", VAR_SCOPE},
6351 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6352};
6353
6354static struct object_constant {
6355 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006356 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006357} object_constants[] = {
6358 {"buffers", (PyObject *)(void *)&TheBufferMap},
6359 {"windows", (PyObject *)(void *)&TheWindowList},
6360 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6361 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006362
6363 {"Buffer", (PyObject *)&BufferType},
6364 {"Range", (PyObject *)&RangeType},
6365 {"Window", (PyObject *)&WindowType},
6366 {"TabPage", (PyObject *)&TabPageType},
6367 {"Dictionary", (PyObject *)&DictionaryType},
6368 {"List", (PyObject *)&ListType},
6369 {"Function", (PyObject *)&FunctionType},
6370 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006371 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006372};
6373
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006374#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006375 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006376 return -1;
6377
6378#define ADD_CHECKED_OBJECT(m, name, obj) \
6379 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006380 PyObject *valObject = obj; \
6381 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006382 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006383 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006384 }
6385
6386 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006387populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006388{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006389 int i;
6390 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006391 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006392 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006393
6394 for (i = 0; i < (int)(sizeof(numeric_constants)
6395 / sizeof(struct numeric_constant));
6396 ++i)
6397 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006398 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006399
6400 for (i = 0; i < (int)(sizeof(object_constants)
6401 / sizeof(struct object_constant));
6402 ++i)
6403 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006404 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006405
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006406 valObject = object_constants[i].valObject;
6407 Py_INCREF(valObject);
6408 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006409 }
6410
6411 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6412 return -1;
6413 ADD_OBJECT(m, "error", VimError);
6414
Bram Moolenaara9922d62013-05-30 13:01:18 +02006415 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6416 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006417 ADD_CHECKED_OBJECT(m, "options",
6418 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006419
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006420 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006421 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006422 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006423
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006424 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006425 return -1;
6426 ADD_OBJECT(m, "_getcwd", py_getcwd)
6427
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006428 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006429 return -1;
6430 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006431 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006432 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006433 if (PyObject_SetAttrString(other_module, "chdir", attr))
6434 {
6435 Py_DECREF(attr);
6436 return -1;
6437 }
6438 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006439
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006440 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006441 {
6442 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006443 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006444 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006445 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6446 {
6447 Py_DECREF(attr);
6448 return -1;
6449 }
6450 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006451 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006452 else
6453 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006454
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006455 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6456 return -1;
6457
6458 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6459
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006460 if (!(imp = PyImport_ImportModule("imp")))
6461 return -1;
6462
6463 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6464 {
6465 Py_DECREF(imp);
6466 return -1;
6467 }
6468
6469 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6470 {
6471 Py_DECREF(py_find_module);
6472 Py_DECREF(imp);
6473 return -1;
6474 }
6475
6476 Py_DECREF(imp);
6477
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006478 ADD_OBJECT(m, "_find_module", py_find_module);
6479 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006480
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006481 return 0;
6482}