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