1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
10 * Python extensions by Paul Moore, David Leonard, Roland Puntaier.
12 * Common code for if_python.c and if_python3.c.
16 # define ENC_OPT p_enc
18 # define ENC_OPT "latin1"
22 * obtain a lock on the Vim data structures
30 * release a lock on the Vim data structures
33 Python_Release_Vim(void)
37 /* Output object definition
40 static PyObject *OutputWrite(PyObject *, PyObject *);
41 static PyObject *OutputWritelines(PyObject *, PyObject *);
42 static PyObject *OutputFlush(PyObject *, PyObject *);
44 /* Function to write a line, points to either msg() or emsg(). */
45 typedef void (*writefn)(char_u *);
46 static void writer(writefn fn, char_u *str, PyInt n);
55 static struct PyMethodDef OutputMethods[] = {
56 /* name, function, calling, documentation */
57 {"write", OutputWrite, 1, ""},
58 {"writelines", OutputWritelines, 1, ""},
59 {"flush", OutputFlush, 1, ""},
60 { NULL, NULL, 0, NULL}
63 #define PyErr_SetVim(str) PyErr_SetString(VimError, str)
67 /* Output buffer management
71 OutputWrite(PyObject *self, PyObject *args)
75 int error = ((OutputObject *)(self))->error;
77 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
80 Py_BEGIN_ALLOW_THREADS
82 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
92 OutputWritelines(PyObject *self, PyObject *args)
97 int error = ((OutputObject *)(self))->error;
99 if (!PyArg_ParseTuple(args, "O", &list))
103 if (!PyList_Check(list)) {
104 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
109 n = PyList_Size(list);
111 for (i = 0; i < n; ++i)
113 PyObject *line = PyList_GetItem(list, i);
117 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len)) {
118 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
123 Py_BEGIN_ALLOW_THREADS
125 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
126 Python_Release_Vim();
137 OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED)
145 /* Buffer IO, we write one whole line at a time. */
146 static garray_T io_ga = {0, 0, 1, 80, NULL};
147 static writefn old_fn = NULL;
152 if (old_fn != NULL && io_ga.ga_len > 0)
154 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
155 old_fn((char_u *)io_ga.ga_data);
161 writer(writefn fn, char_u *str, PyInt n)
165 /* Flush when switching output function. */
170 /* Write each NL separated line. Text after the last NL is kept for
172 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
174 PyInt len = ptr - str;
176 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
179 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
180 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
181 fn((char_u *)io_ga.ga_data);
187 /* Put the remaining text into io_ga for later printing. */
188 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
190 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
191 io_ga.ga_len += (int)n;
197 static PyTypeObject OutputType;
199 static OutputObject Output =
201 PyObject_HEAD_INIT(&OutputType)
206 static OutputObject Error =
208 PyObject_HEAD_INIT(&OutputType)
214 PythonIO_Init_io(void)
216 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
217 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
219 if (PyErr_Occurred())
221 EMSG(_("E264: Python: Error initialising I/O objects"));
229 static PyObject *VimError;
231 /* Check to see whether a Vim error has been reported, or a keyboard
232 * interrupt has been detected.
239 PyErr_SetNone(PyExc_KeyboardInterrupt);
242 else if (did_emsg && !PyErr_Occurred())
244 PyErr_SetNone(VimError);
251 /* Vim module - Implementation
254 VimCommand(PyObject *self UNUSED, PyObject *args)
259 if (!PyArg_ParseTuple(args, "s", &cmd))
264 Py_BEGIN_ALLOW_THREADS
267 do_cmdline_cmd((char_u *)cmd);
268 update_screen(VALID);
270 Python_Release_Vim();
284 * Function to translate a typval_T into a PyObject; this will recursively
285 * translate lists/dictionaries into their Python equivalents.
287 * The depth parameter is to avoid infinite recursion, set it to 1 when
288 * you call VimToPython.
291 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
295 char ptrBuf[NUMBUFLEN];
297 /* Avoid infinite recursion */
305 /* Check if we run into a recursive loop. The item must be in lookupDict
306 * then and we can use it again. */
307 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
308 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
310 sprintf(ptrBuf, PRINTF_DECIMAL_LONG_U,
311 our_tv->v_type == VAR_LIST ? (long_u)our_tv->vval.v_list
312 : (long_u)our_tv->vval.v_dict);
313 result = PyDict_GetItemString(lookupDict, ptrBuf);
321 if (our_tv->v_type == VAR_STRING)
323 result = Py_BuildValue("s", our_tv->vval.v_string);
325 else if (our_tv->v_type == VAR_NUMBER)
329 /* For backwards compatibility numbers are stored as strings. */
330 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
331 result = Py_BuildValue("s", buf);
334 else if (our_tv->v_type == VAR_FLOAT)
338 sprintf(buf, "%f", our_tv->vval.v_float);
339 result = Py_BuildValue("s", buf);
342 else if (our_tv->v_type == VAR_LIST)
344 list_T *list = our_tv->vval.v_list;
347 result = PyList_New(0);
351 PyDict_SetItemString(lookupDict, ptrBuf, result);
353 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
355 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
356 PyList_Append(result, newObj);
361 else if (our_tv->v_type == VAR_DICT)
363 result = PyDict_New();
365 if (our_tv->vval.v_dict != NULL)
367 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
368 long_u todo = ht->ht_used;
372 PyDict_SetItemString(lookupDict, ptrBuf, result);
374 for (hi = ht->ht_array; todo > 0; ++hi)
376 if (!HASHITEM_EMPTY(hi))
380 di = dict_lookup(hi);
381 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
382 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
399 VimEval(PyObject *self UNUSED, PyObject *args UNUSED)
405 PyObject *lookup_dict;
407 if (!PyArg_ParseTuple(args, "s", &expr))
410 Py_BEGIN_ALLOW_THREADS
412 our_tv = eval_expr((char_u *)expr, NULL);
414 Python_Release_Vim();
419 PyErr_SetVim(_("invalid expression"));
423 /* Convert the Vim type into a Python type. Create a dictionary that's
424 * used to check for recursive loops. */
425 lookup_dict = PyDict_New();
426 result = VimToPython(our_tv, 1, lookup_dict);
427 Py_DECREF(lookup_dict);
430 Py_BEGIN_ALLOW_THREADS
433 Python_Release_Vim();
438 PyErr_SetVim(_("expressions disabled at compile time"));
444 * Vim module - Definitions
447 static struct PyMethodDef VimMethods[] = {
448 /* name, function, calling, documentation */
449 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
450 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
451 { NULL, NULL, 0, NULL }
461 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
464 * Buffer list object - Implementation
468 BufListLength(PyObject *self UNUSED)
483 BufListItem(PyObject *self UNUSED, PyInt n)
487 for (b = firstbuf; b; b = b->b_next, --n)
493 PyErr_SetString(PyExc_IndexError, _("no such buffer"));
503 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
506 CheckWindow(WindowObject *this)
508 if (this->win == INVALID_WINDOW_VALUE)
510 PyErr_SetVim(_("attempt to refer to deleted window"));
517 static int WindowSetattr(PyObject *, char *, PyObject *);
518 static PyObject *WindowRepr(PyObject *);
521 WindowSetattr(PyObject *self, char *name, PyObject *val)
523 WindowObject *this = (WindowObject *)(self);
525 if (CheckWindow(this))
528 if (strcmp(name, "buffer") == 0)
530 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
533 else if (strcmp(name, "cursor") == 0)
538 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
541 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
543 PyErr_SetVim(_("cursor position outside buffer"));
547 /* Check for keyboard interrupts */
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;
556 /* When column is out of range silently correct it. */
557 check_cursor_col_win(this->win);
559 update_screen(VALID);
562 else if (strcmp(name, "height") == 0)
567 if (!PyArg_Parse(val, "i", &height))
571 need_mouse_correct = TRUE;
575 win_setheight(height);
578 /* Check for keyboard interrupts */
584 #ifdef FEAT_VERTSPLIT
585 else if (strcmp(name, "width") == 0)
590 if (!PyArg_Parse(val, "i", &width))
594 need_mouse_correct = TRUE;
601 /* Check for keyboard interrupts */
610 PyErr_SetString(PyExc_AttributeError, name);
616 WindowRepr(PyObject *self)
618 static char repr[100];
619 WindowObject *this = (WindowObject *)(self);
621 if (this->win == INVALID_WINDOW_VALUE)
623 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
624 return PyString_FromString(repr);
631 for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w))
635 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
638 vim_snprintf(repr, 100, _("<window %d>"), i);
640 return PyString_FromString(repr);
645 * Window list object - Implementation
648 WinListLength(PyObject *self UNUSED)
663 WinListItem(PyObject *self UNUSED, PyInt n)
667 for (w = firstwin; w != NULL; w = W_NEXT(w), --n)
671 PyErr_SetString(PyExc_IndexError, _("no such window"));
675 /* Convert a Python string into a Vim line.
677 * The result is in allocated memory. All internal nulls are replaced by
678 * newline characters. It is an error for the string to contain newline
681 * On errors, the Python exception data is set, and NULL is returned.
684 StringToLine(PyObject *obj)
693 if (obj == NULL || !PyString_Check(obj))
699 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
700 str = PyString_AsString(bytes);
701 len = PyString_Size(bytes);
704 * Error checking: String must not contain newlines, as we
705 * are replacing a single line, and we must replace it with
707 * A trailing newline is removed, so that append(f.readlines()) works.
709 p = memchr(str, '\n', len);
712 if (p == str + len - 1)
716 PyErr_SetVim(_("string cannot contain newlines"));
721 /* Create a copy of the string, with internal nulls replaced by
722 * newline characters, as is the vim convention.
724 save = (char *)alloc((unsigned)(len+1));
731 for (i = 0; i < len; ++i)
740 PyString_FreeBytes(bytes); /* Python 2 does nothing here */
745 /* Get a line from the specified buffer. The line number is
746 * in Vim format (1-based). The line is returned as a Python
750 GetBufferLine(buf_T *buf, PyInt n)
752 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
756 /* Get a list of lines from the specified buffer. The line numbers
757 * are in Vim format (1-based). The range is from lo up to, but not
758 * including, hi. The list is returned as a Python list of string objects.
761 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
765 PyObject *list = PyList_New(n);
770 for (i = 0; i < n; ++i)
772 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
774 /* Error check - was the Python string creation OK? */
781 /* Set the list item */
782 if (PyList_SetItem(list, i, str))
790 /* The ownership of the Python list is passed to the caller (ie,
791 * the caller should Py_DECREF() the object when it is finished
799 * Check if deleting lines made the cursor position invalid.
800 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
804 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
806 if (curwin->w_cursor.lnum >= lo)
808 /* Adjust the cursor position if it's in/after the changed
810 if (curwin->w_cursor.lnum >= hi)
812 curwin->w_cursor.lnum += extra;
817 curwin->w_cursor.lnum = lo;
822 changed_cline_bef_curs();
824 invalidate_botline();
828 * Replace a line in the specified buffer. The line number is
829 * in Vim format (1-based). The replacement line is given as
830 * a Python string object. The object is checked for validity
831 * and correct format. Errors are returned as a value of FAIL.
832 * The return value is OK on success.
833 * If OK is returned and len_change is not NULL, *len_change
834 * is set to the change in the buffer length.
837 SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
839 /* First of all, we check the thpe of the supplied Python object.
840 * There are three cases:
841 * 1. NULL, or None - this is a deletion.
842 * 2. A string - this is a replacement.
843 * 3. Anything else - this is an error.
845 if (line == Py_None || line == NULL)
847 buf_T *savebuf = curbuf;
852 if (u_savedel((linenr_T)n, 1L) == FAIL)
853 PyErr_SetVim(_("cannot save undo information"));
854 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
855 PyErr_SetVim(_("cannot delete line"));
858 if (buf == curwin->w_buffer)
859 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
860 deleted_lines_mark((linenr_T)n, 1L);
865 if (PyErr_Occurred() || VimErrorCheck())
873 else if (PyString_Check(line))
875 char *save = StringToLine(line);
876 buf_T *savebuf = curbuf;
881 /* We do not need to free "save" if ml_replace() consumes it. */
885 if (u_savesub((linenr_T)n) == FAIL)
887 PyErr_SetVim(_("cannot save undo information"));
890 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
892 PyErr_SetVim(_("cannot replace line"));
896 changed_bytes((linenr_T)n, 0);
900 /* Check that the cursor is not beyond the end of the line now. */
901 if (buf == curwin->w_buffer)
904 if (PyErr_Occurred() || VimErrorCheck())
919 /* Replace a range of lines in the specified buffer. The line numbers are in
920 * Vim format (1-based). The range is from lo up to, but not including, hi.
921 * The replacement lines are given as a Python list of string objects. The
922 * list is checked for validity and correct format. Errors are returned as a
923 * value of FAIL. The return value is OK on success.
924 * If OK is returned and len_change is not NULL, *len_change
925 * is set to the change in the buffer length.
928 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
930 /* First of all, we check the thpe of the supplied Python object.
931 * There are three cases:
932 * 1. NULL, or None - this is a deletion.
933 * 2. A list - this is a replacement.
934 * 3. Anything else - this is an error.
936 if (list == Py_None || list == NULL)
939 PyInt n = (int)(hi - lo);
940 buf_T *savebuf = curbuf;
945 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
946 PyErr_SetVim(_("cannot save undo information"));
949 for (i = 0; i < n; ++i)
951 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
953 PyErr_SetVim(_("cannot delete line"));
957 if (buf == curwin->w_buffer)
958 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
959 deleted_lines_mark((linenr_T)lo, (long)i);
964 if (PyErr_Occurred() || VimErrorCheck())
972 else if (PyList_Check(list))
975 PyInt new_len = PyList_Size(list);
976 PyInt old_len = hi - lo;
977 PyInt extra = 0; /* lines added to text, can be negative */
981 if (new_len == 0) /* avoid allocating zero bytes */
985 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
993 for (i = 0; i < new_len; ++i)
995 PyObject *line = PyList_GetItem(list, i);
997 array[i] = StringToLine(line);
998 if (array[i] == NULL)
1001 vim_free(array[--i]);
1012 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
1013 PyErr_SetVim(_("cannot save undo information"));
1015 /* If the size of the range is reducing (ie, new_len < old_len) we
1016 * need to delete some old_len. We do this at the start, by
1017 * repeatedly deleting line "lo".
1019 if (!PyErr_Occurred())
1021 for (i = 0; i < old_len - new_len; ++i)
1022 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1024 PyErr_SetVim(_("cannot delete line"));
1030 /* For as long as possible, replace the existing old_len with the
1031 * new old_len. This is a more efficient operation, as it requires
1032 * less memory allocation and freeing.
1034 if (!PyErr_Occurred())
1036 for (i = 0; i < old_len && i < new_len; ++i)
1037 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
1040 PyErr_SetVim(_("cannot replace line"));
1047 /* Now we may need to insert the remaining new old_len. If we do, we
1048 * must free the strings as we finish with them (we can't pass the
1049 * responsibility to vim in this case).
1051 if (!PyErr_Occurred())
1055 if (ml_append((linenr_T)(lo + i - 1),
1056 (char_u *)array[i], 0, FALSE) == FAIL)
1058 PyErr_SetVim(_("cannot insert line"));
1067 /* Free any left-over old_len, as a result of an error */
1074 /* Free the array of old_len. All of its contents have now
1075 * been dealt with (either freed, or the responsibility passed
1080 /* Adjust marks. Invalidate any which lie in the
1081 * changed range, and move any in the remainder of the buffer.
1083 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
1084 (long)MAXLNUM, (long)extra);
1085 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
1087 if (buf == curwin->w_buffer)
1088 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
1092 if (PyErr_Occurred() || VimErrorCheck())
1096 *len_change = new_len - old_len;
1102 PyErr_BadArgument();
1107 /* Insert a number of lines into the specified buffer after the specifed line.
1108 * The line number is in Vim format (1-based). The lines to be inserted are
1109 * given as a Python list of string objects or as a single string. The lines
1110 * to be added are checked for validity and correct format. Errors are
1111 * returned as a value of FAIL. The return value is OK on success.
1112 * If OK is returned and len_change is not NULL, *len_change
1113 * is set to the change in the buffer length.
1116 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
1118 /* First of all, we check the type of the supplied Python object.
1119 * It must be a string or a list, or the call is in error.
1121 if (PyString_Check(lines))
1123 char *str = StringToLine(lines);
1134 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
1135 PyErr_SetVim(_("cannot save undo information"));
1136 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
1137 PyErr_SetVim(_("cannot insert line"));
1139 appended_lines_mark((linenr_T)n, 1L);
1143 update_screen(VALID);
1145 if (PyErr_Occurred() || VimErrorCheck())
1153 else if (PyList_Check(lines))
1156 PyInt size = PyList_Size(lines);
1160 array = (char **)alloc((unsigned)(size * sizeof(char *)));
1167 for (i = 0; i < size; ++i)
1169 PyObject *line = PyList_GetItem(lines, i);
1170 array[i] = StringToLine(line);
1172 if (array[i] == NULL)
1175 vim_free(array[--i]);
1186 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
1187 PyErr_SetVim(_("cannot save undo information"));
1190 for (i = 0; i < size; ++i)
1192 if (ml_append((linenr_T)(n + i),
1193 (char_u *)array[i], 0, FALSE) == FAIL)
1195 PyErr_SetVim(_("cannot insert line"));
1197 /* Free the rest of the lines */
1199 vim_free(array[i++]);
1206 appended_lines_mark((linenr_T)n, (long)i);
1209 /* Free the array of lines. All of its contents have now
1215 update_screen(VALID);
1217 if (PyErr_Occurred() || VimErrorCheck())
1227 PyErr_BadArgument();
1233 * Common routines for buffers and line ranges
1234 * -------------------------------------------
1238 CheckBuffer(BufferObject *this)
1240 if (this->buf == INVALID_BUFFER_VALUE)
1242 PyErr_SetVim(_("attempt to refer to deleted buffer"));
1250 RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
1252 if (CheckBuffer(self))
1255 if (n < 0 || n > end - start)
1257 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
1261 return GetBufferLine(self->buf, n+start);
1265 RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
1269 if (CheckBuffer(self))
1272 size = end - start + 1;
1285 return GetBufferLineList(self->buf, lo+start, hi+start);
1289 RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
1293 if (CheckBuffer(self))
1296 if (n < 0 || n > end - start)
1298 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
1302 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
1306 *new_end = end + len_change;
1312 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
1317 /* Self must be a valid buffer */
1318 if (CheckBuffer(self))
1321 /* Sort out the slice range */
1322 size = end - start + 1;
1335 if (SetBufferLineList(self->buf, lo + start, hi + start,
1336 val, &len_change) == FAIL)
1340 *new_end = end + len_change;
1347 RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
1354 if (CheckBuffer(self))
1357 max = n = end - start + 1;
1359 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
1362 if (n < 0 || n > max)
1364 PyErr_SetString(PyExc_ValueError, _("line number out of range"));
1368 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
1372 *new_end = end + len_change;
1379 /* Buffer object - Definitions
1391 RangeNew(buf_T *buf, PyInt start, PyInt end)
1395 self = PyObject_NEW(RangeObject, &RangeType);
1399 bufr = (BufferObject *)BufferNew(buf);
1408 self->start = start;
1411 return (PyObject *)(self);
1415 BufferAppend(PyObject *self, PyObject *args)
1417 return RBAppend((BufferObject *)(self), args, 1,
1418 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1423 BufferMark(PyObject *self, PyObject *args)
1430 if (CheckBuffer((BufferObject *)(self)))
1433 if (!PyArg_ParseTuple(args, "s", &pmark))
1437 curbuf_save = curbuf;
1438 curbuf = ((BufferObject *)(self))->buf;
1439 posp = getmark(mark, FALSE);
1440 curbuf = curbuf_save;
1444 PyErr_SetVim(_("invalid mark name"));
1448 /* Ckeck for keyboard interrupt */
1449 if (VimErrorCheck())
1452 if (posp->lnum <= 0)
1454 /* Or raise an error? */
1459 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
1463 BufferRange(PyObject *self, PyObject *args)
1468 if (CheckBuffer((BufferObject *)(self)))
1471 if (!PyArg_ParseTuple(args, "nn", &start, &end))
1474 return RangeNew(((BufferObject *)(self))->buf, start, end);
1477 static struct PyMethodDef BufferMethods[] = {
1478 /* name, function, calling, documentation */
1479 {"append", BufferAppend, 1, "Append data to Vim buffer" },
1480 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
1481 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" },
1482 { NULL, NULL, 0, NULL }
1486 RangeAppend(PyObject *self, PyObject *args)
1488 return RBAppend(((RangeObject *)(self))->buf, args,
1489 ((RangeObject *)(self))->start,
1490 ((RangeObject *)(self))->end,
1491 &((RangeObject *)(self))->end);
1495 RangeLength(PyObject *self)
1497 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1498 if (CheckBuffer(((RangeObject *)(self))->buf))
1499 return -1; /* ??? */
1501 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
1505 RangeItem(PyObject *self, PyInt n)
1507 return RBItem(((RangeObject *)(self))->buf, n,
1508 ((RangeObject *)(self))->start,
1509 ((RangeObject *)(self))->end);
1513 RangeRepr(PyObject *self)
1515 static char repr[100];
1516 RangeObject *this = (RangeObject *)(self);
1518 if (this->buf->buf == INVALID_BUFFER_VALUE)
1520 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
1522 return PyString_FromString(repr);
1526 char *name = (char *)this->buf->buf->b_fname;
1531 len = (int)strlen(name);
1534 name = name + (45 - len);
1536 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
1537 len > 45 ? "..." : "", name,
1538 this->start, this->end);
1540 return PyString_FromString(repr);
1545 RangeSlice(PyObject *self, PyInt lo, PyInt hi)
1547 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
1548 ((RangeObject *)(self))->start,
1549 ((RangeObject *)(self))->end);
1553 * Line range object - Definitions
1556 static struct PyMethodDef RangeMethods[] = {
1557 /* name, function, calling, documentation */
1558 {"append", RangeAppend, 1, "Append data to the Vim range" },
1559 { NULL, NULL, 0, NULL }