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