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