blob: 5319466e57a723b6d4f73e4bd9f3c9d16011e2e3 [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 Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020021# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020022#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020027static const char *vim_special_path = "_vim_path_";
28
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020029#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020030#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020031#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaarc476e522013-06-23 13:46:40 +020032#define PyErr_FORMAT(exc, str, tail) PyErr_Format(exc, _(str), tail)
33#define PyErr_VIM_FORMAT(str, tail) PyErr_FORMAT(VimError, str, tail)
34
35#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020039#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
40 "empty keys are not allowed")
Bram Moolenaarc476e522013-06-23 13:46:40 +020041#define RAISE_LOCKED(type) PyErr_SET_VIM(_(type " is locked"))
42#define RAISE_LOCKED_DICTIONARY RAISE_LOCKED("dictionary")
43#define RAISE_LOCKED_LIST RAISE_LOCKED("list")
44#define RAISE_UNDO_FAIL PyErr_SET_VIM("cannot save undo information")
45#define RAISE_LINE_FAIL(act) PyErr_SET_VIM("cannot " act " line")
46#define RAISE_KEY_ADD_FAIL(key) \
47 PyErr_VIM_FORMAT("failed to add key '%s' to dictionary", key)
48#define RAISE_INVALID_INDEX_TYPE(idx) \
49 PyErr_FORMAT(PyExc_TypeError, "index must be int or slice, not %s", \
50 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020051
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020052#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
53#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020054#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020055
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020056typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020057typedef void (*runner)(const char *, void *
58#ifdef PY_CAN_RECURSE
59 , PyGILState_STATE *
60#endif
61 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020062
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020063static int ConvertFromPyObject(PyObject *, typval_T *);
64static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020065static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020066static PyObject *WindowNew(win_T *, tabpage_T *);
67static PyObject *BufferNew (buf_T *);
68static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020069
70static PyInt RangeStart;
71static PyInt RangeEnd;
72
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020073static PyObject *globals;
74
Bram Moolenaarf4258302013-06-02 18:20:17 +020075static PyObject *py_chdir;
76static PyObject *py_fchdir;
77static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020078static PyObject *vim_module;
79static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020080
Bram Moolenaar81c40c52013-06-12 14:41:04 +020081static PyObject *py_find_module;
82static PyObject *py_load_module;
83
84static PyObject *VimError;
85
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020086/*
87 * obtain a lock on the Vim data structures
88 */
89 static void
90Python_Lock_Vim(void)
91{
92}
93
94/*
95 * release a lock on the Vim data structures
96 */
97 static void
98Python_Release_Vim(void)
99{
100}
101
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200102/*
103 * The "todecref" argument holds a pointer to PyObject * that must be
104 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
105 * was needed to generate returned value is object.
106 *
107 * Use Py_XDECREF to decrement reference count.
108 */
109 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200110StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200111{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200112 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200113
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200114 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200115 {
116
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200117 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
118 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200119 return NULL;
120
121 *todecref = NULL;
122 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200124 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200125 PyObject *bytes;
126
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200127 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 return NULL;
129
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200130 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
131 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200132 {
133 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200136
137 *todecref = bytes;
138 }
139 else
140 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200141 PyErr_FORMAT(PyExc_TypeError,
142#if PY_MAJOR_VERSION < 3
143 "expected str() or unicode() instance, but got %s"
144#else
145 "expected bytes() or str() instance, but got %s"
146#endif
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200147 , Py_TYPE_NAME(obj));
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200148 return NULL;
149 }
150
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200151 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200152}
153
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200154#define NUMBER_LONG 1
155#define NUMBER_INT 2
156#define NUMBER_NATURAL 4
157#define NUMBER_UNSIGNED 8
158
159 static int
160NumberToLong(PyObject *obj, long *result, int flags)
161{
162#if PY_MAJOR_VERSION < 3
163 if (PyInt_Check(obj))
164 {
165 *result = PyInt_AsLong(obj);
166 if (PyErr_Occurred())
167 return -1;
168 }
169 else
170#endif
171 if (PyLong_Check(obj))
172 {
173 *result = PyLong_AsLong(obj);
174 if (PyErr_Occurred())
175 return -1;
176 }
177 else if (PyNumber_Check(obj))
178 {
179 PyObject *num;
180
181 if (!(num = PyNumber_Long(obj)))
182 return -1;
183
184 *result = PyLong_AsLong(num);
185
186 Py_DECREF(num);
187
188 if (PyErr_Occurred())
189 return -1;
190 }
191 else
192 {
193 PyErr_FORMAT(PyExc_TypeError,
194#if PY_MAJOR_VERSION < 3
195 "expected int(), long() or something supporting "
196 "coercing to long(), but got %s"
197#else
198 "expected int() or something supporting coercing to int(), "
199 "but got %s"
200#endif
201 , Py_TYPE_NAME(obj));
202 return -1;
203 }
204
205 if (flags & NUMBER_INT)
206 {
207 if (*result > INT_MAX)
208 {
209 PyErr_SET_STRING(PyExc_OverflowError,
210 "value is too large to fit into C int type");
211 return -1;
212 }
213 else if (*result < INT_MIN)
214 {
215 PyErr_SET_STRING(PyExc_OverflowError,
216 "value is too small to fit into C int type");
217 return -1;
218 }
219 }
220
221 if (flags & NUMBER_NATURAL)
222 {
223 if (*result <= 0)
224 {
225 PyErr_SET_STRING(PyExc_ValueError,
226 "number must be greater then zero");
227 return -1;
228 }
229 }
230 else if (flags & NUMBER_UNSIGNED)
231 {
232 if (*result < 0)
233 {
234 PyErr_SET_STRING(PyExc_ValueError,
235 "number must be greater or equal to zero");
236 return -1;
237 }
238 }
239
240 return 0;
241}
242
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200243 static int
244add_string(PyObject *list, char *s)
245{
246 PyObject *string;
247
248 if (!(string = PyString_FromString(s)))
249 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200250
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200251 if (PyList_Append(list, string))
252 {
253 Py_DECREF(string);
254 return -1;
255 }
256
257 Py_DECREF(string);
258 return 0;
259}
260
261 static PyObject *
262ObjectDir(PyObject *self, char **attributes)
263{
264 PyMethodDef *method;
265 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200266 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200267
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200268 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200269 return NULL;
270
271 if (self)
272 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200273 if (add_string(ret, (char *) method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200274 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200275 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200276 return NULL;
277 }
278
279 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200282 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 return NULL;
284 }
285
286#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200287 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 return NULL;
291 }
292#endif
293
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295}
296
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200297/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200298 */
299
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200300/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200301typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200302
303static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200304
305typedef struct
306{
307 PyObject_HEAD
308 long softspace;
309 long error;
310} OutputObject;
311
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200312static char *OutputAttrs[] = {
313 "softspace",
314 NULL
315};
316
317 static PyObject *
318OutputDir(PyObject *self)
319{
320 return ObjectDir(self, OutputAttrs);
321}
322
Bram Moolenaar77045652012-09-21 13:46:06 +0200323 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200324OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200325{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200326 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200327 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200328 PyErr_SET_STRING(PyExc_AttributeError,
329 "can't delete OutputObject attributes");
Bram Moolenaar77045652012-09-21 13:46:06 +0200330 return -1;
331 }
332
333 if (strcmp(name, "softspace") == 0)
334 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200335 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 return 0;
338 }
339
Bram Moolenaarc476e522013-06-23 13:46:40 +0200340 PyErr_FORMAT(PyExc_AttributeError, "invalid attribute: %s", name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 return -1;
342}
343
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200344/* Buffer IO, we write one whole line at a time. */
345static garray_T io_ga = {0, 0, 1, 80, NULL};
346static writefn old_fn = NULL;
347
348 static void
349PythonIO_Flush(void)
350{
351 if (old_fn != NULL && io_ga.ga_len > 0)
352 {
353 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
354 old_fn((char_u *)io_ga.ga_data);
355 }
356 io_ga.ga_len = 0;
357}
358
359 static void
360writer(writefn fn, char_u *str, PyInt n)
361{
362 char_u *ptr;
363
364 /* Flush when switching output function. */
365 if (fn != old_fn)
366 PythonIO_Flush();
367 old_fn = fn;
368
369 /* Write each NL separated line. Text after the last NL is kept for
370 * writing later. */
371 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
372 {
373 PyInt len = ptr - str;
374
375 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
376 break;
377
378 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
379 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
380 fn((char_u *)io_ga.ga_data);
381 str = ptr + 1;
382 n -= len + 1;
383 io_ga.ga_len = 0;
384 }
385
386 /* Put the remaining text into io_ga for later printing. */
387 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
388 {
389 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
390 io_ga.ga_len += (int)n;
391 }
392}
393
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200394 static int
395write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200396{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200397 Py_ssize_t len = 0;
398 char *str = NULL;
399 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200400
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200401 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
402 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200403
404 Py_BEGIN_ALLOW_THREADS
405 Python_Lock_Vim();
406 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
407 Python_Release_Vim();
408 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200409 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 return 0;
412}
413
414 static PyObject *
415OutputWrite(OutputObject *self, PyObject *string)
416{
417 if (write_output(self, string))
418 return NULL;
419
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200420 Py_INCREF(Py_None);
421 return Py_None;
422}
423
424 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200426{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200427 PyObject *iterator;
428 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200429
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200430 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200431 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200432
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200433 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200435 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200436 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200437 Py_DECREF(iterator);
438 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439 return NULL;
440 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442 }
443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 Py_DECREF(iterator);
445
446 /* Iterator may have finished due to an exception */
447 if (PyErr_Occurred())
448 return NULL;
449
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450 Py_INCREF(Py_None);
451 return Py_None;
452}
453
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100454 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200455OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100456{
457 /* do nothing */
458 Py_INCREF(Py_None);
459 return Py_None;
460}
461
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200462/***************/
463
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200464static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200465 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200466 {"write", (PyCFunction)OutputWrite, METH_O, ""},
467 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200468 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200469 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200470 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200471};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200472
473static OutputObject Output =
474{
475 PyObject_HEAD_INIT(&OutputType)
476 0,
477 0
478};
479
480static OutputObject Error =
481{
482 PyObject_HEAD_INIT(&OutputType)
483 0,
484 1
485};
486
487 static int
488PythonIO_Init_io(void)
489{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200490 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
491 return -1;
492 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
493 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494
495 if (PyErr_Occurred())
496 {
497 EMSG(_("E264: Python: Error initialising I/O objects"));
498 return -1;
499 }
500
501 return 0;
502}
503
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200504typedef struct
505{
506 PyObject_HEAD
507 PyObject *module;
508} LoaderObject;
509static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200510
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200511 static void
512LoaderDestructor(LoaderObject *self)
513{
514 Py_DECREF(self->module);
515 DESTRUCTOR_FINISH(self);
516}
517
518 static PyObject *
519LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
520{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200521 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200522
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200523 Py_INCREF(ret);
524 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200525}
526
527static struct PyMethodDef LoaderMethods[] = {
528 /* name, function, calling, doc */
529 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
530 { NULL, NULL, 0, NULL}
531};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200532
533/* Check to see whether a Vim error has been reported, or a keyboard
534 * interrupt has been detected.
535 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200536
537 static void
538VimTryStart(void)
539{
540 ++trylevel;
541}
542
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200543 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200544VimTryEnd(void)
545{
546 --trylevel;
547 if (got_int)
548 {
549 PyErr_SetNone(PyExc_KeyboardInterrupt);
550 return 1;
551 }
552 else if (!did_throw)
553 return 0;
554 else if (PyErr_Occurred())
555 return 1;
556 else
557 {
558 PyErr_SetVim((char *) current_exception->value);
559 discard_current_exception();
560 return 1;
561 }
562}
563
564 static int
565VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200566{
567 if (got_int)
568 {
569 PyErr_SetNone(PyExc_KeyboardInterrupt);
570 return 1;
571 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200572 return 0;
573}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574
575/* Vim module - Implementation
576 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200577
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200579VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200581 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200582 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200583 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200584
Bram Moolenaar389a1792013-06-23 13:00:44 +0200585 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586 return NULL;
587
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200588 Py_BEGIN_ALLOW_THREADS
589 Python_Lock_Vim();
590
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200591 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200592 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593 update_screen(VALID);
594
595 Python_Release_Vim();
596 Py_END_ALLOW_THREADS
597
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200598 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200599 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200600 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200601 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200602
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200603 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200604 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200605 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200606}
607
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200608/*
609 * Function to translate a typval_T into a PyObject; this will recursively
610 * translate lists/dictionaries into their Python equivalents.
611 *
612 * The depth parameter is to avoid infinite recursion, set it to 1 when
613 * you call VimToPython.
614 */
615 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200616VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200618 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200619 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200620 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200621
622 /* Avoid infinite recursion */
623 if (depth > 100)
624 {
625 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200626 ret = Py_None;
627 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628 }
629
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200630 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631 * then and we can use it again. */
632 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
633 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
634 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200635 sprintf(ptrBuf, "%p",
636 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
637 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200638
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200639 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200640 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200641 Py_INCREF(ret);
642 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200643 }
644 }
645
646 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200647 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200648 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649 else if (our_tv->v_type == VAR_NUMBER)
650 {
651 char buf[NUMBUFLEN];
652
653 /* For backwards compatibility numbers are stored as strings. */
654 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200655 ret = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656 }
657# ifdef FEAT_FLOAT
658 else if (our_tv->v_type == VAR_FLOAT)
659 {
660 char buf[NUMBUFLEN];
661
662 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200663 ret = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200664 }
665# endif
666 else if (our_tv->v_type == VAR_LIST)
667 {
668 list_T *list = our_tv->vval.v_list;
669 listitem_T *curr;
670
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200671 if (list == NULL)
672 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200675 return NULL;
676
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200677 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200678 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200679 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200680 return NULL;
681 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200682
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200683 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
684 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200685 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200686 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200687 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200688 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200690 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200691 {
692 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200693 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200694 return NULL;
695 }
696 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 }
698 }
699 else if (our_tv->v_type == VAR_DICT)
700 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200702 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
703 long_u todo = ht->ht_used;
704 hashitem_T *hi;
705 dictitem_T *di;
706 if (our_tv->vval.v_dict == NULL)
707 return NULL;
708
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200709 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200710 return NULL;
711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200714 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200715 return NULL;
716 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200718 for (hi = ht->ht_array; todo > 0; ++hi)
719 {
720 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200722 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200723
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200724 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200725 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200726 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200729 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200730 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200731 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200732 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200733 Py_DECREF(newObj);
734 return NULL;
735 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736 }
737 }
738 }
739 else
740 {
741 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200742 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200743 }
744
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200745 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200746}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200747
748 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200749VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200751 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200752 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200753 PyObject *string;
754 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200755 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200756 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757
Bram Moolenaar389a1792013-06-23 13:00:44 +0200758 if (!PyArg_ParseTuple(args, "O", &string))
759 return NULL;
760
761 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200762 return NULL;
763
764 Py_BEGIN_ALLOW_THREADS
765 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200766 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200767 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768 Python_Release_Vim();
769 Py_END_ALLOW_THREADS
770
Bram Moolenaar389a1792013-06-23 13:00:44 +0200771 Py_XDECREF(todecref);
772
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200773 if (VimTryEnd())
774 return NULL;
775
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200776 if (our_tv == NULL)
777 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200778 PyErr_SET_VIM("invalid expression");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200779 return NULL;
780 }
781
782 /* Convert the Vim type into a Python type. Create a dictionary that's
783 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200784 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200785 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200786 else
787 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200788 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200789 Py_DECREF(lookup_dict);
790 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791
792
793 Py_BEGIN_ALLOW_THREADS
794 Python_Lock_Vim();
795 free_tv(our_tv);
796 Python_Release_Vim();
797 Py_END_ALLOW_THREADS
798
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200799 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800}
801
Bram Moolenaardb913952012-06-29 12:54:53 +0200802static PyObject *ConvertToPyObject(typval_T *);
803
804 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200805VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200806{
Bram Moolenaardb913952012-06-29 12:54:53 +0200807 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200808 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200809 char_u *expr;
810 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200811
Bram Moolenaar389a1792013-06-23 13:00:44 +0200812 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200813 return NULL;
814
815 Py_BEGIN_ALLOW_THREADS
816 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200817 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200818 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200819 Python_Release_Vim();
820 Py_END_ALLOW_THREADS
821
Bram Moolenaar389a1792013-06-23 13:00:44 +0200822 Py_XDECREF(todecref);
823
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200824 if (VimTryEnd())
825 return NULL;
826
Bram Moolenaardb913952012-06-29 12:54:53 +0200827 if (our_tv == NULL)
828 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200829 PyErr_SET_VIM("invalid expression");
Bram Moolenaardb913952012-06-29 12:54:53 +0200830 return NULL;
831 }
832
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200833 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200834 Py_BEGIN_ALLOW_THREADS
835 Python_Lock_Vim();
836 free_tv(our_tv);
837 Python_Release_Vim();
838 Py_END_ALLOW_THREADS
839
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200840 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200841}
842
843 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200844VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200845{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200846 char_u *str;
847 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200848 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200849
Bram Moolenaar389a1792013-06-23 13:00:44 +0200850 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200851 return NULL;
852
Bram Moolenaara54bf402012-12-05 16:30:07 +0100853#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200854 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100855#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200856 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100857#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200858
859 Py_XDECREF(todecref);
860
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200861 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200862}
863
Bram Moolenaarf4258302013-06-02 18:20:17 +0200864 static PyObject *
865_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
866{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200867 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200868 PyObject *newwd;
869 PyObject *todecref;
870 char_u *new_dir;
871
Bram Moolenaard4209d22013-06-05 20:34:15 +0200872 if (_chdir == NULL)
873 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200874 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200875 return NULL;
876
877 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
878 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200879 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200880 return NULL;
881 }
882
883 if (!(new_dir = StringToChars(newwd, &todecref)))
884 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200885 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200886 Py_DECREF(newwd);
887 return NULL;
888 }
889
890 VimTryStart();
891
892 if (vim_chdir(new_dir))
893 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200894 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200895 Py_DECREF(newwd);
896 Py_XDECREF(todecref);
897
898 if (VimTryEnd())
899 return NULL;
900
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200901 PyErr_SET_VIM("failed to change directory");
Bram Moolenaarf4258302013-06-02 18:20:17 +0200902 return NULL;
903 }
904
905 Py_DECREF(newwd);
906 Py_XDECREF(todecref);
907
908 post_chdir(FALSE);
909
910 if (VimTryEnd())
911 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200912 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200913 return NULL;
914 }
915
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200916 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200917}
918
919 static PyObject *
920VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
921{
922 return _VimChdir(py_chdir, args, kwargs);
923}
924
925 static PyObject *
926VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
927{
928 return _VimChdir(py_fchdir, args, kwargs);
929}
930
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200931typedef struct {
932 PyObject *callable;
933 PyObject *result;
934} map_rtp_data;
935
936 static void
937map_rtp_callback(char_u *path, void *_data)
938{
939 void **data = (void **) _data;
940 PyObject *pathObject;
941 map_rtp_data *mr_data = *((map_rtp_data **) data);
942
943 if (!(pathObject = PyString_FromString((char *) path)))
944 {
945 *data = NULL;
946 return;
947 }
948
949 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
950 pathObject, NULL);
951
952 Py_DECREF(pathObject);
953
954 if (!mr_data->result || mr_data->result != Py_None)
955 *data = NULL;
956 else
957 {
958 Py_DECREF(mr_data->result);
959 mr_data->result = NULL;
960 }
961}
962
963 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200964VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200965{
966 map_rtp_data data;
967
Bram Moolenaar389a1792013-06-23 13:00:44 +0200968 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200969 data.result = NULL;
970
971 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
972
973 if (data.result == NULL)
974 {
975 if (PyErr_Occurred())
976 return NULL;
977 else
978 {
979 Py_INCREF(Py_None);
980 return Py_None;
981 }
982 }
983 return data.result;
984}
985
986/*
987 * _vim_runtimepath_ special path implementation.
988 */
989
990 static void
991map_finder_callback(char_u *path, void *_data)
992{
993 void **data = (void **) _data;
994 PyObject *list = *((PyObject **) data);
995 PyObject *pathObject1, *pathObject2;
996 char *pathbuf;
997 size_t pathlen;
998
999 pathlen = STRLEN(path);
1000
1001#if PY_MAJOR_VERSION < 3
1002# define PY_MAIN_DIR_STRING "python2"
1003#else
1004# define PY_MAIN_DIR_STRING "python3"
1005#endif
1006#define PY_ALTERNATE_DIR_STRING "pythonx"
1007
1008#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1009 if (!(pathbuf = PyMem_New(char,
1010 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1011 {
1012 PyErr_NoMemory();
1013 *data = NULL;
1014 return;
1015 }
1016
1017 mch_memmove(pathbuf, path, pathlen + 1);
1018 add_pathsep((char_u *) pathbuf);
1019
1020 pathlen = STRLEN(pathbuf);
1021 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1022 PYTHONX_STRING_LENGTH + 1);
1023
1024 if (!(pathObject1 = PyString_FromString(pathbuf)))
1025 {
1026 *data = NULL;
1027 PyMem_Free(pathbuf);
1028 return;
1029 }
1030
1031 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1032 PYTHONX_STRING_LENGTH + 1);
1033
1034 if (!(pathObject2 = PyString_FromString(pathbuf)))
1035 {
1036 Py_DECREF(pathObject1);
1037 PyMem_Free(pathbuf);
1038 *data = NULL;
1039 return;
1040 }
1041
1042 PyMem_Free(pathbuf);
1043
1044 if (PyList_Append(list, pathObject1)
1045 || PyList_Append(list, pathObject2))
1046 *data = NULL;
1047
1048 Py_DECREF(pathObject1);
1049 Py_DECREF(pathObject2);
1050}
1051
1052 static PyObject *
1053Vim_GetPaths(PyObject *self UNUSED)
1054{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001055 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001056
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001057 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001058 return NULL;
1059
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001060 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001061
1062 if (PyErr_Occurred())
1063 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001064 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001065 return NULL;
1066 }
1067
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001068 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001069}
1070
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001071 static PyObject *
1072call_load_module(char *name, int len, PyObject *find_module_result)
1073{
1074 PyObject *fd, *pathname, *description;
1075
Bram Moolenaarc476e522013-06-23 13:46:40 +02001076 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001077 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001078 PyErr_FORMAT(PyExc_TypeError,
1079 "expected 3-tuple as imp.find_module() result, but got %s",
1080 Py_TYPE_NAME(find_module_result));
1081 return NULL;
1082 }
1083 if (PyTuple_GET_SIZE(find_module_result) != 3)
1084 {
1085 PyErr_FORMAT(PyExc_TypeError,
1086 "expected 3-tuple as imp.find_module() result, but got "
1087 "tuple of size %d",
1088 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001089 return NULL;
1090 }
1091
1092 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1093 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1094 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1095 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001096 PyErr_SET_STRING(PyExc_RuntimeError,
1097 "internal error: imp.find_module returned tuple with NULL");
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001098 return NULL;
1099 }
1100
1101 return PyObject_CallFunction(py_load_module,
1102 "s#OOO", name, len, fd, pathname, description);
1103}
1104
1105 static PyObject *
1106find_module(char *fullname, char *tail, PyObject *new_path)
1107{
1108 PyObject *find_module_result;
1109 PyObject *module;
1110 char *dot;
1111
1112 if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
1113 {
1114 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001115 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001116 * first component
1117 */
1118 PyObject *newest_path;
1119 int partlen = (int) (dot - 1 - tail);
1120
1121 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1122 "s#O", tail, partlen, new_path)))
1123 return NULL;
1124
1125 if (!(module = call_load_module(
1126 fullname,
1127 ((int) (tail - fullname)) + partlen,
1128 find_module_result)))
1129 {
1130 Py_DECREF(find_module_result);
1131 return NULL;
1132 }
1133
1134 Py_DECREF(find_module_result);
1135
1136 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1137 {
1138 Py_DECREF(module);
1139 return NULL;
1140 }
1141
1142 Py_DECREF(module);
1143
1144 module = find_module(fullname, dot + 1, newest_path);
1145
1146 Py_DECREF(newest_path);
1147
1148 return module;
1149 }
1150 else
1151 {
1152 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1153 "sO", tail, new_path)))
1154 return NULL;
1155
1156 if (!(module = call_load_module(
1157 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001158 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001159 find_module_result)))
1160 {
1161 Py_DECREF(find_module_result);
1162 return NULL;
1163 }
1164
1165 Py_DECREF(find_module_result);
1166
1167 return module;
1168 }
1169}
1170
1171 static PyObject *
1172FinderFindModule(PyObject *self, PyObject *args)
1173{
1174 char *fullname;
1175 PyObject *module;
1176 PyObject *new_path;
1177 LoaderObject *loader;
1178
1179 if (!PyArg_ParseTuple(args, "s", &fullname))
1180 return NULL;
1181
1182 if (!(new_path = Vim_GetPaths(self)))
1183 return NULL;
1184
1185 module = find_module(fullname, fullname, new_path);
1186
1187 Py_DECREF(new_path);
1188
1189 if (!module)
1190 {
1191 Py_INCREF(Py_None);
1192 return Py_None;
1193 }
1194
1195 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1196 {
1197 Py_DECREF(module);
1198 return NULL;
1199 }
1200
1201 loader->module = module;
1202
1203 return (PyObject *) loader;
1204}
1205
1206 static PyObject *
1207VimPathHook(PyObject *self UNUSED, PyObject *args)
1208{
1209 char *path;
1210
1211 if (PyArg_ParseTuple(args, "s", &path)
1212 && STRCMP(path, vim_special_path) == 0)
1213 {
1214 Py_INCREF(vim_module);
1215 return vim_module;
1216 }
1217
1218 PyErr_Clear();
1219 PyErr_SetNone(PyExc_ImportError);
1220 return NULL;
1221}
1222
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001223/*
1224 * Vim module - Definitions
1225 */
1226
1227static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001228 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001229 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001230 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001231 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1232 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001233 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1234 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001235 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001236 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001237 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1238 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1239 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001240};
1241
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001242/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001243 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001244 */
1245
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001246static PyTypeObject IterType;
1247
1248typedef PyObject *(*nextfun)(void **);
1249typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001250typedef int (*traversefun)(void *, visitproc, void *);
1251typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001252
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001253/* Main purpose of this object is removing the need for do python
1254 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1255 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001256
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001257typedef struct
1258{
1259 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001260 void *cur;
1261 nextfun next;
1262 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001263 traversefun traverse;
1264 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001265} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001266
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001267 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001268IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1269 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001270{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001271 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001272
Bram Moolenaar774267b2013-05-21 20:51:59 +02001273 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001274 self->cur = start;
1275 self->next = next;
1276 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001277 self->traverse = traverse;
1278 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001279
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001280 return (PyObject *)(self);
1281}
1282
1283 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001284IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001285{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001286 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001287 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001288 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001289}
1290
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001291 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001292IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001293{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001294 if (self->traverse != NULL)
1295 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001296 else
1297 return 0;
1298}
1299
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001300/* Mac OSX defines clear() somewhere. */
1301#ifdef clear
1302# undef clear
1303#endif
1304
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001305 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001306IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001307{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001308 if (self->clear != NULL)
1309 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001310 else
1311 return 0;
1312}
1313
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001314 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001315IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001316{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001317 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001318}
1319
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001320 static PyObject *
1321IterIter(PyObject *self)
1322{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001323 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001324 return self;
1325}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001326
Bram Moolenaardb913952012-06-29 12:54:53 +02001327typedef struct pylinkedlist_S {
1328 struct pylinkedlist_S *pll_next;
1329 struct pylinkedlist_S *pll_prev;
1330 PyObject *pll_obj;
1331} pylinkedlist_T;
1332
1333static pylinkedlist_T *lastdict = NULL;
1334static pylinkedlist_T *lastlist = NULL;
1335
1336 static void
1337pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1338{
1339 if (ref->pll_prev == NULL)
1340 {
1341 if (ref->pll_next == NULL)
1342 {
1343 *last = NULL;
1344 return;
1345 }
1346 }
1347 else
1348 ref->pll_prev->pll_next = ref->pll_next;
1349
1350 if (ref->pll_next == NULL)
1351 *last = ref->pll_prev;
1352 else
1353 ref->pll_next->pll_prev = ref->pll_prev;
1354}
1355
1356 static void
1357pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1358{
1359 if (*last == NULL)
1360 ref->pll_prev = NULL;
1361 else
1362 {
1363 (*last)->pll_next = ref;
1364 ref->pll_prev = *last;
1365 }
1366 ref->pll_next = NULL;
1367 ref->pll_obj = self;
1368 *last = ref;
1369}
1370
1371static PyTypeObject DictionaryType;
1372
1373typedef struct
1374{
1375 PyObject_HEAD
1376 dict_T *dict;
1377 pylinkedlist_T ref;
1378} DictionaryObject;
1379
Bram Moolenaara9922d62013-05-30 13:01:18 +02001380static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1381
1382#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1383
Bram Moolenaardb913952012-06-29 12:54:53 +02001384 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001385DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001386{
1387 DictionaryObject *self;
1388
Bram Moolenaara9922d62013-05-30 13:01:18 +02001389 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001390 if (self == NULL)
1391 return NULL;
1392 self->dict = dict;
1393 ++dict->dv_refcount;
1394
1395 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1396
1397 return (PyObject *)(self);
1398}
1399
Bram Moolenaara9922d62013-05-30 13:01:18 +02001400 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001401py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001402{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001403 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001404
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001405 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001406 {
1407 PyErr_NoMemory();
1408 return NULL;
1409 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001410 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001411
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001412 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001413}
1414
1415 static PyObject *
1416DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1417{
1418 DictionaryObject *self;
1419 dict_T *dict;
1420
1421 if (!(dict = py_dict_alloc()))
1422 return NULL;
1423
1424 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1425
1426 --dict->dv_refcount;
1427
1428 if (kwargs || PyTuple_Size(args))
1429 {
1430 PyObject *tmp;
1431 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1432 {
1433 Py_DECREF(self);
1434 return NULL;
1435 }
1436
1437 Py_DECREF(tmp);
1438 }
1439
1440 return (PyObject *)(self);
1441}
1442
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001443 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001444DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001445{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001446 pyll_remove(&self->ref, &lastdict);
1447 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001448
1449 DESTRUCTOR_FINISH(self);
1450}
1451
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001452static char *DictionaryAttrs[] = {
1453 "locked", "scope",
1454 NULL
1455};
1456
1457 static PyObject *
1458DictionaryDir(PyObject *self)
1459{
1460 return ObjectDir(self, DictionaryAttrs);
1461}
1462
Bram Moolenaardb913952012-06-29 12:54:53 +02001463 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001464DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001465{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001467 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001468 PyErr_SET_STRING(PyExc_AttributeError,
1469 "cannot delete vim.Dictionary attributes");
Bram Moolenaar66b79852012-09-21 14:00:35 +02001470 return -1;
1471 }
1472
1473 if (strcmp(name, "locked") == 0)
1474 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001475 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001476 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001477 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed dictionary");
Bram Moolenaar66b79852012-09-21 14:00:35 +02001478 return -1;
1479 }
1480 else
1481 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001482 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001483 if (istrue == -1)
1484 return -1;
1485 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001486 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001487 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001488 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001489 }
1490 return 0;
1491 }
1492 else
1493 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001494 PyErr_FORMAT(PyExc_AttributeError, "cannot set attribute %s", name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001495 return -1;
1496 }
1497}
1498
1499 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001500DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001501{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001503}
1504
Bram Moolenaara9922d62013-05-30 13:01:18 +02001505#define DICT_FLAG_HAS_DEFAULT 0x01
1506#define DICT_FLAG_POP 0x02
1507#define DICT_FLAG_NONE_DEFAULT 0x04
1508#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1509#define DICT_FLAG_RETURN_PAIR 0x10
1510
Bram Moolenaardb913952012-06-29 12:54:53 +02001511 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001512_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001513{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001514 PyObject *keyObject;
1515 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001516 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001517 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001518 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001519 dict_T *dict = self->dict;
1520 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001521 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001522
Bram Moolenaara9922d62013-05-30 13:01:18 +02001523 if (flags & DICT_FLAG_HAS_DEFAULT)
1524 {
1525 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1526 return NULL;
1527 }
1528 else
1529 keyObject = args;
1530
1531 if (flags & DICT_FLAG_RETURN_BOOL)
1532 defObject = Py_False;
1533
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001534 if (!(key = StringToChars(keyObject, &todecref)))
1535 return NULL;
1536
1537 if (*key == NUL)
1538 {
1539 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001540 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001541 return NULL;
1542 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001543
Bram Moolenaara9922d62013-05-30 13:01:18 +02001544 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001545
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001546 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001547
Bram Moolenaara9922d62013-05-30 13:01:18 +02001548 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001549 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001550 if (defObject)
1551 {
1552 Py_INCREF(defObject);
1553 return defObject;
1554 }
1555 else
1556 {
1557 PyErr_SetObject(PyExc_KeyError, keyObject);
1558 return NULL;
1559 }
1560 }
1561 else if (flags & DICT_FLAG_RETURN_BOOL)
1562 {
1563 Py_INCREF(Py_True);
1564 return Py_True;
1565 }
1566
1567 di = dict_lookup(hi);
1568
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001569 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001570 return NULL;
1571
1572 if (flags & DICT_FLAG_POP)
1573 {
1574 if (dict->dv_lock)
1575 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001576 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001577 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001578 return NULL;
1579 }
1580
1581 hash_remove(&dict->dv_hashtab, hi);
1582 dictitem_free(di);
1583 }
1584
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001585 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001586}
1587
1588 static PyObject *
1589DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1590{
1591 return _DictionaryItem(self, keyObject, 0);
1592}
1593
1594 static int
1595DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1596{
1597 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001598 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001599
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001600 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601
1602 Py_DECREF(Py_True);
1603
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001604 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605}
1606
1607typedef struct
1608{
1609 hashitem_T *ht_array;
1610 long_u ht_used;
1611 hashtab_T *ht;
1612 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001613 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001614} dictiterinfo_T;
1615
1616 static PyObject *
1617DictionaryIterNext(dictiterinfo_T **dii)
1618{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001619 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001620
1621 if (!(*dii)->todo)
1622 return NULL;
1623
1624 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1625 (*dii)->ht->ht_used != (*dii)->ht_used)
1626 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001627 PyErr_SET_STRING(PyExc_RuntimeError,
1628 "hashtab changed during iteration");
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001629 return NULL;
1630 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001631
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1633 ++((*dii)->hi);
1634
1635 --((*dii)->todo);
1636
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001637 if (!(ret = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001638 return NULL;
1639
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001640 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001641}
1642
1643 static PyObject *
1644DictionaryIter(DictionaryObject *self)
1645{
1646 dictiterinfo_T *dii;
1647 hashtab_T *ht;
1648
1649 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1650 {
1651 PyErr_NoMemory();
1652 return NULL;
1653 }
1654
1655 ht = &self->dict->dv_hashtab;
1656 dii->ht_array = ht->ht_array;
1657 dii->ht_used = ht->ht_used;
1658 dii->ht = ht;
1659 dii->hi = dii->ht_array;
1660 dii->todo = dii->ht_used;
1661
1662 return IterNew(dii,
1663 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1664 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001665}
1666
1667 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001668DictionaryAssItem(
1669 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001670{
1671 char_u *key;
1672 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001673 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001674 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001675 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001676
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001677 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001678 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001679 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001680 return -1;
1681 }
1682
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001683 if (!(key = StringToChars(keyObject, &todecref)))
1684 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001685
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001686 if (*key == NUL)
1687 {
1688 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001689 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001690 return -1;
1691 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001692
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001693 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001694
1695 if (valObject == NULL)
1696 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001697 hashitem_T *hi;
1698
Bram Moolenaardb913952012-06-29 12:54:53 +02001699 if (di == NULL)
1700 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001701 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001702 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001703 return -1;
1704 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001705 hi = hash_find(&dict->dv_hashtab, di->di_key);
1706 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001707 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001708 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001709 return 0;
1710 }
1711
1712 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001713 {
1714 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001715 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001716 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001717
1718 if (di == NULL)
1719 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001720 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001721 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001722 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001723 PyErr_NoMemory();
1724 return -1;
1725 }
1726 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001727 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001728
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001729 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001730 {
1731 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001732 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001733 RAISE_KEY_ADD_FAIL(key);
1734 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001735 return -1;
1736 }
1737 }
1738 else
1739 clear_tv(&di->di_tv);
1740
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001741 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001742
1743 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001744 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 return 0;
1746}
1747
Bram Moolenaara9922d62013-05-30 13:01:18 +02001748typedef PyObject *(*hi_to_py)(hashitem_T *);
1749
Bram Moolenaardb913952012-06-29 12:54:53 +02001750 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001751DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001752{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001753 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001754 long_u todo = dict->dv_hashtab.ht_used;
1755 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001756 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001757 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001758 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001760 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001761 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1762 {
1763 if (!HASHITEM_EMPTY(hi))
1764 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001765 if (!(newObj = hiconvert(hi)))
1766 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001767 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001768 return NULL;
1769 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001770 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001771 --todo;
1772 ++i;
1773 }
1774 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001775 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001776}
1777
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778 static PyObject *
1779dict_key(hashitem_T *hi)
1780{
1781 return PyBytes_FromString((char *)(hi->hi_key));
1782}
1783
1784 static PyObject *
1785DictionaryListKeys(DictionaryObject *self)
1786{
1787 return DictionaryListObjects(self, dict_key);
1788}
1789
1790 static PyObject *
1791dict_val(hashitem_T *hi)
1792{
1793 dictitem_T *di;
1794
1795 di = dict_lookup(hi);
1796 return ConvertToPyObject(&di->di_tv);
1797}
1798
1799 static PyObject *
1800DictionaryListValues(DictionaryObject *self)
1801{
1802 return DictionaryListObjects(self, dict_val);
1803}
1804
1805 static PyObject *
1806dict_item(hashitem_T *hi)
1807{
1808 PyObject *keyObject;
1809 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001810 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001811
1812 if (!(keyObject = dict_key(hi)))
1813 return NULL;
1814
1815 if (!(valObject = dict_val(hi)))
1816 {
1817 Py_DECREF(keyObject);
1818 return NULL;
1819 }
1820
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001821 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001822
1823 Py_DECREF(keyObject);
1824 Py_DECREF(valObject);
1825
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001826 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001827}
1828
1829 static PyObject *
1830DictionaryListItems(DictionaryObject *self)
1831{
1832 return DictionaryListObjects(self, dict_item);
1833}
1834
1835 static PyObject *
1836DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1837{
1838 dict_T *dict = self->dict;
1839
1840 if (dict->dv_lock)
1841 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001842 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001843 return NULL;
1844 }
1845
1846 if (kwargs)
1847 {
1848 typval_T tv;
1849
1850 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1851 return NULL;
1852
1853 VimTryStart();
1854 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1855 clear_tv(&tv);
1856 if (VimTryEnd())
1857 return NULL;
1858 }
1859 else
1860 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001861 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001862
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001863 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001864 return NULL;
1865
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001866 if (PyObject_HasAttrString(obj, "keys"))
1867 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001868 else
1869 {
1870 PyObject *iterator;
1871 PyObject *item;
1872
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001873 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001874 return NULL;
1875
1876 while ((item = PyIter_Next(iterator)))
1877 {
1878 PyObject *fast;
1879 PyObject *keyObject;
1880 PyObject *valObject;
1881 PyObject *todecref;
1882 char_u *key;
1883 dictitem_T *di;
1884
1885 if (!(fast = PySequence_Fast(item, "")))
1886 {
1887 Py_DECREF(iterator);
1888 Py_DECREF(item);
1889 return NULL;
1890 }
1891
1892 Py_DECREF(item);
1893
1894 if (PySequence_Fast_GET_SIZE(fast) != 2)
1895 {
1896 Py_DECREF(iterator);
1897 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001898 PyErr_FORMAT(PyExc_ValueError,
1899 "expected sequence element of size 2, "
1900 "but got sequence of size %d",
1901 PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001902 return NULL;
1903 }
1904
1905 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1906
1907 if (!(key = StringToChars(keyObject, &todecref)))
1908 {
1909 Py_DECREF(iterator);
1910 Py_DECREF(fast);
1911 return NULL;
1912 }
1913
1914 di = dictitem_alloc(key);
1915
1916 Py_XDECREF(todecref);
1917
1918 if (di == NULL)
1919 {
1920 Py_DECREF(fast);
1921 Py_DECREF(iterator);
1922 PyErr_NoMemory();
1923 return NULL;
1924 }
1925 di->di_tv.v_lock = 0;
1926 di->di_tv.v_type = VAR_UNKNOWN;
1927
1928 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1929
1930 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1931 {
1932 Py_DECREF(iterator);
1933 Py_DECREF(fast);
1934 dictitem_free(di);
1935 return NULL;
1936 }
1937
1938 Py_DECREF(fast);
1939
1940 if (dict_add(dict, di) == FAIL)
1941 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001942 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001943 Py_DECREF(iterator);
1944 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001945 return NULL;
1946 }
1947 }
1948
1949 Py_DECREF(iterator);
1950
1951 /* Iterator may have finished due to an exception */
1952 if (PyErr_Occurred())
1953 return NULL;
1954 }
1955 }
1956 Py_INCREF(Py_None);
1957 return Py_None;
1958}
1959
1960 static PyObject *
1961DictionaryGet(DictionaryObject *self, PyObject *args)
1962{
1963 return _DictionaryItem(self, args,
1964 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1965}
1966
1967 static PyObject *
1968DictionaryPop(DictionaryObject *self, PyObject *args)
1969{
1970 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1971}
1972
1973 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001974DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001975{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001976 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001977 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02001978 PyObject *valObject;
1979 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001980
Bram Moolenaarde71b562013-06-02 17:41:54 +02001981 if (self->dict->dv_hashtab.ht_used == 0)
1982 {
1983 PyErr_SetNone(PyExc_KeyError);
1984 return NULL;
1985 }
1986
1987 hi = self->dict->dv_hashtab.ht_array;
1988 while (HASHITEM_EMPTY(hi))
1989 ++hi;
1990
1991 di = dict_lookup(hi);
1992
1993 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001994 return NULL;
1995
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001996 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02001997 {
1998 Py_DECREF(valObject);
1999 return NULL;
2000 }
2001
2002 hash_remove(&self->dict->dv_hashtab, hi);
2003 dictitem_free(di);
2004
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002005 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002006}
2007
2008 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002009DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002011 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2012}
2013
2014static PySequenceMethods DictionaryAsSeq = {
2015 0, /* sq_length */
2016 0, /* sq_concat */
2017 0, /* sq_repeat */
2018 0, /* sq_item */
2019 0, /* sq_slice */
2020 0, /* sq_ass_item */
2021 0, /* sq_ass_slice */
2022 (objobjproc) DictionaryContains, /* sq_contains */
2023 0, /* sq_inplace_concat */
2024 0, /* sq_inplace_repeat */
2025};
2026
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002027static PyMappingMethods DictionaryAsMapping = {
2028 (lenfunc) DictionaryLength,
2029 (binaryfunc) DictionaryItem,
2030 (objobjargproc) DictionaryAssItem,
2031};
2032
Bram Moolenaardb913952012-06-29 12:54:53 +02002033static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002034 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002035 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2036 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2037 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2038 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2039 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002040 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002041 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002042 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2043 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002044};
2045
2046static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002047static PySequenceMethods ListAsSeq;
2048static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002049
2050typedef struct
2051{
2052 PyObject_HEAD
2053 list_T *list;
2054 pylinkedlist_T ref;
2055} ListObject;
2056
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002057#define NEW_LIST(list) ListNew(&ListType, list)
2058
Bram Moolenaardb913952012-06-29 12:54:53 +02002059 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002060ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002061{
2062 ListObject *self;
2063
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002064 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002065 if (self == NULL)
2066 return NULL;
2067 self->list = list;
2068 ++list->lv_refcount;
2069
2070 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2071
2072 return (PyObject *)(self);
2073}
2074
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002075 static list_T *
2076py_list_alloc()
2077{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002078 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002079
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002080 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002081 {
2082 PyErr_NoMemory();
2083 return NULL;
2084 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002085 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002086
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002087 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002088}
2089
2090 static int
2091list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2092{
2093 PyObject *iterator;
2094 PyObject *item;
2095 listitem_T *li;
2096
2097 if (!(iterator = PyObject_GetIter(obj)))
2098 return -1;
2099
2100 while ((item = PyIter_Next(iterator)))
2101 {
2102 if (!(li = listitem_alloc()))
2103 {
2104 PyErr_NoMemory();
2105 Py_DECREF(item);
2106 Py_DECREF(iterator);
2107 return -1;
2108 }
2109 li->li_tv.v_lock = 0;
2110 li->li_tv.v_type = VAR_UNKNOWN;
2111
2112 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2113 {
2114 Py_DECREF(item);
2115 Py_DECREF(iterator);
2116 listitem_free(li);
2117 return -1;
2118 }
2119
2120 Py_DECREF(item);
2121
2122 list_append(l, li);
2123 }
2124
2125 Py_DECREF(iterator);
2126
2127 /* Iterator may have finished due to an exception */
2128 if (PyErr_Occurred())
2129 return -1;
2130
2131 return 0;
2132}
2133
2134 static PyObject *
2135ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2136{
2137 list_T *list;
2138 PyObject *obj = NULL;
2139
2140 if (kwargs)
2141 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002142 PyErr_SET_STRING(PyExc_TypeError,
2143 "list constructor does not accept keyword arguments");
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002144 return NULL;
2145 }
2146
2147 if (!PyArg_ParseTuple(args, "|O", &obj))
2148 return NULL;
2149
2150 if (!(list = py_list_alloc()))
2151 return NULL;
2152
2153 if (obj)
2154 {
2155 PyObject *lookup_dict;
2156
2157 if (!(lookup_dict = PyDict_New()))
2158 {
2159 list_unref(list);
2160 return NULL;
2161 }
2162
2163 if (list_py_concat(list, obj, lookup_dict) == -1)
2164 {
2165 Py_DECREF(lookup_dict);
2166 list_unref(list);
2167 return NULL;
2168 }
2169
2170 Py_DECREF(lookup_dict);
2171 }
2172
2173 return ListNew(subtype, list);
2174}
2175
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002176 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002177ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002178{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002179 pyll_remove(&self->ref, &lastlist);
2180 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002181
2182 DESTRUCTOR_FINISH(self);
2183}
2184
Bram Moolenaardb913952012-06-29 12:54:53 +02002185 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002186ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002187{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002188 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002189}
2190
2191 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002192ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002193{
2194 listitem_T *li;
2195
Bram Moolenaard6e39182013-05-21 18:30:34 +02002196 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002197 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002198 PyErr_SET_STRING(PyExc_IndexError, "list index out of range");
Bram Moolenaardb913952012-06-29 12:54:53 +02002199 return NULL;
2200 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002201 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002202 if (li == NULL)
2203 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002204 /* No more suitable format specifications in python-2.3 */
2205 PyErr_VIM_FORMAT("internal error: failed to get vim list item %d",
2206 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002207 return NULL;
2208 }
2209 return ConvertToPyObject(&li->li_tv);
2210}
2211
2212#define PROC_RANGE \
2213 if (last < 0) {\
2214 if (last < -size) \
2215 last = 0; \
2216 else \
2217 last += size; \
2218 } \
2219 if (first < 0) \
2220 first = 0; \
2221 if (first > size) \
2222 first = size; \
2223 if (last > size) \
2224 last = size;
2225
2226 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002227ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002228{
2229 PyInt i;
2230 PyInt size = ListLength(self);
2231 PyInt n;
2232 PyObject *list;
2233 int reversed = 0;
2234
2235 PROC_RANGE
2236 if (first >= last)
2237 first = last;
2238
2239 n = last-first;
2240 list = PyList_New(n);
2241 if (list == NULL)
2242 return NULL;
2243
2244 for (i = 0; i < n; ++i)
2245 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002246 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002247 if (item == NULL)
2248 {
2249 Py_DECREF(list);
2250 return NULL;
2251 }
2252
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002253 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002254 }
2255
2256 return list;
2257}
2258
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002259typedef struct
2260{
2261 listwatch_T lw;
2262 list_T *list;
2263} listiterinfo_T;
2264
2265 static void
2266ListIterDestruct(listiterinfo_T *lii)
2267{
2268 list_rem_watch(lii->list, &lii->lw);
2269 PyMem_Free(lii);
2270}
2271
2272 static PyObject *
2273ListIterNext(listiterinfo_T **lii)
2274{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002275 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002276
2277 if (!((*lii)->lw.lw_item))
2278 return NULL;
2279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002280 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002281 return NULL;
2282
2283 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2284
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002285 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002286}
2287
2288 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002289ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002290{
2291 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002292 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002293
2294 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2295 {
2296 PyErr_NoMemory();
2297 return NULL;
2298 }
2299
2300 list_add_watch(l, &lii->lw);
2301 lii->lw.lw_item = l->lv_first;
2302 lii->list = l;
2303
2304 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002305 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2306 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002307}
2308
Bram Moolenaardb913952012-06-29 12:54:53 +02002309 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002310ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002311{
2312 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002313 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002314 listitem_T *li;
2315 Py_ssize_t length = ListLength(self);
2316
2317 if (l->lv_lock)
2318 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002319 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002320 return -1;
2321 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002322 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002323 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002324 PyErr_SET_STRING(PyExc_IndexError, "list index out of range");
Bram Moolenaardb913952012-06-29 12:54:53 +02002325 return -1;
2326 }
2327
2328 if (obj == NULL)
2329 {
2330 li = list_find(l, (long) index);
2331 list_remove(l, li, li);
2332 clear_tv(&li->li_tv);
2333 vim_free(li);
2334 return 0;
2335 }
2336
2337 if (ConvertFromPyObject(obj, &tv) == -1)
2338 return -1;
2339
2340 if (index == length)
2341 {
2342 if (list_append_tv(l, &tv) == FAIL)
2343 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002344 clear_tv(&tv);
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002345 PyErr_SET_VIM("failed to add item to list");
Bram Moolenaardb913952012-06-29 12:54:53 +02002346 return -1;
2347 }
2348 }
2349 else
2350 {
2351 li = list_find(l, (long) index);
2352 clear_tv(&li->li_tv);
2353 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002354 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002355 }
2356 return 0;
2357}
2358
2359 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002360ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002361{
2362 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002363 PyObject *iterator;
2364 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002365 listitem_T *li;
2366 listitem_T *next;
2367 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002368 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002369 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002370
2371 if (l->lv_lock)
2372 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002373 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002374 return -1;
2375 }
2376
2377 PROC_RANGE
2378
2379 if (first == size)
2380 li = NULL;
2381 else
2382 {
2383 li = list_find(l, (long) first);
2384 if (li == NULL)
2385 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002386 PyErr_VIM_FORMAT("internal error: no vim list item %d", (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002387 return -1;
2388 }
2389 if (last > first)
2390 {
2391 i = last - first;
2392 while (i-- && li != NULL)
2393 {
2394 next = li->li_next;
2395 listitem_remove(l, li);
2396 li = next;
2397 }
2398 }
2399 }
2400
2401 if (obj == NULL)
2402 return 0;
2403
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002404 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002405 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002406
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002407 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002409 if (ConvertFromPyObject(item, &v) == -1)
2410 {
2411 Py_DECREF(iterator);
2412 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002413 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002414 }
2415 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 if (list_insert_tv(l, &v, li) == FAIL)
2417 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002418 clear_tv(&v);
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002419 PyErr_SET_VIM("internal error: failed to add item to list");
Bram Moolenaardb913952012-06-29 12:54:53 +02002420 return -1;
2421 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002422 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002423 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002424 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02002425 return 0;
2426}
2427
2428 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002429ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002430{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002431 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002432 PyObject *lookup_dict;
2433
2434 if (l->lv_lock)
2435 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002436 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002437 return NULL;
2438 }
2439
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002440 if (!(lookup_dict = PyDict_New()))
2441 return NULL;
2442
Bram Moolenaardb913952012-06-29 12:54:53 +02002443 if (list_py_concat(l, obj, lookup_dict) == -1)
2444 {
2445 Py_DECREF(lookup_dict);
2446 return NULL;
2447 }
2448 Py_DECREF(lookup_dict);
2449
2450 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002451 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002452}
2453
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002454static char *ListAttrs[] = {
2455 "locked",
2456 NULL
2457};
2458
2459 static PyObject *
2460ListDir(PyObject *self)
2461{
2462 return ObjectDir(self, ListAttrs);
2463}
2464
Bram Moolenaar66b79852012-09-21 14:00:35 +02002465 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002466ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002467{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002468 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002469 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002470 PyErr_SET_STRING(PyExc_AttributeError,
2471 "cannot delete vim.List attributes");
Bram Moolenaar66b79852012-09-21 14:00:35 +02002472 return -1;
2473 }
2474
2475 if (strcmp(name, "locked") == 0)
2476 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002477 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002478 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002479 PyErr_SET_STRING(PyExc_TypeError, "cannot modify fixed list");
Bram Moolenaar66b79852012-09-21 14:00:35 +02002480 return -1;
2481 }
2482 else
2483 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002484 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002485 if (istrue == -1)
2486 return -1;
2487 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002488 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002489 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002490 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002491 }
2492 return 0;
2493 }
2494 else
2495 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002496 PyErr_FORMAT(PyExc_AttributeError, "cannot set attribute %s", name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002497 return -1;
2498 }
2499}
2500
Bram Moolenaardb913952012-06-29 12:54:53 +02002501static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002502 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2503 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2504 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002505};
2506
2507typedef struct
2508{
2509 PyObject_HEAD
2510 char_u *name;
2511} FunctionObject;
2512
2513static PyTypeObject FunctionType;
2514
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002515#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2516
Bram Moolenaardb913952012-06-29 12:54:53 +02002517 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002518FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002519{
2520 FunctionObject *self;
2521
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002522 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2523
Bram Moolenaardb913952012-06-29 12:54:53 +02002524 if (self == NULL)
2525 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002526
2527 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002528 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002529 if (!translated_function_exists(name))
2530 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002531 PyErr_FORMAT(PyExc_ValueError,
2532 "unnamed function %s does not exist", name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002533 return NULL;
2534 }
2535 self->name = vim_strsave(name);
2536 func_ref(self->name);
2537 }
2538 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002539 if ((self->name = get_expanded_name(name,
2540 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2541 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002542 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002543 PyErr_FORMAT(PyExc_ValueError, "function %s does not exist", name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002544 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002545 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002546
2547 return (PyObject *)(self);
2548}
2549
2550 static PyObject *
2551FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2552{
2553 PyObject *self;
2554 char_u *name;
2555
2556 if (kwargs)
2557 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002558 PyErr_SET_STRING(PyExc_TypeError,
2559 "function constructor does not accept keyword arguments");
Bram Moolenaardb913952012-06-29 12:54:53 +02002560 return NULL;
2561 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002562
Bram Moolenaar389a1792013-06-23 13:00:44 +02002563 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002564 return NULL;
2565
2566 self = FunctionNew(subtype, name);
2567
Bram Moolenaar389a1792013-06-23 13:00:44 +02002568 PyMem_Free(name);
2569
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002570 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002571}
2572
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002573 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002574FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002575{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002576 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002577 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002578
2579 DESTRUCTOR_FINISH(self);
2580}
2581
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002582static char *FunctionAttrs[] = {
2583 "softspace",
2584 NULL
2585};
2586
2587 static PyObject *
2588FunctionDir(PyObject *self)
2589{
2590 return ObjectDir(self, FunctionAttrs);
2591}
2592
Bram Moolenaardb913952012-06-29 12:54:53 +02002593 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002594FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002595{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002596 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002597 typval_T args;
2598 typval_T selfdicttv;
2599 typval_T rettv;
2600 dict_T *selfdict = NULL;
2601 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002602 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002603 int error;
2604
2605 if (ConvertFromPyObject(argsObject, &args) == -1)
2606 return NULL;
2607
2608 if (kwargs != NULL)
2609 {
2610 selfdictObject = PyDict_GetItemString(kwargs, "self");
2611 if (selfdictObject != NULL)
2612 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002613 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002614 {
2615 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002616 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002617 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002618 selfdict = selfdicttv.vval.v_dict;
2619 }
2620 }
2621
Bram Moolenaar71700b82013-05-15 17:49:05 +02002622 Py_BEGIN_ALLOW_THREADS
2623 Python_Lock_Vim();
2624
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002625 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002626 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002627
2628 Python_Release_Vim();
2629 Py_END_ALLOW_THREADS
2630
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002631 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002632 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002633 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002634 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002635 ret = NULL;
Bram Moolenaarc476e522013-06-23 13:46:40 +02002636 PyErr_VIM_FORMAT("failed to run function %s", (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002637 }
2638 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002639 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002640
Bram Moolenaardb913952012-06-29 12:54:53 +02002641 clear_tv(&args);
2642 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002643 if (selfdict != NULL)
2644 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002645
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002646 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002647}
2648
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002649 static PyObject *
2650FunctionRepr(FunctionObject *self)
2651{
2652 return PyString_FromFormat("<vim.Function '%s'>", self->name);
2653}
2654
Bram Moolenaardb913952012-06-29 12:54:53 +02002655static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002656 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2657 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002658};
2659
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002660/*
2661 * Options object
2662 */
2663
2664static PyTypeObject OptionsType;
2665
2666typedef int (*checkfun)(void *);
2667
2668typedef struct
2669{
2670 PyObject_HEAD
2671 int opt_type;
2672 void *from;
2673 checkfun Check;
2674 PyObject *fromObj;
2675} OptionsObject;
2676
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002677 static int
2678dummy_check(void *arg UNUSED)
2679{
2680 return 0;
2681}
2682
2683 static PyObject *
2684OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2685{
2686 OptionsObject *self;
2687
Bram Moolenaar774267b2013-05-21 20:51:59 +02002688 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002689 if (self == NULL)
2690 return NULL;
2691
2692 self->opt_type = opt_type;
2693 self->from = from;
2694 self->Check = Check;
2695 self->fromObj = fromObj;
2696 if (fromObj)
2697 Py_INCREF(fromObj);
2698
2699 return (PyObject *)(self);
2700}
2701
2702 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002703OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002704{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002705 PyObject_GC_UnTrack((void *)(self));
2706 Py_XDECREF(self->fromObj);
2707 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002708}
2709
2710 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002711OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002712{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002713 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002714 return 0;
2715}
2716
2717 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002718OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002719{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002720 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002721 return 0;
2722}
2723
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002724 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002725OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002726{
2727 char_u *key;
2728 int flags;
2729 long numval;
2730 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002731 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002732
Bram Moolenaard6e39182013-05-21 18:30:34 +02002733 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002734 return NULL;
2735
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002736 if (!(key = StringToChars(keyObject, &todecref)))
2737 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002738
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002739 if (*key == NUL)
2740 {
2741 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002742 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002743 return NULL;
2744 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002745
2746 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002747 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002748
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002749 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002750
2751 if (flags == 0)
2752 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002753 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002754 return NULL;
2755 }
2756
2757 if (flags & SOPT_UNSET)
2758 {
2759 Py_INCREF(Py_None);
2760 return Py_None;
2761 }
2762 else if (flags & SOPT_BOOL)
2763 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002764 PyObject *ret;
2765 ret = numval ? Py_True : Py_False;
2766 Py_INCREF(ret);
2767 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002768 }
2769 else if (flags & SOPT_NUM)
2770 return PyInt_FromLong(numval);
2771 else if (flags & SOPT_STRING)
2772 {
2773 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002774 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002775 PyObject *ret = PyBytes_FromString((char *) stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002776 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002777 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002778 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002779 else
2780 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002781 PyErr_SET_STRING(PyExc_RuntimeError,
2782 "unable to get option value");
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002783 return NULL;
2784 }
2785 }
2786 else
2787 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002788 PyErr_SET_VIM("internal error: unknown option type");
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002789 return NULL;
2790 }
2791}
2792
2793 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002794set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002795{
2796 char_u *errmsg;
2797
2798 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2799 {
2800 if (VimTryEnd())
2801 return FAIL;
2802 PyErr_SetVim((char *)errmsg);
2803 return FAIL;
2804 }
2805 return OK;
2806}
2807
2808 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002809set_option_value_for(
2810 char_u *key,
2811 int numval,
2812 char_u *stringval,
2813 int opt_flags,
2814 int opt_type,
2815 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002816{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002817 win_T *save_curwin = NULL;
2818 tabpage_T *save_curtab = NULL;
2819 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002820 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002821
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002822 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002823 switch (opt_type)
2824 {
2825 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002826 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002827 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002828 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002829 if (VimTryEnd())
2830 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002831 PyErr_SET_VIM("problem while switching windows");
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002832 return -1;
2833 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002834 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2835 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002836 break;
2837 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002838 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002839 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002840 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002841 break;
2842 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002843 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002844 break;
2845 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002846 if (set_ret == FAIL)
2847 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002848 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002849}
2850
2851 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002852OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002853{
2854 char_u *key;
2855 int flags;
2856 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002857 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002858 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002859
Bram Moolenaard6e39182013-05-21 18:30:34 +02002860 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002861 return -1;
2862
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002863 if (!(key = StringToChars(keyObject, &todecref)))
2864 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002865
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002866 if (*key == NUL)
2867 {
2868 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002869 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002870 return -1;
2871 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002872
2873 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002874 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002875
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002876 if (flags == 0)
2877 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002878 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002879 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002880 return -1;
2881 }
2882
2883 if (valObject == NULL)
2884 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002885 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002886 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002887 PyErr_FORMAT(PyExc_ValueError,
2888 "unable to unset global option %s", key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002889 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002890 return -1;
2891 }
2892 else if (!(flags & SOPT_GLOBAL))
2893 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002894 PyErr_FORMAT(PyExc_ValueError,
2895 "unable to unset option %s "
2896 "which does not have global value", key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002897 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002898 return -1;
2899 }
2900 else
2901 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002902 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002903 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002904 return 0;
2905 }
2906 }
2907
Bram Moolenaard6e39182013-05-21 18:30:34 +02002908 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002909
2910 if (flags & SOPT_BOOL)
2911 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002912 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002913
Bram Moolenaarb983f752013-05-15 16:11:50 +02002914 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002915 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002916 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002917 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002918 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002919 }
2920 else if (flags & SOPT_NUM)
2921 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002922 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002923
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002924 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002925 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002926 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002927 return -1;
2928 }
2929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002930 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002931 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002932 }
2933 else
2934 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002935 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002936 PyObject *todecref;
2937
2938 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002939 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002940 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002941 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002942 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002943 }
2944
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002945 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002946
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002947 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002948}
2949
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002950static PyMappingMethods OptionsAsMapping = {
2951 (lenfunc) NULL,
2952 (binaryfunc) OptionsItem,
2953 (objobjargproc) OptionsAssItem,
2954};
2955
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002956/* Tabpage object
2957 */
2958
2959typedef struct
2960{
2961 PyObject_HEAD
2962 tabpage_T *tab;
2963} TabPageObject;
2964
2965static PyObject *WinListNew(TabPageObject *tabObject);
2966
2967static PyTypeObject TabPageType;
2968
2969 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002970CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002971{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002972 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002973 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002974 PyErr_SET_VIM("attempt to refer to deleted tab page");
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002975 return -1;
2976 }
2977
2978 return 0;
2979}
2980
2981 static PyObject *
2982TabPageNew(tabpage_T *tab)
2983{
2984 TabPageObject *self;
2985
2986 if (TAB_PYTHON_REF(tab))
2987 {
2988 self = TAB_PYTHON_REF(tab);
2989 Py_INCREF(self);
2990 }
2991 else
2992 {
2993 self = PyObject_NEW(TabPageObject, &TabPageType);
2994 if (self == NULL)
2995 return NULL;
2996 self->tab = tab;
2997 TAB_PYTHON_REF(tab) = self;
2998 }
2999
3000 return (PyObject *)(self);
3001}
3002
3003 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003004TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003005{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003006 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3007 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003008
3009 DESTRUCTOR_FINISH(self);
3010}
3011
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003012static char *TabPageAttrs[] = {
3013 "windows", "number", "vars", "window", "valid",
3014 NULL
3015};
3016
3017 static PyObject *
3018TabPageDir(PyObject *self)
3019{
3020 return ObjectDir(self, TabPageAttrs);
3021}
3022
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003023 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003024TabPageAttrValid(TabPageObject *self, char *name)
3025{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003026 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003027
3028 if (strcmp(name, "valid") != 0)
3029 return NULL;
3030
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003031 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3032 Py_INCREF(ret);
3033 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003034}
3035
3036 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003037TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003038{
3039 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003040 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003041 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003042 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003043 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003044 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003045 else if (strcmp(name, "window") == 0)
3046 {
3047 /* For current tab window.c does not bother to set or update tp_curwin
3048 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003049 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003050 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003051 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003052 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003053 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003054 else if (strcmp(name, "__members__") == 0)
3055 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003056 return NULL;
3057}
3058
3059 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003060TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003061{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003062 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003063 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003064 else
3065 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003066 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003067
3068 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003069 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3070 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003071 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003072 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003073 }
3074}
3075
3076static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003077 /* name, function, calling, documentation */
3078 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3079 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003080};
3081
3082/*
3083 * Window list object
3084 */
3085
3086static PyTypeObject TabListType;
3087static PySequenceMethods TabListAsSeq;
3088
3089typedef struct
3090{
3091 PyObject_HEAD
3092} TabListObject;
3093
3094 static PyInt
3095TabListLength(PyObject *self UNUSED)
3096{
3097 tabpage_T *tp = first_tabpage;
3098 PyInt n = 0;
3099
3100 while (tp != NULL)
3101 {
3102 ++n;
3103 tp = tp->tp_next;
3104 }
3105
3106 return n;
3107}
3108
3109 static PyObject *
3110TabListItem(PyObject *self UNUSED, PyInt n)
3111{
3112 tabpage_T *tp;
3113
3114 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3115 if (n == 0)
3116 return TabPageNew(tp);
3117
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003118 PyErr_SET_STRING(PyExc_IndexError, "no such tab page");
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003119 return NULL;
3120}
3121
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003122/*
3123 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003124 */
3125
3126typedef struct
3127{
3128 PyObject_HEAD
3129 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003130 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003131} WindowObject;
3132
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003133static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003134
3135 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003136CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003137{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003138 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003139 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003140 PyErr_SET_VIM("attempt to refer to deleted window");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003141 return -1;
3142 }
3143
3144 return 0;
3145}
3146
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003147 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003148WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003149{
3150 /* We need to handle deletion of windows underneath us.
3151 * If we add a "w_python*_ref" field to the win_T structure,
3152 * then we can get at it in win_free() in vim. We then
3153 * need to create only ONE Python object per window - if
3154 * we try to create a second, just INCREF the existing one
3155 * and return it. The (single) Python object referring to
3156 * the window is stored in "w_python*_ref".
3157 * On a win_free() we set the Python object's win_T* field
3158 * to an invalid value. We trap all uses of a window
3159 * object, and reject them if the win_T* field is invalid.
3160 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003161 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003162 * w_python_ref and w_python3_ref fields respectively.
3163 */
3164
3165 WindowObject *self;
3166
3167 if (WIN_PYTHON_REF(win))
3168 {
3169 self = WIN_PYTHON_REF(win);
3170 Py_INCREF(self);
3171 }
3172 else
3173 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003174 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003175 if (self == NULL)
3176 return NULL;
3177 self->win = win;
3178 WIN_PYTHON_REF(win) = self;
3179 }
3180
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003181 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3182
Bram Moolenaar971db462013-05-12 18:44:48 +02003183 return (PyObject *)(self);
3184}
3185
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003186 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003187WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003188{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003189 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003190 if (self->win && self->win != INVALID_WINDOW_VALUE)
3191 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003192 Py_XDECREF(((PyObject *)(self->tabObject)));
3193 PyObject_GC_Del((void *)(self));
3194}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003195
Bram Moolenaar774267b2013-05-21 20:51:59 +02003196 static int
3197WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3198{
3199 Py_VISIT(((PyObject *)(self->tabObject)));
3200 return 0;
3201}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003202
Bram Moolenaar774267b2013-05-21 20:51:59 +02003203 static int
3204WindowClear(WindowObject *self)
3205{
3206 Py_CLEAR(self->tabObject);
3207 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003208}
3209
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003210 static win_T *
3211get_firstwin(TabPageObject *tabObject)
3212{
3213 if (tabObject)
3214 {
3215 if (CheckTabPage(tabObject))
3216 return NULL;
3217 /* For current tab window.c does not bother to set or update tp_firstwin
3218 */
3219 else if (tabObject->tab == curtab)
3220 return firstwin;
3221 else
3222 return tabObject->tab->tp_firstwin;
3223 }
3224 else
3225 return firstwin;
3226}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003227static char *WindowAttrs[] = {
3228 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3229 "tabpage", "valid",
3230 NULL
3231};
3232
3233 static PyObject *
3234WindowDir(PyObject *self)
3235{
3236 return ObjectDir(self, WindowAttrs);
3237}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003238
Bram Moolenaar971db462013-05-12 18:44:48 +02003239 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003240WindowAttrValid(WindowObject *self, char *name)
3241{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003242 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003243
3244 if (strcmp(name, "valid") != 0)
3245 return NULL;
3246
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003247 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3248 Py_INCREF(ret);
3249 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003250}
3251
3252 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003253WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003254{
3255 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003256 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003257 else if (strcmp(name, "cursor") == 0)
3258 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003259 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003260
3261 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3262 }
3263 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003264 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003265#ifdef FEAT_WINDOWS
3266 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003267 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003268#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003269#ifdef FEAT_VERTSPLIT
3270 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003271 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003272 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003273 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003274#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003275 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003276 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003277 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003278 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3279 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003280 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003281 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003282 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003283 return NULL;
3284 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003285 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003286 }
3287 else if (strcmp(name, "tabpage") == 0)
3288 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003289 Py_INCREF(self->tabObject);
3290 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003291 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003292 else if (strcmp(name, "__members__") == 0)
3293 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003294 else
3295 return NULL;
3296}
3297
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003298 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003299WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003300{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003301 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003302 return -1;
3303
3304 if (strcmp(name, "buffer") == 0)
3305 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003306 PyErr_SET_STRING(PyExc_TypeError, "readonly attribute: buffer");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003307 return -1;
3308 }
3309 else if (strcmp(name, "cursor") == 0)
3310 {
3311 long lnum;
3312 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003313
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003314 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003315 return -1;
3316
Bram Moolenaard6e39182013-05-21 18:30:34 +02003317 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003318 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003319 PyErr_SET_VIM("cursor position outside buffer");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003320 return -1;
3321 }
3322
3323 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003324 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003325 return -1;
3326
Bram Moolenaard6e39182013-05-21 18:30:34 +02003327 self->win->w_cursor.lnum = lnum;
3328 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003329#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003330 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003331#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003332 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003334
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003335 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003336 return 0;
3337 }
3338 else if (strcmp(name, "height") == 0)
3339 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003340 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003341 win_T *savewin;
3342
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003343 if (NumberToLong(valObject, &height, NUMBER_INT))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003344 return -1;
3345
3346#ifdef FEAT_GUI
3347 need_mouse_correct = TRUE;
3348#endif
3349 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003350 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003351
3352 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003353 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003354 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003355 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003356 return -1;
3357
3358 return 0;
3359 }
3360#ifdef FEAT_VERTSPLIT
3361 else if (strcmp(name, "width") == 0)
3362 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003363 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003364 win_T *savewin;
3365
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003366 if (NumberToLong(valObject, &width, NUMBER_INT))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003367 return -1;
3368
3369#ifdef FEAT_GUI
3370 need_mouse_correct = TRUE;
3371#endif
3372 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003373 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003374
3375 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003376 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003377 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003378 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003379 return -1;
3380
3381 return 0;
3382 }
3383#endif
3384 else
3385 {
3386 PyErr_SetString(PyExc_AttributeError, name);
3387 return -1;
3388 }
3389}
3390
3391 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003392WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003393{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003394 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003395 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003396 else
3397 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003398 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003399
Bram Moolenaar6d216452013-05-12 19:00:41 +02003400 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003401 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003402 (self));
3403 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003404 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003405 }
3406}
3407
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003408static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003409 /* name, function, calling, documentation */
3410 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3411 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003412};
3413
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003414/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003415 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003416 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003417
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003418static PyTypeObject WinListType;
3419static PySequenceMethods WinListAsSeq;
3420
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003421typedef struct
3422{
3423 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003424 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003425} WinListObject;
3426
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003427 static PyObject *
3428WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003429{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003430 WinListObject *self;
3431
3432 self = PyObject_NEW(WinListObject, &WinListType);
3433 self->tabObject = tabObject;
3434 Py_INCREF(tabObject);
3435
3436 return (PyObject *)(self);
3437}
3438
3439 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003440WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003441{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003442 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003443
3444 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003445 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003446 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003447 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003448
3449 DESTRUCTOR_FINISH(self);
3450}
3451
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003452 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003453WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003454{
3455 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003456 PyInt n = 0;
3457
Bram Moolenaard6e39182013-05-21 18:30:34 +02003458 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003459 return -1;
3460
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003461 while (w != NULL)
3462 {
3463 ++n;
3464 w = W_NEXT(w);
3465 }
3466
3467 return n;
3468}
3469
3470 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003471WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003472{
3473 win_T *w;
3474
Bram Moolenaard6e39182013-05-21 18:30:34 +02003475 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003476 return NULL;
3477
3478 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003479 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003480 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003481
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003482 PyErr_SET_STRING(PyExc_IndexError, "no such window");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003483 return NULL;
3484}
3485
3486/* Convert a Python string into a Vim line.
3487 *
3488 * The result is in allocated memory. All internal nulls are replaced by
3489 * newline characters. It is an error for the string to contain newline
3490 * characters.
3491 *
3492 * On errors, the Python exception data is set, and NULL is returned.
3493 */
3494 static char *
3495StringToLine(PyObject *obj)
3496{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003497 char *str;
3498 char *save;
3499 PyObject *bytes = NULL;
3500 Py_ssize_t len;
3501 PyInt i;
3502 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003503
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003504 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003505 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003506 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3507 || str == NULL)
3508 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003509 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003510 else if (PyUnicode_Check(obj))
3511 {
3512 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3513 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003514
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003515 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3516 || str == NULL)
3517 {
3518 Py_DECREF(bytes);
3519 return NULL;
3520 }
3521 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003522
3523 /*
3524 * Error checking: String must not contain newlines, as we
3525 * are replacing a single line, and we must replace it with
3526 * a single line.
3527 * A trailing newline is removed, so that append(f.readlines()) works.
3528 */
3529 p = memchr(str, '\n', len);
3530 if (p != NULL)
3531 {
3532 if (p == str + len - 1)
3533 --len;
3534 else
3535 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003536 PyErr_SET_VIM("string cannot contain newlines");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003537 return NULL;
3538 }
3539 }
3540
3541 /* Create a copy of the string, with internal nulls replaced by
3542 * newline characters, as is the vim convention.
3543 */
3544 save = (char *)alloc((unsigned)(len+1));
3545 if (save == NULL)
3546 {
3547 PyErr_NoMemory();
3548 return NULL;
3549 }
3550
3551 for (i = 0; i < len; ++i)
3552 {
3553 if (str[i] == '\0')
3554 save[i] = '\n';
3555 else
3556 save[i] = str[i];
3557 }
3558
3559 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003560 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003561
3562 return save;
3563}
3564
3565/* Get a line from the specified buffer. The line number is
3566 * in Vim format (1-based). The line is returned as a Python
3567 * string object.
3568 */
3569 static PyObject *
3570GetBufferLine(buf_T *buf, PyInt n)
3571{
3572 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3573}
3574
3575
3576/* Get a list of lines from the specified buffer. The line numbers
3577 * are in Vim format (1-based). The range is from lo up to, but not
3578 * including, hi. The list is returned as a Python list of string objects.
3579 */
3580 static PyObject *
3581GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3582{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003583 PyInt i;
3584 PyInt n = hi - lo;
3585 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003586
3587 if (list == NULL)
3588 return NULL;
3589
3590 for (i = 0; i < n; ++i)
3591 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003592 PyObject *string = LineToString(
3593 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003594
3595 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003596 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003597 {
3598 Py_DECREF(list);
3599 return NULL;
3600 }
3601
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003602 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003603 }
3604
3605 /* The ownership of the Python list is passed to the caller (ie,
3606 * the caller should Py_DECREF() the object when it is finished
3607 * with it).
3608 */
3609
3610 return list;
3611}
3612
3613/*
3614 * Check if deleting lines made the cursor position invalid.
3615 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3616 * deleted).
3617 */
3618 static void
3619py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3620{
3621 if (curwin->w_cursor.lnum >= lo)
3622 {
3623 /* Adjust the cursor position if it's in/after the changed
3624 * lines. */
3625 if (curwin->w_cursor.lnum >= hi)
3626 {
3627 curwin->w_cursor.lnum += extra;
3628 check_cursor_col();
3629 }
3630 else if (extra < 0)
3631 {
3632 curwin->w_cursor.lnum = lo;
3633 check_cursor();
3634 }
3635 else
3636 check_cursor_col();
3637 changed_cline_bef_curs();
3638 }
3639 invalidate_botline();
3640}
3641
Bram Moolenaar19e60942011-06-19 00:27:51 +02003642/*
3643 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003644 * in Vim format (1-based). The replacement line is given as
3645 * a Python string object. The object is checked for validity
3646 * and correct format. Errors are returned as a value of FAIL.
3647 * The return value is OK on success.
3648 * If OK is returned and len_change is not NULL, *len_change
3649 * is set to the change in the buffer length.
3650 */
3651 static int
3652SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3653{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003654 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003655 * There are three cases:
3656 * 1. NULL, or None - this is a deletion.
3657 * 2. A string - this is a replacement.
3658 * 3. Anything else - this is an error.
3659 */
3660 if (line == Py_None || line == NULL)
3661 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003662 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003663
3664 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003665 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003666
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003667 VimTryStart();
3668
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003669 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003670 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003671 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003672 RAISE_LINE_FAIL("delete");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003673 else
3674 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003675 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003676 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3677 deleted_lines_mark((linenr_T)n, 1L);
3678 }
3679
Bram Moolenaar105bc352013-05-17 16:03:57 +02003680 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003681
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003682 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003683 return FAIL;
3684
3685 if (len_change)
3686 *len_change = -1;
3687
3688 return OK;
3689 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003690 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003692 char *save = StringToLine(line);
3693 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003694
3695 if (save == NULL)
3696 return FAIL;
3697
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003698 VimTryStart();
3699
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003700 /* We do not need to free "save" if ml_replace() consumes it. */
3701 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003702 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003703
3704 if (u_savesub((linenr_T)n) == FAIL)
3705 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003706 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003707 vim_free(save);
3708 }
3709 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3710 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003711 RAISE_LINE_FAIL("replace");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003712 vim_free(save);
3713 }
3714 else
3715 changed_bytes((linenr_T)n, 0);
3716
Bram Moolenaar105bc352013-05-17 16:03:57 +02003717 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718
3719 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003720 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721 check_cursor_col();
3722
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003723 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003724 return FAIL;
3725
3726 if (len_change)
3727 *len_change = 0;
3728
3729 return OK;
3730 }
3731 else
3732 {
3733 PyErr_BadArgument();
3734 return FAIL;
3735 }
3736}
3737
Bram Moolenaar19e60942011-06-19 00:27:51 +02003738/* Replace a range of lines in the specified buffer. The line numbers are in
3739 * Vim format (1-based). The range is from lo up to, but not including, hi.
3740 * The replacement lines are given as a Python list of string objects. The
3741 * list is checked for validity and correct format. Errors are returned as a
3742 * value of FAIL. The return value is OK on success.
3743 * If OK is returned and len_change is not NULL, *len_change
3744 * is set to the change in the buffer length.
3745 */
3746 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003747SetBufferLineList(
3748 buf_T *buf,
3749 PyInt lo,
3750 PyInt hi,
3751 PyObject *list,
3752 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003753{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003754 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003755 * There are three cases:
3756 * 1. NULL, or None - this is a deletion.
3757 * 2. A list - this is a replacement.
3758 * 3. Anything else - this is an error.
3759 */
3760 if (list == Py_None || list == NULL)
3761 {
3762 PyInt i;
3763 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003764 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003765
3766 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003767 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003768 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003769
3770 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003771 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003772 else
3773 {
3774 for (i = 0; i < n; ++i)
3775 {
3776 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3777 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003778 RAISE_LINE_FAIL("delete");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003779 break;
3780 }
3781 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003782 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003783 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3784 deleted_lines_mark((linenr_T)lo, (long)i);
3785 }
3786
Bram Moolenaar105bc352013-05-17 16:03:57 +02003787 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003788
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003789 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003790 return FAIL;
3791
3792 if (len_change)
3793 *len_change = -n;
3794
3795 return OK;
3796 }
3797 else if (PyList_Check(list))
3798 {
3799 PyInt i;
3800 PyInt new_len = PyList_Size(list);
3801 PyInt old_len = hi - lo;
3802 PyInt extra = 0; /* lines added to text, can be negative */
3803 char **array;
3804 buf_T *savebuf;
3805
3806 if (new_len == 0) /* avoid allocating zero bytes */
3807 array = NULL;
3808 else
3809 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003810 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003811 if (array == NULL)
3812 {
3813 PyErr_NoMemory();
3814 return FAIL;
3815 }
3816 }
3817
3818 for (i = 0; i < new_len; ++i)
3819 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003820 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003821
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003822 if (!(line = PyList_GetItem(list, i)) ||
3823 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003824 {
3825 while (i)
3826 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003827 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003828 return FAIL;
3829 }
3830 }
3831
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003832 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003833 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003834
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003835 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003836 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003837
3838 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003839 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003840
3841 /* If the size of the range is reducing (ie, new_len < old_len) we
3842 * need to delete some old_len. We do this at the start, by
3843 * repeatedly deleting line "lo".
3844 */
3845 if (!PyErr_Occurred())
3846 {
3847 for (i = 0; i < old_len - new_len; ++i)
3848 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3849 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003850 RAISE_LINE_FAIL("delete");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003851 break;
3852 }
3853 extra -= i;
3854 }
3855
3856 /* For as long as possible, replace the existing old_len with the
3857 * new old_len. This is a more efficient operation, as it requires
3858 * less memory allocation and freeing.
3859 */
3860 if (!PyErr_Occurred())
3861 {
3862 for (i = 0; i < old_len && i < new_len; ++i)
3863 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3864 == FAIL)
3865 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003866 RAISE_LINE_FAIL("replace");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003867 break;
3868 }
3869 }
3870 else
3871 i = 0;
3872
3873 /* Now we may need to insert the remaining new old_len. If we do, we
3874 * must free the strings as we finish with them (we can't pass the
3875 * responsibility to vim in this case).
3876 */
3877 if (!PyErr_Occurred())
3878 {
3879 while (i < new_len)
3880 {
3881 if (ml_append((linenr_T)(lo + i - 1),
3882 (char_u *)array[i], 0, FALSE) == FAIL)
3883 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003884 RAISE_LINE_FAIL("insert");
Bram Moolenaar19e60942011-06-19 00:27:51 +02003885 break;
3886 }
3887 vim_free(array[i]);
3888 ++i;
3889 ++extra;
3890 }
3891 }
3892
3893 /* Free any left-over old_len, as a result of an error */
3894 while (i < new_len)
3895 {
3896 vim_free(array[i]);
3897 ++i;
3898 }
3899
3900 /* Free the array of old_len. All of its contents have now
3901 * been dealt with (either freed, or the responsibility passed
3902 * to vim.
3903 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003904 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003905
3906 /* Adjust marks. Invalidate any which lie in the
3907 * changed range, and move any in the remainder of the buffer.
3908 */
3909 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3910 (long)MAXLNUM, (long)extra);
3911 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3912
Bram Moolenaar105bc352013-05-17 16:03:57 +02003913 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003914 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3915
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003916 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003917 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003918
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003919 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003920 return FAIL;
3921
3922 if (len_change)
3923 *len_change = new_len - old_len;
3924
3925 return OK;
3926 }
3927 else
3928 {
3929 PyErr_BadArgument();
3930 return FAIL;
3931 }
3932}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003933
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003934/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003935 * The line number is in Vim format (1-based). The lines to be inserted are
3936 * given as a Python list of string objects or as a single string. The lines
3937 * to be added are checked for validity and correct format. Errors are
3938 * returned as a value of FAIL. The return value is OK on success.
3939 * If OK is returned and len_change is not NULL, *len_change
3940 * is set to the change in the buffer length.
3941 */
3942 static int
3943InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3944{
3945 /* First of all, we check the type of the supplied Python object.
3946 * It must be a string or a list, or the call is in error.
3947 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003948 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003949 {
3950 char *str = StringToLine(lines);
3951 buf_T *savebuf;
3952
3953 if (str == NULL)
3954 return FAIL;
3955
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003956 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003957 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003958 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003959
3960 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003961 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003962 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003963 RAISE_LINE_FAIL("insert");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003964 else
3965 appended_lines_mark((linenr_T)n, 1L);
3966
3967 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003968 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003969 update_screen(VALID);
3970
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003971 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972 return FAIL;
3973
3974 if (len_change)
3975 *len_change = 1;
3976
3977 return OK;
3978 }
3979 else if (PyList_Check(lines))
3980 {
3981 PyInt i;
3982 PyInt size = PyList_Size(lines);
3983 char **array;
3984 buf_T *savebuf;
3985
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003986 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003987 if (array == NULL)
3988 {
3989 PyErr_NoMemory();
3990 return FAIL;
3991 }
3992
3993 for (i = 0; i < size; ++i)
3994 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003995 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003996
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003997 if (!(line = PyList_GetItem(lines, i)) ||
3998 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003999 {
4000 while (i)
4001 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004002 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003 return FAIL;
4004 }
4005 }
4006
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004007 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004008 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004009 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010
4011 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004012 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004013 else
4014 {
4015 for (i = 0; i < size; ++i)
4016 {
4017 if (ml_append((linenr_T)(n + i),
4018 (char_u *)array[i], 0, FALSE) == FAIL)
4019 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004020 RAISE_LINE_FAIL("insert");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004021
4022 /* Free the rest of the lines */
4023 while (i < size)
4024 vim_free(array[i++]);
4025
4026 break;
4027 }
4028 vim_free(array[i]);
4029 }
4030 if (i > 0)
4031 appended_lines_mark((linenr_T)n, (long)i);
4032 }
4033
4034 /* Free the array of lines. All of its contents have now
4035 * been freed.
4036 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004037 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038
Bram Moolenaar105bc352013-05-17 16:03:57 +02004039 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004040 update_screen(VALID);
4041
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004042 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043 return FAIL;
4044
4045 if (len_change)
4046 *len_change = size;
4047
4048 return OK;
4049 }
4050 else
4051 {
4052 PyErr_BadArgument();
4053 return FAIL;
4054 }
4055}
4056
4057/*
4058 * Common routines for buffers and line ranges
4059 * -------------------------------------------
4060 */
4061
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004062typedef struct
4063{
4064 PyObject_HEAD
4065 buf_T *buf;
4066} BufferObject;
4067
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004069CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004071 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004072 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004073 PyErr_SET_VIM("attempt to refer to deleted buffer");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 return -1;
4075 }
4076
4077 return 0;
4078}
4079
4080 static PyObject *
4081RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4082{
4083 if (CheckBuffer(self))
4084 return NULL;
4085
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004086 if (end == -1)
4087 end = self->buf->b_ml.ml_line_count;
4088
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004089 if (n < 0)
4090 n += end - start + 1;
4091
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 if (n < 0 || n > end - start)
4093 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004094 PyErr_SET_STRING(PyExc_IndexError, "line number out of range");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 return NULL;
4096 }
4097
4098 return GetBufferLine(self->buf, n+start);
4099}
4100
4101 static PyObject *
4102RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4103{
4104 PyInt size;
4105
4106 if (CheckBuffer(self))
4107 return NULL;
4108
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004109 if (end == -1)
4110 end = self->buf->b_ml.ml_line_count;
4111
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 size = end - start + 1;
4113
4114 if (lo < 0)
4115 lo = 0;
4116 else if (lo > size)
4117 lo = size;
4118 if (hi < 0)
4119 hi = 0;
4120 if (hi < lo)
4121 hi = lo;
4122 else if (hi > size)
4123 hi = size;
4124
4125 return GetBufferLineList(self->buf, lo+start, hi+start);
4126}
4127
4128 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004129RBAsItem(
4130 BufferObject *self,
4131 PyInt n,
4132 PyObject *valObject,
4133 PyInt start,
4134 PyInt end,
4135 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136{
4137 PyInt len_change;
4138
4139 if (CheckBuffer(self))
4140 return -1;
4141
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004142 if (end == -1)
4143 end = self->buf->b_ml.ml_line_count;
4144
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004145 if (n < 0)
4146 n += end - start + 1;
4147
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004148 if (n < 0 || n > end - start)
4149 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004150 PyErr_SET_STRING(PyExc_IndexError, "line number out of range");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004151 return -1;
4152 }
4153
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004154 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004155 return -1;
4156
4157 if (new_end)
4158 *new_end = end + len_change;
4159
4160 return 0;
4161}
4162
Bram Moolenaar19e60942011-06-19 00:27:51 +02004163 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004164RBAsSlice(
4165 BufferObject *self,
4166 PyInt lo,
4167 PyInt hi,
4168 PyObject *valObject,
4169 PyInt start,
4170 PyInt end,
4171 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004172{
4173 PyInt size;
4174 PyInt len_change;
4175
4176 /* Self must be a valid buffer */
4177 if (CheckBuffer(self))
4178 return -1;
4179
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004180 if (end == -1)
4181 end = self->buf->b_ml.ml_line_count;
4182
Bram Moolenaar19e60942011-06-19 00:27:51 +02004183 /* Sort out the slice range */
4184 size = end - start + 1;
4185
4186 if (lo < 0)
4187 lo = 0;
4188 else if (lo > size)
4189 lo = size;
4190 if (hi < 0)
4191 hi = 0;
4192 if (hi < lo)
4193 hi = lo;
4194 else if (hi > size)
4195 hi = size;
4196
4197 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004198 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004199 return -1;
4200
4201 if (new_end)
4202 *new_end = end + len_change;
4203
4204 return 0;
4205}
4206
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004207
4208 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004209RBAppend(
4210 BufferObject *self,
4211 PyObject *args,
4212 PyInt start,
4213 PyInt end,
4214 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004215{
4216 PyObject *lines;
4217 PyInt len_change;
4218 PyInt max;
4219 PyInt n;
4220
4221 if (CheckBuffer(self))
4222 return NULL;
4223
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004224 if (end == -1)
4225 end = self->buf->b_ml.ml_line_count;
4226
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004227 max = n = end - start + 1;
4228
4229 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4230 return NULL;
4231
4232 if (n < 0 || n > max)
4233 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004234 PyErr_SET_STRING(PyExc_IndexError, "line number out of range");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004235 return NULL;
4236 }
4237
4238 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4239 return NULL;
4240
4241 if (new_end)
4242 *new_end = end + len_change;
4243
4244 Py_INCREF(Py_None);
4245 return Py_None;
4246}
4247
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004248/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004249 */
4250
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004251static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004252static PySequenceMethods RangeAsSeq;
4253static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004254
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004255typedef struct
4256{
4257 PyObject_HEAD
4258 BufferObject *buf;
4259 PyInt start;
4260 PyInt end;
4261} RangeObject;
4262
4263 static PyObject *
4264RangeNew(buf_T *buf, PyInt start, PyInt end)
4265{
4266 BufferObject *bufr;
4267 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004268 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269 if (self == NULL)
4270 return NULL;
4271
4272 bufr = (BufferObject *)BufferNew(buf);
4273 if (bufr == NULL)
4274 {
4275 Py_DECREF(self);
4276 return NULL;
4277 }
4278 Py_INCREF(bufr);
4279
4280 self->buf = bufr;
4281 self->start = start;
4282 self->end = end;
4283
4284 return (PyObject *)(self);
4285}
4286
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004287 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004288RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004289{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004290 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004291 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004292 PyObject_GC_Del((void *)(self));
4293}
4294
4295 static int
4296RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4297{
4298 Py_VISIT(((PyObject *)(self->buf)));
4299 return 0;
4300}
4301
4302 static int
4303RangeClear(RangeObject *self)
4304{
4305 Py_CLEAR(self->buf);
4306 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004307}
4308
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004309 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004310RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004311{
4312 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004313 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004314 return -1; /* ??? */
4315
Bram Moolenaard6e39182013-05-21 18:30:34 +02004316 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004317}
4318
4319 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004320RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004321{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004322 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004323}
4324
4325 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004326RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004327{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004328 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004329}
4330
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004331static char *RangeAttrs[] = {
4332 "start", "end",
4333 NULL
4334};
4335
4336 static PyObject *
4337RangeDir(PyObject *self)
4338{
4339 return ObjectDir(self, RangeAttrs);
4340}
4341
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004342 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004343RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004344{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004345 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004346}
4347
4348 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004349RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004350{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004351 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004352 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4353 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004354 else
4355 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004356 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004357
4358 if (name == NULL)
4359 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004360
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004361 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004362 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004363 }
4364}
4365
4366static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004367 /* name, function, calling, documentation */
4368 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004369 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4370 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004371};
4372
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004373static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004374static PySequenceMethods BufferAsSeq;
4375static PyMappingMethods BufferAsMapping;
4376
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004377 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004378BufferNew(buf_T *buf)
4379{
4380 /* We need to handle deletion of buffers underneath us.
4381 * If we add a "b_python*_ref" field to the buf_T structure,
4382 * then we can get at it in buf_freeall() in vim. We then
4383 * need to create only ONE Python object per buffer - if
4384 * we try to create a second, just INCREF the existing one
4385 * and return it. The (single) Python object referring to
4386 * the buffer is stored in "b_python*_ref".
4387 * Question: what to do on a buf_freeall(). We'll probably
4388 * have to either delete the Python object (DECREF it to
4389 * zero - a bad idea, as it leaves dangling refs!) or
4390 * set the buf_T * value to an invalid value (-1?), which
4391 * means we need checks in all access functions... Bah.
4392 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004393 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004394 * b_python_ref and b_python3_ref fields respectively.
4395 */
4396
4397 BufferObject *self;
4398
4399 if (BUF_PYTHON_REF(buf) != NULL)
4400 {
4401 self = BUF_PYTHON_REF(buf);
4402 Py_INCREF(self);
4403 }
4404 else
4405 {
4406 self = PyObject_NEW(BufferObject, &BufferType);
4407 if (self == NULL)
4408 return NULL;
4409 self->buf = buf;
4410 BUF_PYTHON_REF(buf) = self;
4411 }
4412
4413 return (PyObject *)(self);
4414}
4415
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004416 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004417BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004418{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004419 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4420 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004421
4422 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004423}
4424
Bram Moolenaar971db462013-05-12 18:44:48 +02004425 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004426BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004427{
4428 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004429 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004430 return -1; /* ??? */
4431
Bram Moolenaard6e39182013-05-21 18:30:34 +02004432 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004433}
4434
4435 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004436BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004437{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004438 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004439}
4440
4441 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004442BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004443{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004444 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004445}
4446
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004447static char *BufferAttrs[] = {
4448 "name", "number", "vars", "options", "valid",
4449 NULL
4450};
4451
4452 static PyObject *
4453BufferDir(PyObject *self)
4454{
4455 return ObjectDir(self, BufferAttrs);
4456}
4457
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004458 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004459BufferAttrValid(BufferObject *self, char *name)
4460{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004461 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004462
4463 if (strcmp(name, "valid") != 0)
4464 return NULL;
4465
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004466 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4467 Py_INCREF(ret);
4468 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004469}
4470
4471 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004472BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004473{
4474 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004475 return PyString_FromString((self->buf->b_ffname == NULL
4476 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004477 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004478 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004479 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004480 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004481 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004482 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4483 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004484 else if (strcmp(name, "__members__") == 0)
4485 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004486 else
4487 return NULL;
4488}
4489
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004490 static int
4491BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4492{
4493 if (CheckBuffer(self))
4494 return -1;
4495
4496 if (strcmp(name, "name") == 0)
4497 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004498 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004499 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004500 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004501 PyObject *todecref;
4502
4503 if (!(val = StringToChars(valObject, &todecref)))
4504 return -1;
4505
4506 VimTryStart();
4507 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4508 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004509 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004510 aucmd_restbuf(&aco);
4511 Py_XDECREF(todecref);
4512 if (VimTryEnd())
4513 return -1;
4514
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004515 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004516 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004517 PyErr_SET_VIM("failed to rename buffer");
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004518 return -1;
4519 }
4520 return 0;
4521 }
4522 else
4523 {
4524 PyErr_SetString(PyExc_AttributeError, name);
4525 return -1;
4526 }
4527}
4528
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004529 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004530BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004531{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004532 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004533}
4534
4535 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004536BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004537{
4538 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004539 char_u *pmark;
4540 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004541 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004542 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004543
Bram Moolenaard6e39182013-05-21 18:30:34 +02004544 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004545 return NULL;
4546
Bram Moolenaar389a1792013-06-23 13:00:44 +02004547 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004548 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004549
Bram Moolenaar389a1792013-06-23 13:00:44 +02004550 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004551 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004552 PyErr_SET_STRING(PyExc_ValueError,
4553 "mark name must be a single character");
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004554 return NULL;
4555 }
4556
4557 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004558
4559 Py_XDECREF(todecref);
4560
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004561 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004562 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004563 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004564 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004565 if (VimTryEnd())
4566 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004567
4568 if (posp == NULL)
4569 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004570 PyErr_SET_VIM("invalid mark name");
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004571 return NULL;
4572 }
4573
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004574 if (posp->lnum <= 0)
4575 {
4576 /* Or raise an error? */
4577 Py_INCREF(Py_None);
4578 return Py_None;
4579 }
4580
4581 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4582}
4583
4584 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004585BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004586{
4587 PyInt start;
4588 PyInt end;
4589
Bram Moolenaard6e39182013-05-21 18:30:34 +02004590 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004591 return NULL;
4592
4593 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4594 return NULL;
4595
Bram Moolenaard6e39182013-05-21 18:30:34 +02004596 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004597}
4598
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004599 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004600BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004601{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004602 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004603 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004604 else
4605 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004606 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004607
4608 if (name == NULL)
4609 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004610
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004611 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004612 }
4613}
4614
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004615static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004616 /* name, function, calling, documentation */
4617 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004618 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004619 {"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 +02004620 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4621 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004622};
4623
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004624/*
4625 * Buffer list object - Implementation
4626 */
4627
4628static PyTypeObject BufMapType;
4629
4630typedef struct
4631{
4632 PyObject_HEAD
4633} BufMapObject;
4634
4635 static PyInt
4636BufMapLength(PyObject *self UNUSED)
4637{
4638 buf_T *b = firstbuf;
4639 PyInt n = 0;
4640
4641 while (b)
4642 {
4643 ++n;
4644 b = b->b_next;
4645 }
4646
4647 return n;
4648}
4649
4650 static PyObject *
4651BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4652{
4653 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004654 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004655
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004656 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004657 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004658
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004659 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004660
4661 if (b)
4662 return BufferNew(b);
4663 else
4664 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004665 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004666 return NULL;
4667 }
4668}
4669
4670 static void
4671BufMapIterDestruct(PyObject *buffer)
4672{
4673 /* Iteration was stopped before all buffers were processed */
4674 if (buffer)
4675 {
4676 Py_DECREF(buffer);
4677 }
4678}
4679
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004680 static int
4681BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4682{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004683 if (buffer)
4684 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004685 return 0;
4686}
4687
4688 static int
4689BufMapIterClear(PyObject **buffer)
4690{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004691 if (*buffer)
4692 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004693 return 0;
4694}
4695
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004696 static PyObject *
4697BufMapIterNext(PyObject **buffer)
4698{
4699 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004700 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004701
4702 if (!*buffer)
4703 return NULL;
4704
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004705 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004706
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004707 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004708 {
4709 *buffer = NULL;
4710 return NULL;
4711 }
4712
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004713 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004714 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004715 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004716 return NULL;
4717 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004718 /* Do not increment reference: we no longer hold it (decref), but whoever
4719 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004720 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004721}
4722
4723 static PyObject *
4724BufMapIter(PyObject *self UNUSED)
4725{
4726 PyObject *buffer;
4727
4728 buffer = BufferNew(firstbuf);
4729 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004730 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4731 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004732}
4733
4734static PyMappingMethods BufMapAsMapping = {
4735 (lenfunc) BufMapLength,
4736 (binaryfunc) BufMapItem,
4737 (objobjargproc) 0,
4738};
4739
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004740/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741 */
4742
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004743static char *CurrentAttrs[] = {
4744 "buffer", "window", "line", "range", "tabpage",
4745 NULL
4746};
4747
4748 static PyObject *
4749CurrentDir(PyObject *self)
4750{
4751 return ObjectDir(self, CurrentAttrs);
4752}
4753
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004754 static PyObject *
4755CurrentGetattr(PyObject *self UNUSED, char *name)
4756{
4757 if (strcmp(name, "buffer") == 0)
4758 return (PyObject *)BufferNew(curbuf);
4759 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004760 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004761 else if (strcmp(name, "tabpage") == 0)
4762 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004763 else if (strcmp(name, "line") == 0)
4764 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4765 else if (strcmp(name, "range") == 0)
4766 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004767 else if (strcmp(name, "__members__") == 0)
4768 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004769 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004770#if PY_MAJOR_VERSION < 3
4771 return Py_FindMethod(WindowMethods, self, name);
4772#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004773 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004774#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004775}
4776
4777 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004778CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004779{
4780 if (strcmp(name, "line") == 0)
4781 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004782 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4783 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004784 return -1;
4785
4786 return 0;
4787 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004788 else if (strcmp(name, "buffer") == 0)
4789 {
4790 int count;
4791
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004792 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004793 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004794 PyErr_FORMAT(PyExc_TypeError,
4795 "expected vim.Buffer object, but got %s",
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004796 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004797 return -1;
4798 }
4799
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004800 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004801 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004802 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004803
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004804 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004805 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4806 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004807 if (VimTryEnd())
4808 return -1;
Bram Moolenaarc476e522013-06-23 13:46:40 +02004809 PyErr_VIM_FORMAT("failed to switch to buffer %d", count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004810 return -1;
4811 }
4812
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004813 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004814 }
4815 else if (strcmp(name, "window") == 0)
4816 {
4817 int count;
4818
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004819 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004820 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004821 PyErr_FORMAT(PyExc_TypeError,
4822 "expected vim.Window object, but got %s",
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004823 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004824 return -1;
4825 }
4826
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004827 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004828 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004829 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004830
4831 if (!count)
4832 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004833 PyErr_SET_STRING(PyExc_ValueError,
4834 "failed to find window in the current tab page");
Bram Moolenaare7614592013-05-15 15:51:08 +02004835 return -1;
4836 }
4837
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004838 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004839 win_goto(((WindowObject *)(valObject))->win);
4840 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004841 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004842 if (VimTryEnd())
4843 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004844 PyErr_SET_STRING(PyExc_RuntimeError,
4845 "did not switch to the specified window");
Bram Moolenaare7614592013-05-15 15:51:08 +02004846 return -1;
4847 }
4848
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004849 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004850 }
4851 else if (strcmp(name, "tabpage") == 0)
4852 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004853 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004854 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004855 PyErr_FORMAT(PyExc_TypeError,
4856 "expected vim.TabPage object, but got %s",
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004857 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004858 return -1;
4859 }
4860
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004861 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004862 return -1;
4863
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004864 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004865 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4866 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004867 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004868 if (VimTryEnd())
4869 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004870 PyErr_SET_STRING(PyExc_RuntimeError,
4871 "did not switch to the specified tab page");
Bram Moolenaare7614592013-05-15 15:51:08 +02004872 return -1;
4873 }
4874
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004875 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004876 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004877 else
4878 {
4879 PyErr_SetString(PyExc_AttributeError, name);
4880 return -1;
4881 }
4882}
4883
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004884static struct PyMethodDef CurrentMethods[] = {
4885 /* name, function, calling, documentation */
4886 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4887 { NULL, NULL, 0, NULL}
4888};
4889
Bram Moolenaardb913952012-06-29 12:54:53 +02004890 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004891init_range_cmd(exarg_T *eap)
4892{
4893 RangeStart = eap->line1;
4894 RangeEnd = eap->line2;
4895}
4896
4897 static void
4898init_range_eval(typval_T *rettv UNUSED)
4899{
4900 RangeStart = (PyInt) curwin->w_cursor.lnum;
4901 RangeEnd = RangeStart;
4902}
4903
4904 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004905run_cmd(const char *cmd, void *arg UNUSED
4906#ifdef PY_CAN_RECURSE
4907 , PyGILState_STATE *pygilstate UNUSED
4908#endif
4909 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004910{
4911 PyRun_SimpleString((char *) cmd);
4912}
4913
4914static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4915static int code_hdr_len = 30;
4916
4917 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004918run_do(const char *cmd, void *arg UNUSED
4919#ifdef PY_CAN_RECURSE
4920 , PyGILState_STATE *pygilstate
4921#endif
4922 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004923{
4924 PyInt lnum;
4925 size_t len;
4926 char *code;
4927 int status;
4928 PyObject *pyfunc, *pymain;
4929
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004930 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004931 {
4932 EMSG(_("cannot save undo information"));
4933 return;
4934 }
4935
4936 len = code_hdr_len + STRLEN(cmd);
4937 code = PyMem_New(char, len + 1);
4938 memcpy(code, code_hdr, code_hdr_len);
4939 STRCPY(code + code_hdr_len, cmd);
4940 status = PyRun_SimpleString(code);
4941 PyMem_Free(code);
4942
4943 if (status)
4944 {
4945 EMSG(_("failed to run the code"));
4946 return;
4947 }
4948
4949 status = 0;
4950 pymain = PyImport_AddModule("__main__");
4951 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004952#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004953 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004954#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004955
4956 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4957 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004958 PyObject *line;
4959 PyObject *linenr;
4960 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004961
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004962#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004963 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004964#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004965 if (!(line = GetBufferLine(curbuf, lnum)))
4966 goto err;
4967 if (!(linenr = PyInt_FromLong((long) lnum)))
4968 {
4969 Py_DECREF(line);
4970 goto err;
4971 }
4972 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4973 Py_DECREF(line);
4974 Py_DECREF(linenr);
4975 if (!ret)
4976 goto err;
4977
4978 if (ret != Py_None)
4979 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
4980 goto err;
4981
4982 Py_XDECREF(ret);
4983 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004984#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004985 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004986#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004987 }
4988 goto out;
4989err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004990#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004991 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004992#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004993 PyErr_PrintEx(0);
4994 PythonIO_Flush();
4995 status = 1;
4996out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004997#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004998 if (!status)
4999 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005000#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005001 Py_DECREF(pyfunc);
5002 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5003 if (status)
5004 return;
5005 check_cursor();
5006 update_curbuf(NOT_VALID);
5007}
5008
5009 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005010run_eval(const char *cmd, typval_T *rettv
5011#ifdef PY_CAN_RECURSE
5012 , PyGILState_STATE *pygilstate UNUSED
5013#endif
5014 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005015{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005016 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005017
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005018 run_ret = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
5019 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005020 {
5021 if (PyErr_Occurred() && !msg_silent)
5022 PyErr_PrintEx(0);
5023 EMSG(_("E858: Eval did not return a valid python object"));
5024 }
5025 else
5026 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005027 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005028 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005029 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005030 }
5031 PyErr_Clear();
5032}
5033
5034 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005035set_ref_in_py(const int copyID)
5036{
5037 pylinkedlist_T *cur;
5038 dict_T *dd;
5039 list_T *ll;
5040
5041 if (lastdict != NULL)
5042 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5043 {
5044 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5045 if (dd->dv_copyID != copyID)
5046 {
5047 dd->dv_copyID = copyID;
5048 set_ref_in_ht(&dd->dv_hashtab, copyID);
5049 }
5050 }
5051
5052 if (lastlist != NULL)
5053 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5054 {
5055 ll = ((ListObject *) (cur->pll_obj))->list;
5056 if (ll->lv_copyID != copyID)
5057 {
5058 ll->lv_copyID = copyID;
5059 set_ref_in_list(ll, copyID);
5060 }
5061 }
5062}
5063
5064 static int
5065set_string_copy(char_u *str, typval_T *tv)
5066{
5067 tv->vval.v_string = vim_strsave(str);
5068 if (tv->vval.v_string == NULL)
5069 {
5070 PyErr_NoMemory();
5071 return -1;
5072 }
5073 return 0;
5074}
5075
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005076 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005077pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005078{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005079 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005080 char_u *key;
5081 dictitem_T *di;
5082 PyObject *keyObject;
5083 PyObject *valObject;
5084 Py_ssize_t iter = 0;
5085
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005086 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005088
5089 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005090 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005091
5092 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5093 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005094 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005095
Bram Moolenaara03e6312013-05-29 22:49:26 +02005096 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005097 {
5098 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005099 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005100 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005101
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005102 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005103 {
5104 dict_unref(dict);
5105 return -1;
5106 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005107
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005108 if (*key == NUL)
5109 {
5110 dict_unref(dict);
5111 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005112 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005113 return -1;
5114 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005115
5116 di = dictitem_alloc(key);
5117
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005118 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119
5120 if (di == NULL)
5121 {
5122 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005123 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124 return -1;
5125 }
5126 di->di_tv.v_lock = 0;
5127
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005128 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129 {
5130 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005131 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005132 return -1;
5133 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005134
5135 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005137 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005138 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005140 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005141 return -1;
5142 }
5143 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005144
5145 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005146 return 0;
5147}
5148
5149 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005150pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005151{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005152 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005153 char_u *key;
5154 dictitem_T *di;
5155 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005156 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005157 PyObject *keyObject;
5158 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005159
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005160 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005161 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005162
5163 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005164 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005165
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005166 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005167 {
5168 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005170 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005171
5172 if (!(iterator = PyObject_GetIter(list)))
5173 {
5174 dict_unref(dict);
5175 Py_DECREF(list);
5176 return -1;
5177 }
5178 Py_DECREF(list);
5179
5180 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005181 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005182 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005183
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005184 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005185 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005186 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005187 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005188 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005189 return -1;
5190 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005191
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005192 if (*key == NUL)
5193 {
5194 Py_DECREF(keyObject);
5195 Py_DECREF(iterator);
5196 Py_XDECREF(todecref);
5197 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005198 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005199 return -1;
5200 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005201
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005202 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005203 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005204 Py_DECREF(keyObject);
5205 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005206 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005207 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005208 return -1;
5209 }
5210
5211 di = dictitem_alloc(key);
5212
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005213 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005214 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005215
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005216 if (di == NULL)
5217 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005218 Py_DECREF(iterator);
5219 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005220 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005221 PyErr_NoMemory();
5222 return -1;
5223 }
5224 di->di_tv.v_lock = 0;
5225
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005226 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005227 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005228 Py_DECREF(iterator);
5229 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005230 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005231 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 return -1;
5233 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005234
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005235 Py_DECREF(valObject);
5236
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005237 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005238 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005239 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005240 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005241 dictitem_free(di);
5242 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005243 return -1;
5244 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005245 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005246 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005247 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005248 return 0;
5249}
5250
5251 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005252pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005253{
5254 list_T *l;
5255
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005256 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005257 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258
5259 tv->v_type = VAR_LIST;
5260 tv->vval.v_list = l;
5261
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005262 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005263 {
5264 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005265 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005266 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005267
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005268 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005269 return 0;
5270}
5271
Bram Moolenaardb913952012-06-29 12:54:53 +02005272typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5273
5274 static int
5275convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005276 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005277{
5278 PyObject *capsule;
5279 char hexBuf[sizeof(void *) * 2 + 3];
5280
5281 sprintf(hexBuf, "%p", obj);
5282
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005283# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005284 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005285# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005286 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005287# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005288 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005289 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005290# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005291 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005292# else
5293 capsule = PyCObject_FromVoidPtr(tv, NULL);
5294# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005295 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5296 {
5297 Py_DECREF(capsule);
5298 tv->v_type = VAR_UNKNOWN;
5299 return -1;
5300 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005301 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005302 {
5303 tv->v_type = VAR_UNKNOWN;
5304 return -1;
5305 }
5306 /* As we are not using copy_tv which increments reference count we must
5307 * do it ourself. */
5308 switch(tv->v_type)
5309 {
5310 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5311 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5312 }
5313 }
5314 else
5315 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005316 typval_T *v;
5317
5318# ifdef PY_USE_CAPSULE
5319 v = PyCapsule_GetPointer(capsule, NULL);
5320# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005321 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005322# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005323 copy_tv(v, tv);
5324 }
5325 return 0;
5326}
5327
5328 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005329ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5330{
5331 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005332 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005333
5334 if (!(lookup_dict = PyDict_New()))
5335 return -1;
5336
5337 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5338 {
5339 tv->v_type = VAR_DICT;
5340 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5341 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005342 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005343 }
5344 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005345 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005346 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005347 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005348 else
5349 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005350 PyErr_FORMAT(PyExc_TypeError,
5351 "unable to convert %s to vim dictionary",
5352 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005353 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005354 }
5355 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005356 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005357}
5358
5359 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005360ConvertFromPyObject(PyObject *obj, typval_T *tv)
5361{
5362 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005363 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005364
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005365 if (!(lookup_dict = PyDict_New()))
5366 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005367 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005368 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005369 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005370}
5371
5372 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005373_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005374{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005375 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005376 {
5377 tv->v_type = VAR_DICT;
5378 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5379 ++tv->vval.v_dict->dv_refcount;
5380 }
5381 else if (obj->ob_type == &ListType)
5382 {
5383 tv->v_type = VAR_LIST;
5384 tv->vval.v_list = (((ListObject *)(obj))->list);
5385 ++tv->vval.v_list->lv_refcount;
5386 }
5387 else if (obj->ob_type == &FunctionType)
5388 {
5389 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5390 return -1;
5391
5392 tv->v_type = VAR_FUNC;
5393 func_ref(tv->vval.v_string);
5394 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005395 else if (PyBytes_Check(obj))
5396 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005397 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005398
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005399 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005400 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005401 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005402 return -1;
5403
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005404 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005405 return -1;
5406
5407 tv->v_type = VAR_STRING;
5408 }
5409 else if (PyUnicode_Check(obj))
5410 {
5411 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005412 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005413
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005414 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005415 if (bytes == NULL)
5416 return -1;
5417
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005418 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005419 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005420 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005421 return -1;
5422
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005423 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005424 {
5425 Py_XDECREF(bytes);
5426 return -1;
5427 }
5428 Py_XDECREF(bytes);
5429
5430 tv->v_type = VAR_STRING;
5431 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005432#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005433 else if (PyInt_Check(obj))
5434 {
5435 tv->v_type = VAR_NUMBER;
5436 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005437 if (PyErr_Occurred())
5438 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005439 }
5440#endif
5441 else if (PyLong_Check(obj))
5442 {
5443 tv->v_type = VAR_NUMBER;
5444 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005445 if (PyErr_Occurred())
5446 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005447 }
5448 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005449 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005450#ifdef FEAT_FLOAT
5451 else if (PyFloat_Check(obj))
5452 {
5453 tv->v_type = VAR_FLOAT;
5454 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5455 }
5456#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005457 else if (PyObject_HasAttrString(obj, "keys"))
5458 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005459 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005460 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005461 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005462 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005463 else if (PyNumber_Check(obj))
5464 {
5465 PyObject *num;
5466
5467 if (!(num = PyNumber_Long(obj)))
5468 return -1;
5469
5470 tv->v_type = VAR_NUMBER;
5471 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5472
5473 Py_DECREF(num);
5474 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005475 else
5476 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005477 PyErr_FORMAT(PyExc_TypeError,
5478 "unable to convert %s to vim structure",
5479 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005480 return -1;
5481 }
5482 return 0;
5483}
5484
5485 static PyObject *
5486ConvertToPyObject(typval_T *tv)
5487{
5488 if (tv == NULL)
5489 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005490 PyErr_SET_VIM("internal error: NULL reference passed");
Bram Moolenaardb913952012-06-29 12:54:53 +02005491 return NULL;
5492 }
5493 switch (tv->v_type)
5494 {
5495 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005496 return PyBytes_FromString(tv->vval.v_string == NULL
5497 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005498 case VAR_NUMBER:
5499 return PyLong_FromLong((long) tv->vval.v_number);
5500#ifdef FEAT_FLOAT
5501 case VAR_FLOAT:
5502 return PyFloat_FromDouble((double) tv->vval.v_float);
5503#endif
5504 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005505 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005506 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005507 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005508 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005509 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005510 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005511 case VAR_UNKNOWN:
5512 Py_INCREF(Py_None);
5513 return Py_None;
5514 default:
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005515 PyErr_SET_VIM("internal error: invalid value type");
Bram Moolenaardb913952012-06-29 12:54:53 +02005516 return NULL;
5517 }
5518}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005519
5520typedef struct
5521{
5522 PyObject_HEAD
5523} CurrentObject;
5524static PyTypeObject CurrentType;
5525
5526 static void
5527init_structs(void)
5528{
5529 vim_memset(&OutputType, 0, sizeof(OutputType));
5530 OutputType.tp_name = "vim.message";
5531 OutputType.tp_basicsize = sizeof(OutputObject);
5532 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5533 OutputType.tp_doc = "vim message object";
5534 OutputType.tp_methods = OutputMethods;
5535#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005536 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5537 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005538 OutputType.tp_alloc = call_PyType_GenericAlloc;
5539 OutputType.tp_new = call_PyType_GenericNew;
5540 OutputType.tp_free = call_PyObject_Free;
5541#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005542 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5543 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005544#endif
5545
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005546 vim_memset(&IterType, 0, sizeof(IterType));
5547 IterType.tp_name = "vim.iter";
5548 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005549 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005550 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005551 IterType.tp_iter = (getiterfunc)IterIter;
5552 IterType.tp_iternext = (iternextfunc)IterNext;
5553 IterType.tp_dealloc = (destructor)IterDestructor;
5554 IterType.tp_traverse = (traverseproc)IterTraverse;
5555 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005556
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005557 vim_memset(&BufferType, 0, sizeof(BufferType));
5558 BufferType.tp_name = "vim.buffer";
5559 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005560 BufferType.tp_dealloc = (destructor)BufferDestructor;
5561 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005562 BufferType.tp_as_sequence = &BufferAsSeq;
5563 BufferType.tp_as_mapping = &BufferAsMapping;
5564 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5565 BufferType.tp_doc = "vim buffer object";
5566 BufferType.tp_methods = BufferMethods;
5567#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005568 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005569 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005570 BufferType.tp_alloc = call_PyType_GenericAlloc;
5571 BufferType.tp_new = call_PyType_GenericNew;
5572 BufferType.tp_free = call_PyObject_Free;
5573#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005574 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005575 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005576#endif
5577
5578 vim_memset(&WindowType, 0, sizeof(WindowType));
5579 WindowType.tp_name = "vim.window";
5580 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005581 WindowType.tp_dealloc = (destructor)WindowDestructor;
5582 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005583 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005584 WindowType.tp_doc = "vim Window object";
5585 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005586 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5587 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005588#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005589 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5590 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005591 WindowType.tp_alloc = call_PyType_GenericAlloc;
5592 WindowType.tp_new = call_PyType_GenericNew;
5593 WindowType.tp_free = call_PyObject_Free;
5594#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005595 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5596 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005597#endif
5598
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005599 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5600 TabPageType.tp_name = "vim.tabpage";
5601 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005602 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5603 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005604 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5605 TabPageType.tp_doc = "vim tab page object";
5606 TabPageType.tp_methods = TabPageMethods;
5607#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005608 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005609 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5610 TabPageType.tp_new = call_PyType_GenericNew;
5611 TabPageType.tp_free = call_PyObject_Free;
5612#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005613 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005614#endif
5615
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005616 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5617 BufMapType.tp_name = "vim.bufferlist";
5618 BufMapType.tp_basicsize = sizeof(BufMapObject);
5619 BufMapType.tp_as_mapping = &BufMapAsMapping;
5620 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005621 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005622 BufferType.tp_doc = "vim buffer list";
5623
5624 vim_memset(&WinListType, 0, sizeof(WinListType));
5625 WinListType.tp_name = "vim.windowlist";
5626 WinListType.tp_basicsize = sizeof(WinListType);
5627 WinListType.tp_as_sequence = &WinListAsSeq;
5628 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5629 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005630 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005631
5632 vim_memset(&TabListType, 0, sizeof(TabListType));
5633 TabListType.tp_name = "vim.tabpagelist";
5634 TabListType.tp_basicsize = sizeof(TabListType);
5635 TabListType.tp_as_sequence = &TabListAsSeq;
5636 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5637 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005638
5639 vim_memset(&RangeType, 0, sizeof(RangeType));
5640 RangeType.tp_name = "vim.range";
5641 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005642 RangeType.tp_dealloc = (destructor)RangeDestructor;
5643 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005644 RangeType.tp_as_sequence = &RangeAsSeq;
5645 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005646 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005647 RangeType.tp_doc = "vim Range object";
5648 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005649 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5650 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005651#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005652 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005653 RangeType.tp_alloc = call_PyType_GenericAlloc;
5654 RangeType.tp_new = call_PyType_GenericNew;
5655 RangeType.tp_free = call_PyObject_Free;
5656#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005657 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005658#endif
5659
5660 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5661 CurrentType.tp_name = "vim.currentdata";
5662 CurrentType.tp_basicsize = sizeof(CurrentObject);
5663 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5664 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005665 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005666#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005667 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5668 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005669#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005670 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5671 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005672#endif
5673
5674 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5675 DictionaryType.tp_name = "vim.dictionary";
5676 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005677 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005678 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005679 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005680 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005681 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5682 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005683 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5684 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5685 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005686#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005687 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5688 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005689#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005690 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5691 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005692#endif
5693
5694 vim_memset(&ListType, 0, sizeof(ListType));
5695 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005696 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005697 ListType.tp_basicsize = sizeof(ListObject);
5698 ListType.tp_as_sequence = &ListAsSeq;
5699 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005700 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005701 ListType.tp_doc = "list pushing modifications to vim structure";
5702 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005703 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005704 ListType.tp_new = (newfunc)ListConstructor;
5705 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005706#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005707 ListType.tp_getattro = (getattrofunc)ListGetattro;
5708 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005709#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005710 ListType.tp_getattr = (getattrfunc)ListGetattr;
5711 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005712#endif
5713
5714 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005715 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005716 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005717 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5718 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005719 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005720 FunctionType.tp_doc = "object that calls vim function";
5721 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005722 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005723 FunctionType.tp_new = (newfunc)FunctionConstructor;
5724 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005725#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005726 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005727#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005728 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005729#endif
5730
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005731 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5732 OptionsType.tp_name = "vim.options";
5733 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005734 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005735 OptionsType.tp_doc = "object for manipulating options";
5736 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005737 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5738 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5739 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005740
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005741 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5742 LoaderType.tp_name = "vim.Loader";
5743 LoaderType.tp_basicsize = sizeof(LoaderObject);
5744 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5745 LoaderType.tp_doc = "vim message object";
5746 LoaderType.tp_methods = LoaderMethods;
5747 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5748
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005749#if PY_MAJOR_VERSION >= 3
5750 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5751 vimmodule.m_name = "vim";
5752 vimmodule.m_doc = "Vim Python interface\n";
5753 vimmodule.m_size = -1;
5754 vimmodule.m_methods = VimMethods;
5755#endif
5756}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005757
5758#define PYTYPE_READY(type) \
5759 if (PyType_Ready(&type)) \
5760 return -1;
5761
5762 static int
5763init_types()
5764{
5765 PYTYPE_READY(IterType);
5766 PYTYPE_READY(BufferType);
5767 PYTYPE_READY(RangeType);
5768 PYTYPE_READY(WindowType);
5769 PYTYPE_READY(TabPageType);
5770 PYTYPE_READY(BufMapType);
5771 PYTYPE_READY(WinListType);
5772 PYTYPE_READY(TabListType);
5773 PYTYPE_READY(CurrentType);
5774 PYTYPE_READY(DictionaryType);
5775 PYTYPE_READY(ListType);
5776 PYTYPE_READY(FunctionType);
5777 PYTYPE_READY(OptionsType);
5778 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005779 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005780 return 0;
5781}
5782
5783 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005784init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005785{
5786 PyObject *path;
5787 PyObject *path_hook;
5788 PyObject *path_hooks;
5789
5790 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5791 return -1;
5792
5793 if (!(path_hooks = PySys_GetObject("path_hooks")))
5794 {
5795 PyErr_Clear();
5796 path_hooks = PyList_New(1);
5797 PyList_SET_ITEM(path_hooks, 0, path_hook);
5798 if (PySys_SetObject("path_hooks", path_hooks))
5799 {
5800 Py_DECREF(path_hooks);
5801 return -1;
5802 }
5803 Py_DECREF(path_hooks);
5804 }
5805 else if (PyList_Check(path_hooks))
5806 {
5807 if (PyList_Append(path_hooks, path_hook))
5808 {
5809 Py_DECREF(path_hook);
5810 return -1;
5811 }
5812 Py_DECREF(path_hook);
5813 }
5814 else
5815 {
5816 VimTryStart();
5817 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5818 "You should now do the following:\n"
5819 "- append vim.path_hook to sys.path_hooks\n"
5820 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5821 VimTryEnd(); /* Discard the error */
5822 Py_DECREF(path_hook);
5823 return 0;
5824 }
5825
5826 if (!(path = PySys_GetObject("path")))
5827 {
5828 PyErr_Clear();
5829 path = PyList_New(1);
5830 Py_INCREF(vim_special_path_object);
5831 PyList_SET_ITEM(path, 0, vim_special_path_object);
5832 if (PySys_SetObject("path", path))
5833 {
5834 Py_DECREF(path);
5835 return -1;
5836 }
5837 Py_DECREF(path);
5838 }
5839 else if (PyList_Check(path))
5840 {
5841 if (PyList_Append(path, vim_special_path_object))
5842 return -1;
5843 }
5844 else
5845 {
5846 VimTryStart();
5847 EMSG(_("Failed to set path: sys.path is not a list\n"
5848 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5849 VimTryEnd(); /* Discard the error */
5850 }
5851
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005852 return 0;
5853}
5854
5855static BufMapObject TheBufferMap =
5856{
5857 PyObject_HEAD_INIT(&BufMapType)
5858};
5859
5860static WinListObject TheWindowList =
5861{
5862 PyObject_HEAD_INIT(&WinListType)
5863 NULL
5864};
5865
5866static CurrentObject TheCurrent =
5867{
5868 PyObject_HEAD_INIT(&CurrentType)
5869};
5870
5871static TabListObject TheTabPageList =
5872{
5873 PyObject_HEAD_INIT(&TabListType)
5874};
5875
5876static struct numeric_constant {
5877 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005878 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005879} numeric_constants[] = {
5880 {"VAR_LOCKED", VAR_LOCKED},
5881 {"VAR_FIXED", VAR_FIXED},
5882 {"VAR_SCOPE", VAR_SCOPE},
5883 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5884};
5885
5886static struct object_constant {
5887 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005888 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005889} object_constants[] = {
5890 {"buffers", (PyObject *)(void *)&TheBufferMap},
5891 {"windows", (PyObject *)(void *)&TheWindowList},
5892 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5893 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005894
5895 {"Buffer", (PyObject *)&BufferType},
5896 {"Range", (PyObject *)&RangeType},
5897 {"Window", (PyObject *)&WindowType},
5898 {"TabPage", (PyObject *)&TabPageType},
5899 {"Dictionary", (PyObject *)&DictionaryType},
5900 {"List", (PyObject *)&ListType},
5901 {"Function", (PyObject *)&FunctionType},
5902 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005903 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005904};
5905
5906typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005907typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005908
5909#define ADD_OBJECT(m, name, obj) \
5910 if (add_object(m, name, obj)) \
5911 return -1;
5912
5913#define ADD_CHECKED_OBJECT(m, name, obj) \
5914 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005915 PyObject *valObject = obj; \
5916 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005917 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005918 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005919 }
5920
5921 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005922populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005923{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005924 int i;
5925 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005926 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005927 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005928
5929 for (i = 0; i < (int)(sizeof(numeric_constants)
5930 / sizeof(struct numeric_constant));
5931 ++i)
5932 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005933 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005934
5935 for (i = 0; i < (int)(sizeof(object_constants)
5936 / sizeof(struct object_constant));
5937 ++i)
5938 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005939 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005941 valObject = object_constants[i].valObject;
5942 Py_INCREF(valObject);
5943 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005944 }
5945
5946 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5947 return -1;
5948 ADD_OBJECT(m, "error", VimError);
5949
Bram Moolenaara9922d62013-05-30 13:01:18 +02005950 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5951 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005952 ADD_CHECKED_OBJECT(m, "options",
5953 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005954
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005955 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005956 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005957 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005958
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005959 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005960 return -1;
5961 ADD_OBJECT(m, "_getcwd", py_getcwd)
5962
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005963 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005964 return -1;
5965 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005966 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005967 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005968 if (PyObject_SetAttrString(other_module, "chdir", attr))
5969 {
5970 Py_DECREF(attr);
5971 return -1;
5972 }
5973 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005974
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005975 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005976 {
5977 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005978 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005979 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005980 if (PyObject_SetAttrString(other_module, "fchdir", attr))
5981 {
5982 Py_DECREF(attr);
5983 return -1;
5984 }
5985 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005986 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02005987 else
5988 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02005989
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005990 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
5991 return -1;
5992
5993 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
5994
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005995 if (!(imp = PyImport_ImportModule("imp")))
5996 return -1;
5997
5998 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
5999 {
6000 Py_DECREF(imp);
6001 return -1;
6002 }
6003
6004 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6005 {
6006 Py_DECREF(py_find_module);
6007 Py_DECREF(imp);
6008 return -1;
6009 }
6010
6011 Py_DECREF(imp);
6012
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006013 ADD_OBJECT(m, "_find_module", py_find_module);
6014 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006015
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006016 return 0;
6017}