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