Mercurial > vim
annotate src/if_py_both.h @ 4589:fa39483a1363 v7.3.1042
updated for version 7.3.1042
Problem: Python: can't assign to vim.Buffer.name.
Solution: Python patch 3. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 29 May 2013 22:02:22 +0200 |
parents | 63c9b681c3db |
children | 0cf552b325b5 |
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 * | |
4488 | 1818 TabPageAttr(TabPageObject *self, char *name) |
4401 | 1819 { |
1820 if (strcmp(name, "windows") == 0) | |
4488 | 1821 return WinListNew(self); |
4401 | 1822 else if (strcmp(name, "number") == 0) |
4488 | 1823 return PyLong_FromLong((long) get_tab_number(self->tab)); |
4401 | 1824 else if (strcmp(name, "vars") == 0) |
4488 | 1825 return DictionaryNew(self->tab->tp_vars); |
4401 | 1826 else if (strcmp(name, "window") == 0) |
1827 { | |
1828 /* For current tab window.c does not bother to set or update tp_curwin | |
1829 */ | |
4488 | 1830 if (self->tab == curtab) |
4431 | 1831 return WindowNew(curwin, curtab); |
4401 | 1832 else |
4488 | 1833 return WindowNew(self->tab->tp_curwin, self->tab); |
4401 | 1834 } |
1835 return NULL; | |
1836 } | |
1837 | |
1838 static PyObject * | |
4488 | 1839 TabPageRepr(TabPageObject *self) |
4401 | 1840 { |
1841 static char repr[100]; | |
4488 | 1842 |
1843 if (self->tab == INVALID_TABPAGE_VALUE) | |
4401 | 1844 { |
1845 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self)); | |
1846 return PyString_FromString(repr); | |
1847 } | |
1848 else | |
1849 { | |
4488 | 1850 int t = get_tab_number(self->tab); |
4401 | 1851 |
1852 if (t == 0) | |
1853 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"), | |
1854 (self)); | |
1855 else | |
1856 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1); | |
1857 | |
1858 return PyString_FromString(repr); | |
1859 } | |
1860 } | |
1861 | |
1862 static struct PyMethodDef TabPageMethods[] = { | |
1863 /* name, function, calling, documentation */ | |
1864 { NULL, NULL, 0, NULL } | |
1865 }; | |
1866 | |
1867 /* | |
1868 * Window list object | |
1869 */ | |
1870 | |
1871 static PyTypeObject TabListType; | |
1872 static PySequenceMethods TabListAsSeq; | |
1873 | |
1874 typedef struct | |
1875 { | |
1876 PyObject_HEAD | |
1877 } TabListObject; | |
1878 | |
1879 static PyInt | |
1880 TabListLength(PyObject *self UNUSED) | |
1881 { | |
1882 tabpage_T *tp = first_tabpage; | |
1883 PyInt n = 0; | |
1884 | |
1885 while (tp != NULL) | |
1886 { | |
1887 ++n; | |
1888 tp = tp->tp_next; | |
1889 } | |
1890 | |
1891 return n; | |
1892 } | |
1893 | |
1894 static PyObject * | |
1895 TabListItem(PyObject *self UNUSED, PyInt n) | |
1896 { | |
1897 tabpage_T *tp; | |
1898 | |
1899 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n) | |
1900 if (n == 0) | |
1901 return TabPageNew(tp); | |
1902 | |
1903 PyErr_SetString(PyExc_IndexError, _("no such tab page")); | |
1904 return NULL; | |
1905 } | |
1906 | |
4385 | 1907 /* Window object |
1908 */ | |
1909 | |
1910 typedef struct | |
1911 { | |
1912 PyObject_HEAD | |
1913 win_T *win; | |
4431 | 1914 TabPageObject *tabObject; |
4385 | 1915 } WindowObject; |
1916 | |
1917 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
|
1918 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1919 static int |
4488 | 1920 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
|
1921 { |
4488 | 1922 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
|
1923 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1924 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
|
1925 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1926 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1927 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1928 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1929 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1930 |
4319 | 1931 static PyObject * |
4431 | 1932 WindowNew(win_T *win, tabpage_T *tab) |
4377 | 1933 { |
1934 /* We need to handle deletion of windows underneath us. | |
1935 * If we add a "w_python*_ref" field to the win_T structure, | |
1936 * then we can get at it in win_free() in vim. We then | |
1937 * need to create only ONE Python object per window - if | |
1938 * we try to create a second, just INCREF the existing one | |
1939 * and return it. The (single) Python object referring to | |
1940 * the window is stored in "w_python*_ref". | |
1941 * On a win_free() we set the Python object's win_T* field | |
1942 * to an invalid value. We trap all uses of a window | |
1943 * object, and reject them if the win_T* field is invalid. | |
1944 * | |
4385 | 1945 * Python2 and Python3 get different fields and different objects: |
4377 | 1946 * w_python_ref and w_python3_ref fields respectively. |
1947 */ | |
1948 | |
1949 WindowObject *self; | |
1950 | |
1951 if (WIN_PYTHON_REF(win)) | |
1952 { | |
1953 self = WIN_PYTHON_REF(win); | |
1954 Py_INCREF(self); | |
1955 } | |
1956 else | |
1957 { | |
4500 | 1958 self = PyObject_GC_New(WindowObject, &WindowType); |
4377 | 1959 if (self == NULL) |
1960 return NULL; | |
1961 self->win = win; | |
1962 WIN_PYTHON_REF(win) = self; | |
1963 } | |
1964 | |
4431 | 1965 self->tabObject = ((TabPageObject *)(TabPageNew(tab))); |
1966 | |
4377 | 1967 return (PyObject *)(self); |
1968 } | |
1969 | |
4385 | 1970 static void |
4488 | 1971 WindowDestructor(WindowObject *self) |
4385 | 1972 { |
4500 | 1973 PyObject_GC_UnTrack((void *)(self)); |
4488 | 1974 if (self->win && self->win != INVALID_WINDOW_VALUE) |
1975 WIN_PYTHON_REF(self->win) = NULL; | |
4500 | 1976 Py_XDECREF(((PyObject *)(self->tabObject))); |
1977 PyObject_GC_Del((void *)(self)); | |
1978 } | |
1979 | |
1980 static int | |
1981 WindowTraverse(WindowObject *self, visitproc visit, void *arg) | |
1982 { | |
1983 Py_VISIT(((PyObject *)(self->tabObject))); | |
1984 return 0; | |
1985 } | |
1986 | |
1987 static int | |
1988 WindowClear(WindowObject *self) | |
1989 { | |
1990 Py_CLEAR(self->tabObject); | |
1991 return 0; | |
4385 | 1992 } |
1993 | |
4431 | 1994 static win_T * |
1995 get_firstwin(TabPageObject *tabObject) | |
1996 { | |
1997 if (tabObject) | |
1998 { | |
1999 if (CheckTabPage(tabObject)) | |
2000 return NULL; | |
2001 /* For current tab window.c does not bother to set or update tp_firstwin | |
2002 */ | |
2003 else if (tabObject->tab == curtab) | |
2004 return firstwin; | |
2005 else | |
2006 return tabObject->tab->tp_firstwin; | |
2007 } | |
2008 else | |
2009 return firstwin; | |
2010 } | |
2011 | |
4377 | 2012 static PyObject * |
4488 | 2013 WindowAttr(WindowObject *self, char *name) |
4319 | 2014 { |
2015 if (strcmp(name, "buffer") == 0) | |
4488 | 2016 return (PyObject *)BufferNew(self->win->w_buffer); |
4319 | 2017 else if (strcmp(name, "cursor") == 0) |
2018 { | |
4488 | 2019 pos_T *pos = &self->win->w_cursor; |
4319 | 2020 |
2021 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); | |
2022 } | |
2023 else if (strcmp(name, "height") == 0) | |
4488 | 2024 return PyLong_FromLong((long)(self->win->w_height)); |
4383 | 2025 #ifdef FEAT_WINDOWS |
2026 else if (strcmp(name, "row") == 0) | |
4488 | 2027 return PyLong_FromLong((long)(self->win->w_winrow)); |
4383 | 2028 #endif |
4319 | 2029 #ifdef FEAT_VERTSPLIT |
2030 else if (strcmp(name, "width") == 0) | |
4488 | 2031 return PyLong_FromLong((long)(W_WIDTH(self->win))); |
4383 | 2032 else if (strcmp(name, "col") == 0) |
4488 | 2033 return PyLong_FromLong((long)(W_WINCOL(self->win))); |
4319 | 2034 #endif |
4323 | 2035 else if (strcmp(name, "vars") == 0) |
4488 | 2036 return DictionaryNew(self->win->w_vars); |
4350 | 2037 else if (strcmp(name, "options") == 0) |
4488 | 2038 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow, |
2039 (PyObject *) self); | |
4379 | 2040 else if (strcmp(name, "number") == 0) |
4431 | 2041 { |
4488 | 2042 if (CheckTabPage(self->tabObject)) |
4431 | 2043 return NULL; |
2044 return PyLong_FromLong((long) | |
4488 | 2045 get_win_number(self->win, get_firstwin(self->tabObject))); |
4431 | 2046 } |
2047 else if (strcmp(name, "tabpage") == 0) | |
2048 { | |
4488 | 2049 Py_INCREF(self->tabObject); |
2050 return (PyObject *)(self->tabObject); | |
4431 | 2051 } |
4319 | 2052 else if (strcmp(name,"__members__") == 0) |
4431 | 2053 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height", |
2054 "vars", "options", "number", "row", "col", "tabpage"); | |
4319 | 2055 else |
2056 return NULL; | |
2057 } | |
2058 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2059 static int |
4488 | 2060 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
|
2061 { |
4488 | 2062 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
|
2063 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2064 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2065 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
|
2066 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2067 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
|
2068 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2069 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2070 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
|
2071 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2072 long lnum; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2073 long col; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2074 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2075 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
|
2076 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2077 |
4488 | 2078 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
|
2079 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2080 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
|
2081 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2082 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2083 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2084 /* Check for keyboard interrupts */ |
4498 | 2085 if (VimCheckInterrupt()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2086 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2087 |
4488 | 2088 self->win->w_cursor.lnum = lnum; |
2089 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
|
2090 #ifdef FEAT_VIRTUALEDIT |
4488 | 2091 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
|
2092 #endif |
2933 | 2093 /* When column is out of range silently correct it. */ |
4488 | 2094 check_cursor_col_win(self->win); |
2933 | 2095 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2096 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2097 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2098 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2099 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
|
2100 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2101 int height; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2102 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2103 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2104 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
|
2105 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2106 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2107 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2108 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
|
2109 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2110 savewin = curwin; |
4488 | 2111 curwin = self->win; |
4498 | 2112 |
2113 VimTryStart(); | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2114 win_setheight(height); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2115 curwin = savewin; |
4498 | 2116 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2117 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2118 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2119 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2120 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2121 #ifdef FEAT_VERTSPLIT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2122 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
|
2123 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2124 int width; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2125 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2126 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2127 if (!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
|
2128 return -1; |
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 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2131 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
|
2132 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2133 savewin = curwin; |
4488 | 2134 curwin = self->win; |
4498 | 2135 |
2136 VimTryStart(); | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2137 win_setwidth(width); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2138 curwin = savewin; |
4498 | 2139 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2140 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2141 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2142 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2143 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2144 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2145 else |
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 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
|
2148 return -1; |
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 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2151 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2152 static PyObject * |
4488 | 2153 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
|
2154 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2155 static char repr[100]; |
4488 | 2156 |
2157 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
|
2158 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2159 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
|
2160 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
|
2161 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2162 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2163 { |
4488 | 2164 int w = get_win_number(self->win, firstwin); |
4379 | 2165 |
2166 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
|
2167 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
|
2168 (self)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2169 else |
4379 | 2170 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
|
2171 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2172 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
|
2173 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2174 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2175 |
4385 | 2176 static struct PyMethodDef WindowMethods[] = { |
2177 /* name, function, calling, documentation */ | |
2178 { NULL, NULL, 0, NULL } | |
2179 }; | |
2180 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2181 /* |
4385 | 2182 * 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
|
2183 */ |
4319 | 2184 |
4385 | 2185 static PyTypeObject WinListType; |
2186 static PySequenceMethods WinListAsSeq; | |
2187 | |
4319 | 2188 typedef struct |
2189 { | |
2190 PyObject_HEAD | |
4401 | 2191 TabPageObject *tabObject; |
4319 | 2192 } WinListObject; |
2193 | |
4401 | 2194 static PyObject * |
2195 WinListNew(TabPageObject *tabObject) | |
2196 { | |
2197 WinListObject *self; | |
2198 | |
2199 self = PyObject_NEW(WinListObject, &WinListType); | |
2200 self->tabObject = tabObject; | |
2201 Py_INCREF(tabObject); | |
2202 | |
2203 return (PyObject *)(self); | |
2204 } | |
2205 | |
2206 static void | |
4488 | 2207 WinListDestructor(WinListObject *self) |
4401 | 2208 { |
4488 | 2209 TabPageObject *tabObject = self->tabObject; |
4401 | 2210 |
2211 if (tabObject) | |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
2212 { |
4401 | 2213 Py_DECREF((PyObject *)(tabObject)); |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
2214 } |
4401 | 2215 |
2216 DESTRUCTOR_FINISH(self); | |
2217 } | |
2218 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2219 static PyInt |
4488 | 2220 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
|
2221 { |
4401 | 2222 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
|
2223 PyInt n = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2224 |
4488 | 2225 if (!(w = get_firstwin(self->tabObject))) |
4401 | 2226 return -1; |
2227 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2228 while (w != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2229 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2230 ++n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2231 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
|
2232 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2233 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2234 return n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2235 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2236 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2237 static PyObject * |
4488 | 2238 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
|
2239 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2240 win_T *w; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2241 |
4488 | 2242 if (!(w = get_firstwin(self->tabObject))) |
4401 | 2243 return NULL; |
2244 | |
2245 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
|
2246 if (n == 0) |
4488 | 2247 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
|
2248 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2249 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
|
2250 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2251 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2252 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2253 /* 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
|
2254 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2255 * 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
|
2256 * 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
|
2257 * characters. |
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 * 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
|
2260 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2261 static char * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2262 StringToLine(PyObject *obj) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2263 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2264 const char *str; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2265 char *save; |
2894 | 2266 PyObject *bytes; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2267 PyInt len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2268 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2269 char *p; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2270 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2271 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
|
2272 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2273 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2274 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2275 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2276 |
2894 | 2277 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
2278 str = PyString_AsString(bytes); | |
2279 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
|
2280 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2281 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2282 * 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
|
2283 * 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
|
2284 * a single line. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2285 * 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
|
2286 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2287 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
|
2288 if (p != NULL) |
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 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
|
2291 --len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2292 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2293 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2294 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
|
2295 return NULL; |
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 } |
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 /* 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
|
2300 * 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
|
2301 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2302 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
|
2303 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2304 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2305 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2306 return NULL; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2309 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
|
2310 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2311 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
|
2312 save[i] = '\n'; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2313 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2314 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
|
2315 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2316 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2317 save[i] = '\0'; |
2894 | 2318 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
|
2319 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2320 return save; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2321 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2322 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2323 /* 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
|
2324 * 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
|
2325 * string object. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2326 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2327 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2328 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
|
2329 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2330 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
|
2331 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2332 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2333 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2334 /* 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
|
2335 * 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
|
2336 * 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
|
2337 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2338 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2339 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
|
2340 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2341 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2342 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
|
2343 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
|
2344 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2345 if (list == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2346 return NULL; |
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 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
|
2349 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2350 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
|
2351 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2352 /* 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
|
2353 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2354 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2355 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2356 return NULL; |
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 /* 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
|
2360 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
|
2361 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2362 Py_DECREF(str); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2363 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2364 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2365 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2366 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2367 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2368 /* 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
|
2369 * 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
|
2370 * with it). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2371 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2372 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2373 return list; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2374 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2375 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2376 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2377 * 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
|
2378 * 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
|
2379 * deleted). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2380 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2381 static void |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2382 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
|
2383 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2384 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
|
2385 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2386 /* 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
|
2387 * lines. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2388 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
|
2389 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2390 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
|
2391 check_cursor_col(); |
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 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
|
2394 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2395 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
|
2396 check_cursor(); |
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 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2399 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2400 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
|
2401 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2402 invalidate_botline(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2403 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2404 |
2894 | 2405 /* |
2406 * 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
|
2407 * 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
|
2408 * 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
|
2409 * 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
|
2410 * 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
|
2411 * 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
|
2412 * 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
|
2413 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2414 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2415 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
|
2416 { |
4352 | 2417 /* 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
|
2418 * 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
|
2419 * 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
|
2420 * 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
|
2421 * 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
|
2422 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2423 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
|
2424 { |
4429 | 2425 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
|
2426 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2427 PyErr_Clear(); |
4429 | 2428 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
|
2429 |
4498 | 2430 VimTryStart(); |
2431 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2432 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
|
2433 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
|
2434 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
|
2435 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
|
2436 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2437 { |
4429 | 2438 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
|
2439 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
|
2440 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
|
2441 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2442 |
4429 | 2443 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
|
2444 |
4498 | 2445 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2446 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2447 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2448 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2449 *len_change = -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2450 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2451 return OK; |
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 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
|
2454 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2455 char *save = StringToLine(line); |
4429 | 2456 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
|
2457 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2458 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2459 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2460 |
4498 | 2461 VimTryStart(); |
2462 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2463 /* 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
|
2464 PyErr_Clear(); |
4429 | 2465 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
|
2466 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2467 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
|
2468 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2469 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
|
2470 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2471 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2472 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
|
2473 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2474 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
|
2475 vim_free(save); |
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 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2478 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
|
2479 |
4429 | 2480 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
|
2481 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2482 /* Check that the cursor is not beyond the end of the line now. */ |
4429 | 2483 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
|
2484 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2485 |
4498 | 2486 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2487 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2488 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2489 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2490 *len_change = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2491 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2492 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2493 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2494 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2495 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2496 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2497 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2498 } |
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 |
2894 | 2501 /* Replace a range of lines in the specified buffer. The line numbers are in |
2502 * Vim format (1-based). The range is from lo up to, but not including, hi. | |
2503 * The replacement lines are given as a Python list of string objects. The | |
2504 * list is checked for validity and correct format. Errors are returned as a | |
2505 * value of FAIL. The return value is OK on success. | |
2506 * If OK is returned and len_change is not NULL, *len_change | |
2507 * is set to the change in the buffer length. | |
2508 */ | |
2509 static int | |
2510 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) | |
2511 { | |
4352 | 2512 /* First of all, we check the type of the supplied Python object. |
2894 | 2513 * There are three cases: |
2514 * 1. NULL, or None - this is a deletion. | |
2515 * 2. A list - this is a replacement. | |
2516 * 3. Anything else - this is an error. | |
2517 */ | |
2518 if (list == Py_None || list == NULL) | |
2519 { | |
2520 PyInt i; | |
2521 PyInt n = (int)(hi - lo); | |
4429 | 2522 buf_T *savebuf; |
2894 | 2523 |
2524 PyErr_Clear(); | |
4498 | 2525 VimTryStart(); |
4429 | 2526 switch_buffer(&savebuf, buf); |
2894 | 2527 |
2528 if (u_savedel((linenr_T)lo, (long)n) == FAIL) | |
2529 PyErr_SetVim(_("cannot save undo information")); | |
2530 else | |
2531 { | |
2532 for (i = 0; i < n; ++i) | |
2533 { | |
2534 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2535 { | |
2536 PyErr_SetVim(_("cannot delete line")); | |
2537 break; | |
2538 } | |
2539 } | |
4429 | 2540 if (buf == savebuf) |
2894 | 2541 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
2542 deleted_lines_mark((linenr_T)lo, (long)i); | |
2543 } | |
2544 | |
4429 | 2545 restore_buffer(savebuf); |
2894 | 2546 |
4498 | 2547 if (VimTryEnd()) |
2894 | 2548 return FAIL; |
2549 | |
2550 if (len_change) | |
2551 *len_change = -n; | |
2552 | |
2553 return OK; | |
2554 } | |
2555 else if (PyList_Check(list)) | |
2556 { | |
2557 PyInt i; | |
2558 PyInt new_len = PyList_Size(list); | |
2559 PyInt old_len = hi - lo; | |
2560 PyInt extra = 0; /* lines added to text, can be negative */ | |
2561 char **array; | |
2562 buf_T *savebuf; | |
2563 | |
2564 if (new_len == 0) /* avoid allocating zero bytes */ | |
2565 array = NULL; | |
2566 else | |
2567 { | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2568 array = PyMem_New(char *, new_len); |
2894 | 2569 if (array == NULL) |
2570 { | |
2571 PyErr_NoMemory(); | |
2572 return FAIL; | |
2573 } | |
2574 } | |
2575 | |
2576 for (i = 0; i < new_len; ++i) | |
2577 { | |
2578 PyObject *line = PyList_GetItem(list, i); | |
2579 | |
2580 array[i] = StringToLine(line); | |
2581 if (array[i] == NULL) | |
2582 { | |
2583 while (i) | |
2584 vim_free(array[--i]); | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2585 PyMem_Free(array); |
2894 | 2586 return FAIL; |
2587 } | |
2588 } | |
2589 | |
4498 | 2590 VimTryStart(); |
2894 | 2591 PyErr_Clear(); |
4429 | 2592 |
2593 // START of region without "return". Must call restore_buffer()! | |
2594 switch_buffer(&savebuf, buf); | |
2894 | 2595 |
2596 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) | |
2597 PyErr_SetVim(_("cannot save undo information")); | |
2598 | |
2599 /* If the size of the range is reducing (ie, new_len < old_len) we | |
2600 * need to delete some old_len. We do this at the start, by | |
2601 * repeatedly deleting line "lo". | |
2602 */ | |
2603 if (!PyErr_Occurred()) | |
2604 { | |
2605 for (i = 0; i < old_len - new_len; ++i) | |
2606 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2607 { | |
2608 PyErr_SetVim(_("cannot delete line")); | |
2609 break; | |
2610 } | |
2611 extra -= i; | |
2612 } | |
2613 | |
2614 /* For as long as possible, replace the existing old_len with the | |
2615 * new old_len. This is a more efficient operation, as it requires | |
2616 * less memory allocation and freeing. | |
2617 */ | |
2618 if (!PyErr_Occurred()) | |
2619 { | |
2620 for (i = 0; i < old_len && i < new_len; ++i) | |
2621 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) | |
2622 == FAIL) | |
2623 { | |
2624 PyErr_SetVim(_("cannot replace line")); | |
2625 break; | |
2626 } | |
2627 } | |
2628 else | |
2629 i = 0; | |
2630 | |
2631 /* Now we may need to insert the remaining new old_len. If we do, we | |
2632 * must free the strings as we finish with them (we can't pass the | |
2633 * responsibility to vim in this case). | |
2634 */ | |
2635 if (!PyErr_Occurred()) | |
2636 { | |
2637 while (i < new_len) | |
2638 { | |
2639 if (ml_append((linenr_T)(lo + i - 1), | |
2640 (char_u *)array[i], 0, FALSE) == FAIL) | |
2641 { | |
2642 PyErr_SetVim(_("cannot insert line")); | |
2643 break; | |
2644 } | |
2645 vim_free(array[i]); | |
2646 ++i; | |
2647 ++extra; | |
2648 } | |
2649 } | |
2650 | |
2651 /* Free any left-over old_len, as a result of an error */ | |
2652 while (i < new_len) | |
2653 { | |
2654 vim_free(array[i]); | |
2655 ++i; | |
2656 } | |
2657 | |
2658 /* Free the array of old_len. All of its contents have now | |
2659 * been dealt with (either freed, or the responsibility passed | |
2660 * to vim. | |
2661 */ | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2662 PyMem_Free(array); |
2894 | 2663 |
2664 /* Adjust marks. Invalidate any which lie in the | |
2665 * changed range, and move any in the remainder of the buffer. | |
2666 */ | |
2667 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), | |
2668 (long)MAXLNUM, (long)extra); | |
2669 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); | |
2670 | |
4429 | 2671 if (buf == savebuf) |
2894 | 2672 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
2673 | |
4429 | 2674 // END of region without "return". |
2675 restore_buffer(savebuf); | |
2894 | 2676 |
4498 | 2677 if (VimTryEnd()) |
2894 | 2678 return FAIL; |
2679 | |
2680 if (len_change) | |
2681 *len_change = new_len - old_len; | |
2682 | |
2683 return OK; | |
2684 } | |
2685 else | |
2686 { | |
2687 PyErr_BadArgument(); | |
2688 return FAIL; | |
2689 } | |
2690 } | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2691 |
4352 | 2692 /* 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
|
2693 * 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
|
2694 * 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
|
2695 * 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
|
2696 * 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
|
2697 * 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
|
2698 * 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
|
2699 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2700 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2701 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
|
2702 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2703 /* 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
|
2704 * 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
|
2705 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2706 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
|
2707 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2708 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
|
2709 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2710 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2711 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2712 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2713 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2714 PyErr_Clear(); |
4498 | 2715 VimTryStart(); |
4429 | 2716 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
|
2717 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2718 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
|
2719 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
|
2720 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
|
2721 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
|
2722 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2723 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
|
2724 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2725 vim_free(str); |
4429 | 2726 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
|
2727 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2728 |
4498 | 2729 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2730 return FAIL; |
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 (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2733 *len_change = 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2734 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2735 return OK; |
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 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
|
2738 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2739 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2740 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
|
2741 char **array; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2742 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2743 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2744 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
|
2745 if (array == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2746 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2747 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2748 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2749 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2750 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2751 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
|
2752 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2753 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
|
2754 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
|
2755 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2756 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
|
2757 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2758 while (i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2759 vim_free(array[--i]); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2760 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
|
2761 return FAIL; |
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 } |
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 PyErr_Clear(); |
4498 | 2766 VimTryStart(); |
4429 | 2767 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
|
2768 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2769 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
|
2770 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
|
2771 else |
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 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
|
2774 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2775 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
|
2776 (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
|
2777 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2778 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
|
2779 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2780 /* 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
|
2781 while (i < size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2782 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
|
2783 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2784 break; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2785 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2786 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
|
2787 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2788 if (i > 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2789 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
|
2790 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2791 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2792 /* 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
|
2793 * been freed. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2794 */ |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2795 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
|
2796 |
4429 | 2797 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
|
2798 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2799 |
4498 | 2800 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2801 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2802 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2803 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2804 *len_change = size; |
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 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2807 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2808 else |
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 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2811 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2812 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2813 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2814 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2815 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2816 * 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
|
2817 * ------------------------------------------- |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2818 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2819 |
4385 | 2820 typedef struct |
2821 { | |
2822 PyObject_HEAD | |
2823 buf_T *buf; | |
2824 } BufferObject; | |
2825 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2826 static int |
4488 | 2827 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
|
2828 { |
4488 | 2829 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
|
2830 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2831 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
|
2832 return -1; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2835 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2836 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2837 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2838 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2839 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
|
2840 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2841 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2842 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2843 |
4387 | 2844 if (end == -1) |
2845 end = self->buf->b_ml.ml_line_count; | |
2846 | |
4389 | 2847 if (n < 0) |
2848 n += end - start + 1; | |
2849 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2850 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
|
2851 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2852 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
|
2853 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2854 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2855 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2856 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
|
2857 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2858 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2859 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2860 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
|
2861 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2862 PyInt size; |
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 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2865 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2866 |
4387 | 2867 if (end == -1) |
2868 end = self->buf->b_ml.ml_line_count; | |
2869 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2870 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
|
2871 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2872 if (lo < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2873 lo = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2874 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
|
2875 lo = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2876 if (hi < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2877 hi = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2878 if (hi < lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2879 hi = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2880 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
|
2881 hi = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2882 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2883 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
|
2884 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2885 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2886 static PyInt |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2887 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
|
2888 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2889 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2890 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2891 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2892 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2893 |
4387 | 2894 if (end == -1) |
2895 end = self->buf->b_ml.ml_line_count; | |
2896 | |
4389 | 2897 if (n < 0) |
2898 n += end - start + 1; | |
2899 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2900 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
|
2901 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2902 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
|
2903 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2904 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2905 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2906 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
|
2907 return -1; |
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 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2910 *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
|
2911 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2912 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2913 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2914 |
2894 | 2915 static PyInt |
2916 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) | |
2917 { | |
2918 PyInt size; | |
2919 PyInt len_change; | |
2920 | |
2921 /* Self must be a valid buffer */ | |
2922 if (CheckBuffer(self)) | |
2923 return -1; | |
2924 | |
4387 | 2925 if (end == -1) |
2926 end = self->buf->b_ml.ml_line_count; | |
2927 | |
2894 | 2928 /* Sort out the slice range */ |
2929 size = end - start + 1; | |
2930 | |
2931 if (lo < 0) | |
2932 lo = 0; | |
2933 else if (lo > size) | |
2934 lo = size; | |
2935 if (hi < 0) | |
2936 hi = 0; | |
2937 if (hi < lo) | |
2938 hi = lo; | |
2939 else if (hi > size) | |
2940 hi = size; | |
2941 | |
2942 if (SetBufferLineList(self->buf, lo + start, hi + start, | |
2943 val, &len_change) == FAIL) | |
2944 return -1; | |
2945 | |
2946 if (new_end) | |
2947 *new_end = end + len_change; | |
2948 | |
2949 return 0; | |
2950 } | |
2951 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2952 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2953 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2954 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
|
2955 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2956 PyObject *lines; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2957 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2958 PyInt max; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2959 PyInt n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2960 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2961 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2962 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2963 |
4387 | 2964 if (end == -1) |
2965 end = self->buf->b_ml.ml_line_count; | |
2966 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2967 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
|
2968 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2969 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
|
2970 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2971 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2972 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
|
2973 { |
4405 | 2974 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
|
2975 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2976 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2977 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2978 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
|
2979 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2980 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2981 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2982 *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
|
2983 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2984 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
|
2985 return Py_None; |
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 |
4385 | 2988 /* Range object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2989 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2990 |
4319 | 2991 static PyTypeObject RangeType; |
4385 | 2992 static PySequenceMethods RangeAsSeq; |
2993 static PyMappingMethods RangeAsMapping; | |
4319 | 2994 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2995 typedef struct |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2996 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2997 PyObject_HEAD |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2998 BufferObject *buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2999 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3000 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3001 } RangeObject; |
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 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3004 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
|
3005 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3006 BufferObject *bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3007 RangeObject *self; |
4500 | 3008 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
|
3009 if (self == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3010 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3011 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3012 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
|
3013 if (bufr == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3014 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3015 Py_DECREF(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3016 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3017 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3018 Py_INCREF(bufr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3019 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3020 self->buf = bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3021 self->start = start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3022 self->end = end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3023 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3024 return (PyObject *)(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3025 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3026 |
4319 | 3027 static void |
4488 | 3028 RangeDestructor(RangeObject *self) |
4319 | 3029 { |
4500 | 3030 PyObject_GC_UnTrack((void *)(self)); |
4498 | 3031 Py_XDECREF(self->buf); |
4500 | 3032 PyObject_GC_Del((void *)(self)); |
3033 } | |
3034 | |
3035 static int | |
3036 RangeTraverse(RangeObject *self, visitproc visit, void *arg) | |
3037 { | |
3038 Py_VISIT(((PyObject *)(self->buf))); | |
3039 return 0; | |
3040 } | |
3041 | |
3042 static int | |
3043 RangeClear(RangeObject *self) | |
3044 { | |
3045 Py_CLEAR(self->buf); | |
3046 return 0; | |
4319 | 3047 } |
3048 | |
4385 | 3049 static PyInt |
4488 | 3050 RangeLength(RangeObject *self) |
4385 | 3051 { |
3052 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 3053 if (CheckBuffer(self->buf)) |
4385 | 3054 return -1; /* ??? */ |
3055 | |
4488 | 3056 return (self->end - self->start + 1); |
4385 | 3057 } |
3058 | |
3059 static PyObject * | |
4488 | 3060 RangeItem(RangeObject *self, PyInt n) |
4385 | 3061 { |
4488 | 3062 return RBItem(self->buf, n, self->start, self->end); |
4385 | 3063 } |
3064 | |
3065 static PyObject * | |
4488 | 3066 RangeSlice(RangeObject *self, PyInt lo, PyInt hi) |
4385 | 3067 { |
4488 | 3068 return RBSlice(self->buf, lo, hi, self->start, self->end); |
4385 | 3069 } |
3070 | |
3071 static PyObject * | |
4488 | 3072 RangeAppend(RangeObject *self, PyObject *args) |
4385 | 3073 { |
4488 | 3074 return RBAppend(self->buf, args, self->start, self->end, &self->end); |
4385 | 3075 } |
3076 | |
3077 static PyObject * | |
4488 | 3078 RangeRepr(RangeObject *self) |
4385 | 3079 { |
3080 static char repr[100]; | |
4488 | 3081 |
3082 if (self->buf->buf == INVALID_BUFFER_VALUE) | |
4385 | 3083 { |
3084 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", | |
3085 (self)); | |
3086 return PyString_FromString(repr); | |
3087 } | |
3088 else | |
3089 { | |
4488 | 3090 char *name = (char *)self->buf->buf->b_fname; |
4385 | 3091 int len; |
3092 | |
3093 if (name == NULL) | |
3094 name = ""; | |
3095 len = (int)strlen(name); | |
3096 | |
3097 if (len > 45) | |
3098 name = name + (45 - len); | |
3099 | |
3100 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", | |
3101 len > 45 ? "..." : "", name, | |
4488 | 3102 self->start, self->end); |
4385 | 3103 |
3104 return PyString_FromString(repr); | |
3105 } | |
3106 } | |
3107 | |
3108 static struct PyMethodDef RangeMethods[] = { | |
4492 | 3109 /* name, function, calling, documentation */ |
3110 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" }, | |
3111 { NULL, NULL, 0, NULL } | |
4385 | 3112 }; |
3113 | |
4319 | 3114 static PyTypeObject BufferType; |
3115 static PySequenceMethods BufferAsSeq; | |
3116 static PyMappingMethods BufferAsMapping; | |
3117 | |
3118 static PyObject * | |
4377 | 3119 BufferNew(buf_T *buf) |
3120 { | |
3121 /* We need to handle deletion of buffers underneath us. | |
3122 * If we add a "b_python*_ref" field to the buf_T structure, | |
3123 * then we can get at it in buf_freeall() in vim. We then | |
3124 * need to create only ONE Python object per buffer - if | |
3125 * we try to create a second, just INCREF the existing one | |
3126 * and return it. The (single) Python object referring to | |
3127 * the buffer is stored in "b_python*_ref". | |
3128 * Question: what to do on a buf_freeall(). We'll probably | |
3129 * have to either delete the Python object (DECREF it to | |
3130 * zero - a bad idea, as it leaves dangling refs!) or | |
3131 * set the buf_T * value to an invalid value (-1?), which | |
3132 * means we need checks in all access functions... Bah. | |
3133 * | |
4385 | 3134 * Python2 and Python3 get different fields and different objects: |
4377 | 3135 * b_python_ref and b_python3_ref fields respectively. |
3136 */ | |
3137 | |
3138 BufferObject *self; | |
3139 | |
3140 if (BUF_PYTHON_REF(buf) != NULL) | |
3141 { | |
3142 self = BUF_PYTHON_REF(buf); | |
3143 Py_INCREF(self); | |
3144 } | |
3145 else | |
3146 { | |
3147 self = PyObject_NEW(BufferObject, &BufferType); | |
3148 if (self == NULL) | |
3149 return NULL; | |
3150 self->buf = buf; | |
3151 BUF_PYTHON_REF(buf) = self; | |
3152 } | |
3153 | |
3154 return (PyObject *)(self); | |
3155 } | |
3156 | |
4385 | 3157 static void |
4488 | 3158 BufferDestructor(BufferObject *self) |
4319 | 3159 { |
4488 | 3160 if (self->buf && self->buf != INVALID_BUFFER_VALUE) |
3161 BUF_PYTHON_REF(self->buf) = NULL; | |
4385 | 3162 |
3163 DESTRUCTOR_FINISH(self); | |
4319 | 3164 } |
3165 | |
4377 | 3166 static PyInt |
4488 | 3167 BufferLength(BufferObject *self) |
4377 | 3168 { |
3169 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 3170 if (CheckBuffer(self)) |
4377 | 3171 return -1; /* ??? */ |
3172 | |
4488 | 3173 return (PyInt)(self->buf->b_ml.ml_line_count); |
4377 | 3174 } |
3175 | |
3176 static PyObject * | |
4488 | 3177 BufferItem(BufferObject *self, PyInt n) |
4377 | 3178 { |
4488 | 3179 return RBItem(self, n, 1, -1); |
4377 | 3180 } |
3181 | |
3182 static PyObject * | |
4488 | 3183 BufferSlice(BufferObject *self, PyInt lo, PyInt hi) |
4377 | 3184 { |
4488 | 3185 return RBSlice(self, lo, hi, 1, -1); |
4377 | 3186 } |
3187 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3188 static PyObject * |
4488 | 3189 BufferAttr(BufferObject *self, char *name) |
4385 | 3190 { |
3191 if (strcmp(name, "name") == 0) | |
4488 | 3192 return Py_BuildValue("s", self->buf->b_ffname); |
4385 | 3193 else if (strcmp(name, "number") == 0) |
4488 | 3194 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum); |
4385 | 3195 else if (strcmp(name, "vars") == 0) |
4488 | 3196 return DictionaryNew(self->buf->b_vars); |
4385 | 3197 else if (strcmp(name, "options") == 0) |
4488 | 3198 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer, |
3199 (PyObject *) self); | |
4385 | 3200 else if (strcmp(name,"__members__") == 0) |
3201 return Py_BuildValue("[ssss]", "name", "number", "vars", "options"); | |
3202 else | |
3203 return NULL; | |
3204 } | |
3205 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3206 static int |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3207 BufferSetattr(BufferObject *self, char *name, PyObject *valObject) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3208 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3209 if (CheckBuffer(self)) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3210 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3211 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3212 if (strcmp(name, "name") == 0) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3213 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3214 char_u *val; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3215 aco_save_T aco; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3216 int r; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3217 PyObject *todecref; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3218 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3219 if (!(val = StringToChars(valObject, &todecref))) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3220 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3221 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3222 VimTryStart(); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3223 /* 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
|
3224 aucmd_prepbuf(&aco, self->buf); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3225 r = rename_buffer(val); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3226 aucmd_restbuf(&aco); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3227 Py_XDECREF(todecref); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3228 if (VimTryEnd()) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3229 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3230 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3231 if (r == FAIL) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3232 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3233 PyErr_SetVim(_("failed to rename buffer")); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3234 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3235 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3236 return 0; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3237 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3238 else |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3239 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3240 PyErr_SetString(PyExc_AttributeError, name); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3241 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3242 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3243 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3244 |
4385 | 3245 static PyObject * |
4488 | 3246 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
|
3247 { |
4488 | 3248 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
|
3249 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3250 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3251 static PyObject * |
4488 | 3252 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
|
3253 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3254 pos_T *posp; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3255 char *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3256 char mark; |
4429 | 3257 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
|
3258 |
4488 | 3259 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
|
3260 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3261 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3262 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
|
3263 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3264 mark = *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3265 |
4498 | 3266 VimTryStart(); |
4488 | 3267 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
|
3268 posp = getmark(mark, FALSE); |
4429 | 3269 restore_buffer(savebuf); |
4498 | 3270 if (VimTryEnd()) |
3271 return NULL; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3272 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3273 if (posp == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3274 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3275 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
|
3276 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3277 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3278 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3279 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
|
3280 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3281 /* 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
|
3282 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
|
3283 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3284 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3285 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3286 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
|
3287 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3288 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3289 static PyObject * |
4488 | 3290 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
|
3291 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3292 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3293 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3294 |
4488 | 3295 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
|
3296 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3297 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3298 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
|
3299 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3300 |
4488 | 3301 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
|
3302 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3303 |
4319 | 3304 static PyObject * |
4488 | 3305 BufferRepr(BufferObject *self) |
4319 | 3306 { |
3307 static char repr[100]; | |
4488 | 3308 |
3309 if (self->buf == INVALID_BUFFER_VALUE) | |
4319 | 3310 { |
3311 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); | |
3312 return PyString_FromString(repr); | |
3313 } | |
3314 else | |
3315 { | |
4488 | 3316 char *name = (char *)self->buf->b_fname; |
4319 | 3317 PyInt len; |
3318 | |
3319 if (name == NULL) | |
3320 name = ""; | |
3321 len = strlen(name); | |
3322 | |
3323 if (len > 35) | |
3324 name = name + (35 - len); | |
3325 | |
3326 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); | |
3327 | |
3328 return PyString_FromString(repr); | |
3329 } | |
3330 } | |
3331 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3332 static struct PyMethodDef BufferMethods[] = { |
4492 | 3333 /* name, function, calling, documentation */ |
3334 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, | |
3335 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" }, | |
3336 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, | |
3310 | 3337 #if PY_VERSION_HEX >= 0x03000000 |
4492 | 3338 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, "List buffer attributes" }, |
3310 | 3339 #endif |
4492 | 3340 { 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
|
3341 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3342 |
4397 | 3343 /* |
3344 * Buffer list object - Implementation | |
3345 */ | |
3346 | |
3347 static PyTypeObject BufMapType; | |
3348 | |
3349 typedef struct | |
3350 { | |
3351 PyObject_HEAD | |
3352 } BufMapObject; | |
3353 | |
3354 static PyInt | |
3355 BufMapLength(PyObject *self UNUSED) | |
3356 { | |
3357 buf_T *b = firstbuf; | |
3358 PyInt n = 0; | |
3359 | |
3360 while (b) | |
3361 { | |
3362 ++n; | |
3363 b = b->b_next; | |
3364 } | |
3365 | |
3366 return n; | |
3367 } | |
3368 | |
3369 static PyObject * | |
3370 BufMapItem(PyObject *self UNUSED, PyObject *keyObject) | |
3371 { | |
3372 buf_T *b; | |
3373 int bnr; | |
3374 | |
3375 #if PY_MAJOR_VERSION < 3 | |
3376 if (PyInt_Check(keyObject)) | |
3377 bnr = PyInt_AsLong(keyObject); | |
3378 else | |
3379 #endif | |
3380 if (PyLong_Check(keyObject)) | |
3381 bnr = PyLong_AsLong(keyObject); | |
3382 else | |
3383 { | |
4405 | 3384 PyErr_SetString(PyExc_TypeError, _("key must be integer")); |
4397 | 3385 return NULL; |
3386 } | |
3387 | |
3388 b = buflist_findnr(bnr); | |
3389 | |
3390 if (b) | |
3391 return BufferNew(b); | |
3392 else | |
3393 { | |
4403 | 3394 PyErr_SetObject(PyExc_KeyError, keyObject); |
4397 | 3395 return NULL; |
3396 } | |
3397 } | |
3398 | |
3399 static void | |
3400 BufMapIterDestruct(PyObject *buffer) | |
3401 { | |
3402 /* Iteration was stopped before all buffers were processed */ | |
3403 if (buffer) | |
3404 { | |
3405 Py_DECREF(buffer); | |
3406 } | |
3407 } | |
3408 | |
4433 | 3409 static int |
3410 BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg) | |
3411 { | |
4500 | 3412 if (buffer) |
3413 Py_VISIT(buffer); | |
4433 | 3414 return 0; |
3415 } | |
3416 | |
3417 static int | |
3418 BufMapIterClear(PyObject **buffer) | |
3419 { | |
4500 | 3420 if (*buffer) |
3421 Py_CLEAR(*buffer); | |
4433 | 3422 return 0; |
3423 } | |
3424 | |
4397 | 3425 static PyObject * |
3426 BufMapIterNext(PyObject **buffer) | |
3427 { | |
3428 PyObject *next; | |
3429 PyObject *r; | |
3430 | |
3431 if (!*buffer) | |
3432 return NULL; | |
3433 | |
3434 r = *buffer; | |
3435 | |
3436 if (CheckBuffer((BufferObject *)(r))) | |
3437 { | |
3438 *buffer = NULL; | |
3439 return NULL; | |
3440 } | |
3441 | |
3442 if (!((BufferObject *)(r))->buf->b_next) | |
3443 next = NULL; | |
3444 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next))) | |
3445 return NULL; | |
3446 *buffer = next; | |
4438 | 3447 /* Do not increment reference: we no longer hold it (decref), but whoever |
3448 * on other side will hold (incref). Decref+incref = nothing. */ | |
4397 | 3449 return r; |
3450 } | |
3451 | |
3452 static PyObject * | |
3453 BufMapIter(PyObject *self UNUSED) | |
3454 { | |
3455 PyObject *buffer; | |
3456 | |
3457 buffer = BufferNew(firstbuf); | |
3458 return IterNew(buffer, | |
4433 | 3459 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext, |
3460 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear); | |
4397 | 3461 } |
3462 | |
3463 static PyMappingMethods BufMapAsMapping = { | |
3464 (lenfunc) BufMapLength, | |
3465 (binaryfunc) BufMapItem, | |
3466 (objobjargproc) 0, | |
3467 }; | |
3468 | |
4385 | 3469 /* 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
|
3470 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3471 |
4319 | 3472 static PyObject * |
3473 CurrentGetattr(PyObject *self UNUSED, char *name) | |
3474 { | |
3475 if (strcmp(name, "buffer") == 0) | |
3476 return (PyObject *)BufferNew(curbuf); | |
3477 else if (strcmp(name, "window") == 0) | |
4431 | 3478 return (PyObject *)WindowNew(curwin, curtab); |
4401 | 3479 else if (strcmp(name, "tabpage") == 0) |
3480 return (PyObject *)TabPageNew(curtab); | |
4319 | 3481 else if (strcmp(name, "line") == 0) |
3482 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); | |
3483 else if (strcmp(name, "range") == 0) | |
3484 return RangeNew(curbuf, RangeStart, RangeEnd); | |
3485 else if (strcmp(name,"__members__") == 0) | |
4401 | 3486 return Py_BuildValue("[sssss]", "buffer", "window", "line", "range", |
3487 "tabpage"); | |
4319 | 3488 else |
3489 { | |
3490 PyErr_SetString(PyExc_AttributeError, name); | |
3491 return NULL; | |
3492 } | |
3493 } | |
3494 | |
3495 static int | |
3496 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) | |
3497 { | |
3498 if (strcmp(name, "line") == 0) | |
3499 { | |
3500 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) | |
3501 return -1; | |
3502 | |
3503 return 0; | |
3504 } | |
4407 | 3505 else if (strcmp(name, "buffer") == 0) |
3506 { | |
3507 int count; | |
3508 | |
3509 if (value->ob_type != &BufferType) | |
3510 { | |
3511 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object")); | |
3512 return -1; | |
3513 } | |
3514 | |
3515 if (CheckBuffer((BufferObject *)(value))) | |
3516 return -1; | |
3517 count = ((BufferObject *)(value))->buf->b_fnum; | |
3518 | |
4498 | 3519 VimTryStart(); |
4407 | 3520 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) |
3521 { | |
4498 | 3522 if (VimTryEnd()) |
3523 return -1; | |
4407 | 3524 PyErr_SetVim(_("failed to switch to given buffer")); |
3525 return -1; | |
3526 } | |
3527 | |
4498 | 3528 return VimTryEnd(); |
4407 | 3529 } |
3530 else if (strcmp(name, "window") == 0) | |
3531 { | |
3532 int count; | |
3533 | |
3534 if (value->ob_type != &WindowType) | |
3535 { | |
3536 PyErr_SetString(PyExc_TypeError, _("expected vim.window object")); | |
3537 return -1; | |
3538 } | |
3539 | |
3540 if (CheckWindow((WindowObject *)(value))) | |
3541 return -1; | |
3542 count = get_win_number(((WindowObject *)(value))->win, firstwin); | |
3543 | |
3544 if (!count) | |
3545 { | |
3546 PyErr_SetString(PyExc_ValueError, | |
3547 _("failed to find window in the current tab page")); | |
3548 return -1; | |
3549 } | |
3550 | |
4498 | 3551 VimTryStart(); |
4407 | 3552 win_goto(((WindowObject *)(value))->win); |
3553 if (((WindowObject *)(value))->win != curwin) | |
3554 { | |
4498 | 3555 if (VimTryEnd()) |
3556 return -1; | |
4407 | 3557 PyErr_SetString(PyExc_RuntimeError, |
3558 _("did not switch to the specified window")); | |
3559 return -1; | |
3560 } | |
3561 | |
4498 | 3562 return VimTryEnd(); |
4407 | 3563 } |
3564 else if (strcmp(name, "tabpage") == 0) | |
3565 { | |
3566 if (value->ob_type != &TabPageType) | |
3567 { | |
3568 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object")); | |
3569 return -1; | |
3570 } | |
3571 | |
3572 if (CheckTabPage((TabPageObject *)(value))) | |
3573 return -1; | |
3574 | |
4498 | 3575 VimTryStart(); |
4407 | 3576 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE); |
3577 if (((TabPageObject *)(value))->tab != curtab) | |
3578 { | |
4498 | 3579 if (VimTryEnd()) |
3580 return -1; | |
4407 | 3581 PyErr_SetString(PyExc_RuntimeError, |
3582 _("did not switch to the specified tab page")); | |
3583 return -1; | |
3584 } | |
3585 | |
4498 | 3586 return VimTryEnd(); |
4407 | 3587 } |
4319 | 3588 else |
3589 { | |
3590 PyErr_SetString(PyExc_AttributeError, name); | |
3591 return -1; | |
3592 } | |
3593 } | |
3594 | |
3618 | 3595 static void |
4486 | 3596 init_range_cmd(exarg_T *eap) |
3597 { | |
3598 RangeStart = eap->line1; | |
3599 RangeEnd = eap->line2; | |
3600 } | |
3601 | |
3602 static void | |
3603 init_range_eval(typval_T *rettv UNUSED) | |
3604 { | |
3605 RangeStart = (PyInt) curwin->w_cursor.lnum; | |
3606 RangeEnd = RangeStart; | |
3607 } | |
3608 | |
3609 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3610 run_cmd(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3611 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3612 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3613 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3614 ) |
4486 | 3615 { |
3616 PyRun_SimpleString((char *) cmd); | |
3617 } | |
3618 | |
3619 static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n "; | |
3620 static int code_hdr_len = 30; | |
3621 | |
3622 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3623 run_do(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3624 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3625 , PyGILState_STATE *pygilstate |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3626 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3627 ) |
4486 | 3628 { |
3629 PyInt lnum; | |
3630 size_t len; | |
3631 char *code; | |
3632 int status; | |
3633 PyObject *pyfunc, *pymain; | |
3634 | |
4575
626e9ccb7c48
updated for version 7.3.1035
Bram Moolenaar <bram@vim.org>
parents:
4523
diff
changeset
|
3635 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK) |
4486 | 3636 { |
3637 EMSG(_("cannot save undo information")); | |
3638 return; | |
3639 } | |
3640 | |
3641 len = code_hdr_len + STRLEN(cmd); | |
3642 code = PyMem_New(char, len + 1); | |
3643 memcpy(code, code_hdr, code_hdr_len); | |
3644 STRCPY(code + code_hdr_len, cmd); | |
3645 status = PyRun_SimpleString(code); | |
3646 PyMem_Free(code); | |
3647 | |
3648 if (status) | |
3649 { | |
3650 EMSG(_("failed to run the code")); | |
3651 return; | |
3652 } | |
3653 | |
3654 status = 0; | |
3655 pymain = PyImport_AddModule("__main__"); | |
3656 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3657 #ifdef PY_CAN_RECURSE |
4486 | 3658 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3659 #endif |
4486 | 3660 |
3661 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) | |
3662 { | |
3663 PyObject *line, *linenr, *ret; | |
3664 | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3665 #ifdef PY_CAN_RECURSE |
4486 | 3666 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3667 #endif |
4486 | 3668 if (!(line = GetBufferLine(curbuf, lnum))) |
3669 goto err; | |
3670 if (!(linenr = PyInt_FromLong((long) lnum))) | |
3671 { | |
3672 Py_DECREF(line); | |
3673 goto err; | |
3674 } | |
3675 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL); | |
3676 Py_DECREF(line); | |
3677 Py_DECREF(linenr); | |
3678 if (!ret) | |
3679 goto err; | |
3680 | |
3681 if (ret != Py_None) | |
3682 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL) | |
3683 goto err; | |
3684 | |
3685 Py_XDECREF(ret); | |
3686 PythonIO_Flush(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3687 #ifdef PY_CAN_RECURSE |
4486 | 3688 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3689 #endif |
4486 | 3690 } |
3691 goto out; | |
3692 err: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3693 #ifdef PY_CAN_RECURSE |
4486 | 3694 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3695 #endif |
4486 | 3696 PyErr_PrintEx(0); |
3697 PythonIO_Flush(); | |
3698 status = 1; | |
3699 out: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3700 #ifdef PY_CAN_RECURSE |
4486 | 3701 if (!status) |
3702 *pygilstate = PyGILState_Ensure(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3703 #endif |
4486 | 3704 Py_DECREF(pyfunc); |
3705 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL); | |
3706 if (status) | |
3707 return; | |
3708 check_cursor(); | |
3709 update_curbuf(NOT_VALID); | |
3710 } | |
3711 | |
3712 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3713 run_eval(const char *cmd, typval_T *rettv |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3714 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3715 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3716 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3717 ) |
4486 | 3718 { |
3719 PyObject *r; | |
3720 | |
3721 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals); | |
3722 if (r == NULL) | |
3723 { | |
3724 if (PyErr_Occurred() && !msg_silent) | |
3725 PyErr_PrintEx(0); | |
3726 EMSG(_("E858: Eval did not return a valid python object")); | |
3727 } | |
3728 else | |
3729 { | |
3730 if (ConvertFromPyObject(r, rettv) == -1) | |
3731 EMSG(_("E859: Failed to convert returned python object to vim value")); | |
3732 Py_DECREF(r); | |
3733 } | |
3734 PyErr_Clear(); | |
3735 } | |
3736 | |
3737 static void | |
3618 | 3738 set_ref_in_py(const int copyID) |
3739 { | |
3740 pylinkedlist_T *cur; | |
3741 dict_T *dd; | |
3742 list_T *ll; | |
3743 | |
3744 if (lastdict != NULL) | |
3745 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) | |
3746 { | |
3747 dd = ((DictionaryObject *) (cur->pll_obj))->dict; | |
3748 if (dd->dv_copyID != copyID) | |
3749 { | |
3750 dd->dv_copyID = copyID; | |
3751 set_ref_in_ht(&dd->dv_hashtab, copyID); | |
3752 } | |
3753 } | |
3754 | |
3755 if (lastlist != NULL) | |
3756 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) | |
3757 { | |
3758 ll = ((ListObject *) (cur->pll_obj))->list; | |
3759 if (ll->lv_copyID != copyID) | |
3760 { | |
3761 ll->lv_copyID = copyID; | |
3762 set_ref_in_list(ll, copyID); | |
3763 } | |
3764 } | |
3765 } | |
3766 | |
3767 static int | |
3768 set_string_copy(char_u *str, typval_T *tv) | |
3769 { | |
3770 tv->vval.v_string = vim_strsave(str); | |
3771 if (tv->vval.v_string == NULL) | |
3772 { | |
3773 PyErr_NoMemory(); | |
3774 return -1; | |
3775 } | |
3776 return 0; | |
3777 } | |
3778 | |
4385 | 3779 static int |
3780 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3781 { | |
3782 dict_T *d; | |
3783 char_u *key; | |
3784 dictitem_T *di; | |
3785 PyObject *keyObject; | |
3786 PyObject *valObject; | |
3787 Py_ssize_t iter = 0; | |
3788 | |
3789 d = dict_alloc(); | |
3790 if (d == NULL) | |
3791 { | |
3792 PyErr_NoMemory(); | |
3793 return -1; | |
3794 } | |
3795 | |
3796 tv->v_type = VAR_DICT; | |
3797 tv->vval.v_dict = d; | |
3798 | |
3799 while (PyDict_Next(obj, &iter, &keyObject, &valObject)) | |
3800 { | |
3801 DICTKEY_DECL | |
3802 | |
3803 if (keyObject == NULL) | |
3804 return -1; | |
3805 if (valObject == NULL) | |
3806 return -1; | |
3807 | |
3808 DICTKEY_GET_NOTEMPTY(-1) | |
3809 | |
3810 di = dictitem_alloc(key); | |
3811 | |
3812 DICTKEY_UNREF | |
3813 | |
3814 if (di == NULL) | |
3815 { | |
3816 PyErr_NoMemory(); | |
3817 return -1; | |
3818 } | |
3819 di->di_tv.v_lock = 0; | |
3820 | |
3821 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
3822 { | |
3823 vim_free(di); | |
3824 return -1; | |
3825 } | |
3826 if (dict_add(d, di) == FAIL) | |
3827 { | |
3828 vim_free(di); | |
3829 PyErr_SetVim(_("failed to add key to dictionary")); | |
3830 return -1; | |
3831 } | |
3832 } | |
3833 return 0; | |
3834 } | |
3835 | |
3836 static int | |
3837 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3838 { | |
3839 dict_T *d; | |
3840 char_u *key; | |
3841 dictitem_T *di; | |
3842 PyObject *list; | |
3843 PyObject *litem; | |
3844 PyObject *keyObject; | |
3845 PyObject *valObject; | |
3846 Py_ssize_t lsize; | |
3847 | |
3848 d = dict_alloc(); | |
3849 if (d == NULL) | |
3850 { | |
3851 PyErr_NoMemory(); | |
3852 return -1; | |
3853 } | |
3854 | |
3855 tv->v_type = VAR_DICT; | |
3856 tv->vval.v_dict = d; | |
3857 | |
3858 list = PyMapping_Items(obj); | |
3859 if (list == NULL) | |
3860 return -1; | |
3861 lsize = PyList_Size(list); | |
3862 while (lsize--) | |
3863 { | |
3864 DICTKEY_DECL | |
3865 | |
3866 litem = PyList_GetItem(list, lsize); | |
3867 if (litem == NULL) | |
3868 { | |
3869 Py_DECREF(list); | |
3870 return -1; | |
3871 } | |
3872 | |
3873 keyObject = PyTuple_GetItem(litem, 0); | |
3874 if (keyObject == NULL) | |
3875 { | |
3876 Py_DECREF(list); | |
3877 Py_DECREF(litem); | |
3878 return -1; | |
3879 } | |
3880 | |
3881 DICTKEY_GET_NOTEMPTY(-1) | |
3882 | |
3883 valObject = PyTuple_GetItem(litem, 1); | |
3884 if (valObject == NULL) | |
3885 { | |
3886 Py_DECREF(list); | |
3887 Py_DECREF(litem); | |
3888 return -1; | |
3889 } | |
3890 | |
3891 di = dictitem_alloc(key); | |
3892 | |
3893 DICTKEY_UNREF | |
3894 | |
3895 if (di == NULL) | |
3896 { | |
3897 Py_DECREF(list); | |
3898 Py_DECREF(litem); | |
3899 PyErr_NoMemory(); | |
3900 return -1; | |
3901 } | |
3902 di->di_tv.v_lock = 0; | |
3903 | |
3904 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
3905 { | |
3906 vim_free(di); | |
3907 Py_DECREF(list); | |
3908 Py_DECREF(litem); | |
3909 return -1; | |
3910 } | |
3911 if (dict_add(d, di) == FAIL) | |
3912 { | |
3913 vim_free(di); | |
3914 Py_DECREF(list); | |
3915 Py_DECREF(litem); | |
3916 PyErr_SetVim(_("failed to add key to dictionary")); | |
3917 return -1; | |
3918 } | |
3919 Py_DECREF(litem); | |
3920 } | |
3921 Py_DECREF(list); | |
3922 return 0; | |
3923 } | |
3924 | |
3925 static int | |
3926 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3927 { | |
3928 list_T *l; | |
3929 | |
3930 l = list_alloc(); | |
3931 if (l == NULL) | |
3932 { | |
3933 PyErr_NoMemory(); | |
3934 return -1; | |
3935 } | |
3936 | |
3937 tv->v_type = VAR_LIST; | |
3938 tv->vval.v_list = l; | |
3939 | |
3940 if (list_py_concat(l, obj, lookupDict) == -1) | |
3941 return -1; | |
3942 | |
3943 return 0; | |
3944 } | |
3945 | |
3946 static int | |
3947 pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
3948 { | |
3949 PyObject *iterator = PyObject_GetIter(obj); | |
3950 PyObject *item; | |
3951 list_T *l; | |
3952 listitem_T *li; | |
3953 | |
3954 l = list_alloc(); | |
3955 | |
3956 if (l == NULL) | |
3957 { | |
3958 PyErr_NoMemory(); | |
3959 return -1; | |
3960 } | |
3961 | |
3962 tv->vval.v_list = l; | |
3963 tv->v_type = VAR_LIST; | |
3964 | |
3965 | |
3966 if (iterator == NULL) | |
3967 return -1; | |
3968 | |
3969 while ((item = PyIter_Next(obj))) | |
3970 { | |
3971 li = listitem_alloc(); | |
3972 if (li == NULL) | |
3973 { | |
3974 PyErr_NoMemory(); | |
3975 return -1; | |
3976 } | |
3977 li->li_tv.v_lock = 0; | |
3978 | |
3979 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1) | |
3980 return -1; | |
3981 | |
3982 list_append(l, li); | |
3983 | |
3984 Py_DECREF(item); | |
3985 } | |
3986 | |
3987 Py_DECREF(iterator); | |
3988 return 0; | |
3989 } | |
3990 | |
3618 | 3991 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
3992 | |
3993 static int | |
3994 convert_dl(PyObject *obj, typval_T *tv, | |
3995 pytotvfunc py_to_tv, PyObject *lookupDict) | |
3996 { | |
3997 PyObject *capsule; | |
3998 char hexBuf[sizeof(void *) * 2 + 3]; | |
3999 | |
4000 sprintf(hexBuf, "%p", obj); | |
4001 | |
3638 | 4002 # ifdef PY_USE_CAPSULE |
3618 | 4003 capsule = PyDict_GetItemString(lookupDict, hexBuf); |
3648 | 4004 # else |
4005 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf); | |
4006 # endif | |
3618 | 4007 if (capsule == NULL) |
4008 { | |
3638 | 4009 # ifdef PY_USE_CAPSULE |
3618 | 4010 capsule = PyCapsule_New(tv, NULL, NULL); |
3648 | 4011 # else |
4012 capsule = PyCObject_FromVoidPtr(tv, NULL); | |
4013 # endif | |
3618 | 4014 PyDict_SetItemString(lookupDict, hexBuf, capsule); |
4015 Py_DECREF(capsule); | |
4016 if (py_to_tv(obj, tv, lookupDict) == -1) | |
4017 { | |
4018 tv->v_type = VAR_UNKNOWN; | |
4019 return -1; | |
4020 } | |
4021 /* As we are not using copy_tv which increments reference count we must | |
4022 * do it ourself. */ | |
4023 switch(tv->v_type) | |
4024 { | |
4025 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; | |
4026 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; | |
4027 } | |
4028 } | |
4029 else | |
4030 { | |
3638 | 4031 typval_T *v; |
4032 | |
4033 # ifdef PY_USE_CAPSULE | |
4034 v = PyCapsule_GetPointer(capsule, NULL); | |
4035 # else | |
3648 | 4036 v = PyCObject_AsVoidPtr(capsule); |
3638 | 4037 # endif |
3618 | 4038 copy_tv(v, tv); |
4039 } | |
4040 return 0; | |
4041 } | |
4042 | |
4043 static int | |
4044 ConvertFromPyObject(PyObject *obj, typval_T *tv) | |
4045 { | |
4046 PyObject *lookup_dict; | |
4047 int r; | |
4048 | |
4049 lookup_dict = PyDict_New(); | |
4050 r = _ConvertFromPyObject(obj, tv, lookup_dict); | |
4051 Py_DECREF(lookup_dict); | |
4052 return r; | |
4053 } | |
4054 | |
4055 static int | |
4056 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
4057 { | |
4058 if (obj->ob_type == &DictionaryType) | |
4059 { | |
4060 tv->v_type = VAR_DICT; | |
4061 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); | |
4062 ++tv->vval.v_dict->dv_refcount; | |
4063 } | |
4064 else if (obj->ob_type == &ListType) | |
4065 { | |
4066 tv->v_type = VAR_LIST; | |
4067 tv->vval.v_list = (((ListObject *)(obj))->list); | |
4068 ++tv->vval.v_list->lv_refcount; | |
4069 } | |
4070 else if (obj->ob_type == &FunctionType) | |
4071 { | |
4072 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) | |
4073 return -1; | |
4074 | |
4075 tv->v_type = VAR_FUNC; | |
4076 func_ref(tv->vval.v_string); | |
4077 } | |
4078 else if (PyBytes_Check(obj)) | |
4079 { | |
3800 | 4080 char_u *result; |
4081 | |
4082 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) | |
4083 return -1; | |
3618 | 4084 if (result == NULL) |
4085 return -1; | |
4086 | |
4087 if (set_string_copy(result, tv) == -1) | |
4088 return -1; | |
4089 | |
4090 tv->v_type = VAR_STRING; | |
4091 } | |
4092 else if (PyUnicode_Check(obj)) | |
4093 { | |
4094 PyObject *bytes; | |
4095 char_u *result; | |
4096 | |
4097 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); | |
4098 if (bytes == NULL) | |
4099 return -1; | |
4100 | |
3800 | 4101 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
4102 return -1; | |
3618 | 4103 if (result == NULL) |
4104 return -1; | |
4105 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4106 if (set_string_copy(result, tv)) |
3618 | 4107 { |
4108 Py_XDECREF(bytes); | |
4109 return -1; | |
4110 } | |
4111 Py_XDECREF(bytes); | |
4112 | |
4113 tv->v_type = VAR_STRING; | |
4114 } | |
4321 | 4115 #if PY_MAJOR_VERSION < 3 |
3618 | 4116 else if (PyInt_Check(obj)) |
4117 { | |
4118 tv->v_type = VAR_NUMBER; | |
4119 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); | |
4120 } | |
4121 #endif | |
4122 else if (PyLong_Check(obj)) | |
4123 { | |
4124 tv->v_type = VAR_NUMBER; | |
4125 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); | |
4126 } | |
4127 else if (PyDict_Check(obj)) | |
4128 return convert_dl(obj, tv, pydict_to_tv, lookupDict); | |
4129 #ifdef FEAT_FLOAT | |
4130 else if (PyFloat_Check(obj)) | |
4131 { | |
4132 tv->v_type = VAR_FLOAT; | |
4133 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); | |
4134 } | |
4135 #endif | |
4136 else if (PyIter_Check(obj)) | |
4137 return convert_dl(obj, tv, pyiter_to_tv, lookupDict); | |
4138 else if (PySequence_Check(obj)) | |
4139 return convert_dl(obj, tv, pyseq_to_tv, lookupDict); | |
4140 else if (PyMapping_Check(obj)) | |
4141 return convert_dl(obj, tv, pymap_to_tv, lookupDict); | |
4142 else | |
4143 { | |
4405 | 4144 PyErr_SetString(PyExc_TypeError, |
4145 _("unable to convert to vim structure")); | |
3618 | 4146 return -1; |
4147 } | |
4148 return 0; | |
4149 } | |
4150 | |
4151 static PyObject * | |
4152 ConvertToPyObject(typval_T *tv) | |
4153 { | |
4154 if (tv == NULL) | |
4155 { | |
4156 PyErr_SetVim(_("NULL reference passed")); | |
4157 return NULL; | |
4158 } | |
4159 switch (tv->v_type) | |
4160 { | |
4161 case VAR_STRING: | |
3852 | 4162 return PyBytes_FromString(tv->vval.v_string == NULL |
4163 ? "" : (char *)tv->vval.v_string); | |
3618 | 4164 case VAR_NUMBER: |
4165 return PyLong_FromLong((long) tv->vval.v_number); | |
4166 #ifdef FEAT_FLOAT | |
4167 case VAR_FLOAT: | |
4168 return PyFloat_FromDouble((double) tv->vval.v_float); | |
4169 #endif | |
4170 case VAR_LIST: | |
4171 return ListNew(tv->vval.v_list); | |
4172 case VAR_DICT: | |
4173 return DictionaryNew(tv->vval.v_dict); | |
4174 case VAR_FUNC: | |
3852 | 4175 return FunctionNew(tv->vval.v_string == NULL |
4176 ? (char_u *)"" : tv->vval.v_string); | |
3618 | 4177 case VAR_UNKNOWN: |
4178 Py_INCREF(Py_None); | |
4179 return Py_None; | |
4180 default: | |
4181 PyErr_SetVim(_("internal error: invalid value type")); | |
4182 return NULL; | |
4183 } | |
4184 } | |
4319 | 4185 |
4186 typedef struct | |
4187 { | |
4188 PyObject_HEAD | |
4189 } CurrentObject; | |
4190 static PyTypeObject CurrentType; | |
4191 | |
4192 static void | |
4193 init_structs(void) | |
4194 { | |
4195 vim_memset(&OutputType, 0, sizeof(OutputType)); | |
4196 OutputType.tp_name = "vim.message"; | |
4197 OutputType.tp_basicsize = sizeof(OutputObject); | |
4198 OutputType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4199 OutputType.tp_doc = "vim message object"; | |
4200 OutputType.tp_methods = OutputMethods; | |
4201 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4202 OutputType.tp_getattro = (getattrofunc)OutputGetattro; |
4203 OutputType.tp_setattro = (setattrofunc)OutputSetattro; | |
4319 | 4204 OutputType.tp_alloc = call_PyType_GenericAlloc; |
4205 OutputType.tp_new = call_PyType_GenericNew; | |
4206 OutputType.tp_free = call_PyObject_Free; | |
4207 #else | |
4488 | 4208 OutputType.tp_getattr = (getattrfunc)OutputGetattr; |
4209 OutputType.tp_setattr = (setattrfunc)OutputSetattr; | |
4319 | 4210 #endif |
4211 | |
4397 | 4212 vim_memset(&IterType, 0, sizeof(IterType)); |
4213 IterType.tp_name = "vim.iter"; | |
4214 IterType.tp_basicsize = sizeof(IterObject); | |
4215 IterType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4216 IterType.tp_doc = "generic iterator object"; | |
4488 | 4217 IterType.tp_iter = (getiterfunc)IterIter; |
4218 IterType.tp_iternext = (iternextfunc)IterNext; | |
4219 IterType.tp_dealloc = (destructor)IterDestructor; | |
4220 IterType.tp_traverse = (traverseproc)IterTraverse; | |
4221 IterType.tp_clear = (inquiry)IterClear; | |
4397 | 4222 |
4319 | 4223 vim_memset(&BufferType, 0, sizeof(BufferType)); |
4224 BufferType.tp_name = "vim.buffer"; | |
4225 BufferType.tp_basicsize = sizeof(BufferType); | |
4488 | 4226 BufferType.tp_dealloc = (destructor)BufferDestructor; |
4227 BufferType.tp_repr = (reprfunc)BufferRepr; | |
4319 | 4228 BufferType.tp_as_sequence = &BufferAsSeq; |
4229 BufferType.tp_as_mapping = &BufferAsMapping; | |
4230 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4231 BufferType.tp_doc = "vim buffer object"; | |
4232 BufferType.tp_methods = BufferMethods; | |
4233 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4234 BufferType.tp_getattro = (getattrofunc)BufferGetattro; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4235 BufferType.tp_setattro = (setattrofunc)BufferSetattro; |
4319 | 4236 BufferType.tp_alloc = call_PyType_GenericAlloc; |
4237 BufferType.tp_new = call_PyType_GenericNew; | |
4238 BufferType.tp_free = call_PyObject_Free; | |
4239 #else | |
4488 | 4240 BufferType.tp_getattr = (getattrfunc)BufferGetattr; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4241 BufferType.tp_setattr = (setattrfunc)BufferSetattr; |
4319 | 4242 #endif |
4243 | |
4244 vim_memset(&WindowType, 0, sizeof(WindowType)); | |
4245 WindowType.tp_name = "vim.window"; | |
4246 WindowType.tp_basicsize = sizeof(WindowObject); | |
4488 | 4247 WindowType.tp_dealloc = (destructor)WindowDestructor; |
4248 WindowType.tp_repr = (reprfunc)WindowRepr; | |
4319 | 4249 WindowType.tp_flags = Py_TPFLAGS_DEFAULT; |
4250 WindowType.tp_doc = "vim Window object"; | |
4251 WindowType.tp_methods = WindowMethods; | |
4488 | 4252 WindowType.tp_traverse = (traverseproc)WindowTraverse; |
4253 WindowType.tp_clear = (inquiry)WindowClear; | |
4319 | 4254 #if PY_MAJOR_VERSION >= 3 |
4488 | 4255 WindowType.tp_getattro = (getattrofunc)WindowGetattro; |
4256 WindowType.tp_setattro = (setattrofunc)WindowSetattro; | |
4319 | 4257 WindowType.tp_alloc = call_PyType_GenericAlloc; |
4258 WindowType.tp_new = call_PyType_GenericNew; | |
4259 WindowType.tp_free = call_PyObject_Free; | |
4260 #else | |
4488 | 4261 WindowType.tp_getattr = (getattrfunc)WindowGetattr; |
4262 WindowType.tp_setattr = (setattrfunc)WindowSetattr; | |
4319 | 4263 #endif |
4264 | |
4401 | 4265 vim_memset(&TabPageType, 0, sizeof(TabPageType)); |
4266 TabPageType.tp_name = "vim.tabpage"; | |
4267 TabPageType.tp_basicsize = sizeof(TabPageObject); | |
4488 | 4268 TabPageType.tp_dealloc = (destructor)TabPageDestructor; |
4269 TabPageType.tp_repr = (reprfunc)TabPageRepr; | |
4401 | 4270 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT; |
4271 TabPageType.tp_doc = "vim tab page object"; | |
4272 TabPageType.tp_methods = TabPageMethods; | |
4273 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4274 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro; |
4401 | 4275 TabPageType.tp_alloc = call_PyType_GenericAlloc; |
4276 TabPageType.tp_new = call_PyType_GenericNew; | |
4277 TabPageType.tp_free = call_PyObject_Free; | |
4278 #else | |
4488 | 4279 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr; |
4401 | 4280 #endif |
4281 | |
4393 | 4282 vim_memset(&BufMapType, 0, sizeof(BufMapType)); |
4283 BufMapType.tp_name = "vim.bufferlist"; | |
4284 BufMapType.tp_basicsize = sizeof(BufMapObject); | |
4285 BufMapType.tp_as_mapping = &BufMapAsMapping; | |
4286 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4397 | 4287 BufMapType.tp_iter = BufMapIter; |
4319 | 4288 BufferType.tp_doc = "vim buffer list"; |
4289 | |
4290 vim_memset(&WinListType, 0, sizeof(WinListType)); | |
4291 WinListType.tp_name = "vim.windowlist"; | |
4292 WinListType.tp_basicsize = sizeof(WinListType); | |
4293 WinListType.tp_as_sequence = &WinListAsSeq; | |
4294 WinListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4295 WinListType.tp_doc = "vim window list"; | |
4488 | 4296 WinListType.tp_dealloc = (destructor)WinListDestructor; |
4401 | 4297 |
4298 vim_memset(&TabListType, 0, sizeof(TabListType)); | |
4299 TabListType.tp_name = "vim.tabpagelist"; | |
4300 TabListType.tp_basicsize = sizeof(TabListType); | |
4301 TabListType.tp_as_sequence = &TabListAsSeq; | |
4302 TabListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4303 TabListType.tp_doc = "vim tab page list"; | |
4319 | 4304 |
4305 vim_memset(&RangeType, 0, sizeof(RangeType)); | |
4306 RangeType.tp_name = "vim.range"; | |
4307 RangeType.tp_basicsize = sizeof(RangeObject); | |
4488 | 4308 RangeType.tp_dealloc = (destructor)RangeDestructor; |
4309 RangeType.tp_repr = (reprfunc)RangeRepr; | |
4319 | 4310 RangeType.tp_as_sequence = &RangeAsSeq; |
4311 RangeType.tp_as_mapping = &RangeAsMapping; | |
4312 RangeType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4313 RangeType.tp_doc = "vim Range object"; | |
4314 RangeType.tp_methods = RangeMethods; | |
4500 | 4315 RangeType.tp_traverse = (traverseproc)RangeTraverse; |
4316 RangeType.tp_clear = (inquiry)RangeClear; | |
4319 | 4317 #if PY_MAJOR_VERSION >= 3 |
4488 | 4318 RangeType.tp_getattro = (getattrofunc)RangeGetattro; |
4319 | 4319 RangeType.tp_alloc = call_PyType_GenericAlloc; |
4320 RangeType.tp_new = call_PyType_GenericNew; | |
4321 RangeType.tp_free = call_PyObject_Free; | |
4322 #else | |
4488 | 4323 RangeType.tp_getattr = (getattrfunc)RangeGetattr; |
4319 | 4324 #endif |
4325 | |
4326 vim_memset(&CurrentType, 0, sizeof(CurrentType)); | |
4327 CurrentType.tp_name = "vim.currentdata"; | |
4328 CurrentType.tp_basicsize = sizeof(CurrentObject); | |
4329 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4330 CurrentType.tp_doc = "vim current object"; | |
4331 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4332 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro; |
4333 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro; | |
4319 | 4334 #else |
4488 | 4335 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr; |
4336 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr; | |
4319 | 4337 #endif |
4338 | |
4339 vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); | |
4340 DictionaryType.tp_name = "vim.dictionary"; | |
4341 DictionaryType.tp_basicsize = sizeof(DictionaryObject); | |
4488 | 4342 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor; |
4319 | 4343 DictionaryType.tp_as_mapping = &DictionaryAsMapping; |
4344 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4345 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; | |
4346 DictionaryType.tp_methods = DictionaryMethods; | |
4347 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4348 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro; |
4349 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro; | |
4319 | 4350 #else |
4488 | 4351 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr; |
4352 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr; | |
4319 | 4353 #endif |
4354 | |
4355 vim_memset(&ListType, 0, sizeof(ListType)); | |
4356 ListType.tp_name = "vim.list"; | |
4488 | 4357 ListType.tp_dealloc = (destructor)ListDestructor; |
4319 | 4358 ListType.tp_basicsize = sizeof(ListObject); |
4359 ListType.tp_as_sequence = &ListAsSeq; | |
4360 ListType.tp_as_mapping = &ListAsMapping; | |
4361 ListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4362 ListType.tp_doc = "list pushing modifications to vim structure"; | |
4363 ListType.tp_methods = ListMethods; | |
4488 | 4364 ListType.tp_iter = (getiterfunc)ListIter; |
4319 | 4365 #if PY_MAJOR_VERSION >= 3 |
4488 | 4366 ListType.tp_getattro = (getattrofunc)ListGetattro; |
4367 ListType.tp_setattro = (setattrofunc)ListSetattro; | |
4319 | 4368 #else |
4488 | 4369 ListType.tp_getattr = (getattrfunc)ListGetattr; |
4370 ListType.tp_setattr = (setattrfunc)ListSetattr; | |
4319 | 4371 #endif |
4372 | |
4373 vim_memset(&FunctionType, 0, sizeof(FunctionType)); | |
4397 | 4374 FunctionType.tp_name = "vim.function"; |
4319 | 4375 FunctionType.tp_basicsize = sizeof(FunctionObject); |
4488 | 4376 FunctionType.tp_dealloc = (destructor)FunctionDestructor; |
4377 FunctionType.tp_call = (ternaryfunc)FunctionCall; | |
4319 | 4378 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; |
4379 FunctionType.tp_doc = "object that calls vim function"; | |
4380 FunctionType.tp_methods = FunctionMethods; | |
4381 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4382 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro; |
4319 | 4383 #else |
4488 | 4384 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr; |
4319 | 4385 #endif |
4386 | |
4350 | 4387 vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
4388 OptionsType.tp_name = "vim.options"; | |
4389 OptionsType.tp_basicsize = sizeof(OptionsObject); | |
4390 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4391 OptionsType.tp_doc = "object for manipulating options"; | |
4392 OptionsType.tp_as_mapping = &OptionsAsMapping; | |
4488 | 4393 OptionsType.tp_dealloc = (destructor)OptionsDestructor; |
4394 OptionsType.tp_traverse = (traverseproc)OptionsTraverse; | |
4395 OptionsType.tp_clear = (inquiry)OptionsClear; | |
4350 | 4396 |
4319 | 4397 #if PY_MAJOR_VERSION >= 3 |
4398 vim_memset(&vimmodule, 0, sizeof(vimmodule)); | |
4399 vimmodule.m_name = "vim"; | |
4400 vimmodule.m_doc = "Vim Python interface\n"; | |
4401 vimmodule.m_size = -1; | |
4402 vimmodule.m_methods = VimMethods; | |
4403 #endif | |
4404 } | |
4494 | 4405 |
4406 #define PYTYPE_READY(type) \ | |
4407 if (PyType_Ready(&type)) \ | |
4408 return -1; | |
4409 | |
4410 static int | |
4411 init_types() | |
4412 { | |
4413 PYTYPE_READY(IterType); | |
4414 PYTYPE_READY(BufferType); | |
4415 PYTYPE_READY(RangeType); | |
4416 PYTYPE_READY(WindowType); | |
4417 PYTYPE_READY(TabPageType); | |
4418 PYTYPE_READY(BufMapType); | |
4419 PYTYPE_READY(WinListType); | |
4420 PYTYPE_READY(TabListType); | |
4421 PYTYPE_READY(CurrentType); | |
4422 PYTYPE_READY(DictionaryType); | |
4423 PYTYPE_READY(ListType); | |
4424 PYTYPE_READY(FunctionType); | |
4425 PYTYPE_READY(OptionsType); | |
4426 PYTYPE_READY(OutputType); | |
4427 return 0; | |
4428 } | |
4429 | |
4430 static BufMapObject TheBufferMap = | |
4431 { | |
4432 PyObject_HEAD_INIT(&BufMapType) | |
4433 }; | |
4434 | |
4435 static WinListObject TheWindowList = | |
4436 { | |
4437 PyObject_HEAD_INIT(&WinListType) | |
4438 NULL | |
4439 }; | |
4440 | |
4441 static CurrentObject TheCurrent = | |
4442 { | |
4443 PyObject_HEAD_INIT(&CurrentType) | |
4444 }; | |
4445 | |
4446 static TabListObject TheTabPageList = | |
4447 { | |
4448 PyObject_HEAD_INIT(&TabListType) | |
4449 }; | |
4450 | |
4451 static struct numeric_constant { | |
4452 char *name; | |
4453 int value; | |
4454 } numeric_constants[] = { | |
4455 {"VAR_LOCKED", VAR_LOCKED}, | |
4456 {"VAR_FIXED", VAR_FIXED}, | |
4457 {"VAR_SCOPE", VAR_SCOPE}, | |
4458 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, | |
4459 }; | |
4460 | |
4461 static struct object_constant { | |
4462 char *name; | |
4463 PyObject *value; | |
4464 } object_constants[] = { | |
4465 {"buffers", (PyObject *)(void *)&TheBufferMap}, | |
4466 {"windows", (PyObject *)(void *)&TheWindowList}, | |
4467 {"tabpages", (PyObject *)(void *)&TheTabPageList}, | |
4468 {"current", (PyObject *)(void *)&TheCurrent}, | |
4496 | 4469 |
4470 {"Buffer", (PyObject *)&BufferType}, | |
4471 {"Range", (PyObject *)&RangeType}, | |
4472 {"Window", (PyObject *)&WindowType}, | |
4473 {"TabPage", (PyObject *)&TabPageType}, | |
4474 {"Dictionary", (PyObject *)&DictionaryType}, | |
4475 {"List", (PyObject *)&ListType}, | |
4476 {"Function", (PyObject *)&FunctionType}, | |
4477 {"Options", (PyObject *)&OptionsType}, | |
4494 | 4478 }; |
4479 | |
4480 typedef int (*object_adder)(PyObject *, const char *, PyObject *); | |
4481 | |
4482 #define ADD_OBJECT(m, name, obj) \ | |
4483 if (add_object(m, name, obj)) \ | |
4484 return -1; | |
4485 | |
4486 #define ADD_CHECKED_OBJECT(m, name, obj) \ | |
4487 { \ | |
4488 PyObject *value = obj; \ | |
4489 if (!value) \ | |
4490 return -1; \ | |
4491 ADD_OBJECT(m, name, value); \ | |
4492 } | |
4493 | |
4494 static int | |
4495 populate_module(PyObject *m, object_adder add_object) | |
4496 { | |
4497 int i; | |
4498 | |
4499 for (i = 0; i < (int)(sizeof(numeric_constants) | |
4500 / sizeof(struct numeric_constant)); | |
4501 ++i) | |
4502 ADD_CHECKED_OBJECT(m, numeric_constants[i].name, | |
4503 PyInt_FromLong(numeric_constants[i].value)); | |
4504 | |
4505 for (i = 0; i < (int)(sizeof(object_constants) | |
4506 / sizeof(struct object_constant)); | |
4507 ++i) | |
4508 { | |
4509 PyObject *value; | |
4510 | |
4511 value = object_constants[i].value; | |
4512 Py_INCREF(value); | |
4513 ADD_OBJECT(m, object_constants[i].name, value); | |
4514 } | |
4515 | |
4516 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) | |
4517 return -1; | |
4518 ADD_OBJECT(m, "error", VimError); | |
4519 | |
4520 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict)); | |
4521 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict)); | |
4522 ADD_CHECKED_OBJECT(m, "options", | |
4523 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL)); | |
4524 return 0; | |
4525 } |