Mercurial > vim
annotate src/if_py_both.h @ 4387:358c10968c7f v7.3.942
updated for version 7.3.942
Problem: Python: SEGV in Buffer functions.
Solution: Call CheckBuffer() at the right time. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 12 May 2013 20:36:14 +0200 |
parents | da71e66dc1d4 |
children | 736b8e18a3bc |
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 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
534 * Buffer list object - Implementation |
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 |
4385 | 537 static PyTypeObject BufListType; |
538 static PySequenceMethods BufListAsSeq; | |
539 | |
4319 | 540 typedef struct |
541 { | |
542 PyObject_HEAD | |
543 } BufListObject; | |
544 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
545 static PyInt |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
546 BufListLength(PyObject *self UNUSED) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
547 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
548 buf_T *b = firstbuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
549 PyInt n = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
550 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
551 while (b) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
552 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
553 ++n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
554 b = b->b_next; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
555 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
556 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
557 return n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
558 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
559 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
560 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
561 BufListItem(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
|
562 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
563 buf_T *b; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
564 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
565 for (b = firstbuf; b; b = b->b_next, --n) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
566 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
567 if (n == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
568 return BufferNew(b); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
569 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
570 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
571 PyErr_SetString(PyExc_IndexError, _("no such buffer")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
572 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
573 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
574 |
3618 | 575 typedef struct pylinkedlist_S { |
576 struct pylinkedlist_S *pll_next; | |
577 struct pylinkedlist_S *pll_prev; | |
578 PyObject *pll_obj; | |
579 } pylinkedlist_T; | |
580 | |
581 static pylinkedlist_T *lastdict = NULL; | |
582 static pylinkedlist_T *lastlist = NULL; | |
583 | |
584 static void | |
585 pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last) | |
586 { | |
587 if (ref->pll_prev == NULL) | |
588 { | |
589 if (ref->pll_next == NULL) | |
590 { | |
591 *last = NULL; | |
592 return; | |
593 } | |
594 } | |
595 else | |
596 ref->pll_prev->pll_next = ref->pll_next; | |
597 | |
598 if (ref->pll_next == NULL) | |
599 *last = ref->pll_prev; | |
600 else | |
601 ref->pll_next->pll_prev = ref->pll_prev; | |
602 } | |
603 | |
604 static void | |
605 pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last) | |
606 { | |
607 if (*last == NULL) | |
608 ref->pll_prev = NULL; | |
609 else | |
610 { | |
611 (*last)->pll_next = ref; | |
612 ref->pll_prev = *last; | |
613 } | |
614 ref->pll_next = NULL; | |
615 ref->pll_obj = self; | |
616 *last = ref; | |
617 } | |
618 | |
619 static PyTypeObject DictionaryType; | |
620 | |
3792 | 621 #define DICTKEY_GET_NOTEMPTY(err) \ |
622 DICTKEY_GET(err) \ | |
623 if (*key == NUL) \ | |
624 { \ | |
625 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \ | |
626 return err; \ | |
627 } | |
628 | |
3618 | 629 typedef struct |
630 { | |
631 PyObject_HEAD | |
632 dict_T *dict; | |
633 pylinkedlist_T ref; | |
634 } DictionaryObject; | |
635 | |
636 static PyObject * | |
637 DictionaryNew(dict_T *dict) | |
638 { | |
639 DictionaryObject *self; | |
640 | |
641 self = PyObject_NEW(DictionaryObject, &DictionaryType); | |
642 if (self == NULL) | |
643 return NULL; | |
644 self->dict = dict; | |
645 ++dict->dv_refcount; | |
646 | |
647 pyll_add((PyObject *)(self), &self->ref, &lastdict); | |
648 | |
649 return (PyObject *)(self); | |
650 } | |
651 | |
4319 | 652 static void |
653 DictionaryDestructor(PyObject *self) | |
654 { | |
655 DictionaryObject *this = ((DictionaryObject *) (self)); | |
656 | |
657 pyll_remove(&this->ref, &lastdict); | |
658 dict_unref(this->dict); | |
659 | |
660 DESTRUCTOR_FINISH(self); | |
661 } | |
662 | |
3618 | 663 static int |
4319 | 664 DictionarySetattr(PyObject *self, char *name, PyObject *val) |
3828 | 665 { |
4319 | 666 DictionaryObject *this = (DictionaryObject *)(self); |
667 | |
3828 | 668 if (val == NULL) |
669 { | |
670 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); | |
671 return -1; | |
672 } | |
673 | |
674 if (strcmp(name, "locked") == 0) | |
675 { | |
4319 | 676 if (this->dict->dv_lock == VAR_FIXED) |
3828 | 677 { |
678 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary")); | |
679 return -1; | |
680 } | |
681 else | |
682 { | |
683 if (!PyBool_Check(val)) | |
684 { | |
685 PyErr_SetString(PyExc_TypeError, _("Only boolean objects are allowed")); | |
686 return -1; | |
687 } | |
688 | |
689 if (val == Py_True) | |
4319 | 690 this->dict->dv_lock = VAR_LOCKED; |
3828 | 691 else |
4319 | 692 this->dict->dv_lock = 0; |
3828 | 693 } |
694 return 0; | |
695 } | |
696 else | |
697 { | |
698 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute")); | |
699 return -1; | |
700 } | |
701 } | |
702 | |
703 static PyInt | |
3618 | 704 DictionaryLength(PyObject *self) |
705 { | |
706 return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used))); | |
707 } | |
708 | |
709 static PyObject * | |
710 DictionaryItem(PyObject *self, PyObject *keyObject) | |
711 { | |
712 char_u *key; | |
3792 | 713 dictitem_T *di; |
3618 | 714 DICTKEY_DECL |
715 | |
3792 | 716 DICTKEY_GET_NOTEMPTY(NULL) |
717 | |
718 di = dict_find(((DictionaryObject *) (self))->dict, key, -1); | |
719 | |
3824 | 720 DICTKEY_UNREF |
721 | |
3792 | 722 if (di == NULL) |
723 { | |
4315 | 724 PyErr_SetString(PyExc_KeyError, _("no such key in dictionary")); |
3792 | 725 return NULL; |
726 } | |
3618 | 727 |
3792 | 728 return ConvertToPyObject(&di->di_tv); |
3618 | 729 } |
730 | |
731 static PyInt | |
732 DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject) | |
733 { | |
734 char_u *key; | |
735 typval_T tv; | |
736 dict_T *d = ((DictionaryObject *)(self))->dict; | |
737 dictitem_T *di; | |
738 DICTKEY_DECL | |
739 | |
740 if (d->dv_lock) | |
741 { | |
742 PyErr_SetVim(_("dict is locked")); | |
743 return -1; | |
744 } | |
745 | |
3792 | 746 DICTKEY_GET_NOTEMPTY(-1) |
3618 | 747 |
748 di = dict_find(d, key, -1); | |
749 | |
750 if (valObject == NULL) | |
751 { | |
3636 | 752 hashitem_T *hi; |
753 | |
3618 | 754 if (di == NULL) |
755 { | |
3824 | 756 DICTKEY_UNREF |
3618 | 757 PyErr_SetString(PyExc_IndexError, _("no such key in dictionary")); |
758 return -1; | |
759 } | |
3636 | 760 hi = hash_find(&d->dv_hashtab, di->di_key); |
3618 | 761 hash_remove(&d->dv_hashtab, hi); |
762 dictitem_free(di); | |
763 return 0; | |
764 } | |
765 | |
766 if (ConvertFromPyObject(valObject, &tv) == -1) | |
767 return -1; | |
768 | |
769 if (di == NULL) | |
770 { | |
771 di = dictitem_alloc(key); | |
772 if (di == NULL) | |
773 { | |
774 PyErr_NoMemory(); | |
775 return -1; | |
776 } | |
777 di->di_tv.v_lock = 0; | |
778 | |
779 if (dict_add(d, di) == FAIL) | |
780 { | |
3824 | 781 DICTKEY_UNREF |
3618 | 782 vim_free(di); |
783 PyErr_SetVim(_("failed to add key to dictionary")); | |
784 return -1; | |
785 } | |
786 } | |
787 else | |
788 clear_tv(&di->di_tv); | |
789 | |
790 DICTKEY_UNREF | |
791 | |
792 copy_tv(&tv, &di->di_tv); | |
793 return 0; | |
794 } | |
795 | |
796 static PyObject * | |
4133 | 797 DictionaryListKeys(PyObject *self UNUSED) |
3618 | 798 { |
799 dict_T *dict = ((DictionaryObject *)(self))->dict; | |
800 long_u todo = dict->dv_hashtab.ht_used; | |
801 Py_ssize_t i = 0; | |
802 PyObject *r; | |
803 hashitem_T *hi; | |
804 | |
805 r = PyList_New(todo); | |
806 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) | |
807 { | |
808 if (!HASHITEM_EMPTY(hi)) | |
809 { | |
810 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key))); | |
811 --todo; | |
812 ++i; | |
813 } | |
814 } | |
815 return r; | |
816 } | |
817 | |
4385 | 818 static PyMappingMethods DictionaryAsMapping = { |
819 (lenfunc) DictionaryLength, | |
820 (binaryfunc) DictionaryItem, | |
821 (objobjargproc) DictionaryAssItem, | |
822 }; | |
823 | |
3618 | 824 static struct PyMethodDef DictionaryMethods[] = { |
825 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""}, | |
826 { NULL, NULL, 0, NULL } | |
827 }; | |
828 | |
829 static PyTypeObject ListType; | |
4319 | 830 static PySequenceMethods ListAsSeq; |
831 static PyMappingMethods ListAsMapping; | |
3618 | 832 |
833 typedef struct | |
834 { | |
835 PyObject_HEAD | |
836 list_T *list; | |
837 pylinkedlist_T ref; | |
838 } ListObject; | |
839 | |
840 static PyObject * | |
841 ListNew(list_T *list) | |
842 { | |
843 ListObject *self; | |
844 | |
845 self = PyObject_NEW(ListObject, &ListType); | |
846 if (self == NULL) | |
847 return NULL; | |
848 self->list = list; | |
849 ++list->lv_refcount; | |
850 | |
851 pyll_add((PyObject *)(self), &self->ref, &lastlist); | |
852 | |
853 return (PyObject *)(self); | |
854 } | |
855 | |
4319 | 856 static void |
857 ListDestructor(PyObject *self) | |
858 { | |
859 ListObject *this = (ListObject *)(self); | |
860 | |
861 pyll_remove(&this->ref, &lastlist); | |
862 list_unref(this->list); | |
863 | |
864 DESTRUCTOR_FINISH(self); | |
865 } | |
866 | |
3618 | 867 static int |
868 list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict) | |
869 { | |
870 Py_ssize_t i; | |
871 Py_ssize_t lsize = PySequence_Size(obj); | |
872 PyObject *litem; | |
873 listitem_T *li; | |
874 | |
875 for(i=0; i<lsize; i++) | |
876 { | |
877 li = listitem_alloc(); | |
878 if (li == NULL) | |
879 { | |
880 PyErr_NoMemory(); | |
881 return -1; | |
882 } | |
883 li->li_tv.v_lock = 0; | |
884 | |
885 litem = PySequence_GetItem(obj, i); | |
886 if (litem == NULL) | |
887 return -1; | |
888 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1) | |
889 return -1; | |
890 | |
891 list_append(l, li); | |
892 } | |
893 return 0; | |
894 } | |
895 | |
896 static PyInt | |
897 ListLength(PyObject *self) | |
898 { | |
899 return ((PyInt) (((ListObject *) (self))->list->lv_len)); | |
900 } | |
901 | |
902 static PyObject * | |
903 ListItem(PyObject *self, Py_ssize_t index) | |
904 { | |
905 listitem_T *li; | |
906 | |
907 if (index>=ListLength(self)) | |
908 { | |
909 PyErr_SetString(PyExc_IndexError, "list index out of range"); | |
910 return NULL; | |
911 } | |
912 li = list_find(((ListObject *) (self))->list, (long) index); | |
913 if (li == NULL) | |
914 { | |
915 PyErr_SetVim(_("internal error: failed to get vim list item")); | |
916 return NULL; | |
917 } | |
918 return ConvertToPyObject(&li->li_tv); | |
919 } | |
920 | |
921 #define PROC_RANGE \ | |
922 if (last < 0) {\ | |
923 if (last < -size) \ | |
924 last = 0; \ | |
925 else \ | |
926 last += size; \ | |
927 } \ | |
928 if (first < 0) \ | |
929 first = 0; \ | |
930 if (first > size) \ | |
931 first = size; \ | |
932 if (last > size) \ | |
933 last = size; | |
934 | |
935 static PyObject * | |
936 ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last) | |
937 { | |
938 PyInt i; | |
939 PyInt size = ListLength(self); | |
940 PyInt n; | |
941 PyObject *list; | |
942 int reversed = 0; | |
943 | |
944 PROC_RANGE | |
945 if (first >= last) | |
946 first = last; | |
947 | |
948 n = last-first; | |
949 list = PyList_New(n); | |
950 if (list == NULL) | |
951 return NULL; | |
952 | |
953 for (i = 0; i < n; ++i) | |
954 { | |
4260 | 955 PyObject *item = ListItem(self, first + i); |
3618 | 956 if (item == NULL) |
957 { | |
958 Py_DECREF(list); | |
959 return NULL; | |
960 } | |
961 | |
962 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item))) | |
963 { | |
964 Py_DECREF(item); | |
965 Py_DECREF(list); | |
966 return NULL; | |
967 } | |
968 } | |
969 | |
970 return list; | |
971 } | |
972 | |
973 static int | |
974 ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj) | |
975 { | |
976 typval_T tv; | |
977 list_T *l = ((ListObject *) (self))->list; | |
978 listitem_T *li; | |
979 Py_ssize_t length = ListLength(self); | |
980 | |
981 if (l->lv_lock) | |
982 { | |
983 PyErr_SetVim(_("list is locked")); | |
984 return -1; | |
985 } | |
986 if (index>length || (index==length && obj==NULL)) | |
987 { | |
988 PyErr_SetString(PyExc_IndexError, "list index out of range"); | |
989 return -1; | |
990 } | |
991 | |
992 if (obj == NULL) | |
993 { | |
994 li = list_find(l, (long) index); | |
995 list_remove(l, li, li); | |
996 clear_tv(&li->li_tv); | |
997 vim_free(li); | |
998 return 0; | |
999 } | |
1000 | |
1001 if (ConvertFromPyObject(obj, &tv) == -1) | |
1002 return -1; | |
1003 | |
1004 if (index == length) | |
1005 { | |
1006 if (list_append_tv(l, &tv) == FAIL) | |
1007 { | |
1008 PyErr_SetVim(_("Failed to add item to list")); | |
1009 return -1; | |
1010 } | |
1011 } | |
1012 else | |
1013 { | |
1014 li = list_find(l, (long) index); | |
1015 clear_tv(&li->li_tv); | |
1016 copy_tv(&tv, &li->li_tv); | |
1017 } | |
1018 return 0; | |
1019 } | |
1020 | |
1021 static int | |
1022 ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj) | |
1023 { | |
1024 PyInt size = ListLength(self); | |
1025 Py_ssize_t i; | |
1026 Py_ssize_t lsize; | |
1027 PyObject *litem; | |
1028 listitem_T *li; | |
1029 listitem_T *next; | |
1030 typval_T v; | |
1031 list_T *l = ((ListObject *) (self))->list; | |
1032 | |
1033 if (l->lv_lock) | |
1034 { | |
1035 PyErr_SetVim(_("list is locked")); | |
1036 return -1; | |
1037 } | |
1038 | |
1039 PROC_RANGE | |
1040 | |
1041 if (first == size) | |
1042 li = NULL; | |
1043 else | |
1044 { | |
1045 li = list_find(l, (long) first); | |
1046 if (li == NULL) | |
1047 { | |
1048 PyErr_SetVim(_("internal error: no vim list item")); | |
1049 return -1; | |
1050 } | |
1051 if (last > first) | |
1052 { | |
1053 i = last - first; | |
1054 while (i-- && li != NULL) | |
1055 { | |
1056 next = li->li_next; | |
1057 listitem_remove(l, li); | |
1058 li = next; | |
1059 } | |
1060 } | |
1061 } | |
1062 | |
1063 if (obj == NULL) | |
1064 return 0; | |
1065 | |
1066 if (!PyList_Check(obj)) | |
1067 { | |
1068 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice")); | |
1069 return -1; | |
1070 } | |
1071 | |
1072 lsize = PyList_Size(obj); | |
1073 | |
1074 for(i=0; i<lsize; i++) | |
1075 { | |
1076 litem = PyList_GetItem(obj, i); | |
1077 if (litem == NULL) | |
1078 return -1; | |
1079 if (ConvertFromPyObject(litem, &v) == -1) | |
1080 return -1; | |
1081 if (list_insert_tv(l, &v, li) == FAIL) | |
1082 { | |
1083 PyErr_SetVim(_("internal error: failed to add item to list")); | |
1084 return -1; | |
1085 } | |
1086 } | |
1087 return 0; | |
1088 } | |
1089 | |
1090 static PyObject * | |
1091 ListConcatInPlace(PyObject *self, PyObject *obj) | |
1092 { | |
1093 list_T *l = ((ListObject *) (self))->list; | |
1094 PyObject *lookup_dict; | |
1095 | |
1096 if (l->lv_lock) | |
1097 { | |
1098 PyErr_SetVim(_("list is locked")); | |
1099 return NULL; | |
1100 } | |
1101 | |
1102 if (!PySequence_Check(obj)) | |
1103 { | |
1104 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists")); | |
1105 return NULL; | |
1106 } | |
1107 | |
1108 lookup_dict = PyDict_New(); | |
1109 if (list_py_concat(l, obj, lookup_dict) == -1) | |
1110 { | |
1111 Py_DECREF(lookup_dict); | |
1112 return NULL; | |
1113 } | |
1114 Py_DECREF(lookup_dict); | |
1115 | |
1116 Py_INCREF(self); | |
1117 return self; | |
1118 } | |
1119 | |
3828 | 1120 static int |
4319 | 1121 ListSetattr(PyObject *self, char *name, PyObject *val) |
3828 | 1122 { |
4319 | 1123 ListObject *this = (ListObject *)(self); |
1124 | |
3828 | 1125 if (val == NULL) |
1126 { | |
1127 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); | |
1128 return -1; | |
1129 } | |
1130 | |
1131 if (strcmp(name, "locked") == 0) | |
1132 { | |
4319 | 1133 if (this->list->lv_lock == VAR_FIXED) |
3828 | 1134 { |
1135 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed list")); | |
1136 return -1; | |
1137 } | |
1138 else | |
1139 { | |
1140 if (!PyBool_Check(val)) | |
1141 { | |
1142 PyErr_SetString(PyExc_TypeError, _("Only boolean objects are allowed")); | |
1143 return -1; | |
1144 } | |
1145 | |
1146 if (val == Py_True) | |
4319 | 1147 this->list->lv_lock = VAR_LOCKED; |
3828 | 1148 else |
4319 | 1149 this->list->lv_lock = 0; |
3828 | 1150 } |
1151 return 0; | |
1152 } | |
1153 else | |
1154 { | |
1155 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute")); | |
1156 return -1; | |
1157 } | |
1158 } | |
1159 | |
3618 | 1160 static struct PyMethodDef ListMethods[] = { |
1161 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""}, | |
1162 { NULL, NULL, 0, NULL } | |
1163 }; | |
1164 | |
1165 typedef struct | |
1166 { | |
1167 PyObject_HEAD | |
1168 char_u *name; | |
1169 } FunctionObject; | |
1170 | |
1171 static PyTypeObject FunctionType; | |
1172 | |
1173 static PyObject * | |
1174 FunctionNew(char_u *name) | |
1175 { | |
1176 FunctionObject *self; | |
1177 | |
1178 self = PyObject_NEW(FunctionObject, &FunctionType); | |
1179 if (self == NULL) | |
1180 return NULL; | |
1181 self->name = PyMem_New(char_u, STRLEN(name) + 1); | |
1182 if (self->name == NULL) | |
1183 { | |
1184 PyErr_NoMemory(); | |
1185 return NULL; | |
1186 } | |
1187 STRCPY(self->name, name); | |
1188 func_ref(name); | |
1189 return (PyObject *)(self); | |
1190 } | |
1191 | |
4319 | 1192 static void |
1193 FunctionDestructor(PyObject *self) | |
1194 { | |
1195 FunctionObject *this = (FunctionObject *) (self); | |
1196 | |
1197 func_unref(this->name); | |
1198 PyMem_Del(this->name); | |
1199 | |
1200 DESTRUCTOR_FINISH(self); | |
1201 } | |
1202 | |
3618 | 1203 static PyObject * |
1204 FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs) | |
1205 { | |
1206 FunctionObject *this = (FunctionObject *)(self); | |
1207 char_u *name = this->name; | |
1208 typval_T args; | |
1209 typval_T selfdicttv; | |
1210 typval_T rettv; | |
1211 dict_T *selfdict = NULL; | |
1212 PyObject *selfdictObject; | |
1213 PyObject *result; | |
1214 int error; | |
1215 | |
1216 if (ConvertFromPyObject(argsObject, &args) == -1) | |
1217 return NULL; | |
1218 | |
1219 if (kwargs != NULL) | |
1220 { | |
1221 selfdictObject = PyDict_GetItemString(kwargs, "self"); | |
1222 if (selfdictObject != NULL) | |
1223 { | |
3703 | 1224 if (!PyMapping_Check(selfdictObject)) |
3618 | 1225 { |
3703 | 1226 PyErr_SetString(PyExc_TypeError, |
1227 _("'self' argument must be a dictionary")); | |
3618 | 1228 clear_tv(&args); |
1229 return NULL; | |
1230 } | |
1231 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1) | |
1232 return NULL; | |
1233 selfdict = selfdicttv.vval.v_dict; | |
1234 } | |
1235 } | |
1236 | |
1237 error = func_call(name, &args, selfdict, &rettv); | |
1238 if (error != OK) | |
1239 { | |
1240 result = NULL; | |
1241 PyErr_SetVim(_("failed to run function")); | |
1242 } | |
1243 else | |
1244 result = ConvertToPyObject(&rettv); | |
1245 | |
1246 /* FIXME Check what should really be cleared. */ | |
1247 clear_tv(&args); | |
1248 clear_tv(&rettv); | |
1249 /* | |
1250 * if (selfdict!=NULL) | |
1251 * clear_tv(selfdicttv); | |
1252 */ | |
1253 | |
1254 return result; | |
1255 } | |
1256 | |
1257 static struct PyMethodDef FunctionMethods[] = { | |
1258 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""}, | |
1259 { NULL, NULL, 0, NULL } | |
1260 }; | |
1261 | |
4350 | 1262 /* |
1263 * Options object | |
1264 */ | |
1265 | |
1266 static PyTypeObject OptionsType; | |
1267 | |
1268 typedef int (*checkfun)(void *); | |
1269 | |
1270 typedef struct | |
1271 { | |
1272 PyObject_HEAD | |
1273 int opt_type; | |
1274 void *from; | |
1275 checkfun Check; | |
1276 PyObject *fromObj; | |
1277 } OptionsObject; | |
1278 | |
1279 static PyObject * | |
1280 OptionsItem(OptionsObject *this, PyObject *keyObject) | |
1281 { | |
1282 char_u *key; | |
1283 int flags; | |
1284 long numval; | |
1285 char_u *stringval; | |
4359 | 1286 DICTKEY_DECL |
4350 | 1287 |
1288 if (this->Check(this->from)) | |
1289 return NULL; | |
1290 | |
1291 DICTKEY_GET_NOTEMPTY(NULL) | |
1292 | |
1293 flags = get_option_value_strict(key, &numval, &stringval, | |
1294 this->opt_type, this->from); | |
1295 | |
1296 DICTKEY_UNREF | |
1297 | |
1298 if (flags == 0) | |
1299 { | |
1300 PyErr_SetString(PyExc_KeyError, "Option does not exist in given scope"); | |
1301 return NULL; | |
1302 } | |
1303 | |
1304 if (flags & SOPT_UNSET) | |
1305 { | |
1306 Py_INCREF(Py_None); | |
1307 return Py_None; | |
1308 } | |
1309 else if (flags & SOPT_BOOL) | |
1310 { | |
1311 PyObject *r; | |
1312 r = numval ? Py_True : Py_False; | |
1313 Py_INCREF(r); | |
1314 return r; | |
1315 } | |
1316 else if (flags & SOPT_NUM) | |
1317 return PyInt_FromLong(numval); | |
1318 else if (flags & SOPT_STRING) | |
1319 { | |
1320 if (stringval) | |
1321 return PyBytes_FromString((char *) stringval); | |
1322 else | |
1323 { | |
1324 PyErr_SetString(PyExc_ValueError, "Unable to get option value"); | |
1325 return NULL; | |
1326 } | |
1327 } | |
1328 else | |
1329 { | |
1330 PyErr_SetVim("Internal error: unknown option type. Should not happen"); | |
1331 return NULL; | |
1332 } | |
1333 } | |
1334 | |
1335 static int | |
1336 set_option_value_for(key, numval, stringval, opt_flags, opt_type, from) | |
1337 char_u *key; | |
1338 int numval; | |
1339 char_u *stringval; | |
1340 int opt_flags; | |
1341 int opt_type; | |
1342 void *from; | |
1343 { | |
1344 win_T *save_curwin; | |
1345 tabpage_T *save_curtab; | |
1346 aco_save_T aco; | |
1347 int r = 0; | |
1348 | |
1349 switch (opt_type) | |
1350 { | |
1351 case SREQ_WIN: | |
1352 if (switch_win(&save_curwin, &save_curtab, (win_T *) from, curtab) | |
1353 == FAIL) | |
1354 { | |
1355 PyErr_SetVim("Problem while switching windows."); | |
1356 return -1; | |
1357 } | |
1358 set_option_value(key, numval, stringval, opt_flags); | |
1359 restore_win(save_curwin, save_curtab); | |
1360 break; | |
1361 case SREQ_BUF: | |
1362 aucmd_prepbuf(&aco, (buf_T *) from); | |
1363 set_option_value(key, numval, stringval, opt_flags); | |
1364 aucmd_restbuf(&aco); | |
1365 break; | |
1366 case SREQ_GLOBAL: | |
1367 set_option_value(key, numval, stringval, opt_flags); | |
1368 break; | |
1369 } | |
1370 return r; | |
1371 } | |
1372 | |
1373 static int | |
1374 OptionsAssItem(OptionsObject *this, PyObject *keyObject, PyObject *valObject) | |
1375 { | |
1376 char_u *key; | |
1377 int flags; | |
1378 int opt_flags; | |
1379 int r = 0; | |
4359 | 1380 DICTKEY_DECL |
4350 | 1381 |
1382 if (this->Check(this->from)) | |
1383 return -1; | |
1384 | |
1385 DICTKEY_GET_NOTEMPTY(-1) | |
1386 | |
1387 flags = get_option_value_strict(key, NULL, NULL, | |
1388 this->opt_type, this->from); | |
1389 | |
1390 DICTKEY_UNREF | |
1391 | |
1392 if (flags == 0) | |
1393 { | |
1394 PyErr_SetString(PyExc_KeyError, "Option does not exist in given scope"); | |
1395 return -1; | |
1396 } | |
1397 | |
1398 if (valObject == NULL) | |
1399 { | |
1400 if (this->opt_type == SREQ_GLOBAL) | |
1401 { | |
1402 PyErr_SetString(PyExc_ValueError, "Unable to unset global option"); | |
1403 return -1; | |
1404 } | |
1405 else if (!(flags & SOPT_GLOBAL)) | |
1406 { | |
1407 PyErr_SetString(PyExc_ValueError, "Unable to unset option without " | |
1408 "global value"); | |
1409 return -1; | |
1410 } | |
1411 else | |
1412 { | |
1413 unset_global_local_option(key, this->from); | |
1414 return 0; | |
1415 } | |
1416 } | |
1417 | |
1418 opt_flags = (this->opt_type ? OPT_LOCAL : OPT_GLOBAL); | |
1419 | |
1420 if (flags & SOPT_BOOL) | |
1421 { | |
1422 if (!PyBool_Check(valObject)) | |
1423 { | |
1424 PyErr_SetString(PyExc_ValueError, "Object must be boolean"); | |
1425 return -1; | |
1426 } | |
1427 | |
1428 r = set_option_value_for(key, (valObject == Py_True), NULL, opt_flags, | |
1429 this->opt_type, this->from); | |
1430 } | |
1431 else if (flags & SOPT_NUM) | |
1432 { | |
1433 int val; | |
1434 | |
1435 #if PY_MAJOR_VERSION < 3 | |
1436 if (PyInt_Check(valObject)) | |
1437 val = PyInt_AsLong(valObject); | |
1438 else | |
1439 #endif | |
1440 if (PyLong_Check(valObject)) | |
1441 val = PyLong_AsLong(valObject); | |
1442 else | |
1443 { | |
1444 PyErr_SetString(PyExc_ValueError, "Object must be integer"); | |
1445 return -1; | |
1446 } | |
1447 | |
1448 r = set_option_value_for(key, val, NULL, opt_flags, | |
1449 this->opt_type, this->from); | |
1450 } | |
1451 else | |
1452 { | |
1453 char_u *val; | |
1454 if (PyBytes_Check(valObject)) | |
1455 { | |
1456 | |
1457 if (PyString_AsStringAndSize(valObject, (char **) &val, NULL) == -1) | |
1458 return -1; | |
1459 if (val == NULL) | |
1460 return -1; | |
1461 | |
1462 val = vim_strsave(val); | |
1463 } | |
1464 else if (PyUnicode_Check(valObject)) | |
1465 { | |
1466 PyObject *bytes; | |
1467 | |
1468 bytes = PyUnicode_AsEncodedString(valObject, (char *)ENC_OPT, NULL); | |
1469 if (bytes == NULL) | |
1470 return -1; | |
1471 | |
1472 if(PyString_AsStringAndSize(bytes, (char **) &val, NULL) == -1) | |
1473 return -1; | |
1474 if (val == NULL) | |
1475 return -1; | |
1476 | |
1477 val = vim_strsave(val); | |
1478 Py_XDECREF(bytes); | |
1479 } | |
1480 else | |
1481 { | |
1482 PyErr_SetString(PyExc_ValueError, "Object must be string"); | |
1483 return -1; | |
1484 } | |
1485 | |
1486 r = set_option_value_for(key, 0, val, opt_flags, | |
1487 this->opt_type, this->from); | |
1488 vim_free(val); | |
1489 } | |
1490 | |
1491 return r; | |
1492 } | |
1493 | |
1494 static int | |
1495 dummy_check(void *arg UNUSED) | |
1496 { | |
1497 return 0; | |
1498 } | |
1499 | |
1500 static PyObject * | |
1501 OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj) | |
1502 { | |
1503 OptionsObject *self; | |
1504 | |
1505 self = PyObject_NEW(OptionsObject, &OptionsType); | |
1506 if (self == NULL) | |
1507 return NULL; | |
1508 | |
1509 self->opt_type = opt_type; | |
1510 self->from = from; | |
1511 self->Check = Check; | |
1512 self->fromObj = fromObj; | |
1513 if (fromObj) | |
1514 Py_INCREF(fromObj); | |
1515 | |
1516 return (PyObject *)(self); | |
1517 } | |
1518 | |
1519 static void | |
1520 OptionsDestructor(PyObject *self) | |
1521 { | |
1522 if (((OptionsObject *)(self))->fromObj) | |
1523 Py_DECREF(((OptionsObject *)(self))->fromObj); | |
1524 DESTRUCTOR_FINISH(self); | |
1525 } | |
1526 | |
1527 static PyMappingMethods OptionsAsMapping = { | |
1528 (lenfunc) NULL, | |
1529 (binaryfunc) OptionsItem, | |
1530 (objobjargproc) OptionsAssItem, | |
1531 }; | |
1532 | |
4385 | 1533 /* Window object |
1534 */ | |
1535 | |
1536 typedef struct | |
1537 { | |
1538 PyObject_HEAD | |
1539 win_T *win; | |
1540 } WindowObject; | |
1541 | |
1542 static int WindowSetattr(PyObject *, char *, PyObject *); | |
1543 static PyObject *WindowRepr(PyObject *); | |
1544 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
|
1545 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1546 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1547 CheckWindow(WindowObject *this) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1548 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1549 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
|
1550 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1551 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
|
1552 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1553 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1554 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1555 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1556 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1557 |
4319 | 1558 static PyObject * |
4377 | 1559 WindowNew(win_T *win) |
1560 { | |
1561 /* We need to handle deletion of windows underneath us. | |
1562 * If we add a "w_python*_ref" field to the win_T structure, | |
1563 * then we can get at it in win_free() in vim. We then | |
1564 * need to create only ONE Python object per window - if | |
1565 * we try to create a second, just INCREF the existing one | |
1566 * and return it. The (single) Python object referring to | |
1567 * the window is stored in "w_python*_ref". | |
1568 * On a win_free() we set the Python object's win_T* field | |
1569 * to an invalid value. We trap all uses of a window | |
1570 * object, and reject them if the win_T* field is invalid. | |
1571 * | |
4385 | 1572 * Python2 and Python3 get different fields and different objects: |
4377 | 1573 * w_python_ref and w_python3_ref fields respectively. |
1574 */ | |
1575 | |
1576 WindowObject *self; | |
1577 | |
1578 if (WIN_PYTHON_REF(win)) | |
1579 { | |
1580 self = WIN_PYTHON_REF(win); | |
1581 Py_INCREF(self); | |
1582 } | |
1583 else | |
1584 { | |
1585 self = PyObject_NEW(WindowObject, &WindowType); | |
1586 if (self == NULL) | |
1587 return NULL; | |
1588 self->win = win; | |
1589 WIN_PYTHON_REF(win) = self; | |
1590 } | |
1591 | |
1592 return (PyObject *)(self); | |
1593 } | |
1594 | |
4385 | 1595 static void |
1596 WindowDestructor(PyObject *self) | |
1597 { | |
1598 WindowObject *this = (WindowObject *)(self); | |
1599 | |
1600 if (this->win && this->win != INVALID_WINDOW_VALUE) | |
1601 WIN_PYTHON_REF(this->win) = NULL; | |
1602 | |
1603 DESTRUCTOR_FINISH(self); | |
1604 } | |
1605 | |
4377 | 1606 static PyObject * |
4319 | 1607 WindowAttr(WindowObject *this, char *name) |
1608 { | |
1609 if (strcmp(name, "buffer") == 0) | |
1610 return (PyObject *)BufferNew(this->win->w_buffer); | |
1611 else if (strcmp(name, "cursor") == 0) | |
1612 { | |
1613 pos_T *pos = &this->win->w_cursor; | |
1614 | |
1615 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); | |
1616 } | |
1617 else if (strcmp(name, "height") == 0) | |
4381 | 1618 return PyLong_FromLong((long)(this->win->w_height)); |
4383 | 1619 #ifdef FEAT_WINDOWS |
1620 else if (strcmp(name, "row") == 0) | |
1621 return PyLong_FromLong((long)(this->win->w_winrow)); | |
1622 #endif | |
4319 | 1623 #ifdef FEAT_VERTSPLIT |
1624 else if (strcmp(name, "width") == 0) | |
4381 | 1625 return PyLong_FromLong((long)(W_WIDTH(this->win))); |
4383 | 1626 else if (strcmp(name, "col") == 0) |
1627 return PyLong_FromLong((long)(W_WINCOL(this->win))); | |
4319 | 1628 #endif |
4323 | 1629 else if (strcmp(name, "vars") == 0) |
1630 return DictionaryNew(this->win->w_vars); | |
4350 | 1631 else if (strcmp(name, "options") == 0) |
1632 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow, | |
1633 (PyObject *) this); | |
4379 | 1634 else if (strcmp(name, "number") == 0) |
1635 return PyLong_FromLong((long) get_win_number(this->win)); | |
4319 | 1636 else if (strcmp(name,"__members__") == 0) |
4383 | 1637 return Py_BuildValue("[ssssssss]", "buffer", "cursor", "height", "vars", |
1638 "options", "number", "row", "col"); | |
4319 | 1639 else |
1640 return NULL; | |
1641 } | |
1642 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1643 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1644 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
|
1645 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1646 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
|
1647 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1648 if (CheckWindow(this)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1649 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1650 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1651 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
|
1652 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1653 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
|
1654 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1655 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1656 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
|
1657 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1658 long lnum; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1659 long col; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1660 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1661 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
|
1662 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1663 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1664 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
|
1665 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1666 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
|
1667 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1668 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1669 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1670 /* 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
|
1671 if (VimErrorCheck()) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1672 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1673 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1674 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
|
1675 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
|
1676 #ifdef FEAT_VIRTUALEDIT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1677 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
|
1678 #endif |
2933 | 1679 /* When column is out of range silently correct it. */ |
1680 check_cursor_col_win(this->win); | |
1681 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1682 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1683 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1684 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1685 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
|
1686 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1687 int height; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1688 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1689 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1690 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
|
1691 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1692 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1693 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1694 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
|
1695 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1696 savewin = curwin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1697 curwin = this->win; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1698 win_setheight(height); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1699 curwin = savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1700 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1701 /* 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
|
1702 if (VimErrorCheck()) |
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 return 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 #ifdef FEAT_VERTSPLIT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1708 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
|
1709 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1710 int width; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1711 win_T *savewin; |
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 (!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
|
1714 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1715 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1716 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1717 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
|
1718 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1719 savewin = curwin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1720 curwin = this->win; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1721 win_setwidth(width); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1722 curwin = savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1723 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1724 /* 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
|
1725 if (VimErrorCheck()) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1726 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1727 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1728 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1729 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1730 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1731 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1732 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1733 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
|
1734 return -1; |
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 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1737 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1738 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1739 WindowRepr(PyObject *self) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1740 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1741 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
|
1742 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
|
1743 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1744 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
|
1745 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1746 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
|
1747 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
|
1748 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1749 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1750 { |
4379 | 1751 int w = get_win_number(this->win); |
1752 | |
1753 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
|
1754 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
|
1755 (self)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1756 else |
4379 | 1757 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
|
1758 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1759 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
|
1760 } |
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 |
4385 | 1763 static struct PyMethodDef WindowMethods[] = { |
1764 /* name, function, calling, documentation */ | |
1765 { NULL, NULL, 0, NULL } | |
1766 }; | |
1767 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1768 /* |
4385 | 1769 * 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
|
1770 */ |
4319 | 1771 |
4385 | 1772 static PyTypeObject WinListType; |
1773 static PySequenceMethods WinListAsSeq; | |
1774 | |
4319 | 1775 typedef struct |
1776 { | |
1777 PyObject_HEAD | |
1778 } WinListObject; | |
1779 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1780 static PyInt |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1781 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
|
1782 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1783 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
|
1784 PyInt n = 0; |
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 while (w != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1787 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1788 ++n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1789 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
|
1790 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1791 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1792 return n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1793 } |
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 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1796 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
|
1797 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1798 win_T *w; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1799 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1800 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
|
1801 if (n == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1802 return WindowNew(w); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1803 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1804 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
|
1805 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1806 } |
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 /* 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
|
1809 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1810 * 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
|
1811 * 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
|
1812 * characters. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1813 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1814 * 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
|
1815 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1816 static char * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1817 StringToLine(PyObject *obj) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1818 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1819 const char *str; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1820 char *save; |
2894 | 1821 PyObject *bytes; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1822 PyInt len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1823 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1824 char *p; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1825 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1826 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
|
1827 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1828 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1829 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1830 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1831 |
2894 | 1832 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
1833 str = PyString_AsString(bytes); | |
1834 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
|
1835 |
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 * 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
|
1838 * 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
|
1839 * a single line. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1840 * 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
|
1841 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1842 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
|
1843 if (p != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1844 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1845 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
|
1846 --len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1847 else |
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 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
|
1850 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1851 } |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1854 /* 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
|
1855 * 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
|
1856 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1857 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
|
1858 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1859 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1860 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1861 return NULL; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1864 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
|
1865 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1866 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
|
1867 save[i] = '\n'; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1868 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1869 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
|
1870 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1871 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1872 save[i] = '\0'; |
2894 | 1873 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
|
1874 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1875 return save; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1878 /* 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
|
1879 * 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
|
1880 * string object. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1881 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1882 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1883 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
|
1884 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1885 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
|
1886 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1887 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1888 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1889 /* 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
|
1890 * 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
|
1891 * 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
|
1892 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1893 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1894 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
|
1895 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1896 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1897 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
|
1898 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
|
1899 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1900 if (list == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1901 return NULL; |
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 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
|
1904 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1905 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
|
1906 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1907 /* 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
|
1908 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1909 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1910 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1911 return NULL; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1914 /* 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
|
1915 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
|
1916 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1917 Py_DECREF(str); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1918 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1919 return NULL; |
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 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1922 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1923 /* 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
|
1924 * 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
|
1925 * with it). |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1928 return list; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1929 } |
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 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1932 * 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
|
1933 * 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
|
1934 * deleted). |
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 static void |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1937 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
|
1938 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1939 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
|
1940 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1941 /* 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
|
1942 * lines. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1943 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
|
1944 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1945 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
|
1946 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1947 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1948 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
|
1949 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1950 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
|
1951 check_cursor(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1952 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1953 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1954 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1955 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
|
1956 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1957 invalidate_botline(); |
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 |
2894 | 1960 /* |
1961 * 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
|
1962 * 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
|
1963 * 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
|
1964 * 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
|
1965 * 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
|
1966 * 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
|
1967 * 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
|
1968 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1969 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1970 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
|
1971 { |
4352 | 1972 /* 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
|
1973 * 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
|
1974 * 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
|
1975 * 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
|
1976 * 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
|
1977 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1978 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
|
1979 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1980 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
|
1981 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1982 PyErr_Clear(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1983 curbuf = buf; |
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 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
|
1986 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
|
1987 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
|
1988 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
|
1989 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1990 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1991 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
|
1992 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
|
1993 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
|
1994 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1995 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1996 curbuf = savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1997 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1998 if (PyErr_Occurred() || VimErrorCheck()) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1999 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2000 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2001 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2002 *len_change = -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2003 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2004 return OK; |
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 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
|
2007 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2008 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
|
2009 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
|
2010 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2011 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2012 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2013 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2014 /* 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
|
2015 PyErr_Clear(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2016 curbuf = buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2017 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2018 if (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
|
2019 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2020 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
|
2021 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2022 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2023 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
|
2024 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2025 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
|
2026 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2027 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2028 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2029 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
|
2030 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2031 curbuf = savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2032 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2033 /* 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
|
2034 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
|
2035 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2036 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2037 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
|
2038 return FAIL; |
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 (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2041 *len_change = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2042 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2043 return OK; |
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 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2046 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2047 PyErr_BadArgument(); |
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 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2051 |
2894 | 2052 /* Replace a range of lines in the specified buffer. The line numbers are in |
2053 * Vim format (1-based). The range is from lo up to, but not including, hi. | |
2054 * The replacement lines are given as a Python list of string objects. The | |
2055 * list is checked for validity and correct format. Errors are returned as a | |
2056 * value of FAIL. The return value is OK on success. | |
2057 * If OK is returned and len_change is not NULL, *len_change | |
2058 * is set to the change in the buffer length. | |
2059 */ | |
2060 static int | |
2061 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) | |
2062 { | |
4352 | 2063 /* First of all, we check the type of the supplied Python object. |
2894 | 2064 * There are three cases: |
2065 * 1. NULL, or None - this is a deletion. | |
2066 * 2. A list - this is a replacement. | |
2067 * 3. Anything else - this is an error. | |
2068 */ | |
2069 if (list == Py_None || list == NULL) | |
2070 { | |
2071 PyInt i; | |
2072 PyInt n = (int)(hi - lo); | |
2073 buf_T *savebuf = curbuf; | |
2074 | |
2075 PyErr_Clear(); | |
2076 curbuf = buf; | |
2077 | |
2078 if (u_savedel((linenr_T)lo, (long)n) == FAIL) | |
2079 PyErr_SetVim(_("cannot save undo information")); | |
2080 else | |
2081 { | |
2082 for (i = 0; i < n; ++i) | |
2083 { | |
2084 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2085 { | |
2086 PyErr_SetVim(_("cannot delete line")); | |
2087 break; | |
2088 } | |
2089 } | |
2090 if (buf == curwin->w_buffer) | |
2091 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); | |
2092 deleted_lines_mark((linenr_T)lo, (long)i); | |
2093 } | |
2094 | |
2095 curbuf = savebuf; | |
2096 | |
2097 if (PyErr_Occurred() || VimErrorCheck()) | |
2098 return FAIL; | |
2099 | |
2100 if (len_change) | |
2101 *len_change = -n; | |
2102 | |
2103 return OK; | |
2104 } | |
2105 else if (PyList_Check(list)) | |
2106 { | |
2107 PyInt i; | |
2108 PyInt new_len = PyList_Size(list); | |
2109 PyInt old_len = hi - lo; | |
2110 PyInt extra = 0; /* lines added to text, can be negative */ | |
2111 char **array; | |
2112 buf_T *savebuf; | |
2113 | |
2114 if (new_len == 0) /* avoid allocating zero bytes */ | |
2115 array = NULL; | |
2116 else | |
2117 { | |
2118 array = (char **)alloc((unsigned)(new_len * sizeof(char *))); | |
2119 if (array == NULL) | |
2120 { | |
2121 PyErr_NoMemory(); | |
2122 return FAIL; | |
2123 } | |
2124 } | |
2125 | |
2126 for (i = 0; i < new_len; ++i) | |
2127 { | |
2128 PyObject *line = PyList_GetItem(list, i); | |
2129 | |
2130 array[i] = StringToLine(line); | |
2131 if (array[i] == NULL) | |
2132 { | |
2133 while (i) | |
2134 vim_free(array[--i]); | |
2135 vim_free(array); | |
2136 return FAIL; | |
2137 } | |
2138 } | |
2139 | |
2140 savebuf = curbuf; | |
2141 | |
2142 PyErr_Clear(); | |
2143 curbuf = buf; | |
2144 | |
2145 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) | |
2146 PyErr_SetVim(_("cannot save undo information")); | |
2147 | |
2148 /* If the size of the range is reducing (ie, new_len < old_len) we | |
2149 * need to delete some old_len. We do this at the start, by | |
2150 * repeatedly deleting line "lo". | |
2151 */ | |
2152 if (!PyErr_Occurred()) | |
2153 { | |
2154 for (i = 0; i < old_len - new_len; ++i) | |
2155 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2156 { | |
2157 PyErr_SetVim(_("cannot delete line")); | |
2158 break; | |
2159 } | |
2160 extra -= i; | |
2161 } | |
2162 | |
2163 /* For as long as possible, replace the existing old_len with the | |
2164 * new old_len. This is a more efficient operation, as it requires | |
2165 * less memory allocation and freeing. | |
2166 */ | |
2167 if (!PyErr_Occurred()) | |
2168 { | |
2169 for (i = 0; i < old_len && i < new_len; ++i) | |
2170 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) | |
2171 == FAIL) | |
2172 { | |
2173 PyErr_SetVim(_("cannot replace line")); | |
2174 break; | |
2175 } | |
2176 } | |
2177 else | |
2178 i = 0; | |
2179 | |
2180 /* Now we may need to insert the remaining new old_len. If we do, we | |
2181 * must free the strings as we finish with them (we can't pass the | |
2182 * responsibility to vim in this case). | |
2183 */ | |
2184 if (!PyErr_Occurred()) | |
2185 { | |
2186 while (i < new_len) | |
2187 { | |
2188 if (ml_append((linenr_T)(lo + i - 1), | |
2189 (char_u *)array[i], 0, FALSE) == FAIL) | |
2190 { | |
2191 PyErr_SetVim(_("cannot insert line")); | |
2192 break; | |
2193 } | |
2194 vim_free(array[i]); | |
2195 ++i; | |
2196 ++extra; | |
2197 } | |
2198 } | |
2199 | |
2200 /* Free any left-over old_len, as a result of an error */ | |
2201 while (i < new_len) | |
2202 { | |
2203 vim_free(array[i]); | |
2204 ++i; | |
2205 } | |
2206 | |
2207 /* Free the array of old_len. All of its contents have now | |
2208 * been dealt with (either freed, or the responsibility passed | |
2209 * to vim. | |
2210 */ | |
2211 vim_free(array); | |
2212 | |
2213 /* Adjust marks. Invalidate any which lie in the | |
2214 * changed range, and move any in the remainder of the buffer. | |
2215 */ | |
2216 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), | |
2217 (long)MAXLNUM, (long)extra); | |
2218 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); | |
2219 | |
2220 if (buf == curwin->w_buffer) | |
2221 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); | |
2222 | |
2223 curbuf = savebuf; | |
2224 | |
2225 if (PyErr_Occurred() || VimErrorCheck()) | |
2226 return FAIL; | |
2227 | |
2228 if (len_change) | |
2229 *len_change = new_len - old_len; | |
2230 | |
2231 return OK; | |
2232 } | |
2233 else | |
2234 { | |
2235 PyErr_BadArgument(); | |
2236 return FAIL; | |
2237 } | |
2238 } | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2239 |
4352 | 2240 /* 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
|
2241 * 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
|
2242 * 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
|
2243 * 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
|
2244 * 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
|
2245 * 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
|
2246 * 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
|
2247 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2248 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2249 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
|
2250 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2251 /* 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
|
2252 * 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
|
2253 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2254 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
|
2255 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2256 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
|
2257 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2258 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2259 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2260 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2261 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2262 savebuf = curbuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2263 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2264 PyErr_Clear(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2265 curbuf = buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2266 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2267 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
|
2268 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
|
2269 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
|
2270 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
|
2271 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2272 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
|
2273 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2274 vim_free(str); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2275 curbuf = savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2276 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2277 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2278 if (PyErr_Occurred() || VimErrorCheck()) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2279 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2280 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2281 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2282 *len_change = 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2283 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2284 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2285 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2286 else if (PyList_Check(lines)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2287 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2288 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2289 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
|
2290 char **array; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2291 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2292 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2293 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
|
2294 if (array == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2295 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2296 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2297 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2298 } |
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 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
|
2301 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2302 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
|
2303 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
|
2304 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2305 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
|
2306 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2307 while (i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2308 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
|
2309 vim_free(array); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2310 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2311 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2312 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2313 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2314 savebuf = curbuf; |
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 PyErr_Clear(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2317 curbuf = buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2318 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2319 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
|
2320 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
|
2321 else |
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 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
|
2324 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2325 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
|
2326 (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
|
2327 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2328 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
|
2329 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2330 /* 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
|
2331 while (i < size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2332 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
|
2333 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2334 break; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2335 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2336 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
|
2337 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2338 if (i > 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2339 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
|
2340 } |
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 /* 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
|
2343 * been freed. |
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 vim_free(array); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2346 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2347 curbuf = savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2348 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2349 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2350 if (PyErr_Occurred() || VimErrorCheck()) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2351 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2352 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2353 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2354 *len_change = size; |
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 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2357 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2358 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2359 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2360 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2361 return FAIL; |
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 } |
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 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2366 * 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
|
2367 * ------------------------------------------- |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2368 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2369 |
4385 | 2370 typedef struct |
2371 { | |
2372 PyObject_HEAD | |
2373 buf_T *buf; | |
2374 } BufferObject; | |
2375 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2376 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2377 CheckBuffer(BufferObject *this) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2378 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2379 if (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
|
2380 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2381 PyErr_SetVim(_("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
|
2382 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2383 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2384 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2385 return 0; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2388 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2389 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
|
2390 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2391 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2392 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2393 |
4387 | 2394 if (end == -1) |
2395 end = self->buf->b_ml.ml_line_count; | |
2396 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2397 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
|
2398 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2399 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
|
2400 return NULL; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2403 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
|
2404 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2405 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2406 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2407 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
|
2408 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2409 PyInt size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2410 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2411 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2412 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2413 |
4387 | 2414 if (end == -1) |
2415 end = self->buf->b_ml.ml_line_count; | |
2416 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2417 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
|
2418 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2419 if (lo < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2420 lo = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2421 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
|
2422 lo = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2423 if (hi < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2424 hi = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2425 if (hi < lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2426 hi = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2427 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
|
2428 hi = size; |
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 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
|
2431 } |
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 static PyInt |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2434 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
|
2435 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2436 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2437 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2438 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2439 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2440 |
4387 | 2441 if (end == -1) |
2442 end = self->buf->b_ml.ml_line_count; | |
2443 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2444 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
|
2445 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2446 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
|
2447 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2448 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2449 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2450 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
|
2451 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2452 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2453 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2454 *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
|
2455 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2456 return 0; |
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 |
2894 | 2459 static PyInt |
2460 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) | |
2461 { | |
2462 PyInt size; | |
2463 PyInt len_change; | |
2464 | |
2465 /* Self must be a valid buffer */ | |
2466 if (CheckBuffer(self)) | |
2467 return -1; | |
2468 | |
4387 | 2469 if (end == -1) |
2470 end = self->buf->b_ml.ml_line_count; | |
2471 | |
2894 | 2472 /* Sort out the slice range */ |
2473 size = end - start + 1; | |
2474 | |
2475 if (lo < 0) | |
2476 lo = 0; | |
2477 else if (lo > size) | |
2478 lo = size; | |
2479 if (hi < 0) | |
2480 hi = 0; | |
2481 if (hi < lo) | |
2482 hi = lo; | |
2483 else if (hi > size) | |
2484 hi = size; | |
2485 | |
2486 if (SetBufferLineList(self->buf, lo + start, hi + start, | |
2487 val, &len_change) == FAIL) | |
2488 return -1; | |
2489 | |
2490 if (new_end) | |
2491 *new_end = end + len_change; | |
2492 | |
2493 return 0; | |
2494 } | |
2495 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2496 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2497 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2498 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
|
2499 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2500 PyObject *lines; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2501 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2502 PyInt max; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2503 PyInt n; |
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 (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2506 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2507 |
4387 | 2508 if (end == -1) |
2509 end = self->buf->b_ml.ml_line_count; | |
2510 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2511 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
|
2512 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2513 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
|
2514 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2515 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2516 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
|
2517 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2518 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
|
2519 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2520 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2521 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2522 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
|
2523 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2524 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2525 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2526 *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
|
2527 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2528 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
|
2529 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2530 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2531 |
4385 | 2532 /* Range object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2533 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2534 |
4319 | 2535 static PyTypeObject RangeType; |
4385 | 2536 static PySequenceMethods RangeAsSeq; |
2537 static PyMappingMethods RangeAsMapping; | |
4319 | 2538 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2539 typedef struct |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2540 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2541 PyObject_HEAD |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2542 BufferObject *buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2543 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2544 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2545 } RangeObject; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2546 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2547 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2548 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
|
2549 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2550 BufferObject *bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2551 RangeObject *self; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2552 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
|
2553 if (self == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2554 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2555 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2556 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
|
2557 if (bufr == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2558 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2559 Py_DECREF(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2560 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2561 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2562 Py_INCREF(bufr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2563 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2564 self->buf = bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2565 self->start = start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2566 self->end = end; |
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 return (PyObject *)(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2569 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2570 |
4319 | 2571 static void |
2572 RangeDestructor(PyObject *self) | |
2573 { | |
2574 Py_DECREF(((RangeObject *)(self))->buf); | |
2575 DESTRUCTOR_FINISH(self); | |
2576 } | |
2577 | |
4385 | 2578 static PyInt |
2579 RangeLength(PyObject *self) | |
2580 { | |
2581 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
2582 if (CheckBuffer(((RangeObject *)(self))->buf)) | |
2583 return -1; /* ??? */ | |
2584 | |
2585 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); | |
2586 } | |
2587 | |
2588 static PyObject * | |
2589 RangeItem(PyObject *self, PyInt n) | |
2590 { | |
2591 return RBItem(((RangeObject *)(self))->buf, n, | |
2592 ((RangeObject *)(self))->start, | |
2593 ((RangeObject *)(self))->end); | |
2594 } | |
2595 | |
2596 static PyObject * | |
2597 RangeSlice(PyObject *self, PyInt lo, PyInt hi) | |
2598 { | |
2599 return RBSlice(((RangeObject *)(self))->buf, lo, hi, | |
2600 ((RangeObject *)(self))->start, | |
2601 ((RangeObject *)(self))->end); | |
2602 } | |
2603 | |
2604 static PyObject * | |
2605 RangeAppend(PyObject *self, PyObject *args) | |
2606 { | |
2607 return RBAppend(((RangeObject *)(self))->buf, args, | |
2608 ((RangeObject *)(self))->start, | |
2609 ((RangeObject *)(self))->end, | |
2610 &((RangeObject *)(self))->end); | |
2611 } | |
2612 | |
2613 static PyObject * | |
2614 RangeRepr(PyObject *self) | |
2615 { | |
2616 static char repr[100]; | |
2617 RangeObject *this = (RangeObject *)(self); | |
2618 | |
2619 if (this->buf->buf == INVALID_BUFFER_VALUE) | |
2620 { | |
2621 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", | |
2622 (self)); | |
2623 return PyString_FromString(repr); | |
2624 } | |
2625 else | |
2626 { | |
2627 char *name = (char *)this->buf->buf->b_fname; | |
2628 int len; | |
2629 | |
2630 if (name == NULL) | |
2631 name = ""; | |
2632 len = (int)strlen(name); | |
2633 | |
2634 if (len > 45) | |
2635 name = name + (45 - len); | |
2636 | |
2637 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", | |
2638 len > 45 ? "..." : "", name, | |
2639 this->start, this->end); | |
2640 | |
2641 return PyString_FromString(repr); | |
2642 } | |
2643 } | |
2644 | |
2645 static struct PyMethodDef RangeMethods[] = { | |
2646 /* name, function, calling, documentation */ | |
2647 {"append", RangeAppend, 1, "Append data to the Vim range" }, | |
2648 { NULL, NULL, 0, NULL } | |
2649 }; | |
2650 | |
4319 | 2651 static PyTypeObject BufferType; |
2652 static PySequenceMethods BufferAsSeq; | |
2653 static PyMappingMethods BufferAsMapping; | |
2654 | |
2655 static PyObject * | |
4377 | 2656 BufferNew(buf_T *buf) |
2657 { | |
2658 /* We need to handle deletion of buffers underneath us. | |
2659 * If we add a "b_python*_ref" field to the buf_T structure, | |
2660 * then we can get at it in buf_freeall() in vim. We then | |
2661 * need to create only ONE Python object per buffer - if | |
2662 * we try to create a second, just INCREF the existing one | |
2663 * and return it. The (single) Python object referring to | |
2664 * the buffer is stored in "b_python*_ref". | |
2665 * Question: what to do on a buf_freeall(). We'll probably | |
2666 * have to either delete the Python object (DECREF it to | |
2667 * zero - a bad idea, as it leaves dangling refs!) or | |
2668 * set the buf_T * value to an invalid value (-1?), which | |
2669 * means we need checks in all access functions... Bah. | |
2670 * | |
4385 | 2671 * Python2 and Python3 get different fields and different objects: |
4377 | 2672 * b_python_ref and b_python3_ref fields respectively. |
2673 */ | |
2674 | |
2675 BufferObject *self; | |
2676 | |
2677 if (BUF_PYTHON_REF(buf) != NULL) | |
2678 { | |
2679 self = BUF_PYTHON_REF(buf); | |
2680 Py_INCREF(self); | |
2681 } | |
2682 else | |
2683 { | |
2684 self = PyObject_NEW(BufferObject, &BufferType); | |
2685 if (self == NULL) | |
2686 return NULL; | |
2687 self->buf = buf; | |
2688 BUF_PYTHON_REF(buf) = self; | |
2689 } | |
2690 | |
2691 return (PyObject *)(self); | |
2692 } | |
2693 | |
4385 | 2694 static void |
2695 BufferDestructor(PyObject *self) | |
4319 | 2696 { |
4385 | 2697 BufferObject *this = (BufferObject *)(self); |
2698 | |
2699 if (this->buf && this->buf != INVALID_BUFFER_VALUE) | |
2700 BUF_PYTHON_REF(this->buf) = NULL; | |
2701 | |
2702 DESTRUCTOR_FINISH(self); | |
4319 | 2703 } |
2704 | |
4377 | 2705 static PyInt |
2706 BufferLength(PyObject *self) | |
2707 { | |
2708 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
2709 if (CheckBuffer((BufferObject *)(self))) | |
2710 return -1; /* ??? */ | |
2711 | |
2712 return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count); | |
2713 } | |
2714 | |
2715 static PyObject * | |
2716 BufferItem(PyObject *self, PyInt n) | |
2717 { | |
4387 | 2718 return RBItem((BufferObject *)(self), n, 1, -1); |
4377 | 2719 } |
2720 | |
2721 static PyObject * | |
2722 BufferSlice(PyObject *self, PyInt lo, PyInt hi) | |
2723 { | |
4387 | 2724 return RBSlice((BufferObject *)(self), lo, hi, 1, -1); |
4377 | 2725 } |
2726 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2727 static PyObject * |
4385 | 2728 BufferAttr(BufferObject *this, char *name) |
2729 { | |
2730 if (strcmp(name, "name") == 0) | |
2731 return Py_BuildValue("s", this->buf->b_ffname); | |
2732 else if (strcmp(name, "number") == 0) | |
2733 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum); | |
2734 else if (strcmp(name, "vars") == 0) | |
2735 return DictionaryNew(this->buf->b_vars); | |
2736 else if (strcmp(name, "options") == 0) | |
2737 return OptionsNew(SREQ_BUF, this->buf, (checkfun) CheckBuffer, | |
2738 (PyObject *) this); | |
2739 else if (strcmp(name,"__members__") == 0) | |
2740 return Py_BuildValue("[ssss]", "name", "number", "vars", "options"); | |
2741 else | |
2742 return NULL; | |
2743 } | |
2744 | |
2745 static PyObject * | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2746 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
|
2747 { |
4387 | 2748 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
|
2749 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2750 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2751 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2752 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
|
2753 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2754 pos_T *posp; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2755 char *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2756 char mark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2757 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
|
2758 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2759 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
|
2760 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2761 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2762 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
|
2763 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2764 mark = *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2765 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2766 curbuf_save = curbuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2767 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
|
2768 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
|
2769 curbuf = curbuf_save; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2770 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2771 if (posp == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2772 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2773 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
|
2774 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2775 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2776 |
4352 | 2777 /* 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
|
2778 if (VimErrorCheck()) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2779 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2780 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2781 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
|
2782 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2783 /* 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
|
2784 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
|
2785 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2786 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2787 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2788 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
|
2789 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2790 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2791 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2792 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
|
2793 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2794 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2795 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2796 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2797 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
|
2798 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2799 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2800 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
|
2801 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2802 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2803 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
|
2804 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2805 |
4319 | 2806 static PyObject * |
2807 BufferRepr(PyObject *self) | |
2808 { | |
2809 static char repr[100]; | |
2810 BufferObject *this = (BufferObject *)(self); | |
2811 | |
2812 if (this->buf == INVALID_BUFFER_VALUE) | |
2813 { | |
2814 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); | |
2815 return PyString_FromString(repr); | |
2816 } | |
2817 else | |
2818 { | |
2819 char *name = (char *)this->buf->b_fname; | |
2820 PyInt len; | |
2821 | |
2822 if (name == NULL) | |
2823 name = ""; | |
2824 len = strlen(name); | |
2825 | |
2826 if (len > 35) | |
2827 name = name + (35 - len); | |
2828 | |
2829 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); | |
2830 | |
2831 return PyString_FromString(repr); | |
2832 } | |
2833 } | |
2834 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2835 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
|
2836 /* 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
|
2837 {"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
|
2838 {"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
|
2839 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
3310 | 2840 #if PY_VERSION_HEX >= 0x03000000 |
2841 {"__dir__", BufferDir, 4, "List its attributes" }, | |
2842 #endif | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2843 { 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
|
2844 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2845 |
4385 | 2846 /* 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
|
2847 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2848 |
4319 | 2849 static PyObject * |
2850 CurrentGetattr(PyObject *self UNUSED, char *name) | |
2851 { | |
2852 if (strcmp(name, "buffer") == 0) | |
2853 return (PyObject *)BufferNew(curbuf); | |
2854 else if (strcmp(name, "window") == 0) | |
2855 return (PyObject *)WindowNew(curwin); | |
2856 else if (strcmp(name, "line") == 0) | |
2857 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); | |
2858 else if (strcmp(name, "range") == 0) | |
2859 return RangeNew(curbuf, RangeStart, RangeEnd); | |
2860 else if (strcmp(name,"__members__") == 0) | |
2861 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); | |
2862 else | |
2863 { | |
2864 PyErr_SetString(PyExc_AttributeError, name); | |
2865 return NULL; | |
2866 } | |
2867 } | |
2868 | |
2869 static int | |
2870 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) | |
2871 { | |
2872 if (strcmp(name, "line") == 0) | |
2873 { | |
2874 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) | |
2875 return -1; | |
2876 | |
2877 return 0; | |
2878 } | |
2879 else | |
2880 { | |
2881 PyErr_SetString(PyExc_AttributeError, name); | |
2882 return -1; | |
2883 } | |
2884 } | |
2885 | |
3618 | 2886 static void |
2887 set_ref_in_py(const int copyID) | |
2888 { | |
2889 pylinkedlist_T *cur; | |
2890 dict_T *dd; | |
2891 list_T *ll; | |
2892 | |
2893 if (lastdict != NULL) | |
2894 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) | |
2895 { | |
2896 dd = ((DictionaryObject *) (cur->pll_obj))->dict; | |
2897 if (dd->dv_copyID != copyID) | |
2898 { | |
2899 dd->dv_copyID = copyID; | |
2900 set_ref_in_ht(&dd->dv_hashtab, copyID); | |
2901 } | |
2902 } | |
2903 | |
2904 if (lastlist != NULL) | |
2905 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) | |
2906 { | |
2907 ll = ((ListObject *) (cur->pll_obj))->list; | |
2908 if (ll->lv_copyID != copyID) | |
2909 { | |
2910 ll->lv_copyID = copyID; | |
2911 set_ref_in_list(ll, copyID); | |
2912 } | |
2913 } | |
2914 } | |
2915 | |
2916 static int | |
2917 set_string_copy(char_u *str, typval_T *tv) | |
2918 { | |
2919 tv->vval.v_string = vim_strsave(str); | |
2920 if (tv->vval.v_string == NULL) | |
2921 { | |
2922 PyErr_NoMemory(); | |
2923 return -1; | |
2924 } | |
2925 return 0; | |
2926 } | |
2927 | |
4385 | 2928 static int |
2929 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
2930 { | |
2931 dict_T *d; | |
2932 char_u *key; | |
2933 dictitem_T *di; | |
2934 PyObject *keyObject; | |
2935 PyObject *valObject; | |
2936 Py_ssize_t iter = 0; | |
2937 | |
2938 d = dict_alloc(); | |
2939 if (d == NULL) | |
2940 { | |
2941 PyErr_NoMemory(); | |
2942 return -1; | |
2943 } | |
2944 | |
2945 tv->v_type = VAR_DICT; | |
2946 tv->vval.v_dict = d; | |
2947 | |
2948 while (PyDict_Next(obj, &iter, &keyObject, &valObject)) | |
2949 { | |
2950 DICTKEY_DECL | |
2951 | |
2952 if (keyObject == NULL) | |
2953 return -1; | |
2954 if (valObject == NULL) | |
2955 return -1; | |
2956 | |
2957 DICTKEY_GET_NOTEMPTY(-1) | |
2958 | |
2959 di = dictitem_alloc(key); | |
2960 | |
2961 DICTKEY_UNREF | |
2962 | |
2963 if (di == NULL) | |
2964 { | |
2965 PyErr_NoMemory(); | |
2966 return -1; | |
2967 } | |
2968 di->di_tv.v_lock = 0; | |
2969 | |
2970 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
2971 { | |
2972 vim_free(di); | |
2973 return -1; | |
2974 } | |
2975 if (dict_add(d, di) == FAIL) | |
2976 { | |
2977 vim_free(di); | |
2978 PyErr_SetVim(_("failed to add key to dictionary")); | |
2979 return -1; | |
2980 } | |
2981 } | |
2982 return 0; | |
2983 } | |
2984 | |
2985 static int | |
2986 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
2987 { | |
2988 dict_T *d; | |
2989 char_u *key; | |
2990 dictitem_T *di; | |
2991 PyObject *list; | |
2992 PyObject *litem; | |
2993 PyObject *keyObject; | |
2994 PyObject *valObject; | |
2995 Py_ssize_t lsize; | |
2996 | |
2997 d = dict_alloc(); | |
2998 if (d == NULL) | |
2999 { | |
3000 PyErr_NoMemory(); | |
3001 return -1; | |
3002 } | |
3003 | |
3004 tv->v_type = VAR_DICT; | |
3005 tv->vval.v_dict = d; | |
3006 | |
3007 list = PyMapping_Items(obj); | |
3008 if (list == NULL) | |
3009 return -1; | |
3010 lsize = PyList_Size(list); | |
3011 while (lsize--) | |
3012 { | |
3013 DICTKEY_DECL | |
3014 | |
3015 litem = PyList_GetItem(list, lsize); | |
3016 if (litem == NULL) | |
3017 { | |
3018 Py_DECREF(list); | |
3019 return -1; | |
3020 } | |
3021 | |
3022 keyObject = PyTuple_GetItem(litem, 0); | |
3023 if (keyObject == NULL) | |
3024 { | |
3025 Py_DECREF(list); | |
3026 Py_DECREF(litem); | |
3027 return -1; | |
3028 } | |
3029 | |
3030 DICTKEY_GET_NOTEMPTY(-1) | |
3031 | |
3032 valObject = PyTuple_GetItem(litem, 1); | |
3033 if (valObject == NULL) | |
3034 { | |
3035 Py_DECREF(list); | |
3036 Py_DECREF(litem); | |
3037 return -1; | |
3038 } | |
3039 | |
3040 di = dictitem_alloc(key); | |
3041 | |
3042 DICTKEY_UNREF | |
3043 | |
3044 if (di == NULL) | |
3045 { | |
3046 Py_DECREF(list); | |
3047 Py_DECREF(litem); | |
3048 PyErr_NoMemory(); | |
3049 return -1; | |
3050 } | |
3051 di->di_tv.v_lock = 0; | |
3052 | |
3053 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
3054 { | |
3055 vim_free(di); | |
3056 Py_DECREF(list); | |
3057 Py_DECREF(litem); | |
3058 return -1; | |
3059 } | |
3060 if (dict_add(d, di) == FAIL) | |
3061 { | |
3062 vim_free(di); | |
3063 Py_DECREF(list); | |
3064 Py_DECREF(litem); | |
3065 PyErr_SetVim(_("failed to add key to dictionary")); | |
3066 return -1; | |
3067 } | |
3068 Py_DECREF(litem); | |
3069 } | |
3070 Py_DECREF(list); | |
3071 return 0; | |
3072 } | |
3073 | |
3074 static int | |
3075 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3076 { | |
3077 list_T *l; | |
3078 | |
3079 l = list_alloc(); | |
3080 if (l == NULL) | |
3081 { | |
3082 PyErr_NoMemory(); | |
3083 return -1; | |
3084 } | |
3085 | |
3086 tv->v_type = VAR_LIST; | |
3087 tv->vval.v_list = l; | |
3088 | |
3089 if (list_py_concat(l, obj, lookupDict) == -1) | |
3090 return -1; | |
3091 | |
3092 return 0; | |
3093 } | |
3094 | |
3095 static int | |
3096 pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3097 { | |
3098 PyObject *iterator = PyObject_GetIter(obj); | |
3099 PyObject *item; | |
3100 list_T *l; | |
3101 listitem_T *li; | |
3102 | |
3103 l = list_alloc(); | |
3104 | |
3105 if (l == NULL) | |
3106 { | |
3107 PyErr_NoMemory(); | |
3108 return -1; | |
3109 } | |
3110 | |
3111 tv->vval.v_list = l; | |
3112 tv->v_type = VAR_LIST; | |
3113 | |
3114 | |
3115 if (iterator == NULL) | |
3116 return -1; | |
3117 | |
3118 while ((item = PyIter_Next(obj))) | |
3119 { | |
3120 li = listitem_alloc(); | |
3121 if (li == NULL) | |
3122 { | |
3123 PyErr_NoMemory(); | |
3124 return -1; | |
3125 } | |
3126 li->li_tv.v_lock = 0; | |
3127 | |
3128 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1) | |
3129 return -1; | |
3130 | |
3131 list_append(l, li); | |
3132 | |
3133 Py_DECREF(item); | |
3134 } | |
3135 | |
3136 Py_DECREF(iterator); | |
3137 return 0; | |
3138 } | |
3139 | |
3618 | 3140 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
3141 | |
3142 static int | |
3143 convert_dl(PyObject *obj, typval_T *tv, | |
3144 pytotvfunc py_to_tv, PyObject *lookupDict) | |
3145 { | |
3146 PyObject *capsule; | |
3147 char hexBuf[sizeof(void *) * 2 + 3]; | |
3148 | |
3149 sprintf(hexBuf, "%p", obj); | |
3150 | |
3638 | 3151 # ifdef PY_USE_CAPSULE |
3618 | 3152 capsule = PyDict_GetItemString(lookupDict, hexBuf); |
3648 | 3153 # else |
3154 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf); | |
3155 # endif | |
3618 | 3156 if (capsule == NULL) |
3157 { | |
3638 | 3158 # ifdef PY_USE_CAPSULE |
3618 | 3159 capsule = PyCapsule_New(tv, NULL, NULL); |
3648 | 3160 # else |
3161 capsule = PyCObject_FromVoidPtr(tv, NULL); | |
3162 # endif | |
3618 | 3163 PyDict_SetItemString(lookupDict, hexBuf, capsule); |
3164 Py_DECREF(capsule); | |
3165 if (py_to_tv(obj, tv, lookupDict) == -1) | |
3166 { | |
3167 tv->v_type = VAR_UNKNOWN; | |
3168 return -1; | |
3169 } | |
3170 /* As we are not using copy_tv which increments reference count we must | |
3171 * do it ourself. */ | |
3172 switch(tv->v_type) | |
3173 { | |
3174 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; | |
3175 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; | |
3176 } | |
3177 } | |
3178 else | |
3179 { | |
3638 | 3180 typval_T *v; |
3181 | |
3182 # ifdef PY_USE_CAPSULE | |
3183 v = PyCapsule_GetPointer(capsule, NULL); | |
3184 # else | |
3648 | 3185 v = PyCObject_AsVoidPtr(capsule); |
3638 | 3186 # endif |
3618 | 3187 copy_tv(v, tv); |
3188 } | |
3189 return 0; | |
3190 } | |
3191 | |
3192 static int | |
3193 ConvertFromPyObject(PyObject *obj, typval_T *tv) | |
3194 { | |
3195 PyObject *lookup_dict; | |
3196 int r; | |
3197 | |
3198 lookup_dict = PyDict_New(); | |
3199 r = _ConvertFromPyObject(obj, tv, lookup_dict); | |
3200 Py_DECREF(lookup_dict); | |
3201 return r; | |
3202 } | |
3203 | |
3204 static int | |
3205 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3206 { | |
3207 if (obj->ob_type == &DictionaryType) | |
3208 { | |
3209 tv->v_type = VAR_DICT; | |
3210 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); | |
3211 ++tv->vval.v_dict->dv_refcount; | |
3212 } | |
3213 else if (obj->ob_type == &ListType) | |
3214 { | |
3215 tv->v_type = VAR_LIST; | |
3216 tv->vval.v_list = (((ListObject *)(obj))->list); | |
3217 ++tv->vval.v_list->lv_refcount; | |
3218 } | |
3219 else if (obj->ob_type == &FunctionType) | |
3220 { | |
3221 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) | |
3222 return -1; | |
3223 | |
3224 tv->v_type = VAR_FUNC; | |
3225 func_ref(tv->vval.v_string); | |
3226 } | |
3227 else if (PyBytes_Check(obj)) | |
3228 { | |
3800 | 3229 char_u *result; |
3230 | |
3231 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) | |
3232 return -1; | |
3618 | 3233 if (result == NULL) |
3234 return -1; | |
3235 | |
3236 if (set_string_copy(result, tv) == -1) | |
3237 return -1; | |
3238 | |
3239 tv->v_type = VAR_STRING; | |
3240 } | |
3241 else if (PyUnicode_Check(obj)) | |
3242 { | |
3243 PyObject *bytes; | |
3244 char_u *result; | |
3245 | |
3246 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); | |
3247 if (bytes == NULL) | |
3248 return -1; | |
3249 | |
3800 | 3250 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
3251 return -1; | |
3618 | 3252 if (result == NULL) |
3253 return -1; | |
3254 | |
3255 if (set_string_copy(result, tv) == -1) | |
3256 { | |
3257 Py_XDECREF(bytes); | |
3258 return -1; | |
3259 } | |
3260 Py_XDECREF(bytes); | |
3261 | |
3262 tv->v_type = VAR_STRING; | |
3263 } | |
4321 | 3264 #if PY_MAJOR_VERSION < 3 |
3618 | 3265 else if (PyInt_Check(obj)) |
3266 { | |
3267 tv->v_type = VAR_NUMBER; | |
3268 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); | |
3269 } | |
3270 #endif | |
3271 else if (PyLong_Check(obj)) | |
3272 { | |
3273 tv->v_type = VAR_NUMBER; | |
3274 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); | |
3275 } | |
3276 else if (PyDict_Check(obj)) | |
3277 return convert_dl(obj, tv, pydict_to_tv, lookupDict); | |
3278 #ifdef FEAT_FLOAT | |
3279 else if (PyFloat_Check(obj)) | |
3280 { | |
3281 tv->v_type = VAR_FLOAT; | |
3282 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); | |
3283 } | |
3284 #endif | |
3285 else if (PyIter_Check(obj)) | |
3286 return convert_dl(obj, tv, pyiter_to_tv, lookupDict); | |
3287 else if (PySequence_Check(obj)) | |
3288 return convert_dl(obj, tv, pyseq_to_tv, lookupDict); | |
3289 else if (PyMapping_Check(obj)) | |
3290 return convert_dl(obj, tv, pymap_to_tv, lookupDict); | |
3291 else | |
3292 { | |
3293 PyErr_SetString(PyExc_TypeError, _("unable to convert to vim structure")); | |
3294 return -1; | |
3295 } | |
3296 return 0; | |
3297 } | |
3298 | |
3299 static PyObject * | |
3300 ConvertToPyObject(typval_T *tv) | |
3301 { | |
3302 if (tv == NULL) | |
3303 { | |
3304 PyErr_SetVim(_("NULL reference passed")); | |
3305 return NULL; | |
3306 } | |
3307 switch (tv->v_type) | |
3308 { | |
3309 case VAR_STRING: | |
3852 | 3310 return PyBytes_FromString(tv->vval.v_string == NULL |
3311 ? "" : (char *)tv->vval.v_string); | |
3618 | 3312 case VAR_NUMBER: |
3313 return PyLong_FromLong((long) tv->vval.v_number); | |
3314 #ifdef FEAT_FLOAT | |
3315 case VAR_FLOAT: | |
3316 return PyFloat_FromDouble((double) tv->vval.v_float); | |
3317 #endif | |
3318 case VAR_LIST: | |
3319 return ListNew(tv->vval.v_list); | |
3320 case VAR_DICT: | |
3321 return DictionaryNew(tv->vval.v_dict); | |
3322 case VAR_FUNC: | |
3852 | 3323 return FunctionNew(tv->vval.v_string == NULL |
3324 ? (char_u *)"" : tv->vval.v_string); | |
3618 | 3325 case VAR_UNKNOWN: |
3326 Py_INCREF(Py_None); | |
3327 return Py_None; | |
3328 default: | |
3329 PyErr_SetVim(_("internal error: invalid value type")); | |
3330 return NULL; | |
3331 } | |
3332 } | |
4319 | 3333 |
3334 typedef struct | |
3335 { | |
3336 PyObject_HEAD | |
3337 } CurrentObject; | |
3338 static PyTypeObject CurrentType; | |
3339 | |
3340 static void | |
3341 init_structs(void) | |
3342 { | |
3343 vim_memset(&OutputType, 0, sizeof(OutputType)); | |
3344 OutputType.tp_name = "vim.message"; | |
3345 OutputType.tp_basicsize = sizeof(OutputObject); | |
3346 OutputType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3347 OutputType.tp_doc = "vim message object"; | |
3348 OutputType.tp_methods = OutputMethods; | |
3349 #if PY_MAJOR_VERSION >= 3 | |
3350 OutputType.tp_getattro = OutputGetattro; | |
3351 OutputType.tp_setattro = OutputSetattro; | |
3352 OutputType.tp_alloc = call_PyType_GenericAlloc; | |
3353 OutputType.tp_new = call_PyType_GenericNew; | |
3354 OutputType.tp_free = call_PyObject_Free; | |
3355 #else | |
3356 OutputType.tp_getattr = OutputGetattr; | |
3357 OutputType.tp_setattr = OutputSetattr; | |
3358 #endif | |
3359 | |
3360 vim_memset(&BufferType, 0, sizeof(BufferType)); | |
3361 BufferType.tp_name = "vim.buffer"; | |
3362 BufferType.tp_basicsize = sizeof(BufferType); | |
3363 BufferType.tp_dealloc = BufferDestructor; | |
3364 BufferType.tp_repr = BufferRepr; | |
3365 BufferType.tp_as_sequence = &BufferAsSeq; | |
3366 BufferType.tp_as_mapping = &BufferAsMapping; | |
3367 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3368 BufferType.tp_doc = "vim buffer object"; | |
3369 BufferType.tp_methods = BufferMethods; | |
3370 #if PY_MAJOR_VERSION >= 3 | |
3371 BufferType.tp_getattro = BufferGetattro; | |
3372 BufferType.tp_alloc = call_PyType_GenericAlloc; | |
3373 BufferType.tp_new = call_PyType_GenericNew; | |
3374 BufferType.tp_free = call_PyObject_Free; | |
3375 #else | |
3376 BufferType.tp_getattr = BufferGetattr; | |
3377 #endif | |
3378 | |
3379 vim_memset(&WindowType, 0, sizeof(WindowType)); | |
3380 WindowType.tp_name = "vim.window"; | |
3381 WindowType.tp_basicsize = sizeof(WindowObject); | |
3382 WindowType.tp_dealloc = WindowDestructor; | |
3383 WindowType.tp_repr = WindowRepr; | |
3384 WindowType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3385 WindowType.tp_doc = "vim Window object"; | |
3386 WindowType.tp_methods = WindowMethods; | |
3387 #if PY_MAJOR_VERSION >= 3 | |
3388 WindowType.tp_getattro = WindowGetattro; | |
3389 WindowType.tp_setattro = WindowSetattro; | |
3390 WindowType.tp_alloc = call_PyType_GenericAlloc; | |
3391 WindowType.tp_new = call_PyType_GenericNew; | |
3392 WindowType.tp_free = call_PyObject_Free; | |
3393 #else | |
3394 WindowType.tp_getattr = WindowGetattr; | |
3395 WindowType.tp_setattr = WindowSetattr; | |
3396 #endif | |
3397 | |
3398 vim_memset(&BufListType, 0, sizeof(BufListType)); | |
3399 BufListType.tp_name = "vim.bufferlist"; | |
3400 BufListType.tp_basicsize = sizeof(BufListObject); | |
3401 BufListType.tp_as_sequence = &BufListAsSeq; | |
3402 BufListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3403 BufferType.tp_doc = "vim buffer list"; | |
3404 | |
3405 vim_memset(&WinListType, 0, sizeof(WinListType)); | |
3406 WinListType.tp_name = "vim.windowlist"; | |
3407 WinListType.tp_basicsize = sizeof(WinListType); | |
3408 WinListType.tp_as_sequence = &WinListAsSeq; | |
3409 WinListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3410 WinListType.tp_doc = "vim window list"; | |
3411 | |
3412 vim_memset(&RangeType, 0, sizeof(RangeType)); | |
3413 RangeType.tp_name = "vim.range"; | |
3414 RangeType.tp_basicsize = sizeof(RangeObject); | |
3415 RangeType.tp_dealloc = RangeDestructor; | |
3416 RangeType.tp_repr = RangeRepr; | |
3417 RangeType.tp_as_sequence = &RangeAsSeq; | |
3418 RangeType.tp_as_mapping = &RangeAsMapping; | |
3419 RangeType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3420 RangeType.tp_doc = "vim Range object"; | |
3421 RangeType.tp_methods = RangeMethods; | |
3422 #if PY_MAJOR_VERSION >= 3 | |
3423 RangeType.tp_getattro = RangeGetattro; | |
3424 RangeType.tp_alloc = call_PyType_GenericAlloc; | |
3425 RangeType.tp_new = call_PyType_GenericNew; | |
3426 RangeType.tp_free = call_PyObject_Free; | |
3427 #else | |
3428 RangeType.tp_getattr = RangeGetattr; | |
3429 #endif | |
3430 | |
3431 vim_memset(&CurrentType, 0, sizeof(CurrentType)); | |
3432 CurrentType.tp_name = "vim.currentdata"; | |
3433 CurrentType.tp_basicsize = sizeof(CurrentObject); | |
3434 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3435 CurrentType.tp_doc = "vim current object"; | |
3436 #if PY_MAJOR_VERSION >= 3 | |
3437 CurrentType.tp_getattro = CurrentGetattro; | |
3438 CurrentType.tp_setattro = CurrentSetattro; | |
3439 #else | |
3440 CurrentType.tp_getattr = CurrentGetattr; | |
3441 CurrentType.tp_setattr = CurrentSetattr; | |
3442 #endif | |
3443 | |
3444 vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); | |
3445 DictionaryType.tp_name = "vim.dictionary"; | |
3446 DictionaryType.tp_basicsize = sizeof(DictionaryObject); | |
3447 DictionaryType.tp_dealloc = DictionaryDestructor; | |
3448 DictionaryType.tp_as_mapping = &DictionaryAsMapping; | |
3449 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3450 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; | |
3451 DictionaryType.tp_methods = DictionaryMethods; | |
3452 #if PY_MAJOR_VERSION >= 3 | |
3453 DictionaryType.tp_getattro = DictionaryGetattro; | |
3454 DictionaryType.tp_setattro = DictionarySetattro; | |
3455 #else | |
3456 DictionaryType.tp_getattr = DictionaryGetattr; | |
3457 DictionaryType.tp_setattr = DictionarySetattr; | |
3458 #endif | |
3459 | |
3460 vim_memset(&ListType, 0, sizeof(ListType)); | |
3461 ListType.tp_name = "vim.list"; | |
3462 ListType.tp_dealloc = ListDestructor; | |
3463 ListType.tp_basicsize = sizeof(ListObject); | |
3464 ListType.tp_as_sequence = &ListAsSeq; | |
3465 ListType.tp_as_mapping = &ListAsMapping; | |
3466 ListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3467 ListType.tp_doc = "list pushing modifications to vim structure"; | |
3468 ListType.tp_methods = ListMethods; | |
3469 #if PY_MAJOR_VERSION >= 3 | |
3470 ListType.tp_getattro = ListGetattro; | |
3471 ListType.tp_setattro = ListSetattro; | |
3472 #else | |
3473 ListType.tp_getattr = ListGetattr; | |
3474 ListType.tp_setattr = ListSetattr; | |
3475 #endif | |
3476 | |
3477 vim_memset(&FunctionType, 0, sizeof(FunctionType)); | |
3478 FunctionType.tp_name = "vim.list"; | |
3479 FunctionType.tp_basicsize = sizeof(FunctionObject); | |
3480 FunctionType.tp_dealloc = FunctionDestructor; | |
3481 FunctionType.tp_call = FunctionCall; | |
3482 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3483 FunctionType.tp_doc = "object that calls vim function"; | |
3484 FunctionType.tp_methods = FunctionMethods; | |
3485 #if PY_MAJOR_VERSION >= 3 | |
3486 FunctionType.tp_getattro = FunctionGetattro; | |
3487 #else | |
3488 FunctionType.tp_getattr = FunctionGetattr; | |
3489 #endif | |
3490 | |
4350 | 3491 vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
3492 OptionsType.tp_name = "vim.options"; | |
3493 OptionsType.tp_basicsize = sizeof(OptionsObject); | |
3494 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3495 OptionsType.tp_doc = "object for manipulating options"; | |
3496 OptionsType.tp_as_mapping = &OptionsAsMapping; | |
3497 OptionsType.tp_dealloc = OptionsDestructor; | |
3498 | |
4319 | 3499 #if PY_MAJOR_VERSION >= 3 |
3500 vim_memset(&vimmodule, 0, sizeof(vimmodule)); | |
3501 vimmodule.m_name = "vim"; | |
3502 vimmodule.m_doc = "Vim Python interface\n"; | |
3503 vimmodule.m_size = -1; | |
3504 vimmodule.m_methods = VimMethods; | |
3505 #endif | |
3506 } |