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