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