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