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