blob: 6cd1b23d3b16f026d687507d77d391f567e3d7ee [file] [log] [blame]
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001/* vi:set ts=8 sts=4 sw=4:
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
15/*
16 * obtain a lock on the Vim data structures
17 */
18 static void
19Python_Lock_Vim(void)
20{
21}
22
23/*
24 * release a lock on the Vim data structures
25 */
26 static void
27Python_Release_Vim(void)
28{
29}
30
31/* Output object definition
32 */
33
34static PyObject *OutputWrite(PyObject *, PyObject *);
35static PyObject *OutputWritelines(PyObject *, PyObject *);
Bram Moolenaara29a37d2011-03-22 15:47:44 +010036static PyObject *OutputFlush(PyObject *, PyObject *);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020037
Bram Moolenaar2eea1982010-09-21 16:49:37 +020038/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020039typedef void (*writefn)(char_u *);
40static void writer(writefn fn, char_u *str, PyInt n);
41
42typedef struct
43{
44 PyObject_HEAD
45 long softspace;
46 long error;
47} OutputObject;
48
49static struct PyMethodDef OutputMethods[] = {
50 /* name, function, calling, documentation */
Bram Moolenaara29a37d2011-03-22 15:47:44 +010051 {"write", OutputWrite, 1, ""},
52 {"writelines", OutputWritelines, 1, ""},
53 {"flush", OutputFlush, 1, ""},
54 { NULL, NULL, 0, NULL}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020055};
56
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020057#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
58
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020059/*************/
60
61/* Output buffer management
62 */
63
64 static PyObject *
65OutputWrite(PyObject *self, PyObject *args)
66{
67 int len;
Bram Moolenaar19e60942011-06-19 00:27:51 +020068 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020069 int error = ((OutputObject *)(self))->error;
70
Bram Moolenaar19e60942011-06-19 00:27:51 +020071 if (!PyArg_ParseTuple(args, "es#", p_enc, &str, &len))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020072 return NULL;
73
74 Py_BEGIN_ALLOW_THREADS
75 Python_Lock_Vim();
76 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
77 Python_Release_Vim();
78 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +020079 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020080
81 Py_INCREF(Py_None);
82 return Py_None;
83}
84
85 static PyObject *
86OutputWritelines(PyObject *self, PyObject *args)
87{
88 PyInt n;
89 PyInt i;
90 PyObject *list;
91 int error = ((OutputObject *)(self))->error;
92
93 if (!PyArg_ParseTuple(args, "O", &list))
94 return NULL;
95 Py_INCREF(list);
96
97 if (!PyList_Check(list)) {
98 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
99 Py_DECREF(list);
100 return NULL;
101 }
102
103 n = PyList_Size(list);
104
105 for (i = 0; i < n; ++i)
106 {
107 PyObject *line = PyList_GetItem(list, i);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200108 char *str = NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200109 PyInt len;
110
Bram Moolenaar19e60942011-06-19 00:27:51 +0200111 if (!PyArg_Parse(line, "es#", p_enc, &str, &len)) {
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200112 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
113 Py_DECREF(list);
114 return NULL;
115 }
116
117 Py_BEGIN_ALLOW_THREADS
118 Python_Lock_Vim();
119 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
120 Python_Release_Vim();
121 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200122 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200123 }
124
125 Py_DECREF(list);
126 Py_INCREF(Py_None);
127 return Py_None;
128}
129
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100130 static PyObject *
131OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED)
132{
133 /* do nothing */
134 Py_INCREF(Py_None);
135 return Py_None;
136}
137
138
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200139/* Buffer IO, we write one whole line at a time. */
140static garray_T io_ga = {0, 0, 1, 80, NULL};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200141static writefn old_fn = NULL;
142
143 static void
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200144PythonIO_Flush(void)
145{
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200146 if (old_fn != NULL && io_ga.ga_len > 0)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200147 {
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200148 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
149 old_fn((char_u *)io_ga.ga_data);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200150 }
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200151 io_ga.ga_len = 0;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200152}
153
154 static void
155writer(writefn fn, char_u *str, PyInt n)
156{
157 char_u *ptr;
158
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200159 /* Flush when switching output function. */
160 if (fn != old_fn)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200161 PythonIO_Flush();
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200162 old_fn = fn;
163
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200164 /* Write each NL separated line. Text after the last NL is kept for
165 * writing later. */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200166 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
167 {
168 PyInt len = ptr - str;
169
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200170 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200171 break;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200172
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200173 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
174 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
175 fn((char_u *)io_ga.ga_data);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200176 str = ptr + 1;
177 n -= len + 1;
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200178 io_ga.ga_len = 0;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200179 }
180
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200181 /* Put the remaining text into io_ga for later printing. */
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200182 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200183 {
184 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
Bram Moolenaar6b5ef062010-10-27 12:18:00 +0200185 io_ga.ga_len += (int)n;
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200186 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200187}
188
189/***************/
190
191static PyTypeObject OutputType;
192
193static OutputObject Output =
194{
195 PyObject_HEAD_INIT(&OutputType)
196 0,
197 0
198};
199
200static OutputObject Error =
201{
202 PyObject_HEAD_INIT(&OutputType)
203 0,
204 1
205};
206
207 static int
208PythonIO_Init_io(void)
209{
210 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
211 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
212
213 if (PyErr_Occurred())
214 {
215 EMSG(_("E264: Python: Error initialising I/O objects"));
216 return -1;
217 }
218
219 return 0;
220}
221
222
223static PyObject *VimError;
224
225/* Check to see whether a Vim error has been reported, or a keyboard
226 * interrupt has been detected.
227 */
228 static int
229VimErrorCheck(void)
230{
231 if (got_int)
232 {
233 PyErr_SetNone(PyExc_KeyboardInterrupt);
234 return 1;
235 }
236 else if (did_emsg && !PyErr_Occurred())
237 {
238 PyErr_SetNone(VimError);
239 return 1;
240 }
241
242 return 0;
243}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200244
245/* Vim module - Implementation
246 */
247 static PyObject *
248VimCommand(PyObject *self UNUSED, PyObject *args)
249{
250 char *cmd;
251 PyObject *result;
252
253 if (!PyArg_ParseTuple(args, "s", &cmd))
254 return NULL;
255
256 PyErr_Clear();
257
258 Py_BEGIN_ALLOW_THREADS
259 Python_Lock_Vim();
260
261 do_cmdline_cmd((char_u *)cmd);
262 update_screen(VALID);
263
264 Python_Release_Vim();
265 Py_END_ALLOW_THREADS
266
267 if (VimErrorCheck())
268 result = NULL;
269 else
270 result = Py_None;
271
272 Py_XINCREF(result);
273 return result;
274}
275
276#ifdef FEAT_EVAL
277/*
278 * Function to translate a typval_T into a PyObject; this will recursively
279 * translate lists/dictionaries into their Python equivalents.
280 *
281 * The depth parameter is to avoid infinite recursion, set it to 1 when
282 * you call VimToPython.
283 */
284 static PyObject *
285VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
286{
287 PyObject *result;
288 PyObject *newObj;
289 char ptrBuf[NUMBUFLEN];
290
291 /* Avoid infinite recursion */
292 if (depth > 100)
293 {
294 Py_INCREF(Py_None);
295 result = Py_None;
296 return result;
297 }
298
299 /* Check if we run into a recursive loop. The item must be in lookupDict
300 * then and we can use it again. */
301 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
302 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
303 {
304 sprintf(ptrBuf, PRINTF_DECIMAL_LONG_U,
305 our_tv->v_type == VAR_LIST ? (long_u)our_tv->vval.v_list
306 : (long_u)our_tv->vval.v_dict);
307 result = PyDict_GetItemString(lookupDict, ptrBuf);
308 if (result != NULL)
309 {
310 Py_INCREF(result);
311 return result;
312 }
313 }
314
315 if (our_tv->v_type == VAR_STRING)
316 {
317 result = Py_BuildValue("s", our_tv->vval.v_string);
318 }
319 else if (our_tv->v_type == VAR_NUMBER)
320 {
321 char buf[NUMBUFLEN];
322
323 /* For backwards compatibility numbers are stored as strings. */
324 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
325 result = Py_BuildValue("s", buf);
326 }
327# ifdef FEAT_FLOAT
328 else if (our_tv->v_type == VAR_FLOAT)
329 {
330 char buf[NUMBUFLEN];
331
332 sprintf(buf, "%f", our_tv->vval.v_float);
333 result = Py_BuildValue("s", buf);
334 }
335# endif
336 else if (our_tv->v_type == VAR_LIST)
337 {
338 list_T *list = our_tv->vval.v_list;
339 listitem_T *curr;
340
341 result = PyList_New(0);
342
343 if (list != NULL)
344 {
345 PyDict_SetItemString(lookupDict, ptrBuf, result);
346
347 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
348 {
349 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
350 PyList_Append(result, newObj);
351 Py_DECREF(newObj);
352 }
353 }
354 }
355 else if (our_tv->v_type == VAR_DICT)
356 {
357 result = PyDict_New();
358
359 if (our_tv->vval.v_dict != NULL)
360 {
361 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
362 long_u todo = ht->ht_used;
363 hashitem_T *hi;
364 dictitem_T *di;
365
366 PyDict_SetItemString(lookupDict, ptrBuf, result);
367
368 for (hi = ht->ht_array; todo > 0; ++hi)
369 {
370 if (!HASHITEM_EMPTY(hi))
371 {
372 --todo;
373
374 di = dict_lookup(hi);
375 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
376 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
377 Py_DECREF(newObj);
378 }
379 }
380 }
381 }
382 else
383 {
384 Py_INCREF(Py_None);
385 result = Py_None;
386 }
387
388 return result;
389}
390#endif
391
392 static PyObject *
Bram Moolenaar09092152010-08-08 16:38:42 +0200393VimEval(PyObject *self UNUSED, PyObject *args UNUSED)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200394{
395#ifdef FEAT_EVAL
396 char *expr;
397 typval_T *our_tv;
398 PyObject *result;
399 PyObject *lookup_dict;
400
401 if (!PyArg_ParseTuple(args, "s", &expr))
402 return NULL;
403
404 Py_BEGIN_ALLOW_THREADS
405 Python_Lock_Vim();
406 our_tv = eval_expr((char_u *)expr, NULL);
407
408 Python_Release_Vim();
409 Py_END_ALLOW_THREADS
410
411 if (our_tv == NULL)
412 {
413 PyErr_SetVim(_("invalid expression"));
414 return NULL;
415 }
416
417 /* Convert the Vim type into a Python type. Create a dictionary that's
418 * used to check for recursive loops. */
419 lookup_dict = PyDict_New();
420 result = VimToPython(our_tv, 1, lookup_dict);
421 Py_DECREF(lookup_dict);
422
423
424 Py_BEGIN_ALLOW_THREADS
425 Python_Lock_Vim();
426 free_tv(our_tv);
427 Python_Release_Vim();
428 Py_END_ALLOW_THREADS
429
430 return result;
431#else
432 PyErr_SetVim(_("expressions disabled at compile time"));
433 return NULL;
434#endif
435}
436
437/*
438 * Vim module - Definitions
439 */
440
441static struct PyMethodDef VimMethods[] = {
442 /* name, function, calling, documentation */
443 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
444 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
445 { NULL, NULL, 0, NULL }
446};
447
448typedef struct
449{
450 PyObject_HEAD
451 buf_T *buf;
452}
453BufferObject;
454
455#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
456
457/*
458 * Buffer list object - Implementation
459 */
460
461 static PyInt
462BufListLength(PyObject *self UNUSED)
463{
464 buf_T *b = firstbuf;
465 PyInt n = 0;
466
467 while (b)
468 {
469 ++n;
470 b = b->b_next;
471 }
472
473 return n;
474}
475
476 static PyObject *
477BufListItem(PyObject *self UNUSED, PyInt n)
478{
479 buf_T *b;
480
481 for (b = firstbuf; b; b = b->b_next, --n)
482 {
483 if (n == 0)
484 return BufferNew(b);
485 }
486
487 PyErr_SetString(PyExc_IndexError, _("no such buffer"));
488 return NULL;
489}
490
491typedef struct
492{
493 PyObject_HEAD
494 win_T *win;
495} WindowObject;
496
497#define INVALID_WINDOW_VALUE ((win_T *)(-1))
498
499 static int
500CheckWindow(WindowObject *this)
501{
502 if (this->win == INVALID_WINDOW_VALUE)
503 {
504 PyErr_SetVim(_("attempt to refer to deleted window"));
505 return -1;
506 }
507
508 return 0;
509}
510
511static int WindowSetattr(PyObject *, char *, PyObject *);
512static PyObject *WindowRepr(PyObject *);
513
514 static int
515WindowSetattr(PyObject *self, char *name, PyObject *val)
516{
517 WindowObject *this = (WindowObject *)(self);
518
519 if (CheckWindow(this))
520 return -1;
521
522 if (strcmp(name, "buffer") == 0)
523 {
524 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
525 return -1;
526 }
527 else if (strcmp(name, "cursor") == 0)
528 {
529 long lnum;
530 long col;
531 long len;
532
533 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
534 return -1;
535
536 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
537 {
538 PyErr_SetVim(_("cursor position outside buffer"));
539 return -1;
540 }
541
542 /* Check for keyboard interrupts */
543 if (VimErrorCheck())
544 return -1;
545
546 /* When column is out of range silently correct it. */
547 len = (long)STRLEN(ml_get_buf(this->win->w_buffer, lnum, FALSE));
548 if (col > len)
549 col = len;
550
551 this->win->w_cursor.lnum = lnum;
552 this->win->w_cursor.col = col;
553#ifdef FEAT_VIRTUALEDIT
554 this->win->w_cursor.coladd = 0;
555#endif
556 update_screen(VALID);
557
558 return 0;
559 }
560 else if (strcmp(name, "height") == 0)
561 {
562 int height;
563 win_T *savewin;
564
565 if (!PyArg_Parse(val, "i", &height))
566 return -1;
567
568#ifdef FEAT_GUI
569 need_mouse_correct = TRUE;
570#endif
571 savewin = curwin;
572 curwin = this->win;
573 win_setheight(height);
574 curwin = savewin;
575
576 /* Check for keyboard interrupts */
577 if (VimErrorCheck())
578 return -1;
579
580 return 0;
581 }
582#ifdef FEAT_VERTSPLIT
583 else if (strcmp(name, "width") == 0)
584 {
585 int width;
586 win_T *savewin;
587
588 if (!PyArg_Parse(val, "i", &width))
589 return -1;
590
591#ifdef FEAT_GUI
592 need_mouse_correct = TRUE;
593#endif
594 savewin = curwin;
595 curwin = this->win;
596 win_setwidth(width);
597 curwin = savewin;
598
599 /* Check for keyboard interrupts */
600 if (VimErrorCheck())
601 return -1;
602
603 return 0;
604 }
605#endif
606 else
607 {
608 PyErr_SetString(PyExc_AttributeError, name);
609 return -1;
610 }
611}
612
613 static PyObject *
614WindowRepr(PyObject *self)
615{
616 static char repr[100];
617 WindowObject *this = (WindowObject *)(self);
618
619 if (this->win == INVALID_WINDOW_VALUE)
620 {
621 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
622 return PyString_FromString(repr);
623 }
624 else
625 {
626 int i = 0;
627 win_T *w;
628
629 for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w))
630 ++i;
631
632 if (w == NULL)
633 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
634 (self));
635 else
636 vim_snprintf(repr, 100, _("<window %d>"), i);
637
638 return PyString_FromString(repr);
639 }
640}
641
642/*
643 * Window list object - Implementation
644 */
645 static PyInt
646WinListLength(PyObject *self UNUSED)
647{
648 win_T *w = firstwin;
649 PyInt n = 0;
650
651 while (w != NULL)
652 {
653 ++n;
654 w = W_NEXT(w);
655 }
656
657 return n;
658}
659
660 static PyObject *
661WinListItem(PyObject *self UNUSED, PyInt n)
662{
663 win_T *w;
664
665 for (w = firstwin; w != NULL; w = W_NEXT(w), --n)
666 if (n == 0)
667 return WindowNew(w);
668
669 PyErr_SetString(PyExc_IndexError, _("no such window"));
670 return NULL;
671}
672
673/* Convert a Python string into a Vim line.
674 *
675 * The result is in allocated memory. All internal nulls are replaced by
676 * newline characters. It is an error for the string to contain newline
677 * characters.
678 *
679 * On errors, the Python exception data is set, and NULL is returned.
680 */
681 static char *
682StringToLine(PyObject *obj)
683{
684 const char *str;
685 char *save;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200686 PyObject *bytes;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687 PyInt len;
688 PyInt i;
689 char *p;
690
691 if (obj == NULL || !PyString_Check(obj))
692 {
693 PyErr_BadArgument();
694 return NULL;
695 }
696
Bram Moolenaar19e60942011-06-19 00:27:51 +0200697 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
698 str = PyString_AsString(bytes);
699 len = PyString_Size(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700
701 /*
702 * Error checking: String must not contain newlines, as we
703 * are replacing a single line, and we must replace it with
704 * a single line.
705 * A trailing newline is removed, so that append(f.readlines()) works.
706 */
707 p = memchr(str, '\n', len);
708 if (p != NULL)
709 {
710 if (p == str + len - 1)
711 --len;
712 else
713 {
714 PyErr_SetVim(_("string cannot contain newlines"));
715 return NULL;
716 }
717 }
718
719 /* Create a copy of the string, with internal nulls replaced by
720 * newline characters, as is the vim convention.
721 */
722 save = (char *)alloc((unsigned)(len+1));
723 if (save == NULL)
724 {
725 PyErr_NoMemory();
726 return NULL;
727 }
728
729 for (i = 0; i < len; ++i)
730 {
731 if (str[i] == '\0')
732 save[i] = '\n';
733 else
734 save[i] = str[i];
735 }
736
737 save[i] = '\0';
Bram Moolenaar19e60942011-06-19 00:27:51 +0200738 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200739
740 return save;
741}
742
743/* Get a line from the specified buffer. The line number is
744 * in Vim format (1-based). The line is returned as a Python
745 * string object.
746 */
747 static PyObject *
748GetBufferLine(buf_T *buf, PyInt n)
749{
750 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
751}
752
753
754/* Get a list of lines from the specified buffer. The line numbers
755 * are in Vim format (1-based). The range is from lo up to, but not
756 * including, hi. The list is returned as a Python list of string objects.
757 */
758 static PyObject *
759GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
760{
761 PyInt i;
762 PyInt n = hi - lo;
763 PyObject *list = PyList_New(n);
764
765 if (list == NULL)
766 return NULL;
767
768 for (i = 0; i < n; ++i)
769 {
770 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
771
772 /* Error check - was the Python string creation OK? */
773 if (str == NULL)
774 {
775 Py_DECREF(list);
776 return NULL;
777 }
778
779 /* Set the list item */
780 if (PyList_SetItem(list, i, str))
781 {
782 Py_DECREF(str);
783 Py_DECREF(list);
784 return NULL;
785 }
786 }
787
788 /* The ownership of the Python list is passed to the caller (ie,
789 * the caller should Py_DECREF() the object when it is finished
790 * with it).
791 */
792
793 return list;
794}
795
796/*
797 * Check if deleting lines made the cursor position invalid.
798 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
799 * deleted).
800 */
801 static void
802py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
803{
804 if (curwin->w_cursor.lnum >= lo)
805 {
806 /* Adjust the cursor position if it's in/after the changed
807 * lines. */
808 if (curwin->w_cursor.lnum >= hi)
809 {
810 curwin->w_cursor.lnum += extra;
811 check_cursor_col();
812 }
813 else if (extra < 0)
814 {
815 curwin->w_cursor.lnum = lo;
816 check_cursor();
817 }
818 else
819 check_cursor_col();
820 changed_cline_bef_curs();
821 }
822 invalidate_botline();
823}
824
Bram Moolenaar19e60942011-06-19 00:27:51 +0200825/*
826 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827 * in Vim format (1-based). The replacement line is given as
828 * a Python string object. The object is checked for validity
829 * and correct format. Errors are returned as a value of FAIL.
830 * The return value is OK on success.
831 * If OK is returned and len_change is not NULL, *len_change
832 * is set to the change in the buffer length.
833 */
834 static int
835SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
836{
837 /* First of all, we check the thpe of the supplied Python object.
838 * There are three cases:
839 * 1. NULL, or None - this is a deletion.
840 * 2. A string - this is a replacement.
841 * 3. Anything else - this is an error.
842 */
843 if (line == Py_None || line == NULL)
844 {
845 buf_T *savebuf = curbuf;
846
847 PyErr_Clear();
848 curbuf = buf;
849
850 if (u_savedel((linenr_T)n, 1L) == FAIL)
851 PyErr_SetVim(_("cannot save undo information"));
852 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
853 PyErr_SetVim(_("cannot delete line"));
854 else
855 {
856 if (buf == curwin->w_buffer)
857 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
858 deleted_lines_mark((linenr_T)n, 1L);
859 }
860
861 curbuf = savebuf;
862
863 if (PyErr_Occurred() || VimErrorCheck())
864 return FAIL;
865
866 if (len_change)
867 *len_change = -1;
868
869 return OK;
870 }
871 else if (PyString_Check(line))
872 {
873 char *save = StringToLine(line);
874 buf_T *savebuf = curbuf;
875
876 if (save == NULL)
877 return FAIL;
878
879 /* We do not need to free "save" if ml_replace() consumes it. */
880 PyErr_Clear();
881 curbuf = buf;
882
883 if (u_savesub((linenr_T)n) == FAIL)
884 {
885 PyErr_SetVim(_("cannot save undo information"));
886 vim_free(save);
887 }
888 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
889 {
890 PyErr_SetVim(_("cannot replace line"));
891 vim_free(save);
892 }
893 else
894 changed_bytes((linenr_T)n, 0);
895
896 curbuf = savebuf;
897
898 /* Check that the cursor is not beyond the end of the line now. */
899 if (buf == curwin->w_buffer)
900 check_cursor_col();
901
902 if (PyErr_Occurred() || VimErrorCheck())
903 return FAIL;
904
905 if (len_change)
906 *len_change = 0;
907
908 return OK;
909 }
910 else
911 {
912 PyErr_BadArgument();
913 return FAIL;
914 }
915}
916
Bram Moolenaar19e60942011-06-19 00:27:51 +0200917/* Replace a range of lines in the specified buffer. The line numbers are in
918 * Vim format (1-based). The range is from lo up to, but not including, hi.
919 * The replacement lines are given as a Python list of string objects. The
920 * list is checked for validity and correct format. Errors are returned as a
921 * value of FAIL. The return value is OK on success.
922 * If OK is returned and len_change is not NULL, *len_change
923 * is set to the change in the buffer length.
924 */
925 static int
926SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
927{
928 /* First of all, we check the thpe of the supplied Python object.
929 * There are three cases:
930 * 1. NULL, or None - this is a deletion.
931 * 2. A list - this is a replacement.
932 * 3. Anything else - this is an error.
933 */
934 if (list == Py_None || list == NULL)
935 {
936 PyInt i;
937 PyInt n = (int)(hi - lo);
938 buf_T *savebuf = curbuf;
939
940 PyErr_Clear();
941 curbuf = buf;
942
943 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
944 PyErr_SetVim(_("cannot save undo information"));
945 else
946 {
947 for (i = 0; i < n; ++i)
948 {
949 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
950 {
951 PyErr_SetVim(_("cannot delete line"));
952 break;
953 }
954 }
955 if (buf == curwin->w_buffer)
956 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
957 deleted_lines_mark((linenr_T)lo, (long)i);
958 }
959
960 curbuf = savebuf;
961
962 if (PyErr_Occurred() || VimErrorCheck())
963 return FAIL;
964
965 if (len_change)
966 *len_change = -n;
967
968 return OK;
969 }
970 else if (PyList_Check(list))
971 {
972 PyInt i;
973 PyInt new_len = PyList_Size(list);
974 PyInt old_len = hi - lo;
975 PyInt extra = 0; /* lines added to text, can be negative */
976 char **array;
977 buf_T *savebuf;
978
979 if (new_len == 0) /* avoid allocating zero bytes */
980 array = NULL;
981 else
982 {
983 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
984 if (array == NULL)
985 {
986 PyErr_NoMemory();
987 return FAIL;
988 }
989 }
990
991 for (i = 0; i < new_len; ++i)
992 {
993 PyObject *line = PyList_GetItem(list, i);
994
995 array[i] = StringToLine(line);
996 if (array[i] == NULL)
997 {
998 while (i)
999 vim_free(array[--i]);
1000 vim_free(array);
1001 return FAIL;
1002 }
1003 }
1004
1005 savebuf = curbuf;
1006
1007 PyErr_Clear();
1008 curbuf = buf;
1009
1010 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
1011 PyErr_SetVim(_("cannot save undo information"));
1012
1013 /* If the size of the range is reducing (ie, new_len < old_len) we
1014 * need to delete some old_len. We do this at the start, by
1015 * repeatedly deleting line "lo".
1016 */
1017 if (!PyErr_Occurred())
1018 {
1019 for (i = 0; i < old_len - new_len; ++i)
1020 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1021 {
1022 PyErr_SetVim(_("cannot delete line"));
1023 break;
1024 }
1025 extra -= i;
1026 }
1027
1028 /* For as long as possible, replace the existing old_len with the
1029 * new old_len. This is a more efficient operation, as it requires
1030 * less memory allocation and freeing.
1031 */
1032 if (!PyErr_Occurred())
1033 {
1034 for (i = 0; i < old_len && i < new_len; ++i)
1035 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
1036 == FAIL)
1037 {
1038 PyErr_SetVim(_("cannot replace line"));
1039 break;
1040 }
1041 }
1042 else
1043 i = 0;
1044
1045 /* Now we may need to insert the remaining new old_len. If we do, we
1046 * must free the strings as we finish with them (we can't pass the
1047 * responsibility to vim in this case).
1048 */
1049 if (!PyErr_Occurred())
1050 {
1051 while (i < new_len)
1052 {
1053 if (ml_append((linenr_T)(lo + i - 1),
1054 (char_u *)array[i], 0, FALSE) == FAIL)
1055 {
1056 PyErr_SetVim(_("cannot insert line"));
1057 break;
1058 }
1059 vim_free(array[i]);
1060 ++i;
1061 ++extra;
1062 }
1063 }
1064
1065 /* Free any left-over old_len, as a result of an error */
1066 while (i < new_len)
1067 {
1068 vim_free(array[i]);
1069 ++i;
1070 }
1071
1072 /* Free the array of old_len. All of its contents have now
1073 * been dealt with (either freed, or the responsibility passed
1074 * to vim.
1075 */
1076 vim_free(array);
1077
1078 /* Adjust marks. Invalidate any which lie in the
1079 * changed range, and move any in the remainder of the buffer.
1080 */
1081 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
1082 (long)MAXLNUM, (long)extra);
1083 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
1084
1085 if (buf == curwin->w_buffer)
1086 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
1087
1088 curbuf = savebuf;
1089
1090 if (PyErr_Occurred() || VimErrorCheck())
1091 return FAIL;
1092
1093 if (len_change)
1094 *len_change = new_len - old_len;
1095
1096 return OK;
1097 }
1098 else
1099 {
1100 PyErr_BadArgument();
1101 return FAIL;
1102 }
1103}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001104
1105/* Insert a number of lines into the specified buffer after the specifed line.
1106 * The line number is in Vim format (1-based). The lines to be inserted are
1107 * given as a Python list of string objects or as a single string. The lines
1108 * to be added are checked for validity and correct format. Errors are
1109 * returned as a value of FAIL. The return value is OK on success.
1110 * If OK is returned and len_change is not NULL, *len_change
1111 * is set to the change in the buffer length.
1112 */
1113 static int
1114InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
1115{
1116 /* First of all, we check the type of the supplied Python object.
1117 * It must be a string or a list, or the call is in error.
1118 */
1119 if (PyString_Check(lines))
1120 {
1121 char *str = StringToLine(lines);
1122 buf_T *savebuf;
1123
1124 if (str == NULL)
1125 return FAIL;
1126
1127 savebuf = curbuf;
1128
1129 PyErr_Clear();
1130 curbuf = buf;
1131
1132 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
1133 PyErr_SetVim(_("cannot save undo information"));
1134 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
1135 PyErr_SetVim(_("cannot insert line"));
1136 else
1137 appended_lines_mark((linenr_T)n, 1L);
1138
1139 vim_free(str);
1140 curbuf = savebuf;
1141 update_screen(VALID);
1142
1143 if (PyErr_Occurred() || VimErrorCheck())
1144 return FAIL;
1145
1146 if (len_change)
1147 *len_change = 1;
1148
1149 return OK;
1150 }
1151 else if (PyList_Check(lines))
1152 {
1153 PyInt i;
1154 PyInt size = PyList_Size(lines);
1155 char **array;
1156 buf_T *savebuf;
1157
1158 array = (char **)alloc((unsigned)(size * sizeof(char *)));
1159 if (array == NULL)
1160 {
1161 PyErr_NoMemory();
1162 return FAIL;
1163 }
1164
1165 for (i = 0; i < size; ++i)
1166 {
1167 PyObject *line = PyList_GetItem(lines, i);
1168 array[i] = StringToLine(line);
1169
1170 if (array[i] == NULL)
1171 {
1172 while (i)
1173 vim_free(array[--i]);
1174 vim_free(array);
1175 return FAIL;
1176 }
1177 }
1178
1179 savebuf = curbuf;
1180
1181 PyErr_Clear();
1182 curbuf = buf;
1183
1184 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
1185 PyErr_SetVim(_("cannot save undo information"));
1186 else
1187 {
1188 for (i = 0; i < size; ++i)
1189 {
1190 if (ml_append((linenr_T)(n + i),
1191 (char_u *)array[i], 0, FALSE) == FAIL)
1192 {
1193 PyErr_SetVim(_("cannot insert line"));
1194
1195 /* Free the rest of the lines */
1196 while (i < size)
1197 vim_free(array[i++]);
1198
1199 break;
1200 }
1201 vim_free(array[i]);
1202 }
1203 if (i > 0)
1204 appended_lines_mark((linenr_T)n, (long)i);
1205 }
1206
1207 /* Free the array of lines. All of its contents have now
1208 * been freed.
1209 */
1210 vim_free(array);
1211
1212 curbuf = savebuf;
1213 update_screen(VALID);
1214
1215 if (PyErr_Occurred() || VimErrorCheck())
1216 return FAIL;
1217
1218 if (len_change)
1219 *len_change = size;
1220
1221 return OK;
1222 }
1223 else
1224 {
1225 PyErr_BadArgument();
1226 return FAIL;
1227 }
1228}
1229
1230/*
1231 * Common routines for buffers and line ranges
1232 * -------------------------------------------
1233 */
1234
1235 static int
1236CheckBuffer(BufferObject *this)
1237{
1238 if (this->buf == INVALID_BUFFER_VALUE)
1239 {
1240 PyErr_SetVim(_("attempt to refer to deleted buffer"));
1241 return -1;
1242 }
1243
1244 return 0;
1245}
1246
1247 static PyObject *
1248RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
1249{
1250 if (CheckBuffer(self))
1251 return NULL;
1252
1253 if (n < 0 || n > end - start)
1254 {
1255 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
1256 return NULL;
1257 }
1258
1259 return GetBufferLine(self->buf, n+start);
1260}
1261
1262 static PyObject *
1263RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
1264{
1265 PyInt size;
1266
1267 if (CheckBuffer(self))
1268 return NULL;
1269
1270 size = end - start + 1;
1271
1272 if (lo < 0)
1273 lo = 0;
1274 else if (lo > size)
1275 lo = size;
1276 if (hi < 0)
1277 hi = 0;
1278 if (hi < lo)
1279 hi = lo;
1280 else if (hi > size)
1281 hi = size;
1282
1283 return GetBufferLineList(self->buf, lo+start, hi+start);
1284}
1285
1286 static PyInt
1287RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
1288{
1289 PyInt len_change;
1290
1291 if (CheckBuffer(self))
1292 return -1;
1293
1294 if (n < 0 || n > end - start)
1295 {
1296 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
1297 return -1;
1298 }
1299
1300 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
1301 return -1;
1302
1303 if (new_end)
1304 *new_end = end + len_change;
1305
1306 return 0;
1307}
1308
Bram Moolenaar19e60942011-06-19 00:27:51 +02001309 static PyInt
1310RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
1311{
1312 PyInt size;
1313 PyInt len_change;
1314
1315 /* Self must be a valid buffer */
1316 if (CheckBuffer(self))
1317 return -1;
1318
1319 /* Sort out the slice range */
1320 size = end - start + 1;
1321
1322 if (lo < 0)
1323 lo = 0;
1324 else if (lo > size)
1325 lo = size;
1326 if (hi < 0)
1327 hi = 0;
1328 if (hi < lo)
1329 hi = lo;
1330 else if (hi > size)
1331 hi = size;
1332
1333 if (SetBufferLineList(self->buf, lo + start, hi + start,
1334 val, &len_change) == FAIL)
1335 return -1;
1336
1337 if (new_end)
1338 *new_end = end + len_change;
1339
1340 return 0;
1341}
1342
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001343
1344 static PyObject *
1345RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
1346{
1347 PyObject *lines;
1348 PyInt len_change;
1349 PyInt max;
1350 PyInt n;
1351
1352 if (CheckBuffer(self))
1353 return NULL;
1354
1355 max = n = end - start + 1;
1356
1357 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
1358 return NULL;
1359
1360 if (n < 0 || n > max)
1361 {
1362 PyErr_SetString(PyExc_ValueError, _("line number out of range"));
1363 return NULL;
1364 }
1365
1366 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
1367 return NULL;
1368
1369 if (new_end)
1370 *new_end = end + len_change;
1371
1372 Py_INCREF(Py_None);
1373 return Py_None;
1374}
1375
1376
1377/* Buffer object - Definitions
1378 */
1379
1380typedef struct
1381{
1382 PyObject_HEAD
1383 BufferObject *buf;
1384 PyInt start;
1385 PyInt end;
1386} RangeObject;
1387
1388 static PyObject *
1389RangeNew(buf_T *buf, PyInt start, PyInt end)
1390{
1391 BufferObject *bufr;
1392 RangeObject *self;
1393 self = PyObject_NEW(RangeObject, &RangeType);
1394 if (self == NULL)
1395 return NULL;
1396
1397 bufr = (BufferObject *)BufferNew(buf);
1398 if (bufr == NULL)
1399 {
1400 Py_DECREF(self);
1401 return NULL;
1402 }
1403 Py_INCREF(bufr);
1404
1405 self->buf = bufr;
1406 self->start = start;
1407 self->end = end;
1408
1409 return (PyObject *)(self);
1410}
1411
1412 static PyObject *
1413BufferAppend(PyObject *self, PyObject *args)
1414{
1415 return RBAppend((BufferObject *)(self), args, 1,
1416 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1417 NULL);
1418}
1419
1420 static PyObject *
1421BufferMark(PyObject *self, PyObject *args)
1422{
1423 pos_T *posp;
1424 char *pmark;
1425 char mark;
1426 buf_T *curbuf_save;
1427
1428 if (CheckBuffer((BufferObject *)(self)))
1429 return NULL;
1430
1431 if (!PyArg_ParseTuple(args, "s", &pmark))
1432 return NULL;
1433 mark = *pmark;
1434
1435 curbuf_save = curbuf;
1436 curbuf = ((BufferObject *)(self))->buf;
1437 posp = getmark(mark, FALSE);
1438 curbuf = curbuf_save;
1439
1440 if (posp == NULL)
1441 {
1442 PyErr_SetVim(_("invalid mark name"));
1443 return NULL;
1444 }
1445
1446 /* Ckeck for keyboard interrupt */
1447 if (VimErrorCheck())
1448 return NULL;
1449
1450 if (posp->lnum <= 0)
1451 {
1452 /* Or raise an error? */
1453 Py_INCREF(Py_None);
1454 return Py_None;
1455 }
1456
1457 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
1458}
1459
1460 static PyObject *
1461BufferRange(PyObject *self, PyObject *args)
1462{
1463 PyInt start;
1464 PyInt end;
1465
1466 if (CheckBuffer((BufferObject *)(self)))
1467 return NULL;
1468
1469 if (!PyArg_ParseTuple(args, "nn", &start, &end))
1470 return NULL;
1471
1472 return RangeNew(((BufferObject *)(self))->buf, start, end);
1473}
1474
1475static struct PyMethodDef BufferMethods[] = {
1476 /* name, function, calling, documentation */
1477 {"append", BufferAppend, 1, "Append data to Vim buffer" },
1478 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
1479 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" },
1480 { NULL, NULL, 0, NULL }
1481};
1482
1483 static PyObject *
1484RangeAppend(PyObject *self, PyObject *args)
1485{
1486 return RBAppend(((RangeObject *)(self))->buf, args,
1487 ((RangeObject *)(self))->start,
1488 ((RangeObject *)(self))->end,
1489 &((RangeObject *)(self))->end);
1490}
1491
1492 static PyInt
1493RangeLength(PyObject *self)
1494{
1495 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1496 if (CheckBuffer(((RangeObject *)(self))->buf))
1497 return -1; /* ??? */
1498
1499 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
1500}
1501
1502 static PyObject *
1503RangeItem(PyObject *self, PyInt n)
1504{
1505 return RBItem(((RangeObject *)(self))->buf, n,
1506 ((RangeObject *)(self))->start,
1507 ((RangeObject *)(self))->end);
1508}
1509
1510 static PyObject *
1511RangeRepr(PyObject *self)
1512{
1513 static char repr[100];
1514 RangeObject *this = (RangeObject *)(self);
1515
1516 if (this->buf->buf == INVALID_BUFFER_VALUE)
1517 {
1518 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
1519 (self));
1520 return PyString_FromString(repr);
1521 }
1522 else
1523 {
1524 char *name = (char *)this->buf->buf->b_fname;
1525 int len;
1526
1527 if (name == NULL)
1528 name = "";
1529 len = (int)strlen(name);
1530
1531 if (len > 45)
1532 name = name + (45 - len);
1533
1534 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
1535 len > 45 ? "..." : "", name,
1536 this->start, this->end);
1537
1538 return PyString_FromString(repr);
1539 }
1540}
1541
1542 static PyObject *
1543RangeSlice(PyObject *self, PyInt lo, PyInt hi)
1544{
1545 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
1546 ((RangeObject *)(self))->start,
1547 ((RangeObject *)(self))->end);
1548}
1549
1550/*
1551 * Line range object - Definitions
1552 */
1553
1554static struct PyMethodDef RangeMethods[] = {
1555 /* name, function, calling, documentation */
1556 {"append", RangeAppend, 1, "Append data to the Vim range" },
1557 { NULL, NULL, 0, NULL }
1558};
1559