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