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