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