Mercurial > vim
annotate src/if_py_both.h @ 4389:736b8e18a3bc v7.3.943
updated for version 7.3.943
Problem: Python: Negative indices were failing.
Solution: Fix negative indices. Add tests. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 12 May 2013 21:16:23 +0200 |
parents | 358c10968c7f |
children | 80eea7a9d6b9 |
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 | |
4389 | 2397 if (n < 0) |
2398 n += end - start + 1; | |
2399 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2400 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
|
2401 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2402 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
|
2403 return NULL; |
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 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
|
2407 } |
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 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2410 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
|
2411 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2412 PyInt size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2413 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2414 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2415 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2416 |
4387 | 2417 if (end == -1) |
2418 end = self->buf->b_ml.ml_line_count; | |
2419 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2420 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
|
2421 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2422 if (lo < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2423 lo = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2424 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
|
2425 lo = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2426 if (hi < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2427 hi = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2428 if (hi < lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2429 hi = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2430 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
|
2431 hi = size; |
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 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
|
2434 } |
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 static PyInt |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2437 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
|
2438 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2439 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2440 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2441 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2442 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2443 |
4387 | 2444 if (end == -1) |
2445 end = self->buf->b_ml.ml_line_count; | |
2446 | |
4389 | 2447 if (n < 0) |
2448 n += end - start + 1; | |
2449 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2450 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
|
2451 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2452 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
|
2453 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2454 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2455 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2456 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
|
2457 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2458 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2459 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2460 *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
|
2461 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2462 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2463 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2464 |
2894 | 2465 static PyInt |
2466 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) | |
2467 { | |
2468 PyInt size; | |
2469 PyInt len_change; | |
2470 | |
2471 /* Self must be a valid buffer */ | |
2472 if (CheckBuffer(self)) | |
2473 return -1; | |
2474 | |
4387 | 2475 if (end == -1) |
2476 end = self->buf->b_ml.ml_line_count; | |
2477 | |
2894 | 2478 /* Sort out the slice range */ |
2479 size = end - start + 1; | |
2480 | |
2481 if (lo < 0) | |
2482 lo = 0; | |
2483 else if (lo > size) | |
2484 lo = size; | |
2485 if (hi < 0) | |
2486 hi = 0; | |
2487 if (hi < lo) | |
2488 hi = lo; | |
2489 else if (hi > size) | |
2490 hi = size; | |
2491 | |
2492 if (SetBufferLineList(self->buf, lo + start, hi + start, | |
2493 val, &len_change) == FAIL) | |
2494 return -1; | |
2495 | |
2496 if (new_end) | |
2497 *new_end = end + len_change; | |
2498 | |
2499 return 0; | |
2500 } | |
2501 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2502 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2503 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2504 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
|
2505 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2506 PyObject *lines; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2507 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2508 PyInt max; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2509 PyInt n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2510 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2511 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2512 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2513 |
4387 | 2514 if (end == -1) |
2515 end = self->buf->b_ml.ml_line_count; | |
2516 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2517 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
|
2518 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2519 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
|
2520 return NULL; |
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 (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
|
2523 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2524 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
|
2525 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2526 } |
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 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
|
2529 return NULL; |
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 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2532 *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
|
2533 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2534 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
|
2535 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2536 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2537 |
4385 | 2538 /* Range object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2539 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2540 |
4319 | 2541 static PyTypeObject RangeType; |
4385 | 2542 static PySequenceMethods RangeAsSeq; |
2543 static PyMappingMethods RangeAsMapping; | |
4319 | 2544 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2545 typedef struct |
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 PyObject_HEAD |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2548 BufferObject *buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2549 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2550 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2551 } RangeObject; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2552 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2553 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2554 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
|
2555 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2556 BufferObject *bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2557 RangeObject *self; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2558 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
|
2559 if (self == NULL) |
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 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
|
2563 if (bufr == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2564 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2565 Py_DECREF(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2566 return NULL; |
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 Py_INCREF(bufr); |
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 self->buf = bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2571 self->start = start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2572 self->end = end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2573 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2574 return (PyObject *)(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2575 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2576 |
4319 | 2577 static void |
2578 RangeDestructor(PyObject *self) | |
2579 { | |
2580 Py_DECREF(((RangeObject *)(self))->buf); | |
2581 DESTRUCTOR_FINISH(self); | |
2582 } | |
2583 | |
4385 | 2584 static PyInt |
2585 RangeLength(PyObject *self) | |
2586 { | |
2587 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
2588 if (CheckBuffer(((RangeObject *)(self))->buf)) | |
2589 return -1; /* ??? */ | |
2590 | |
2591 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); | |
2592 } | |
2593 | |
2594 static PyObject * | |
2595 RangeItem(PyObject *self, PyInt n) | |
2596 { | |
2597 return RBItem(((RangeObject *)(self))->buf, n, | |
2598 ((RangeObject *)(self))->start, | |
2599 ((RangeObject *)(self))->end); | |
2600 } | |
2601 | |
2602 static PyObject * | |
2603 RangeSlice(PyObject *self, PyInt lo, PyInt hi) | |
2604 { | |
2605 return RBSlice(((RangeObject *)(self))->buf, lo, hi, | |
2606 ((RangeObject *)(self))->start, | |
2607 ((RangeObject *)(self))->end); | |
2608 } | |
2609 | |
2610 static PyObject * | |
2611 RangeAppend(PyObject *self, PyObject *args) | |
2612 { | |
2613 return RBAppend(((RangeObject *)(self))->buf, args, | |
2614 ((RangeObject *)(self))->start, | |
2615 ((RangeObject *)(self))->end, | |
2616 &((RangeObject *)(self))->end); | |
2617 } | |
2618 | |
2619 static PyObject * | |
2620 RangeRepr(PyObject *self) | |
2621 { | |
2622 static char repr[100]; | |
2623 RangeObject *this = (RangeObject *)(self); | |
2624 | |
2625 if (this->buf->buf == INVALID_BUFFER_VALUE) | |
2626 { | |
2627 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", | |
2628 (self)); | |
2629 return PyString_FromString(repr); | |
2630 } | |
2631 else | |
2632 { | |
2633 char *name = (char *)this->buf->buf->b_fname; | |
2634 int len; | |
2635 | |
2636 if (name == NULL) | |
2637 name = ""; | |
2638 len = (int)strlen(name); | |
2639 | |
2640 if (len > 45) | |
2641 name = name + (45 - len); | |
2642 | |
2643 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", | |
2644 len > 45 ? "..." : "", name, | |
2645 this->start, this->end); | |
2646 | |
2647 return PyString_FromString(repr); | |
2648 } | |
2649 } | |
2650 | |
2651 static struct PyMethodDef RangeMethods[] = { | |
2652 /* name, function, calling, documentation */ | |
2653 {"append", RangeAppend, 1, "Append data to the Vim range" }, | |
2654 { NULL, NULL, 0, NULL } | |
2655 }; | |
2656 | |
4319 | 2657 static PyTypeObject BufferType; |
2658 static PySequenceMethods BufferAsSeq; | |
2659 static PyMappingMethods BufferAsMapping; | |
2660 | |
2661 static PyObject * | |
4377 | 2662 BufferNew(buf_T *buf) |
2663 { | |
2664 /* We need to handle deletion of buffers underneath us. | |
2665 * If we add a "b_python*_ref" field to the buf_T structure, | |
2666 * then we can get at it in buf_freeall() in vim. We then | |
2667 * need to create only ONE Python object per buffer - if | |
2668 * we try to create a second, just INCREF the existing one | |
2669 * and return it. The (single) Python object referring to | |
2670 * the buffer is stored in "b_python*_ref". | |
2671 * Question: what to do on a buf_freeall(). We'll probably | |
2672 * have to either delete the Python object (DECREF it to | |
2673 * zero - a bad idea, as it leaves dangling refs!) or | |
2674 * set the buf_T * value to an invalid value (-1?), which | |
2675 * means we need checks in all access functions... Bah. | |
2676 * | |
4385 | 2677 * Python2 and Python3 get different fields and different objects: |
4377 | 2678 * b_python_ref and b_python3_ref fields respectively. |
2679 */ | |
2680 | |
2681 BufferObject *self; | |
2682 | |
2683 if (BUF_PYTHON_REF(buf) != NULL) | |
2684 { | |
2685 self = BUF_PYTHON_REF(buf); | |
2686 Py_INCREF(self); | |
2687 } | |
2688 else | |
2689 { | |
2690 self = PyObject_NEW(BufferObject, &BufferType); | |
2691 if (self == NULL) | |
2692 return NULL; | |
2693 self->buf = buf; | |
2694 BUF_PYTHON_REF(buf) = self; | |
2695 } | |
2696 | |
2697 return (PyObject *)(self); | |
2698 } | |
2699 | |
4385 | 2700 static void |
2701 BufferDestructor(PyObject *self) | |
4319 | 2702 { |
4385 | 2703 BufferObject *this = (BufferObject *)(self); |
2704 | |
2705 if (this->buf && this->buf != INVALID_BUFFER_VALUE) | |
2706 BUF_PYTHON_REF(this->buf) = NULL; | |
2707 | |
2708 DESTRUCTOR_FINISH(self); | |
4319 | 2709 } |
2710 | |
4377 | 2711 static PyInt |
2712 BufferLength(PyObject *self) | |
2713 { | |
2714 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
2715 if (CheckBuffer((BufferObject *)(self))) | |
2716 return -1; /* ??? */ | |
2717 | |
2718 return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count); | |
2719 } | |
2720 | |
2721 static PyObject * | |
2722 BufferItem(PyObject *self, PyInt n) | |
2723 { | |
4387 | 2724 return RBItem((BufferObject *)(self), n, 1, -1); |
4377 | 2725 } |
2726 | |
2727 static PyObject * | |
2728 BufferSlice(PyObject *self, PyInt lo, PyInt hi) | |
2729 { | |
4387 | 2730 return RBSlice((BufferObject *)(self), lo, hi, 1, -1); |
4377 | 2731 } |
2732 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2733 static PyObject * |
4385 | 2734 BufferAttr(BufferObject *this, char *name) |
2735 { | |
2736 if (strcmp(name, "name") == 0) | |
2737 return Py_BuildValue("s", this->buf->b_ffname); | |
2738 else if (strcmp(name, "number") == 0) | |
2739 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum); | |
2740 else if (strcmp(name, "vars") == 0) | |
2741 return DictionaryNew(this->buf->b_vars); | |
2742 else if (strcmp(name, "options") == 0) | |
2743 return OptionsNew(SREQ_BUF, this->buf, (checkfun) CheckBuffer, | |
2744 (PyObject *) this); | |
2745 else if (strcmp(name,"__members__") == 0) | |
2746 return Py_BuildValue("[ssss]", "name", "number", "vars", "options"); | |
2747 else | |
2748 return NULL; | |
2749 } | |
2750 | |
2751 static PyObject * | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2752 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
|
2753 { |
4387 | 2754 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
|
2755 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2756 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2757 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2758 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
|
2759 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2760 pos_T *posp; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2761 char *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2762 char mark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2763 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
|
2764 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2765 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
|
2766 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2767 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2768 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
|
2769 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2770 mark = *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2771 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2772 curbuf_save = curbuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2773 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
|
2774 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
|
2775 curbuf = curbuf_save; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2776 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2777 if (posp == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2778 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2779 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
|
2780 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2781 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2782 |
4352 | 2783 /* 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
|
2784 if (VimErrorCheck()) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2785 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2786 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2787 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
|
2788 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2789 /* 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
|
2790 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
|
2791 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2792 } |
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 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
|
2795 } |
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 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2798 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
|
2799 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2800 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2801 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2802 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2803 if (CheckBuffer((BufferObject *)(self))) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2804 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2805 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2806 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
|
2807 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2808 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2809 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
|
2810 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2811 |
4319 | 2812 static PyObject * |
2813 BufferRepr(PyObject *self) | |
2814 { | |
2815 static char repr[100]; | |
2816 BufferObject *this = (BufferObject *)(self); | |
2817 | |
2818 if (this->buf == INVALID_BUFFER_VALUE) | |
2819 { | |
2820 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); | |
2821 return PyString_FromString(repr); | |
2822 } | |
2823 else | |
2824 { | |
2825 char *name = (char *)this->buf->b_fname; | |
2826 PyInt len; | |
2827 | |
2828 if (name == NULL) | |
2829 name = ""; | |
2830 len = strlen(name); | |
2831 | |
2832 if (len > 35) | |
2833 name = name + (35 - len); | |
2834 | |
2835 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); | |
2836 | |
2837 return PyString_FromString(repr); | |
2838 } | |
2839 } | |
2840 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2841 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
|
2842 /* 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
|
2843 {"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
|
2844 {"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
|
2845 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
3310 | 2846 #if PY_VERSION_HEX >= 0x03000000 |
2847 {"__dir__", BufferDir, 4, "List its attributes" }, | |
2848 #endif | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2849 { 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
|
2850 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2851 |
4385 | 2852 /* 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
|
2853 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2854 |
4319 | 2855 static PyObject * |
2856 CurrentGetattr(PyObject *self UNUSED, char *name) | |
2857 { | |
2858 if (strcmp(name, "buffer") == 0) | |
2859 return (PyObject *)BufferNew(curbuf); | |
2860 else if (strcmp(name, "window") == 0) | |
2861 return (PyObject *)WindowNew(curwin); | |
2862 else if (strcmp(name, "line") == 0) | |
2863 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); | |
2864 else if (strcmp(name, "range") == 0) | |
2865 return RangeNew(curbuf, RangeStart, RangeEnd); | |
2866 else if (strcmp(name,"__members__") == 0) | |
2867 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); | |
2868 else | |
2869 { | |
2870 PyErr_SetString(PyExc_AttributeError, name); | |
2871 return NULL; | |
2872 } | |
2873 } | |
2874 | |
2875 static int | |
2876 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) | |
2877 { | |
2878 if (strcmp(name, "line") == 0) | |
2879 { | |
2880 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) | |
2881 return -1; | |
2882 | |
2883 return 0; | |
2884 } | |
2885 else | |
2886 { | |
2887 PyErr_SetString(PyExc_AttributeError, name); | |
2888 return -1; | |
2889 } | |
2890 } | |
2891 | |
3618 | 2892 static void |
2893 set_ref_in_py(const int copyID) | |
2894 { | |
2895 pylinkedlist_T *cur; | |
2896 dict_T *dd; | |
2897 list_T *ll; | |
2898 | |
2899 if (lastdict != NULL) | |
2900 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) | |
2901 { | |
2902 dd = ((DictionaryObject *) (cur->pll_obj))->dict; | |
2903 if (dd->dv_copyID != copyID) | |
2904 { | |
2905 dd->dv_copyID = copyID; | |
2906 set_ref_in_ht(&dd->dv_hashtab, copyID); | |
2907 } | |
2908 } | |
2909 | |
2910 if (lastlist != NULL) | |
2911 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) | |
2912 { | |
2913 ll = ((ListObject *) (cur->pll_obj))->list; | |
2914 if (ll->lv_copyID != copyID) | |
2915 { | |
2916 ll->lv_copyID = copyID; | |
2917 set_ref_in_list(ll, copyID); | |
2918 } | |
2919 } | |
2920 } | |
2921 | |
2922 static int | |
2923 set_string_copy(char_u *str, typval_T *tv) | |
2924 { | |
2925 tv->vval.v_string = vim_strsave(str); | |
2926 if (tv->vval.v_string == NULL) | |
2927 { | |
2928 PyErr_NoMemory(); | |
2929 return -1; | |
2930 } | |
2931 return 0; | |
2932 } | |
2933 | |
4385 | 2934 static int |
2935 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
2936 { | |
2937 dict_T *d; | |
2938 char_u *key; | |
2939 dictitem_T *di; | |
2940 PyObject *keyObject; | |
2941 PyObject *valObject; | |
2942 Py_ssize_t iter = 0; | |
2943 | |
2944 d = dict_alloc(); | |
2945 if (d == NULL) | |
2946 { | |
2947 PyErr_NoMemory(); | |
2948 return -1; | |
2949 } | |
2950 | |
2951 tv->v_type = VAR_DICT; | |
2952 tv->vval.v_dict = d; | |
2953 | |
2954 while (PyDict_Next(obj, &iter, &keyObject, &valObject)) | |
2955 { | |
2956 DICTKEY_DECL | |
2957 | |
2958 if (keyObject == NULL) | |
2959 return -1; | |
2960 if (valObject == NULL) | |
2961 return -1; | |
2962 | |
2963 DICTKEY_GET_NOTEMPTY(-1) | |
2964 | |
2965 di = dictitem_alloc(key); | |
2966 | |
2967 DICTKEY_UNREF | |
2968 | |
2969 if (di == NULL) | |
2970 { | |
2971 PyErr_NoMemory(); | |
2972 return -1; | |
2973 } | |
2974 di->di_tv.v_lock = 0; | |
2975 | |
2976 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
2977 { | |
2978 vim_free(di); | |
2979 return -1; | |
2980 } | |
2981 if (dict_add(d, di) == FAIL) | |
2982 { | |
2983 vim_free(di); | |
2984 PyErr_SetVim(_("failed to add key to dictionary")); | |
2985 return -1; | |
2986 } | |
2987 } | |
2988 return 0; | |
2989 } | |
2990 | |
2991 static int | |
2992 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
2993 { | |
2994 dict_T *d; | |
2995 char_u *key; | |
2996 dictitem_T *di; | |
2997 PyObject *list; | |
2998 PyObject *litem; | |
2999 PyObject *keyObject; | |
3000 PyObject *valObject; | |
3001 Py_ssize_t lsize; | |
3002 | |
3003 d = dict_alloc(); | |
3004 if (d == NULL) | |
3005 { | |
3006 PyErr_NoMemory(); | |
3007 return -1; | |
3008 } | |
3009 | |
3010 tv->v_type = VAR_DICT; | |
3011 tv->vval.v_dict = d; | |
3012 | |
3013 list = PyMapping_Items(obj); | |
3014 if (list == NULL) | |
3015 return -1; | |
3016 lsize = PyList_Size(list); | |
3017 while (lsize--) | |
3018 { | |
3019 DICTKEY_DECL | |
3020 | |
3021 litem = PyList_GetItem(list, lsize); | |
3022 if (litem == NULL) | |
3023 { | |
3024 Py_DECREF(list); | |
3025 return -1; | |
3026 } | |
3027 | |
3028 keyObject = PyTuple_GetItem(litem, 0); | |
3029 if (keyObject == NULL) | |
3030 { | |
3031 Py_DECREF(list); | |
3032 Py_DECREF(litem); | |
3033 return -1; | |
3034 } | |
3035 | |
3036 DICTKEY_GET_NOTEMPTY(-1) | |
3037 | |
3038 valObject = PyTuple_GetItem(litem, 1); | |
3039 if (valObject == NULL) | |
3040 { | |
3041 Py_DECREF(list); | |
3042 Py_DECREF(litem); | |
3043 return -1; | |
3044 } | |
3045 | |
3046 di = dictitem_alloc(key); | |
3047 | |
3048 DICTKEY_UNREF | |
3049 | |
3050 if (di == NULL) | |
3051 { | |
3052 Py_DECREF(list); | |
3053 Py_DECREF(litem); | |
3054 PyErr_NoMemory(); | |
3055 return -1; | |
3056 } | |
3057 di->di_tv.v_lock = 0; | |
3058 | |
3059 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
3060 { | |
3061 vim_free(di); | |
3062 Py_DECREF(list); | |
3063 Py_DECREF(litem); | |
3064 return -1; | |
3065 } | |
3066 if (dict_add(d, di) == FAIL) | |
3067 { | |
3068 vim_free(di); | |
3069 Py_DECREF(list); | |
3070 Py_DECREF(litem); | |
3071 PyErr_SetVim(_("failed to add key to dictionary")); | |
3072 return -1; | |
3073 } | |
3074 Py_DECREF(litem); | |
3075 } | |
3076 Py_DECREF(list); | |
3077 return 0; | |
3078 } | |
3079 | |
3080 static int | |
3081 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3082 { | |
3083 list_T *l; | |
3084 | |
3085 l = list_alloc(); | |
3086 if (l == NULL) | |
3087 { | |
3088 PyErr_NoMemory(); | |
3089 return -1; | |
3090 } | |
3091 | |
3092 tv->v_type = VAR_LIST; | |
3093 tv->vval.v_list = l; | |
3094 | |
3095 if (list_py_concat(l, obj, lookupDict) == -1) | |
3096 return -1; | |
3097 | |
3098 return 0; | |
3099 } | |
3100 | |
3101 static int | |
3102 pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3103 { | |
3104 PyObject *iterator = PyObject_GetIter(obj); | |
3105 PyObject *item; | |
3106 list_T *l; | |
3107 listitem_T *li; | |
3108 | |
3109 l = list_alloc(); | |
3110 | |
3111 if (l == NULL) | |
3112 { | |
3113 PyErr_NoMemory(); | |
3114 return -1; | |
3115 } | |
3116 | |
3117 tv->vval.v_list = l; | |
3118 tv->v_type = VAR_LIST; | |
3119 | |
3120 | |
3121 if (iterator == NULL) | |
3122 return -1; | |
3123 | |
3124 while ((item = PyIter_Next(obj))) | |
3125 { | |
3126 li = listitem_alloc(); | |
3127 if (li == NULL) | |
3128 { | |
3129 PyErr_NoMemory(); | |
3130 return -1; | |
3131 } | |
3132 li->li_tv.v_lock = 0; | |
3133 | |
3134 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1) | |
3135 return -1; | |
3136 | |
3137 list_append(l, li); | |
3138 | |
3139 Py_DECREF(item); | |
3140 } | |
3141 | |
3142 Py_DECREF(iterator); | |
3143 return 0; | |
3144 } | |
3145 | |
3618 | 3146 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
3147 | |
3148 static int | |
3149 convert_dl(PyObject *obj, typval_T *tv, | |
3150 pytotvfunc py_to_tv, PyObject *lookupDict) | |
3151 { | |
3152 PyObject *capsule; | |
3153 char hexBuf[sizeof(void *) * 2 + 3]; | |
3154 | |
3155 sprintf(hexBuf, "%p", obj); | |
3156 | |
3638 | 3157 # ifdef PY_USE_CAPSULE |
3618 | 3158 capsule = PyDict_GetItemString(lookupDict, hexBuf); |
3648 | 3159 # else |
3160 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf); | |
3161 # endif | |
3618 | 3162 if (capsule == NULL) |
3163 { | |
3638 | 3164 # ifdef PY_USE_CAPSULE |
3618 | 3165 capsule = PyCapsule_New(tv, NULL, NULL); |
3648 | 3166 # else |
3167 capsule = PyCObject_FromVoidPtr(tv, NULL); | |
3168 # endif | |
3618 | 3169 PyDict_SetItemString(lookupDict, hexBuf, capsule); |
3170 Py_DECREF(capsule); | |
3171 if (py_to_tv(obj, tv, lookupDict) == -1) | |
3172 { | |
3173 tv->v_type = VAR_UNKNOWN; | |
3174 return -1; | |
3175 } | |
3176 /* As we are not using copy_tv which increments reference count we must | |
3177 * do it ourself. */ | |
3178 switch(tv->v_type) | |
3179 { | |
3180 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; | |
3181 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; | |
3182 } | |
3183 } | |
3184 else | |
3185 { | |
3638 | 3186 typval_T *v; |
3187 | |
3188 # ifdef PY_USE_CAPSULE | |
3189 v = PyCapsule_GetPointer(capsule, NULL); | |
3190 # else | |
3648 | 3191 v = PyCObject_AsVoidPtr(capsule); |
3638 | 3192 # endif |
3618 | 3193 copy_tv(v, tv); |
3194 } | |
3195 return 0; | |
3196 } | |
3197 | |
3198 static int | |
3199 ConvertFromPyObject(PyObject *obj, typval_T *tv) | |
3200 { | |
3201 PyObject *lookup_dict; | |
3202 int r; | |
3203 | |
3204 lookup_dict = PyDict_New(); | |
3205 r = _ConvertFromPyObject(obj, tv, lookup_dict); | |
3206 Py_DECREF(lookup_dict); | |
3207 return r; | |
3208 } | |
3209 | |
3210 static int | |
3211 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3212 { | |
3213 if (obj->ob_type == &DictionaryType) | |
3214 { | |
3215 tv->v_type = VAR_DICT; | |
3216 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); | |
3217 ++tv->vval.v_dict->dv_refcount; | |
3218 } | |
3219 else if (obj->ob_type == &ListType) | |
3220 { | |
3221 tv->v_type = VAR_LIST; | |
3222 tv->vval.v_list = (((ListObject *)(obj))->list); | |
3223 ++tv->vval.v_list->lv_refcount; | |
3224 } | |
3225 else if (obj->ob_type == &FunctionType) | |
3226 { | |
3227 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) | |
3228 return -1; | |
3229 | |
3230 tv->v_type = VAR_FUNC; | |
3231 func_ref(tv->vval.v_string); | |
3232 } | |
3233 else if (PyBytes_Check(obj)) | |
3234 { | |
3800 | 3235 char_u *result; |
3236 | |
3237 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) | |
3238 return -1; | |
3618 | 3239 if (result == NULL) |
3240 return -1; | |
3241 | |
3242 if (set_string_copy(result, tv) == -1) | |
3243 return -1; | |
3244 | |
3245 tv->v_type = VAR_STRING; | |
3246 } | |
3247 else if (PyUnicode_Check(obj)) | |
3248 { | |
3249 PyObject *bytes; | |
3250 char_u *result; | |
3251 | |
3252 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); | |
3253 if (bytes == NULL) | |
3254 return -1; | |
3255 | |
3800 | 3256 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
3257 return -1; | |
3618 | 3258 if (result == NULL) |
3259 return -1; | |
3260 | |
3261 if (set_string_copy(result, tv) == -1) | |
3262 { | |
3263 Py_XDECREF(bytes); | |
3264 return -1; | |
3265 } | |
3266 Py_XDECREF(bytes); | |
3267 | |
3268 tv->v_type = VAR_STRING; | |
3269 } | |
4321 | 3270 #if PY_MAJOR_VERSION < 3 |
3618 | 3271 else if (PyInt_Check(obj)) |
3272 { | |
3273 tv->v_type = VAR_NUMBER; | |
3274 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); | |
3275 } | |
3276 #endif | |
3277 else if (PyLong_Check(obj)) | |
3278 { | |
3279 tv->v_type = VAR_NUMBER; | |
3280 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); | |
3281 } | |
3282 else if (PyDict_Check(obj)) | |
3283 return convert_dl(obj, tv, pydict_to_tv, lookupDict); | |
3284 #ifdef FEAT_FLOAT | |
3285 else if (PyFloat_Check(obj)) | |
3286 { | |
3287 tv->v_type = VAR_FLOAT; | |
3288 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); | |
3289 } | |
3290 #endif | |
3291 else if (PyIter_Check(obj)) | |
3292 return convert_dl(obj, tv, pyiter_to_tv, lookupDict); | |
3293 else if (PySequence_Check(obj)) | |
3294 return convert_dl(obj, tv, pyseq_to_tv, lookupDict); | |
3295 else if (PyMapping_Check(obj)) | |
3296 return convert_dl(obj, tv, pymap_to_tv, lookupDict); | |
3297 else | |
3298 { | |
3299 PyErr_SetString(PyExc_TypeError, _("unable to convert to vim structure")); | |
3300 return -1; | |
3301 } | |
3302 return 0; | |
3303 } | |
3304 | |
3305 static PyObject * | |
3306 ConvertToPyObject(typval_T *tv) | |
3307 { | |
3308 if (tv == NULL) | |
3309 { | |
3310 PyErr_SetVim(_("NULL reference passed")); | |
3311 return NULL; | |
3312 } | |
3313 switch (tv->v_type) | |
3314 { | |
3315 case VAR_STRING: | |
3852 | 3316 return PyBytes_FromString(tv->vval.v_string == NULL |
3317 ? "" : (char *)tv->vval.v_string); | |
3618 | 3318 case VAR_NUMBER: |
3319 return PyLong_FromLong((long) tv->vval.v_number); | |
3320 #ifdef FEAT_FLOAT | |
3321 case VAR_FLOAT: | |
3322 return PyFloat_FromDouble((double) tv->vval.v_float); | |
3323 #endif | |
3324 case VAR_LIST: | |
3325 return ListNew(tv->vval.v_list); | |
3326 case VAR_DICT: | |
3327 return DictionaryNew(tv->vval.v_dict); | |
3328 case VAR_FUNC: | |
3852 | 3329 return FunctionNew(tv->vval.v_string == NULL |
3330 ? (char_u *)"" : tv->vval.v_string); | |
3618 | 3331 case VAR_UNKNOWN: |
3332 Py_INCREF(Py_None); | |
3333 return Py_None; | |
3334 default: | |
3335 PyErr_SetVim(_("internal error: invalid value type")); | |
3336 return NULL; | |
3337 } | |
3338 } | |
4319 | 3339 |
3340 typedef struct | |
3341 { | |
3342 PyObject_HEAD | |
3343 } CurrentObject; | |
3344 static PyTypeObject CurrentType; | |
3345 | |
3346 static void | |
3347 init_structs(void) | |
3348 { | |
3349 vim_memset(&OutputType, 0, sizeof(OutputType)); | |
3350 OutputType.tp_name = "vim.message"; | |
3351 OutputType.tp_basicsize = sizeof(OutputObject); | |
3352 OutputType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3353 OutputType.tp_doc = "vim message object"; | |
3354 OutputType.tp_methods = OutputMethods; | |
3355 #if PY_MAJOR_VERSION >= 3 | |
3356 OutputType.tp_getattro = OutputGetattro; | |
3357 OutputType.tp_setattro = OutputSetattro; | |
3358 OutputType.tp_alloc = call_PyType_GenericAlloc; | |
3359 OutputType.tp_new = call_PyType_GenericNew; | |
3360 OutputType.tp_free = call_PyObject_Free; | |
3361 #else | |
3362 OutputType.tp_getattr = OutputGetattr; | |
3363 OutputType.tp_setattr = OutputSetattr; | |
3364 #endif | |
3365 | |
3366 vim_memset(&BufferType, 0, sizeof(BufferType)); | |
3367 BufferType.tp_name = "vim.buffer"; | |
3368 BufferType.tp_basicsize = sizeof(BufferType); | |
3369 BufferType.tp_dealloc = BufferDestructor; | |
3370 BufferType.tp_repr = BufferRepr; | |
3371 BufferType.tp_as_sequence = &BufferAsSeq; | |
3372 BufferType.tp_as_mapping = &BufferAsMapping; | |
3373 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3374 BufferType.tp_doc = "vim buffer object"; | |
3375 BufferType.tp_methods = BufferMethods; | |
3376 #if PY_MAJOR_VERSION >= 3 | |
3377 BufferType.tp_getattro = BufferGetattro; | |
3378 BufferType.tp_alloc = call_PyType_GenericAlloc; | |
3379 BufferType.tp_new = call_PyType_GenericNew; | |
3380 BufferType.tp_free = call_PyObject_Free; | |
3381 #else | |
3382 BufferType.tp_getattr = BufferGetattr; | |
3383 #endif | |
3384 | |
3385 vim_memset(&WindowType, 0, sizeof(WindowType)); | |
3386 WindowType.tp_name = "vim.window"; | |
3387 WindowType.tp_basicsize = sizeof(WindowObject); | |
3388 WindowType.tp_dealloc = WindowDestructor; | |
3389 WindowType.tp_repr = WindowRepr; | |
3390 WindowType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3391 WindowType.tp_doc = "vim Window object"; | |
3392 WindowType.tp_methods = WindowMethods; | |
3393 #if PY_MAJOR_VERSION >= 3 | |
3394 WindowType.tp_getattro = WindowGetattro; | |
3395 WindowType.tp_setattro = WindowSetattro; | |
3396 WindowType.tp_alloc = call_PyType_GenericAlloc; | |
3397 WindowType.tp_new = call_PyType_GenericNew; | |
3398 WindowType.tp_free = call_PyObject_Free; | |
3399 #else | |
3400 WindowType.tp_getattr = WindowGetattr; | |
3401 WindowType.tp_setattr = WindowSetattr; | |
3402 #endif | |
3403 | |
3404 vim_memset(&BufListType, 0, sizeof(BufListType)); | |
3405 BufListType.tp_name = "vim.bufferlist"; | |
3406 BufListType.tp_basicsize = sizeof(BufListObject); | |
3407 BufListType.tp_as_sequence = &BufListAsSeq; | |
3408 BufListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3409 BufferType.tp_doc = "vim buffer list"; | |
3410 | |
3411 vim_memset(&WinListType, 0, sizeof(WinListType)); | |
3412 WinListType.tp_name = "vim.windowlist"; | |
3413 WinListType.tp_basicsize = sizeof(WinListType); | |
3414 WinListType.tp_as_sequence = &WinListAsSeq; | |
3415 WinListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3416 WinListType.tp_doc = "vim window list"; | |
3417 | |
3418 vim_memset(&RangeType, 0, sizeof(RangeType)); | |
3419 RangeType.tp_name = "vim.range"; | |
3420 RangeType.tp_basicsize = sizeof(RangeObject); | |
3421 RangeType.tp_dealloc = RangeDestructor; | |
3422 RangeType.tp_repr = RangeRepr; | |
3423 RangeType.tp_as_sequence = &RangeAsSeq; | |
3424 RangeType.tp_as_mapping = &RangeAsMapping; | |
3425 RangeType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3426 RangeType.tp_doc = "vim Range object"; | |
3427 RangeType.tp_methods = RangeMethods; | |
3428 #if PY_MAJOR_VERSION >= 3 | |
3429 RangeType.tp_getattro = RangeGetattro; | |
3430 RangeType.tp_alloc = call_PyType_GenericAlloc; | |
3431 RangeType.tp_new = call_PyType_GenericNew; | |
3432 RangeType.tp_free = call_PyObject_Free; | |
3433 #else | |
3434 RangeType.tp_getattr = RangeGetattr; | |
3435 #endif | |
3436 | |
3437 vim_memset(&CurrentType, 0, sizeof(CurrentType)); | |
3438 CurrentType.tp_name = "vim.currentdata"; | |
3439 CurrentType.tp_basicsize = sizeof(CurrentObject); | |
3440 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3441 CurrentType.tp_doc = "vim current object"; | |
3442 #if PY_MAJOR_VERSION >= 3 | |
3443 CurrentType.tp_getattro = CurrentGetattro; | |
3444 CurrentType.tp_setattro = CurrentSetattro; | |
3445 #else | |
3446 CurrentType.tp_getattr = CurrentGetattr; | |
3447 CurrentType.tp_setattr = CurrentSetattr; | |
3448 #endif | |
3449 | |
3450 vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); | |
3451 DictionaryType.tp_name = "vim.dictionary"; | |
3452 DictionaryType.tp_basicsize = sizeof(DictionaryObject); | |
3453 DictionaryType.tp_dealloc = DictionaryDestructor; | |
3454 DictionaryType.tp_as_mapping = &DictionaryAsMapping; | |
3455 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3456 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; | |
3457 DictionaryType.tp_methods = DictionaryMethods; | |
3458 #if PY_MAJOR_VERSION >= 3 | |
3459 DictionaryType.tp_getattro = DictionaryGetattro; | |
3460 DictionaryType.tp_setattro = DictionarySetattro; | |
3461 #else | |
3462 DictionaryType.tp_getattr = DictionaryGetattr; | |
3463 DictionaryType.tp_setattr = DictionarySetattr; | |
3464 #endif | |
3465 | |
3466 vim_memset(&ListType, 0, sizeof(ListType)); | |
3467 ListType.tp_name = "vim.list"; | |
3468 ListType.tp_dealloc = ListDestructor; | |
3469 ListType.tp_basicsize = sizeof(ListObject); | |
3470 ListType.tp_as_sequence = &ListAsSeq; | |
3471 ListType.tp_as_mapping = &ListAsMapping; | |
3472 ListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3473 ListType.tp_doc = "list pushing modifications to vim structure"; | |
3474 ListType.tp_methods = ListMethods; | |
3475 #if PY_MAJOR_VERSION >= 3 | |
3476 ListType.tp_getattro = ListGetattro; | |
3477 ListType.tp_setattro = ListSetattro; | |
3478 #else | |
3479 ListType.tp_getattr = ListGetattr; | |
3480 ListType.tp_setattr = ListSetattr; | |
3481 #endif | |
3482 | |
3483 vim_memset(&FunctionType, 0, sizeof(FunctionType)); | |
3484 FunctionType.tp_name = "vim.list"; | |
3485 FunctionType.tp_basicsize = sizeof(FunctionObject); | |
3486 FunctionType.tp_dealloc = FunctionDestructor; | |
3487 FunctionType.tp_call = FunctionCall; | |
3488 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3489 FunctionType.tp_doc = "object that calls vim function"; | |
3490 FunctionType.tp_methods = FunctionMethods; | |
3491 #if PY_MAJOR_VERSION >= 3 | |
3492 FunctionType.tp_getattro = FunctionGetattro; | |
3493 #else | |
3494 FunctionType.tp_getattr = FunctionGetattr; | |
3495 #endif | |
3496 | |
4350 | 3497 vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
3498 OptionsType.tp_name = "vim.options"; | |
3499 OptionsType.tp_basicsize = sizeof(OptionsObject); | |
3500 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT; | |
3501 OptionsType.tp_doc = "object for manipulating options"; | |
3502 OptionsType.tp_as_mapping = &OptionsAsMapping; | |
3503 OptionsType.tp_dealloc = OptionsDestructor; | |
3504 | |
4319 | 3505 #if PY_MAJOR_VERSION >= 3 |
3506 vim_memset(&vimmodule, 0, sizeof(vimmodule)); | |
3507 vimmodule.m_name = "vim"; | |
3508 vimmodule.m_doc = "Vim Python interface\n"; | |
3509 vimmodule.m_size = -1; | |
3510 vimmodule.m_methods = VimMethods; | |
3511 #endif | |
3512 } |