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 | /* |
| 10 | * Python extensions by Paul Moore, David Leonard, Roland Puntaier. |
| 11 | * |
| 12 | * Common code for if_python.c and if_python3.c. |
| 13 | */ |
| 14 | |
Bram Moolenaar | c1a995d | 2012-08-08 16:05:07 +0200 | [diff] [blame] | 15 | #if PY_VERSION_HEX < 0x02050000 |
| 16 | typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ |
| 17 | #endif |
| 18 | |
Bram Moolenaar | 91805fc | 2011-06-26 04:01:44 +0200 | [diff] [blame] | 19 | #ifdef FEAT_MBYTE |
| 20 | # define ENC_OPT p_enc |
| 21 | #else |
| 22 | # define ENC_OPT "latin1" |
| 23 | #endif |
| 24 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 25 | /* |
| 26 | * obtain a lock on the Vim data structures |
| 27 | */ |
| 28 | static void |
| 29 | Python_Lock_Vim(void) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * release a lock on the Vim data structures |
| 35 | */ |
| 36 | static void |
| 37 | Python_Release_Vim(void) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | /* Output object definition |
| 42 | */ |
| 43 | |
| 44 | static PyObject *OutputWrite(PyObject *, PyObject *); |
| 45 | static PyObject *OutputWritelines(PyObject *, PyObject *); |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 46 | static PyObject *OutputFlush(PyObject *, PyObject *); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 47 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 48 | /* Function to write a line, points to either msg() or emsg(). */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 49 | typedef void (*writefn)(char_u *); |
| 50 | static void writer(writefn fn, char_u *str, PyInt n); |
| 51 | |
| 52 | typedef struct |
| 53 | { |
| 54 | PyObject_HEAD |
| 55 | long softspace; |
| 56 | long error; |
| 57 | } OutputObject; |
| 58 | |
| 59 | static struct PyMethodDef OutputMethods[] = { |
| 60 | /* name, function, calling, documentation */ |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 61 | {"write", OutputWrite, 1, ""}, |
| 62 | {"writelines", OutputWritelines, 1, ""}, |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 63 | {"flush", OutputFlush, 1, ""}, |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 64 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 67 | #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
| 68 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 69 | /*************/ |
| 70 | |
| 71 | /* Output buffer management |
| 72 | */ |
| 73 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 74 | static int |
| 75 | OutputSetattr(PyObject *self, char *name, PyObject *val) |
| 76 | { |
| 77 | if (val == NULL) |
| 78 | { |
| 79 | PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | if (strcmp(name, "softspace") == 0) |
| 84 | { |
| 85 | if (!PyInt_Check(val)) |
| 86 | { |
| 87 | PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | ((OutputObject *)(self))->softspace = PyInt_AsLong(val); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
| 96 | return -1; |
| 97 | } |
| 98 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 99 | static PyObject * |
| 100 | OutputWrite(PyObject *self, PyObject *args) |
| 101 | { |
Bram Moolenaar | e8cdcef | 2012-09-12 20:21:43 +0200 | [diff] [blame] | 102 | Py_ssize_t len = 0; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 103 | char *str = NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 104 | int error = ((OutputObject *)(self))->error; |
| 105 | |
Bram Moolenaar | 2756480 | 2011-09-07 19:30:21 +0200 | [diff] [blame] | 106 | if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len)) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 107 | return NULL; |
| 108 | |
| 109 | Py_BEGIN_ALLOW_THREADS |
| 110 | Python_Lock_Vim(); |
| 111 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 112 | Python_Release_Vim(); |
| 113 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 114 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 115 | |
| 116 | Py_INCREF(Py_None); |
| 117 | return Py_None; |
| 118 | } |
| 119 | |
| 120 | static PyObject * |
| 121 | OutputWritelines(PyObject *self, PyObject *args) |
| 122 | { |
| 123 | PyInt n; |
| 124 | PyInt i; |
| 125 | PyObject *list; |
| 126 | int error = ((OutputObject *)(self))->error; |
| 127 | |
| 128 | if (!PyArg_ParseTuple(args, "O", &list)) |
| 129 | return NULL; |
| 130 | Py_INCREF(list); |
| 131 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 132 | if (!PyList_Check(list)) |
| 133 | { |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 134 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 135 | Py_DECREF(list); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | n = PyList_Size(list); |
| 140 | |
| 141 | for (i = 0; i < n; ++i) |
| 142 | { |
| 143 | PyObject *line = PyList_GetItem(list, i); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 144 | char *str = NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 145 | PyInt len; |
| 146 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 147 | if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len)) |
| 148 | { |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 149 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 150 | Py_DECREF(list); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | Py_BEGIN_ALLOW_THREADS |
| 155 | Python_Lock_Vim(); |
| 156 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 157 | Python_Release_Vim(); |
| 158 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 159 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Py_DECREF(list); |
| 163 | Py_INCREF(Py_None); |
| 164 | return Py_None; |
| 165 | } |
| 166 | |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 167 | static PyObject * |
| 168 | OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED) |
| 169 | { |
| 170 | /* do nothing */ |
| 171 | Py_INCREF(Py_None); |
| 172 | return Py_None; |
| 173 | } |
| 174 | |
| 175 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 176 | /* Buffer IO, we write one whole line at a time. */ |
| 177 | static garray_T io_ga = {0, 0, 1, 80, NULL}; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 178 | static writefn old_fn = NULL; |
| 179 | |
| 180 | static void |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 181 | PythonIO_Flush(void) |
| 182 | { |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 183 | if (old_fn != NULL && io_ga.ga_len > 0) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 184 | { |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 185 | ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL; |
| 186 | old_fn((char_u *)io_ga.ga_data); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 187 | } |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 188 | io_ga.ga_len = 0; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void |
| 192 | writer(writefn fn, char_u *str, PyInt n) |
| 193 | { |
| 194 | char_u *ptr; |
| 195 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 196 | /* Flush when switching output function. */ |
| 197 | if (fn != old_fn) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 198 | PythonIO_Flush(); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 199 | old_fn = fn; |
| 200 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 201 | /* Write each NL separated line. Text after the last NL is kept for |
| 202 | * writing later. */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 203 | while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) |
| 204 | { |
| 205 | PyInt len = ptr - str; |
| 206 | |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 207 | if (ga_grow(&io_ga, (int)(len + 1)) == FAIL) |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 208 | break; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 209 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 210 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len); |
| 211 | ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL; |
| 212 | fn((char_u *)io_ga.ga_data); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 213 | str = ptr + 1; |
| 214 | n -= len + 1; |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 215 | io_ga.ga_len = 0; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 218 | /* Put the remaining text into io_ga for later printing. */ |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 219 | if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK) |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 220 | { |
| 221 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n); |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 222 | io_ga.ga_len += (int)n; |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 223 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /***************/ |
| 227 | |
| 228 | static PyTypeObject OutputType; |
| 229 | |
| 230 | static OutputObject Output = |
| 231 | { |
| 232 | PyObject_HEAD_INIT(&OutputType) |
| 233 | 0, |
| 234 | 0 |
| 235 | }; |
| 236 | |
| 237 | static OutputObject Error = |
| 238 | { |
| 239 | PyObject_HEAD_INIT(&OutputType) |
| 240 | 0, |
| 241 | 1 |
| 242 | }; |
| 243 | |
| 244 | static int |
| 245 | PythonIO_Init_io(void) |
| 246 | { |
| 247 | PySys_SetObject("stdout", (PyObject *)(void *)&Output); |
| 248 | PySys_SetObject("stderr", (PyObject *)(void *)&Error); |
| 249 | |
| 250 | if (PyErr_Occurred()) |
| 251 | { |
| 252 | EMSG(_("E264: Python: Error initialising I/O objects")); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static PyObject *VimError; |
| 261 | |
| 262 | /* Check to see whether a Vim error has been reported, or a keyboard |
| 263 | * interrupt has been detected. |
| 264 | */ |
| 265 | static int |
| 266 | VimErrorCheck(void) |
| 267 | { |
| 268 | if (got_int) |
| 269 | { |
| 270 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 271 | return 1; |
| 272 | } |
| 273 | else if (did_emsg && !PyErr_Occurred()) |
| 274 | { |
| 275 | PyErr_SetNone(VimError); |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 281 | |
| 282 | /* Vim module - Implementation |
| 283 | */ |
| 284 | static PyObject * |
| 285 | VimCommand(PyObject *self UNUSED, PyObject *args) |
| 286 | { |
| 287 | char *cmd; |
| 288 | PyObject *result; |
| 289 | |
| 290 | if (!PyArg_ParseTuple(args, "s", &cmd)) |
| 291 | return NULL; |
| 292 | |
| 293 | PyErr_Clear(); |
| 294 | |
| 295 | Py_BEGIN_ALLOW_THREADS |
| 296 | Python_Lock_Vim(); |
| 297 | |
| 298 | do_cmdline_cmd((char_u *)cmd); |
| 299 | update_screen(VALID); |
| 300 | |
| 301 | Python_Release_Vim(); |
| 302 | Py_END_ALLOW_THREADS |
| 303 | |
| 304 | if (VimErrorCheck()) |
| 305 | result = NULL; |
| 306 | else |
| 307 | result = Py_None; |
| 308 | |
| 309 | Py_XINCREF(result); |
| 310 | return result; |
| 311 | } |
| 312 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 313 | /* |
| 314 | * Function to translate a typval_T into a PyObject; this will recursively |
| 315 | * translate lists/dictionaries into their Python equivalents. |
| 316 | * |
| 317 | * The depth parameter is to avoid infinite recursion, set it to 1 when |
| 318 | * you call VimToPython. |
| 319 | */ |
| 320 | static PyObject * |
| 321 | VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) |
| 322 | { |
| 323 | PyObject *result; |
| 324 | PyObject *newObj; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 325 | char ptrBuf[sizeof(void *) * 2 + 3]; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 326 | |
| 327 | /* Avoid infinite recursion */ |
| 328 | if (depth > 100) |
| 329 | { |
| 330 | Py_INCREF(Py_None); |
| 331 | result = Py_None; |
| 332 | return result; |
| 333 | } |
| 334 | |
| 335 | /* Check if we run into a recursive loop. The item must be in lookupDict |
| 336 | * then and we can use it again. */ |
| 337 | if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
| 338 | || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
| 339 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 340 | sprintf(ptrBuf, "%p", |
| 341 | our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list |
| 342 | : (void *)our_tv->vval.v_dict); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 343 | result = PyDict_GetItemString(lookupDict, ptrBuf); |
| 344 | if (result != NULL) |
| 345 | { |
| 346 | Py_INCREF(result); |
| 347 | return result; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (our_tv->v_type == VAR_STRING) |
| 352 | { |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 353 | result = Py_BuildValue("s", our_tv->vval.v_string == NULL |
| 354 | ? "" : (char *)our_tv->vval.v_string); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 355 | } |
| 356 | else if (our_tv->v_type == VAR_NUMBER) |
| 357 | { |
| 358 | char buf[NUMBUFLEN]; |
| 359 | |
| 360 | /* For backwards compatibility numbers are stored as strings. */ |
| 361 | sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
| 362 | result = Py_BuildValue("s", buf); |
| 363 | } |
| 364 | # ifdef FEAT_FLOAT |
| 365 | else if (our_tv->v_type == VAR_FLOAT) |
| 366 | { |
| 367 | char buf[NUMBUFLEN]; |
| 368 | |
| 369 | sprintf(buf, "%f", our_tv->vval.v_float); |
| 370 | result = Py_BuildValue("s", buf); |
| 371 | } |
| 372 | # endif |
| 373 | else if (our_tv->v_type == VAR_LIST) |
| 374 | { |
| 375 | list_T *list = our_tv->vval.v_list; |
| 376 | listitem_T *curr; |
| 377 | |
| 378 | result = PyList_New(0); |
| 379 | |
| 380 | if (list != NULL) |
| 381 | { |
| 382 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
| 383 | |
| 384 | for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
| 385 | { |
| 386 | newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict); |
| 387 | PyList_Append(result, newObj); |
| 388 | Py_DECREF(newObj); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | else if (our_tv->v_type == VAR_DICT) |
| 393 | { |
| 394 | result = PyDict_New(); |
| 395 | |
| 396 | if (our_tv->vval.v_dict != NULL) |
| 397 | { |
| 398 | hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
| 399 | long_u todo = ht->ht_used; |
| 400 | hashitem_T *hi; |
| 401 | dictitem_T *di; |
| 402 | |
| 403 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
| 404 | |
| 405 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 406 | { |
| 407 | if (!HASHITEM_EMPTY(hi)) |
| 408 | { |
| 409 | --todo; |
| 410 | |
| 411 | di = dict_lookup(hi); |
| 412 | newObj = VimToPython(&di->di_tv, depth + 1, lookupDict); |
| 413 | PyDict_SetItemString(result, (char *)hi->hi_key, newObj); |
| 414 | Py_DECREF(newObj); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | Py_INCREF(Py_None); |
| 422 | result = Py_None; |
| 423 | } |
| 424 | |
| 425 | return result; |
| 426 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 427 | |
| 428 | static PyObject * |
Bram Moolenaar | 0909215 | 2010-08-08 16:38:42 +0200 | [diff] [blame] | 429 | VimEval(PyObject *self UNUSED, PyObject *args UNUSED) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 430 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 431 | char *expr; |
| 432 | typval_T *our_tv; |
| 433 | PyObject *result; |
| 434 | PyObject *lookup_dict; |
| 435 | |
| 436 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 437 | return NULL; |
| 438 | |
| 439 | Py_BEGIN_ALLOW_THREADS |
| 440 | Python_Lock_Vim(); |
| 441 | our_tv = eval_expr((char_u *)expr, NULL); |
| 442 | |
| 443 | Python_Release_Vim(); |
| 444 | Py_END_ALLOW_THREADS |
| 445 | |
| 446 | if (our_tv == NULL) |
| 447 | { |
| 448 | PyErr_SetVim(_("invalid expression")); |
| 449 | return NULL; |
| 450 | } |
| 451 | |
| 452 | /* Convert the Vim type into a Python type. Create a dictionary that's |
| 453 | * used to check for recursive loops. */ |
| 454 | lookup_dict = PyDict_New(); |
| 455 | result = VimToPython(our_tv, 1, lookup_dict); |
| 456 | Py_DECREF(lookup_dict); |
| 457 | |
| 458 | |
| 459 | Py_BEGIN_ALLOW_THREADS |
| 460 | Python_Lock_Vim(); |
| 461 | free_tv(our_tv); |
| 462 | Python_Release_Vim(); |
| 463 | Py_END_ALLOW_THREADS |
| 464 | |
| 465 | return result; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 468 | static PyObject *ConvertToPyObject(typval_T *); |
| 469 | |
| 470 | static PyObject * |
| 471 | VimEvalPy(PyObject *self UNUSED, PyObject *args UNUSED) |
| 472 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 473 | char *expr; |
| 474 | typval_T *our_tv; |
| 475 | PyObject *result; |
| 476 | |
| 477 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 478 | return NULL; |
| 479 | |
| 480 | Py_BEGIN_ALLOW_THREADS |
| 481 | Python_Lock_Vim(); |
| 482 | our_tv = eval_expr((char_u *)expr, NULL); |
| 483 | |
| 484 | Python_Release_Vim(); |
| 485 | Py_END_ALLOW_THREADS |
| 486 | |
| 487 | if (our_tv == NULL) |
| 488 | { |
| 489 | PyErr_SetVim(_("invalid expression")); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | result = ConvertToPyObject(our_tv); |
| 494 | Py_BEGIN_ALLOW_THREADS |
| 495 | Python_Lock_Vim(); |
| 496 | free_tv(our_tv); |
| 497 | Python_Release_Vim(); |
| 498 | Py_END_ALLOW_THREADS |
| 499 | |
| 500 | return result; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static PyObject * |
| 504 | VimStrwidth(PyObject *self UNUSED, PyObject *args) |
| 505 | { |
| 506 | char *expr; |
| 507 | |
| 508 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 509 | return NULL; |
| 510 | |
Bram Moolenaar | a54bf40 | 2012-12-05 16:30:07 +0100 | [diff] [blame] | 511 | return PyLong_FromLong( |
| 512 | #ifdef FEAT_MBYTE |
| 513 | mb_string2cells((char_u *)expr, (int)STRLEN(expr)) |
| 514 | #else |
| 515 | STRLEN(expr) |
| 516 | #endif |
| 517 | ); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 520 | /* |
| 521 | * Vim module - Definitions |
| 522 | */ |
| 523 | |
| 524 | static struct PyMethodDef VimMethods[] = { |
| 525 | /* name, function, calling, documentation */ |
| 526 | {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, |
| 527 | {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 528 | {"bindeval", VimEvalPy, 1, "Like eval(), but returns objects attached to vim ones"}, |
| 529 | {"strwidth", VimStrwidth, 1, "Screen string width, counts <Tab> as having width 1"}, |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 530 | { NULL, NULL, 0, NULL } |
| 531 | }; |
| 532 | |
| 533 | typedef struct |
| 534 | { |
| 535 | PyObject_HEAD |
| 536 | buf_T *buf; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 537 | } BufferObject; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 538 | |
| 539 | #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
| 540 | |
| 541 | /* |
| 542 | * Buffer list object - Implementation |
| 543 | */ |
| 544 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 545 | typedef struct |
| 546 | { |
| 547 | PyObject_HEAD |
| 548 | } BufListObject; |
| 549 | |
| 550 | static PyTypeObject BufListType; |
| 551 | static PySequenceMethods WinListAsSeq; |
| 552 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 553 | static PyInt |
| 554 | BufListLength(PyObject *self UNUSED) |
| 555 | { |
| 556 | buf_T *b = firstbuf; |
| 557 | PyInt n = 0; |
| 558 | |
| 559 | while (b) |
| 560 | { |
| 561 | ++n; |
| 562 | b = b->b_next; |
| 563 | } |
| 564 | |
| 565 | return n; |
| 566 | } |
| 567 | |
| 568 | static PyObject * |
| 569 | BufListItem(PyObject *self UNUSED, PyInt n) |
| 570 | { |
| 571 | buf_T *b; |
| 572 | |
| 573 | for (b = firstbuf; b; b = b->b_next, --n) |
| 574 | { |
| 575 | if (n == 0) |
| 576 | return BufferNew(b); |
| 577 | } |
| 578 | |
| 579 | PyErr_SetString(PyExc_IndexError, _("no such buffer")); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | typedef struct |
| 584 | { |
| 585 | PyObject_HEAD |
| 586 | win_T *win; |
| 587 | } WindowObject; |
| 588 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 589 | static struct PyMethodDef WindowMethods[] = { |
| 590 | /* name, function, calling, documentation */ |
| 591 | { NULL, NULL, 0, NULL } |
| 592 | }; |
| 593 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 594 | static int ConvertFromPyObject(PyObject *, typval_T *); |
| 595 | static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); |
| 596 | |
| 597 | typedef struct pylinkedlist_S { |
| 598 | struct pylinkedlist_S *pll_next; |
| 599 | struct pylinkedlist_S *pll_prev; |
| 600 | PyObject *pll_obj; |
| 601 | } pylinkedlist_T; |
| 602 | |
| 603 | static pylinkedlist_T *lastdict = NULL; |
| 604 | static pylinkedlist_T *lastlist = NULL; |
| 605 | |
| 606 | static void |
| 607 | pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last) |
| 608 | { |
| 609 | if (ref->pll_prev == NULL) |
| 610 | { |
| 611 | if (ref->pll_next == NULL) |
| 612 | { |
| 613 | *last = NULL; |
| 614 | return; |
| 615 | } |
| 616 | } |
| 617 | else |
| 618 | ref->pll_prev->pll_next = ref->pll_next; |
| 619 | |
| 620 | if (ref->pll_next == NULL) |
| 621 | *last = ref->pll_prev; |
| 622 | else |
| 623 | ref->pll_next->pll_prev = ref->pll_prev; |
| 624 | } |
| 625 | |
| 626 | static void |
| 627 | pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last) |
| 628 | { |
| 629 | if (*last == NULL) |
| 630 | ref->pll_prev = NULL; |
| 631 | else |
| 632 | { |
| 633 | (*last)->pll_next = ref; |
| 634 | ref->pll_prev = *last; |
| 635 | } |
| 636 | ref->pll_next = NULL; |
| 637 | ref->pll_obj = self; |
| 638 | *last = ref; |
| 639 | } |
| 640 | |
| 641 | static PyTypeObject DictionaryType; |
| 642 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 643 | #define DICTKEY_GET_NOTEMPTY(err) \ |
| 644 | DICTKEY_GET(err) \ |
| 645 | if (*key == NUL) \ |
| 646 | { \ |
| 647 | PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \ |
| 648 | return err; \ |
| 649 | } |
| 650 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 651 | typedef struct |
| 652 | { |
| 653 | PyObject_HEAD |
| 654 | dict_T *dict; |
| 655 | pylinkedlist_T ref; |
| 656 | } DictionaryObject; |
| 657 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 658 | static PyInt DictionaryAssItem(PyObject *, PyObject *, PyObject *); |
| 659 | static PyInt DictionaryLength(PyObject *); |
| 660 | static PyObject *DictionaryItem(PyObject *, PyObject *); |
| 661 | |
| 662 | static PyMappingMethods DictionaryAsMapping = { |
| 663 | (lenfunc) DictionaryLength, |
| 664 | (binaryfunc) DictionaryItem, |
| 665 | (objobjargproc) DictionaryAssItem, |
| 666 | }; |
| 667 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 668 | static PyObject * |
| 669 | DictionaryNew(dict_T *dict) |
| 670 | { |
| 671 | DictionaryObject *self; |
| 672 | |
| 673 | self = PyObject_NEW(DictionaryObject, &DictionaryType); |
| 674 | if (self == NULL) |
| 675 | return NULL; |
| 676 | self->dict = dict; |
| 677 | ++dict->dv_refcount; |
| 678 | |
| 679 | pyll_add((PyObject *)(self), &self->ref, &lastdict); |
| 680 | |
| 681 | return (PyObject *)(self); |
| 682 | } |
| 683 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 684 | static void |
| 685 | DictionaryDestructor(PyObject *self) |
| 686 | { |
| 687 | DictionaryObject *this = ((DictionaryObject *) (self)); |
| 688 | |
| 689 | pyll_remove(&this->ref, &lastdict); |
| 690 | dict_unref(this->dict); |
| 691 | |
| 692 | DESTRUCTOR_FINISH(self); |
| 693 | } |
| 694 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 695 | static int |
| 696 | pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 697 | { |
| 698 | dict_T *d; |
| 699 | char_u *key; |
| 700 | dictitem_T *di; |
| 701 | PyObject *keyObject; |
| 702 | PyObject *valObject; |
| 703 | Py_ssize_t iter = 0; |
| 704 | |
| 705 | d = dict_alloc(); |
| 706 | if (d == NULL) |
| 707 | { |
| 708 | PyErr_NoMemory(); |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | tv->v_type = VAR_DICT; |
| 713 | tv->vval.v_dict = d; |
| 714 | |
| 715 | while (PyDict_Next(obj, &iter, &keyObject, &valObject)) |
| 716 | { |
| 717 | DICTKEY_DECL |
| 718 | |
| 719 | if (keyObject == NULL) |
| 720 | return -1; |
| 721 | if (valObject == NULL) |
| 722 | return -1; |
| 723 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 724 | DICTKEY_GET_NOTEMPTY(-1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 725 | |
| 726 | di = dictitem_alloc(key); |
| 727 | |
| 728 | DICTKEY_UNREF |
| 729 | |
| 730 | if (di == NULL) |
| 731 | { |
| 732 | PyErr_NoMemory(); |
| 733 | return -1; |
| 734 | } |
| 735 | di->di_tv.v_lock = 0; |
| 736 | |
| 737 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) |
| 738 | { |
| 739 | vim_free(di); |
| 740 | return -1; |
| 741 | } |
| 742 | if (dict_add(d, di) == FAIL) |
| 743 | { |
| 744 | vim_free(di); |
| 745 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 746 | return -1; |
| 747 | } |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int |
| 753 | pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 754 | { |
| 755 | dict_T *d; |
| 756 | char_u *key; |
| 757 | dictitem_T *di; |
| 758 | PyObject *list; |
| 759 | PyObject *litem; |
| 760 | PyObject *keyObject; |
| 761 | PyObject *valObject; |
| 762 | Py_ssize_t lsize; |
| 763 | |
| 764 | d = dict_alloc(); |
| 765 | if (d == NULL) |
| 766 | { |
| 767 | PyErr_NoMemory(); |
| 768 | return -1; |
| 769 | } |
| 770 | |
| 771 | tv->v_type = VAR_DICT; |
| 772 | tv->vval.v_dict = d; |
| 773 | |
| 774 | list = PyMapping_Items(obj); |
Bram Moolenaar | 7a26dd8 | 2013-04-24 13:10:41 +0200 | [diff] [blame] | 775 | if (list == NULL) |
| 776 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 777 | lsize = PyList_Size(list); |
| 778 | while (lsize--) |
| 779 | { |
| 780 | DICTKEY_DECL |
| 781 | |
| 782 | litem = PyList_GetItem(list, lsize); |
| 783 | if (litem == NULL) |
| 784 | { |
| 785 | Py_DECREF(list); |
| 786 | return -1; |
| 787 | } |
| 788 | |
| 789 | keyObject = PyTuple_GetItem(litem, 0); |
| 790 | if (keyObject == NULL) |
| 791 | { |
| 792 | Py_DECREF(list); |
| 793 | Py_DECREF(litem); |
| 794 | return -1; |
| 795 | } |
| 796 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 797 | DICTKEY_GET_NOTEMPTY(-1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 798 | |
| 799 | valObject = PyTuple_GetItem(litem, 1); |
| 800 | if (valObject == NULL) |
| 801 | { |
| 802 | Py_DECREF(list); |
| 803 | Py_DECREF(litem); |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | di = dictitem_alloc(key); |
| 808 | |
| 809 | DICTKEY_UNREF |
| 810 | |
| 811 | if (di == NULL) |
| 812 | { |
| 813 | Py_DECREF(list); |
| 814 | Py_DECREF(litem); |
| 815 | PyErr_NoMemory(); |
| 816 | return -1; |
| 817 | } |
| 818 | di->di_tv.v_lock = 0; |
| 819 | |
| 820 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) |
| 821 | { |
| 822 | vim_free(di); |
| 823 | Py_DECREF(list); |
| 824 | Py_DECREF(litem); |
| 825 | return -1; |
| 826 | } |
| 827 | if (dict_add(d, di) == FAIL) |
| 828 | { |
| 829 | vim_free(di); |
| 830 | Py_DECREF(list); |
| 831 | Py_DECREF(litem); |
| 832 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 833 | return -1; |
| 834 | } |
| 835 | Py_DECREF(litem); |
| 836 | } |
| 837 | Py_DECREF(list); |
| 838 | return 0; |
| 839 | } |
| 840 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 841 | static int |
| 842 | DictionarySetattr(PyObject *self, char *name, PyObject *val) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 843 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 844 | DictionaryObject *this = (DictionaryObject *)(self); |
| 845 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 846 | if (val == NULL) |
| 847 | { |
| 848 | PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | if (strcmp(name, "locked") == 0) |
| 853 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 854 | if (this->dict->dv_lock == VAR_FIXED) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 855 | { |
| 856 | PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary")); |
| 857 | return -1; |
| 858 | } |
| 859 | else |
| 860 | { |
| 861 | if (!PyBool_Check(val)) |
| 862 | { |
| 863 | PyErr_SetString(PyExc_TypeError, _("Only boolean objects are allowed")); |
| 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | if (val == Py_True) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 868 | this->dict->dv_lock = VAR_LOCKED; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 869 | else |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 870 | this->dict->dv_lock = 0; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 871 | } |
| 872 | return 0; |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute")); |
| 877 | return -1; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | static PyInt |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 882 | DictionaryLength(PyObject *self) |
| 883 | { |
| 884 | return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used))); |
| 885 | } |
| 886 | |
| 887 | static PyObject * |
| 888 | DictionaryItem(PyObject *self, PyObject *keyObject) |
| 889 | { |
| 890 | char_u *key; |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 891 | dictitem_T *di; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 892 | DICTKEY_DECL |
| 893 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 894 | DICTKEY_GET_NOTEMPTY(NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 895 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 896 | di = dict_find(((DictionaryObject *) (self))->dict, key, -1); |
| 897 | |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 898 | DICTKEY_UNREF |
| 899 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 900 | if (di == NULL) |
| 901 | { |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 902 | PyErr_SetString(PyExc_KeyError, _("no such key in dictionary")); |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 903 | return NULL; |
| 904 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 905 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 906 | return ConvertToPyObject(&di->di_tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static PyInt |
| 910 | DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject) |
| 911 | { |
| 912 | char_u *key; |
| 913 | typval_T tv; |
| 914 | dict_T *d = ((DictionaryObject *)(self))->dict; |
| 915 | dictitem_T *di; |
| 916 | DICTKEY_DECL |
| 917 | |
| 918 | if (d->dv_lock) |
| 919 | { |
| 920 | PyErr_SetVim(_("dict is locked")); |
| 921 | return -1; |
| 922 | } |
| 923 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 924 | DICTKEY_GET_NOTEMPTY(-1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 925 | |
| 926 | di = dict_find(d, key, -1); |
| 927 | |
| 928 | if (valObject == NULL) |
| 929 | { |
Bram Moolenaar | f27839c | 2012-06-29 16:19:50 +0200 | [diff] [blame] | 930 | hashitem_T *hi; |
| 931 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 932 | if (di == NULL) |
| 933 | { |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 934 | DICTKEY_UNREF |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 935 | PyErr_SetString(PyExc_IndexError, _("no such key in dictionary")); |
| 936 | return -1; |
| 937 | } |
Bram Moolenaar | f27839c | 2012-06-29 16:19:50 +0200 | [diff] [blame] | 938 | hi = hash_find(&d->dv_hashtab, di->di_key); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 939 | hash_remove(&d->dv_hashtab, hi); |
| 940 | dictitem_free(di); |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | if (ConvertFromPyObject(valObject, &tv) == -1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 945 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 946 | |
| 947 | if (di == NULL) |
| 948 | { |
| 949 | di = dictitem_alloc(key); |
| 950 | if (di == NULL) |
| 951 | { |
| 952 | PyErr_NoMemory(); |
| 953 | return -1; |
| 954 | } |
| 955 | di->di_tv.v_lock = 0; |
| 956 | |
| 957 | if (dict_add(d, di) == FAIL) |
| 958 | { |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 959 | DICTKEY_UNREF |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 960 | vim_free(di); |
| 961 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 962 | return -1; |
| 963 | } |
| 964 | } |
| 965 | else |
| 966 | clear_tv(&di->di_tv); |
| 967 | |
| 968 | DICTKEY_UNREF |
| 969 | |
| 970 | copy_tv(&tv, &di->di_tv); |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static PyObject * |
Bram Moolenaar | b2c5a5a | 2013-02-14 22:11:39 +0100 | [diff] [blame] | 975 | DictionaryListKeys(PyObject *self UNUSED) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 976 | { |
| 977 | dict_T *dict = ((DictionaryObject *)(self))->dict; |
| 978 | long_u todo = dict->dv_hashtab.ht_used; |
| 979 | Py_ssize_t i = 0; |
| 980 | PyObject *r; |
| 981 | hashitem_T *hi; |
| 982 | |
| 983 | r = PyList_New(todo); |
| 984 | for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) |
| 985 | { |
| 986 | if (!HASHITEM_EMPTY(hi)) |
| 987 | { |
| 988 | PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key))); |
| 989 | --todo; |
| 990 | ++i; |
| 991 | } |
| 992 | } |
| 993 | return r; |
| 994 | } |
| 995 | |
| 996 | static struct PyMethodDef DictionaryMethods[] = { |
| 997 | {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""}, |
| 998 | { NULL, NULL, 0, NULL } |
| 999 | }; |
| 1000 | |
| 1001 | static PyTypeObject ListType; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1002 | static PySequenceMethods ListAsSeq; |
| 1003 | static PyMappingMethods ListAsMapping; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1004 | |
| 1005 | typedef struct |
| 1006 | { |
| 1007 | PyObject_HEAD |
| 1008 | list_T *list; |
| 1009 | pylinkedlist_T ref; |
| 1010 | } ListObject; |
| 1011 | |
| 1012 | static PyObject * |
| 1013 | ListNew(list_T *list) |
| 1014 | { |
| 1015 | ListObject *self; |
| 1016 | |
| 1017 | self = PyObject_NEW(ListObject, &ListType); |
| 1018 | if (self == NULL) |
| 1019 | return NULL; |
| 1020 | self->list = list; |
| 1021 | ++list->lv_refcount; |
| 1022 | |
| 1023 | pyll_add((PyObject *)(self), &self->ref, &lastlist); |
| 1024 | |
| 1025 | return (PyObject *)(self); |
| 1026 | } |
| 1027 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1028 | static void |
| 1029 | ListDestructor(PyObject *self) |
| 1030 | { |
| 1031 | ListObject *this = (ListObject *)(self); |
| 1032 | |
| 1033 | pyll_remove(&this->ref, &lastlist); |
| 1034 | list_unref(this->list); |
| 1035 | |
| 1036 | DESTRUCTOR_FINISH(self); |
| 1037 | } |
| 1038 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1039 | static int |
| 1040 | list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict) |
| 1041 | { |
| 1042 | Py_ssize_t i; |
| 1043 | Py_ssize_t lsize = PySequence_Size(obj); |
| 1044 | PyObject *litem; |
| 1045 | listitem_T *li; |
| 1046 | |
| 1047 | for(i=0; i<lsize; i++) |
| 1048 | { |
| 1049 | li = listitem_alloc(); |
| 1050 | if (li == NULL) |
| 1051 | { |
| 1052 | PyErr_NoMemory(); |
| 1053 | return -1; |
| 1054 | } |
| 1055 | li->li_tv.v_lock = 0; |
| 1056 | |
| 1057 | litem = PySequence_GetItem(obj, i); |
| 1058 | if (litem == NULL) |
| 1059 | return -1; |
| 1060 | if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1) |
| 1061 | return -1; |
| 1062 | |
| 1063 | list_append(l, li); |
| 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | static int |
| 1069 | pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 1070 | { |
| 1071 | list_T *l; |
| 1072 | |
| 1073 | l = list_alloc(); |
| 1074 | if (l == NULL) |
| 1075 | { |
| 1076 | PyErr_NoMemory(); |
| 1077 | return -1; |
| 1078 | } |
| 1079 | |
| 1080 | tv->v_type = VAR_LIST; |
| 1081 | tv->vval.v_list = l; |
| 1082 | |
| 1083 | if (list_py_concat(l, obj, lookupDict) == -1) |
| 1084 | return -1; |
| 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int |
| 1090 | pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 1091 | { |
| 1092 | PyObject *iterator = PyObject_GetIter(obj); |
| 1093 | PyObject *item; |
| 1094 | list_T *l; |
| 1095 | listitem_T *li; |
| 1096 | |
| 1097 | l = list_alloc(); |
| 1098 | |
| 1099 | if (l == NULL) |
| 1100 | { |
| 1101 | PyErr_NoMemory(); |
| 1102 | return -1; |
| 1103 | } |
| 1104 | |
| 1105 | tv->vval.v_list = l; |
| 1106 | tv->v_type = VAR_LIST; |
| 1107 | |
| 1108 | |
| 1109 | if (iterator == NULL) |
| 1110 | return -1; |
| 1111 | |
| 1112 | while ((item = PyIter_Next(obj))) |
| 1113 | { |
| 1114 | li = listitem_alloc(); |
| 1115 | if (li == NULL) |
| 1116 | { |
| 1117 | PyErr_NoMemory(); |
| 1118 | return -1; |
| 1119 | } |
| 1120 | li->li_tv.v_lock = 0; |
| 1121 | |
| 1122 | if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1) |
| 1123 | return -1; |
| 1124 | |
| 1125 | list_append(l, li); |
| 1126 | |
| 1127 | Py_DECREF(item); |
| 1128 | } |
| 1129 | |
| 1130 | Py_DECREF(iterator); |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | static PyInt |
| 1135 | ListLength(PyObject *self) |
| 1136 | { |
| 1137 | return ((PyInt) (((ListObject *) (self))->list->lv_len)); |
| 1138 | } |
| 1139 | |
| 1140 | static PyObject * |
| 1141 | ListItem(PyObject *self, Py_ssize_t index) |
| 1142 | { |
| 1143 | listitem_T *li; |
| 1144 | |
| 1145 | if (index>=ListLength(self)) |
| 1146 | { |
| 1147 | PyErr_SetString(PyExc_IndexError, "list index out of range"); |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | li = list_find(((ListObject *) (self))->list, (long) index); |
| 1151 | if (li == NULL) |
| 1152 | { |
| 1153 | PyErr_SetVim(_("internal error: failed to get vim list item")); |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | return ConvertToPyObject(&li->li_tv); |
| 1157 | } |
| 1158 | |
| 1159 | #define PROC_RANGE \ |
| 1160 | if (last < 0) {\ |
| 1161 | if (last < -size) \ |
| 1162 | last = 0; \ |
| 1163 | else \ |
| 1164 | last += size; \ |
| 1165 | } \ |
| 1166 | if (first < 0) \ |
| 1167 | first = 0; \ |
| 1168 | if (first > size) \ |
| 1169 | first = size; \ |
| 1170 | if (last > size) \ |
| 1171 | last = size; |
| 1172 | |
| 1173 | static PyObject * |
| 1174 | ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last) |
| 1175 | { |
| 1176 | PyInt i; |
| 1177 | PyInt size = ListLength(self); |
| 1178 | PyInt n; |
| 1179 | PyObject *list; |
| 1180 | int reversed = 0; |
| 1181 | |
| 1182 | PROC_RANGE |
| 1183 | if (first >= last) |
| 1184 | first = last; |
| 1185 | |
| 1186 | n = last-first; |
| 1187 | list = PyList_New(n); |
| 1188 | if (list == NULL) |
| 1189 | return NULL; |
| 1190 | |
| 1191 | for (i = 0; i < n; ++i) |
| 1192 | { |
Bram Moolenaar | 24b11fb | 2013-04-05 19:32:36 +0200 | [diff] [blame] | 1193 | PyObject *item = ListItem(self, first + i); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1194 | if (item == NULL) |
| 1195 | { |
| 1196 | Py_DECREF(list); |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | |
| 1200 | if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item))) |
| 1201 | { |
| 1202 | Py_DECREF(item); |
| 1203 | Py_DECREF(list); |
| 1204 | return NULL; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | return list; |
| 1209 | } |
| 1210 | |
| 1211 | static int |
| 1212 | ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj) |
| 1213 | { |
| 1214 | typval_T tv; |
| 1215 | list_T *l = ((ListObject *) (self))->list; |
| 1216 | listitem_T *li; |
| 1217 | Py_ssize_t length = ListLength(self); |
| 1218 | |
| 1219 | if (l->lv_lock) |
| 1220 | { |
| 1221 | PyErr_SetVim(_("list is locked")); |
| 1222 | return -1; |
| 1223 | } |
| 1224 | if (index>length || (index==length && obj==NULL)) |
| 1225 | { |
| 1226 | PyErr_SetString(PyExc_IndexError, "list index out of range"); |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
| 1230 | if (obj == NULL) |
| 1231 | { |
| 1232 | li = list_find(l, (long) index); |
| 1233 | list_remove(l, li, li); |
| 1234 | clear_tv(&li->li_tv); |
| 1235 | vim_free(li); |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | if (ConvertFromPyObject(obj, &tv) == -1) |
| 1240 | return -1; |
| 1241 | |
| 1242 | if (index == length) |
| 1243 | { |
| 1244 | if (list_append_tv(l, &tv) == FAIL) |
| 1245 | { |
| 1246 | PyErr_SetVim(_("Failed to add item to list")); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | } |
| 1250 | else |
| 1251 | { |
| 1252 | li = list_find(l, (long) index); |
| 1253 | clear_tv(&li->li_tv); |
| 1254 | copy_tv(&tv, &li->li_tv); |
| 1255 | } |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | static int |
| 1260 | ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj) |
| 1261 | { |
| 1262 | PyInt size = ListLength(self); |
| 1263 | Py_ssize_t i; |
| 1264 | Py_ssize_t lsize; |
| 1265 | PyObject *litem; |
| 1266 | listitem_T *li; |
| 1267 | listitem_T *next; |
| 1268 | typval_T v; |
| 1269 | list_T *l = ((ListObject *) (self))->list; |
| 1270 | |
| 1271 | if (l->lv_lock) |
| 1272 | { |
| 1273 | PyErr_SetVim(_("list is locked")); |
| 1274 | return -1; |
| 1275 | } |
| 1276 | |
| 1277 | PROC_RANGE |
| 1278 | |
| 1279 | if (first == size) |
| 1280 | li = NULL; |
| 1281 | else |
| 1282 | { |
| 1283 | li = list_find(l, (long) first); |
| 1284 | if (li == NULL) |
| 1285 | { |
| 1286 | PyErr_SetVim(_("internal error: no vim list item")); |
| 1287 | return -1; |
| 1288 | } |
| 1289 | if (last > first) |
| 1290 | { |
| 1291 | i = last - first; |
| 1292 | while (i-- && li != NULL) |
| 1293 | { |
| 1294 | next = li->li_next; |
| 1295 | listitem_remove(l, li); |
| 1296 | li = next; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | if (obj == NULL) |
| 1302 | return 0; |
| 1303 | |
| 1304 | if (!PyList_Check(obj)) |
| 1305 | { |
| 1306 | PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice")); |
| 1307 | return -1; |
| 1308 | } |
| 1309 | |
| 1310 | lsize = PyList_Size(obj); |
| 1311 | |
| 1312 | for(i=0; i<lsize; i++) |
| 1313 | { |
| 1314 | litem = PyList_GetItem(obj, i); |
| 1315 | if (litem == NULL) |
| 1316 | return -1; |
| 1317 | if (ConvertFromPyObject(litem, &v) == -1) |
| 1318 | return -1; |
| 1319 | if (list_insert_tv(l, &v, li) == FAIL) |
| 1320 | { |
| 1321 | PyErr_SetVim(_("internal error: failed to add item to list")); |
| 1322 | return -1; |
| 1323 | } |
| 1324 | } |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static PyObject * |
| 1329 | ListConcatInPlace(PyObject *self, PyObject *obj) |
| 1330 | { |
| 1331 | list_T *l = ((ListObject *) (self))->list; |
| 1332 | PyObject *lookup_dict; |
| 1333 | |
| 1334 | if (l->lv_lock) |
| 1335 | { |
| 1336 | PyErr_SetVim(_("list is locked")); |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | |
| 1340 | if (!PySequence_Check(obj)) |
| 1341 | { |
| 1342 | PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists")); |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | |
| 1346 | lookup_dict = PyDict_New(); |
| 1347 | if (list_py_concat(l, obj, lookup_dict) == -1) |
| 1348 | { |
| 1349 | Py_DECREF(lookup_dict); |
| 1350 | return NULL; |
| 1351 | } |
| 1352 | Py_DECREF(lookup_dict); |
| 1353 | |
| 1354 | Py_INCREF(self); |
| 1355 | return self; |
| 1356 | } |
| 1357 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1358 | static int |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1359 | ListSetattr(PyObject *self, char *name, PyObject *val) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1360 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1361 | ListObject *this = (ListObject *)(self); |
| 1362 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1363 | if (val == NULL) |
| 1364 | { |
| 1365 | PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); |
| 1366 | return -1; |
| 1367 | } |
| 1368 | |
| 1369 | if (strcmp(name, "locked") == 0) |
| 1370 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1371 | if (this->list->lv_lock == VAR_FIXED) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1372 | { |
| 1373 | PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed list")); |
| 1374 | return -1; |
| 1375 | } |
| 1376 | else |
| 1377 | { |
| 1378 | if (!PyBool_Check(val)) |
| 1379 | { |
| 1380 | PyErr_SetString(PyExc_TypeError, _("Only boolean objects are allowed")); |
| 1381 | return -1; |
| 1382 | } |
| 1383 | |
| 1384 | if (val == Py_True) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1385 | this->list->lv_lock = VAR_LOCKED; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1386 | else |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1387 | this->list->lv_lock = 0; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1388 | } |
| 1389 | return 0; |
| 1390 | } |
| 1391 | else |
| 1392 | { |
| 1393 | PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute")); |
| 1394 | return -1; |
| 1395 | } |
| 1396 | } |
| 1397 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1398 | static struct PyMethodDef ListMethods[] = { |
| 1399 | {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""}, |
| 1400 | { NULL, NULL, 0, NULL } |
| 1401 | }; |
| 1402 | |
| 1403 | typedef struct |
| 1404 | { |
| 1405 | PyObject_HEAD |
| 1406 | char_u *name; |
| 1407 | } FunctionObject; |
| 1408 | |
| 1409 | static PyTypeObject FunctionType; |
| 1410 | |
| 1411 | static PyObject * |
| 1412 | FunctionNew(char_u *name) |
| 1413 | { |
| 1414 | FunctionObject *self; |
| 1415 | |
| 1416 | self = PyObject_NEW(FunctionObject, &FunctionType); |
| 1417 | if (self == NULL) |
| 1418 | return NULL; |
| 1419 | self->name = PyMem_New(char_u, STRLEN(name) + 1); |
| 1420 | if (self->name == NULL) |
| 1421 | { |
| 1422 | PyErr_NoMemory(); |
| 1423 | return NULL; |
| 1424 | } |
| 1425 | STRCPY(self->name, name); |
| 1426 | func_ref(name); |
| 1427 | return (PyObject *)(self); |
| 1428 | } |
| 1429 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1430 | static void |
| 1431 | FunctionDestructor(PyObject *self) |
| 1432 | { |
| 1433 | FunctionObject *this = (FunctionObject *) (self); |
| 1434 | |
| 1435 | func_unref(this->name); |
| 1436 | PyMem_Del(this->name); |
| 1437 | |
| 1438 | DESTRUCTOR_FINISH(self); |
| 1439 | } |
| 1440 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1441 | static PyObject * |
| 1442 | FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs) |
| 1443 | { |
| 1444 | FunctionObject *this = (FunctionObject *)(self); |
| 1445 | char_u *name = this->name; |
| 1446 | typval_T args; |
| 1447 | typval_T selfdicttv; |
| 1448 | typval_T rettv; |
| 1449 | dict_T *selfdict = NULL; |
| 1450 | PyObject *selfdictObject; |
| 1451 | PyObject *result; |
| 1452 | int error; |
| 1453 | |
| 1454 | if (ConvertFromPyObject(argsObject, &args) == -1) |
| 1455 | return NULL; |
| 1456 | |
| 1457 | if (kwargs != NULL) |
| 1458 | { |
| 1459 | selfdictObject = PyDict_GetItemString(kwargs, "self"); |
| 1460 | if (selfdictObject != NULL) |
| 1461 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1462 | if (!PyMapping_Check(selfdictObject)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1463 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1464 | PyErr_SetString(PyExc_TypeError, |
| 1465 | _("'self' argument must be a dictionary")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1466 | clear_tv(&args); |
| 1467 | return NULL; |
| 1468 | } |
| 1469 | if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1) |
| 1470 | return NULL; |
| 1471 | selfdict = selfdicttv.vval.v_dict; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | error = func_call(name, &args, selfdict, &rettv); |
| 1476 | if (error != OK) |
| 1477 | { |
| 1478 | result = NULL; |
| 1479 | PyErr_SetVim(_("failed to run function")); |
| 1480 | } |
| 1481 | else |
| 1482 | result = ConvertToPyObject(&rettv); |
| 1483 | |
| 1484 | /* FIXME Check what should really be cleared. */ |
| 1485 | clear_tv(&args); |
| 1486 | clear_tv(&rettv); |
| 1487 | /* |
| 1488 | * if (selfdict!=NULL) |
| 1489 | * clear_tv(selfdicttv); |
| 1490 | */ |
| 1491 | |
| 1492 | return result; |
| 1493 | } |
| 1494 | |
| 1495 | static struct PyMethodDef FunctionMethods[] = { |
| 1496 | {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""}, |
| 1497 | { NULL, NULL, 0, NULL } |
| 1498 | }; |
| 1499 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1500 | /* |
| 1501 | * Options object |
| 1502 | */ |
| 1503 | |
| 1504 | static PyTypeObject OptionsType; |
| 1505 | |
| 1506 | typedef int (*checkfun)(void *); |
| 1507 | |
| 1508 | typedef struct |
| 1509 | { |
| 1510 | PyObject_HEAD |
| 1511 | int opt_type; |
| 1512 | void *from; |
| 1513 | checkfun Check; |
| 1514 | PyObject *fromObj; |
| 1515 | } OptionsObject; |
| 1516 | |
| 1517 | static PyObject * |
| 1518 | OptionsItem(OptionsObject *this, PyObject *keyObject) |
| 1519 | { |
| 1520 | char_u *key; |
| 1521 | int flags; |
| 1522 | long numval; |
| 1523 | char_u *stringval; |
Bram Moolenaar | 161fb5e | 2013-05-06 06:26:15 +0200 | [diff] [blame] | 1524 | DICTKEY_DECL |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1525 | |
| 1526 | if (this->Check(this->from)) |
| 1527 | return NULL; |
| 1528 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1529 | DICTKEY_GET_NOTEMPTY(NULL) |
| 1530 | |
| 1531 | flags = get_option_value_strict(key, &numval, &stringval, |
| 1532 | this->opt_type, this->from); |
| 1533 | |
| 1534 | DICTKEY_UNREF |
| 1535 | |
| 1536 | if (flags == 0) |
| 1537 | { |
| 1538 | PyErr_SetString(PyExc_KeyError, "Option does not exist in given scope"); |
| 1539 | return NULL; |
| 1540 | } |
| 1541 | |
| 1542 | if (flags & SOPT_UNSET) |
| 1543 | { |
| 1544 | Py_INCREF(Py_None); |
| 1545 | return Py_None; |
| 1546 | } |
| 1547 | else if (flags & SOPT_BOOL) |
| 1548 | { |
| 1549 | PyObject *r; |
| 1550 | r = numval ? Py_True : Py_False; |
| 1551 | Py_INCREF(r); |
| 1552 | return r; |
| 1553 | } |
| 1554 | else if (flags & SOPT_NUM) |
| 1555 | return PyInt_FromLong(numval); |
| 1556 | else if (flags & SOPT_STRING) |
| 1557 | { |
| 1558 | if (stringval) |
| 1559 | return PyBytes_FromString((char *) stringval); |
| 1560 | else |
| 1561 | { |
| 1562 | PyErr_SetString(PyExc_ValueError, "Unable to get option value"); |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | } |
| 1566 | else |
| 1567 | { |
| 1568 | PyErr_SetVim("Internal error: unknown option type. Should not happen"); |
| 1569 | return NULL; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | static int |
| 1574 | set_option_value_for(key, numval, stringval, opt_flags, opt_type, from) |
| 1575 | char_u *key; |
| 1576 | int numval; |
| 1577 | char_u *stringval; |
| 1578 | int opt_flags; |
| 1579 | int opt_type; |
| 1580 | void *from; |
| 1581 | { |
| 1582 | win_T *save_curwin; |
| 1583 | tabpage_T *save_curtab; |
| 1584 | aco_save_T aco; |
| 1585 | int r = 0; |
| 1586 | |
| 1587 | switch (opt_type) |
| 1588 | { |
| 1589 | case SREQ_WIN: |
| 1590 | if (switch_win(&save_curwin, &save_curtab, (win_T *) from, curtab) |
| 1591 | == FAIL) |
| 1592 | { |
| 1593 | PyErr_SetVim("Problem while switching windows."); |
| 1594 | return -1; |
| 1595 | } |
| 1596 | set_option_value(key, numval, stringval, opt_flags); |
| 1597 | restore_win(save_curwin, save_curtab); |
| 1598 | break; |
| 1599 | case SREQ_BUF: |
| 1600 | aucmd_prepbuf(&aco, (buf_T *) from); |
| 1601 | set_option_value(key, numval, stringval, opt_flags); |
| 1602 | aucmd_restbuf(&aco); |
| 1603 | break; |
| 1604 | case SREQ_GLOBAL: |
| 1605 | set_option_value(key, numval, stringval, opt_flags); |
| 1606 | break; |
| 1607 | } |
| 1608 | return r; |
| 1609 | } |
| 1610 | |
| 1611 | static int |
| 1612 | OptionsAssItem(OptionsObject *this, PyObject *keyObject, PyObject *valObject) |
| 1613 | { |
| 1614 | char_u *key; |
| 1615 | int flags; |
| 1616 | int opt_flags; |
| 1617 | int r = 0; |
Bram Moolenaar | 161fb5e | 2013-05-06 06:26:15 +0200 | [diff] [blame] | 1618 | DICTKEY_DECL |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1619 | |
| 1620 | if (this->Check(this->from)) |
| 1621 | return -1; |
| 1622 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1623 | DICTKEY_GET_NOTEMPTY(-1) |
| 1624 | |
| 1625 | flags = get_option_value_strict(key, NULL, NULL, |
| 1626 | this->opt_type, this->from); |
| 1627 | |
| 1628 | DICTKEY_UNREF |
| 1629 | |
| 1630 | if (flags == 0) |
| 1631 | { |
| 1632 | PyErr_SetString(PyExc_KeyError, "Option does not exist in given scope"); |
| 1633 | return -1; |
| 1634 | } |
| 1635 | |
| 1636 | if (valObject == NULL) |
| 1637 | { |
| 1638 | if (this->opt_type == SREQ_GLOBAL) |
| 1639 | { |
| 1640 | PyErr_SetString(PyExc_ValueError, "Unable to unset global option"); |
| 1641 | return -1; |
| 1642 | } |
| 1643 | else if (!(flags & SOPT_GLOBAL)) |
| 1644 | { |
| 1645 | PyErr_SetString(PyExc_ValueError, "Unable to unset option without " |
| 1646 | "global value"); |
| 1647 | return -1; |
| 1648 | } |
| 1649 | else |
| 1650 | { |
| 1651 | unset_global_local_option(key, this->from); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | opt_flags = (this->opt_type ? OPT_LOCAL : OPT_GLOBAL); |
| 1657 | |
| 1658 | if (flags & SOPT_BOOL) |
| 1659 | { |
| 1660 | if (!PyBool_Check(valObject)) |
| 1661 | { |
| 1662 | PyErr_SetString(PyExc_ValueError, "Object must be boolean"); |
| 1663 | return -1; |
| 1664 | } |
| 1665 | |
| 1666 | r = set_option_value_for(key, (valObject == Py_True), NULL, opt_flags, |
| 1667 | this->opt_type, this->from); |
| 1668 | } |
| 1669 | else if (flags & SOPT_NUM) |
| 1670 | { |
| 1671 | int val; |
| 1672 | |
| 1673 | #if PY_MAJOR_VERSION < 3 |
| 1674 | if (PyInt_Check(valObject)) |
| 1675 | val = PyInt_AsLong(valObject); |
| 1676 | else |
| 1677 | #endif |
| 1678 | if (PyLong_Check(valObject)) |
| 1679 | val = PyLong_AsLong(valObject); |
| 1680 | else |
| 1681 | { |
| 1682 | PyErr_SetString(PyExc_ValueError, "Object must be integer"); |
| 1683 | return -1; |
| 1684 | } |
| 1685 | |
| 1686 | r = set_option_value_for(key, val, NULL, opt_flags, |
| 1687 | this->opt_type, this->from); |
| 1688 | } |
| 1689 | else |
| 1690 | { |
| 1691 | char_u *val; |
| 1692 | if (PyBytes_Check(valObject)) |
| 1693 | { |
| 1694 | |
| 1695 | if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1) |
| 1696 | return -1; |
| 1697 | if (val == NULL) |
| 1698 | return -1; |
| 1699 | |
| 1700 | val = vim_strsave(val); |
| 1701 | } |
| 1702 | else if (PyUnicode_Check(valObject)) |
| 1703 | { |
| 1704 | PyObject *bytes; |
| 1705 | |
| 1706 | bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL); |
| 1707 | if (bytes == NULL) |
| 1708 | return -1; |
| 1709 | |
| 1710 | if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1) |
| 1711 | return -1; |
| 1712 | if (val == NULL) |
| 1713 | return -1; |
| 1714 | |
| 1715 | val = vim_strsave(val); |
| 1716 | Py_XDECREF(bytes); |
| 1717 | } |
| 1718 | else |
| 1719 | { |
| 1720 | PyErr_SetString(PyExc_ValueError, "Object must be string"); |
| 1721 | return -1; |
| 1722 | } |
| 1723 | |
| 1724 | r = set_option_value_for(key, 0, val, opt_flags, |
| 1725 | this->opt_type, this->from); |
| 1726 | vim_free(val); |
| 1727 | } |
| 1728 | |
| 1729 | return r; |
| 1730 | } |
| 1731 | |
| 1732 | static int |
| 1733 | dummy_check(void *arg UNUSED) |
| 1734 | { |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | static PyObject * |
| 1739 | OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj) |
| 1740 | { |
| 1741 | OptionsObject *self; |
| 1742 | |
| 1743 | self = PyObject_NEW(OptionsObject, &OptionsType); |
| 1744 | if (self == NULL) |
| 1745 | return NULL; |
| 1746 | |
| 1747 | self->opt_type = opt_type; |
| 1748 | self->from = from; |
| 1749 | self->Check = Check; |
| 1750 | self->fromObj = fromObj; |
| 1751 | if (fromObj) |
| 1752 | Py_INCREF(fromObj); |
| 1753 | |
| 1754 | return (PyObject *)(self); |
| 1755 | } |
| 1756 | |
| 1757 | static void |
| 1758 | OptionsDestructor(PyObject *self) |
| 1759 | { |
| 1760 | if (((OptionsObject *)(self))->fromObj) |
| 1761 | Py_DECREF(((OptionsObject *)(self))->fromObj); |
| 1762 | DESTRUCTOR_FINISH(self); |
| 1763 | } |
| 1764 | |
| 1765 | static PyMappingMethods OptionsAsMapping = { |
| 1766 | (lenfunc) NULL, |
| 1767 | (binaryfunc) OptionsItem, |
| 1768 | (objobjargproc) OptionsAssItem, |
| 1769 | }; |
| 1770 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1771 | #define INVALID_WINDOW_VALUE ((win_T *)(-1)) |
| 1772 | |
| 1773 | static int |
| 1774 | CheckWindow(WindowObject *this) |
| 1775 | { |
| 1776 | if (this->win == INVALID_WINDOW_VALUE) |
| 1777 | { |
| 1778 | PyErr_SetVim(_("attempt to refer to deleted window")); |
| 1779 | return -1; |
| 1780 | } |
| 1781 | |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame^] | 1785 | /* Window object |
| 1786 | */ |
| 1787 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1788 | static int WindowSetattr(PyObject *, char *, PyObject *); |
| 1789 | static PyObject *WindowRepr(PyObject *); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1790 | static PyTypeObject WindowType; |
| 1791 | |
| 1792 | static PyObject * |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame^] | 1793 | WindowNew(win_T *win) |
| 1794 | { |
| 1795 | /* We need to handle deletion of windows underneath us. |
| 1796 | * If we add a "w_python*_ref" field to the win_T structure, |
| 1797 | * then we can get at it in win_free() in vim. We then |
| 1798 | * need to create only ONE Python object per window - if |
| 1799 | * we try to create a second, just INCREF the existing one |
| 1800 | * and return it. The (single) Python object referring to |
| 1801 | * the window is stored in "w_python*_ref". |
| 1802 | * On a win_free() we set the Python object's win_T* field |
| 1803 | * to an invalid value. We trap all uses of a window |
| 1804 | * object, and reject them if the win_T* field is invalid. |
| 1805 | * |
| 1806 | * Python2 and Python3 get different fields and different objects: |
| 1807 | * w_python_ref and w_python3_ref fields respectively. |
| 1808 | */ |
| 1809 | |
| 1810 | WindowObject *self; |
| 1811 | |
| 1812 | if (WIN_PYTHON_REF(win)) |
| 1813 | { |
| 1814 | self = WIN_PYTHON_REF(win); |
| 1815 | Py_INCREF(self); |
| 1816 | } |
| 1817 | else |
| 1818 | { |
| 1819 | self = PyObject_NEW(WindowObject, &WindowType); |
| 1820 | if (self == NULL) |
| 1821 | return NULL; |
| 1822 | self->win = win; |
| 1823 | WIN_PYTHON_REF(win) = self; |
| 1824 | } |
| 1825 | |
| 1826 | return (PyObject *)(self); |
| 1827 | } |
| 1828 | |
| 1829 | static PyObject * |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1830 | WindowAttr(WindowObject *this, char *name) |
| 1831 | { |
| 1832 | if (strcmp(name, "buffer") == 0) |
| 1833 | return (PyObject *)BufferNew(this->win->w_buffer); |
| 1834 | else if (strcmp(name, "cursor") == 0) |
| 1835 | { |
| 1836 | pos_T *pos = &this->win->w_cursor; |
| 1837 | |
| 1838 | return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); |
| 1839 | } |
| 1840 | else if (strcmp(name, "height") == 0) |
| 1841 | return Py_BuildValue("l", (long)(this->win->w_height)); |
| 1842 | #ifdef FEAT_VERTSPLIT |
| 1843 | else if (strcmp(name, "width") == 0) |
| 1844 | return Py_BuildValue("l", (long)(W_WIDTH(this->win))); |
| 1845 | #endif |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 1846 | else if (strcmp(name, "vars") == 0) |
| 1847 | return DictionaryNew(this->win->w_vars); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1848 | else if (strcmp(name, "options") == 0) |
| 1849 | return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow, |
| 1850 | (PyObject *) this); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1851 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame^] | 1852 | return Py_BuildValue("[ssssss]", "buffer", "cursor", "height", "vars", |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1853 | "options"); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1854 | else |
| 1855 | return NULL; |
| 1856 | } |
| 1857 | |
| 1858 | static void |
| 1859 | WindowDestructor(PyObject *self) |
| 1860 | { |
| 1861 | WindowObject *this = (WindowObject *)(self); |
| 1862 | |
| 1863 | if (this->win && this->win != INVALID_WINDOW_VALUE) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame^] | 1864 | WIN_PYTHON_REF(this->win) = NULL; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1865 | |
| 1866 | DESTRUCTOR_FINISH(self); |
| 1867 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1868 | |
| 1869 | static int |
| 1870 | WindowSetattr(PyObject *self, char *name, PyObject *val) |
| 1871 | { |
| 1872 | WindowObject *this = (WindowObject *)(self); |
| 1873 | |
| 1874 | if (CheckWindow(this)) |
| 1875 | return -1; |
| 1876 | |
| 1877 | if (strcmp(name, "buffer") == 0) |
| 1878 | { |
| 1879 | PyErr_SetString(PyExc_TypeError, _("readonly attribute")); |
| 1880 | return -1; |
| 1881 | } |
| 1882 | else if (strcmp(name, "cursor") == 0) |
| 1883 | { |
| 1884 | long lnum; |
| 1885 | long col; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1886 | |
| 1887 | if (!PyArg_Parse(val, "(ll)", &lnum, &col)) |
| 1888 | return -1; |
| 1889 | |
| 1890 | if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count) |
| 1891 | { |
| 1892 | PyErr_SetVim(_("cursor position outside buffer")); |
| 1893 | return -1; |
| 1894 | } |
| 1895 | |
| 1896 | /* Check for keyboard interrupts */ |
| 1897 | if (VimErrorCheck()) |
| 1898 | return -1; |
| 1899 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1900 | this->win->w_cursor.lnum = lnum; |
| 1901 | this->win->w_cursor.col = col; |
| 1902 | #ifdef FEAT_VIRTUALEDIT |
| 1903 | this->win->w_cursor.coladd = 0; |
| 1904 | #endif |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 1905 | /* When column is out of range silently correct it. */ |
| 1906 | check_cursor_col_win(this->win); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1907 | |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 1908 | update_screen(VALID); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1909 | return 0; |
| 1910 | } |
| 1911 | else if (strcmp(name, "height") == 0) |
| 1912 | { |
| 1913 | int height; |
| 1914 | win_T *savewin; |
| 1915 | |
| 1916 | if (!PyArg_Parse(val, "i", &height)) |
| 1917 | return -1; |
| 1918 | |
| 1919 | #ifdef FEAT_GUI |
| 1920 | need_mouse_correct = TRUE; |
| 1921 | #endif |
| 1922 | savewin = curwin; |
| 1923 | curwin = this->win; |
| 1924 | win_setheight(height); |
| 1925 | curwin = savewin; |
| 1926 | |
| 1927 | /* Check for keyboard interrupts */ |
| 1928 | if (VimErrorCheck()) |
| 1929 | return -1; |
| 1930 | |
| 1931 | return 0; |
| 1932 | } |
| 1933 | #ifdef FEAT_VERTSPLIT |
| 1934 | else if (strcmp(name, "width") == 0) |
| 1935 | { |
| 1936 | int width; |
| 1937 | win_T *savewin; |
| 1938 | |
| 1939 | if (!PyArg_Parse(val, "i", &width)) |
| 1940 | return -1; |
| 1941 | |
| 1942 | #ifdef FEAT_GUI |
| 1943 | need_mouse_correct = TRUE; |
| 1944 | #endif |
| 1945 | savewin = curwin; |
| 1946 | curwin = this->win; |
| 1947 | win_setwidth(width); |
| 1948 | curwin = savewin; |
| 1949 | |
| 1950 | /* Check for keyboard interrupts */ |
| 1951 | if (VimErrorCheck()) |
| 1952 | return -1; |
| 1953 | |
| 1954 | return 0; |
| 1955 | } |
| 1956 | #endif |
| 1957 | else |
| 1958 | { |
| 1959 | PyErr_SetString(PyExc_AttributeError, name); |
| 1960 | return -1; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | static PyObject * |
| 1965 | WindowRepr(PyObject *self) |
| 1966 | { |
| 1967 | static char repr[100]; |
| 1968 | WindowObject *this = (WindowObject *)(self); |
| 1969 | |
| 1970 | if (this->win == INVALID_WINDOW_VALUE) |
| 1971 | { |
| 1972 | vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
| 1973 | return PyString_FromString(repr); |
| 1974 | } |
| 1975 | else |
| 1976 | { |
| 1977 | int i = 0; |
| 1978 | win_T *w; |
| 1979 | |
| 1980 | for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w)) |
| 1981 | ++i; |
| 1982 | |
| 1983 | if (w == NULL) |
| 1984 | vim_snprintf(repr, 100, _("<window object (unknown) at %p>"), |
| 1985 | (self)); |
| 1986 | else |
| 1987 | vim_snprintf(repr, 100, _("<window %d>"), i); |
| 1988 | |
| 1989 | return PyString_FromString(repr); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * Window list object - Implementation |
| 1995 | */ |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1996 | |
| 1997 | typedef struct |
| 1998 | { |
| 1999 | PyObject_HEAD |
| 2000 | } WinListObject; |
| 2001 | |
| 2002 | static PyTypeObject WinListType; |
| 2003 | static PySequenceMethods BufListAsSeq; |
| 2004 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2005 | static PyInt |
| 2006 | WinListLength(PyObject *self UNUSED) |
| 2007 | { |
| 2008 | win_T *w = firstwin; |
| 2009 | PyInt n = 0; |
| 2010 | |
| 2011 | while (w != NULL) |
| 2012 | { |
| 2013 | ++n; |
| 2014 | w = W_NEXT(w); |
| 2015 | } |
| 2016 | |
| 2017 | return n; |
| 2018 | } |
| 2019 | |
| 2020 | static PyObject * |
| 2021 | WinListItem(PyObject *self UNUSED, PyInt n) |
| 2022 | { |
| 2023 | win_T *w; |
| 2024 | |
| 2025 | for (w = firstwin; w != NULL; w = W_NEXT(w), --n) |
| 2026 | if (n == 0) |
| 2027 | return WindowNew(w); |
| 2028 | |
| 2029 | PyErr_SetString(PyExc_IndexError, _("no such window")); |
| 2030 | return NULL; |
| 2031 | } |
| 2032 | |
| 2033 | /* Convert a Python string into a Vim line. |
| 2034 | * |
| 2035 | * The result is in allocated memory. All internal nulls are replaced by |
| 2036 | * newline characters. It is an error for the string to contain newline |
| 2037 | * characters. |
| 2038 | * |
| 2039 | * On errors, the Python exception data is set, and NULL is returned. |
| 2040 | */ |
| 2041 | static char * |
| 2042 | StringToLine(PyObject *obj) |
| 2043 | { |
| 2044 | const char *str; |
| 2045 | char *save; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2046 | PyObject *bytes; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2047 | PyInt len; |
| 2048 | PyInt i; |
| 2049 | char *p; |
| 2050 | |
| 2051 | if (obj == NULL || !PyString_Check(obj)) |
| 2052 | { |
| 2053 | PyErr_BadArgument(); |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2057 | bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
| 2058 | str = PyString_AsString(bytes); |
| 2059 | len = PyString_Size(bytes); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2060 | |
| 2061 | /* |
| 2062 | * Error checking: String must not contain newlines, as we |
| 2063 | * are replacing a single line, and we must replace it with |
| 2064 | * a single line. |
| 2065 | * A trailing newline is removed, so that append(f.readlines()) works. |
| 2066 | */ |
| 2067 | p = memchr(str, '\n', len); |
| 2068 | if (p != NULL) |
| 2069 | { |
| 2070 | if (p == str + len - 1) |
| 2071 | --len; |
| 2072 | else |
| 2073 | { |
| 2074 | PyErr_SetVim(_("string cannot contain newlines")); |
| 2075 | return NULL; |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | /* Create a copy of the string, with internal nulls replaced by |
| 2080 | * newline characters, as is the vim convention. |
| 2081 | */ |
| 2082 | save = (char *)alloc((unsigned)(len+1)); |
| 2083 | if (save == NULL) |
| 2084 | { |
| 2085 | PyErr_NoMemory(); |
| 2086 | return NULL; |
| 2087 | } |
| 2088 | |
| 2089 | for (i = 0; i < len; ++i) |
| 2090 | { |
| 2091 | if (str[i] == '\0') |
| 2092 | save[i] = '\n'; |
| 2093 | else |
| 2094 | save[i] = str[i]; |
| 2095 | } |
| 2096 | |
| 2097 | save[i] = '\0'; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2098 | PyString_FreeBytes(bytes); /* Python 2 does nothing here */ |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2099 | |
| 2100 | return save; |
| 2101 | } |
| 2102 | |
| 2103 | /* Get a line from the specified buffer. The line number is |
| 2104 | * in Vim format (1-based). The line is returned as a Python |
| 2105 | * string object. |
| 2106 | */ |
| 2107 | static PyObject * |
| 2108 | GetBufferLine(buf_T *buf, PyInt n) |
| 2109 | { |
| 2110 | return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
| 2111 | } |
| 2112 | |
| 2113 | |
| 2114 | /* Get a list of lines from the specified buffer. The line numbers |
| 2115 | * are in Vim format (1-based). The range is from lo up to, but not |
| 2116 | * including, hi. The list is returned as a Python list of string objects. |
| 2117 | */ |
| 2118 | static PyObject * |
| 2119 | GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
| 2120 | { |
| 2121 | PyInt i; |
| 2122 | PyInt n = hi - lo; |
| 2123 | PyObject *list = PyList_New(n); |
| 2124 | |
| 2125 | if (list == NULL) |
| 2126 | return NULL; |
| 2127 | |
| 2128 | for (i = 0; i < n; ++i) |
| 2129 | { |
| 2130 | PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
| 2131 | |
| 2132 | /* Error check - was the Python string creation OK? */ |
| 2133 | if (str == NULL) |
| 2134 | { |
| 2135 | Py_DECREF(list); |
| 2136 | return NULL; |
| 2137 | } |
| 2138 | |
| 2139 | /* Set the list item */ |
| 2140 | if (PyList_SetItem(list, i, str)) |
| 2141 | { |
| 2142 | Py_DECREF(str); |
| 2143 | Py_DECREF(list); |
| 2144 | return NULL; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | /* The ownership of the Python list is passed to the caller (ie, |
| 2149 | * the caller should Py_DECREF() the object when it is finished |
| 2150 | * with it). |
| 2151 | */ |
| 2152 | |
| 2153 | return list; |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * Check if deleting lines made the cursor position invalid. |
| 2158 | * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
| 2159 | * deleted). |
| 2160 | */ |
| 2161 | static void |
| 2162 | py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
| 2163 | { |
| 2164 | if (curwin->w_cursor.lnum >= lo) |
| 2165 | { |
| 2166 | /* Adjust the cursor position if it's in/after the changed |
| 2167 | * lines. */ |
| 2168 | if (curwin->w_cursor.lnum >= hi) |
| 2169 | { |
| 2170 | curwin->w_cursor.lnum += extra; |
| 2171 | check_cursor_col(); |
| 2172 | } |
| 2173 | else if (extra < 0) |
| 2174 | { |
| 2175 | curwin->w_cursor.lnum = lo; |
| 2176 | check_cursor(); |
| 2177 | } |
| 2178 | else |
| 2179 | check_cursor_col(); |
| 2180 | changed_cline_bef_curs(); |
| 2181 | } |
| 2182 | invalidate_botline(); |
| 2183 | } |
| 2184 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2185 | /* |
| 2186 | * Replace a line in the specified buffer. The line number is |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2187 | * in Vim format (1-based). The replacement line is given as |
| 2188 | * a Python string object. The object is checked for validity |
| 2189 | * and correct format. Errors are returned as a value of FAIL. |
| 2190 | * The return value is OK on success. |
| 2191 | * If OK is returned and len_change is not NULL, *len_change |
| 2192 | * is set to the change in the buffer length. |
| 2193 | */ |
| 2194 | static int |
| 2195 | SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) |
| 2196 | { |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2197 | /* First of all, we check the type of the supplied Python object. |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2198 | * There are three cases: |
| 2199 | * 1. NULL, or None - this is a deletion. |
| 2200 | * 2. A string - this is a replacement. |
| 2201 | * 3. Anything else - this is an error. |
| 2202 | */ |
| 2203 | if (line == Py_None || line == NULL) |
| 2204 | { |
| 2205 | buf_T *savebuf = curbuf; |
| 2206 | |
| 2207 | PyErr_Clear(); |
| 2208 | curbuf = buf; |
| 2209 | |
| 2210 | if (u_savedel((linenr_T)n, 1L) == FAIL) |
| 2211 | PyErr_SetVim(_("cannot save undo information")); |
| 2212 | else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
| 2213 | PyErr_SetVim(_("cannot delete line")); |
| 2214 | else |
| 2215 | { |
| 2216 | if (buf == curwin->w_buffer) |
| 2217 | py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
| 2218 | deleted_lines_mark((linenr_T)n, 1L); |
| 2219 | } |
| 2220 | |
| 2221 | curbuf = savebuf; |
| 2222 | |
| 2223 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2224 | return FAIL; |
| 2225 | |
| 2226 | if (len_change) |
| 2227 | *len_change = -1; |
| 2228 | |
| 2229 | return OK; |
| 2230 | } |
| 2231 | else if (PyString_Check(line)) |
| 2232 | { |
| 2233 | char *save = StringToLine(line); |
| 2234 | buf_T *savebuf = curbuf; |
| 2235 | |
| 2236 | if (save == NULL) |
| 2237 | return FAIL; |
| 2238 | |
| 2239 | /* We do not need to free "save" if ml_replace() consumes it. */ |
| 2240 | PyErr_Clear(); |
| 2241 | curbuf = buf; |
| 2242 | |
| 2243 | if (u_savesub((linenr_T)n) == FAIL) |
| 2244 | { |
| 2245 | PyErr_SetVim(_("cannot save undo information")); |
| 2246 | vim_free(save); |
| 2247 | } |
| 2248 | else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
| 2249 | { |
| 2250 | PyErr_SetVim(_("cannot replace line")); |
| 2251 | vim_free(save); |
| 2252 | } |
| 2253 | else |
| 2254 | changed_bytes((linenr_T)n, 0); |
| 2255 | |
| 2256 | curbuf = savebuf; |
| 2257 | |
| 2258 | /* Check that the cursor is not beyond the end of the line now. */ |
| 2259 | if (buf == curwin->w_buffer) |
| 2260 | check_cursor_col(); |
| 2261 | |
| 2262 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2263 | return FAIL; |
| 2264 | |
| 2265 | if (len_change) |
| 2266 | *len_change = 0; |
| 2267 | |
| 2268 | return OK; |
| 2269 | } |
| 2270 | else |
| 2271 | { |
| 2272 | PyErr_BadArgument(); |
| 2273 | return FAIL; |
| 2274 | } |
| 2275 | } |
| 2276 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2277 | /* Replace a range of lines in the specified buffer. The line numbers are in |
| 2278 | * Vim format (1-based). The range is from lo up to, but not including, hi. |
| 2279 | * The replacement lines are given as a Python list of string objects. The |
| 2280 | * list is checked for validity and correct format. Errors are returned as a |
| 2281 | * value of FAIL. The return value is OK on success. |
| 2282 | * If OK is returned and len_change is not NULL, *len_change |
| 2283 | * is set to the change in the buffer length. |
| 2284 | */ |
| 2285 | static int |
| 2286 | SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) |
| 2287 | { |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2288 | /* First of all, we check the type of the supplied Python object. |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2289 | * There are three cases: |
| 2290 | * 1. NULL, or None - this is a deletion. |
| 2291 | * 2. A list - this is a replacement. |
| 2292 | * 3. Anything else - this is an error. |
| 2293 | */ |
| 2294 | if (list == Py_None || list == NULL) |
| 2295 | { |
| 2296 | PyInt i; |
| 2297 | PyInt n = (int)(hi - lo); |
| 2298 | buf_T *savebuf = curbuf; |
| 2299 | |
| 2300 | PyErr_Clear(); |
| 2301 | curbuf = buf; |
| 2302 | |
| 2303 | if (u_savedel((linenr_T)lo, (long)n) == FAIL) |
| 2304 | PyErr_SetVim(_("cannot save undo information")); |
| 2305 | else |
| 2306 | { |
| 2307 | for (i = 0; i < n; ++i) |
| 2308 | { |
| 2309 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 2310 | { |
| 2311 | PyErr_SetVim(_("cannot delete line")); |
| 2312 | break; |
| 2313 | } |
| 2314 | } |
| 2315 | if (buf == curwin->w_buffer) |
| 2316 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
| 2317 | deleted_lines_mark((linenr_T)lo, (long)i); |
| 2318 | } |
| 2319 | |
| 2320 | curbuf = savebuf; |
| 2321 | |
| 2322 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2323 | return FAIL; |
| 2324 | |
| 2325 | if (len_change) |
| 2326 | *len_change = -n; |
| 2327 | |
| 2328 | return OK; |
| 2329 | } |
| 2330 | else if (PyList_Check(list)) |
| 2331 | { |
| 2332 | PyInt i; |
| 2333 | PyInt new_len = PyList_Size(list); |
| 2334 | PyInt old_len = hi - lo; |
| 2335 | PyInt extra = 0; /* lines added to text, can be negative */ |
| 2336 | char **array; |
| 2337 | buf_T *savebuf; |
| 2338 | |
| 2339 | if (new_len == 0) /* avoid allocating zero bytes */ |
| 2340 | array = NULL; |
| 2341 | else |
| 2342 | { |
| 2343 | array = (char **)alloc((unsigned)(new_len * sizeof(char *))); |
| 2344 | if (array == NULL) |
| 2345 | { |
| 2346 | PyErr_NoMemory(); |
| 2347 | return FAIL; |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | for (i = 0; i < new_len; ++i) |
| 2352 | { |
| 2353 | PyObject *line = PyList_GetItem(list, i); |
| 2354 | |
| 2355 | array[i] = StringToLine(line); |
| 2356 | if (array[i] == NULL) |
| 2357 | { |
| 2358 | while (i) |
| 2359 | vim_free(array[--i]); |
| 2360 | vim_free(array); |
| 2361 | return FAIL; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | savebuf = curbuf; |
| 2366 | |
| 2367 | PyErr_Clear(); |
| 2368 | curbuf = buf; |
| 2369 | |
| 2370 | if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) |
| 2371 | PyErr_SetVim(_("cannot save undo information")); |
| 2372 | |
| 2373 | /* If the size of the range is reducing (ie, new_len < old_len) we |
| 2374 | * need to delete some old_len. We do this at the start, by |
| 2375 | * repeatedly deleting line "lo". |
| 2376 | */ |
| 2377 | if (!PyErr_Occurred()) |
| 2378 | { |
| 2379 | for (i = 0; i < old_len - new_len; ++i) |
| 2380 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 2381 | { |
| 2382 | PyErr_SetVim(_("cannot delete line")); |
| 2383 | break; |
| 2384 | } |
| 2385 | extra -= i; |
| 2386 | } |
| 2387 | |
| 2388 | /* For as long as possible, replace the existing old_len with the |
| 2389 | * new old_len. This is a more efficient operation, as it requires |
| 2390 | * less memory allocation and freeing. |
| 2391 | */ |
| 2392 | if (!PyErr_Occurred()) |
| 2393 | { |
| 2394 | for (i = 0; i < old_len && i < new_len; ++i) |
| 2395 | if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) |
| 2396 | == FAIL) |
| 2397 | { |
| 2398 | PyErr_SetVim(_("cannot replace line")); |
| 2399 | break; |
| 2400 | } |
| 2401 | } |
| 2402 | else |
| 2403 | i = 0; |
| 2404 | |
| 2405 | /* Now we may need to insert the remaining new old_len. If we do, we |
| 2406 | * must free the strings as we finish with them (we can't pass the |
| 2407 | * responsibility to vim in this case). |
| 2408 | */ |
| 2409 | if (!PyErr_Occurred()) |
| 2410 | { |
| 2411 | while (i < new_len) |
| 2412 | { |
| 2413 | if (ml_append((linenr_T)(lo + i - 1), |
| 2414 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2415 | { |
| 2416 | PyErr_SetVim(_("cannot insert line")); |
| 2417 | break; |
| 2418 | } |
| 2419 | vim_free(array[i]); |
| 2420 | ++i; |
| 2421 | ++extra; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | /* Free any left-over old_len, as a result of an error */ |
| 2426 | while (i < new_len) |
| 2427 | { |
| 2428 | vim_free(array[i]); |
| 2429 | ++i; |
| 2430 | } |
| 2431 | |
| 2432 | /* Free the array of old_len. All of its contents have now |
| 2433 | * been dealt with (either freed, or the responsibility passed |
| 2434 | * to vim. |
| 2435 | */ |
| 2436 | vim_free(array); |
| 2437 | |
| 2438 | /* Adjust marks. Invalidate any which lie in the |
| 2439 | * changed range, and move any in the remainder of the buffer. |
| 2440 | */ |
| 2441 | mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), |
| 2442 | (long)MAXLNUM, (long)extra); |
| 2443 | changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); |
| 2444 | |
| 2445 | if (buf == curwin->w_buffer) |
| 2446 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
| 2447 | |
| 2448 | curbuf = savebuf; |
| 2449 | |
| 2450 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2451 | return FAIL; |
| 2452 | |
| 2453 | if (len_change) |
| 2454 | *len_change = new_len - old_len; |
| 2455 | |
| 2456 | return OK; |
| 2457 | } |
| 2458 | else |
| 2459 | { |
| 2460 | PyErr_BadArgument(); |
| 2461 | return FAIL; |
| 2462 | } |
| 2463 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2464 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2465 | /* 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] | 2466 | * The line number is in Vim format (1-based). The lines to be inserted are |
| 2467 | * given as a Python list of string objects or as a single string. The lines |
| 2468 | * to be added are checked for validity and correct format. Errors are |
| 2469 | * returned as a value of FAIL. The return value is OK on success. |
| 2470 | * If OK is returned and len_change is not NULL, *len_change |
| 2471 | * is set to the change in the buffer length. |
| 2472 | */ |
| 2473 | static int |
| 2474 | InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
| 2475 | { |
| 2476 | /* First of all, we check the type of the supplied Python object. |
| 2477 | * It must be a string or a list, or the call is in error. |
| 2478 | */ |
| 2479 | if (PyString_Check(lines)) |
| 2480 | { |
| 2481 | char *str = StringToLine(lines); |
| 2482 | buf_T *savebuf; |
| 2483 | |
| 2484 | if (str == NULL) |
| 2485 | return FAIL; |
| 2486 | |
| 2487 | savebuf = curbuf; |
| 2488 | |
| 2489 | PyErr_Clear(); |
| 2490 | curbuf = buf; |
| 2491 | |
| 2492 | if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) |
| 2493 | PyErr_SetVim(_("cannot save undo information")); |
| 2494 | else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
| 2495 | PyErr_SetVim(_("cannot insert line")); |
| 2496 | else |
| 2497 | appended_lines_mark((linenr_T)n, 1L); |
| 2498 | |
| 2499 | vim_free(str); |
| 2500 | curbuf = savebuf; |
| 2501 | update_screen(VALID); |
| 2502 | |
| 2503 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2504 | return FAIL; |
| 2505 | |
| 2506 | if (len_change) |
| 2507 | *len_change = 1; |
| 2508 | |
| 2509 | return OK; |
| 2510 | } |
| 2511 | else if (PyList_Check(lines)) |
| 2512 | { |
| 2513 | PyInt i; |
| 2514 | PyInt size = PyList_Size(lines); |
| 2515 | char **array; |
| 2516 | buf_T *savebuf; |
| 2517 | |
| 2518 | array = (char **)alloc((unsigned)(size * sizeof(char *))); |
| 2519 | if (array == NULL) |
| 2520 | { |
| 2521 | PyErr_NoMemory(); |
| 2522 | return FAIL; |
| 2523 | } |
| 2524 | |
| 2525 | for (i = 0; i < size; ++i) |
| 2526 | { |
| 2527 | PyObject *line = PyList_GetItem(lines, i); |
| 2528 | array[i] = StringToLine(line); |
| 2529 | |
| 2530 | if (array[i] == NULL) |
| 2531 | { |
| 2532 | while (i) |
| 2533 | vim_free(array[--i]); |
| 2534 | vim_free(array); |
| 2535 | return FAIL; |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | savebuf = curbuf; |
| 2540 | |
| 2541 | PyErr_Clear(); |
| 2542 | curbuf = buf; |
| 2543 | |
| 2544 | if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
| 2545 | PyErr_SetVim(_("cannot save undo information")); |
| 2546 | else |
| 2547 | { |
| 2548 | for (i = 0; i < size; ++i) |
| 2549 | { |
| 2550 | if (ml_append((linenr_T)(n + i), |
| 2551 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2552 | { |
| 2553 | PyErr_SetVim(_("cannot insert line")); |
| 2554 | |
| 2555 | /* Free the rest of the lines */ |
| 2556 | while (i < size) |
| 2557 | vim_free(array[i++]); |
| 2558 | |
| 2559 | break; |
| 2560 | } |
| 2561 | vim_free(array[i]); |
| 2562 | } |
| 2563 | if (i > 0) |
| 2564 | appended_lines_mark((linenr_T)n, (long)i); |
| 2565 | } |
| 2566 | |
| 2567 | /* Free the array of lines. All of its contents have now |
| 2568 | * been freed. |
| 2569 | */ |
| 2570 | vim_free(array); |
| 2571 | |
| 2572 | curbuf = savebuf; |
| 2573 | update_screen(VALID); |
| 2574 | |
| 2575 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2576 | return FAIL; |
| 2577 | |
| 2578 | if (len_change) |
| 2579 | *len_change = size; |
| 2580 | |
| 2581 | return OK; |
| 2582 | } |
| 2583 | else |
| 2584 | { |
| 2585 | PyErr_BadArgument(); |
| 2586 | return FAIL; |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * Common routines for buffers and line ranges |
| 2592 | * ------------------------------------------- |
| 2593 | */ |
| 2594 | |
| 2595 | static int |
| 2596 | CheckBuffer(BufferObject *this) |
| 2597 | { |
| 2598 | if (this->buf == INVALID_BUFFER_VALUE) |
| 2599 | { |
| 2600 | PyErr_SetVim(_("attempt to refer to deleted buffer")); |
| 2601 | return -1; |
| 2602 | } |
| 2603 | |
| 2604 | return 0; |
| 2605 | } |
| 2606 | |
| 2607 | static PyObject * |
| 2608 | RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end) |
| 2609 | { |
| 2610 | if (CheckBuffer(self)) |
| 2611 | return NULL; |
| 2612 | |
| 2613 | if (n < 0 || n > end - start) |
| 2614 | { |
| 2615 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 2616 | return NULL; |
| 2617 | } |
| 2618 | |
| 2619 | return GetBufferLine(self->buf, n+start); |
| 2620 | } |
| 2621 | |
| 2622 | static PyObject * |
| 2623 | RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end) |
| 2624 | { |
| 2625 | PyInt size; |
| 2626 | |
| 2627 | if (CheckBuffer(self)) |
| 2628 | return NULL; |
| 2629 | |
| 2630 | size = end - start + 1; |
| 2631 | |
| 2632 | if (lo < 0) |
| 2633 | lo = 0; |
| 2634 | else if (lo > size) |
| 2635 | lo = size; |
| 2636 | if (hi < 0) |
| 2637 | hi = 0; |
| 2638 | if (hi < lo) |
| 2639 | hi = lo; |
| 2640 | else if (hi > size) |
| 2641 | hi = size; |
| 2642 | |
| 2643 | return GetBufferLineList(self->buf, lo+start, hi+start); |
| 2644 | } |
| 2645 | |
| 2646 | static PyInt |
| 2647 | RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 2648 | { |
| 2649 | PyInt len_change; |
| 2650 | |
| 2651 | if (CheckBuffer(self)) |
| 2652 | return -1; |
| 2653 | |
| 2654 | if (n < 0 || n > end - start) |
| 2655 | { |
| 2656 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 2657 | return -1; |
| 2658 | } |
| 2659 | |
| 2660 | if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) |
| 2661 | return -1; |
| 2662 | |
| 2663 | if (new_end) |
| 2664 | *new_end = end + len_change; |
| 2665 | |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2669 | static PyInt |
| 2670 | RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 2671 | { |
| 2672 | PyInt size; |
| 2673 | PyInt len_change; |
| 2674 | |
| 2675 | /* Self must be a valid buffer */ |
| 2676 | if (CheckBuffer(self)) |
| 2677 | return -1; |
| 2678 | |
| 2679 | /* Sort out the slice range */ |
| 2680 | size = end - start + 1; |
| 2681 | |
| 2682 | if (lo < 0) |
| 2683 | lo = 0; |
| 2684 | else if (lo > size) |
| 2685 | lo = size; |
| 2686 | if (hi < 0) |
| 2687 | hi = 0; |
| 2688 | if (hi < lo) |
| 2689 | hi = lo; |
| 2690 | else if (hi > size) |
| 2691 | hi = size; |
| 2692 | |
| 2693 | if (SetBufferLineList(self->buf, lo + start, hi + start, |
| 2694 | val, &len_change) == FAIL) |
| 2695 | return -1; |
| 2696 | |
| 2697 | if (new_end) |
| 2698 | *new_end = end + len_change; |
| 2699 | |
| 2700 | return 0; |
| 2701 | } |
| 2702 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2703 | |
| 2704 | static PyObject * |
| 2705 | RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end) |
| 2706 | { |
| 2707 | PyObject *lines; |
| 2708 | PyInt len_change; |
| 2709 | PyInt max; |
| 2710 | PyInt n; |
| 2711 | |
| 2712 | if (CheckBuffer(self)) |
| 2713 | return NULL; |
| 2714 | |
| 2715 | max = n = end - start + 1; |
| 2716 | |
| 2717 | if (!PyArg_ParseTuple(args, "O|n", &lines, &n)) |
| 2718 | return NULL; |
| 2719 | |
| 2720 | if (n < 0 || n > max) |
| 2721 | { |
| 2722 | PyErr_SetString(PyExc_ValueError, _("line number out of range")); |
| 2723 | return NULL; |
| 2724 | } |
| 2725 | |
| 2726 | if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
| 2727 | return NULL; |
| 2728 | |
| 2729 | if (new_end) |
| 2730 | *new_end = end + len_change; |
| 2731 | |
| 2732 | Py_INCREF(Py_None); |
| 2733 | return Py_None; |
| 2734 | } |
| 2735 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2736 | /* Range object - Definitions |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2737 | */ |
| 2738 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2739 | static PyTypeObject RangeType; |
| 2740 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2741 | typedef struct |
| 2742 | { |
| 2743 | PyObject_HEAD |
| 2744 | BufferObject *buf; |
| 2745 | PyInt start; |
| 2746 | PyInt end; |
| 2747 | } RangeObject; |
| 2748 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2749 | static void RangeDestructor(PyObject *); |
| 2750 | static PySequenceMethods RangeAsSeq; |
| 2751 | static PyMappingMethods RangeAsMapping; |
| 2752 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2753 | static PyObject * |
| 2754 | RangeNew(buf_T *buf, PyInt start, PyInt end) |
| 2755 | { |
| 2756 | BufferObject *bufr; |
| 2757 | RangeObject *self; |
| 2758 | self = PyObject_NEW(RangeObject, &RangeType); |
| 2759 | if (self == NULL) |
| 2760 | return NULL; |
| 2761 | |
| 2762 | bufr = (BufferObject *)BufferNew(buf); |
| 2763 | if (bufr == NULL) |
| 2764 | { |
| 2765 | Py_DECREF(self); |
| 2766 | return NULL; |
| 2767 | } |
| 2768 | Py_INCREF(bufr); |
| 2769 | |
| 2770 | self->buf = bufr; |
| 2771 | self->start = start; |
| 2772 | self->end = end; |
| 2773 | |
| 2774 | return (PyObject *)(self); |
| 2775 | } |
| 2776 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2777 | static void |
| 2778 | RangeDestructor(PyObject *self) |
| 2779 | { |
| 2780 | Py_DECREF(((RangeObject *)(self))->buf); |
| 2781 | DESTRUCTOR_FINISH(self); |
| 2782 | } |
| 2783 | |
| 2784 | static PyTypeObject BufferType; |
| 2785 | static PyObject *BufferRepr(PyObject *); |
| 2786 | static PySequenceMethods BufferAsSeq; |
| 2787 | static PyMappingMethods BufferAsMapping; |
| 2788 | |
| 2789 | static void |
| 2790 | BufferDestructor(PyObject *self) |
| 2791 | { |
| 2792 | BufferObject *this = (BufferObject *)(self); |
| 2793 | |
| 2794 | if (this->buf && this->buf != INVALID_BUFFER_VALUE) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame^] | 2795 | BUF_PYTHON_REF(this->buf) = NULL; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2796 | |
| 2797 | DESTRUCTOR_FINISH(self); |
| 2798 | } |
| 2799 | |
| 2800 | static PyObject * |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame^] | 2801 | BufferNew(buf_T *buf) |
| 2802 | { |
| 2803 | /* We need to handle deletion of buffers underneath us. |
| 2804 | * If we add a "b_python*_ref" field to the buf_T structure, |
| 2805 | * then we can get at it in buf_freeall() in vim. We then |
| 2806 | * need to create only ONE Python object per buffer - if |
| 2807 | * we try to create a second, just INCREF the existing one |
| 2808 | * and return it. The (single) Python object referring to |
| 2809 | * the buffer is stored in "b_python*_ref". |
| 2810 | * Question: what to do on a buf_freeall(). We'll probably |
| 2811 | * have to either delete the Python object (DECREF it to |
| 2812 | * zero - a bad idea, as it leaves dangling refs!) or |
| 2813 | * set the buf_T * value to an invalid value (-1?), which |
| 2814 | * means we need checks in all access functions... Bah. |
| 2815 | * |
| 2816 | * Python2 and Python3 get different fields and different objects: |
| 2817 | * b_python_ref and b_python3_ref fields respectively. |
| 2818 | */ |
| 2819 | |
| 2820 | BufferObject *self; |
| 2821 | |
| 2822 | if (BUF_PYTHON_REF(buf) != NULL) |
| 2823 | { |
| 2824 | self = BUF_PYTHON_REF(buf); |
| 2825 | Py_INCREF(self); |
| 2826 | } |
| 2827 | else |
| 2828 | { |
| 2829 | self = PyObject_NEW(BufferObject, &BufferType); |
| 2830 | if (self == NULL) |
| 2831 | return NULL; |
| 2832 | self->buf = buf; |
| 2833 | BUF_PYTHON_REF(buf) = self; |
| 2834 | } |
| 2835 | |
| 2836 | return (PyObject *)(self); |
| 2837 | } |
| 2838 | |
| 2839 | static PyObject * |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2840 | BufferAttr(BufferObject *this, char *name) |
| 2841 | { |
| 2842 | if (strcmp(name, "name") == 0) |
| 2843 | return Py_BuildValue("s", this->buf->b_ffname); |
| 2844 | else if (strcmp(name, "number") == 0) |
| 2845 | return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum); |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 2846 | else if (strcmp(name, "vars") == 0) |
| 2847 | return DictionaryNew(this->buf->b_vars); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 2848 | else if (strcmp(name, "options") == 0) |
| 2849 | return OptionsNew(SREQ_BUF, this->buf, (checkfun) CheckBuffer, |
| 2850 | (PyObject *) this); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2851 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 2852 | return Py_BuildValue("[ssss]", "name", "number", "vars", "options"); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2853 | else |
| 2854 | return NULL; |
| 2855 | } |
| 2856 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame^] | 2857 | static PyInt |
| 2858 | BufferLength(PyObject *self) |
| 2859 | { |
| 2860 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
| 2861 | if (CheckBuffer((BufferObject *)(self))) |
| 2862 | return -1; /* ??? */ |
| 2863 | |
| 2864 | return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 2865 | } |
| 2866 | |
| 2867 | static PyObject * |
| 2868 | BufferItem(PyObject *self, PyInt n) |
| 2869 | { |
| 2870 | return RBItem((BufferObject *)(self), n, 1, |
| 2871 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 2872 | } |
| 2873 | |
| 2874 | static PyObject * |
| 2875 | BufferSlice(PyObject *self, PyInt lo, PyInt hi) |
| 2876 | { |
| 2877 | return RBSlice((BufferObject *)(self), lo, hi, 1, |
| 2878 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 2879 | } |
| 2880 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2881 | static PyObject * |
| 2882 | BufferAppend(PyObject *self, PyObject *args) |
| 2883 | { |
| 2884 | return RBAppend((BufferObject *)(self), args, 1, |
| 2885 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 2886 | NULL); |
| 2887 | } |
| 2888 | |
| 2889 | static PyObject * |
| 2890 | BufferMark(PyObject *self, PyObject *args) |
| 2891 | { |
| 2892 | pos_T *posp; |
| 2893 | char *pmark; |
| 2894 | char mark; |
| 2895 | buf_T *curbuf_save; |
| 2896 | |
| 2897 | if (CheckBuffer((BufferObject *)(self))) |
| 2898 | return NULL; |
| 2899 | |
| 2900 | if (!PyArg_ParseTuple(args, "s", &pmark)) |
| 2901 | return NULL; |
| 2902 | mark = *pmark; |
| 2903 | |
| 2904 | curbuf_save = curbuf; |
| 2905 | curbuf = ((BufferObject *)(self))->buf; |
| 2906 | posp = getmark(mark, FALSE); |
| 2907 | curbuf = curbuf_save; |
| 2908 | |
| 2909 | if (posp == NULL) |
| 2910 | { |
| 2911 | PyErr_SetVim(_("invalid mark name")); |
| 2912 | return NULL; |
| 2913 | } |
| 2914 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2915 | /* Check for keyboard interrupt */ |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2916 | if (VimErrorCheck()) |
| 2917 | return NULL; |
| 2918 | |
| 2919 | if (posp->lnum <= 0) |
| 2920 | { |
| 2921 | /* Or raise an error? */ |
| 2922 | Py_INCREF(Py_None); |
| 2923 | return Py_None; |
| 2924 | } |
| 2925 | |
| 2926 | return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
| 2927 | } |
| 2928 | |
| 2929 | static PyObject * |
| 2930 | BufferRange(PyObject *self, PyObject *args) |
| 2931 | { |
| 2932 | PyInt start; |
| 2933 | PyInt end; |
| 2934 | |
| 2935 | if (CheckBuffer((BufferObject *)(self))) |
| 2936 | return NULL; |
| 2937 | |
| 2938 | if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
| 2939 | return NULL; |
| 2940 | |
| 2941 | return RangeNew(((BufferObject *)(self))->buf, start, end); |
| 2942 | } |
| 2943 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2944 | static PyObject * |
| 2945 | BufferRepr(PyObject *self) |
| 2946 | { |
| 2947 | static char repr[100]; |
| 2948 | BufferObject *this = (BufferObject *)(self); |
| 2949 | |
| 2950 | if (this->buf == INVALID_BUFFER_VALUE) |
| 2951 | { |
| 2952 | vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); |
| 2953 | return PyString_FromString(repr); |
| 2954 | } |
| 2955 | else |
| 2956 | { |
| 2957 | char *name = (char *)this->buf->b_fname; |
| 2958 | PyInt len; |
| 2959 | |
| 2960 | if (name == NULL) |
| 2961 | name = ""; |
| 2962 | len = strlen(name); |
| 2963 | |
| 2964 | if (len > 35) |
| 2965 | name = name + (35 - len); |
| 2966 | |
| 2967 | vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); |
| 2968 | |
| 2969 | return PyString_FromString(repr); |
| 2970 | } |
| 2971 | } |
| 2972 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2973 | static struct PyMethodDef BufferMethods[] = { |
| 2974 | /* name, function, calling, documentation */ |
| 2975 | {"append", BufferAppend, 1, "Append data to Vim buffer" }, |
| 2976 | {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" }, |
| 2977 | {"range", BufferRange, 1, "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] | 2978 | #if PY_VERSION_HEX >= 0x03000000 |
| 2979 | {"__dir__", BufferDir, 4, "List its attributes" }, |
| 2980 | #endif |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2981 | { NULL, NULL, 0, NULL } |
| 2982 | }; |
| 2983 | |
| 2984 | static PyObject * |
| 2985 | RangeAppend(PyObject *self, PyObject *args) |
| 2986 | { |
| 2987 | return RBAppend(((RangeObject *)(self))->buf, args, |
| 2988 | ((RangeObject *)(self))->start, |
| 2989 | ((RangeObject *)(self))->end, |
| 2990 | &((RangeObject *)(self))->end); |
| 2991 | } |
| 2992 | |
| 2993 | static PyInt |
| 2994 | RangeLength(PyObject *self) |
| 2995 | { |
| 2996 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
| 2997 | if (CheckBuffer(((RangeObject *)(self))->buf)) |
| 2998 | return -1; /* ??? */ |
| 2999 | |
| 3000 | return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); |
| 3001 | } |
| 3002 | |
| 3003 | static PyObject * |
| 3004 | RangeItem(PyObject *self, PyInt n) |
| 3005 | { |
| 3006 | return RBItem(((RangeObject *)(self))->buf, n, |
| 3007 | ((RangeObject *)(self))->start, |
| 3008 | ((RangeObject *)(self))->end); |
| 3009 | } |
| 3010 | |
| 3011 | static PyObject * |
| 3012 | RangeRepr(PyObject *self) |
| 3013 | { |
| 3014 | static char repr[100]; |
| 3015 | RangeObject *this = (RangeObject *)(self); |
| 3016 | |
| 3017 | if (this->buf->buf == INVALID_BUFFER_VALUE) |
| 3018 | { |
| 3019 | vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", |
| 3020 | (self)); |
| 3021 | return PyString_FromString(repr); |
| 3022 | } |
| 3023 | else |
| 3024 | { |
| 3025 | char *name = (char *)this->buf->buf->b_fname; |
| 3026 | int len; |
| 3027 | |
| 3028 | if (name == NULL) |
| 3029 | name = ""; |
| 3030 | len = (int)strlen(name); |
| 3031 | |
| 3032 | if (len > 45) |
| 3033 | name = name + (45 - len); |
| 3034 | |
| 3035 | vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", |
| 3036 | len > 45 ? "..." : "", name, |
| 3037 | this->start, this->end); |
| 3038 | |
| 3039 | return PyString_FromString(repr); |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | static PyObject * |
| 3044 | RangeSlice(PyObject *self, PyInt lo, PyInt hi) |
| 3045 | { |
| 3046 | return RBSlice(((RangeObject *)(self))->buf, lo, hi, |
| 3047 | ((RangeObject *)(self))->start, |
| 3048 | ((RangeObject *)(self))->end); |
| 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * Line range object - Definitions |
| 3053 | */ |
| 3054 | |
| 3055 | static struct PyMethodDef RangeMethods[] = { |
| 3056 | /* name, function, calling, documentation */ |
| 3057 | {"append", RangeAppend, 1, "Append data to the Vim range" }, |
| 3058 | { NULL, NULL, 0, NULL } |
| 3059 | }; |
| 3060 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3061 | /* Current items object - Implementation |
| 3062 | */ |
| 3063 | |
| 3064 | static PyInt RangeStart; |
| 3065 | static PyInt RangeEnd; |
| 3066 | |
| 3067 | static PyObject * |
| 3068 | CurrentGetattr(PyObject *self UNUSED, char *name) |
| 3069 | { |
| 3070 | if (strcmp(name, "buffer") == 0) |
| 3071 | return (PyObject *)BufferNew(curbuf); |
| 3072 | else if (strcmp(name, "window") == 0) |
| 3073 | return (PyObject *)WindowNew(curwin); |
| 3074 | else if (strcmp(name, "line") == 0) |
| 3075 | return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); |
| 3076 | else if (strcmp(name, "range") == 0) |
| 3077 | return RangeNew(curbuf, RangeStart, RangeEnd); |
| 3078 | else if (strcmp(name,"__members__") == 0) |
| 3079 | return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); |
| 3080 | else |
| 3081 | { |
| 3082 | PyErr_SetString(PyExc_AttributeError, name); |
| 3083 | return NULL; |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | static int |
| 3088 | CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) |
| 3089 | { |
| 3090 | if (strcmp(name, "line") == 0) |
| 3091 | { |
| 3092 | if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) |
| 3093 | return -1; |
| 3094 | |
| 3095 | return 0; |
| 3096 | } |
| 3097 | else |
| 3098 | { |
| 3099 | PyErr_SetString(PyExc_AttributeError, name); |
| 3100 | return -1; |
| 3101 | } |
| 3102 | } |
| 3103 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3104 | static void |
| 3105 | set_ref_in_py(const int copyID) |
| 3106 | { |
| 3107 | pylinkedlist_T *cur; |
| 3108 | dict_T *dd; |
| 3109 | list_T *ll; |
| 3110 | |
| 3111 | if (lastdict != NULL) |
| 3112 | for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) |
| 3113 | { |
| 3114 | dd = ((DictionaryObject *) (cur->pll_obj))->dict; |
| 3115 | if (dd->dv_copyID != copyID) |
| 3116 | { |
| 3117 | dd->dv_copyID = copyID; |
| 3118 | set_ref_in_ht(&dd->dv_hashtab, copyID); |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | if (lastlist != NULL) |
| 3123 | for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) |
| 3124 | { |
| 3125 | ll = ((ListObject *) (cur->pll_obj))->list; |
| 3126 | if (ll->lv_copyID != copyID) |
| 3127 | { |
| 3128 | ll->lv_copyID = copyID; |
| 3129 | set_ref_in_list(ll, copyID); |
| 3130 | } |
| 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | static int |
| 3135 | set_string_copy(char_u *str, typval_T *tv) |
| 3136 | { |
| 3137 | tv->vval.v_string = vim_strsave(str); |
| 3138 | if (tv->vval.v_string == NULL) |
| 3139 | { |
| 3140 | PyErr_NoMemory(); |
| 3141 | return -1; |
| 3142 | } |
| 3143 | return 0; |
| 3144 | } |
| 3145 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3146 | typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
| 3147 | |
| 3148 | static int |
| 3149 | convert_dl(PyObject *obj, typval_T *tv, |
| 3150 | pytotvfunc py_to_tv, PyObject *lookupDict) |
| 3151 | { |
| 3152 | PyObject *capsule; |
| 3153 | char hexBuf[sizeof(void *) * 2 + 3]; |
| 3154 | |
| 3155 | sprintf(hexBuf, "%p", obj); |
| 3156 | |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 3157 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3158 | capsule = PyDict_GetItemString(lookupDict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 3159 | # else |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 3160 | capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 3161 | # endif |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 3162 | if (capsule == NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3163 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 3164 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3165 | capsule = PyCapsule_New(tv, NULL, NULL); |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 3166 | # else |
| 3167 | capsule = PyCObject_FromVoidPtr(tv, NULL); |
| 3168 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3169 | PyDict_SetItemString(lookupDict, hexBuf, capsule); |
| 3170 | Py_DECREF(capsule); |
| 3171 | if (py_to_tv(obj, tv, lookupDict) == -1) |
| 3172 | { |
| 3173 | tv->v_type = VAR_UNKNOWN; |
| 3174 | return -1; |
| 3175 | } |
| 3176 | /* As we are not using copy_tv which increments reference count we must |
| 3177 | * do it ourself. */ |
| 3178 | switch(tv->v_type) |
| 3179 | { |
| 3180 | case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; |
| 3181 | case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; |
| 3182 | } |
| 3183 | } |
| 3184 | else |
| 3185 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 3186 | typval_T *v; |
| 3187 | |
| 3188 | # ifdef PY_USE_CAPSULE |
| 3189 | v = PyCapsule_GetPointer(capsule, NULL); |
| 3190 | # else |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 3191 | v = PyCObject_AsVoidPtr(capsule); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 3192 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3193 | copy_tv(v, tv); |
| 3194 | } |
| 3195 | return 0; |
| 3196 | } |
| 3197 | |
| 3198 | static int |
| 3199 | ConvertFromPyObject(PyObject *obj, typval_T *tv) |
| 3200 | { |
| 3201 | PyObject *lookup_dict; |
| 3202 | int r; |
| 3203 | |
| 3204 | lookup_dict = PyDict_New(); |
| 3205 | r = _ConvertFromPyObject(obj, tv, lookup_dict); |
| 3206 | Py_DECREF(lookup_dict); |
| 3207 | return r; |
| 3208 | } |
| 3209 | |
| 3210 | static int |
| 3211 | _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 3212 | { |
| 3213 | if (obj->ob_type == &DictionaryType) |
| 3214 | { |
| 3215 | tv->v_type = VAR_DICT; |
| 3216 | tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); |
| 3217 | ++tv->vval.v_dict->dv_refcount; |
| 3218 | } |
| 3219 | else if (obj->ob_type == &ListType) |
| 3220 | { |
| 3221 | tv->v_type = VAR_LIST; |
| 3222 | tv->vval.v_list = (((ListObject *)(obj))->list); |
| 3223 | ++tv->vval.v_list->lv_refcount; |
| 3224 | } |
| 3225 | else if (obj->ob_type == &FunctionType) |
| 3226 | { |
| 3227 | if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) |
| 3228 | return -1; |
| 3229 | |
| 3230 | tv->v_type = VAR_FUNC; |
| 3231 | func_ref(tv->vval.v_string); |
| 3232 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3233 | else if (PyBytes_Check(obj)) |
| 3234 | { |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 3235 | char_u *result; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3236 | |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 3237 | if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) |
| 3238 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3239 | if (result == NULL) |
| 3240 | return -1; |
| 3241 | |
| 3242 | if (set_string_copy(result, tv) == -1) |
| 3243 | return -1; |
| 3244 | |
| 3245 | tv->v_type = VAR_STRING; |
| 3246 | } |
| 3247 | else if (PyUnicode_Check(obj)) |
| 3248 | { |
| 3249 | PyObject *bytes; |
| 3250 | char_u *result; |
| 3251 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3252 | bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); |
| 3253 | if (bytes == NULL) |
| 3254 | return -1; |
| 3255 | |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 3256 | if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
| 3257 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3258 | if (result == NULL) |
| 3259 | return -1; |
| 3260 | |
| 3261 | if (set_string_copy(result, tv) == -1) |
| 3262 | { |
| 3263 | Py_XDECREF(bytes); |
| 3264 | return -1; |
| 3265 | } |
| 3266 | Py_XDECREF(bytes); |
| 3267 | |
| 3268 | tv->v_type = VAR_STRING; |
| 3269 | } |
Bram Moolenaar | 335e0b6 | 2013-04-24 13:47:45 +0200 | [diff] [blame] | 3270 | #if PY_MAJOR_VERSION < 3 |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3271 | else if (PyInt_Check(obj)) |
| 3272 | { |
| 3273 | tv->v_type = VAR_NUMBER; |
| 3274 | tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); |
| 3275 | } |
| 3276 | #endif |
| 3277 | else if (PyLong_Check(obj)) |
| 3278 | { |
| 3279 | tv->v_type = VAR_NUMBER; |
| 3280 | tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); |
| 3281 | } |
| 3282 | else if (PyDict_Check(obj)) |
| 3283 | return convert_dl(obj, tv, pydict_to_tv, lookupDict); |
| 3284 | #ifdef FEAT_FLOAT |
| 3285 | else if (PyFloat_Check(obj)) |
| 3286 | { |
| 3287 | tv->v_type = VAR_FLOAT; |
| 3288 | tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); |
| 3289 | } |
| 3290 | #endif |
| 3291 | else if (PyIter_Check(obj)) |
| 3292 | return convert_dl(obj, tv, pyiter_to_tv, lookupDict); |
| 3293 | else if (PySequence_Check(obj)) |
| 3294 | return convert_dl(obj, tv, pyseq_to_tv, lookupDict); |
| 3295 | else if (PyMapping_Check(obj)) |
| 3296 | return convert_dl(obj, tv, pymap_to_tv, lookupDict); |
| 3297 | else |
| 3298 | { |
| 3299 | PyErr_SetString(PyExc_TypeError, _("unable to convert to vim structure")); |
| 3300 | return -1; |
| 3301 | } |
| 3302 | return 0; |
| 3303 | } |
| 3304 | |
| 3305 | static PyObject * |
| 3306 | ConvertToPyObject(typval_T *tv) |
| 3307 | { |
| 3308 | if (tv == NULL) |
| 3309 | { |
| 3310 | PyErr_SetVim(_("NULL reference passed")); |
| 3311 | return NULL; |
| 3312 | } |
| 3313 | switch (tv->v_type) |
| 3314 | { |
| 3315 | case VAR_STRING: |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 3316 | return PyBytes_FromString(tv->vval.v_string == NULL |
| 3317 | ? "" : (char *)tv->vval.v_string); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3318 | case VAR_NUMBER: |
| 3319 | return PyLong_FromLong((long) tv->vval.v_number); |
| 3320 | #ifdef FEAT_FLOAT |
| 3321 | case VAR_FLOAT: |
| 3322 | return PyFloat_FromDouble((double) tv->vval.v_float); |
| 3323 | #endif |
| 3324 | case VAR_LIST: |
| 3325 | return ListNew(tv->vval.v_list); |
| 3326 | case VAR_DICT: |
| 3327 | return DictionaryNew(tv->vval.v_dict); |
| 3328 | case VAR_FUNC: |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 3329 | return FunctionNew(tv->vval.v_string == NULL |
| 3330 | ? (char_u *)"" : tv->vval.v_string); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3331 | case VAR_UNKNOWN: |
| 3332 | Py_INCREF(Py_None); |
| 3333 | return Py_None; |
| 3334 | default: |
| 3335 | PyErr_SetVim(_("internal error: invalid value type")); |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | } |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3339 | |
| 3340 | typedef struct |
| 3341 | { |
| 3342 | PyObject_HEAD |
| 3343 | } CurrentObject; |
| 3344 | static PyTypeObject CurrentType; |
| 3345 | |
| 3346 | static void |
| 3347 | init_structs(void) |
| 3348 | { |
| 3349 | vim_memset(&OutputType, 0, sizeof(OutputType)); |
| 3350 | OutputType.tp_name = "vim.message"; |
| 3351 | OutputType.tp_basicsize = sizeof(OutputObject); |
| 3352 | OutputType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3353 | OutputType.tp_doc = "vim message object"; |
| 3354 | OutputType.tp_methods = OutputMethods; |
| 3355 | #if PY_MAJOR_VERSION >= 3 |
| 3356 | OutputType.tp_getattro = OutputGetattro; |
| 3357 | OutputType.tp_setattro = OutputSetattro; |
| 3358 | OutputType.tp_alloc = call_PyType_GenericAlloc; |
| 3359 | OutputType.tp_new = call_PyType_GenericNew; |
| 3360 | OutputType.tp_free = call_PyObject_Free; |
| 3361 | #else |
| 3362 | OutputType.tp_getattr = OutputGetattr; |
| 3363 | OutputType.tp_setattr = OutputSetattr; |
| 3364 | #endif |
| 3365 | |
| 3366 | vim_memset(&BufferType, 0, sizeof(BufferType)); |
| 3367 | BufferType.tp_name = "vim.buffer"; |
| 3368 | BufferType.tp_basicsize = sizeof(BufferType); |
| 3369 | BufferType.tp_dealloc = BufferDestructor; |
| 3370 | BufferType.tp_repr = BufferRepr; |
| 3371 | BufferType.tp_as_sequence = &BufferAsSeq; |
| 3372 | BufferType.tp_as_mapping = &BufferAsMapping; |
| 3373 | BufferType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3374 | BufferType.tp_doc = "vim buffer object"; |
| 3375 | BufferType.tp_methods = BufferMethods; |
| 3376 | #if PY_MAJOR_VERSION >= 3 |
| 3377 | BufferType.tp_getattro = BufferGetattro; |
| 3378 | BufferType.tp_alloc = call_PyType_GenericAlloc; |
| 3379 | BufferType.tp_new = call_PyType_GenericNew; |
| 3380 | BufferType.tp_free = call_PyObject_Free; |
| 3381 | #else |
| 3382 | BufferType.tp_getattr = BufferGetattr; |
| 3383 | #endif |
| 3384 | |
| 3385 | vim_memset(&WindowType, 0, sizeof(WindowType)); |
| 3386 | WindowType.tp_name = "vim.window"; |
| 3387 | WindowType.tp_basicsize = sizeof(WindowObject); |
| 3388 | WindowType.tp_dealloc = WindowDestructor; |
| 3389 | WindowType.tp_repr = WindowRepr; |
| 3390 | WindowType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3391 | WindowType.tp_doc = "vim Window object"; |
| 3392 | WindowType.tp_methods = WindowMethods; |
| 3393 | #if PY_MAJOR_VERSION >= 3 |
| 3394 | WindowType.tp_getattro = WindowGetattro; |
| 3395 | WindowType.tp_setattro = WindowSetattro; |
| 3396 | WindowType.tp_alloc = call_PyType_GenericAlloc; |
| 3397 | WindowType.tp_new = call_PyType_GenericNew; |
| 3398 | WindowType.tp_free = call_PyObject_Free; |
| 3399 | #else |
| 3400 | WindowType.tp_getattr = WindowGetattr; |
| 3401 | WindowType.tp_setattr = WindowSetattr; |
| 3402 | #endif |
| 3403 | |
| 3404 | vim_memset(&BufListType, 0, sizeof(BufListType)); |
| 3405 | BufListType.tp_name = "vim.bufferlist"; |
| 3406 | BufListType.tp_basicsize = sizeof(BufListObject); |
| 3407 | BufListType.tp_as_sequence = &BufListAsSeq; |
| 3408 | BufListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3409 | BufferType.tp_doc = "vim buffer list"; |
| 3410 | |
| 3411 | vim_memset(&WinListType, 0, sizeof(WinListType)); |
| 3412 | WinListType.tp_name = "vim.windowlist"; |
| 3413 | WinListType.tp_basicsize = sizeof(WinListType); |
| 3414 | WinListType.tp_as_sequence = &WinListAsSeq; |
| 3415 | WinListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3416 | WinListType.tp_doc = "vim window list"; |
| 3417 | |
| 3418 | vim_memset(&RangeType, 0, sizeof(RangeType)); |
| 3419 | RangeType.tp_name = "vim.range"; |
| 3420 | RangeType.tp_basicsize = sizeof(RangeObject); |
| 3421 | RangeType.tp_dealloc = RangeDestructor; |
| 3422 | RangeType.tp_repr = RangeRepr; |
| 3423 | RangeType.tp_as_sequence = &RangeAsSeq; |
| 3424 | RangeType.tp_as_mapping = &RangeAsMapping; |
| 3425 | RangeType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3426 | RangeType.tp_doc = "vim Range object"; |
| 3427 | RangeType.tp_methods = RangeMethods; |
| 3428 | #if PY_MAJOR_VERSION >= 3 |
| 3429 | RangeType.tp_getattro = RangeGetattro; |
| 3430 | RangeType.tp_alloc = call_PyType_GenericAlloc; |
| 3431 | RangeType.tp_new = call_PyType_GenericNew; |
| 3432 | RangeType.tp_free = call_PyObject_Free; |
| 3433 | #else |
| 3434 | RangeType.tp_getattr = RangeGetattr; |
| 3435 | #endif |
| 3436 | |
| 3437 | vim_memset(&CurrentType, 0, sizeof(CurrentType)); |
| 3438 | CurrentType.tp_name = "vim.currentdata"; |
| 3439 | CurrentType.tp_basicsize = sizeof(CurrentObject); |
| 3440 | CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3441 | CurrentType.tp_doc = "vim current object"; |
| 3442 | #if PY_MAJOR_VERSION >= 3 |
| 3443 | CurrentType.tp_getattro = CurrentGetattro; |
| 3444 | CurrentType.tp_setattro = CurrentSetattro; |
| 3445 | #else |
| 3446 | CurrentType.tp_getattr = CurrentGetattr; |
| 3447 | CurrentType.tp_setattr = CurrentSetattr; |
| 3448 | #endif |
| 3449 | |
| 3450 | vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); |
| 3451 | DictionaryType.tp_name = "vim.dictionary"; |
| 3452 | DictionaryType.tp_basicsize = sizeof(DictionaryObject); |
| 3453 | DictionaryType.tp_dealloc = DictionaryDestructor; |
| 3454 | DictionaryType.tp_as_mapping = &DictionaryAsMapping; |
| 3455 | DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3456 | DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; |
| 3457 | DictionaryType.tp_methods = DictionaryMethods; |
| 3458 | #if PY_MAJOR_VERSION >= 3 |
| 3459 | DictionaryType.tp_getattro = DictionaryGetattro; |
| 3460 | DictionaryType.tp_setattro = DictionarySetattro; |
| 3461 | #else |
| 3462 | DictionaryType.tp_getattr = DictionaryGetattr; |
| 3463 | DictionaryType.tp_setattr = DictionarySetattr; |
| 3464 | #endif |
| 3465 | |
| 3466 | vim_memset(&ListType, 0, sizeof(ListType)); |
| 3467 | ListType.tp_name = "vim.list"; |
| 3468 | ListType.tp_dealloc = ListDestructor; |
| 3469 | ListType.tp_basicsize = sizeof(ListObject); |
| 3470 | ListType.tp_as_sequence = &ListAsSeq; |
| 3471 | ListType.tp_as_mapping = &ListAsMapping; |
| 3472 | ListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3473 | ListType.tp_doc = "list pushing modifications to vim structure"; |
| 3474 | ListType.tp_methods = ListMethods; |
| 3475 | #if PY_MAJOR_VERSION >= 3 |
| 3476 | ListType.tp_getattro = ListGetattro; |
| 3477 | ListType.tp_setattro = ListSetattro; |
| 3478 | #else |
| 3479 | ListType.tp_getattr = ListGetattr; |
| 3480 | ListType.tp_setattr = ListSetattr; |
| 3481 | #endif |
| 3482 | |
| 3483 | vim_memset(&FunctionType, 0, sizeof(FunctionType)); |
| 3484 | FunctionType.tp_name = "vim.list"; |
| 3485 | FunctionType.tp_basicsize = sizeof(FunctionObject); |
| 3486 | FunctionType.tp_dealloc = FunctionDestructor; |
| 3487 | FunctionType.tp_call = FunctionCall; |
| 3488 | FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3489 | FunctionType.tp_doc = "object that calls vim function"; |
| 3490 | FunctionType.tp_methods = FunctionMethods; |
| 3491 | #if PY_MAJOR_VERSION >= 3 |
| 3492 | FunctionType.tp_getattro = FunctionGetattro; |
| 3493 | #else |
| 3494 | FunctionType.tp_getattr = FunctionGetattr; |
| 3495 | #endif |
| 3496 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 3497 | vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
| 3498 | OptionsType.tp_name = "vim.options"; |
| 3499 | OptionsType.tp_basicsize = sizeof(OptionsObject); |
| 3500 | OptionsType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 3501 | OptionsType.tp_doc = "object for manipulating options"; |
| 3502 | OptionsType.tp_as_mapping = &OptionsAsMapping; |
| 3503 | OptionsType.tp_dealloc = OptionsDestructor; |
| 3504 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3505 | #if PY_MAJOR_VERSION >= 3 |
| 3506 | vim_memset(&vimmodule, 0, sizeof(vimmodule)); |
| 3507 | vimmodule.m_name = "vim"; |
| 3508 | vimmodule.m_doc = "Vim Python interface\n"; |
| 3509 | vimmodule.m_size = -1; |
| 3510 | vimmodule.m_methods = VimMethods; |
| 3511 | #endif |
| 3512 | } |