annotate src/if_python.c @ 497:73f10d8124f4 v7.0136

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