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