annotate src/if_python.c @ 1087:c94bbf85eb16 v7.0.213

updated for version 7.0-213
author vimboss
date Thu, 08 Mar 2007 13:49:53 +0000
parents 1d12208efb37
children 23aeaff96141
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 * Python extensions by Paul Moore.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 * Changes for Unix by David Leonard.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 * This consists of four parts:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 * 1. Python interpreter main program
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 * 2. Python output stream: writes output via [e]msg().
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 * 3. Implementation of the Vim module for Python
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 * 4. Utility functions for handling the interface between Vim and Python.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 #include "vim.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 #include <limits.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 /* Python.h defines _POSIX_THREADS itself (if needed) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 #ifdef _POSIX_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 # undef _POSIX_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 #if defined(_WIN32) && defined (HAVE_FCNTL_H)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 # undef HAVE_FCNTL_H
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 #ifdef _DEBUG
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 # undef _DEBUG
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 #ifdef HAVE_STDARG_H
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 # undef HAVE_STDARG_H /* Python's config.h defines it as well. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 #include <Python.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 # include "macglue.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 # include <CodeFragments.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 #undef main /* Defined in python.h - aargh */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 #undef HAVE_FCNTL_H /* Clash with os_win32.h */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 #if !defined(FEAT_PYTHON) && defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 /* Use this to be able to generate prototypes without python being used. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 # define PyObject int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 # define PyThreadState int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 # define PyTypeObject int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 struct PyMethodDef { int a; };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 # define PySequenceMethods int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 /* Parser flags */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 #define single_input 256
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 #define file_input 257
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 #define eval_input 258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 /* Python 2.3: can invoke ":python" recursively. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 # define PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 #if defined(DYNAMIC_PYTHON) || defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 # ifndef DYNAMIC_PYTHON
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 # define HINSTANCE int /* for generating prototypes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 * Wrapper defines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 # define PyArg_Parse dll_PyArg_Parse
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 # define PyArg_ParseTuple dll_PyArg_ParseTuple
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 # define PyDict_SetItemString dll_PyDict_SetItemString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 # define PyErr_BadArgument dll_PyErr_BadArgument
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 # define PyErr_Clear dll_PyErr_Clear
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 # define PyErr_NoMemory dll_PyErr_NoMemory
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 # define PyErr_Occurred dll_PyErr_Occurred
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 # define PyErr_SetNone dll_PyErr_SetNone
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 # define PyErr_SetString dll_PyErr_SetString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 # define PyEval_InitThreads dll_PyEval_InitThreads
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 # define PyEval_RestoreThread dll_PyEval_RestoreThread
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 # define PyEval_SaveThread dll_PyEval_SaveThread
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 # ifdef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 # define PyGILState_Ensure dll_PyGILState_Ensure
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 # define PyGILState_Release dll_PyGILState_Release
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 # define PyInt_AsLong dll_PyInt_AsLong
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 # define PyInt_FromLong dll_PyInt_FromLong
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 # define PyInt_Type (*dll_PyInt_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 # define PyList_GetItem dll_PyList_GetItem
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
96 # define PyList_Append dll_PyList_Append
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 # define PyList_New dll_PyList_New
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 # define PyList_SetItem dll_PyList_SetItem
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 # define PyList_Size dll_PyList_Size
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 # define PyList_Type (*dll_PyList_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 # define PyImport_ImportModule dll_PyImport_ImportModule
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
102 # define PyDict_New dll_PyDict_New
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 # define PyDict_GetItemString dll_PyDict_GetItemString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 # define PyModule_GetDict dll_PyModule_GetDict
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 # define PyRun_SimpleString dll_PyRun_SimpleString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 # define PyString_AsString dll_PyString_AsString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 # define PyString_FromString dll_PyString_FromString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 # define PyString_Size dll_PyString_Size
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 # define PyString_Type (*dll_PyString_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 # define PySys_SetObject dll_PySys_SetObject
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 # define PySys_SetArgv dll_PySys_SetArgv
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 # define PyType_Type (*dll_PyType_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 # define Py_BuildValue dll_Py_BuildValue
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 # define Py_FindMethod dll_Py_FindMethod
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 # define Py_InitModule4 dll_Py_InitModule4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 # define Py_Initialize dll_Py_Initialize
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
118 # define Py_Finalize dll_Py_Finalize
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
119 # define Py_IsInitialized dll_Py_IsInitialized
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 # define _PyObject_New dll__PyObject_New
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 # define _Py_NoneStruct (*dll__Py_NoneStruct)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 # define PyObject_Init dll__PyObject_Init
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 # define PyType_IsSubtype dll_PyType_IsSubtype
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 # define PyObject_Malloc dll_PyObject_Malloc
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 # define PyObject_Free dll_PyObject_Free
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 * Pointers for dynamic link
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 static int(*dll_PyErr_BadArgument)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 static void(*dll_PyErr_Clear)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 static PyObject*(*dll_PyErr_NoMemory)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 static PyObject*(*dll_PyErr_Occurred)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 static void(*dll_PyErr_SetNone)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 static void(*dll_PyErr_SetString)(PyObject *, const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 static void(*dll_PyEval_InitThreads)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 static void(*dll_PyEval_RestoreThread)(PyThreadState *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 static PyThreadState*(*dll_PyEval_SaveThread)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 # ifdef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 static void (*dll_PyGILState_Release)(PyGILState_STATE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 static long(*dll_PyInt_AsLong)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 static PyObject*(*dll_PyInt_FromLong)(long);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 static PyTypeObject* dll_PyInt_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 static PyObject*(*dll_PyList_GetItem)(PyObject *, int);
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
154 static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 static PyObject*(*dll_PyList_New)(int size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 static int(*dll_PyList_SetItem)(PyObject *, int, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157 static int(*dll_PyList_Size)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 static PyTypeObject* dll_PyList_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 static PyObject*(*dll_PyImport_ImportModule)(const char *);
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
160 static PyObject*(*dll_PyDict_New)(void);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 static PyObject*(*dll_PyModule_GetDict)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 static int(*dll_PyRun_SimpleString)(char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 static char*(*dll_PyString_AsString)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165 static PyObject*(*dll_PyString_FromString)(const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 static int(*dll_PyString_Size)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 static PyTypeObject* dll_PyString_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169 static int(*dll_PySys_SetObject)(char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 static int(*dll_PySys_SetArgv)(int, char **);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 static PyTypeObject* dll_PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 static PyObject*(*dll_Py_BuildValue)(char *, ...);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 static void(*dll_Py_Initialize)(void);
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
176 static void(*dll_Py_Finalize)(void);
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
177 static int(*dll_Py_IsInitialized)(void);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 static PyObject* dll__Py_NoneStruct;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 static void* (*dll_PyObject_Malloc)(size_t);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186 static void (*dll_PyObject_Free)(void*);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 static HINSTANCE hinstPython = 0; /* Instance of python.dll */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191 /* Imported exception objects */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 static PyObject *imp_PyExc_AttributeError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 static PyObject *imp_PyExc_IndexError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 static PyObject *imp_PyExc_KeyboardInterrupt;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195 static PyObject *imp_PyExc_TypeError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 static PyObject *imp_PyExc_ValueError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 # define PyExc_AttributeError imp_PyExc_AttributeError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199 # define PyExc_IndexError imp_PyExc_IndexError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201 # define PyExc_TypeError imp_PyExc_TypeError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202 # define PyExc_ValueError imp_PyExc_ValueError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205 * Table of name to function pointer of python.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207 # define PYTHON_PROC FARPROC
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208 static struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 char *name;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211 PYTHON_PROC *ptr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 } python_funcname_table[] =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 # ifdef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
234 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
241 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
256 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
257 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 {"", NULL},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 * Free python.dll
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275 end_dynamic_python(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277 if (hinstPython)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 FreeLibrary(hinstPython);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280 hinstPython = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285 * Load library and get all pointers.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 * Parameter 'libname' provides name of DLL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 * Return OK or FAIL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 python_runtime_link_init(char *libname, int verbose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
293
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 if (hinstPython)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296 hinstPython = LoadLibrary(libname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297 if (!hinstPython)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 if (verbose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300 EMSG2(_(e_loadlib), libname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304 for (i = 0; python_funcname_table[i].ptr; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
305 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 if ((*python_funcname_table[i].ptr = GetProcAddress(hinstPython,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307 python_funcname_table[i].name)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 FreeLibrary(hinstPython);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 hinstPython = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311 if (verbose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320 * If python is enabled (there is installed python on Windows system) return
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 * TRUE, else FALSE.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 python_enabled(verbose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 int verbose;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330 /* Load the standard Python exceptions - don't import the symbols from the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331 * DLL, as this can cause errors (importing data symbols is not reliable).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333 static void get_exceptions __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336 get_exceptions()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338 PyObject *exmod = PyImport_ImportModule("exceptions");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 PyObject *exdict = PyModule_GetDict(exmod);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 Py_XINCREF(imp_PyExc_AttributeError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 Py_XINCREF(imp_PyExc_IndexError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348 Py_XINCREF(imp_PyExc_TypeError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 Py_XINCREF(imp_PyExc_ValueError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350 Py_XDECREF(exmod);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 #endif /* DYNAMIC_PYTHON */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 * Internal function prototypes.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358 static void DoPythonCommand(exarg_T *, const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359 static int RangeStart;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 static int RangeEnd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 static void PythonIO_Flush(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
363 static int PythonIO_Init(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364 static int PythonMod_Init(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
365
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366 /* Utility functions for the vim/python interface
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367 * ----------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 static PyObject *GetBufferLine(buf_T *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370 static PyObject *GetBufferLineList(buf_T *, int, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372 static int SetBufferLine(buf_T *, int, PyObject *, int *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373 static int SetBufferLineList(buf_T *, int, int, PyObject *, int *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374 static int InsertBufferLines(buf_T *, int, PyObject *, int *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
376 static PyObject *LineToString(const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377 static char *StringToLine(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379 static int VimErrorCheck(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 #define PyErr_SetVim(str) PyErr_SetString(VimError, str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 * 1. Python interpreter main program.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 static int initialised = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390 typedef PyObject PyThreadState;
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
391 #endif
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
392
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
393 #ifdef PY_CAN_RECURSE
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
394 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
395 #else
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
396 static PyThreadState *saved_python_thread = NULL;
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
397 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
398
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400 * Suspend a thread of the Python interpreter, other threads are allowed to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 * run.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402 */
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
403 static void
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
404 Python_SaveThread(void)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405 {
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
406 #ifdef PY_CAN_RECURSE
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
407 PyGILState_Release(pygilstate);
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
408 #else
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409 saved_python_thread = PyEval_SaveThread();
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
410 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
411 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
412
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 * Restore a thread of the Python interpreter, waits for other threads to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415 * block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416 */
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
417 static void
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
418 Python_RestoreThread(void)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419 {
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
420 #ifdef PY_CAN_RECURSE
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
421 pygilstate = PyGILState_Ensure();
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
422 #else
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423 PyEval_RestoreThread(saved_python_thread);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424 saved_python_thread = NULL;
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
425 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
429 * obtain a lock on the Vim data structures
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
430 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
431 static void Python_Lock_Vim(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
432 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
433 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
434
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
435 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436 * release a lock on the Vim data structures
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438 static void Python_Release_Vim(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
441
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
442 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 python_end()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
444 {
557
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
445 static int recurse = 0;
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
446
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
447 /* If a crash occurs while doing this, don't try again. */
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
448 if (recurse != 0)
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
449 return;
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
450
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
451 ++recurse;
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
452
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 #ifdef DYNAMIC_PYTHON
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
454 if (hinstPython && Py_IsInitialized())
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
455 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
456 Python_RestoreThread(); /* enter python */
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
457 Py_Finalize();
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
458 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
459 end_dynamic_python();
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
460 #else
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
461 if (Py_IsInitialized())
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
462 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
463 Python_RestoreThread(); /* enter python */
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
464 Py_Finalize();
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
465 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 #endif
557
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
467
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
468 --recurse;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472 Python_Init(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 if (!initialised)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476 #ifdef DYNAMIC_PYTHON
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477 if (!python_enabled(TRUE))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 goto fail;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
483
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
484 #if !defined(MACOS) || defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
485 Py_Initialize();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
486 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
487 PyMac_Initialize();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
488 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
489 /* initialise threads */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
490 PyEval_InitThreads();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
492 #ifdef DYNAMIC_PYTHON
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
493 get_exceptions();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
494 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
495
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
496 if (PythonIO_Init())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 goto fail;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
498
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
499 if (PythonMod_Init())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
500 goto fail;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
501
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
502 /* the first python thread is vim's, release the lock */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
503 Python_SaveThread();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
505 initialised = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
506 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510 fail:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511 /* We call PythonIO_Flush() here to print any Python errors.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 * This is OK, as it is possible to call this function even
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 * if PythonIO_Init() has not completed successfully (it will
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514 * not do anything in this case).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 PythonIO_Flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521 * External interface
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524 DoPythonCommand(exarg_T *eap, const char *cmd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525 {
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
526 #ifndef PY_CAN_RECURSE
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 static int recursive = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
530 GrafPtr oldPort;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
531 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533 char *saved_locale;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536 #ifndef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 if (recursive)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539 EMSG(_("E659: Cannot invoke Python recursively"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
540 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
541 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
542 ++recursive;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
543 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
544
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
545 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
546 GetPort(&oldPort);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 /* Check if the Python library is available */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549 goto theend;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551 if (Python_Init())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 goto theend;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554 RangeStart = eap->line1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555 RangeEnd = eap->line2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 Python_Release_Vim(); /* leave vim */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559 /* Python only works properly when the LC_NUMERIC locale is "C". */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560 saved_locale = setlocale(LC_NUMERIC, NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562 saved_locale = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565 /* Need to make a copy, value may change when setting new locale. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 (void)setlocale(LC_NUMERIC, "C");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
570
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571 Python_RestoreThread(); /* enter python */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 PyRun_SimpleString((char *)(cmd));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 Python_SaveThread(); /* leave python */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
578 if (saved_locale != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
579 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580 (void)setlocale(LC_NUMERIC, saved_locale);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581 vim_free(saved_locale);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
585 Python_Lock_Vim(); /* enter vim */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
586 PythonIO_Flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
587 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
588 SetPort(oldPort);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
589 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
590
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
591 theend:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
592 #ifndef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
593 --recursive;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
594 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
595 return; /* keeps lint happy */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
596 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
597
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
598 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
599 * ":python"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
600 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
601 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
602 ex_python(exarg_T *eap)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
603 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
604 char_u *script;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
605
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
606 script = script_get(eap, eap->arg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
607 if (!eap->skip)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
608 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
609 if (script == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
610 DoPythonCommand(eap, (char *)eap->arg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
611 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
612 DoPythonCommand(eap, (char *)script);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
613 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
614 vim_free(script);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
615 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
616
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
617 #define BUFFER_SIZE 1024
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
618
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
619 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
620 * ":pyfile"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
621 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
622 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
623 ex_pyfile(exarg_T *eap)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
624 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
625 static char buffer[BUFFER_SIZE];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
626 const char *file = (char *)eap->arg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
627 char *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
628
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
629 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
630 * stdio file pointer, but Vim and the Python DLL are compiled with
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
631 * different options under Windows, meaning that stdio pointers aren't
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
632 * compatible between the two. Yuk.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
633 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
634 * Put the string "execfile('file')" into buffer. But, we need to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
635 * escape any backslashes or single quotes in the file name, so that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
636 * Python won't mangle the file name.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
637 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
638 strcpy(buffer, "execfile('");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
639 p = buffer + 10; /* size of "execfile('" */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
640
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
641 while (*file && p < buffer + (BUFFER_SIZE - 3))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
642 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
643 if (*file == '\\' || *file == '\'')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
644 *p++ = '\\';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
645 *p++ = *file++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
646 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
647
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
648 /* If we didn't finish the file name, we hit a buffer overflow */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
649 if (*file != '\0')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
650 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
651
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
652 /* Put in the terminating "')" and a null */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
653 *p++ = '\'';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
654 *p++ = ')';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
655 *p++ = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
656
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
657 /* Execute the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
658 DoPythonCommand(eap, buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
659 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
660
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
661 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
662 * 2. Python output stream: writes output via [e]msg().
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
663 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
664
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
665 /* Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
666 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
667
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
668 static PyObject *OutputGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
669 static int OutputSetattr(PyObject *, char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
670
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
671 static PyObject *OutputWrite(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
672 static PyObject *OutputWritelines(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
673
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
674 typedef void (*writefn)(char_u *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
675 static void writer(writefn fn, char_u *str, int n);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
676
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
677 /* Output object definition
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
678 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
679
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
680 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
681 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
682 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
683 long softspace;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
684 long error;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
685 } OutputObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
686
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
687 static struct PyMethodDef OutputMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
688 /* name, function, calling, documentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
689 {"write", OutputWrite, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
690 {"writelines", OutputWritelines, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
691 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
692 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
693
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
694 static PyTypeObject OutputType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
695 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
696 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
697 "message",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
698 sizeof(OutputObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
699 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
700
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
701 (destructor) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
702 (printfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
703 (getattrfunc) OutputGetattr,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
704 (setattrfunc) OutputSetattr,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
705 (cmpfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
706 (reprfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
707
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
708 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
709 0, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
710 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
711
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
712 (hashfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
713 (ternaryfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
714 (reprfunc) 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
715 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
716
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
717 /*************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
718
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
719 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
720 OutputGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
721 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
722 if (strcmp(name, "softspace") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
723 return PyInt_FromLong(((OutputObject *)(self))->softspace);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
724
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
725 return Py_FindMethod(OutputMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
726 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
727
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
728 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
729 OutputSetattr(PyObject *self, char *name, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
730 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
731 if (val == NULL) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
732 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
733 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
734 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
735
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
736 if (strcmp(name, "softspace") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
737 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
738 if (!PyInt_Check(val)) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
739 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
740 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
741 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
742
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
743 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
744 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
745 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
746
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
747 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
748 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
749 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
750
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
751 /*************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
752
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
753 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
754 OutputWrite(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
755 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
756 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
757 char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
758 int error = ((OutputObject *)(self))->error;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
759
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
760 if (!PyArg_ParseTuple(args, "s#", &str, &len))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
761 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
762
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
763 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
764 Python_Lock_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
765 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
766 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
767 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
768
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
769 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
770 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
771 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
772
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
773 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
774 OutputWritelines(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
775 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
776 int n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
777 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
778 PyObject *list;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
779 int error = ((OutputObject *)(self))->error;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
780
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
781 if (!PyArg_ParseTuple(args, "O", &list))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
782 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
783 Py_INCREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
784
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
785 if (!PyList_Check(list)) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
786 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
787 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
788 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
789 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
790
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
791 n = PyList_Size(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
792
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
793 for (i = 0; i < n; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
794 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
795 PyObject *line = PyList_GetItem(list, i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
796 char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
797 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
798
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
799 if (!PyArg_Parse(line, "s#", &str, &len)) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
800 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
801 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
802 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
803 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
804
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
805 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
806 Python_Lock_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
807 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
808 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
809 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
810 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
811
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
812 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
813 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
814 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
815 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
816
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
817 /* Output buffer management
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
818 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
819
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
820 static char_u *buffer = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
821 static int buffer_len = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
822 static int buffer_size = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
823
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
824 static writefn old_fn = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
825
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
826 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
827 buffer_ensure(int n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
828 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
829 int new_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
830 char_u *new_buffer;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
831
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
832 if (n < buffer_size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
833 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
834
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
835 new_size = buffer_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
836 while (new_size < n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
837 new_size += 80;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
838
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
839 if (new_size != buffer_size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
840 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
841 new_buffer = alloc((unsigned)new_size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
842 if (new_buffer == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
843 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
844
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
845 if (buffer)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
846 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
847 memcpy(new_buffer, buffer, buffer_len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
848 vim_free(buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
849 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
850
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
851 buffer = new_buffer;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
852 buffer_size = new_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
853 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
854 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
855
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
856 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
857 PythonIO_Flush(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
858 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
859 if (old_fn && buffer_len)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
860 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
861 buffer[buffer_len] = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
862 old_fn(buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
863 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
864
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
865 buffer_len = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
866 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
867
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
868 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
869 writer(writefn fn, char_u *str, int n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
870 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
871 char_u *ptr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
872
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
873 if (fn != old_fn && old_fn != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
874 PythonIO_Flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
875
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
876 old_fn = fn;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
877
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
878 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
879 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
880 int len = ptr - str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
881
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
882 buffer_ensure(buffer_len + len + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
883
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
884 memcpy(buffer + buffer_len, str, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
885 buffer_len += len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
886 buffer[buffer_len] = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
887 fn(buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
888 str = ptr + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
889 n -= len + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
890 buffer_len = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
891 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
892
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
893 /* Put the remaining text into the buffer for later printing */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
894 buffer_ensure(buffer_len + n + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
895 memcpy(buffer + buffer_len, str, n);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
896 buffer_len += n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
897 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
898
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
899 /***************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
900
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
901 static OutputObject Output =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
902 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
903 PyObject_HEAD_INIT(&OutputType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
904 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
905 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
906 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
907
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
908 static OutputObject Error =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
909 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
910 PyObject_HEAD_INIT(&OutputType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
911 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
912 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
913 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
914
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
915 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
916 PythonIO_Init(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
917 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
918 /* Fixups... */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
919 OutputType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
920
135
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
921 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
922 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
923
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
924 if (PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
925 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
926 EMSG(_("E264: Python: Error initialising I/O objects"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
927 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
928 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
929
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
930 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
931 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
932
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
933 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
934 * 3. Implementation of the Vim module for Python
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
935 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
936
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
937 /* Vim module - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
938 * -------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
939 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
940
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
941 static PyObject *VimError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
942
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
943 static PyObject *VimCommand(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
944 static PyObject *VimEval(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
945
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
946 /* Window type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
947 * --------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
948 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
949
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
950 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
951 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
952 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
953 win_T *win;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
954 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
955 WindowObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
956
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
957 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
958
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
959 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
960
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
961 static PyObject *WindowNew(win_T *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
962
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
963 static void WindowDestructor(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
964 static PyObject *WindowGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
965 static int WindowSetattr(PyObject *, char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
966 static PyObject *WindowRepr(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
967
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
968 /* Buffer type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
969 * --------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
970 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
971
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
972 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
973 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
974 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
975 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
976 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
977 BufferObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
978
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
979 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
980
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
981 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
982
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
983 static PyObject *BufferNew (buf_T *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
984
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
985 static void BufferDestructor(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
986 static PyObject *BufferGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
987 static PyObject *BufferRepr(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
988
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
989 static int BufferLength(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
990 static PyObject *BufferItem(PyObject *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
991 static PyObject *BufferSlice(PyObject *, int, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
992 static int BufferAssItem(PyObject *, int, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
993 static int BufferAssSlice(PyObject *, int, int, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
994
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
995 static PyObject *BufferAppend(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
996 static PyObject *BufferMark(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
997 static PyObject *BufferRange(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
998
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
999 /* Line range type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1000 * --------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1001 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1002
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1003 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1004 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1005 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1006 BufferObject *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1007 int start;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1008 int end;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1009 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1010 RangeObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1011
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1012 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1013
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1014 static PyObject *RangeNew(buf_T *, int, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1015
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1016 static void RangeDestructor(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1017 static PyObject *RangeGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1018 static PyObject *RangeRepr(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1019
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1020 static int RangeLength(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1021 static PyObject *RangeItem(PyObject *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1022 static PyObject *RangeSlice(PyObject *, int, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1023 static int RangeAssItem(PyObject *, int, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1024 static int RangeAssSlice(PyObject *, int, int, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1025
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1026 static PyObject *RangeAppend(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1027
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1028 /* Window list type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1029 * -------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1030 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1031
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1032 static int WinListLength(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1033 static PyObject *WinListItem(PyObject *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1034
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1035 /* Buffer list type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1036 * -------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1037 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1038
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1039 static int BufListLength(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1040 static PyObject *BufListItem(PyObject *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1041
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1042 /* Current objects type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1043 * -----------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1044 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1045
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1046 static PyObject *CurrentGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1047 static int CurrentSetattr(PyObject *, char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1048
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1049 /* Vim module - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1050 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1051
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1052 static struct PyMethodDef VimMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1053 /* name, function, calling, documentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1054 {"command", VimCommand, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1055 {"eval", VimEval, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1056 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1057 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1058
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1059 /* Vim module - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1060 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1061 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1062 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1063 VimCommand(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1064 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1065 char *cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1066 PyObject *result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1067
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1068 if (!PyArg_ParseTuple(args, "s", &cmd))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1069 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1070
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1071 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1072
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1073 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1074 Python_Lock_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1075
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1076 do_cmdline_cmd((char_u *)cmd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1077 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1078
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1079 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1080 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1081
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1082 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1083 result = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1084 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1085 result = Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1086
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1087 Py_XINCREF(result);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1088 return result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1089 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1090
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1091 /*
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1092 * Function to translate a typval_T into a PyObject; this will recursively
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1093 * translate lists/dictionaries into their Python equivalents.
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1094 *
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1095 * The depth parameter is too avoid infinite recursion, set it to 1 when
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1096 * you call VimToPython.
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1097 */
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1098 static PyObject *
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1099 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1100 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1101 PyObject *result;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1102 PyObject *newObj;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1103 char ptrBuf[NUMBUFLEN];
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1104
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1105 /* Avoid infinite recursion */
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1106 if (depth > 100)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1107 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1108 Py_INCREF(Py_None);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1109 result = Py_None;
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1110 return result;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1111 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1112
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1113 /* Check if we run into a recursive loop. The item must be in lookupDict
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1114 * then and we can use it again. */
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1115 sprintf(ptrBuf, "%ld", (long)our_tv);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1116 result = PyDict_GetItemString(lookupDict, ptrBuf);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1117 if (result != NULL)
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1118 Py_INCREF(result);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1119 else if (our_tv->v_type == VAR_STRING)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1120 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1121 result = Py_BuildValue("s", our_tv->vval.v_string);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1122 PyDict_SetItemString(lookupDict, ptrBuf, result);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1123 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1124 else if (our_tv->v_type == VAR_NUMBER)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1125 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1126 char buf[NUMBUFLEN];
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1127
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1128 /* For backwards compatibility numbers are stored as strings. */
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1129 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1130 result = Py_BuildValue("s", buf);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1131 PyDict_SetItemString(lookupDict, ptrBuf, result);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1132 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1133 else if (our_tv->v_type == VAR_LIST)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1134 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1135 list_T *list = our_tv->vval.v_list;
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1136 listitem_T *curr;
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1137
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1138 result = PyList_New(0);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1139 PyDict_SetItemString(lookupDict, ptrBuf, result);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1140
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1141 if (list != NULL)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1142 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1143 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1144 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1145 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1146 PyList_Append(result, newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1147 Py_DECREF(newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1148 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1149 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1150 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1151 else if (our_tv->v_type == VAR_DICT)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1152 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1153 result = PyDict_New();
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1154 PyDict_SetItemString(lookupDict, ptrBuf, result);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1155
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1156 if (our_tv->vval.v_dict != NULL)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1157 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1158 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1159 int todo = ht->ht_used;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1160 hashitem_T *hi;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1161 dictitem_T *di;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1162
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1163 for (hi = ht->ht_array; todo > 0; ++hi)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1164 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1165 if (!HASHITEM_EMPTY(hi))
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1166 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1167 --todo;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1168
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1169 di = dict_lookup(hi);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1170 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1171 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1172 Py_DECREF(newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1173 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1174 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1175 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1176 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1177 else
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1178 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1179 Py_INCREF(Py_None);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1180 result = Py_None;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1181 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1182
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1183 return result;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1184 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1185
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1186 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1187 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1188 VimEval(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1189 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1190 #ifdef FEAT_EVAL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1191 char *expr;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1192 typval_T *our_tv;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1193 PyObject *result;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1194 PyObject *lookup_dict;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1195
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1196 if (!PyArg_ParseTuple(args, "s", &expr))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1197 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1198
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1199 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1200 Python_Lock_Vim();
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1201 our_tv = eval_expr((char_u *)expr, NULL);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1202
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1203 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1204 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1205
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1206 if (our_tv == NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1207 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1208 PyErr_SetVim(_("invalid expression"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1209 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1210 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1211
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1212 /* Convert the Vim type into a Python type. Create a dictionary that's
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1213 * used to check for recursive loops. */
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1214 lookup_dict = PyDict_New();
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1215 result = VimToPython(our_tv, 1, lookup_dict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1216 Py_DECREF(lookup_dict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1217
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1218
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1219 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1220 Python_Lock_Vim();
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1221 free_tv(our_tv);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1222 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1223 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1224
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1225 return result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1226 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1227 PyErr_SetVim(_("expressions disabled at compile time"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1228 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1229 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1230 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1231
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1232 /* Common routines for buffers and line ranges
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1233 * -------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1234 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1235 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1236 CheckBuffer(BufferObject *this)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1237 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1238 if (this->buf == INVALID_BUFFER_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1239 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1240 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1241 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1242 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1243
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1244 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1245 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1246
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1247 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1248 RBItem(BufferObject *self, int n, int start, int end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1249 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1250 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1251 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1252
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1253 if (n < 0 || n > end - start)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1254 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1255 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1256 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1257 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1259 return GetBufferLine(self->buf, n+start);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1260 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1261
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1262 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1263 RBSlice(BufferObject *self, int lo, int hi, int start, int end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1264 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1265 int size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1266
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1267 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1268 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1269
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1270 size = end - start + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1271
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1272 if (lo < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1273 lo = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1274 else if (lo > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1275 lo = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1276 if (hi < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1277 hi = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1278 if (hi < lo)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1279 hi = lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1280 else if (hi > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1281 hi = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1282
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1283 return GetBufferLineList(self->buf, lo+start, hi+start);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1284 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1285
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1286 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1287 RBAssItem(BufferObject *self, int n, PyObject *val, int start, int end, int *new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1288 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1289 int len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1290
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1291 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1292 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1293
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1294 if (n < 0 || n > end - start)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1295 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1296 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1297 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1298 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1299
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1300 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1301 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1302
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1303 if (new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1304 *new_end = end + len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1305
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1306 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1307 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1308
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1309 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1310 RBAssSlice(BufferObject *self, int lo, int hi, PyObject *val, int start, int end, int *new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1311 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1312 int size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1313 int len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1314
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1315 /* Self must be a valid buffer */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1316 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1317 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1318
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1319 /* Sort out the slice range */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1320 size = end - start + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1321
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1322 if (lo < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1323 lo = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1324 else if (lo > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1325 lo = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1326 if (hi < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1327 hi = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1328 if (hi < lo)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1329 hi = lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1330 else if (hi > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1331 hi = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1332
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1333 if (SetBufferLineList(self->buf, lo+start, hi+start, val, &len_change) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1334 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1335
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1336 if (new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1337 *new_end = end + len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1338
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1339 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1340 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1341
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1342 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1343 RBAppend(BufferObject *self, PyObject *args, int start, int end, int *new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1344 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1345 PyObject *lines;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1346 int len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1347 int max;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1348 int n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1349
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1350 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1351 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1352
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1353 max = n = end - start + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1354
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1355 if (!PyArg_ParseTuple(args, "O|i", &lines, &n))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1356 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1357
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1358 if (n < 0 || n > max)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1359 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1360 PyErr_SetString(PyExc_ValueError, _("line number out of range"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1361 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1362 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1363
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1364 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1365 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1366
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1367 if (new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1368 *new_end = end + len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1369
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1370 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1371 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1372 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1373
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1374
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1375 /* Buffer object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1376 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1377
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1378 static struct PyMethodDef BufferMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1379 /* name, function, calling, documentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1380 {"append", BufferAppend, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1381 {"mark", BufferMark, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1382 {"range", BufferRange, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1383 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1384 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1385
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1386 static PySequenceMethods BufferAsSeq = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1387 (inquiry) BufferLength, /* sq_length, len(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1388 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1389 (intargfunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1390 (intargfunc) BufferItem, /* sq_item, x[i] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1391 (intintargfunc) BufferSlice, /* sq_slice, x[i:j] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1392 (intobjargproc) BufferAssItem, /* sq_ass_item, x[i]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1393 (intintobjargproc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1394 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1395
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1396 static PyTypeObject BufferType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1397 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1398 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1399 "buffer",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1400 sizeof(BufferObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1401 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1402
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1403 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1404 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1405 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1406 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1407 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1408 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1409
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1410 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1411 &BufferAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1412 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1413
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1414 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1415 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1416 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1417 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1418
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1419 /* Buffer object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1420 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1421
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1422 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1423 BufferNew(buf_T *buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1424 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1425 /* We need to handle deletion of buffers underneath us.
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1426 * If we add a "b_python_ref" field to the buf_T structure,
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1427 * then we can get at it in buf_freeall() in vim. We then
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1428 * need to create only ONE Python object per buffer - if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1429 * we try to create a second, just INCREF the existing one
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1430 * and return it. The (single) Python object referring to
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1431 * the buffer is stored in "b_python_ref".
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1432 * Question: what to do on a buf_freeall(). We'll probably
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1433 * have to either delete the Python object (DECREF it to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1434 * zero - a bad idea, as it leaves dangling refs!) or
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1435 * set the buf_T * value to an invalid value (-1?), which
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1436 * means we need checks in all access functions... Bah.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1437 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1438
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1439 BufferObject *self;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1440
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1441 if (buf->b_python_ref != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1442 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1443 self = buf->b_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1444 Py_INCREF(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1445 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1446 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1447 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1448 self = PyObject_NEW(BufferObject, &BufferType);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1449 if (self == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1450 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1451 self->buf = buf;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1452 buf->b_python_ref = self;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1453 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1454
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1455 return (PyObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1456 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1457
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1458 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1459 BufferDestructor(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1460 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1461 BufferObject *this = (BufferObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1462
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1463 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1464 this->buf->b_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1465
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
1466 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1467 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1468
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1469 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1470 BufferGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1471 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1472 BufferObject *this = (BufferObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1473
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1474 if (CheckBuffer(this))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1475 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1476
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1477 if (strcmp(name, "name") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1478 return Py_BuildValue("s",this->buf->b_ffname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1479 else if (strcmp(name, "number") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1480 return Py_BuildValue("i",this->buf->b_fnum);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1481 else if (strcmp(name,"__members__") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1482 return Py_BuildValue("[ss]", "name", "number");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1483 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1484 return Py_FindMethod(BufferMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1485 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1486
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1487 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1488 BufferRepr(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1489 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1490 static char repr[100];
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1491 BufferObject *this = (BufferObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1492
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1493 if (this->buf == INVALID_BUFFER_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1494 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1495 vim_snprintf(repr, 100, _("<buffer object (deleted) at %8lX>"),
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1496 (long)(self));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1497 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1498 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1499 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1500 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1501 char *name = (char *)this->buf->b_fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1502 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1503
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1504 if (name == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1505 name = "";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1506 len = strlen(name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1507
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1508 if (len > 35)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1509 name = name + (35 - len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1510
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1511 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1512
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1513 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1514 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1515 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1516
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1517 /******************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1518
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1519 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1520 BufferLength(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1521 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1522 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1523 if (CheckBuffer((BufferObject *)(self)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1524 return -1; /* ??? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1525
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1526 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1527 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1528
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1529 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1530 BufferItem(PyObject *self, int n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1531 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1532 return RBItem((BufferObject *)(self), n, 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1533 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1534 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1535
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1536 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1537 BufferSlice(PyObject *self, int lo, int hi)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1538 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1539 return RBSlice((BufferObject *)(self), lo, hi, 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1540 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1541 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1542
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1543 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1544 BufferAssItem(PyObject *self, int n, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1545 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1546 return RBAssItem((BufferObject *)(self), n, val, 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1547 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1548 NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1549 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1550
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1551 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1552 BufferAssSlice(PyObject *self, int lo, int hi, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1553 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1554 return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1555 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1556 NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1557 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1558
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1559 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1560 BufferAppend(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1561 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1562 return RBAppend((BufferObject *)(self), args, 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1563 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1564 NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1565 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1566
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1567 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1568 BufferMark(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1569 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1570 pos_T *posp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1571 char mark;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1572 buf_T *curbuf_save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1573
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1574 if (CheckBuffer((BufferObject *)(self)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1575 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1576
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1577 if (!PyArg_ParseTuple(args, "c", &mark))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1578 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1579
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1580 curbuf_save = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1581 curbuf = ((BufferObject *)(self))->buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1582 posp = getmark(mark, FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1583 curbuf = curbuf_save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1584
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1585 if (posp == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1586 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1587 PyErr_SetVim(_("invalid mark name"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1588 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1589 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1590
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1591 /* Ckeck for keyboard interrupt */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1592 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1593 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1594
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1595 if (posp->lnum <= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1596 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1597 /* Or raise an error? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1598 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1599 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1600 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1601
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1602 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1603 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1604
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1605 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1606 BufferRange(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1607 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1608 int start;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1609 int end;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1610
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1611 if (CheckBuffer((BufferObject *)(self)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1612 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1613
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1614 if (!PyArg_ParseTuple(args, "ii", &start, &end))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1615 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1616
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1617 return RangeNew(((BufferObject *)(self))->buf, start, end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1618 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1619
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1620 /* Line range object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1621 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1622
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1623 static struct PyMethodDef RangeMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1624 /* name, function, calling, documentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1625 {"append", RangeAppend, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1626 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1627 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1628
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1629 static PySequenceMethods RangeAsSeq = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1630 (inquiry) RangeLength, /* sq_length, len(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1631 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1632 (intargfunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1633 (intargfunc) RangeItem, /* sq_item, x[i] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1634 (intintargfunc) RangeSlice, /* sq_slice, x[i:j] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1635 (intobjargproc) RangeAssItem, /* sq_ass_item, x[i]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1636 (intintobjargproc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1637 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1638
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1639 static PyTypeObject RangeType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1640 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1641 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1642 "range",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1643 sizeof(RangeObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1644 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1645
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1646 (destructor) RangeDestructor, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1647 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1648 (getattrfunc) RangeGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1649 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1650 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1651 (reprfunc) RangeRepr, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1652
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1653 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1654 &RangeAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1655 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1656
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1657 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1658 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1659 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1660 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1661
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1662 /* Line range object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1663 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1664
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1665 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1666 RangeNew(buf_T *buf, int start, int end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1667 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1668 BufferObject *bufr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1669 RangeObject *self;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1670 self = PyObject_NEW(RangeObject, &RangeType);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1671 if (self == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1672 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1673
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1674 bufr = (BufferObject *)BufferNew(buf);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1675 if (bufr == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1676 {
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
1677 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1678 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1679 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1680 Py_INCREF(bufr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1681
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1682 self->buf = bufr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1683 self->start = start;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1684 self->end = end;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1685
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1686 return (PyObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1687 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1688
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1689 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1690 RangeDestructor(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1691 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1692 Py_DECREF(((RangeObject *)(self))->buf);
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
1693 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1694 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1695
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1696 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1697 RangeGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1698 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1699 if (strcmp(name, "start") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1700 return Py_BuildValue("i",((RangeObject *)(self))->start - 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1701 else if (strcmp(name, "end") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1702 return Py_BuildValue("i",((RangeObject *)(self))->end - 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1703 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1704 return Py_FindMethod(RangeMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1705 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1706
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1707 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1708 RangeRepr(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1709 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1710 static char repr[100];
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1711 RangeObject *this = (RangeObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1712
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1713 if (this->buf->buf == INVALID_BUFFER_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1714 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1715 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %8lX>",
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1716 (long)(self));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1717 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1718 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1719 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1720 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1721 char *name = (char *)this->buf->buf->b_fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1722 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1723
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1724 if (name == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1725 name = "";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1726 len = strlen(name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1727
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1728 if (len > 45)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1729 name = name + (45 - len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1730
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1731 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1732 len > 45 ? "..." : "", name,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1733 this->start, this->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1734
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1735 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1736 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1737 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1738
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1739 /****************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1740
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1741 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1742 RangeLength(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1743 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1744 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1745 if (CheckBuffer(((RangeObject *)(self))->buf))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1746 return -1; /* ??? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1747
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1748 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1749 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1750
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1751 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1752 RangeItem(PyObject *self, int n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1753 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1754 return RBItem(((RangeObject *)(self))->buf, n,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1755 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1756 ((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1757 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1758
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1759 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1760 RangeSlice(PyObject *self, int lo, int hi)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1761 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1762 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1763 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1764 ((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1765 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1766
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1767 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1768 RangeAssItem(PyObject *self, int n, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1769 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1770 return RBAssItem(((RangeObject *)(self))->buf, n, val,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1771 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1772 ((RangeObject *)(self))->end,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1773 &((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1774 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1775
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1776 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1777 RangeAssSlice(PyObject *self, int lo, int hi, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1778 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1779 return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1780 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1781 ((RangeObject *)(self))->end,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1782 &((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1783 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1784
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1785 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1786 RangeAppend(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1787 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1788 return RBAppend(((RangeObject *)(self))->buf, args,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1789 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1790 ((RangeObject *)(self))->end,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1791 &((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1792 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1793
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1794 /* Buffer list object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1795 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1796
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1797 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1798 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1799 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1800 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1801 BufListObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1802
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1803 static PySequenceMethods BufListAsSeq = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1804 (inquiry) BufListLength, /* sq_length, len(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1805 (binaryfunc) 0, /* sq_concat, x+y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1806 (intargfunc) 0, /* sq_repeat, x*n */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1807 (intargfunc) BufListItem, /* sq_item, x[i] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1808 (intintargfunc) 0, /* sq_slice, x[i:j] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1809 (intobjargproc) 0, /* sq_ass_item, x[i]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1810 (intintobjargproc) 0, /* sq_ass_slice, x[i:j]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1811 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1812
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1813 static PyTypeObject BufListType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1814 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1815 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1816 "buffer list",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1817 sizeof(BufListObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1818 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1819
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1820 (destructor) 0, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1821 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1822 (getattrfunc) 0, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1823 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1824 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1825 (reprfunc) 0, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1826
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1827 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1828 &BufListAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1829 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1830
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1831 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1832 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1833 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1834 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1835
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1836 /* Buffer list object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1837 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1838
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1839 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1840 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1841 BufListLength(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1842 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1843 buf_T *b = firstbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1844 int n = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1845
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1846 while (b)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1847 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1848 ++n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1849 b = b->b_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1850 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1851
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1852 return n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1853 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1854
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1855 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1856 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1857 BufListItem(PyObject *self, int n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1858 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1859 buf_T *b;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1860
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1861 for (b = firstbuf; b; b = b->b_next, --n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1862 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1863 if (n == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1864 return BufferNew(b);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1865 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1866
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1867 PyErr_SetString(PyExc_IndexError, _("no such buffer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1868 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1869 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1870
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1871 /* Window object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1872 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1873
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1874 static struct PyMethodDef WindowMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1875 /* name, function, calling, documentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1876 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1877 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1878
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1879 static PyTypeObject WindowType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1880 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1881 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1882 "window",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1883 sizeof(WindowObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1884 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1885
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1886 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1887 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1888 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1889 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1890 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1891 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1892
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1893 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1894 0, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1895 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1896
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1897 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1898 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1899 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1900 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1901
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1902 /* Window object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1903 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1904
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1905 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1906 WindowNew(win_T *win)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1907 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1908 /* We need to handle deletion of windows underneath us.
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1909 * If we add a "w_python_ref" field to the win_T structure,
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1910 * then we can get at it in win_free() in vim. We then
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1911 * need to create only ONE Python object per window - if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1912 * we try to create a second, just INCREF the existing one
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1913 * and return it. The (single) Python object referring to
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1914 * the window is stored in "w_python_ref".
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1915 * On a win_free() we set the Python object's win_T* field
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1916 * to an invalid value. We trap all uses of a window
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1917 * object, and reject them if the win_T* field is invalid.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1918 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1919
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1920 WindowObject *self;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1921
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1922 if (win->w_python_ref)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1923 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1924 self = win->w_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1925 Py_INCREF(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1926 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1927 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1928 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1929 self = PyObject_NEW(WindowObject, &WindowType);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1930 if (self == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1931 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1932 self->win = win;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1933 win->w_python_ref = self;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1934 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1935
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1936 return (PyObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1937 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1938
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1939 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1940 WindowDestructor(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1941 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1942 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1943
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1944 if (this->win && this->win != INVALID_WINDOW_VALUE)
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1945 this->win->w_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1946
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
1947 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1948 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1949
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1950 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1951 CheckWindow(WindowObject *this)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1952 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1953 if (this->win == INVALID_WINDOW_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1954 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1955 PyErr_SetVim(_("attempt to refer to deleted window"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1956 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1957 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1958
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1959 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1960 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1961
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1962 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1963 WindowGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1964 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1965 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1966
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1967 if (CheckWindow(this))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1968 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1969
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1970 if (strcmp(name, "buffer") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1971 return (PyObject *)BufferNew(this->win->w_buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1972 else if (strcmp(name, "cursor") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1973 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1974 pos_T *pos = &this->win->w_cursor;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1975
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1976 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1977 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1978 else if (strcmp(name, "height") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1979 return Py_BuildValue("l", (long)(this->win->w_height));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1980 #ifdef FEAT_VERTSPLIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1981 else if (strcmp(name, "width") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1982 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1983 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1984 else if (strcmp(name,"__members__") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1985 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1986 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1987 return Py_FindMethod(WindowMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1988 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1989
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1990 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1991 WindowSetattr(PyObject *self, char *name, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1992 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1993 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1994
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1995 if (CheckWindow(this))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1996 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1997
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1998 if (strcmp(name, "buffer") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1999 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2000 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2001 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2002 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2003 else if (strcmp(name, "cursor") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2004 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2005 long lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2006 long col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2007
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2008 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2009 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2010
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2011 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2012 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2013 PyErr_SetVim(_("cursor position outside buffer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2014 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2015 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2016
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2017 /* Check for keyboard interrupts */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2018 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2019 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2020
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2021 /* NO CHECK ON COLUMN - SEEMS NOT TO MATTER */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2022
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2023 this->win->w_cursor.lnum = lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2024 this->win->w_cursor.col = col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2025 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2026
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2027 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2028 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2029 else if (strcmp(name, "height") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2030 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2031 int height;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2032 win_T *savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2033
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2034 if (!PyArg_Parse(val, "i", &height))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2035 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2036
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2037 #ifdef FEAT_GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2038 need_mouse_correct = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2039 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2040 savewin = curwin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2041 curwin = this->win;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2042 win_setheight(height);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2043 curwin = savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2044
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2045 /* Check for keyboard interrupts */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2046 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2047 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2048
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2049 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2050 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2051 #ifdef FEAT_VERTSPLIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2052 else if (strcmp(name, "width") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2053 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2054 int width;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2055 win_T *savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2056
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2057 if (!PyArg_Parse(val, "i", &width))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2058 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2059
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2060 #ifdef FEAT_GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2061 need_mouse_correct = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2062 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2063 savewin = curwin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2064 curwin = this->win;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2065 win_setwidth(width);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2066 curwin = savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2067
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2068 /* Check for keyboard interrupts */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2069 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2070 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2071
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2072 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2073 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2074 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2075 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2076 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2077 PyErr_SetString(PyExc_AttributeError, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2078 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2079 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2080 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2081
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2082 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2083 WindowRepr(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2084 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2085 static char repr[100];
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2086 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2087
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2088 if (this->win == INVALID_WINDOW_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2089 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2090 vim_snprintf(repr, 100, _("<window object (deleted) at %.8lX>"),
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2091 (long)(self));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2092 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2093 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2094 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2095 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2096 int i = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2097 win_T *w;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2098
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2099 for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2100 ++i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2101
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2102 if (w == NULL)
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2103 vim_snprintf(repr, 100, _("<window object (unknown) at %.8lX>"),
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2104 (long)(self));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2105 else
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2106 vim_snprintf(repr, 100, _("<window %d>"), i);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2107
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2108 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2109 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2110 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2111
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2112 /* Window list object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2113 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2114
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2115 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2116 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2117 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2118 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2119 WinListObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2120
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2121 static PySequenceMethods WinListAsSeq = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2122 (inquiry) WinListLength, /* sq_length, len(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2123 (binaryfunc) 0, /* sq_concat, x+y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2124 (intargfunc) 0, /* sq_repeat, x*n */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2125 (intargfunc) WinListItem, /* sq_item, x[i] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2126 (intintargfunc) 0, /* sq_slice, x[i:j] */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2127 (intobjargproc) 0, /* sq_ass_item, x[i]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2128 (intintobjargproc) 0, /* sq_ass_slice, x[i:j]=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2129 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2130
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2131 static PyTypeObject WinListType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2132 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2133 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2134 "window list",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2135 sizeof(WinListObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2136 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2137
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2138 (destructor) 0, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2139 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2140 (getattrfunc) 0, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2141 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2142 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2143 (reprfunc) 0, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2144
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2145 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2146 &WinListAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2147 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2148
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2149 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2150 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2151 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2152 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2153
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2154 /* Window list object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2155 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2156 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2157 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2158 WinListLength(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2159 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2160 win_T *w = firstwin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2161 int n = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2162
671
83a006f81bac updated for version 7.0199
vimboss
parents: 637
diff changeset
2163 while (w != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2164 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2165 ++n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2166 w = W_NEXT(w);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2167 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2168
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2169 return n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2170 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2171
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2172 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2173 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2174 WinListItem(PyObject *self, int n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2175 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2176 win_T *w;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2177
671
83a006f81bac updated for version 7.0199
vimboss
parents: 637
diff changeset
2178 for (w = firstwin; w != NULL; w = W_NEXT(w), --n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2179 if (n == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2180 return WindowNew(w);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2181
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2182 PyErr_SetString(PyExc_IndexError, _("no such window"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2183 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2184 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2185
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2186 /* Current items object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2187 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2188
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2189 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2190 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2191 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2192 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2193 CurrentObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2194
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2195 static PyTypeObject CurrentType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2196 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2197 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2198 "current data",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2199 sizeof(CurrentObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2200 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2201
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2202 (destructor) 0, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2203 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2204 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2205 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2206 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2207 (reprfunc) 0, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2208
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2209 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2210 0, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2211 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2212
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2213 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2214 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2215 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2216 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2217
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2218 /* Current items object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2219 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2220 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2221 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2222 CurrentGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2223 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2224 if (strcmp(name, "buffer") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2225 return (PyObject *)BufferNew(curbuf);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2226 else if (strcmp(name, "window") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2227 return (PyObject *)WindowNew(curwin);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2228 else if (strcmp(name, "line") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2229 return GetBufferLine(curbuf, (int)curwin->w_cursor.lnum);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2230 else if (strcmp(name, "range") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2231 return RangeNew(curbuf, RangeStart, RangeEnd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2232 else if (strcmp(name,"__members__") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2233 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2234 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2235 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2236 PyErr_SetString(PyExc_AttributeError, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2237 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2238 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2239 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2240
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2241 /*ARGSUSED*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2242 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2243 CurrentSetattr(PyObject *self, char *name, PyObject *value)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2244 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2245 if (strcmp(name, "line") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2246 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2247 if (SetBufferLine(curbuf, (int)curwin->w_cursor.lnum, value, NULL) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2248 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2249
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2250 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2251 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2252 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2253 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2254 PyErr_SetString(PyExc_AttributeError, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2255 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2256 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2257 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2259 /* External interface
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2260 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2261
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2262 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2263 python_buffer_free(buf_T *buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2264 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2265 if (buf->b_python_ref != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2266 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2267 BufferObject *bp = buf->b_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2268 bp->buf = INVALID_BUFFER_VALUE;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2269 buf->b_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2270 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2271 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2272
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2273 #if defined(FEAT_WINDOWS) || defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2274 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2275 python_window_free(win_T *win)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2276 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2277 if (win->w_python_ref != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2278 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2279 WindowObject *wp = win->w_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2280 wp->win = INVALID_WINDOW_VALUE;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2281 win->w_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2282 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2283 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2284 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2285
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2286 static BufListObject TheBufferList =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2287 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2288 PyObject_HEAD_INIT(&BufListType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2289 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2290
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2291 static WinListObject TheWindowList =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2292 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2293 PyObject_HEAD_INIT(&WinListType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2294 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2295
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2296 static CurrentObject TheCurrent =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2297 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2298 PyObject_HEAD_INIT(&CurrentType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2299 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2300
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2301 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2302 PythonMod_Init(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2303 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2304 PyObject *mod;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2305 PyObject *dict;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2306 static char *(argv[2]) = {"", NULL};
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2307
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2308 /* Fixups... */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2309 BufferType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2310 RangeType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2311 WindowType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2312 BufListType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2313 WinListType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2314 CurrentType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2315
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2316 /* Set sys.argv[] to avoid a crash in warn(). */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2317 PySys_SetArgv(1, argv);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2318
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2319 mod = Py_InitModule("vim", VimMethods);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2320 dict = PyModule_GetDict(mod);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2321
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2322 VimError = Py_BuildValue("s", "vim.error");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2323
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2324 PyDict_SetItemString(dict, "error", VimError);
135
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
2325 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
2326 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
2327 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2328
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2329 if (PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2330 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2331
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2332 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2333 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2334
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2335 /*************************************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2336 * 4. Utility functions for handling the interface between Vim and Python.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2337 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2338
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2339 /* Get a line from the specified buffer. The line number is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2340 * in Vim format (1-based). The line is returned as a Python
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2341 * string object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2342 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2343 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2344 GetBufferLine(buf_T *buf, int n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2345 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2346 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2347 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2348
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2349 /* Get a list of lines from the specified buffer. The line numbers
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2350 * are in Vim format (1-based). The range is from lo up to, but not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2351 * including, hi. The list is returned as a Python list of string objects.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2352 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2353 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2354 GetBufferLineList(buf_T *buf, int lo, int hi)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2355 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2356 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2357 int n = hi - lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2358 PyObject *list = PyList_New(n);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2359
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2360 if (list == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2361 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2362
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2363 for (i = 0; i < n; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2364 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2365 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2366
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2367 /* Error check - was the Python string creation OK? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2368 if (str == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2369 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2370 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2371 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2372 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2373
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2374 /* Set the list item */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2375 if (PyList_SetItem(list, i, str))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2376 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2377 Py_DECREF(str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2378 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2379 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2380 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2381 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2382
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2383 /* The ownership of the Python list is passed to the caller (ie,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2384 * the caller should Py_DECREF() the object when it is finished
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2385 * with it).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2386 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2387
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2388 return list;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2389 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2390
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2391 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2392 * Check if deleting lines made the cursor position invalid.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2393 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2394 * deleted).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2395 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2396 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2397 py_fix_cursor(int lo, int hi, int extra)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2398 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2399 if (curwin->w_cursor.lnum >= lo)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2400 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2401 /* Adjust the cursor position if it's in/after the changed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2402 * lines. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2403 if (curwin->w_cursor.lnum >= hi)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2404 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2405 curwin->w_cursor.lnum += extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2406 check_cursor_col();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2407 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2408 else if (extra < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2409 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2410 curwin->w_cursor.lnum = lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2411 check_cursor();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2412 }
1083
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2413 else
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2414 check_cursor_col();
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2415 changed_cline_bef_curs();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2416 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2417 invalidate_botline();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2418 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2419
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2420 /* Replace a line in the specified buffer. The line number is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2421 * in Vim format (1-based). The replacement line is given as
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2422 * a Python string object. The object is checked for validity
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2423 * and correct format. Errors are returned as a value of FAIL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2424 * The return value is OK on success.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2425 * If OK is returned and len_change is not NULL, *len_change
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2426 * is set to the change in the buffer length.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2427 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2428 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2429 SetBufferLine(buf_T *buf, int n, PyObject *line, int *len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2430 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2431 /* First of all, we check the thpe of the supplied Python object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2432 * There are three cases:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2433 * 1. NULL, or None - this is a deletion.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2434 * 2. A string - this is a replacement.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2435 * 3. Anything else - this is an error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2436 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2437 if (line == Py_None || line == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2438 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2439 buf_T *savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2440
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2441 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2442 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2443
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2444 if (u_savedel((linenr_T)n, 1L) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2445 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2446 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2447 PyErr_SetVim(_("cannot delete line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2448 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2449 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2450 deleted_lines_mark((linenr_T)n, 1L);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2451 if (buf == curwin->w_buffer)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2452 py_fix_cursor(n, n + 1, -1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2453 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2454
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2455 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2456
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2457 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2458 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2459
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2460 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2461 *len_change = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2462
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2463 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2464 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2465 else if (PyString_Check(line))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2466 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2467 char *save = StringToLine(line);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2468 buf_T *savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2469
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2470 if (save == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2471 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2472
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2473 /* We do not need to free "save" if ml_replace() consumes it. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2474 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2475 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2476
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2477 if (u_savesub((linenr_T)n) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2478 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2479 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2480 vim_free(save);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2481 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2482 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2483 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2484 PyErr_SetVim(_("cannot replace line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2485 vim_free(save);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2486 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2487 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2488 changed_bytes((linenr_T)n, 0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2489
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2490 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2491
1083
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2492 /* Check that the cursor is not beyond the end of the line now. */
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2493 if (buf == curwin->w_buffer)
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2494 check_cursor_col();
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2495
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2496 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2497 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2498
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2499 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2500 *len_change = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2501
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2502 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2503 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2504 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2505 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2506 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2507 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2508 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2509 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2510
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2511 /* Replace a range of lines in the specified buffer. The line numbers are in
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2512 * Vim format (1-based). The range is from lo up to, but not including, hi.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2513 * The replacement lines are given as a Python list of string objects. The
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2514 * list is checked for validity and correct format. Errors are returned as a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2515 * value of FAIL. The return value is OK on success.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2516 * If OK is returned and len_change is not NULL, *len_change
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2517 * is set to the change in the buffer length.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2518 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2519 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2520 SetBufferLineList(buf_T *buf, int lo, int hi, PyObject *list, int *len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2521 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2522 /* First of all, we check the thpe of the supplied Python object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2523 * There are three cases:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2524 * 1. NULL, or None - this is a deletion.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2525 * 2. A list - this is a replacement.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2526 * 3. Anything else - this is an error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2527 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2528 if (list == Py_None || list == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2529 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2530 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2531 int n = hi - lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2532 buf_T *savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2533
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2534 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2535 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2536
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2537 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2538 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2539 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2540 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2541 for (i = 0; i < n; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2542 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2543 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2544 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2545 PyErr_SetVim(_("cannot delete line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2546 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2547 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2548 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2549 deleted_lines_mark((linenr_T)lo, (long)i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2550
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2551 if (buf == curwin->w_buffer)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2552 py_fix_cursor(lo, hi, -n);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2553 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2554
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2555 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2556
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2557 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2558 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2559
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2560 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2561 *len_change = -n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2562
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2563 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2564 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2565 else if (PyList_Check(list))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2566 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2567 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2568 int new_len = PyList_Size(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2569 int old_len = hi - lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2570 int extra = 0; /* lines added to text, can be negative */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2571 char **array;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2572 buf_T *savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2573
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2574 if (new_len == 0) /* avoid allocating zero bytes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2575 array = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2576 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2577 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2578 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2579 if (array == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2580 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2581 PyErr_NoMemory();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2582 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2583 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2584 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2585
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2586 for (i = 0; i < new_len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2587 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2588 PyObject *line = PyList_GetItem(list, i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2589
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2590 array[i] = StringToLine(line);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2591 if (array[i] == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2592 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2593 while (i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2594 vim_free(array[--i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2595 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2596 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2597 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2598 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2599
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2600 savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2601
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2602 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2603 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2604
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2605 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2606 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2607
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2608 /* If the size of the range is reducing (ie, new_len < old_len) we
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2609 * need to delete some old_len. We do this at the start, by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2610 * repeatedly deleting line "lo".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2611 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2612 if (!PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2613 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2614 for (i = 0; i < old_len - new_len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2615 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2616 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2617 PyErr_SetVim(_("cannot delete line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2618 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2619 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2620 extra -= i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2621 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2622
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2623 /* For as long as possible, replace the existing old_len with the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2624 * new old_len. This is a more efficient operation, as it requires
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2625 * less memory allocation and freeing.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2626 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2627 if (!PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2628 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2629 for (i = 0; i < old_len && i < new_len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2630 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2631 == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2632 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2633 PyErr_SetVim(_("cannot replace line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2634 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2635 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2636 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2637 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2638 i = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2639
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2640 /* Now we may need to insert the remaining new old_len. If we do, we
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2641 * must free the strings as we finish with them (we can't pass the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2642 * responsibility to vim in this case).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2643 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2644 if (!PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2645 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2646 while (i < new_len)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2647 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2648 if (ml_append((linenr_T)(lo + i - 1),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2649 (char_u *)array[i], 0, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2650 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2651 PyErr_SetVim(_("cannot insert line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2652 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2653 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2654 vim_free(array[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2655 ++i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2656 ++extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2657 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2658 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2659
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2660 /* Free any left-over old_len, as a result of an error */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2661 while (i < new_len)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2662 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2663 vim_free(array[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2664 ++i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2665 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2666
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2667 /* Free the array of old_len. All of its contents have now
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2668 * been dealt with (either freed, or the responsibility passed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2669 * to vim.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2670 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2671 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2672
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2673 /* Adjust marks. Invalidate any which lie in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2674 * changed range, and move any in the remainder of the buffer.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2675 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2676 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2677 (long)MAXLNUM, (long)extra);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2678 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2679
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2680 if (buf == curwin->w_buffer)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2681 py_fix_cursor(lo, hi, extra);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2682
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2683 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2684
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2685 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2686 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2687
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2688 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2689 *len_change = new_len - old_len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2690
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2691 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2692 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2693 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2694 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2695 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2696 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2697 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2698 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2699
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2700 /* Insert a number of lines into the specified buffer after the specifed line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2701 * The line number is in Vim format (1-based). The lines to be inserted are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2702 * given as a Python list of string objects or as a single string. The lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2703 * to be added are checked for validity and correct format. Errors are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2704 * returned as a value of FAIL. The return value is OK on success.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2705 * If OK is returned and len_change is not NULL, *len_change
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2706 * is set to the change in the buffer length.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2707 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2708 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2709 InsertBufferLines(buf_T *buf, int n, PyObject *lines, int *len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2710 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2711 /* First of all, we check the type of the supplied Python object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2712 * It must be a string or a list, or the call is in error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2713 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2714 if (PyString_Check(lines))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2715 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2716 char *str = StringToLine(lines);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2717 buf_T *savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2718
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2719 if (str == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2720 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2721
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2722 savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2723
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2724 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2725 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2726
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2727 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2728 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2729 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2730 PyErr_SetVim(_("cannot insert line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2731 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2732 appended_lines_mark((linenr_T)n, 1L);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2733
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2734 vim_free(str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2735 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2736 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2737
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2738 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2739 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2740
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2741 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2742 *len_change = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2743
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2744 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2745 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2746 else if (PyList_Check(lines))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2747 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2748 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2749 int size = PyList_Size(lines);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2750 char **array;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2751 buf_T *savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2752
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2753 array = (char **)alloc((unsigned)(size * sizeof(char *)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2754 if (array == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2755 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2756 PyErr_NoMemory();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2757 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2758 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2759
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2760 for (i = 0; i < size; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2761 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2762 PyObject *line = PyList_GetItem(lines, i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2763 array[i] = StringToLine(line);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2764
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2765 if (array[i] == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2766 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2767 while (i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2768 vim_free(array[--i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2769 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2770 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2771 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2772 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2773
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2774 savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2775
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2776 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2777 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2778
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2779 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2780 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2781 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2782 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2783 for (i = 0; i < size; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2784 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2785 if (ml_append((linenr_T)(n + i),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2786 (char_u *)array[i], 0, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2787 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2788 PyErr_SetVim(_("cannot insert line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2789
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2790 /* Free the rest of the lines */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2791 while (i < size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2792 vim_free(array[i++]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2793
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2794 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2795 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2796 vim_free(array[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2797 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2798 if (i > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2799 appended_lines_mark((linenr_T)n, (long)i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2800 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2801
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2802 /* Free the array of lines. All of its contents have now
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2803 * been freed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2804 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2805 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2806
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2807 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2808 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2809
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2810 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2811 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2812
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2813 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2814 *len_change = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2815
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2816 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2817 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2818 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2819 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2820 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2821 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2822 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2823 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2824
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2825 /* Convert a Vim line into a Python string.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2826 * All internal newlines are replaced by null characters.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2827 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2828 * On errors, the Python exception data is set, and NULL is returned.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2829 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2830 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2831 LineToString(const char *str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2832 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2833 PyObject *result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2834 int len = strlen(str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2835 char *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2836
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2837 /* Allocate an Python string object, with uninitialised contents. We
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2838 * must do it this way, so that we can modify the string in place
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2839 * later. See the Python source, Objects/stringobject.c for details.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2840 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2841 result = PyString_FromStringAndSize(NULL, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2842 if (result == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2843 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2844
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2845 p = PyString_AsString(result);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2846
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2847 while (*str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2848 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2849 if (*str == '\n')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2850 *p = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2851 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2852 *p = *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2853
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2854 ++p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2855 ++str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2856 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2857
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2858 return result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2859 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2860
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2861 /* Convert a Python string into a Vim line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2862 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2863 * The result is in allocated memory. All internal nulls are replaced by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2864 * newline characters. It is an error for the string to contain newline
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2865 * characters.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2866 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2867 * On errors, the Python exception data is set, and NULL is returned.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2868 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2869 static char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2870 StringToLine(PyObject *obj)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2871 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2872 const char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2873 char *save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2874 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2875 int i;
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2876 char *p;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2877
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2878 if (obj == NULL || !PyString_Check(obj))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2879 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2880 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2881 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2882 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2883
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2884 str = PyString_AsString(obj);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2885 len = PyString_Size(obj);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2886
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2887 /*
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2888 * Error checking: String must not contain newlines, as we
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2889 * are replacing a single line, and we must replace it with
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2890 * a single line.
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2891 * A trailing newline is removed, so that append(f.readlines()) works.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2892 */
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2893 p = memchr(str, '\n', len);
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2894 if (p != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2895 {
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2896 if (p == str + len - 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2897 --len;
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2898 else
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2899 {
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2900 PyErr_SetVim(_("string cannot contain newlines"));
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2901 return NULL;
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2902 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2903 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2904
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2905 /* Create a copy of the string, with internal nulls replaced by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2906 * newline characters, as is the vim convention.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2907 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2908 save = (char *)alloc((unsigned)(len+1));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2909 if (save == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2910 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2911 PyErr_NoMemory();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2912 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2913 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2914
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2915 for (i = 0; i < len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2916 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2917 if (str[i] == '\0')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2918 save[i] = '\n';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2919 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2920 save[i] = str[i];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2921 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2922
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2923 save[i] = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2924
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2925 return save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2926 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2927
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2928 /* Check to see whether a Vim error has been reported, or a keyboard
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2929 * interrupt has been detected.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2930 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2931 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2932 VimErrorCheck(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2933 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2934 if (got_int)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2935 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2936 PyErr_SetNone(PyExc_KeyboardInterrupt);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2937 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2938 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2939 else if (did_emsg && !PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2940 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2941 PyErr_SetNone(VimError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2942 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2943 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2944
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2945 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2946 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2947
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2948
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2949 /* Don't generate a prototype for the next function, it generates an error on
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2950 * newer Python versions. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2951 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2952
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2953 char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2954 Py_GetProgramName(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2955 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2956 return "vim";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2957 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2958 #endif /* Python 1.4 */