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