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