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