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