Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 2 | * |
| 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 Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 10 | * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay |
| 11 | * Pavlov. |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 12 | * |
| 13 | * Common code for if_python.c and if_python3.c. |
| 14 | */ |
| 15 | |
Bram Moolenaar | c1a995d | 2012-08-08 16:05:07 +0200 | [diff] [blame] | 16 | #if PY_VERSION_HEX < 0x02050000 |
| 17 | typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ |
| 18 | #endif |
| 19 | |
Bram Moolenaar | 91805fc | 2011-06-26 04:01:44 +0200 | [diff] [blame] | 20 | #ifdef FEAT_MBYTE |
| 21 | # define ENC_OPT p_enc |
| 22 | #else |
| 23 | # define ENC_OPT "latin1" |
| 24 | #endif |
Bram Moolenaar | d620aa9 | 2013-05-17 16:40:06 +0200 | [diff] [blame] | 25 | #define DOPY_FUNC "_vim_pydo" |
Bram Moolenaar | 91805fc | 2011-06-26 04:01:44 +0200 | [diff] [blame] | 26 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 27 | #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
| 28 | |
| 29 | #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
| 30 | #define INVALID_WINDOW_VALUE ((win_T *)(-1)) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 31 | #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 32 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 33 | #define DICTKEY_DECL \ |
| 34 | PyObject *dictkey_todecref; |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 35 | #define DICTKEY_CHECK_EMPTY(err) \ |
| 36 | if (*key == NUL) \ |
| 37 | { \ |
| 38 | PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \ |
| 39 | return err; \ |
| 40 | } |
| 41 | #define DICTKEY_SET_KEY (key = StringToChars(keyObject, &dictkey_todecref)) |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 42 | #define DICTKEY_GET(err, decref) \ |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 43 | if (!DICTKEY_SET_KEY) \ |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 44 | { \ |
| 45 | if (decref) \ |
| 46 | { \ |
| 47 | Py_DECREF(keyObject); \ |
| 48 | } \ |
Bram Moolenaar | c37b6ec | 2013-05-29 22:43:37 +0200 | [diff] [blame] | 49 | return err; \ |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 50 | } \ |
| 51 | if (decref && !dictkey_todecref) \ |
| 52 | dictkey_todecref = keyObject; \ |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 53 | DICTKEY_CHECK_EMPTY(err) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 54 | #define DICTKEY_UNREF \ |
| 55 | Py_XDECREF(dictkey_todecref); |
| 56 | |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 57 | typedef void (*rangeinitializer)(void *); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 58 | typedef void (*runner)(const char *, void * |
| 59 | #ifdef PY_CAN_RECURSE |
| 60 | , PyGILState_STATE * |
| 61 | #endif |
| 62 | ); |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 63 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 64 | static int ConvertFromPyObject(PyObject *, typval_T *); |
| 65 | static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 66 | static PyObject *WindowNew(win_T *, tabpage_T *); |
| 67 | static PyObject *BufferNew (buf_T *); |
| 68 | static PyObject *LineToString(const char *); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 69 | |
| 70 | static PyInt RangeStart; |
| 71 | static PyInt RangeEnd; |
| 72 | |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 73 | static PyObject *globals; |
| 74 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 75 | /* |
| 76 | * obtain a lock on the Vim data structures |
| 77 | */ |
| 78 | static void |
| 79 | Python_Lock_Vim(void) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * release a lock on the Vim data structures |
| 85 | */ |
| 86 | static void |
| 87 | Python_Release_Vim(void) |
| 88 | { |
| 89 | } |
| 90 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 91 | /* |
| 92 | * The "todecref" argument holds a pointer to PyObject * that must be |
| 93 | * DECREF'ed after returned char_u * is no longer needed or NULL if all what |
| 94 | * was needed to generate returned value is object. |
| 95 | * |
| 96 | * Use Py_XDECREF to decrement reference count. |
| 97 | */ |
| 98 | static char_u * |
| 99 | StringToChars(PyObject *object, PyObject **todecref) |
| 100 | { |
| 101 | char_u *p; |
| 102 | PyObject *bytes = NULL; |
| 103 | |
| 104 | if (PyBytes_Check(object)) |
| 105 | { |
| 106 | |
| 107 | if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1) |
| 108 | return NULL; |
| 109 | if (p == NULL) |
| 110 | return NULL; |
| 111 | |
| 112 | *todecref = NULL; |
| 113 | } |
| 114 | else if (PyUnicode_Check(object)) |
| 115 | { |
| 116 | bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL); |
| 117 | if (bytes == NULL) |
| 118 | return NULL; |
| 119 | |
| 120 | if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1) |
| 121 | return NULL; |
| 122 | if (p == NULL) |
| 123 | return NULL; |
| 124 | |
| 125 | *todecref = bytes; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | PyErr_SetString(PyExc_TypeError, _("object must be string")); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | return (char_u *) p; |
| 134 | } |
| 135 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 136 | static int |
| 137 | add_string(PyObject *list, char *s) |
| 138 | { |
| 139 | PyObject *string; |
| 140 | |
| 141 | if (!(string = PyString_FromString(s))) |
| 142 | return -1; |
| 143 | if (PyList_Append(list, string)) |
| 144 | { |
| 145 | Py_DECREF(string); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | Py_DECREF(string); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static PyObject * |
| 154 | ObjectDir(PyObject *self, char **attributes) |
| 155 | { |
| 156 | PyMethodDef *method; |
| 157 | char **attr; |
| 158 | PyObject *r; |
| 159 | |
| 160 | if (!(r = PyList_New(0))) |
| 161 | return NULL; |
| 162 | |
| 163 | if (self) |
| 164 | for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method) |
| 165 | if (add_string(r, (char *) method->ml_name)) |
| 166 | { |
| 167 | Py_DECREF(r); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | for (attr = attributes ; *attr ; ++attr) |
| 172 | if (add_string(r, *attr)) |
| 173 | { |
| 174 | Py_DECREF(r); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | #if PY_MAJOR_VERSION < 3 |
| 179 | if (add_string(r, "__members__")) |
| 180 | { |
| 181 | Py_DECREF(r); |
| 182 | return NULL; |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | return r; |
| 187 | } |
| 188 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 189 | /* Output buffer management |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 190 | */ |
| 191 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 192 | /* Function to write a line, points to either msg() or emsg(). */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 193 | typedef void (*writefn)(char_u *); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 194 | |
| 195 | static PyTypeObject OutputType; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 196 | |
| 197 | typedef struct |
| 198 | { |
| 199 | PyObject_HEAD |
| 200 | long softspace; |
| 201 | long error; |
| 202 | } OutputObject; |
| 203 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 204 | static char *OutputAttrs[] = { |
| 205 | "softspace", |
| 206 | NULL |
| 207 | }; |
| 208 | |
| 209 | static PyObject * |
| 210 | OutputDir(PyObject *self) |
| 211 | { |
| 212 | return ObjectDir(self, OutputAttrs); |
| 213 | } |
| 214 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 215 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 216 | OutputSetattr(OutputObject *self, char *name, PyObject *val) |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 217 | { |
| 218 | if (val == NULL) |
| 219 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 220 | PyErr_SetString(PyExc_AttributeError, |
| 221 | _("can't delete OutputObject attributes")); |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | if (strcmp(name, "softspace") == 0) |
| 226 | { |
| 227 | if (!PyInt_Check(val)) |
| 228 | { |
| 229 | PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
| 230 | return -1; |
| 231 | } |
| 232 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 233 | self->softspace = PyInt_AsLong(val); |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
| 238 | return -1; |
| 239 | } |
| 240 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 241 | /* Buffer IO, we write one whole line at a time. */ |
| 242 | static garray_T io_ga = {0, 0, 1, 80, NULL}; |
| 243 | static writefn old_fn = NULL; |
| 244 | |
| 245 | static void |
| 246 | PythonIO_Flush(void) |
| 247 | { |
| 248 | if (old_fn != NULL && io_ga.ga_len > 0) |
| 249 | { |
| 250 | ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL; |
| 251 | old_fn((char_u *)io_ga.ga_data); |
| 252 | } |
| 253 | io_ga.ga_len = 0; |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | writer(writefn fn, char_u *str, PyInt n) |
| 258 | { |
| 259 | char_u *ptr; |
| 260 | |
| 261 | /* Flush when switching output function. */ |
| 262 | if (fn != old_fn) |
| 263 | PythonIO_Flush(); |
| 264 | old_fn = fn; |
| 265 | |
| 266 | /* Write each NL separated line. Text after the last NL is kept for |
| 267 | * writing later. */ |
| 268 | while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) |
| 269 | { |
| 270 | PyInt len = ptr - str; |
| 271 | |
| 272 | if (ga_grow(&io_ga, (int)(len + 1)) == FAIL) |
| 273 | break; |
| 274 | |
| 275 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len); |
| 276 | ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL; |
| 277 | fn((char_u *)io_ga.ga_data); |
| 278 | str = ptr + 1; |
| 279 | n -= len + 1; |
| 280 | io_ga.ga_len = 0; |
| 281 | } |
| 282 | |
| 283 | /* Put the remaining text into io_ga for later printing. */ |
| 284 | if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK) |
| 285 | { |
| 286 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n); |
| 287 | io_ga.ga_len += (int)n; |
| 288 | } |
| 289 | } |
| 290 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 291 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 292 | OutputWrite(OutputObject *self, PyObject *args) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 293 | { |
Bram Moolenaar | e8cdcef | 2012-09-12 20:21:43 +0200 | [diff] [blame] | 294 | Py_ssize_t len = 0; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 295 | char *str = NULL; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 296 | int error = self->error; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 297 | |
Bram Moolenaar | 2756480 | 2011-09-07 19:30:21 +0200 | [diff] [blame] | 298 | if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len)) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 299 | return NULL; |
| 300 | |
| 301 | Py_BEGIN_ALLOW_THREADS |
| 302 | Python_Lock_Vim(); |
| 303 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 304 | Python_Release_Vim(); |
| 305 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 306 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 307 | |
| 308 | Py_INCREF(Py_None); |
| 309 | return Py_None; |
| 310 | } |
| 311 | |
| 312 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 313 | OutputWritelines(OutputObject *self, PyObject *args) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 314 | { |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 315 | PyObject *seq; |
| 316 | PyObject *iterator; |
| 317 | PyObject *item; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 318 | int error = self->error; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 319 | |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 320 | if (!PyArg_ParseTuple(args, "O", &seq)) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 321 | return NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 322 | |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 323 | if (!(iterator = PyObject_GetIter(seq))) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 324 | return NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 325 | |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 326 | while ((item = PyIter_Next(iterator))) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 327 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 328 | char *str = NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 329 | PyInt len; |
| 330 | |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 331 | if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 332 | { |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 333 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 334 | Py_DECREF(iterator); |
| 335 | Py_DECREF(item); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 336 | return NULL; |
| 337 | } |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 338 | Py_DECREF(item); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 339 | |
| 340 | Py_BEGIN_ALLOW_THREADS |
| 341 | Python_Lock_Vim(); |
| 342 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 343 | Python_Release_Vim(); |
| 344 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 345 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 348 | Py_DECREF(iterator); |
| 349 | |
| 350 | /* Iterator may have finished due to an exception */ |
| 351 | if (PyErr_Occurred()) |
| 352 | return NULL; |
| 353 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 354 | Py_INCREF(Py_None); |
| 355 | return Py_None; |
| 356 | } |
| 357 | |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 358 | static PyObject * |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 359 | OutputFlush(PyObject *self UNUSED) |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 360 | { |
| 361 | /* do nothing */ |
| 362 | Py_INCREF(Py_None); |
| 363 | return Py_None; |
| 364 | } |
| 365 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 366 | /***************/ |
| 367 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 368 | static struct PyMethodDef OutputMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 369 | /* name, function, calling, doc */ |
| 370 | {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""}, |
| 371 | {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""}, |
| 372 | {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""}, |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 373 | {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""}, |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 374 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 375 | }; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 376 | |
| 377 | static OutputObject Output = |
| 378 | { |
| 379 | PyObject_HEAD_INIT(&OutputType) |
| 380 | 0, |
| 381 | 0 |
| 382 | }; |
| 383 | |
| 384 | static OutputObject Error = |
| 385 | { |
| 386 | PyObject_HEAD_INIT(&OutputType) |
| 387 | 0, |
| 388 | 1 |
| 389 | }; |
| 390 | |
| 391 | static int |
| 392 | PythonIO_Init_io(void) |
| 393 | { |
| 394 | PySys_SetObject("stdout", (PyObject *)(void *)&Output); |
| 395 | PySys_SetObject("stderr", (PyObject *)(void *)&Error); |
| 396 | |
| 397 | if (PyErr_Occurred()) |
| 398 | { |
| 399 | EMSG(_("E264: Python: Error initialising I/O objects")); |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | static PyObject *VimError; |
| 408 | |
| 409 | /* Check to see whether a Vim error has been reported, or a keyboard |
| 410 | * interrupt has been detected. |
| 411 | */ |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 412 | |
| 413 | static void |
| 414 | VimTryStart(void) |
| 415 | { |
| 416 | ++trylevel; |
| 417 | } |
| 418 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 419 | static int |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 420 | VimTryEnd(void) |
| 421 | { |
| 422 | --trylevel; |
| 423 | if (got_int) |
| 424 | { |
| 425 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 426 | return 1; |
| 427 | } |
| 428 | else if (!did_throw) |
| 429 | return 0; |
| 430 | else if (PyErr_Occurred()) |
| 431 | return 1; |
| 432 | else |
| 433 | { |
| 434 | PyErr_SetVim((char *) current_exception->value); |
| 435 | discard_current_exception(); |
| 436 | return 1; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static int |
| 441 | VimCheckInterrupt(void) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 442 | { |
| 443 | if (got_int) |
| 444 | { |
| 445 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 446 | return 1; |
| 447 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 448 | return 0; |
| 449 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 450 | |
| 451 | /* Vim module - Implementation |
| 452 | */ |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 453 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 454 | static PyObject * |
| 455 | VimCommand(PyObject *self UNUSED, PyObject *args) |
| 456 | { |
| 457 | char *cmd; |
| 458 | PyObject *result; |
| 459 | |
| 460 | if (!PyArg_ParseTuple(args, "s", &cmd)) |
| 461 | return NULL; |
| 462 | |
| 463 | PyErr_Clear(); |
| 464 | |
| 465 | Py_BEGIN_ALLOW_THREADS |
| 466 | Python_Lock_Vim(); |
| 467 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 468 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 469 | do_cmdline_cmd((char_u *)cmd); |
| 470 | update_screen(VALID); |
| 471 | |
| 472 | Python_Release_Vim(); |
| 473 | Py_END_ALLOW_THREADS |
| 474 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 475 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 476 | result = NULL; |
| 477 | else |
| 478 | result = Py_None; |
| 479 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 480 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 481 | Py_XINCREF(result); |
| 482 | return result; |
| 483 | } |
| 484 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 485 | /* |
| 486 | * Function to translate a typval_T into a PyObject; this will recursively |
| 487 | * translate lists/dictionaries into their Python equivalents. |
| 488 | * |
| 489 | * The depth parameter is to avoid infinite recursion, set it to 1 when |
| 490 | * you call VimToPython. |
| 491 | */ |
| 492 | static PyObject * |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 493 | VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 494 | { |
| 495 | PyObject *result; |
| 496 | PyObject *newObj; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 497 | char ptrBuf[sizeof(void *) * 2 + 3]; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 498 | |
| 499 | /* Avoid infinite recursion */ |
| 500 | if (depth > 100) |
| 501 | { |
| 502 | Py_INCREF(Py_None); |
| 503 | result = Py_None; |
| 504 | return result; |
| 505 | } |
| 506 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 507 | /* Check if we run into a recursive loop. The item must be in lookup_dict |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 508 | * then and we can use it again. */ |
| 509 | if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
| 510 | || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
| 511 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 512 | sprintf(ptrBuf, "%p", |
| 513 | our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list |
| 514 | : (void *)our_tv->vval.v_dict); |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 515 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 516 | if ((result = PyDict_GetItemString(lookup_dict, ptrBuf))) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 517 | { |
| 518 | Py_INCREF(result); |
| 519 | return result; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (our_tv->v_type == VAR_STRING) |
| 524 | { |
Bram Moolenaar | 432b09c | 2013-05-29 22:26:18 +0200 | [diff] [blame] | 525 | result = PyString_FromString(our_tv->vval.v_string == NULL |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 526 | ? "" : (char *)our_tv->vval.v_string); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 527 | } |
| 528 | else if (our_tv->v_type == VAR_NUMBER) |
| 529 | { |
| 530 | char buf[NUMBUFLEN]; |
| 531 | |
| 532 | /* For backwards compatibility numbers are stored as strings. */ |
| 533 | sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
Bram Moolenaar | 432b09c | 2013-05-29 22:26:18 +0200 | [diff] [blame] | 534 | result = PyString_FromString((char *) buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 535 | } |
| 536 | # ifdef FEAT_FLOAT |
| 537 | else if (our_tv->v_type == VAR_FLOAT) |
| 538 | { |
| 539 | char buf[NUMBUFLEN]; |
| 540 | |
| 541 | sprintf(buf, "%f", our_tv->vval.v_float); |
Bram Moolenaar | 432b09c | 2013-05-29 22:26:18 +0200 | [diff] [blame] | 542 | result = PyString_FromString((char *) buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 543 | } |
| 544 | # endif |
| 545 | else if (our_tv->v_type == VAR_LIST) |
| 546 | { |
| 547 | list_T *list = our_tv->vval.v_list; |
| 548 | listitem_T *curr; |
| 549 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 550 | if (list == NULL) |
| 551 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 552 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 553 | if (!(result = PyList_New(0))) |
| 554 | return NULL; |
| 555 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 556 | if (PyDict_SetItemString(lookup_dict, ptrBuf, result)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 557 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 558 | Py_DECREF(result); |
| 559 | return NULL; |
| 560 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 561 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 562 | for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
| 563 | { |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 564 | if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict))) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 565 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 566 | Py_DECREF(result); |
| 567 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 568 | } |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 569 | if (PyList_Append(result, newObj)) |
| 570 | { |
| 571 | Py_DECREF(newObj); |
| 572 | Py_DECREF(result); |
| 573 | return NULL; |
| 574 | } |
| 575 | Py_DECREF(newObj); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | else if (our_tv->v_type == VAR_DICT) |
| 579 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 580 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 581 | hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
| 582 | long_u todo = ht->ht_used; |
| 583 | hashitem_T *hi; |
| 584 | dictitem_T *di; |
| 585 | if (our_tv->vval.v_dict == NULL) |
| 586 | return NULL; |
| 587 | |
| 588 | if (!(result = PyDict_New())) |
| 589 | return NULL; |
| 590 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 591 | if (PyDict_SetItemString(lookup_dict, ptrBuf, result)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 592 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 593 | Py_DECREF(result); |
| 594 | return NULL; |
| 595 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 596 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 597 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 598 | { |
| 599 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 600 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 601 | --todo; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 602 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 603 | di = dict_lookup(hi); |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 604 | if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict))) |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 605 | { |
| 606 | Py_DECREF(result); |
| 607 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 608 | } |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame] | 609 | if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj)) |
| 610 | { |
| 611 | Py_DECREF(result); |
| 612 | Py_DECREF(newObj); |
| 613 | return NULL; |
| 614 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | Py_INCREF(Py_None); |
| 621 | result = Py_None; |
| 622 | } |
| 623 | |
| 624 | return result; |
| 625 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 626 | |
| 627 | static PyObject * |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 628 | VimEval(PyObject *self UNUSED, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 629 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 630 | char *expr; |
| 631 | typval_T *our_tv; |
| 632 | PyObject *result; |
| 633 | PyObject *lookup_dict; |
| 634 | |
| 635 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 636 | return NULL; |
| 637 | |
| 638 | Py_BEGIN_ALLOW_THREADS |
| 639 | Python_Lock_Vim(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 640 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 641 | our_tv = eval_expr((char_u *)expr, NULL); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 642 | Python_Release_Vim(); |
| 643 | Py_END_ALLOW_THREADS |
| 644 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 645 | if (VimTryEnd()) |
| 646 | return NULL; |
| 647 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 648 | if (our_tv == NULL) |
| 649 | { |
| 650 | PyErr_SetVim(_("invalid expression")); |
| 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | /* Convert the Vim type into a Python type. Create a dictionary that's |
| 655 | * used to check for recursive loops. */ |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 656 | if (!(lookup_dict = PyDict_New())) |
| 657 | result = NULL; |
| 658 | else |
| 659 | { |
| 660 | result = VimToPython(our_tv, 1, lookup_dict); |
| 661 | Py_DECREF(lookup_dict); |
| 662 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 663 | |
| 664 | |
| 665 | Py_BEGIN_ALLOW_THREADS |
| 666 | Python_Lock_Vim(); |
| 667 | free_tv(our_tv); |
| 668 | Python_Release_Vim(); |
| 669 | Py_END_ALLOW_THREADS |
| 670 | |
| 671 | return result; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 674 | static PyObject *ConvertToPyObject(typval_T *); |
| 675 | |
| 676 | static PyObject * |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 677 | VimEvalPy(PyObject *self UNUSED, PyObject *args) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 678 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 679 | char *expr; |
| 680 | typval_T *our_tv; |
| 681 | PyObject *result; |
| 682 | |
| 683 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 684 | return NULL; |
| 685 | |
| 686 | Py_BEGIN_ALLOW_THREADS |
| 687 | Python_Lock_Vim(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 688 | VimTryStart(); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 689 | our_tv = eval_expr((char_u *)expr, NULL); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 690 | Python_Release_Vim(); |
| 691 | Py_END_ALLOW_THREADS |
| 692 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 693 | if (VimTryEnd()) |
| 694 | return NULL; |
| 695 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 696 | if (our_tv == NULL) |
| 697 | { |
| 698 | PyErr_SetVim(_("invalid expression")); |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | result = ConvertToPyObject(our_tv); |
| 703 | Py_BEGIN_ALLOW_THREADS |
| 704 | Python_Lock_Vim(); |
| 705 | free_tv(our_tv); |
| 706 | Python_Release_Vim(); |
| 707 | Py_END_ALLOW_THREADS |
| 708 | |
| 709 | return result; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | static PyObject * |
| 713 | VimStrwidth(PyObject *self UNUSED, PyObject *args) |
| 714 | { |
| 715 | char *expr; |
| 716 | |
| 717 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 718 | return NULL; |
| 719 | |
Bram Moolenaar | a54bf40 | 2012-12-05 16:30:07 +0100 | [diff] [blame] | 720 | return PyLong_FromLong( |
| 721 | #ifdef FEAT_MBYTE |
| 722 | mb_string2cells((char_u *)expr, (int)STRLEN(expr)) |
| 723 | #else |
| 724 | STRLEN(expr) |
| 725 | #endif |
| 726 | ); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 729 | /* |
| 730 | * Vim module - Definitions |
| 731 | */ |
| 732 | |
| 733 | static struct PyMethodDef VimMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 734 | /* name, function, calling, documentation */ |
| 735 | {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" }, |
| 736 | {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" }, |
| 737 | {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"}, |
| 738 | {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"}, |
| 739 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 740 | }; |
| 741 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 742 | /* |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 743 | * Generic iterator object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 744 | */ |
| 745 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 746 | static PyTypeObject IterType; |
| 747 | |
| 748 | typedef PyObject *(*nextfun)(void **); |
| 749 | typedef void (*destructorfun)(void *); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 750 | typedef int (*traversefun)(void *, visitproc, void *); |
| 751 | typedef int (*clearfun)(void **); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 752 | |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 753 | /* Main purpose of this object is removing the need for do python |
| 754 | * initialization (i.e. PyType_Ready and setting type attributes) for a big |
| 755 | * bunch of objects. */ |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 756 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 757 | typedef struct |
| 758 | { |
| 759 | PyObject_HEAD |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 760 | void *cur; |
| 761 | nextfun next; |
| 762 | destructorfun destruct; |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 763 | traversefun traverse; |
| 764 | clearfun clear; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 765 | } IterObject; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 766 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 767 | static PyObject * |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 768 | IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse, |
| 769 | clearfun clear) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 770 | { |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 771 | IterObject *self; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 772 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 773 | self = PyObject_GC_New(IterObject, &IterType); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 774 | self->cur = start; |
| 775 | self->next = next; |
| 776 | self->destruct = destruct; |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 777 | self->traverse = traverse; |
| 778 | self->clear = clear; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 779 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 780 | return (PyObject *)(self); |
| 781 | } |
| 782 | |
| 783 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 784 | IterDestructor(IterObject *self) |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 785 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 786 | PyObject_GC_UnTrack((void *)(self)); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 787 | self->destruct(self->cur); |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 788 | PyObject_GC_Del((void *)(self)); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 791 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 792 | IterTraverse(IterObject *self, visitproc visit, void *arg) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 793 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 794 | if (self->traverse != NULL) |
| 795 | return self->traverse(self->cur, visit, arg); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 796 | else |
| 797 | return 0; |
| 798 | } |
| 799 | |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 800 | /* Mac OSX defines clear() somewhere. */ |
| 801 | #ifdef clear |
| 802 | # undef clear |
| 803 | #endif |
| 804 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 805 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 806 | IterClear(IterObject *self) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 807 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 808 | if (self->clear != NULL) |
| 809 | return self->clear(&self->cur); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 810 | else |
| 811 | return 0; |
| 812 | } |
| 813 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 814 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 815 | IterNext(IterObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 816 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 817 | return self->next(&self->cur); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 820 | static PyObject * |
| 821 | IterIter(PyObject *self) |
| 822 | { |
Bram Moolenaar | 1bcabe1 | 2013-05-29 22:52:32 +0200 | [diff] [blame] | 823 | Py_INCREF(self); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 824 | return self; |
| 825 | } |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 826 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 827 | typedef struct pylinkedlist_S { |
| 828 | struct pylinkedlist_S *pll_next; |
| 829 | struct pylinkedlist_S *pll_prev; |
| 830 | PyObject *pll_obj; |
| 831 | } pylinkedlist_T; |
| 832 | |
| 833 | static pylinkedlist_T *lastdict = NULL; |
| 834 | static pylinkedlist_T *lastlist = NULL; |
| 835 | |
| 836 | static void |
| 837 | pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last) |
| 838 | { |
| 839 | if (ref->pll_prev == NULL) |
| 840 | { |
| 841 | if (ref->pll_next == NULL) |
| 842 | { |
| 843 | *last = NULL; |
| 844 | return; |
| 845 | } |
| 846 | } |
| 847 | else |
| 848 | ref->pll_prev->pll_next = ref->pll_next; |
| 849 | |
| 850 | if (ref->pll_next == NULL) |
| 851 | *last = ref->pll_prev; |
| 852 | else |
| 853 | ref->pll_next->pll_prev = ref->pll_prev; |
| 854 | } |
| 855 | |
| 856 | static void |
| 857 | pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last) |
| 858 | { |
| 859 | if (*last == NULL) |
| 860 | ref->pll_prev = NULL; |
| 861 | else |
| 862 | { |
| 863 | (*last)->pll_next = ref; |
| 864 | ref->pll_prev = *last; |
| 865 | } |
| 866 | ref->pll_next = NULL; |
| 867 | ref->pll_obj = self; |
| 868 | *last = ref; |
| 869 | } |
| 870 | |
| 871 | static PyTypeObject DictionaryType; |
| 872 | |
| 873 | typedef struct |
| 874 | { |
| 875 | PyObject_HEAD |
| 876 | dict_T *dict; |
| 877 | pylinkedlist_T ref; |
| 878 | } DictionaryObject; |
| 879 | |
| 880 | static PyObject * |
| 881 | DictionaryNew(dict_T *dict) |
| 882 | { |
| 883 | DictionaryObject *self; |
| 884 | |
| 885 | self = PyObject_NEW(DictionaryObject, &DictionaryType); |
| 886 | if (self == NULL) |
| 887 | return NULL; |
| 888 | self->dict = dict; |
| 889 | ++dict->dv_refcount; |
| 890 | |
| 891 | pyll_add((PyObject *)(self), &self->ref, &lastdict); |
| 892 | |
| 893 | return (PyObject *)(self); |
| 894 | } |
| 895 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 896 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 897 | DictionaryDestructor(DictionaryObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 898 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 899 | pyll_remove(&self->ref, &lastdict); |
| 900 | dict_unref(self->dict); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 901 | |
| 902 | DESTRUCTOR_FINISH(self); |
| 903 | } |
| 904 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 905 | static char *DictionaryAttrs[] = { |
| 906 | "locked", "scope", |
| 907 | NULL |
| 908 | }; |
| 909 | |
| 910 | static PyObject * |
| 911 | DictionaryDir(PyObject *self) |
| 912 | { |
| 913 | return ObjectDir(self, DictionaryAttrs); |
| 914 | } |
| 915 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 916 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 917 | DictionarySetattr(DictionaryObject *self, char *name, PyObject *val) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 918 | { |
| 919 | if (val == NULL) |
| 920 | { |
| 921 | PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); |
| 922 | return -1; |
| 923 | } |
| 924 | |
| 925 | if (strcmp(name, "locked") == 0) |
| 926 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 927 | if (self->dict->dv_lock == VAR_FIXED) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 928 | { |
| 929 | PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary")); |
| 930 | return -1; |
| 931 | } |
| 932 | else |
| 933 | { |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 934 | int istrue = PyObject_IsTrue(val); |
| 935 | if (istrue == -1) |
| 936 | return -1; |
| 937 | else if (istrue) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 938 | self->dict->dv_lock = VAR_LOCKED; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 939 | else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 940 | self->dict->dv_lock = 0; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 941 | } |
| 942 | return 0; |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute")); |
| 947 | return -1; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 952 | DictionaryLength(DictionaryObject *self) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 953 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 954 | return ((PyInt) (self->dict->dv_hashtab.ht_used)); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 958 | DictionaryItem(DictionaryObject *self, PyObject *keyObject) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 959 | { |
| 960 | char_u *key; |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 961 | dictitem_T *di; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 962 | DICTKEY_DECL |
| 963 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 964 | DICTKEY_GET(NULL, 0) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 965 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 966 | di = dict_find(self->dict, key, -1); |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 967 | |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 968 | DICTKEY_UNREF |
| 969 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 970 | if (di == NULL) |
| 971 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 972 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 973 | return NULL; |
| 974 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 975 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 976 | return ConvertToPyObject(&di->di_tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 980 | DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 981 | { |
| 982 | char_u *key; |
| 983 | typval_T tv; |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 984 | dict_T *dict = self->dict; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 985 | dictitem_T *di; |
| 986 | DICTKEY_DECL |
| 987 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 988 | if (dict->dv_lock) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 989 | { |
| 990 | PyErr_SetVim(_("dict is locked")); |
| 991 | return -1; |
| 992 | } |
| 993 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 994 | DICTKEY_GET(-1, 0) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 995 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 996 | di = dict_find(dict, key, -1); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 997 | |
| 998 | if (valObject == NULL) |
| 999 | { |
Bram Moolenaar | f27839c | 2012-06-29 16:19:50 +0200 | [diff] [blame] | 1000 | hashitem_T *hi; |
| 1001 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1002 | if (di == NULL) |
| 1003 | { |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 1004 | DICTKEY_UNREF |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 1005 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1006 | return -1; |
| 1007 | } |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 1008 | hi = hash_find(&dict->dv_hashtab, di->di_key); |
| 1009 | hash_remove(&dict->dv_hashtab, hi); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1010 | dictitem_free(di); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | if (ConvertFromPyObject(valObject, &tv) == -1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1015 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1016 | |
| 1017 | if (di == NULL) |
| 1018 | { |
| 1019 | di = dictitem_alloc(key); |
| 1020 | if (di == NULL) |
| 1021 | { |
| 1022 | PyErr_NoMemory(); |
| 1023 | return -1; |
| 1024 | } |
| 1025 | di->di_tv.v_lock = 0; |
| 1026 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 1027 | if (dict_add(dict, di) == FAIL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1028 | { |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 1029 | DICTKEY_UNREF |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1030 | vim_free(di); |
| 1031 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 1032 | return -1; |
| 1033 | } |
| 1034 | } |
| 1035 | else |
| 1036 | clear_tv(&di->di_tv); |
| 1037 | |
| 1038 | DICTKEY_UNREF |
| 1039 | |
| 1040 | copy_tv(&tv, &di->di_tv); |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1041 | clear_tv(&tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1046 | DictionaryListKeys(DictionaryObject *self) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1047 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1048 | dict_T *dict = self->dict; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1049 | long_u todo = dict->dv_hashtab.ht_used; |
| 1050 | Py_ssize_t i = 0; |
| 1051 | PyObject *r; |
| 1052 | hashitem_T *hi; |
| 1053 | |
| 1054 | r = PyList_New(todo); |
| 1055 | for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) |
| 1056 | { |
| 1057 | if (!HASHITEM_EMPTY(hi)) |
| 1058 | { |
| 1059 | PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key))); |
| 1060 | --todo; |
| 1061 | ++i; |
| 1062 | } |
| 1063 | } |
| 1064 | return r; |
| 1065 | } |
| 1066 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 1067 | static PyMappingMethods DictionaryAsMapping = { |
| 1068 | (lenfunc) DictionaryLength, |
| 1069 | (binaryfunc) DictionaryItem, |
| 1070 | (objobjargproc) DictionaryAssItem, |
| 1071 | }; |
| 1072 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1073 | static struct PyMethodDef DictionaryMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 1074 | {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""}, |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1075 | {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""}, |
| 1076 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1077 | }; |
| 1078 | |
| 1079 | static PyTypeObject ListType; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1080 | static PySequenceMethods ListAsSeq; |
| 1081 | static PyMappingMethods ListAsMapping; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1082 | |
| 1083 | typedef struct |
| 1084 | { |
| 1085 | PyObject_HEAD |
| 1086 | list_T *list; |
| 1087 | pylinkedlist_T ref; |
| 1088 | } ListObject; |
| 1089 | |
| 1090 | static PyObject * |
| 1091 | ListNew(list_T *list) |
| 1092 | { |
| 1093 | ListObject *self; |
| 1094 | |
| 1095 | self = PyObject_NEW(ListObject, &ListType); |
| 1096 | if (self == NULL) |
| 1097 | return NULL; |
| 1098 | self->list = list; |
| 1099 | ++list->lv_refcount; |
| 1100 | |
| 1101 | pyll_add((PyObject *)(self), &self->ref, &lastlist); |
| 1102 | |
| 1103 | return (PyObject *)(self); |
| 1104 | } |
| 1105 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1106 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1107 | ListDestructor(ListObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1108 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1109 | pyll_remove(&self->ref, &lastlist); |
| 1110 | list_unref(self->list); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1111 | |
| 1112 | DESTRUCTOR_FINISH(self); |
| 1113 | } |
| 1114 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1115 | static int |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 1116 | list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1117 | { |
| 1118 | Py_ssize_t i; |
| 1119 | Py_ssize_t lsize = PySequence_Size(obj); |
| 1120 | PyObject *litem; |
| 1121 | listitem_T *li; |
| 1122 | |
| 1123 | for(i=0; i<lsize; i++) |
| 1124 | { |
| 1125 | li = listitem_alloc(); |
| 1126 | if (li == NULL) |
| 1127 | { |
| 1128 | PyErr_NoMemory(); |
| 1129 | return -1; |
| 1130 | } |
| 1131 | li->li_tv.v_lock = 0; |
| 1132 | |
| 1133 | litem = PySequence_GetItem(obj, i); |
| 1134 | if (litem == NULL) |
| 1135 | return -1; |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 1136 | if (_ConvertFromPyObject(litem, &li->li_tv, lookup_dict) == -1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1137 | return -1; |
| 1138 | |
| 1139 | list_append(l, li); |
| 1140 | } |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1144 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1145 | ListLength(ListObject *self) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1146 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1147 | return ((PyInt) (self->list->lv_len)); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1151 | ListItem(ListObject *self, Py_ssize_t index) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1152 | { |
| 1153 | listitem_T *li; |
| 1154 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1155 | if (index >= ListLength(self)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1156 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1157 | PyErr_SetString(PyExc_IndexError, _("list index out of range")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1158 | return NULL; |
| 1159 | } |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1160 | li = list_find(self->list, (long) index); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1161 | if (li == NULL) |
| 1162 | { |
| 1163 | PyErr_SetVim(_("internal error: failed to get vim list item")); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | return ConvertToPyObject(&li->li_tv); |
| 1167 | } |
| 1168 | |
| 1169 | #define PROC_RANGE \ |
| 1170 | if (last < 0) {\ |
| 1171 | if (last < -size) \ |
| 1172 | last = 0; \ |
| 1173 | else \ |
| 1174 | last += size; \ |
| 1175 | } \ |
| 1176 | if (first < 0) \ |
| 1177 | first = 0; \ |
| 1178 | if (first > size) \ |
| 1179 | first = size; \ |
| 1180 | if (last > size) \ |
| 1181 | last = size; |
| 1182 | |
| 1183 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1184 | ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1185 | { |
| 1186 | PyInt i; |
| 1187 | PyInt size = ListLength(self); |
| 1188 | PyInt n; |
| 1189 | PyObject *list; |
| 1190 | int reversed = 0; |
| 1191 | |
| 1192 | PROC_RANGE |
| 1193 | if (first >= last) |
| 1194 | first = last; |
| 1195 | |
| 1196 | n = last-first; |
| 1197 | list = PyList_New(n); |
| 1198 | if (list == NULL) |
| 1199 | return NULL; |
| 1200 | |
| 1201 | for (i = 0; i < n; ++i) |
| 1202 | { |
Bram Moolenaar | 24b11fb | 2013-04-05 19:32:36 +0200 | [diff] [blame] | 1203 | PyObject *item = ListItem(self, first + i); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1204 | if (item == NULL) |
| 1205 | { |
| 1206 | Py_DECREF(list); |
| 1207 | return NULL; |
| 1208 | } |
| 1209 | |
| 1210 | if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item))) |
| 1211 | { |
| 1212 | Py_DECREF(item); |
| 1213 | Py_DECREF(list); |
| 1214 | return NULL; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | return list; |
| 1219 | } |
| 1220 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1221 | typedef struct |
| 1222 | { |
| 1223 | listwatch_T lw; |
| 1224 | list_T *list; |
| 1225 | } listiterinfo_T; |
| 1226 | |
| 1227 | static void |
| 1228 | ListIterDestruct(listiterinfo_T *lii) |
| 1229 | { |
| 1230 | list_rem_watch(lii->list, &lii->lw); |
| 1231 | PyMem_Free(lii); |
| 1232 | } |
| 1233 | |
| 1234 | static PyObject * |
| 1235 | ListIterNext(listiterinfo_T **lii) |
| 1236 | { |
| 1237 | PyObject *r; |
| 1238 | |
| 1239 | if (!((*lii)->lw.lw_item)) |
| 1240 | return NULL; |
| 1241 | |
| 1242 | if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv)))) |
| 1243 | return NULL; |
| 1244 | |
| 1245 | (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next; |
| 1246 | |
| 1247 | return r; |
| 1248 | } |
| 1249 | |
| 1250 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1251 | ListIter(ListObject *self) |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1252 | { |
| 1253 | listiterinfo_T *lii; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1254 | list_T *l = self->list; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1255 | |
| 1256 | if (!(lii = PyMem_New(listiterinfo_T, 1))) |
| 1257 | { |
| 1258 | PyErr_NoMemory(); |
| 1259 | return NULL; |
| 1260 | } |
| 1261 | |
| 1262 | list_add_watch(l, &lii->lw); |
| 1263 | lii->lw.lw_item = l->lv_first; |
| 1264 | lii->list = l; |
| 1265 | |
| 1266 | return IterNew(lii, |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1267 | (destructorfun) ListIterDestruct, (nextfun) ListIterNext, |
| 1268 | NULL, NULL); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1271 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1272 | ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1273 | { |
| 1274 | typval_T tv; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1275 | list_T *l = self->list; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1276 | listitem_T *li; |
| 1277 | Py_ssize_t length = ListLength(self); |
| 1278 | |
| 1279 | if (l->lv_lock) |
| 1280 | { |
| 1281 | PyErr_SetVim(_("list is locked")); |
| 1282 | return -1; |
| 1283 | } |
| 1284 | if (index>length || (index==length && obj==NULL)) |
| 1285 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1286 | PyErr_SetString(PyExc_IndexError, _("list index out of range")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1287 | return -1; |
| 1288 | } |
| 1289 | |
| 1290 | if (obj == NULL) |
| 1291 | { |
| 1292 | li = list_find(l, (long) index); |
| 1293 | list_remove(l, li, li); |
| 1294 | clear_tv(&li->li_tv); |
| 1295 | vim_free(li); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | if (ConvertFromPyObject(obj, &tv) == -1) |
| 1300 | return -1; |
| 1301 | |
| 1302 | if (index == length) |
| 1303 | { |
| 1304 | if (list_append_tv(l, &tv) == FAIL) |
| 1305 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1306 | clear_tv(&tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1307 | PyErr_SetVim(_("Failed to add item to list")); |
| 1308 | return -1; |
| 1309 | } |
| 1310 | } |
| 1311 | else |
| 1312 | { |
| 1313 | li = list_find(l, (long) index); |
| 1314 | clear_tv(&li->li_tv); |
| 1315 | copy_tv(&tv, &li->li_tv); |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1316 | clear_tv(&tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1317 | } |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
| 1321 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1322 | ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1323 | { |
| 1324 | PyInt size = ListLength(self); |
| 1325 | Py_ssize_t i; |
| 1326 | Py_ssize_t lsize; |
| 1327 | PyObject *litem; |
| 1328 | listitem_T *li; |
| 1329 | listitem_T *next; |
| 1330 | typval_T v; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1331 | list_T *l = self->list; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1332 | |
| 1333 | if (l->lv_lock) |
| 1334 | { |
| 1335 | PyErr_SetVim(_("list is locked")); |
| 1336 | return -1; |
| 1337 | } |
| 1338 | |
| 1339 | PROC_RANGE |
| 1340 | |
| 1341 | if (first == size) |
| 1342 | li = NULL; |
| 1343 | else |
| 1344 | { |
| 1345 | li = list_find(l, (long) first); |
| 1346 | if (li == NULL) |
| 1347 | { |
| 1348 | PyErr_SetVim(_("internal error: no vim list item")); |
| 1349 | return -1; |
| 1350 | } |
| 1351 | if (last > first) |
| 1352 | { |
| 1353 | i = last - first; |
| 1354 | while (i-- && li != NULL) |
| 1355 | { |
| 1356 | next = li->li_next; |
| 1357 | listitem_remove(l, li); |
| 1358 | li = next; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | if (obj == NULL) |
| 1364 | return 0; |
| 1365 | |
| 1366 | if (!PyList_Check(obj)) |
| 1367 | { |
| 1368 | PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice")); |
| 1369 | return -1; |
| 1370 | } |
| 1371 | |
| 1372 | lsize = PyList_Size(obj); |
| 1373 | |
| 1374 | for(i=0; i<lsize; i++) |
| 1375 | { |
| 1376 | litem = PyList_GetItem(obj, i); |
| 1377 | if (litem == NULL) |
| 1378 | return -1; |
| 1379 | if (ConvertFromPyObject(litem, &v) == -1) |
| 1380 | return -1; |
| 1381 | if (list_insert_tv(l, &v, li) == FAIL) |
| 1382 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1383 | clear_tv(&v); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1384 | PyErr_SetVim(_("internal error: failed to add item to list")); |
| 1385 | return -1; |
| 1386 | } |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1387 | clear_tv(&v); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1388 | } |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1393 | ListConcatInPlace(ListObject *self, PyObject *obj) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1394 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1395 | list_T *l = self->list; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1396 | PyObject *lookup_dict; |
| 1397 | |
| 1398 | if (l->lv_lock) |
| 1399 | { |
| 1400 | PyErr_SetVim(_("list is locked")); |
| 1401 | return NULL; |
| 1402 | } |
| 1403 | |
| 1404 | if (!PySequence_Check(obj)) |
| 1405 | { |
| 1406 | PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists")); |
| 1407 | return NULL; |
| 1408 | } |
| 1409 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 1410 | if (!(lookup_dict = PyDict_New())) |
| 1411 | return NULL; |
| 1412 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1413 | if (list_py_concat(l, obj, lookup_dict) == -1) |
| 1414 | { |
| 1415 | Py_DECREF(lookup_dict); |
| 1416 | return NULL; |
| 1417 | } |
| 1418 | Py_DECREF(lookup_dict); |
| 1419 | |
| 1420 | Py_INCREF(self); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1421 | return (PyObject *)(self); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1422 | } |
| 1423 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1424 | static char *ListAttrs[] = { |
| 1425 | "locked", |
| 1426 | NULL |
| 1427 | }; |
| 1428 | |
| 1429 | static PyObject * |
| 1430 | ListDir(PyObject *self) |
| 1431 | { |
| 1432 | return ObjectDir(self, ListAttrs); |
| 1433 | } |
| 1434 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1435 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1436 | ListSetattr(ListObject *self, char *name, PyObject *val) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1437 | { |
| 1438 | if (val == NULL) |
| 1439 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1440 | PyErr_SetString(PyExc_AttributeError, |
| 1441 | _("cannot delete vim.dictionary attributes")); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1442 | return -1; |
| 1443 | } |
| 1444 | |
| 1445 | if (strcmp(name, "locked") == 0) |
| 1446 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1447 | if (self->list->lv_lock == VAR_FIXED) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1448 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1449 | PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list")); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1450 | return -1; |
| 1451 | } |
| 1452 | else |
| 1453 | { |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 1454 | int istrue = PyObject_IsTrue(val); |
| 1455 | if (istrue == -1) |
| 1456 | return -1; |
| 1457 | else if (istrue) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1458 | self->list->lv_lock = VAR_LOCKED; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1459 | else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1460 | self->list->lv_lock = 0; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1461 | } |
| 1462 | return 0; |
| 1463 | } |
| 1464 | else |
| 1465 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1466 | PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute")); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1467 | return -1; |
| 1468 | } |
| 1469 | } |
| 1470 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1471 | static struct PyMethodDef ListMethods[] = { |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1472 | {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""}, |
| 1473 | {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""}, |
| 1474 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1475 | }; |
| 1476 | |
| 1477 | typedef struct |
| 1478 | { |
| 1479 | PyObject_HEAD |
| 1480 | char_u *name; |
| 1481 | } FunctionObject; |
| 1482 | |
| 1483 | static PyTypeObject FunctionType; |
| 1484 | |
| 1485 | static PyObject * |
| 1486 | FunctionNew(char_u *name) |
| 1487 | { |
| 1488 | FunctionObject *self; |
| 1489 | |
| 1490 | self = PyObject_NEW(FunctionObject, &FunctionType); |
| 1491 | if (self == NULL) |
| 1492 | return NULL; |
| 1493 | self->name = PyMem_New(char_u, STRLEN(name) + 1); |
| 1494 | if (self->name == NULL) |
| 1495 | { |
| 1496 | PyErr_NoMemory(); |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | STRCPY(self->name, name); |
| 1500 | func_ref(name); |
| 1501 | return (PyObject *)(self); |
| 1502 | } |
| 1503 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1504 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1505 | FunctionDestructor(FunctionObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1506 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1507 | func_unref(self->name); |
| 1508 | PyMem_Free(self->name); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1509 | |
| 1510 | DESTRUCTOR_FINISH(self); |
| 1511 | } |
| 1512 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1513 | static char *FunctionAttrs[] = { |
| 1514 | "softspace", |
| 1515 | NULL |
| 1516 | }; |
| 1517 | |
| 1518 | static PyObject * |
| 1519 | FunctionDir(PyObject *self) |
| 1520 | { |
| 1521 | return ObjectDir(self, FunctionAttrs); |
| 1522 | } |
| 1523 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1524 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1525 | FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1526 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1527 | char_u *name = self->name; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1528 | typval_T args; |
| 1529 | typval_T selfdicttv; |
| 1530 | typval_T rettv; |
| 1531 | dict_T *selfdict = NULL; |
| 1532 | PyObject *selfdictObject; |
| 1533 | PyObject *result; |
| 1534 | int error; |
| 1535 | |
| 1536 | if (ConvertFromPyObject(argsObject, &args) == -1) |
| 1537 | return NULL; |
| 1538 | |
| 1539 | if (kwargs != NULL) |
| 1540 | { |
| 1541 | selfdictObject = PyDict_GetItemString(kwargs, "self"); |
| 1542 | if (selfdictObject != NULL) |
| 1543 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1544 | if (!PyMapping_Check(selfdictObject)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1545 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1546 | PyErr_SetString(PyExc_TypeError, |
| 1547 | _("'self' argument must be a dictionary")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1548 | clear_tv(&args); |
| 1549 | return NULL; |
| 1550 | } |
| 1551 | if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1) |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1552 | { |
| 1553 | clear_tv(&args); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1554 | return NULL; |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1555 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1556 | selfdict = selfdicttv.vval.v_dict; |
| 1557 | } |
| 1558 | } |
| 1559 | |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 1560 | Py_BEGIN_ALLOW_THREADS |
| 1561 | Python_Lock_Vim(); |
| 1562 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1563 | VimTryStart(); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1564 | error = func_call(name, &args, selfdict, &rettv); |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 1565 | |
| 1566 | Python_Release_Vim(); |
| 1567 | Py_END_ALLOW_THREADS |
| 1568 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1569 | if (VimTryEnd()) |
| 1570 | result = NULL; |
| 1571 | else if (error != OK) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1572 | { |
| 1573 | result = NULL; |
| 1574 | PyErr_SetVim(_("failed to run function")); |
| 1575 | } |
| 1576 | else |
| 1577 | result = ConvertToPyObject(&rettv); |
| 1578 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1579 | clear_tv(&args); |
| 1580 | clear_tv(&rettv); |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1581 | if (selfdict != NULL) |
| 1582 | clear_tv(&selfdicttv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1583 | |
| 1584 | return result; |
| 1585 | } |
| 1586 | |
| 1587 | static struct PyMethodDef FunctionMethods[] = { |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1588 | {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""}, |
| 1589 | {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""}, |
| 1590 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1591 | }; |
| 1592 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1593 | /* |
| 1594 | * Options object |
| 1595 | */ |
| 1596 | |
| 1597 | static PyTypeObject OptionsType; |
| 1598 | |
| 1599 | typedef int (*checkfun)(void *); |
| 1600 | |
| 1601 | typedef struct |
| 1602 | { |
| 1603 | PyObject_HEAD |
| 1604 | int opt_type; |
| 1605 | void *from; |
| 1606 | checkfun Check; |
| 1607 | PyObject *fromObj; |
| 1608 | } OptionsObject; |
| 1609 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1610 | static int |
| 1611 | dummy_check(void *arg UNUSED) |
| 1612 | { |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | static PyObject * |
| 1617 | OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj) |
| 1618 | { |
| 1619 | OptionsObject *self; |
| 1620 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 1621 | self = PyObject_GC_New(OptionsObject, &OptionsType); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1622 | if (self == NULL) |
| 1623 | return NULL; |
| 1624 | |
| 1625 | self->opt_type = opt_type; |
| 1626 | self->from = from; |
| 1627 | self->Check = Check; |
| 1628 | self->fromObj = fromObj; |
| 1629 | if (fromObj) |
| 1630 | Py_INCREF(fromObj); |
| 1631 | |
| 1632 | return (PyObject *)(self); |
| 1633 | } |
| 1634 | |
| 1635 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1636 | OptionsDestructor(OptionsObject *self) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1637 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 1638 | PyObject_GC_UnTrack((void *)(self)); |
| 1639 | Py_XDECREF(self->fromObj); |
| 1640 | PyObject_GC_Del((void *)(self)); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1644 | OptionsTraverse(OptionsObject *self, visitproc visit, void *arg) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1645 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1646 | Py_VISIT(self->fromObj); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1647 | return 0; |
| 1648 | } |
| 1649 | |
| 1650 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1651 | OptionsClear(OptionsObject *self) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1652 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1653 | Py_CLEAR(self->fromObj); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1654 | return 0; |
| 1655 | } |
| 1656 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1657 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1658 | OptionsItem(OptionsObject *self, PyObject *keyObject) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1659 | { |
| 1660 | char_u *key; |
| 1661 | int flags; |
| 1662 | long numval; |
| 1663 | char_u *stringval; |
Bram Moolenaar | 161fb5e | 2013-05-06 06:26:15 +0200 | [diff] [blame] | 1664 | DICTKEY_DECL |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1665 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1666 | if (self->Check(self->from)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1667 | return NULL; |
| 1668 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 1669 | DICTKEY_GET(NULL, 0) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1670 | |
| 1671 | flags = get_option_value_strict(key, &numval, &stringval, |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1672 | self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1673 | |
| 1674 | DICTKEY_UNREF |
| 1675 | |
| 1676 | if (flags == 0) |
| 1677 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 1678 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1679 | return NULL; |
| 1680 | } |
| 1681 | |
| 1682 | if (flags & SOPT_UNSET) |
| 1683 | { |
| 1684 | Py_INCREF(Py_None); |
| 1685 | return Py_None; |
| 1686 | } |
| 1687 | else if (flags & SOPT_BOOL) |
| 1688 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1689 | PyObject *r; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1690 | r = numval ? Py_True : Py_False; |
| 1691 | Py_INCREF(r); |
| 1692 | return r; |
| 1693 | } |
| 1694 | else if (flags & SOPT_NUM) |
| 1695 | return PyInt_FromLong(numval); |
| 1696 | else if (flags & SOPT_STRING) |
| 1697 | { |
| 1698 | if (stringval) |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1699 | { |
| 1700 | PyObject *r = PyBytes_FromString((char *) stringval); |
| 1701 | vim_free(stringval); |
| 1702 | return r; |
| 1703 | } |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1704 | else |
| 1705 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1706 | PyErr_SetString(PyExc_RuntimeError, |
| 1707 | _("unable to get option value")); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1708 | return NULL; |
| 1709 | } |
| 1710 | } |
| 1711 | else |
| 1712 | { |
| 1713 | PyErr_SetVim("Internal error: unknown option type. Should not happen"); |
| 1714 | return NULL; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | static int |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1719 | set_option_value_err(key, numval, stringval, opt_flags) |
| 1720 | char_u *key; |
| 1721 | int numval; |
| 1722 | char_u *stringval; |
| 1723 | int opt_flags; |
| 1724 | { |
| 1725 | char_u *errmsg; |
| 1726 | |
| 1727 | if ((errmsg = set_option_value(key, numval, stringval, opt_flags))) |
| 1728 | { |
| 1729 | if (VimTryEnd()) |
| 1730 | return FAIL; |
| 1731 | PyErr_SetVim((char *)errmsg); |
| 1732 | return FAIL; |
| 1733 | } |
| 1734 | return OK; |
| 1735 | } |
| 1736 | |
| 1737 | static int |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1738 | set_option_value_for(key, numval, stringval, opt_flags, opt_type, from) |
| 1739 | char_u *key; |
| 1740 | int numval; |
| 1741 | char_u *stringval; |
| 1742 | int opt_flags; |
| 1743 | int opt_type; |
| 1744 | void *from; |
| 1745 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1746 | win_T *save_curwin = NULL; |
| 1747 | tabpage_T *save_curtab = NULL; |
| 1748 | buf_T *save_curbuf = NULL; |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1749 | int r = 0; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1750 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1751 | VimTryStart(); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1752 | switch (opt_type) |
| 1753 | { |
| 1754 | case SREQ_WIN: |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 1755 | if (switch_win(&save_curwin, &save_curtab, (win_T *)from, |
| 1756 | win_find_tabpage((win_T *)from)) == FAIL) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1757 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1758 | if (VimTryEnd()) |
| 1759 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1760 | PyErr_SetVim("Problem while switching windows."); |
| 1761 | return -1; |
| 1762 | } |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1763 | r = set_option_value_err(key, numval, stringval, opt_flags); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1764 | restore_win(save_curwin, save_curtab); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1765 | if (r == FAIL) |
| 1766 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1767 | break; |
| 1768 | case SREQ_BUF: |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 1769 | switch_buffer(&save_curbuf, (buf_T *)from); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1770 | r = set_option_value_err(key, numval, stringval, opt_flags); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 1771 | restore_buffer(save_curbuf); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1772 | if (r == FAIL) |
| 1773 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1774 | break; |
| 1775 | case SREQ_GLOBAL: |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1776 | r = set_option_value_err(key, numval, stringval, opt_flags); |
| 1777 | if (r == FAIL) |
| 1778 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1779 | break; |
| 1780 | } |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1781 | return VimTryEnd(); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1784 | static void * |
| 1785 | py_memsave(void *p, size_t len) |
| 1786 | { |
| 1787 | void *r; |
| 1788 | if (!(r = PyMem_Malloc(len))) |
| 1789 | return NULL; |
| 1790 | mch_memmove(r, p, len); |
| 1791 | return r; |
| 1792 | } |
| 1793 | |
| 1794 | #define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1)) |
| 1795 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1796 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1797 | OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1798 | { |
| 1799 | char_u *key; |
| 1800 | int flags; |
| 1801 | int opt_flags; |
| 1802 | int r = 0; |
Bram Moolenaar | 161fb5e | 2013-05-06 06:26:15 +0200 | [diff] [blame] | 1803 | DICTKEY_DECL |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1804 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1805 | if (self->Check(self->from)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1806 | return -1; |
| 1807 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 1808 | DICTKEY_GET(-1, 0) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1809 | |
| 1810 | flags = get_option_value_strict(key, NULL, NULL, |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1811 | self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1812 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1813 | if (flags == 0) |
| 1814 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 1815 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1816 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1817 | return -1; |
| 1818 | } |
| 1819 | |
| 1820 | if (valObject == NULL) |
| 1821 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1822 | if (self->opt_type == SREQ_GLOBAL) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1823 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1824 | PyErr_SetString(PyExc_ValueError, |
| 1825 | _("unable to unset global option")); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1826 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1827 | return -1; |
| 1828 | } |
| 1829 | else if (!(flags & SOPT_GLOBAL)) |
| 1830 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1831 | PyErr_SetString(PyExc_ValueError, _("unable to unset option " |
| 1832 | "without global value")); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1833 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1834 | return -1; |
| 1835 | } |
| 1836 | else |
| 1837 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1838 | unset_global_local_option(key, self->from); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1839 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1840 | return 0; |
| 1841 | } |
| 1842 | } |
| 1843 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1844 | opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1845 | |
| 1846 | if (flags & SOPT_BOOL) |
| 1847 | { |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 1848 | int istrue = PyObject_IsTrue(valObject); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1849 | |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 1850 | if (istrue == -1) |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1851 | r = -1; |
| 1852 | else |
| 1853 | r = set_option_value_for(key, istrue, NULL, |
| 1854 | opt_flags, self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1855 | } |
| 1856 | else if (flags & SOPT_NUM) |
| 1857 | { |
| 1858 | int val; |
| 1859 | |
| 1860 | #if PY_MAJOR_VERSION < 3 |
| 1861 | if (PyInt_Check(valObject)) |
| 1862 | val = PyInt_AsLong(valObject); |
| 1863 | else |
| 1864 | #endif |
| 1865 | if (PyLong_Check(valObject)) |
| 1866 | val = PyLong_AsLong(valObject); |
| 1867 | else |
| 1868 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1869 | PyErr_SetString(PyExc_TypeError, _("object must be integer")); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1870 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1871 | return -1; |
| 1872 | } |
| 1873 | |
| 1874 | r = set_option_value_for(key, val, NULL, opt_flags, |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1875 | self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1876 | } |
| 1877 | else |
| 1878 | { |
| 1879 | char_u *val; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1880 | PyObject *todecref; |
| 1881 | |
| 1882 | if ((val = StringToChars(valObject, &todecref))) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1883 | { |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1884 | r = set_option_value_for(key, 0, val, opt_flags, |
| 1885 | self->opt_type, self->from); |
| 1886 | Py_XDECREF(todecref); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1887 | } |
| 1888 | else |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1889 | r = -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1890 | } |
| 1891 | |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1892 | DICTKEY_UNREF |
| 1893 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1894 | return r; |
| 1895 | } |
| 1896 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1897 | static PyMappingMethods OptionsAsMapping = { |
| 1898 | (lenfunc) NULL, |
| 1899 | (binaryfunc) OptionsItem, |
| 1900 | (objobjargproc) OptionsAssItem, |
| 1901 | }; |
| 1902 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1903 | /* Tabpage object |
| 1904 | */ |
| 1905 | |
| 1906 | typedef struct |
| 1907 | { |
| 1908 | PyObject_HEAD |
| 1909 | tabpage_T *tab; |
| 1910 | } TabPageObject; |
| 1911 | |
| 1912 | static PyObject *WinListNew(TabPageObject *tabObject); |
| 1913 | |
| 1914 | static PyTypeObject TabPageType; |
| 1915 | |
| 1916 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1917 | CheckTabPage(TabPageObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1918 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1919 | if (self->tab == INVALID_TABPAGE_VALUE) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1920 | { |
| 1921 | PyErr_SetVim(_("attempt to refer to deleted tab page")); |
| 1922 | return -1; |
| 1923 | } |
| 1924 | |
| 1925 | return 0; |
| 1926 | } |
| 1927 | |
| 1928 | static PyObject * |
| 1929 | TabPageNew(tabpage_T *tab) |
| 1930 | { |
| 1931 | TabPageObject *self; |
| 1932 | |
| 1933 | if (TAB_PYTHON_REF(tab)) |
| 1934 | { |
| 1935 | self = TAB_PYTHON_REF(tab); |
| 1936 | Py_INCREF(self); |
| 1937 | } |
| 1938 | else |
| 1939 | { |
| 1940 | self = PyObject_NEW(TabPageObject, &TabPageType); |
| 1941 | if (self == NULL) |
| 1942 | return NULL; |
| 1943 | self->tab = tab; |
| 1944 | TAB_PYTHON_REF(tab) = self; |
| 1945 | } |
| 1946 | |
| 1947 | return (PyObject *)(self); |
| 1948 | } |
| 1949 | |
| 1950 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1951 | TabPageDestructor(TabPageObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1952 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1953 | if (self->tab && self->tab != INVALID_TABPAGE_VALUE) |
| 1954 | TAB_PYTHON_REF(self->tab) = NULL; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1955 | |
| 1956 | DESTRUCTOR_FINISH(self); |
| 1957 | } |
| 1958 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1959 | static char *TabPageAttrs[] = { |
| 1960 | "windows", "number", "vars", "window", "valid", |
| 1961 | NULL |
| 1962 | }; |
| 1963 | |
| 1964 | static PyObject * |
| 1965 | TabPageDir(PyObject *self) |
| 1966 | { |
| 1967 | return ObjectDir(self, TabPageAttrs); |
| 1968 | } |
| 1969 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1970 | static PyObject * |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 1971 | TabPageAttrValid(TabPageObject *self, char *name) |
| 1972 | { |
| 1973 | PyObject *r; |
| 1974 | |
| 1975 | if (strcmp(name, "valid") != 0) |
| 1976 | return NULL; |
| 1977 | |
| 1978 | r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True); |
| 1979 | Py_INCREF(r); |
| 1980 | return r; |
| 1981 | } |
| 1982 | |
| 1983 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1984 | TabPageAttr(TabPageObject *self, char *name) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1985 | { |
| 1986 | if (strcmp(name, "windows") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1987 | return WinListNew(self); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1988 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1989 | return PyLong_FromLong((long) get_tab_number(self->tab)); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1990 | else if (strcmp(name, "vars") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1991 | return DictionaryNew(self->tab->tp_vars); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1992 | else if (strcmp(name, "window") == 0) |
| 1993 | { |
| 1994 | /* For current tab window.c does not bother to set or update tp_curwin |
| 1995 | */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1996 | if (self->tab == curtab) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 1997 | return WindowNew(curwin, curtab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1998 | else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1999 | return WindowNew(self->tab->tp_curwin, self->tab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2000 | } |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 2001 | else if (strcmp(name, "__members__") == 0) |
| 2002 | return ObjectDir(NULL, TabPageAttrs); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2003 | return NULL; |
| 2004 | } |
| 2005 | |
| 2006 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2007 | TabPageRepr(TabPageObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2008 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2009 | if (self->tab == INVALID_TABPAGE_VALUE) |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 2010 | return PyString_FromFormat("<tabpage object (deleted) at %p>", (self)); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2011 | else |
| 2012 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2013 | int t = get_tab_number(self->tab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2014 | |
| 2015 | if (t == 0) |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 2016 | return PyString_FromFormat("<tabpage object (unknown) at %p>", |
| 2017 | (self)); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2018 | else |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 2019 | return PyString_FromFormat("<tabpage %d>", t - 1); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | static struct PyMethodDef TabPageMethods[] = { |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 2024 | /* name, function, calling, documentation */ |
| 2025 | {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""}, |
| 2026 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2027 | }; |
| 2028 | |
| 2029 | /* |
| 2030 | * Window list object |
| 2031 | */ |
| 2032 | |
| 2033 | static PyTypeObject TabListType; |
| 2034 | static PySequenceMethods TabListAsSeq; |
| 2035 | |
| 2036 | typedef struct |
| 2037 | { |
| 2038 | PyObject_HEAD |
| 2039 | } TabListObject; |
| 2040 | |
| 2041 | static PyInt |
| 2042 | TabListLength(PyObject *self UNUSED) |
| 2043 | { |
| 2044 | tabpage_T *tp = first_tabpage; |
| 2045 | PyInt n = 0; |
| 2046 | |
| 2047 | while (tp != NULL) |
| 2048 | { |
| 2049 | ++n; |
| 2050 | tp = tp->tp_next; |
| 2051 | } |
| 2052 | |
| 2053 | return n; |
| 2054 | } |
| 2055 | |
| 2056 | static PyObject * |
| 2057 | TabListItem(PyObject *self UNUSED, PyInt n) |
| 2058 | { |
| 2059 | tabpage_T *tp; |
| 2060 | |
| 2061 | for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n) |
| 2062 | if (n == 0) |
| 2063 | return TabPageNew(tp); |
| 2064 | |
| 2065 | PyErr_SetString(PyExc_IndexError, _("no such tab page")); |
| 2066 | return NULL; |
| 2067 | } |
| 2068 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2069 | /* Window object |
| 2070 | */ |
| 2071 | |
| 2072 | typedef struct |
| 2073 | { |
| 2074 | PyObject_HEAD |
| 2075 | win_T *win; |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2076 | TabPageObject *tabObject; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2077 | } WindowObject; |
| 2078 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2079 | static PyTypeObject WindowType; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2080 | |
| 2081 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2082 | CheckWindow(WindowObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2083 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2084 | if (self->win == INVALID_WINDOW_VALUE) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2085 | { |
| 2086 | PyErr_SetVim(_("attempt to refer to deleted window")); |
| 2087 | return -1; |
| 2088 | } |
| 2089 | |
| 2090 | return 0; |
| 2091 | } |
| 2092 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2093 | static PyObject * |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2094 | WindowNew(win_T *win, tabpage_T *tab) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2095 | { |
| 2096 | /* We need to handle deletion of windows underneath us. |
| 2097 | * If we add a "w_python*_ref" field to the win_T structure, |
| 2098 | * then we can get at it in win_free() in vim. We then |
| 2099 | * need to create only ONE Python object per window - if |
| 2100 | * we try to create a second, just INCREF the existing one |
| 2101 | * and return it. The (single) Python object referring to |
| 2102 | * the window is stored in "w_python*_ref". |
| 2103 | * On a win_free() we set the Python object's win_T* field |
| 2104 | * to an invalid value. We trap all uses of a window |
| 2105 | * object, and reject them if the win_T* field is invalid. |
| 2106 | * |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2107 | * Python2 and Python3 get different fields and different objects: |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2108 | * w_python_ref and w_python3_ref fields respectively. |
| 2109 | */ |
| 2110 | |
| 2111 | WindowObject *self; |
| 2112 | |
| 2113 | if (WIN_PYTHON_REF(win)) |
| 2114 | { |
| 2115 | self = WIN_PYTHON_REF(win); |
| 2116 | Py_INCREF(self); |
| 2117 | } |
| 2118 | else |
| 2119 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2120 | self = PyObject_GC_New(WindowObject, &WindowType); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2121 | if (self == NULL) |
| 2122 | return NULL; |
| 2123 | self->win = win; |
| 2124 | WIN_PYTHON_REF(win) = self; |
| 2125 | } |
| 2126 | |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2127 | self->tabObject = ((TabPageObject *)(TabPageNew(tab))); |
| 2128 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2129 | return (PyObject *)(self); |
| 2130 | } |
| 2131 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2132 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2133 | WindowDestructor(WindowObject *self) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2134 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2135 | PyObject_GC_UnTrack((void *)(self)); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2136 | if (self->win && self->win != INVALID_WINDOW_VALUE) |
| 2137 | WIN_PYTHON_REF(self->win) = NULL; |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2138 | Py_XDECREF(((PyObject *)(self->tabObject))); |
| 2139 | PyObject_GC_Del((void *)(self)); |
| 2140 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2141 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2142 | static int |
| 2143 | WindowTraverse(WindowObject *self, visitproc visit, void *arg) |
| 2144 | { |
| 2145 | Py_VISIT(((PyObject *)(self->tabObject))); |
| 2146 | return 0; |
| 2147 | } |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2148 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2149 | static int |
| 2150 | WindowClear(WindowObject *self) |
| 2151 | { |
| 2152 | Py_CLEAR(self->tabObject); |
| 2153 | return 0; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2154 | } |
| 2155 | |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2156 | static win_T * |
| 2157 | get_firstwin(TabPageObject *tabObject) |
| 2158 | { |
| 2159 | if (tabObject) |
| 2160 | { |
| 2161 | if (CheckTabPage(tabObject)) |
| 2162 | return NULL; |
| 2163 | /* For current tab window.c does not bother to set or update tp_firstwin |
| 2164 | */ |
| 2165 | else if (tabObject->tab == curtab) |
| 2166 | return firstwin; |
| 2167 | else |
| 2168 | return tabObject->tab->tp_firstwin; |
| 2169 | } |
| 2170 | else |
| 2171 | return firstwin; |
| 2172 | } |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 2173 | static char *WindowAttrs[] = { |
| 2174 | "buffer", "cursor", "height", "vars", "options", "number", "row", "col", |
| 2175 | "tabpage", "valid", |
| 2176 | NULL |
| 2177 | }; |
| 2178 | |
| 2179 | static PyObject * |
| 2180 | WindowDir(PyObject *self) |
| 2181 | { |
| 2182 | return ObjectDir(self, WindowAttrs); |
| 2183 | } |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2184 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2185 | static PyObject * |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 2186 | WindowAttrValid(WindowObject *self, char *name) |
| 2187 | { |
| 2188 | PyObject *r; |
| 2189 | |
| 2190 | if (strcmp(name, "valid") != 0) |
| 2191 | return NULL; |
| 2192 | |
| 2193 | r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True); |
| 2194 | Py_INCREF(r); |
| 2195 | return r; |
| 2196 | } |
| 2197 | |
| 2198 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2199 | WindowAttr(WindowObject *self, char *name) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2200 | { |
| 2201 | if (strcmp(name, "buffer") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2202 | return (PyObject *)BufferNew(self->win->w_buffer); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2203 | else if (strcmp(name, "cursor") == 0) |
| 2204 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2205 | pos_T *pos = &self->win->w_cursor; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2206 | |
| 2207 | return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); |
| 2208 | } |
| 2209 | else if (strcmp(name, "height") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2210 | return PyLong_FromLong((long)(self->win->w_height)); |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 2211 | #ifdef FEAT_WINDOWS |
| 2212 | else if (strcmp(name, "row") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2213 | return PyLong_FromLong((long)(self->win->w_winrow)); |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 2214 | #endif |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2215 | #ifdef FEAT_VERTSPLIT |
| 2216 | else if (strcmp(name, "width") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2217 | return PyLong_FromLong((long)(W_WIDTH(self->win))); |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 2218 | else if (strcmp(name, "col") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2219 | return PyLong_FromLong((long)(W_WINCOL(self->win))); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2220 | #endif |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 2221 | else if (strcmp(name, "vars") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2222 | return DictionaryNew(self->win->w_vars); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 2223 | else if (strcmp(name, "options") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2224 | return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow, |
| 2225 | (PyObject *) self); |
Bram Moolenaar | 6d21645 | 2013-05-12 19:00:41 +0200 | [diff] [blame] | 2226 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2227 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2228 | if (CheckTabPage(self->tabObject)) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2229 | return NULL; |
| 2230 | return PyLong_FromLong((long) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2231 | get_win_number(self->win, get_firstwin(self->tabObject))); |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2232 | } |
| 2233 | else if (strcmp(name, "tabpage") == 0) |
| 2234 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2235 | Py_INCREF(self->tabObject); |
| 2236 | return (PyObject *)(self->tabObject); |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2237 | } |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 2238 | else if (strcmp(name, "__members__") == 0) |
| 2239 | return ObjectDir(NULL, WindowAttrs); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2240 | else |
| 2241 | return NULL; |
| 2242 | } |
| 2243 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2244 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2245 | WindowSetattr(WindowObject *self, char *name, PyObject *val) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2246 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2247 | if (CheckWindow(self)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2248 | return -1; |
| 2249 | |
| 2250 | if (strcmp(name, "buffer") == 0) |
| 2251 | { |
| 2252 | PyErr_SetString(PyExc_TypeError, _("readonly attribute")); |
| 2253 | return -1; |
| 2254 | } |
| 2255 | else if (strcmp(name, "cursor") == 0) |
| 2256 | { |
| 2257 | long lnum; |
| 2258 | long col; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2259 | |
| 2260 | if (!PyArg_Parse(val, "(ll)", &lnum, &col)) |
| 2261 | return -1; |
| 2262 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2263 | if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2264 | { |
| 2265 | PyErr_SetVim(_("cursor position outside buffer")); |
| 2266 | return -1; |
| 2267 | } |
| 2268 | |
| 2269 | /* Check for keyboard interrupts */ |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2270 | if (VimCheckInterrupt()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2271 | return -1; |
| 2272 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2273 | self->win->w_cursor.lnum = lnum; |
| 2274 | self->win->w_cursor.col = col; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2275 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2276 | self->win->w_cursor.coladd = 0; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2277 | #endif |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 2278 | /* When column is out of range silently correct it. */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2279 | check_cursor_col_win(self->win); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2280 | |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 2281 | update_screen(VALID); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2282 | return 0; |
| 2283 | } |
| 2284 | else if (strcmp(name, "height") == 0) |
| 2285 | { |
| 2286 | int height; |
| 2287 | win_T *savewin; |
| 2288 | |
| 2289 | if (!PyArg_Parse(val, "i", &height)) |
| 2290 | return -1; |
| 2291 | |
| 2292 | #ifdef FEAT_GUI |
| 2293 | need_mouse_correct = TRUE; |
| 2294 | #endif |
| 2295 | savewin = curwin; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2296 | curwin = self->win; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2297 | |
| 2298 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2299 | win_setheight(height); |
| 2300 | curwin = savewin; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2301 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2302 | return -1; |
| 2303 | |
| 2304 | return 0; |
| 2305 | } |
| 2306 | #ifdef FEAT_VERTSPLIT |
| 2307 | else if (strcmp(name, "width") == 0) |
| 2308 | { |
| 2309 | int width; |
| 2310 | win_T *savewin; |
| 2311 | |
| 2312 | if (!PyArg_Parse(val, "i", &width)) |
| 2313 | return -1; |
| 2314 | |
| 2315 | #ifdef FEAT_GUI |
| 2316 | need_mouse_correct = TRUE; |
| 2317 | #endif |
| 2318 | savewin = curwin; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2319 | curwin = self->win; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2320 | |
| 2321 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2322 | win_setwidth(width); |
| 2323 | curwin = savewin; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2324 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2325 | return -1; |
| 2326 | |
| 2327 | return 0; |
| 2328 | } |
| 2329 | #endif |
| 2330 | else |
| 2331 | { |
| 2332 | PyErr_SetString(PyExc_AttributeError, name); |
| 2333 | return -1; |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2338 | WindowRepr(WindowObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2339 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2340 | if (self->win == INVALID_WINDOW_VALUE) |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 2341 | return PyString_FromFormat("<window object (deleted) at %p>", (self)); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2342 | else |
| 2343 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2344 | int w = get_win_number(self->win, firstwin); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2345 | |
Bram Moolenaar | 6d21645 | 2013-05-12 19:00:41 +0200 | [diff] [blame] | 2346 | if (w == 0) |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 2347 | return PyString_FromFormat("<window object (unknown) at %p>", |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2348 | (self)); |
| 2349 | else |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 2350 | return PyString_FromFormat("<window %d>", w - 1); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2354 | static struct PyMethodDef WindowMethods[] = { |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 2355 | /* name, function, calling, documentation */ |
| 2356 | {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""}, |
| 2357 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2358 | }; |
| 2359 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2360 | /* |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2361 | * Window list object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2362 | */ |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2363 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2364 | static PyTypeObject WinListType; |
| 2365 | static PySequenceMethods WinListAsSeq; |
| 2366 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2367 | typedef struct |
| 2368 | { |
| 2369 | PyObject_HEAD |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2370 | TabPageObject *tabObject; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2371 | } WinListObject; |
| 2372 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2373 | static PyObject * |
| 2374 | WinListNew(TabPageObject *tabObject) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2375 | { |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2376 | WinListObject *self; |
| 2377 | |
| 2378 | self = PyObject_NEW(WinListObject, &WinListType); |
| 2379 | self->tabObject = tabObject; |
| 2380 | Py_INCREF(tabObject); |
| 2381 | |
| 2382 | return (PyObject *)(self); |
| 2383 | } |
| 2384 | |
| 2385 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2386 | WinListDestructor(WinListObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2387 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2388 | TabPageObject *tabObject = self->tabObject; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2389 | |
| 2390 | if (tabObject) |
Bram Moolenaar | 425154d | 2013-05-24 18:58:43 +0200 | [diff] [blame] | 2391 | { |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2392 | Py_DECREF((PyObject *)(tabObject)); |
Bram Moolenaar | 425154d | 2013-05-24 18:58:43 +0200 | [diff] [blame] | 2393 | } |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2394 | |
| 2395 | DESTRUCTOR_FINISH(self); |
| 2396 | } |
| 2397 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2398 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2399 | WinListLength(WinListObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2400 | { |
| 2401 | win_T *w; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2402 | PyInt n = 0; |
| 2403 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2404 | if (!(w = get_firstwin(self->tabObject))) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2405 | return -1; |
| 2406 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2407 | while (w != NULL) |
| 2408 | { |
| 2409 | ++n; |
| 2410 | w = W_NEXT(w); |
| 2411 | } |
| 2412 | |
| 2413 | return n; |
| 2414 | } |
| 2415 | |
| 2416 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2417 | WinListItem(WinListObject *self, PyInt n) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2418 | { |
| 2419 | win_T *w; |
| 2420 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2421 | if (!(w = get_firstwin(self->tabObject))) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2422 | return NULL; |
| 2423 | |
| 2424 | for (; w != NULL; w = W_NEXT(w), --n) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2425 | if (n == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2426 | return WindowNew(w, self->tabObject? self->tabObject->tab: curtab); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2427 | |
| 2428 | PyErr_SetString(PyExc_IndexError, _("no such window")); |
| 2429 | return NULL; |
| 2430 | } |
| 2431 | |
| 2432 | /* Convert a Python string into a Vim line. |
| 2433 | * |
| 2434 | * The result is in allocated memory. All internal nulls are replaced by |
| 2435 | * newline characters. It is an error for the string to contain newline |
| 2436 | * characters. |
| 2437 | * |
| 2438 | * On errors, the Python exception data is set, and NULL is returned. |
| 2439 | */ |
| 2440 | static char * |
| 2441 | StringToLine(PyObject *obj) |
| 2442 | { |
| 2443 | const char *str; |
| 2444 | char *save; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2445 | PyObject *bytes; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2446 | PyInt len; |
| 2447 | PyInt i; |
| 2448 | char *p; |
| 2449 | |
| 2450 | if (obj == NULL || !PyString_Check(obj)) |
| 2451 | { |
| 2452 | PyErr_BadArgument(); |
| 2453 | return NULL; |
| 2454 | } |
| 2455 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2456 | bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
| 2457 | str = PyString_AsString(bytes); |
| 2458 | len = PyString_Size(bytes); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2459 | |
| 2460 | /* |
| 2461 | * Error checking: String must not contain newlines, as we |
| 2462 | * are replacing a single line, and we must replace it with |
| 2463 | * a single line. |
| 2464 | * A trailing newline is removed, so that append(f.readlines()) works. |
| 2465 | */ |
| 2466 | p = memchr(str, '\n', len); |
| 2467 | if (p != NULL) |
| 2468 | { |
| 2469 | if (p == str + len - 1) |
| 2470 | --len; |
| 2471 | else |
| 2472 | { |
| 2473 | PyErr_SetVim(_("string cannot contain newlines")); |
| 2474 | return NULL; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | /* Create a copy of the string, with internal nulls replaced by |
| 2479 | * newline characters, as is the vim convention. |
| 2480 | */ |
| 2481 | save = (char *)alloc((unsigned)(len+1)); |
| 2482 | if (save == NULL) |
| 2483 | { |
| 2484 | PyErr_NoMemory(); |
| 2485 | return NULL; |
| 2486 | } |
| 2487 | |
| 2488 | for (i = 0; i < len; ++i) |
| 2489 | { |
| 2490 | if (str[i] == '\0') |
| 2491 | save[i] = '\n'; |
| 2492 | else |
| 2493 | save[i] = str[i]; |
| 2494 | } |
| 2495 | |
| 2496 | save[i] = '\0'; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2497 | PyString_FreeBytes(bytes); /* Python 2 does nothing here */ |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2498 | |
| 2499 | return save; |
| 2500 | } |
| 2501 | |
| 2502 | /* Get a line from the specified buffer. The line number is |
| 2503 | * in Vim format (1-based). The line is returned as a Python |
| 2504 | * string object. |
| 2505 | */ |
| 2506 | static PyObject * |
| 2507 | GetBufferLine(buf_T *buf, PyInt n) |
| 2508 | { |
| 2509 | return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
| 2510 | } |
| 2511 | |
| 2512 | |
| 2513 | /* Get a list of lines from the specified buffer. The line numbers |
| 2514 | * are in Vim format (1-based). The range is from lo up to, but not |
| 2515 | * including, hi. The list is returned as a Python list of string objects. |
| 2516 | */ |
| 2517 | static PyObject * |
| 2518 | GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
| 2519 | { |
| 2520 | PyInt i; |
| 2521 | PyInt n = hi - lo; |
| 2522 | PyObject *list = PyList_New(n); |
| 2523 | |
| 2524 | if (list == NULL) |
| 2525 | return NULL; |
| 2526 | |
| 2527 | for (i = 0; i < n; ++i) |
| 2528 | { |
| 2529 | PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
| 2530 | |
| 2531 | /* Error check - was the Python string creation OK? */ |
| 2532 | if (str == NULL) |
| 2533 | { |
| 2534 | Py_DECREF(list); |
| 2535 | return NULL; |
| 2536 | } |
| 2537 | |
| 2538 | /* Set the list item */ |
| 2539 | if (PyList_SetItem(list, i, str)) |
| 2540 | { |
| 2541 | Py_DECREF(str); |
| 2542 | Py_DECREF(list); |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | /* The ownership of the Python list is passed to the caller (ie, |
| 2548 | * the caller should Py_DECREF() the object when it is finished |
| 2549 | * with it). |
| 2550 | */ |
| 2551 | |
| 2552 | return list; |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * Check if deleting lines made the cursor position invalid. |
| 2557 | * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
| 2558 | * deleted). |
| 2559 | */ |
| 2560 | static void |
| 2561 | py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
| 2562 | { |
| 2563 | if (curwin->w_cursor.lnum >= lo) |
| 2564 | { |
| 2565 | /* Adjust the cursor position if it's in/after the changed |
| 2566 | * lines. */ |
| 2567 | if (curwin->w_cursor.lnum >= hi) |
| 2568 | { |
| 2569 | curwin->w_cursor.lnum += extra; |
| 2570 | check_cursor_col(); |
| 2571 | } |
| 2572 | else if (extra < 0) |
| 2573 | { |
| 2574 | curwin->w_cursor.lnum = lo; |
| 2575 | check_cursor(); |
| 2576 | } |
| 2577 | else |
| 2578 | check_cursor_col(); |
| 2579 | changed_cline_bef_curs(); |
| 2580 | } |
| 2581 | invalidate_botline(); |
| 2582 | } |
| 2583 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2584 | /* |
| 2585 | * Replace a line in the specified buffer. The line number is |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2586 | * in Vim format (1-based). The replacement line is given as |
| 2587 | * a Python string object. The object is checked for validity |
| 2588 | * and correct format. Errors are returned as a value of FAIL. |
| 2589 | * The return value is OK on success. |
| 2590 | * If OK is returned and len_change is not NULL, *len_change |
| 2591 | * is set to the change in the buffer length. |
| 2592 | */ |
| 2593 | static int |
| 2594 | SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) |
| 2595 | { |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2596 | /* First of all, we check the type of the supplied Python object. |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2597 | * There are three cases: |
| 2598 | * 1. NULL, or None - this is a deletion. |
| 2599 | * 2. A string - this is a replacement. |
| 2600 | * 3. Anything else - this is an error. |
| 2601 | */ |
| 2602 | if (line == Py_None || line == NULL) |
| 2603 | { |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2604 | buf_T *savebuf; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2605 | |
| 2606 | PyErr_Clear(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2607 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2608 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2609 | VimTryStart(); |
| 2610 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2611 | if (u_savedel((linenr_T)n, 1L) == FAIL) |
| 2612 | PyErr_SetVim(_("cannot save undo information")); |
| 2613 | else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
| 2614 | PyErr_SetVim(_("cannot delete line")); |
| 2615 | else |
| 2616 | { |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2617 | if (buf == savebuf) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2618 | py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
| 2619 | deleted_lines_mark((linenr_T)n, 1L); |
| 2620 | } |
| 2621 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2622 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2623 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2624 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2625 | return FAIL; |
| 2626 | |
| 2627 | if (len_change) |
| 2628 | *len_change = -1; |
| 2629 | |
| 2630 | return OK; |
| 2631 | } |
| 2632 | else if (PyString_Check(line)) |
| 2633 | { |
| 2634 | char *save = StringToLine(line); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2635 | buf_T *savebuf; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2636 | |
| 2637 | if (save == NULL) |
| 2638 | return FAIL; |
| 2639 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2640 | VimTryStart(); |
| 2641 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2642 | /* We do not need to free "save" if ml_replace() consumes it. */ |
| 2643 | PyErr_Clear(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2644 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2645 | |
| 2646 | if (u_savesub((linenr_T)n) == FAIL) |
| 2647 | { |
| 2648 | PyErr_SetVim(_("cannot save undo information")); |
| 2649 | vim_free(save); |
| 2650 | } |
| 2651 | else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
| 2652 | { |
| 2653 | PyErr_SetVim(_("cannot replace line")); |
| 2654 | vim_free(save); |
| 2655 | } |
| 2656 | else |
| 2657 | changed_bytes((linenr_T)n, 0); |
| 2658 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2659 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2660 | |
| 2661 | /* Check that the cursor is not beyond the end of the line now. */ |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2662 | if (buf == savebuf) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2663 | check_cursor_col(); |
| 2664 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2665 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2666 | return FAIL; |
| 2667 | |
| 2668 | if (len_change) |
| 2669 | *len_change = 0; |
| 2670 | |
| 2671 | return OK; |
| 2672 | } |
| 2673 | else |
| 2674 | { |
| 2675 | PyErr_BadArgument(); |
| 2676 | return FAIL; |
| 2677 | } |
| 2678 | } |
| 2679 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2680 | /* Replace a range of lines in the specified buffer. The line numbers are in |
| 2681 | * Vim format (1-based). The range is from lo up to, but not including, hi. |
| 2682 | * The replacement lines are given as a Python list of string objects. The |
| 2683 | * list is checked for validity and correct format. Errors are returned as a |
| 2684 | * value of FAIL. The return value is OK on success. |
| 2685 | * If OK is returned and len_change is not NULL, *len_change |
| 2686 | * is set to the change in the buffer length. |
| 2687 | */ |
| 2688 | static int |
| 2689 | SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) |
| 2690 | { |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2691 | /* First of all, we check the type of the supplied Python object. |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2692 | * There are three cases: |
| 2693 | * 1. NULL, or None - this is a deletion. |
| 2694 | * 2. A list - this is a replacement. |
| 2695 | * 3. Anything else - this is an error. |
| 2696 | */ |
| 2697 | if (list == Py_None || list == NULL) |
| 2698 | { |
| 2699 | PyInt i; |
| 2700 | PyInt n = (int)(hi - lo); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2701 | buf_T *savebuf; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2702 | |
| 2703 | PyErr_Clear(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2704 | VimTryStart(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2705 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2706 | |
| 2707 | if (u_savedel((linenr_T)lo, (long)n) == FAIL) |
| 2708 | PyErr_SetVim(_("cannot save undo information")); |
| 2709 | else |
| 2710 | { |
| 2711 | for (i = 0; i < n; ++i) |
| 2712 | { |
| 2713 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 2714 | { |
| 2715 | PyErr_SetVim(_("cannot delete line")); |
| 2716 | break; |
| 2717 | } |
| 2718 | } |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2719 | if (buf == savebuf) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2720 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
| 2721 | deleted_lines_mark((linenr_T)lo, (long)i); |
| 2722 | } |
| 2723 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2724 | restore_buffer(savebuf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2725 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2726 | if (VimTryEnd()) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2727 | return FAIL; |
| 2728 | |
| 2729 | if (len_change) |
| 2730 | *len_change = -n; |
| 2731 | |
| 2732 | return OK; |
| 2733 | } |
| 2734 | else if (PyList_Check(list)) |
| 2735 | { |
| 2736 | PyInt i; |
| 2737 | PyInt new_len = PyList_Size(list); |
| 2738 | PyInt old_len = hi - lo; |
| 2739 | PyInt extra = 0; /* lines added to text, can be negative */ |
| 2740 | char **array; |
| 2741 | buf_T *savebuf; |
| 2742 | |
| 2743 | if (new_len == 0) /* avoid allocating zero bytes */ |
| 2744 | array = NULL; |
| 2745 | else |
| 2746 | { |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2747 | array = PyMem_New(char *, new_len); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2748 | if (array == NULL) |
| 2749 | { |
| 2750 | PyErr_NoMemory(); |
| 2751 | return FAIL; |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | for (i = 0; i < new_len; ++i) |
| 2756 | { |
| 2757 | PyObject *line = PyList_GetItem(list, i); |
| 2758 | |
| 2759 | array[i] = StringToLine(line); |
| 2760 | if (array[i] == NULL) |
| 2761 | { |
| 2762 | while (i) |
| 2763 | vim_free(array[--i]); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2764 | PyMem_Free(array); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2765 | return FAIL; |
| 2766 | } |
| 2767 | } |
| 2768 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2769 | VimTryStart(); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2770 | PyErr_Clear(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2771 | |
| 2772 | // START of region without "return". Must call restore_buffer()! |
| 2773 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2774 | |
| 2775 | if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) |
| 2776 | PyErr_SetVim(_("cannot save undo information")); |
| 2777 | |
| 2778 | /* If the size of the range is reducing (ie, new_len < old_len) we |
| 2779 | * need to delete some old_len. We do this at the start, by |
| 2780 | * repeatedly deleting line "lo". |
| 2781 | */ |
| 2782 | if (!PyErr_Occurred()) |
| 2783 | { |
| 2784 | for (i = 0; i < old_len - new_len; ++i) |
| 2785 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 2786 | { |
| 2787 | PyErr_SetVim(_("cannot delete line")); |
| 2788 | break; |
| 2789 | } |
| 2790 | extra -= i; |
| 2791 | } |
| 2792 | |
| 2793 | /* For as long as possible, replace the existing old_len with the |
| 2794 | * new old_len. This is a more efficient operation, as it requires |
| 2795 | * less memory allocation and freeing. |
| 2796 | */ |
| 2797 | if (!PyErr_Occurred()) |
| 2798 | { |
| 2799 | for (i = 0; i < old_len && i < new_len; ++i) |
| 2800 | if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) |
| 2801 | == FAIL) |
| 2802 | { |
| 2803 | PyErr_SetVim(_("cannot replace line")); |
| 2804 | break; |
| 2805 | } |
| 2806 | } |
| 2807 | else |
| 2808 | i = 0; |
| 2809 | |
| 2810 | /* Now we may need to insert the remaining new old_len. If we do, we |
| 2811 | * must free the strings as we finish with them (we can't pass the |
| 2812 | * responsibility to vim in this case). |
| 2813 | */ |
| 2814 | if (!PyErr_Occurred()) |
| 2815 | { |
| 2816 | while (i < new_len) |
| 2817 | { |
| 2818 | if (ml_append((linenr_T)(lo + i - 1), |
| 2819 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2820 | { |
| 2821 | PyErr_SetVim(_("cannot insert line")); |
| 2822 | break; |
| 2823 | } |
| 2824 | vim_free(array[i]); |
| 2825 | ++i; |
| 2826 | ++extra; |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | /* Free any left-over old_len, as a result of an error */ |
| 2831 | while (i < new_len) |
| 2832 | { |
| 2833 | vim_free(array[i]); |
| 2834 | ++i; |
| 2835 | } |
| 2836 | |
| 2837 | /* Free the array of old_len. All of its contents have now |
| 2838 | * been dealt with (either freed, or the responsibility passed |
| 2839 | * to vim. |
| 2840 | */ |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2841 | PyMem_Free(array); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2842 | |
| 2843 | /* Adjust marks. Invalidate any which lie in the |
| 2844 | * changed range, and move any in the remainder of the buffer. |
| 2845 | */ |
| 2846 | mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), |
| 2847 | (long)MAXLNUM, (long)extra); |
| 2848 | changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); |
| 2849 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2850 | if (buf == savebuf) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2851 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
| 2852 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2853 | // END of region without "return". |
| 2854 | restore_buffer(savebuf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2855 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2856 | if (VimTryEnd()) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2857 | return FAIL; |
| 2858 | |
| 2859 | if (len_change) |
| 2860 | *len_change = new_len - old_len; |
| 2861 | |
| 2862 | return OK; |
| 2863 | } |
| 2864 | else |
| 2865 | { |
| 2866 | PyErr_BadArgument(); |
| 2867 | return FAIL; |
| 2868 | } |
| 2869 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2870 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2871 | /* Insert a number of lines into the specified buffer after the specified line. |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2872 | * The line number is in Vim format (1-based). The lines to be inserted are |
| 2873 | * given as a Python list of string objects or as a single string. The lines |
| 2874 | * to be added are checked for validity and correct format. Errors are |
| 2875 | * returned as a value of FAIL. The return value is OK on success. |
| 2876 | * If OK is returned and len_change is not NULL, *len_change |
| 2877 | * is set to the change in the buffer length. |
| 2878 | */ |
| 2879 | static int |
| 2880 | InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
| 2881 | { |
| 2882 | /* First of all, we check the type of the supplied Python object. |
| 2883 | * It must be a string or a list, or the call is in error. |
| 2884 | */ |
| 2885 | if (PyString_Check(lines)) |
| 2886 | { |
| 2887 | char *str = StringToLine(lines); |
| 2888 | buf_T *savebuf; |
| 2889 | |
| 2890 | if (str == NULL) |
| 2891 | return FAIL; |
| 2892 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2893 | PyErr_Clear(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2894 | VimTryStart(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2895 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2896 | |
| 2897 | if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) |
| 2898 | PyErr_SetVim(_("cannot save undo information")); |
| 2899 | else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
| 2900 | PyErr_SetVim(_("cannot insert line")); |
| 2901 | else |
| 2902 | appended_lines_mark((linenr_T)n, 1L); |
| 2903 | |
| 2904 | vim_free(str); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2905 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2906 | update_screen(VALID); |
| 2907 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2908 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2909 | return FAIL; |
| 2910 | |
| 2911 | if (len_change) |
| 2912 | *len_change = 1; |
| 2913 | |
| 2914 | return OK; |
| 2915 | } |
| 2916 | else if (PyList_Check(lines)) |
| 2917 | { |
| 2918 | PyInt i; |
| 2919 | PyInt size = PyList_Size(lines); |
| 2920 | char **array; |
| 2921 | buf_T *savebuf; |
| 2922 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2923 | array = PyMem_New(char *, size); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2924 | if (array == NULL) |
| 2925 | { |
| 2926 | PyErr_NoMemory(); |
| 2927 | return FAIL; |
| 2928 | } |
| 2929 | |
| 2930 | for (i = 0; i < size; ++i) |
| 2931 | { |
| 2932 | PyObject *line = PyList_GetItem(lines, i); |
| 2933 | array[i] = StringToLine(line); |
| 2934 | |
| 2935 | if (array[i] == NULL) |
| 2936 | { |
| 2937 | while (i) |
| 2938 | vim_free(array[--i]); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2939 | PyMem_Free(array); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2940 | return FAIL; |
| 2941 | } |
| 2942 | } |
| 2943 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2944 | PyErr_Clear(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2945 | VimTryStart(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2946 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2947 | |
| 2948 | if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
| 2949 | PyErr_SetVim(_("cannot save undo information")); |
| 2950 | else |
| 2951 | { |
| 2952 | for (i = 0; i < size; ++i) |
| 2953 | { |
| 2954 | if (ml_append((linenr_T)(n + i), |
| 2955 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2956 | { |
| 2957 | PyErr_SetVim(_("cannot insert line")); |
| 2958 | |
| 2959 | /* Free the rest of the lines */ |
| 2960 | while (i < size) |
| 2961 | vim_free(array[i++]); |
| 2962 | |
| 2963 | break; |
| 2964 | } |
| 2965 | vim_free(array[i]); |
| 2966 | } |
| 2967 | if (i > 0) |
| 2968 | appended_lines_mark((linenr_T)n, (long)i); |
| 2969 | } |
| 2970 | |
| 2971 | /* Free the array of lines. All of its contents have now |
| 2972 | * been freed. |
| 2973 | */ |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2974 | PyMem_Free(array); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2975 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2976 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2977 | update_screen(VALID); |
| 2978 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2979 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2980 | return FAIL; |
| 2981 | |
| 2982 | if (len_change) |
| 2983 | *len_change = size; |
| 2984 | |
| 2985 | return OK; |
| 2986 | } |
| 2987 | else |
| 2988 | { |
| 2989 | PyErr_BadArgument(); |
| 2990 | return FAIL; |
| 2991 | } |
| 2992 | } |
| 2993 | |
| 2994 | /* |
| 2995 | * Common routines for buffers and line ranges |
| 2996 | * ------------------------------------------- |
| 2997 | */ |
| 2998 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2999 | typedef struct |
| 3000 | { |
| 3001 | PyObject_HEAD |
| 3002 | buf_T *buf; |
| 3003 | } BufferObject; |
| 3004 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3005 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3006 | CheckBuffer(BufferObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3007 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3008 | if (self->buf == INVALID_BUFFER_VALUE) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3009 | { |
| 3010 | PyErr_SetVim(_("attempt to refer to deleted buffer")); |
| 3011 | return -1; |
| 3012 | } |
| 3013 | |
| 3014 | return 0; |
| 3015 | } |
| 3016 | |
| 3017 | static PyObject * |
| 3018 | RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end) |
| 3019 | { |
| 3020 | if (CheckBuffer(self)) |
| 3021 | return NULL; |
| 3022 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 3023 | if (end == -1) |
| 3024 | end = self->buf->b_ml.ml_line_count; |
| 3025 | |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 3026 | if (n < 0) |
| 3027 | n += end - start + 1; |
| 3028 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3029 | if (n < 0 || n > end - start) |
| 3030 | { |
| 3031 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 3032 | return NULL; |
| 3033 | } |
| 3034 | |
| 3035 | return GetBufferLine(self->buf, n+start); |
| 3036 | } |
| 3037 | |
| 3038 | static PyObject * |
| 3039 | RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end) |
| 3040 | { |
| 3041 | PyInt size; |
| 3042 | |
| 3043 | if (CheckBuffer(self)) |
| 3044 | return NULL; |
| 3045 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 3046 | if (end == -1) |
| 3047 | end = self->buf->b_ml.ml_line_count; |
| 3048 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3049 | size = end - start + 1; |
| 3050 | |
| 3051 | if (lo < 0) |
| 3052 | lo = 0; |
| 3053 | else if (lo > size) |
| 3054 | lo = size; |
| 3055 | if (hi < 0) |
| 3056 | hi = 0; |
| 3057 | if (hi < lo) |
| 3058 | hi = lo; |
| 3059 | else if (hi > size) |
| 3060 | hi = size; |
| 3061 | |
| 3062 | return GetBufferLineList(self->buf, lo+start, hi+start); |
| 3063 | } |
| 3064 | |
| 3065 | static PyInt |
| 3066 | RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 3067 | { |
| 3068 | PyInt len_change; |
| 3069 | |
| 3070 | if (CheckBuffer(self)) |
| 3071 | return -1; |
| 3072 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 3073 | if (end == -1) |
| 3074 | end = self->buf->b_ml.ml_line_count; |
| 3075 | |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 3076 | if (n < 0) |
| 3077 | n += end - start + 1; |
| 3078 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3079 | if (n < 0 || n > end - start) |
| 3080 | { |
| 3081 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 3082 | return -1; |
| 3083 | } |
| 3084 | |
| 3085 | if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) |
| 3086 | return -1; |
| 3087 | |
| 3088 | if (new_end) |
| 3089 | *new_end = end + len_change; |
| 3090 | |
| 3091 | return 0; |
| 3092 | } |
| 3093 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 3094 | static PyInt |
| 3095 | RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 3096 | { |
| 3097 | PyInt size; |
| 3098 | PyInt len_change; |
| 3099 | |
| 3100 | /* Self must be a valid buffer */ |
| 3101 | if (CheckBuffer(self)) |
| 3102 | return -1; |
| 3103 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 3104 | if (end == -1) |
| 3105 | end = self->buf->b_ml.ml_line_count; |
| 3106 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 3107 | /* Sort out the slice range */ |
| 3108 | size = end - start + 1; |
| 3109 | |
| 3110 | if (lo < 0) |
| 3111 | lo = 0; |
| 3112 | else if (lo > size) |
| 3113 | lo = size; |
| 3114 | if (hi < 0) |
| 3115 | hi = 0; |
| 3116 | if (hi < lo) |
| 3117 | hi = lo; |
| 3118 | else if (hi > size) |
| 3119 | hi = size; |
| 3120 | |
| 3121 | if (SetBufferLineList(self->buf, lo + start, hi + start, |
| 3122 | val, &len_change) == FAIL) |
| 3123 | return -1; |
| 3124 | |
| 3125 | if (new_end) |
| 3126 | *new_end = end + len_change; |
| 3127 | |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3131 | |
| 3132 | static PyObject * |
| 3133 | RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end) |
| 3134 | { |
| 3135 | PyObject *lines; |
| 3136 | PyInt len_change; |
| 3137 | PyInt max; |
| 3138 | PyInt n; |
| 3139 | |
| 3140 | if (CheckBuffer(self)) |
| 3141 | return NULL; |
| 3142 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 3143 | if (end == -1) |
| 3144 | end = self->buf->b_ml.ml_line_count; |
| 3145 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3146 | max = n = end - start + 1; |
| 3147 | |
| 3148 | if (!PyArg_ParseTuple(args, "O|n", &lines, &n)) |
| 3149 | return NULL; |
| 3150 | |
| 3151 | if (n < 0 || n > max) |
| 3152 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 3153 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3154 | return NULL; |
| 3155 | } |
| 3156 | |
| 3157 | if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
| 3158 | return NULL; |
| 3159 | |
| 3160 | if (new_end) |
| 3161 | *new_end = end + len_change; |
| 3162 | |
| 3163 | Py_INCREF(Py_None); |
| 3164 | return Py_None; |
| 3165 | } |
| 3166 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3167 | /* Range object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3168 | */ |
| 3169 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3170 | static PyTypeObject RangeType; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3171 | static PySequenceMethods RangeAsSeq; |
| 3172 | static PyMappingMethods RangeAsMapping; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3173 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3174 | typedef struct |
| 3175 | { |
| 3176 | PyObject_HEAD |
| 3177 | BufferObject *buf; |
| 3178 | PyInt start; |
| 3179 | PyInt end; |
| 3180 | } RangeObject; |
| 3181 | |
| 3182 | static PyObject * |
| 3183 | RangeNew(buf_T *buf, PyInt start, PyInt end) |
| 3184 | { |
| 3185 | BufferObject *bufr; |
| 3186 | RangeObject *self; |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3187 | self = PyObject_GC_New(RangeObject, &RangeType); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3188 | if (self == NULL) |
| 3189 | return NULL; |
| 3190 | |
| 3191 | bufr = (BufferObject *)BufferNew(buf); |
| 3192 | if (bufr == NULL) |
| 3193 | { |
| 3194 | Py_DECREF(self); |
| 3195 | return NULL; |
| 3196 | } |
| 3197 | Py_INCREF(bufr); |
| 3198 | |
| 3199 | self->buf = bufr; |
| 3200 | self->start = start; |
| 3201 | self->end = end; |
| 3202 | |
| 3203 | return (PyObject *)(self); |
| 3204 | } |
| 3205 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3206 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3207 | RangeDestructor(RangeObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3208 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3209 | PyObject_GC_UnTrack((void *)(self)); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3210 | Py_XDECREF(self->buf); |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3211 | PyObject_GC_Del((void *)(self)); |
| 3212 | } |
| 3213 | |
| 3214 | static int |
| 3215 | RangeTraverse(RangeObject *self, visitproc visit, void *arg) |
| 3216 | { |
| 3217 | Py_VISIT(((PyObject *)(self->buf))); |
| 3218 | return 0; |
| 3219 | } |
| 3220 | |
| 3221 | static int |
| 3222 | RangeClear(RangeObject *self) |
| 3223 | { |
| 3224 | Py_CLEAR(self->buf); |
| 3225 | return 0; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3226 | } |
| 3227 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3228 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3229 | RangeLength(RangeObject *self) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3230 | { |
| 3231 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3232 | if (CheckBuffer(self->buf)) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3233 | return -1; /* ??? */ |
| 3234 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3235 | return (self->end - self->start + 1); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3239 | RangeItem(RangeObject *self, PyInt n) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3240 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3241 | return RBItem(self->buf, n, self->start, self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3245 | RangeSlice(RangeObject *self, PyInt lo, PyInt hi) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3246 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3247 | return RBSlice(self->buf, lo, hi, self->start, self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3248 | } |
| 3249 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3250 | static char *RangeAttrs[] = { |
| 3251 | "start", "end", |
| 3252 | NULL |
| 3253 | }; |
| 3254 | |
| 3255 | static PyObject * |
| 3256 | RangeDir(PyObject *self) |
| 3257 | { |
| 3258 | return ObjectDir(self, RangeAttrs); |
| 3259 | } |
| 3260 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3261 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3262 | RangeAppend(RangeObject *self, PyObject *args) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3263 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3264 | return RBAppend(self->buf, args, self->start, self->end, &self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3268 | RangeRepr(RangeObject *self) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3269 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3270 | if (self->buf->buf == INVALID_BUFFER_VALUE) |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 3271 | return PyString_FromFormat("<range object (for deleted buffer) at %p>", |
| 3272 | (self)); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3273 | else |
| 3274 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3275 | char *name = (char *)self->buf->buf->b_fname; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3276 | |
| 3277 | if (name == NULL) |
| 3278 | name = ""; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3279 | |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 3280 | return PyString_FromFormat("<range %s (%d:%d)>", |
| 3281 | name, self->start, self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3282 | } |
| 3283 | } |
| 3284 | |
| 3285 | static struct PyMethodDef RangeMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 3286 | /* name, function, calling, documentation */ |
| 3287 | {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" }, |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3288 | {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""}, |
| 3289 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3290 | }; |
| 3291 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3292 | static PyTypeObject BufferType; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3293 | static PySequenceMethods BufferAsSeq; |
| 3294 | static PyMappingMethods BufferAsMapping; |
| 3295 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3296 | static PyObject * |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3297 | BufferNew(buf_T *buf) |
| 3298 | { |
| 3299 | /* We need to handle deletion of buffers underneath us. |
| 3300 | * If we add a "b_python*_ref" field to the buf_T structure, |
| 3301 | * then we can get at it in buf_freeall() in vim. We then |
| 3302 | * need to create only ONE Python object per buffer - if |
| 3303 | * we try to create a second, just INCREF the existing one |
| 3304 | * and return it. The (single) Python object referring to |
| 3305 | * the buffer is stored in "b_python*_ref". |
| 3306 | * Question: what to do on a buf_freeall(). We'll probably |
| 3307 | * have to either delete the Python object (DECREF it to |
| 3308 | * zero - a bad idea, as it leaves dangling refs!) or |
| 3309 | * set the buf_T * value to an invalid value (-1?), which |
| 3310 | * means we need checks in all access functions... Bah. |
| 3311 | * |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3312 | * Python2 and Python3 get different fields and different objects: |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3313 | * b_python_ref and b_python3_ref fields respectively. |
| 3314 | */ |
| 3315 | |
| 3316 | BufferObject *self; |
| 3317 | |
| 3318 | if (BUF_PYTHON_REF(buf) != NULL) |
| 3319 | { |
| 3320 | self = BUF_PYTHON_REF(buf); |
| 3321 | Py_INCREF(self); |
| 3322 | } |
| 3323 | else |
| 3324 | { |
| 3325 | self = PyObject_NEW(BufferObject, &BufferType); |
| 3326 | if (self == NULL) |
| 3327 | return NULL; |
| 3328 | self->buf = buf; |
| 3329 | BUF_PYTHON_REF(buf) = self; |
| 3330 | } |
| 3331 | |
| 3332 | return (PyObject *)(self); |
| 3333 | } |
| 3334 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3335 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3336 | BufferDestructor(BufferObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3337 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3338 | if (self->buf && self->buf != INVALID_BUFFER_VALUE) |
| 3339 | BUF_PYTHON_REF(self->buf) = NULL; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3340 | |
| 3341 | DESTRUCTOR_FINISH(self); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3342 | } |
| 3343 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3344 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3345 | BufferLength(BufferObject *self) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3346 | { |
| 3347 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3348 | if (CheckBuffer(self)) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3349 | return -1; /* ??? */ |
| 3350 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3351 | return (PyInt)(self->buf->b_ml.ml_line_count); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3355 | BufferItem(BufferObject *self, PyInt n) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3356 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3357 | return RBItem(self, n, 1, -1); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3361 | BufferSlice(BufferObject *self, PyInt lo, PyInt hi) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3362 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3363 | return RBSlice(self, lo, hi, 1, -1); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3364 | } |
| 3365 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3366 | static char *BufferAttrs[] = { |
| 3367 | "name", "number", "vars", "options", "valid", |
| 3368 | NULL |
| 3369 | }; |
| 3370 | |
| 3371 | static PyObject * |
| 3372 | BufferDir(PyObject *self) |
| 3373 | { |
| 3374 | return ObjectDir(self, BufferAttrs); |
| 3375 | } |
| 3376 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3377 | static PyObject * |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 3378 | BufferAttrValid(BufferObject *self, char *name) |
| 3379 | { |
| 3380 | PyObject *r; |
| 3381 | |
| 3382 | if (strcmp(name, "valid") != 0) |
| 3383 | return NULL; |
| 3384 | |
| 3385 | r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True); |
| 3386 | Py_INCREF(r); |
| 3387 | return r; |
| 3388 | } |
| 3389 | |
| 3390 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3391 | BufferAttr(BufferObject *self, char *name) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3392 | { |
| 3393 | if (strcmp(name, "name") == 0) |
Bram Moolenaar | 432b09c | 2013-05-29 22:26:18 +0200 | [diff] [blame] | 3394 | return PyString_FromString((self->buf->b_ffname == NULL |
| 3395 | ? "" : (char *) self->buf->b_ffname)); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3396 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3397 | return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3398 | else if (strcmp(name, "vars") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3399 | return DictionaryNew(self->buf->b_vars); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3400 | else if (strcmp(name, "options") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3401 | return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer, |
| 3402 | (PyObject *) self); |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3403 | else if (strcmp(name, "__members__") == 0) |
| 3404 | return ObjectDir(NULL, BufferAttrs); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3405 | else |
| 3406 | return NULL; |
| 3407 | } |
| 3408 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 3409 | static int |
| 3410 | BufferSetattr(BufferObject *self, char *name, PyObject *valObject) |
| 3411 | { |
| 3412 | if (CheckBuffer(self)) |
| 3413 | return -1; |
| 3414 | |
| 3415 | if (strcmp(name, "name") == 0) |
| 3416 | { |
| 3417 | char_u *val; |
| 3418 | aco_save_T aco; |
| 3419 | int r; |
| 3420 | PyObject *todecref; |
| 3421 | |
| 3422 | if (!(val = StringToChars(valObject, &todecref))) |
| 3423 | return -1; |
| 3424 | |
| 3425 | VimTryStart(); |
| 3426 | /* Using aucmd_*: autocommands will be executed by rename_buffer */ |
| 3427 | aucmd_prepbuf(&aco, self->buf); |
| 3428 | r = rename_buffer(val); |
| 3429 | aucmd_restbuf(&aco); |
| 3430 | Py_XDECREF(todecref); |
| 3431 | if (VimTryEnd()) |
| 3432 | return -1; |
| 3433 | |
| 3434 | if (r == FAIL) |
| 3435 | { |
| 3436 | PyErr_SetVim(_("failed to rename buffer")); |
| 3437 | return -1; |
| 3438 | } |
| 3439 | return 0; |
| 3440 | } |
| 3441 | else |
| 3442 | { |
| 3443 | PyErr_SetString(PyExc_AttributeError, name); |
| 3444 | return -1; |
| 3445 | } |
| 3446 | } |
| 3447 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3448 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3449 | BufferAppend(BufferObject *self, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3450 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3451 | return RBAppend(self, args, 1, -1, NULL); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3455 | BufferMark(BufferObject *self, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3456 | { |
| 3457 | pos_T *posp; |
| 3458 | char *pmark; |
| 3459 | char mark; |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 3460 | buf_T *savebuf; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3461 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3462 | if (CheckBuffer(self)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3463 | return NULL; |
| 3464 | |
| 3465 | if (!PyArg_ParseTuple(args, "s", &pmark)) |
| 3466 | return NULL; |
| 3467 | mark = *pmark; |
| 3468 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3469 | VimTryStart(); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3470 | switch_buffer(&savebuf, self->buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3471 | posp = getmark(mark, FALSE); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 3472 | restore_buffer(savebuf); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3473 | if (VimTryEnd()) |
| 3474 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3475 | |
| 3476 | if (posp == NULL) |
| 3477 | { |
| 3478 | PyErr_SetVim(_("invalid mark name")); |
| 3479 | return NULL; |
| 3480 | } |
| 3481 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3482 | if (posp->lnum <= 0) |
| 3483 | { |
| 3484 | /* Or raise an error? */ |
| 3485 | Py_INCREF(Py_None); |
| 3486 | return Py_None; |
| 3487 | } |
| 3488 | |
| 3489 | return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
| 3490 | } |
| 3491 | |
| 3492 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3493 | BufferRange(BufferObject *self, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3494 | { |
| 3495 | PyInt start; |
| 3496 | PyInt end; |
| 3497 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3498 | if (CheckBuffer(self)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3499 | return NULL; |
| 3500 | |
| 3501 | if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
| 3502 | return NULL; |
| 3503 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3504 | return RangeNew(self->buf, start, end); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3505 | } |
| 3506 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3507 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3508 | BufferRepr(BufferObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3509 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3510 | if (self->buf == INVALID_BUFFER_VALUE) |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 3511 | return PyString_FromFormat("<buffer object (deleted) at %p>", self); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3512 | else |
| 3513 | { |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 3514 | char *name = (char *)self->buf->b_fname; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3515 | |
| 3516 | if (name == NULL) |
| 3517 | name = ""; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3518 | |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame^] | 3519 | return PyString_FromFormat("<buffer %s>", name); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3520 | } |
| 3521 | } |
| 3522 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3523 | static struct PyMethodDef BufferMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 3524 | /* name, function, calling, documentation */ |
| 3525 | {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, |
| 3526 | {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" }, |
| 3527 | {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3528 | {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""}, |
| 3529 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3530 | }; |
| 3531 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3532 | /* |
| 3533 | * Buffer list object - Implementation |
| 3534 | */ |
| 3535 | |
| 3536 | static PyTypeObject BufMapType; |
| 3537 | |
| 3538 | typedef struct |
| 3539 | { |
| 3540 | PyObject_HEAD |
| 3541 | } BufMapObject; |
| 3542 | |
| 3543 | static PyInt |
| 3544 | BufMapLength(PyObject *self UNUSED) |
| 3545 | { |
| 3546 | buf_T *b = firstbuf; |
| 3547 | PyInt n = 0; |
| 3548 | |
| 3549 | while (b) |
| 3550 | { |
| 3551 | ++n; |
| 3552 | b = b->b_next; |
| 3553 | } |
| 3554 | |
| 3555 | return n; |
| 3556 | } |
| 3557 | |
| 3558 | static PyObject * |
| 3559 | BufMapItem(PyObject *self UNUSED, PyObject *keyObject) |
| 3560 | { |
| 3561 | buf_T *b; |
| 3562 | int bnr; |
| 3563 | |
| 3564 | #if PY_MAJOR_VERSION < 3 |
| 3565 | if (PyInt_Check(keyObject)) |
| 3566 | bnr = PyInt_AsLong(keyObject); |
| 3567 | else |
| 3568 | #endif |
| 3569 | if (PyLong_Check(keyObject)) |
| 3570 | bnr = PyLong_AsLong(keyObject); |
| 3571 | else |
| 3572 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 3573 | PyErr_SetString(PyExc_TypeError, _("key must be integer")); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3574 | return NULL; |
| 3575 | } |
| 3576 | |
| 3577 | b = buflist_findnr(bnr); |
| 3578 | |
| 3579 | if (b) |
| 3580 | return BufferNew(b); |
| 3581 | else |
| 3582 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 3583 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3584 | return NULL; |
| 3585 | } |
| 3586 | } |
| 3587 | |
| 3588 | static void |
| 3589 | BufMapIterDestruct(PyObject *buffer) |
| 3590 | { |
| 3591 | /* Iteration was stopped before all buffers were processed */ |
| 3592 | if (buffer) |
| 3593 | { |
| 3594 | Py_DECREF(buffer); |
| 3595 | } |
| 3596 | } |
| 3597 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3598 | static int |
| 3599 | BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg) |
| 3600 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3601 | if (buffer) |
| 3602 | Py_VISIT(buffer); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3603 | return 0; |
| 3604 | } |
| 3605 | |
| 3606 | static int |
| 3607 | BufMapIterClear(PyObject **buffer) |
| 3608 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3609 | if (*buffer) |
| 3610 | Py_CLEAR(*buffer); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3611 | return 0; |
| 3612 | } |
| 3613 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3614 | static PyObject * |
| 3615 | BufMapIterNext(PyObject **buffer) |
| 3616 | { |
| 3617 | PyObject *next; |
| 3618 | PyObject *r; |
| 3619 | |
| 3620 | if (!*buffer) |
| 3621 | return NULL; |
| 3622 | |
| 3623 | r = *buffer; |
| 3624 | |
| 3625 | if (CheckBuffer((BufferObject *)(r))) |
| 3626 | { |
| 3627 | *buffer = NULL; |
| 3628 | return NULL; |
| 3629 | } |
| 3630 | |
| 3631 | if (!((BufferObject *)(r))->buf->b_next) |
| 3632 | next = NULL; |
| 3633 | else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next))) |
| 3634 | return NULL; |
| 3635 | *buffer = next; |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 3636 | /* Do not increment reference: we no longer hold it (decref), but whoever |
| 3637 | * on other side will hold (incref). Decref+incref = nothing. */ |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3638 | return r; |
| 3639 | } |
| 3640 | |
| 3641 | static PyObject * |
| 3642 | BufMapIter(PyObject *self UNUSED) |
| 3643 | { |
| 3644 | PyObject *buffer; |
| 3645 | |
| 3646 | buffer = BufferNew(firstbuf); |
| 3647 | return IterNew(buffer, |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3648 | (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext, |
| 3649 | (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | static PyMappingMethods BufMapAsMapping = { |
| 3653 | (lenfunc) BufMapLength, |
| 3654 | (binaryfunc) BufMapItem, |
| 3655 | (objobjargproc) 0, |
| 3656 | }; |
| 3657 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3658 | /* Current items object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3659 | */ |
| 3660 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3661 | static char *CurrentAttrs[] = { |
| 3662 | "buffer", "window", "line", "range", "tabpage", |
| 3663 | NULL |
| 3664 | }; |
| 3665 | |
| 3666 | static PyObject * |
| 3667 | CurrentDir(PyObject *self) |
| 3668 | { |
| 3669 | return ObjectDir(self, CurrentAttrs); |
| 3670 | } |
| 3671 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3672 | static PyObject * |
| 3673 | CurrentGetattr(PyObject *self UNUSED, char *name) |
| 3674 | { |
| 3675 | if (strcmp(name, "buffer") == 0) |
| 3676 | return (PyObject *)BufferNew(curbuf); |
| 3677 | else if (strcmp(name, "window") == 0) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 3678 | return (PyObject *)WindowNew(curwin, curtab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 3679 | else if (strcmp(name, "tabpage") == 0) |
| 3680 | return (PyObject *)TabPageNew(curtab); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3681 | else if (strcmp(name, "line") == 0) |
| 3682 | return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); |
| 3683 | else if (strcmp(name, "range") == 0) |
| 3684 | return RangeNew(curbuf, RangeStart, RangeEnd); |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3685 | else if (strcmp(name, "__members__") == 0) |
| 3686 | return ObjectDir(NULL, CurrentAttrs); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3687 | else |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3688 | #if PY_MAJOR_VERSION < 3 |
| 3689 | return Py_FindMethod(WindowMethods, self, name); |
| 3690 | #else |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3691 | return NULL; |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3692 | #endif |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | static int |
| 3696 | CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) |
| 3697 | { |
| 3698 | if (strcmp(name, "line") == 0) |
| 3699 | { |
| 3700 | if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) |
| 3701 | return -1; |
| 3702 | |
| 3703 | return 0; |
| 3704 | } |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3705 | else if (strcmp(name, "buffer") == 0) |
| 3706 | { |
| 3707 | int count; |
| 3708 | |
| 3709 | if (value->ob_type != &BufferType) |
| 3710 | { |
| 3711 | PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object")); |
| 3712 | return -1; |
| 3713 | } |
| 3714 | |
| 3715 | if (CheckBuffer((BufferObject *)(value))) |
| 3716 | return -1; |
| 3717 | count = ((BufferObject *)(value))->buf->b_fnum; |
| 3718 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3719 | VimTryStart(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3720 | if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) |
| 3721 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3722 | if (VimTryEnd()) |
| 3723 | return -1; |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3724 | PyErr_SetVim(_("failed to switch to given buffer")); |
| 3725 | return -1; |
| 3726 | } |
| 3727 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3728 | return VimTryEnd(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3729 | } |
| 3730 | else if (strcmp(name, "window") == 0) |
| 3731 | { |
| 3732 | int count; |
| 3733 | |
| 3734 | if (value->ob_type != &WindowType) |
| 3735 | { |
| 3736 | PyErr_SetString(PyExc_TypeError, _("expected vim.window object")); |
| 3737 | return -1; |
| 3738 | } |
| 3739 | |
| 3740 | if (CheckWindow((WindowObject *)(value))) |
| 3741 | return -1; |
| 3742 | count = get_win_number(((WindowObject *)(value))->win, firstwin); |
| 3743 | |
| 3744 | if (!count) |
| 3745 | { |
| 3746 | PyErr_SetString(PyExc_ValueError, |
| 3747 | _("failed to find window in the current tab page")); |
| 3748 | return -1; |
| 3749 | } |
| 3750 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3751 | VimTryStart(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3752 | win_goto(((WindowObject *)(value))->win); |
| 3753 | if (((WindowObject *)(value))->win != curwin) |
| 3754 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3755 | if (VimTryEnd()) |
| 3756 | return -1; |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3757 | PyErr_SetString(PyExc_RuntimeError, |
| 3758 | _("did not switch to the specified window")); |
| 3759 | return -1; |
| 3760 | } |
| 3761 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3762 | return VimTryEnd(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3763 | } |
| 3764 | else if (strcmp(name, "tabpage") == 0) |
| 3765 | { |
| 3766 | if (value->ob_type != &TabPageType) |
| 3767 | { |
| 3768 | PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object")); |
| 3769 | return -1; |
| 3770 | } |
| 3771 | |
| 3772 | if (CheckTabPage((TabPageObject *)(value))) |
| 3773 | return -1; |
| 3774 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3775 | VimTryStart(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3776 | goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE); |
| 3777 | if (((TabPageObject *)(value))->tab != curtab) |
| 3778 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3779 | if (VimTryEnd()) |
| 3780 | return -1; |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3781 | PyErr_SetString(PyExc_RuntimeError, |
| 3782 | _("did not switch to the specified tab page")); |
| 3783 | return -1; |
| 3784 | } |
| 3785 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3786 | return VimTryEnd(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3787 | } |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3788 | else |
| 3789 | { |
| 3790 | PyErr_SetString(PyExc_AttributeError, name); |
| 3791 | return -1; |
| 3792 | } |
| 3793 | } |
| 3794 | |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 3795 | static struct PyMethodDef CurrentMethods[] = { |
| 3796 | /* name, function, calling, documentation */ |
| 3797 | {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""}, |
| 3798 | { NULL, NULL, 0, NULL} |
| 3799 | }; |
| 3800 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3801 | static void |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3802 | init_range_cmd(exarg_T *eap) |
| 3803 | { |
| 3804 | RangeStart = eap->line1; |
| 3805 | RangeEnd = eap->line2; |
| 3806 | } |
| 3807 | |
| 3808 | static void |
| 3809 | init_range_eval(typval_T *rettv UNUSED) |
| 3810 | { |
| 3811 | RangeStart = (PyInt) curwin->w_cursor.lnum; |
| 3812 | RangeEnd = RangeStart; |
| 3813 | } |
| 3814 | |
| 3815 | static void |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3816 | run_cmd(const char *cmd, void *arg UNUSED |
| 3817 | #ifdef PY_CAN_RECURSE |
| 3818 | , PyGILState_STATE *pygilstate UNUSED |
| 3819 | #endif |
| 3820 | ) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3821 | { |
| 3822 | PyRun_SimpleString((char *) cmd); |
| 3823 | } |
| 3824 | |
| 3825 | static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n "; |
| 3826 | static int code_hdr_len = 30; |
| 3827 | |
| 3828 | static void |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3829 | run_do(const char *cmd, void *arg UNUSED |
| 3830 | #ifdef PY_CAN_RECURSE |
| 3831 | , PyGILState_STATE *pygilstate |
| 3832 | #endif |
| 3833 | ) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3834 | { |
| 3835 | PyInt lnum; |
| 3836 | size_t len; |
| 3837 | char *code; |
| 3838 | int status; |
| 3839 | PyObject *pyfunc, *pymain; |
| 3840 | |
Bram Moolenaar | 4ac6676 | 2013-05-28 22:31:46 +0200 | [diff] [blame] | 3841 | if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3842 | { |
| 3843 | EMSG(_("cannot save undo information")); |
| 3844 | return; |
| 3845 | } |
| 3846 | |
| 3847 | len = code_hdr_len + STRLEN(cmd); |
| 3848 | code = PyMem_New(char, len + 1); |
| 3849 | memcpy(code, code_hdr, code_hdr_len); |
| 3850 | STRCPY(code + code_hdr_len, cmd); |
| 3851 | status = PyRun_SimpleString(code); |
| 3852 | PyMem_Free(code); |
| 3853 | |
| 3854 | if (status) |
| 3855 | { |
| 3856 | EMSG(_("failed to run the code")); |
| 3857 | return; |
| 3858 | } |
| 3859 | |
| 3860 | status = 0; |
| 3861 | pymain = PyImport_AddModule("__main__"); |
| 3862 | pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3863 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3864 | PyGILState_Release(*pygilstate); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3865 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3866 | |
| 3867 | for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) |
| 3868 | { |
| 3869 | PyObject *line, *linenr, *ret; |
| 3870 | |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3871 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3872 | *pygilstate = PyGILState_Ensure(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3873 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3874 | if (!(line = GetBufferLine(curbuf, lnum))) |
| 3875 | goto err; |
| 3876 | if (!(linenr = PyInt_FromLong((long) lnum))) |
| 3877 | { |
| 3878 | Py_DECREF(line); |
| 3879 | goto err; |
| 3880 | } |
| 3881 | ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL); |
| 3882 | Py_DECREF(line); |
| 3883 | Py_DECREF(linenr); |
| 3884 | if (!ret) |
| 3885 | goto err; |
| 3886 | |
| 3887 | if (ret != Py_None) |
| 3888 | if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL) |
| 3889 | goto err; |
| 3890 | |
| 3891 | Py_XDECREF(ret); |
| 3892 | PythonIO_Flush(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3893 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3894 | PyGILState_Release(*pygilstate); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3895 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3896 | } |
| 3897 | goto out; |
| 3898 | err: |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3899 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3900 | *pygilstate = PyGILState_Ensure(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3901 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3902 | PyErr_PrintEx(0); |
| 3903 | PythonIO_Flush(); |
| 3904 | status = 1; |
| 3905 | out: |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3906 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3907 | if (!status) |
| 3908 | *pygilstate = PyGILState_Ensure(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3909 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3910 | Py_DECREF(pyfunc); |
| 3911 | PyObject_SetAttrString(pymain, DOPY_FUNC, NULL); |
| 3912 | if (status) |
| 3913 | return; |
| 3914 | check_cursor(); |
| 3915 | update_curbuf(NOT_VALID); |
| 3916 | } |
| 3917 | |
| 3918 | static void |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3919 | run_eval(const char *cmd, typval_T *rettv |
| 3920 | #ifdef PY_CAN_RECURSE |
| 3921 | , PyGILState_STATE *pygilstate UNUSED |
| 3922 | #endif |
| 3923 | ) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3924 | { |
| 3925 | PyObject *r; |
| 3926 | |
| 3927 | r = PyRun_String((char *) cmd, Py_eval_input, globals, globals); |
| 3928 | if (r == NULL) |
| 3929 | { |
| 3930 | if (PyErr_Occurred() && !msg_silent) |
| 3931 | PyErr_PrintEx(0); |
| 3932 | EMSG(_("E858: Eval did not return a valid python object")); |
| 3933 | } |
| 3934 | else |
| 3935 | { |
| 3936 | if (ConvertFromPyObject(r, rettv) == -1) |
| 3937 | EMSG(_("E859: Failed to convert returned python object to vim value")); |
| 3938 | Py_DECREF(r); |
| 3939 | } |
| 3940 | PyErr_Clear(); |
| 3941 | } |
| 3942 | |
| 3943 | static void |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3944 | set_ref_in_py(const int copyID) |
| 3945 | { |
| 3946 | pylinkedlist_T *cur; |
| 3947 | dict_T *dd; |
| 3948 | list_T *ll; |
| 3949 | |
| 3950 | if (lastdict != NULL) |
| 3951 | for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) |
| 3952 | { |
| 3953 | dd = ((DictionaryObject *) (cur->pll_obj))->dict; |
| 3954 | if (dd->dv_copyID != copyID) |
| 3955 | { |
| 3956 | dd->dv_copyID = copyID; |
| 3957 | set_ref_in_ht(&dd->dv_hashtab, copyID); |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | if (lastlist != NULL) |
| 3962 | for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) |
| 3963 | { |
| 3964 | ll = ((ListObject *) (cur->pll_obj))->list; |
| 3965 | if (ll->lv_copyID != copyID) |
| 3966 | { |
| 3967 | ll->lv_copyID = copyID; |
| 3968 | set_ref_in_list(ll, copyID); |
| 3969 | } |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | static int |
| 3974 | set_string_copy(char_u *str, typval_T *tv) |
| 3975 | { |
| 3976 | tv->vval.v_string = vim_strsave(str); |
| 3977 | if (tv->vval.v_string == NULL) |
| 3978 | { |
| 3979 | PyErr_NoMemory(); |
| 3980 | return -1; |
| 3981 | } |
| 3982 | return 0; |
| 3983 | } |
| 3984 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3985 | static int |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 3986 | pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3987 | { |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 3988 | dict_T *dict; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3989 | char_u *key; |
| 3990 | dictitem_T *di; |
| 3991 | PyObject *keyObject; |
| 3992 | PyObject *valObject; |
| 3993 | Py_ssize_t iter = 0; |
| 3994 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 3995 | if (!(dict = dict_alloc())) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3996 | return -1; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3997 | |
| 3998 | tv->v_type = VAR_DICT; |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 3999 | tv->vval.v_dict = dict; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4000 | |
| 4001 | while (PyDict_Next(obj, &iter, &keyObject, &valObject)) |
| 4002 | { |
| 4003 | DICTKEY_DECL |
| 4004 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4005 | if (keyObject == NULL || valObject == NULL) |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4006 | { |
| 4007 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4008 | return -1; |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4009 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4010 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4011 | if (!DICTKEY_SET_KEY) |
| 4012 | { |
| 4013 | dict_unref(dict); |
| 4014 | return -1; |
| 4015 | } |
| 4016 | DICTKEY_CHECK_EMPTY(-1) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4017 | |
| 4018 | di = dictitem_alloc(key); |
| 4019 | |
| 4020 | DICTKEY_UNREF |
| 4021 | |
| 4022 | if (di == NULL) |
| 4023 | { |
| 4024 | PyErr_NoMemory(); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4025 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4026 | return -1; |
| 4027 | } |
| 4028 | di->di_tv.v_lock = 0; |
| 4029 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4030 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4031 | { |
| 4032 | vim_free(di); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4033 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4034 | return -1; |
| 4035 | } |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4036 | |
| 4037 | if (dict_add(dict, di) == FAIL) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4038 | { |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4039 | clear_tv(&di->di_tv); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4040 | vim_free(di); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4041 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4042 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 4043 | return -1; |
| 4044 | } |
| 4045 | } |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4046 | |
| 4047 | --dict->dv_refcount; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4048 | return 0; |
| 4049 | } |
| 4050 | |
| 4051 | static int |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4052 | pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4053 | { |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4054 | dict_T *dict; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4055 | char_u *key; |
| 4056 | dictitem_T *di; |
| 4057 | PyObject *list; |
| 4058 | PyObject *litem; |
| 4059 | PyObject *keyObject; |
| 4060 | PyObject *valObject; |
| 4061 | Py_ssize_t lsize; |
| 4062 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4063 | if (!(dict = dict_alloc())) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4064 | return -1; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4065 | |
| 4066 | tv->v_type = VAR_DICT; |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4067 | tv->vval.v_dict = dict; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4068 | |
| 4069 | list = PyMapping_Items(obj); |
| 4070 | if (list == NULL) |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4071 | { |
| 4072 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4073 | return -1; |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4074 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4075 | lsize = PyList_Size(list); |
| 4076 | while (lsize--) |
| 4077 | { |
| 4078 | DICTKEY_DECL |
| 4079 | |
| 4080 | litem = PyList_GetItem(list, lsize); |
| 4081 | if (litem == NULL) |
| 4082 | { |
| 4083 | Py_DECREF(list); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4084 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4085 | return -1; |
| 4086 | } |
| 4087 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4088 | if (!(keyObject = PyTuple_GetItem(litem, 0))) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4089 | { |
| 4090 | Py_DECREF(list); |
| 4091 | Py_DECREF(litem); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4092 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4093 | return -1; |
| 4094 | } |
| 4095 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4096 | if (!DICTKEY_SET_KEY) |
| 4097 | { |
| 4098 | dict_unref(dict); |
| 4099 | Py_DECREF(list); |
| 4100 | Py_DECREF(litem); |
| 4101 | DICTKEY_UNREF |
| 4102 | return -1; |
| 4103 | } |
| 4104 | DICTKEY_CHECK_EMPTY(-1) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4105 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4106 | if (!(valObject = PyTuple_GetItem(litem, 1))) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4107 | { |
| 4108 | Py_DECREF(list); |
| 4109 | Py_DECREF(litem); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4110 | dict_unref(dict); |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4111 | DICTKEY_UNREF |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4112 | return -1; |
| 4113 | } |
| 4114 | |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4115 | Py_DECREF(litem); |
| 4116 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4117 | di = dictitem_alloc(key); |
| 4118 | |
| 4119 | DICTKEY_UNREF |
| 4120 | |
| 4121 | if (di == NULL) |
| 4122 | { |
| 4123 | Py_DECREF(list); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4124 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4125 | PyErr_NoMemory(); |
| 4126 | return -1; |
| 4127 | } |
| 4128 | di->di_tv.v_lock = 0; |
| 4129 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4130 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4131 | { |
| 4132 | vim_free(di); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4133 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4134 | Py_DECREF(list); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4135 | return -1; |
| 4136 | } |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4137 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4138 | if (dict_add(dict, di) == FAIL) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4139 | { |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4140 | dictitem_free(di); |
| 4141 | dict_unref(dict); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4142 | Py_DECREF(list); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4143 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 4144 | return -1; |
| 4145 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4146 | } |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4147 | --dict->dv_refcount; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4148 | Py_DECREF(list); |
| 4149 | return 0; |
| 4150 | } |
| 4151 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4152 | static list_T * |
| 4153 | py_list_alloc() |
| 4154 | { |
| 4155 | list_T *r; |
| 4156 | |
| 4157 | if (!(r = list_alloc())) |
| 4158 | { |
| 4159 | PyErr_NoMemory(); |
| 4160 | return NULL; |
| 4161 | } |
| 4162 | ++r->lv_refcount; |
| 4163 | |
| 4164 | return r; |
| 4165 | } |
| 4166 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4167 | static int |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4168 | pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4169 | { |
| 4170 | list_T *l; |
| 4171 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4172 | if (!(l = py_list_alloc())) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4173 | return -1; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4174 | |
| 4175 | tv->v_type = VAR_LIST; |
| 4176 | tv->vval.v_list = l; |
| 4177 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4178 | if (list_py_concat(l, obj, lookup_dict) == -1) |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4179 | { |
| 4180 | list_unref(l); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4181 | return -1; |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4182 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4183 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4184 | --l->lv_refcount; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4185 | return 0; |
| 4186 | } |
| 4187 | |
| 4188 | static int |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4189 | pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4190 | { |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4191 | PyObject *iterator; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4192 | PyObject *item; |
| 4193 | list_T *l; |
| 4194 | listitem_T *li; |
| 4195 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4196 | if (!(l = py_list_alloc())) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4197 | return -1; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4198 | |
| 4199 | tv->vval.v_list = l; |
| 4200 | tv->v_type = VAR_LIST; |
| 4201 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4202 | if (!(iterator = PyObject_GetIter(obj))) |
| 4203 | { |
| 4204 | list_unref(l); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4205 | return -1; |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4206 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4207 | |
Bram Moolenaar | c836679 | 2013-05-29 22:46:26 +0200 | [diff] [blame] | 4208 | while ((item = PyIter_Next(iterator))) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4209 | { |
| 4210 | li = listitem_alloc(); |
| 4211 | if (li == NULL) |
| 4212 | { |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4213 | list_unref(l); |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4214 | Py_DECREF(iterator); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4215 | PyErr_NoMemory(); |
| 4216 | return -1; |
| 4217 | } |
| 4218 | li->li_tv.v_lock = 0; |
| 4219 | |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4220 | if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1) |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4221 | { |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4222 | list_unref(l); |
| 4223 | listitem_free(li); |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4224 | Py_DECREF(item); |
| 4225 | Py_DECREF(iterator); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4226 | return -1; |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4227 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4228 | |
| 4229 | list_append(l, li); |
| 4230 | |
| 4231 | Py_DECREF(item); |
| 4232 | } |
| 4233 | |
| 4234 | Py_DECREF(iterator); |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4235 | |
| 4236 | /* Iterator may have finished due to an exception */ |
| 4237 | if (PyErr_Occurred()) |
| 4238 | { |
| 4239 | list_unref(l); |
| 4240 | return -1; |
| 4241 | } |
| 4242 | |
| 4243 | --l->lv_refcount; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 4244 | return 0; |
| 4245 | } |
| 4246 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4247 | typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
| 4248 | |
| 4249 | static int |
| 4250 | convert_dl(PyObject *obj, typval_T *tv, |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4251 | pytotvfunc py_to_tv, PyObject *lookup_dict) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4252 | { |
| 4253 | PyObject *capsule; |
| 4254 | char hexBuf[sizeof(void *) * 2 + 3]; |
| 4255 | |
| 4256 | sprintf(hexBuf, "%p", obj); |
| 4257 | |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4258 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4259 | capsule = PyDict_GetItemString(lookup_dict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4260 | # else |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4261 | capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4262 | # endif |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 4263 | if (capsule == NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4264 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4265 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4266 | capsule = PyCapsule_New(tv, NULL, NULL); |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 4267 | # else |
| 4268 | capsule = PyCObject_FromVoidPtr(tv, NULL); |
| 4269 | # endif |
Bram Moolenaar | a03e631 | 2013-05-29 22:49:26 +0200 | [diff] [blame] | 4270 | if (PyDict_SetItemString(lookup_dict, hexBuf, capsule)) |
| 4271 | { |
| 4272 | Py_DECREF(capsule); |
| 4273 | tv->v_type = VAR_UNKNOWN; |
| 4274 | return -1; |
| 4275 | } |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4276 | if (py_to_tv(obj, tv, lookup_dict) == -1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4277 | { |
| 4278 | tv->v_type = VAR_UNKNOWN; |
| 4279 | return -1; |
| 4280 | } |
| 4281 | /* As we are not using copy_tv which increments reference count we must |
| 4282 | * do it ourself. */ |
| 4283 | switch(tv->v_type) |
| 4284 | { |
| 4285 | case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; |
| 4286 | case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; |
| 4287 | } |
| 4288 | } |
| 4289 | else |
| 4290 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4291 | typval_T *v; |
| 4292 | |
| 4293 | # ifdef PY_USE_CAPSULE |
| 4294 | v = PyCapsule_GetPointer(capsule, NULL); |
| 4295 | # else |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 4296 | v = PyCObject_AsVoidPtr(capsule); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4297 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4298 | copy_tv(v, tv); |
| 4299 | } |
| 4300 | return 0; |
| 4301 | } |
| 4302 | |
| 4303 | static int |
| 4304 | ConvertFromPyObject(PyObject *obj, typval_T *tv) |
| 4305 | { |
| 4306 | PyObject *lookup_dict; |
| 4307 | int r; |
| 4308 | |
Bram Moolenaar | 9bb77d6 | 2013-05-30 12:14:49 +0200 | [diff] [blame] | 4309 | if (!(lookup_dict = PyDict_New())) |
| 4310 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4311 | r = _ConvertFromPyObject(obj, tv, lookup_dict); |
| 4312 | Py_DECREF(lookup_dict); |
| 4313 | return r; |
| 4314 | } |
| 4315 | |
| 4316 | static int |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4317 | _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4318 | { |
| 4319 | if (obj->ob_type == &DictionaryType) |
| 4320 | { |
| 4321 | tv->v_type = VAR_DICT; |
| 4322 | tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); |
| 4323 | ++tv->vval.v_dict->dv_refcount; |
| 4324 | } |
| 4325 | else if (obj->ob_type == &ListType) |
| 4326 | { |
| 4327 | tv->v_type = VAR_LIST; |
| 4328 | tv->vval.v_list = (((ListObject *)(obj))->list); |
| 4329 | ++tv->vval.v_list->lv_refcount; |
| 4330 | } |
| 4331 | else if (obj->ob_type == &FunctionType) |
| 4332 | { |
| 4333 | if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) |
| 4334 | return -1; |
| 4335 | |
| 4336 | tv->v_type = VAR_FUNC; |
| 4337 | func_ref(tv->vval.v_string); |
| 4338 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4339 | else if (PyBytes_Check(obj)) |
| 4340 | { |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 4341 | char_u *result; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4342 | |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 4343 | if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) |
| 4344 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4345 | if (result == NULL) |
| 4346 | return -1; |
| 4347 | |
| 4348 | if (set_string_copy(result, tv) == -1) |
| 4349 | return -1; |
| 4350 | |
| 4351 | tv->v_type = VAR_STRING; |
| 4352 | } |
| 4353 | else if (PyUnicode_Check(obj)) |
| 4354 | { |
| 4355 | PyObject *bytes; |
| 4356 | char_u *result; |
| 4357 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4358 | bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); |
| 4359 | if (bytes == NULL) |
| 4360 | return -1; |
| 4361 | |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 4362 | if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
| 4363 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4364 | if (result == NULL) |
| 4365 | return -1; |
| 4366 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 4367 | if (set_string_copy(result, tv)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4368 | { |
| 4369 | Py_XDECREF(bytes); |
| 4370 | return -1; |
| 4371 | } |
| 4372 | Py_XDECREF(bytes); |
| 4373 | |
| 4374 | tv->v_type = VAR_STRING; |
| 4375 | } |
Bram Moolenaar | 335e0b6 | 2013-04-24 13:47:45 +0200 | [diff] [blame] | 4376 | #if PY_MAJOR_VERSION < 3 |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4377 | else if (PyInt_Check(obj)) |
| 4378 | { |
| 4379 | tv->v_type = VAR_NUMBER; |
| 4380 | tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); |
| 4381 | } |
| 4382 | #endif |
| 4383 | else if (PyLong_Check(obj)) |
| 4384 | { |
| 4385 | tv->v_type = VAR_NUMBER; |
| 4386 | tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); |
| 4387 | } |
| 4388 | else if (PyDict_Check(obj)) |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4389 | return convert_dl(obj, tv, pydict_to_tv, lookup_dict); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4390 | #ifdef FEAT_FLOAT |
| 4391 | else if (PyFloat_Check(obj)) |
| 4392 | { |
| 4393 | tv->v_type = VAR_FLOAT; |
| 4394 | tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); |
| 4395 | } |
| 4396 | #endif |
| 4397 | else if (PyIter_Check(obj)) |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4398 | return convert_dl(obj, tv, pyiter_to_tv, lookup_dict); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4399 | else if (PySequence_Check(obj)) |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4400 | return convert_dl(obj, tv, pyseq_to_tv, lookup_dict); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4401 | else if (PyMapping_Check(obj)) |
Bram Moolenaar | b38caae | 2013-05-29 22:39:52 +0200 | [diff] [blame] | 4402 | return convert_dl(obj, tv, pymap_to_tv, lookup_dict); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4403 | else |
| 4404 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 4405 | PyErr_SetString(PyExc_TypeError, |
| 4406 | _("unable to convert to vim structure")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4407 | return -1; |
| 4408 | } |
| 4409 | return 0; |
| 4410 | } |
| 4411 | |
| 4412 | static PyObject * |
| 4413 | ConvertToPyObject(typval_T *tv) |
| 4414 | { |
| 4415 | if (tv == NULL) |
| 4416 | { |
| 4417 | PyErr_SetVim(_("NULL reference passed")); |
| 4418 | return NULL; |
| 4419 | } |
| 4420 | switch (tv->v_type) |
| 4421 | { |
| 4422 | case VAR_STRING: |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 4423 | return PyBytes_FromString(tv->vval.v_string == NULL |
| 4424 | ? "" : (char *)tv->vval.v_string); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4425 | case VAR_NUMBER: |
| 4426 | return PyLong_FromLong((long) tv->vval.v_number); |
| 4427 | #ifdef FEAT_FLOAT |
| 4428 | case VAR_FLOAT: |
| 4429 | return PyFloat_FromDouble((double) tv->vval.v_float); |
| 4430 | #endif |
| 4431 | case VAR_LIST: |
| 4432 | return ListNew(tv->vval.v_list); |
| 4433 | case VAR_DICT: |
| 4434 | return DictionaryNew(tv->vval.v_dict); |
| 4435 | case VAR_FUNC: |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 4436 | return FunctionNew(tv->vval.v_string == NULL |
| 4437 | ? (char_u *)"" : tv->vval.v_string); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4438 | case VAR_UNKNOWN: |
| 4439 | Py_INCREF(Py_None); |
| 4440 | return Py_None; |
| 4441 | default: |
| 4442 | PyErr_SetVim(_("internal error: invalid value type")); |
| 4443 | return NULL; |
| 4444 | } |
| 4445 | } |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4446 | |
| 4447 | typedef struct |
| 4448 | { |
| 4449 | PyObject_HEAD |
| 4450 | } CurrentObject; |
| 4451 | static PyTypeObject CurrentType; |
| 4452 | |
| 4453 | static void |
| 4454 | init_structs(void) |
| 4455 | { |
| 4456 | vim_memset(&OutputType, 0, sizeof(OutputType)); |
| 4457 | OutputType.tp_name = "vim.message"; |
| 4458 | OutputType.tp_basicsize = sizeof(OutputObject); |
| 4459 | OutputType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4460 | OutputType.tp_doc = "vim message object"; |
| 4461 | OutputType.tp_methods = OutputMethods; |
| 4462 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4463 | OutputType.tp_getattro = (getattrofunc)OutputGetattro; |
| 4464 | OutputType.tp_setattro = (setattrofunc)OutputSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4465 | OutputType.tp_alloc = call_PyType_GenericAlloc; |
| 4466 | OutputType.tp_new = call_PyType_GenericNew; |
| 4467 | OutputType.tp_free = call_PyObject_Free; |
| 4468 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4469 | OutputType.tp_getattr = (getattrfunc)OutputGetattr; |
| 4470 | OutputType.tp_setattr = (setattrfunc)OutputSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4471 | #endif |
| 4472 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4473 | vim_memset(&IterType, 0, sizeof(IterType)); |
| 4474 | IterType.tp_name = "vim.iter"; |
| 4475 | IterType.tp_basicsize = sizeof(IterObject); |
Bram Moolenaar | 07b8864 | 2013-05-29 22:58:32 +0200 | [diff] [blame] | 4476 | IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4477 | IterType.tp_doc = "generic iterator object"; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4478 | IterType.tp_iter = (getiterfunc)IterIter; |
| 4479 | IterType.tp_iternext = (iternextfunc)IterNext; |
| 4480 | IterType.tp_dealloc = (destructor)IterDestructor; |
| 4481 | IterType.tp_traverse = (traverseproc)IterTraverse; |
| 4482 | IterType.tp_clear = (inquiry)IterClear; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4483 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4484 | vim_memset(&BufferType, 0, sizeof(BufferType)); |
| 4485 | BufferType.tp_name = "vim.buffer"; |
| 4486 | BufferType.tp_basicsize = sizeof(BufferType); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4487 | BufferType.tp_dealloc = (destructor)BufferDestructor; |
| 4488 | BufferType.tp_repr = (reprfunc)BufferRepr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4489 | BufferType.tp_as_sequence = &BufferAsSeq; |
| 4490 | BufferType.tp_as_mapping = &BufferAsMapping; |
| 4491 | BufferType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4492 | BufferType.tp_doc = "vim buffer object"; |
| 4493 | BufferType.tp_methods = BufferMethods; |
| 4494 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4495 | BufferType.tp_getattro = (getattrofunc)BufferGetattro; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 4496 | BufferType.tp_setattro = (setattrofunc)BufferSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4497 | BufferType.tp_alloc = call_PyType_GenericAlloc; |
| 4498 | BufferType.tp_new = call_PyType_GenericNew; |
| 4499 | BufferType.tp_free = call_PyObject_Free; |
| 4500 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4501 | BufferType.tp_getattr = (getattrfunc)BufferGetattr; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 4502 | BufferType.tp_setattr = (setattrfunc)BufferSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4503 | #endif |
| 4504 | |
| 4505 | vim_memset(&WindowType, 0, sizeof(WindowType)); |
| 4506 | WindowType.tp_name = "vim.window"; |
| 4507 | WindowType.tp_basicsize = sizeof(WindowObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4508 | WindowType.tp_dealloc = (destructor)WindowDestructor; |
| 4509 | WindowType.tp_repr = (reprfunc)WindowRepr; |
Bram Moolenaar | 07b8864 | 2013-05-29 22:58:32 +0200 | [diff] [blame] | 4510 | WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4511 | WindowType.tp_doc = "vim Window object"; |
| 4512 | WindowType.tp_methods = WindowMethods; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4513 | WindowType.tp_traverse = (traverseproc)WindowTraverse; |
| 4514 | WindowType.tp_clear = (inquiry)WindowClear; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4515 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4516 | WindowType.tp_getattro = (getattrofunc)WindowGetattro; |
| 4517 | WindowType.tp_setattro = (setattrofunc)WindowSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4518 | WindowType.tp_alloc = call_PyType_GenericAlloc; |
| 4519 | WindowType.tp_new = call_PyType_GenericNew; |
| 4520 | WindowType.tp_free = call_PyObject_Free; |
| 4521 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4522 | WindowType.tp_getattr = (getattrfunc)WindowGetattr; |
| 4523 | WindowType.tp_setattr = (setattrfunc)WindowSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4524 | #endif |
| 4525 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4526 | vim_memset(&TabPageType, 0, sizeof(TabPageType)); |
| 4527 | TabPageType.tp_name = "vim.tabpage"; |
| 4528 | TabPageType.tp_basicsize = sizeof(TabPageObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4529 | TabPageType.tp_dealloc = (destructor)TabPageDestructor; |
| 4530 | TabPageType.tp_repr = (reprfunc)TabPageRepr; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4531 | TabPageType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4532 | TabPageType.tp_doc = "vim tab page object"; |
| 4533 | TabPageType.tp_methods = TabPageMethods; |
| 4534 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4535 | TabPageType.tp_getattro = (getattrofunc)TabPageGetattro; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4536 | TabPageType.tp_alloc = call_PyType_GenericAlloc; |
| 4537 | TabPageType.tp_new = call_PyType_GenericNew; |
| 4538 | TabPageType.tp_free = call_PyObject_Free; |
| 4539 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4540 | TabPageType.tp_getattr = (getattrfunc)TabPageGetattr; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4541 | #endif |
| 4542 | |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 4543 | vim_memset(&BufMapType, 0, sizeof(BufMapType)); |
| 4544 | BufMapType.tp_name = "vim.bufferlist"; |
| 4545 | BufMapType.tp_basicsize = sizeof(BufMapObject); |
| 4546 | BufMapType.tp_as_mapping = &BufMapAsMapping; |
| 4547 | BufMapType.tp_flags = Py_TPFLAGS_DEFAULT; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4548 | BufMapType.tp_iter = BufMapIter; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4549 | BufferType.tp_doc = "vim buffer list"; |
| 4550 | |
| 4551 | vim_memset(&WinListType, 0, sizeof(WinListType)); |
| 4552 | WinListType.tp_name = "vim.windowlist"; |
| 4553 | WinListType.tp_basicsize = sizeof(WinListType); |
| 4554 | WinListType.tp_as_sequence = &WinListAsSeq; |
| 4555 | WinListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4556 | WinListType.tp_doc = "vim window list"; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4557 | WinListType.tp_dealloc = (destructor)WinListDestructor; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4558 | |
| 4559 | vim_memset(&TabListType, 0, sizeof(TabListType)); |
| 4560 | TabListType.tp_name = "vim.tabpagelist"; |
| 4561 | TabListType.tp_basicsize = sizeof(TabListType); |
| 4562 | TabListType.tp_as_sequence = &TabListAsSeq; |
| 4563 | TabListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4564 | TabListType.tp_doc = "vim tab page list"; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4565 | |
| 4566 | vim_memset(&RangeType, 0, sizeof(RangeType)); |
| 4567 | RangeType.tp_name = "vim.range"; |
| 4568 | RangeType.tp_basicsize = sizeof(RangeObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4569 | RangeType.tp_dealloc = (destructor)RangeDestructor; |
| 4570 | RangeType.tp_repr = (reprfunc)RangeRepr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4571 | RangeType.tp_as_sequence = &RangeAsSeq; |
| 4572 | RangeType.tp_as_mapping = &RangeAsMapping; |
Bram Moolenaar | 07b8864 | 2013-05-29 22:58:32 +0200 | [diff] [blame] | 4573 | RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4574 | RangeType.tp_doc = "vim Range object"; |
| 4575 | RangeType.tp_methods = RangeMethods; |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 4576 | RangeType.tp_traverse = (traverseproc)RangeTraverse; |
| 4577 | RangeType.tp_clear = (inquiry)RangeClear; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4578 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4579 | RangeType.tp_getattro = (getattrofunc)RangeGetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4580 | RangeType.tp_alloc = call_PyType_GenericAlloc; |
| 4581 | RangeType.tp_new = call_PyType_GenericNew; |
| 4582 | RangeType.tp_free = call_PyObject_Free; |
| 4583 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4584 | RangeType.tp_getattr = (getattrfunc)RangeGetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4585 | #endif |
| 4586 | |
| 4587 | vim_memset(&CurrentType, 0, sizeof(CurrentType)); |
| 4588 | CurrentType.tp_name = "vim.currentdata"; |
| 4589 | CurrentType.tp_basicsize = sizeof(CurrentObject); |
| 4590 | CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4591 | CurrentType.tp_doc = "vim current object"; |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 4592 | CurrentType.tp_methods = CurrentMethods; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4593 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4594 | CurrentType.tp_getattro = (getattrofunc)CurrentGetattro; |
| 4595 | CurrentType.tp_setattro = (setattrofunc)CurrentSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4596 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4597 | CurrentType.tp_getattr = (getattrfunc)CurrentGetattr; |
| 4598 | CurrentType.tp_setattr = (setattrfunc)CurrentSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4599 | #endif |
| 4600 | |
| 4601 | vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); |
| 4602 | DictionaryType.tp_name = "vim.dictionary"; |
| 4603 | DictionaryType.tp_basicsize = sizeof(DictionaryObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4604 | DictionaryType.tp_dealloc = (destructor)DictionaryDestructor; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4605 | DictionaryType.tp_as_mapping = &DictionaryAsMapping; |
| 4606 | DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4607 | DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; |
| 4608 | DictionaryType.tp_methods = DictionaryMethods; |
| 4609 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4610 | DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro; |
| 4611 | DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4612 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4613 | DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr; |
| 4614 | DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4615 | #endif |
| 4616 | |
| 4617 | vim_memset(&ListType, 0, sizeof(ListType)); |
| 4618 | ListType.tp_name = "vim.list"; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4619 | ListType.tp_dealloc = (destructor)ListDestructor; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4620 | ListType.tp_basicsize = sizeof(ListObject); |
| 4621 | ListType.tp_as_sequence = &ListAsSeq; |
| 4622 | ListType.tp_as_mapping = &ListAsMapping; |
| 4623 | ListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4624 | ListType.tp_doc = "list pushing modifications to vim structure"; |
| 4625 | ListType.tp_methods = ListMethods; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4626 | ListType.tp_iter = (getiterfunc)ListIter; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4627 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4628 | ListType.tp_getattro = (getattrofunc)ListGetattro; |
| 4629 | ListType.tp_setattro = (setattrofunc)ListSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4630 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4631 | ListType.tp_getattr = (getattrfunc)ListGetattr; |
| 4632 | ListType.tp_setattr = (setattrfunc)ListSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4633 | #endif |
| 4634 | |
| 4635 | vim_memset(&FunctionType, 0, sizeof(FunctionType)); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4636 | FunctionType.tp_name = "vim.function"; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4637 | FunctionType.tp_basicsize = sizeof(FunctionObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4638 | FunctionType.tp_dealloc = (destructor)FunctionDestructor; |
| 4639 | FunctionType.tp_call = (ternaryfunc)FunctionCall; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4640 | FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4641 | FunctionType.tp_doc = "object that calls vim function"; |
| 4642 | FunctionType.tp_methods = FunctionMethods; |
| 4643 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4644 | FunctionType.tp_getattro = (getattrofunc)FunctionGetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4645 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4646 | FunctionType.tp_getattr = (getattrfunc)FunctionGetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4647 | #endif |
| 4648 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 4649 | vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
| 4650 | OptionsType.tp_name = "vim.options"; |
| 4651 | OptionsType.tp_basicsize = sizeof(OptionsObject); |
Bram Moolenaar | 07b8864 | 2013-05-29 22:58:32 +0200 | [diff] [blame] | 4652 | OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 4653 | OptionsType.tp_doc = "object for manipulating options"; |
| 4654 | OptionsType.tp_as_mapping = &OptionsAsMapping; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4655 | OptionsType.tp_dealloc = (destructor)OptionsDestructor; |
| 4656 | OptionsType.tp_traverse = (traverseproc)OptionsTraverse; |
| 4657 | OptionsType.tp_clear = (inquiry)OptionsClear; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 4658 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4659 | #if PY_MAJOR_VERSION >= 3 |
| 4660 | vim_memset(&vimmodule, 0, sizeof(vimmodule)); |
| 4661 | vimmodule.m_name = "vim"; |
| 4662 | vimmodule.m_doc = "Vim Python interface\n"; |
| 4663 | vimmodule.m_size = -1; |
| 4664 | vimmodule.m_methods = VimMethods; |
| 4665 | #endif |
| 4666 | } |
Bram Moolenaar | 1dc2878 | 2013-05-21 19:11:01 +0200 | [diff] [blame] | 4667 | |
| 4668 | #define PYTYPE_READY(type) \ |
| 4669 | if (PyType_Ready(&type)) \ |
| 4670 | return -1; |
| 4671 | |
| 4672 | static int |
| 4673 | init_types() |
| 4674 | { |
| 4675 | PYTYPE_READY(IterType); |
| 4676 | PYTYPE_READY(BufferType); |
| 4677 | PYTYPE_READY(RangeType); |
| 4678 | PYTYPE_READY(WindowType); |
| 4679 | PYTYPE_READY(TabPageType); |
| 4680 | PYTYPE_READY(BufMapType); |
| 4681 | PYTYPE_READY(WinListType); |
| 4682 | PYTYPE_READY(TabListType); |
| 4683 | PYTYPE_READY(CurrentType); |
| 4684 | PYTYPE_READY(DictionaryType); |
| 4685 | PYTYPE_READY(ListType); |
| 4686 | PYTYPE_READY(FunctionType); |
| 4687 | PYTYPE_READY(OptionsType); |
| 4688 | PYTYPE_READY(OutputType); |
| 4689 | return 0; |
| 4690 | } |
| 4691 | |
| 4692 | static BufMapObject TheBufferMap = |
| 4693 | { |
| 4694 | PyObject_HEAD_INIT(&BufMapType) |
| 4695 | }; |
| 4696 | |
| 4697 | static WinListObject TheWindowList = |
| 4698 | { |
| 4699 | PyObject_HEAD_INIT(&WinListType) |
| 4700 | NULL |
| 4701 | }; |
| 4702 | |
| 4703 | static CurrentObject TheCurrent = |
| 4704 | { |
| 4705 | PyObject_HEAD_INIT(&CurrentType) |
| 4706 | }; |
| 4707 | |
| 4708 | static TabListObject TheTabPageList = |
| 4709 | { |
| 4710 | PyObject_HEAD_INIT(&TabListType) |
| 4711 | }; |
| 4712 | |
| 4713 | static struct numeric_constant { |
| 4714 | char *name; |
| 4715 | int value; |
| 4716 | } numeric_constants[] = { |
| 4717 | {"VAR_LOCKED", VAR_LOCKED}, |
| 4718 | {"VAR_FIXED", VAR_FIXED}, |
| 4719 | {"VAR_SCOPE", VAR_SCOPE}, |
| 4720 | {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, |
| 4721 | }; |
| 4722 | |
| 4723 | static struct object_constant { |
| 4724 | char *name; |
| 4725 | PyObject *value; |
| 4726 | } object_constants[] = { |
| 4727 | {"buffers", (PyObject *)(void *)&TheBufferMap}, |
| 4728 | {"windows", (PyObject *)(void *)&TheWindowList}, |
| 4729 | {"tabpages", (PyObject *)(void *)&TheTabPageList}, |
| 4730 | {"current", (PyObject *)(void *)&TheCurrent}, |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 4731 | |
| 4732 | {"Buffer", (PyObject *)&BufferType}, |
| 4733 | {"Range", (PyObject *)&RangeType}, |
| 4734 | {"Window", (PyObject *)&WindowType}, |
| 4735 | {"TabPage", (PyObject *)&TabPageType}, |
| 4736 | {"Dictionary", (PyObject *)&DictionaryType}, |
| 4737 | {"List", (PyObject *)&ListType}, |
| 4738 | {"Function", (PyObject *)&FunctionType}, |
| 4739 | {"Options", (PyObject *)&OptionsType}, |
Bram Moolenaar | 1dc2878 | 2013-05-21 19:11:01 +0200 | [diff] [blame] | 4740 | }; |
| 4741 | |
| 4742 | typedef int (*object_adder)(PyObject *, const char *, PyObject *); |
| 4743 | |
| 4744 | #define ADD_OBJECT(m, name, obj) \ |
| 4745 | if (add_object(m, name, obj)) \ |
| 4746 | return -1; |
| 4747 | |
| 4748 | #define ADD_CHECKED_OBJECT(m, name, obj) \ |
| 4749 | { \ |
| 4750 | PyObject *value = obj; \ |
| 4751 | if (!value) \ |
| 4752 | return -1; \ |
| 4753 | ADD_OBJECT(m, name, value); \ |
| 4754 | } |
| 4755 | |
| 4756 | static int |
| 4757 | populate_module(PyObject *m, object_adder add_object) |
| 4758 | { |
| 4759 | int i; |
| 4760 | |
| 4761 | for (i = 0; i < (int)(sizeof(numeric_constants) |
| 4762 | / sizeof(struct numeric_constant)); |
| 4763 | ++i) |
| 4764 | ADD_CHECKED_OBJECT(m, numeric_constants[i].name, |
| 4765 | PyInt_FromLong(numeric_constants[i].value)); |
| 4766 | |
| 4767 | for (i = 0; i < (int)(sizeof(object_constants) |
| 4768 | / sizeof(struct object_constant)); |
| 4769 | ++i) |
| 4770 | { |
| 4771 | PyObject *value; |
| 4772 | |
| 4773 | value = object_constants[i].value; |
| 4774 | Py_INCREF(value); |
| 4775 | ADD_OBJECT(m, object_constants[i].name, value); |
| 4776 | } |
| 4777 | |
| 4778 | if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) |
| 4779 | return -1; |
| 4780 | ADD_OBJECT(m, "error", VimError); |
| 4781 | |
| 4782 | ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict)); |
| 4783 | ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict)); |
| 4784 | ADD_CHECKED_OBJECT(m, "options", |
| 4785 | OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL)); |
| 4786 | return 0; |
| 4787 | } |