1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* Python extensions by Paul Moore, David Leonard, Roland Puntaier.
*
* Common code for if_python.c and if_python3.c.
*/
/*
* obtain a lock on the Vim data structures
*/
static void
Python_Lock_Vim(void)
{
}
/*
* release a lock on the Vim data structures
*/
static void
Python_Release_Vim(void)
{
}
/* Output object definition
*/
static PyObject *OutputWrite(PyObject *, PyObject *);
static PyObject *OutputWritelines(PyObject *, PyObject *);
typedef void (*writefn)(char_u *);
static void writer(writefn fn, char_u *str, PyInt n);
typedef struct
{
PyObject_HEAD
long softspace;
long error;
} OutputObject;
static struct PyMethodDef OutputMethods[] = {
/* name, function, calling, documentation */
{"write", OutputWrite, 1, "" },
{"writelines", OutputWritelines, 1, "" },
{ NULL, NULL, 0, NULL }
};
/*************/
/* Output buffer management
*/
static PyObject *
OutputWrite(PyObject *self, PyObject *args)
{
int len;
char *str;
int error = ((OutputObject *)(self))->error;
if (!PyArg_ParseTuple(args, "s#", &str, &len))
return NULL;
Py_BEGIN_ALLOW_THREADS
Python_Lock_Vim();
writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Python_Release_Vim();
Py_END_ALLOW_THREADS
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
OutputWritelines(PyObject *self, PyObject *args)
{
PyInt n;
PyInt i;
PyObject *list;
int error = ((OutputObject *)(self))->error;
if (!PyArg_ParseTuple(args, "O", &list))
return NULL;
Py_INCREF(list);
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Py_DECREF(list);
return NULL;
}
n = PyList_Size(list);
for (i = 0; i < n; ++i)
{
PyObject *line = PyList_GetItem(list, i);
char *str;
PyInt len;
if (!PyArg_Parse(line, "s#", &str, &len)) {
PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
Py_DECREF(list);
return NULL;
}
Py_BEGIN_ALLOW_THREADS
Python_Lock_Vim();
writer((writefn)(error ? emsg : msg), (char_u *)str, len);
Python_Release_Vim();
Py_END_ALLOW_THREADS
}
Py_DECREF(list);
Py_INCREF(Py_None);
return Py_None;
}
static char_u *buffer = NULL;
static PyInt buffer_len = 0;
static PyInt buffer_size = 0;
static writefn old_fn = NULL;
static void
buffer_ensure(PyInt n)
{
PyInt new_size;
char_u *new_buffer;
if (n < buffer_size)
return;
new_size = buffer_size;
while (new_size < n)
new_size += 80;
if (new_size != buffer_size)
{
new_buffer = alloc((unsigned)new_size);
if (new_buffer == NULL)
return;
if (buffer)
{
memcpy(new_buffer, buffer, buffer_len);
vim_free(buffer);
}
buffer = new_buffer;
buffer_size = new_size;
}
}
static void
PythonIO_Flush(void)
{
if (old_fn && buffer_len)
{
buffer[buffer_len] = 0;
old_fn(buffer);
}
buffer_len = 0;
}
static void
writer(writefn fn, char_u *str, PyInt n)
{
char_u *ptr;
if (fn != old_fn && old_fn != NULL)
PythonIO_Flush();
old_fn = fn;
while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
{
PyInt len = ptr - str;
buffer_ensure(buffer_len + len + 1);
memcpy(buffer + buffer_len, str, len);
buffer_len += len;
buffer[buffer_len] = 0;
fn(buffer);
str = ptr + 1;
n -= len + 1;
buffer_len = 0;
}
/* Put the remaining text into the buffer for later printing */
buffer_ensure(buffer_len + n + 1);
memcpy(buffer + buffer_len, str, n);
buffer_len += n;
}
/***************/
static PyTypeObject OutputType;
static OutputObject Output =
{
PyObject_HEAD_INIT(&OutputType)
0,
0
};
static OutputObject Error =
{
PyObject_HEAD_INIT(&OutputType)
0,
1
};
static int
PythonIO_Init_io(void)
{
PySys_SetObject("stdout", (PyObject *)(void *)&Output);
PySys_SetObject("stderr", (PyObject *)(void *)&Error);
if (PyErr_Occurred())
{
EMSG(_("E264: Python: Error initialising I/O objects"));
return -1;
}
return 0;
}
static PyObject *VimError;
/* Check to see whether a Vim error has been reported, or a keyboard
* interrupt has been detected.
*/
static int
VimErrorCheck(void)
{
if (got_int)
{
PyErr_SetNone(PyExc_KeyboardInterrupt);
return 1;
}
else if (did_emsg && !PyErr_Occurred())
{
PyErr_SetNone(VimError);
return 1;
}
return 0;
}
|