annotate src/if_python.c @ 1969:2e915ea7110f v7.2.266

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