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