blob: 70fd0b8e515e401d1b5935513187c928a199fc46 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * Python extensions by Paul Moore.
11 * Changes for Unix by David Leonard.
12 *
13 * This consists of four parts:
14 * 1. Python interpreter main program
15 * 2. Python output stream: writes output via [e]msg().
16 * 3. Implementation of the Vim module for Python
17 * 4. Utility functions for handling the interface between Vim and Python.
18 */
19
20#include "vim.h"
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#include <limits.h>
23
24/* Python.h defines _POSIX_THREADS itself (if needed) */
25#ifdef _POSIX_THREADS
26# undef _POSIX_THREADS
27#endif
28
Bram Moolenaar860cae12010-06-05 23:22:07 +020029#if defined(_WIN32) && defined(HAVE_FCNTL_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +000030# undef HAVE_FCNTL_H
31#endif
32
33#ifdef _DEBUG
34# undef _DEBUG
35#endif
36
37#ifdef HAVE_STDARG_H
38# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
39#endif
Bram Moolenaarbe2c9ae2009-11-11 14:06:59 +000040#ifdef _POSIX_C_SOURCE
41# undef _POSIX_C_SOURCE /* pyconfig.h defines it as well. */
42#endif
43#ifdef _XOPEN_SOURCE
44# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
45#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
Bram Moolenaar2c45e942008-06-04 11:35:26 +000047#define PY_SSIZE_T_CLEAN
48
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#include <Python.h>
50#if defined(MACOS) && !defined(MACOS_X_UNIX)
51# include "macglue.h"
52# include <CodeFragments.h>
53#endif
54#undef main /* Defined in python.h - aargh */
55#undef HAVE_FCNTL_H /* Clash with os_win32.h */
56
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020057static void init_structs(void);
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#if !defined(FEAT_PYTHON) && defined(PROTO)
60/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000061# define PyObject Py_ssize_t
62# define PyThreadState Py_ssize_t
63# define PyTypeObject Py_ssize_t
64struct PyMethodDef { Py_ssize_t a; };
65# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
67
Bram Moolenaar2c45e942008-06-04 11:35:26 +000068#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
69# define PyInt Py_ssize_t
70# define PyInquiry lenfunc
71# define PyIntArgFunc ssizeargfunc
72# define PyIntIntArgFunc ssizessizeargfunc
73# define PyIntObjArgProc ssizeobjargproc
74# define PyIntIntObjArgProc ssizessizeobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000075# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000076#else
77# define PyInt int
78# define PyInquiry inquiry
79# define PyIntArgFunc intargfunc
80# define PyIntIntArgFunc intintargfunc
81# define PyIntObjArgProc intobjargproc
82# define PyIntIntObjArgProc intintobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000083# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000084#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/* Parser flags */
87#define single_input 256
88#define file_input 257
89#define eval_input 258
90
91#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
92 /* Python 2.3: can invoke ":python" recursively. */
93# define PY_CAN_RECURSE
94#endif
95
96#if defined(DYNAMIC_PYTHON) || defined(PROTO)
97# ifndef DYNAMIC_PYTHON
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000098# define HINSTANCE long_u /* for generating prototypes */
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# endif
100
Bram Moolenaarfa5d1e62010-07-22 21:44:13 +0200101#ifndef WIN3264
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200102# include <dlfcn.h>
103# define FARPROC void*
104# define HINSTANCE void*
Bram Moolenaarfa5d1e62010-07-22 21:44:13 +0200105# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200106# define close_dll dlclose
107# define symbol_from_dll dlsym
108#else
109# define load_dll LoadLibrary
110# define close_dll FreeLibrary
111# define symbol_from_dll GetProcAddress
112#endif
113
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000114/* This makes if_python.c compile without warnings against Python 2.5
115 * on Win32 and Win64. */
116#undef PyRun_SimpleString
117#undef PyArg_Parse
118#undef PyArg_ParseTuple
119#undef Py_BuildValue
120#undef Py_InitModule4
121#undef Py_InitModule4_64
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123/*
124 * Wrapper defines
125 */
126# define PyArg_Parse dll_PyArg_Parse
127# define PyArg_ParseTuple dll_PyArg_ParseTuple
128# define PyDict_SetItemString dll_PyDict_SetItemString
129# define PyErr_BadArgument dll_PyErr_BadArgument
130# define PyErr_Clear dll_PyErr_Clear
131# define PyErr_NoMemory dll_PyErr_NoMemory
132# define PyErr_Occurred dll_PyErr_Occurred
133# define PyErr_SetNone dll_PyErr_SetNone
134# define PyErr_SetString dll_PyErr_SetString
135# define PyEval_InitThreads dll_PyEval_InitThreads
136# define PyEval_RestoreThread dll_PyEval_RestoreThread
137# define PyEval_SaveThread dll_PyEval_SaveThread
138# ifdef PY_CAN_RECURSE
139# define PyGILState_Ensure dll_PyGILState_Ensure
140# define PyGILState_Release dll_PyGILState_Release
141# endif
142# define PyInt_AsLong dll_PyInt_AsLong
143# define PyInt_FromLong dll_PyInt_FromLong
144# define PyInt_Type (*dll_PyInt_Type)
145# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000146# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define PyList_New dll_PyList_New
148# define PyList_SetItem dll_PyList_SetItem
149# define PyList_Size dll_PyList_Size
150# define PyList_Type (*dll_PyList_Type)
151# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000152# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define PyDict_GetItemString dll_PyDict_GetItemString
154# define PyModule_GetDict dll_PyModule_GetDict
155# define PyRun_SimpleString dll_PyRun_SimpleString
156# define PyString_AsString dll_PyString_AsString
157# define PyString_FromString dll_PyString_FromString
158# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
159# define PyString_Size dll_PyString_Size
160# define PyString_Type (*dll_PyString_Type)
161# define PySys_SetObject dll_PySys_SetObject
162# define PySys_SetArgv dll_PySys_SetArgv
163# define PyType_Type (*dll_PyType_Type)
164# define Py_BuildValue dll_Py_BuildValue
165# define Py_FindMethod dll_Py_FindMethod
166# define Py_InitModule4 dll_Py_InitModule4
167# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000168# define Py_Finalize dll_Py_Finalize
169# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# define _PyObject_New dll__PyObject_New
171# define _Py_NoneStruct (*dll__Py_NoneStruct)
172# define PyObject_Init dll__PyObject_Init
173# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
174# define PyType_IsSubtype dll_PyType_IsSubtype
175# endif
176# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
177# define PyObject_Malloc dll_PyObject_Malloc
178# define PyObject_Free dll_PyObject_Free
179# endif
180
181/*
182 * Pointers for dynamic link
183 */
184static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
185static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
186static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
187static int(*dll_PyErr_BadArgument)(void);
188static void(*dll_PyErr_Clear)(void);
189static PyObject*(*dll_PyErr_NoMemory)(void);
190static PyObject*(*dll_PyErr_Occurred)(void);
191static void(*dll_PyErr_SetNone)(PyObject *);
192static void(*dll_PyErr_SetString)(PyObject *, const char *);
193static void(*dll_PyEval_InitThreads)(void);
194static void(*dll_PyEval_RestoreThread)(PyThreadState *);
195static PyThreadState*(*dll_PyEval_SaveThread)(void);
196# ifdef PY_CAN_RECURSE
197static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
198static void (*dll_PyGILState_Release)(PyGILState_STATE);
199#endif
200static long(*dll_PyInt_AsLong)(PyObject *);
201static PyObject*(*dll_PyInt_FromLong)(long);
202static PyTypeObject* dll_PyInt_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000203static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000204static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000205static PyObject*(*dll_PyList_New)(PyInt size);
206static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
207static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static PyTypeObject* dll_PyList_Type;
209static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000210static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
212static PyObject*(*dll_PyModule_GetDict)(PyObject *);
213static int(*dll_PyRun_SimpleString)(char *);
214static char*(*dll_PyString_AsString)(PyObject *);
215static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000216static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
217static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static PyTypeObject* dll_PyString_Type;
219static int(*dll_PySys_SetObject)(char *, PyObject *);
220static int(*dll_PySys_SetArgv)(int, char **);
221static PyTypeObject* dll_PyType_Type;
222static PyObject*(*dll_Py_BuildValue)(char *, ...);
223static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
224static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
225static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000226static void(*dll_Py_Finalize)(void);
227static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
229static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
230static PyObject* dll__Py_NoneStruct;
231# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
232static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
233# endif
234# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
235static void* (*dll_PyObject_Malloc)(size_t);
236static void (*dll_PyObject_Free)(void*);
237# endif
238
239static HINSTANCE hinstPython = 0; /* Instance of python.dll */
240
241/* Imported exception objects */
242static PyObject *imp_PyExc_AttributeError;
243static PyObject *imp_PyExc_IndexError;
244static PyObject *imp_PyExc_KeyboardInterrupt;
245static PyObject *imp_PyExc_TypeError;
246static PyObject *imp_PyExc_ValueError;
247
248# define PyExc_AttributeError imp_PyExc_AttributeError
249# define PyExc_IndexError imp_PyExc_IndexError
250# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
251# define PyExc_TypeError imp_PyExc_TypeError
252# define PyExc_ValueError imp_PyExc_ValueError
253
254/*
255 * Table of name to function pointer of python.
256 */
257# define PYTHON_PROC FARPROC
258static struct
259{
260 char *name;
261 PYTHON_PROC *ptr;
262} python_funcname_table[] =
263{
264 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
265 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
266 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
267 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
268 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
269 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
270 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
271 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
272 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
273 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
274 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
275 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
276# ifdef PY_CAN_RECURSE
277 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
278 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
279# endif
280 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
281 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
282 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
283 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000284 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
286 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
287 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
288 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
289 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
290 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000291 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
293 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
294 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
295 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
296 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
297 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
298 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
299 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
300 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
301 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
302 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
303 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000304# if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT
305 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
306# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000310 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
311 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
313 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
314 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
315# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
316 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
317# endif
318# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
319 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
320 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
321# endif
322 {"", NULL},
323};
324
325/*
326 * Free python.dll
327 */
328 static void
329end_dynamic_python(void)
330{
331 if (hinstPython)
332 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200333 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 hinstPython = 0;
335 }
336}
337
338/*
339 * Load library and get all pointers.
340 * Parameter 'libname' provides name of DLL.
341 * Return OK or FAIL.
342 */
343 static int
344python_runtime_link_init(char *libname, int verbose)
345{
346 int i;
347
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200348#if defined(UNIX) && defined(FEAT_PYTHON3)
349 /* Can't have Python and Python3 loaded at the same time, it may cause a
350 * crash. */
351 if (python3_loaded())
352 {
353 EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
354 return FAIL;
355 }
356#endif
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 if (hinstPython)
359 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200360 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 if (!hinstPython)
362 {
363 if (verbose)
364 EMSG2(_(e_loadlib), libname);
365 return FAIL;
366 }
367
368 for (i = 0; python_funcname_table[i].ptr; ++i)
369 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 python_funcname_table[i].name)) == NULL)
372 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200373 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 hinstPython = 0;
375 if (verbose)
376 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
377 return FAIL;
378 }
379 }
380 return OK;
381}
382
383/*
384 * If python is enabled (there is installed python on Windows system) return
385 * TRUE, else FALSE.
386 */
387 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000388python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
391}
392
393/* Load the standard Python exceptions - don't import the symbols from the
394 * DLL, as this can cause errors (importing data symbols is not reliable).
395 */
396static void get_exceptions __ARGS((void));
397
398 static void
399get_exceptions()
400{
401 PyObject *exmod = PyImport_ImportModule("exceptions");
402 PyObject *exdict = PyModule_GetDict(exmod);
403 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
404 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
405 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
406 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
407 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
408 Py_XINCREF(imp_PyExc_AttributeError);
409 Py_XINCREF(imp_PyExc_IndexError);
410 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
411 Py_XINCREF(imp_PyExc_TypeError);
412 Py_XINCREF(imp_PyExc_ValueError);
413 Py_XDECREF(exmod);
414}
415#endif /* DYNAMIC_PYTHON */
416
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417/*
418 * Include the code shared with if_python3.c
419 */
420#include "if_py_both.h"
421
422
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423/******************************************************
424 * Internal function prototypes.
425 */
426
427static void DoPythonCommand(exarg_T *, const char *);
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000428static PyInt RangeStart;
429static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431static void PythonIO_Flush(void);
432static int PythonIO_Init(void);
433static int PythonMod_Init(void);
434
435/* Utility functions for the vim/python interface
436 * ----------------------------------------------
437 */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000438static PyObject *GetBufferLine(buf_T *, PyInt);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000439static PyObject *GetBufferLineList(buf_T *, PyInt, PyInt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000441static int SetBufferLine(buf_T *, PyInt, PyObject *, PyInt *);
442static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
443static int InsertBufferLines(buf_T *, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445static PyObject *LineToString(const char *);
446static char *StringToLine(PyObject *);
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
449
450/******************************************************
451 * 1. Python interpreter main program.
452 */
453
454static int initialised = 0;
455
456#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
457typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460#ifdef PY_CAN_RECURSE
461static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
462#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000463static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466/*
467 * Suspend a thread of the Python interpreter, other threads are allowed to
468 * run.
469 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000470 static void
471Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473#ifdef PY_CAN_RECURSE
474 PyGILState_Release(pygilstate);
475#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478}
479
480/*
481 * Restore a thread of the Python interpreter, waits for other threads to
482 * block.
483 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000484 static void
485Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487#ifdef PY_CAN_RECURSE
488 pygilstate = PyGILState_Ensure();
489#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 PyEval_RestoreThread(saved_python_thread);
491 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000493}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 void
496python_end()
497{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000498 static int recurse = 0;
499
500 /* If a crash occurs while doing this, don't try again. */
501 if (recurse != 0)
502 return;
503
504 ++recurse;
505
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000507 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000508 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000509 Python_RestoreThread(); /* enter python */
510 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000513#else
514 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000515 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000516 Python_RestoreThread(); /* enter python */
517 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000520
521 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522}
523
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200524#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
525 int
526python_loaded()
527{
528 return (hinstPython != 0);
529}
530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 static int
533Python_Init(void)
534{
535 if (!initialised)
536 {
537#ifdef DYNAMIC_PYTHON
538 if (!python_enabled(TRUE))
539 {
540 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
541 goto fail;
542 }
543#endif
544
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200545 init_structs();
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547#if !defined(MACOS) || defined(MACOS_X_UNIX)
548 Py_Initialize();
549#else
550 PyMac_Initialize();
551#endif
552 /* initialise threads */
553 PyEval_InitThreads();
554
555#ifdef DYNAMIC_PYTHON
556 get_exceptions();
557#endif
558
559 if (PythonIO_Init())
560 goto fail;
561
562 if (PythonMod_Init())
563 goto fail;
564
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000565 /* Remove the element from sys.path that was added because of our
566 * argv[0] value in PythonMod_Init(). Previously we used an empty
567 * string, but dependinding on the OS we then get an empty entry or
568 * the current directory in sys.path. */
569 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
570
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000571 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 initialised = 1;
575 }
576
577 return 0;
578
579fail:
580 /* We call PythonIO_Flush() here to print any Python errors.
581 * This is OK, as it is possible to call this function even
582 * if PythonIO_Init() has not completed successfully (it will
583 * not do anything in this case).
584 */
585 PythonIO_Flush();
586 return -1;
587}
588
589/*
590 * External interface
591 */
592 static void
593DoPythonCommand(exarg_T *eap, const char *cmd)
594{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000595#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 static int recursive = 0;
597#endif
598#if defined(MACOS) && !defined(MACOS_X_UNIX)
599 GrafPtr oldPort;
600#endif
601#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
602 char *saved_locale;
603#endif
604
605#ifndef PY_CAN_RECURSE
606 if (recursive)
607 {
608 EMSG(_("E659: Cannot invoke Python recursively"));
609 return;
610 }
611 ++recursive;
612#endif
613
614#if defined(MACOS) && !defined(MACOS_X_UNIX)
615 GetPort(&oldPort);
616 /* Check if the Python library is available */
617 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
618 goto theend;
619#endif
620 if (Python_Init())
621 goto theend;
622
623 RangeStart = eap->line1;
624 RangeEnd = eap->line2;
625 Python_Release_Vim(); /* leave vim */
626
627#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
628 /* Python only works properly when the LC_NUMERIC locale is "C". */
629 saved_locale = setlocale(LC_NUMERIC, NULL);
630 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
631 saved_locale = NULL;
632 else
633 {
634 /* Need to make a copy, value may change when setting new locale. */
635 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
636 (void)setlocale(LC_NUMERIC, "C");
637 }
638#endif
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 PyRun_SimpleString((char *)(cmd));
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
647 if (saved_locale != NULL)
648 {
649 (void)setlocale(LC_NUMERIC, saved_locale);
650 vim_free(saved_locale);
651 }
652#endif
653
654 Python_Lock_Vim(); /* enter vim */
655 PythonIO_Flush();
656#if defined(MACOS) && !defined(MACOS_X_UNIX)
657 SetPort(oldPort);
658#endif
659
660theend:
661#ifndef PY_CAN_RECURSE
662 --recursive;
663#endif
664 return; /* keeps lint happy */
665}
666
667/*
668 * ":python"
669 */
670 void
671ex_python(exarg_T *eap)
672{
673 char_u *script;
674
675 script = script_get(eap, eap->arg);
676 if (!eap->skip)
677 {
678 if (script == NULL)
679 DoPythonCommand(eap, (char *)eap->arg);
680 else
681 DoPythonCommand(eap, (char *)script);
682 }
683 vim_free(script);
684}
685
686#define BUFFER_SIZE 1024
687
688/*
689 * ":pyfile"
690 */
691 void
692ex_pyfile(exarg_T *eap)
693{
694 static char buffer[BUFFER_SIZE];
695 const char *file = (char *)eap->arg;
696 char *p;
697
698 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
699 * stdio file pointer, but Vim and the Python DLL are compiled with
700 * different options under Windows, meaning that stdio pointers aren't
701 * compatible between the two. Yuk.
702 *
703 * Put the string "execfile('file')" into buffer. But, we need to
704 * escape any backslashes or single quotes in the file name, so that
705 * Python won't mangle the file name.
706 */
707 strcpy(buffer, "execfile('");
708 p = buffer + 10; /* size of "execfile('" */
709
710 while (*file && p < buffer + (BUFFER_SIZE - 3))
711 {
712 if (*file == '\\' || *file == '\'')
713 *p++ = '\\';
714 *p++ = *file++;
715 }
716
717 /* If we didn't finish the file name, we hit a buffer overflow */
718 if (*file != '\0')
719 return;
720
721 /* Put in the terminating "')" and a null */
722 *p++ = '\'';
723 *p++ = ')';
724 *p++ = '\0';
725
726 /* Execute the file */
727 DoPythonCommand(eap, buffer);
728}
729
730/******************************************************
731 * 2. Python output stream: writes output via [e]msg().
732 */
733
734/* Implementation functions
735 */
736
737static PyObject *OutputGetattr(PyObject *, char *);
738static int OutputSetattr(PyObject *, char *, PyObject *);
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740/*************/
741
742 static PyObject *
743OutputGetattr(PyObject *self, char *name)
744{
745 if (strcmp(name, "softspace") == 0)
746 return PyInt_FromLong(((OutputObject *)(self))->softspace);
747
748 return Py_FindMethod(OutputMethods, self, name);
749}
750
751 static int
752OutputSetattr(PyObject *self, char *name, PyObject *val)
753{
754 if (val == NULL) {
755 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
756 return -1;
757 }
758
759 if (strcmp(name, "softspace") == 0)
760 {
761 if (!PyInt_Check(val)) {
762 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
763 return -1;
764 }
765
766 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
767 return 0;
768 }
769
770 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
771 return -1;
772}
773
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774/***************/
775
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 static int
777PythonIO_Init(void)
778{
779 /* Fixups... */
780 OutputType.ob_type = &PyType_Type;
781
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200782 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783}
784
785/******************************************************
786 * 3. Implementation of the Vim module for Python
787 */
788
789/* Vim module - Implementation functions
790 * -------------------------------------
791 */
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793static PyObject *VimCommand(PyObject *, PyObject *);
794static PyObject *VimEval(PyObject *, PyObject *);
795
796/* Window type - Implementation functions
797 * --------------------------------------
798 */
799
800typedef struct
801{
802 PyObject_HEAD
803 win_T *win;
804}
805WindowObject;
806
807#define INVALID_WINDOW_VALUE ((win_T *)(-1))
808
809#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
810
811static PyObject *WindowNew(win_T *);
812
813static void WindowDestructor(PyObject *);
814static PyObject *WindowGetattr(PyObject *, char *);
815static int WindowSetattr(PyObject *, char *, PyObject *);
816static PyObject *WindowRepr(PyObject *);
817
818/* Buffer type - Implementation functions
819 * --------------------------------------
820 */
821
822typedef struct
823{
824 PyObject_HEAD
825 buf_T *buf;
826}
827BufferObject;
828
829#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
830
831#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
832
833static PyObject *BufferNew (buf_T *);
834
835static void BufferDestructor(PyObject *);
836static PyObject *BufferGetattr(PyObject *, char *);
837static PyObject *BufferRepr(PyObject *);
838
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000839static PyInt BufferLength(PyObject *);
840static PyObject *BufferItem(PyObject *, PyInt);
841static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
842static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
843static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845static PyObject *BufferAppend(PyObject *, PyObject *);
846static PyObject *BufferMark(PyObject *, PyObject *);
847static PyObject *BufferRange(PyObject *, PyObject *);
848
849/* Line range type - Implementation functions
850 * --------------------------------------
851 */
852
853typedef struct
854{
855 PyObject_HEAD
856 BufferObject *buf;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000857 PyInt start;
858 PyInt end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860RangeObject;
861
862#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
863
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000864static PyObject *RangeNew(buf_T *, PyInt, PyInt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866static void RangeDestructor(PyObject *);
867static PyObject *RangeGetattr(PyObject *, char *);
868static PyObject *RangeRepr(PyObject *);
869
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000870static PyInt RangeLength(PyObject *);
871static PyObject *RangeItem(PyObject *, PyInt);
872static PyObject *RangeSlice(PyObject *, PyInt, PyInt);
873static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
874static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
876static PyObject *RangeAppend(PyObject *, PyObject *);
877
878/* Window list type - Implementation functions
879 * -------------------------------------------
880 */
881
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000882static PyInt WinListLength(PyObject *);
883static PyObject *WinListItem(PyObject *, PyInt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
885/* Buffer list type - Implementation functions
886 * -------------------------------------------
887 */
888
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000889static PyInt BufListLength(PyObject *);
890static PyObject *BufListItem(PyObject *, PyInt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892/* Current objects type - Implementation functions
893 * -----------------------------------------------
894 */
895
896static PyObject *CurrentGetattr(PyObject *, char *);
897static int CurrentSetattr(PyObject *, char *, PyObject *);
898
899/* Vim module - Definitions
900 */
901
902static struct PyMethodDef VimMethods[] = {
903 /* name, function, calling, documentation */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000904 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
905 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 { NULL, NULL, 0, NULL }
907};
908
909/* Vim module - Implementation
910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000912VimCommand(PyObject *self UNUSED, PyObject *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913{
914 char *cmd;
915 PyObject *result;
916
917 if (!PyArg_ParseTuple(args, "s", &cmd))
918 return NULL;
919
920 PyErr_Clear();
921
922 Py_BEGIN_ALLOW_THREADS
923 Python_Lock_Vim();
924
925 do_cmdline_cmd((char_u *)cmd);
926 update_screen(VALID);
927
928 Python_Release_Vim();
929 Py_END_ALLOW_THREADS
930
931 if (VimErrorCheck())
932 result = NULL;
933 else
934 result = Py_None;
935
936 Py_XINCREF(result);
937 return result;
938}
939
Bram Moolenaar01dd60c2008-07-24 14:24:48 +0000940#ifdef FEAT_EVAL
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000941/*
942 * Function to translate a typval_T into a PyObject; this will recursively
943 * translate lists/dictionaries into their Python equivalents.
944 *
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000945 * The depth parameter is to avoid infinite recursion, set it to 1 when
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000946 * you call VimToPython.
947 */
948 static PyObject *
949VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
950{
951 PyObject *result;
952 PyObject *newObj;
953 char ptrBuf[NUMBUFLEN];
954
955 /* Avoid infinite recursion */
956 if (depth > 100)
957 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000958 Py_INCREF(Py_None);
959 result = Py_None;
960 return result;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000961 }
962
963 /* Check if we run into a recursive loop. The item must be in lookupDict
964 * then and we can use it again. */
Bram Moolenaard72b3862009-01-13 17:11:05 +0000965 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
966 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
967 {
968 sprintf(ptrBuf, PRINTF_DECIMAL_LONG_U,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200969 our_tv->v_type == VAR_LIST ? (long_u)our_tv->vval.v_list
970 : (long_u)our_tv->vval.v_dict);
Bram Moolenaard72b3862009-01-13 17:11:05 +0000971 result = PyDict_GetItemString(lookupDict, ptrBuf);
972 if (result != NULL)
973 {
974 Py_INCREF(result);
975 return result;
976 }
977 }
978
979 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000980 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000981 result = Py_BuildValue("s", our_tv->vval.v_string);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000982 }
983 else if (our_tv->v_type == VAR_NUMBER)
984 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000985 char buf[NUMBUFLEN];
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000986
987 /* For backwards compatibility numbers are stored as strings. */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000988 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
989 result = Py_BuildValue("s", buf);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000990 }
Bram Moolenaar01dd60c2008-07-24 14:24:48 +0000991# ifdef FEAT_FLOAT
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000992 else if (our_tv->v_type == VAR_FLOAT)
993 {
994 char buf[NUMBUFLEN];
995
996 sprintf(buf, "%f", our_tv->vval.v_float);
997 result = Py_BuildValue("s", buf);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000998 }
Bram Moolenaar01dd60c2008-07-24 14:24:48 +0000999# endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001000 else if (our_tv->v_type == VAR_LIST)
1001 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001002 list_T *list = our_tv->vval.v_list;
1003 listitem_T *curr;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001004
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001005 result = PyList_New(0);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001006
1007 if (list != NULL)
1008 {
Bram Moolenaard72b3862009-01-13 17:11:05 +00001009 PyDict_SetItemString(lookupDict, ptrBuf, result);
1010
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001011 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1012 {
1013 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
1014 PyList_Append(result, newObj);
1015 Py_DECREF(newObj);
1016 }
1017 }
1018 }
1019 else if (our_tv->v_type == VAR_DICT)
1020 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001021 result = PyDict_New();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001022
1023 if (our_tv->vval.v_dict != NULL)
1024 {
1025 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001026 long_u todo = ht->ht_used;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001027 hashitem_T *hi;
1028 dictitem_T *di;
1029
Bram Moolenaard72b3862009-01-13 17:11:05 +00001030 PyDict_SetItemString(lookupDict, ptrBuf, result);
1031
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001032 for (hi = ht->ht_array; todo > 0; ++hi)
1033 {
1034 if (!HASHITEM_EMPTY(hi))
1035 {
1036 --todo;
1037
1038 di = dict_lookup(hi);
1039 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
1040 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
1041 Py_DECREF(newObj);
1042 }
1043 }
1044 }
1045 }
1046 else
1047 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001048 Py_INCREF(Py_None);
1049 result = Py_None;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001050 }
1051
1052 return result;
1053}
Bram Moolenaar01dd60c2008-07-24 14:24:48 +00001054#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001057VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
1059#ifdef FEAT_EVAL
1060 char *expr;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001061 typval_T *our_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 PyObject *result;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001063 PyObject *lookup_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 if (!PyArg_ParseTuple(args, "s", &expr))
1066 return NULL;
1067
1068 Py_BEGIN_ALLOW_THREADS
1069 Python_Lock_Vim();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001070 our_tv = eval_expr((char_u *)expr, NULL);
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 Python_Release_Vim();
1073 Py_END_ALLOW_THREADS
1074
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001075 if (our_tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
1077 PyErr_SetVim(_("invalid expression"));
1078 return NULL;
1079 }
1080
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001081 /* Convert the Vim type into a Python type. Create a dictionary that's
1082 * used to check for recursive loops. */
1083 lookup_dict = PyDict_New();
1084 result = VimToPython(our_tv, 1, lookup_dict);
1085 Py_DECREF(lookup_dict);
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 Py_BEGIN_ALLOW_THREADS
1089 Python_Lock_Vim();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001090 free_tv(our_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 Python_Release_Vim();
1092 Py_END_ALLOW_THREADS
1093
1094 return result;
1095#else
1096 PyErr_SetVim(_("expressions disabled at compile time"));
1097 return NULL;
1098#endif
1099}
1100
1101/* Common routines for buffers and line ranges
1102 * -------------------------------------------
1103 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 static int
1106CheckBuffer(BufferObject *this)
1107{
1108 if (this->buf == INVALID_BUFFER_VALUE)
1109 {
1110 PyErr_SetVim(_("attempt to refer to deleted buffer"));
1111 return -1;
1112 }
1113
1114 return 0;
1115}
1116
1117 static PyObject *
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001118RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 if (CheckBuffer(self))
1121 return NULL;
1122
1123 if (n < 0 || n > end - start)
1124 {
1125 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
1126 return NULL;
1127 }
1128
1129 return GetBufferLine(self->buf, n+start);
1130}
1131
1132 static PyObject *
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001133RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001135 PyInt size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137 if (CheckBuffer(self))
1138 return NULL;
1139
1140 size = end - start + 1;
1141
1142 if (lo < 0)
1143 lo = 0;
1144 else if (lo > size)
1145 lo = size;
1146 if (hi < 0)
1147 hi = 0;
1148 if (hi < lo)
1149 hi = lo;
1150 else if (hi > size)
1151 hi = size;
1152
1153 return GetBufferLineList(self->buf, lo+start, hi+start);
1154}
1155
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001156 static PyInt
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001157RBAssItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001159 PyInt len_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 if (CheckBuffer(self))
1162 return -1;
1163
1164 if (n < 0 || n > end - start)
1165 {
1166 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
1167 return -1;
1168 }
1169
1170 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
1171 return -1;
1172
1173 if (new_end)
1174 *new_end = end + len_change;
1175
1176 return 0;
1177}
1178
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001179 static PyInt
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001180RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001182 PyInt size;
1183 PyInt len_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185 /* Self must be a valid buffer */
1186 if (CheckBuffer(self))
1187 return -1;
1188
1189 /* Sort out the slice range */
1190 size = end - start + 1;
1191
1192 if (lo < 0)
1193 lo = 0;
1194 else if (lo > size)
1195 lo = size;
1196 if (hi < 0)
1197 hi = 0;
1198 if (hi < lo)
1199 hi = lo;
1200 else if (hi > size)
1201 hi = size;
1202
1203 if (SetBufferLineList(self->buf, lo+start, hi+start, val, &len_change) == FAIL)
1204 return -1;
1205
1206 if (new_end)
1207 *new_end = end + len_change;
1208
1209 return 0;
1210}
1211
1212 static PyObject *
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001213RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
1215 PyObject *lines;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001216 PyInt len_change;
1217 PyInt max;
1218 PyInt n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 if (CheckBuffer(self))
1221 return NULL;
1222
1223 max = n = end - start + 1;
1224
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001225 if (!PyArg_ParseTuple(args, "O|" Py_ssize_t_fmt, &lines, &n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 return NULL;
1227
1228 if (n < 0 || n > max)
1229 {
1230 PyErr_SetString(PyExc_ValueError, _("line number out of range"));
1231 return NULL;
1232 }
1233
1234 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
1235 return NULL;
1236
1237 if (new_end)
1238 *new_end = end + len_change;
1239
1240 Py_INCREF(Py_None);
1241 return Py_None;
1242}
1243
1244
1245/* Buffer object - Definitions
1246 */
1247
1248static struct PyMethodDef BufferMethods[] = {
1249 /* name, function, calling, documentation */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001250 {"append", BufferAppend, 1, "Append data to Vim buffer" },
1251 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
1252 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 { NULL, NULL, 0, NULL }
1254};
1255
1256static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001257 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001259 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
1260 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1261 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1262 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
1263 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264};
1265
1266static PyTypeObject BufferType = {
1267 PyObject_HEAD_INIT(0)
1268 0,
1269 "buffer",
1270 sizeof(BufferObject),
1271 0,
1272
1273 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
1274 (printfunc) 0, /* tp_print, print x */
1275 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
1276 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1277 (cmpfunc) 0, /* tp_compare, x>y */
1278 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
1279
1280 0, /* as number */
1281 &BufferAsSeq, /* as sequence */
1282 0, /* as mapping */
1283
1284 (hashfunc) 0, /* tp_hash, dict(x) */
1285 (ternaryfunc) 0, /* tp_call, x() */
1286 (reprfunc) 0, /* tp_str, str(x) */
1287};
1288
1289/* Buffer object - Implementation
1290 */
1291
1292 static PyObject *
1293BufferNew(buf_T *buf)
1294{
1295 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001296 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 * then we can get at it in buf_freeall() in vim. We then
1298 * need to create only ONE Python object per buffer - if
1299 * we try to create a second, just INCREF the existing one
1300 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001301 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 * Question: what to do on a buf_freeall(). We'll probably
1303 * have to either delete the Python object (DECREF it to
1304 * zero - a bad idea, as it leaves dangling refs!) or
1305 * set the buf_T * value to an invalid value (-1?), which
1306 * means we need checks in all access functions... Bah.
1307 */
1308
1309 BufferObject *self;
1310
Bram Moolenaare344bea2005-09-01 20:46:49 +00001311 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001313 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 Py_INCREF(self);
1315 }
1316 else
1317 {
1318 self = PyObject_NEW(BufferObject, &BufferType);
1319 if (self == NULL)
1320 return NULL;
1321 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001322 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324
1325 return (PyObject *)(self);
1326}
1327
1328 static void
1329BufferDestructor(PyObject *self)
1330{
1331 BufferObject *this = (BufferObject *)(self);
1332
1333 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001334 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar658ada62006-10-03 13:02:36 +00001336 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337}
1338
1339 static PyObject *
1340BufferGetattr(PyObject *self, char *name)
1341{
1342 BufferObject *this = (BufferObject *)(self);
1343
1344 if (CheckBuffer(this))
1345 return NULL;
1346
1347 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001348 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001350 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 else if (strcmp(name,"__members__") == 0)
1352 return Py_BuildValue("[ss]", "name", "number");
1353 else
1354 return Py_FindMethod(BufferMethods, self, name);
1355}
1356
1357 static PyObject *
1358BufferRepr(PyObject *self)
1359{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001360 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 BufferObject *this = (BufferObject *)(self);
1362
1363 if (this->buf == INVALID_BUFFER_VALUE)
1364 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001365 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 return PyString_FromString(repr);
1367 }
1368 else
1369 {
1370 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001371 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 if (name == NULL)
1374 name = "";
1375 len = strlen(name);
1376
1377 if (len > 35)
1378 name = name + (35 - len);
1379
Bram Moolenaar555b2802005-05-19 21:08:39 +00001380 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 return PyString_FromString(repr);
1383 }
1384}
1385
1386/******************/
1387
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001388 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389BufferLength(PyObject *self)
1390{
1391 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1392 if (CheckBuffer((BufferObject *)(self)))
1393 return -1; /* ??? */
1394
1395 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1396}
1397
1398 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001399BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 return RBItem((BufferObject *)(self), n, 1,
1402 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1403}
1404
1405 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001406BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 return RBSlice((BufferObject *)(self), lo, hi, 1,
1409 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1410}
1411
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001412 static PyInt
1413BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 return RBAssItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001416 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 NULL);
1418}
1419
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001420 static PyInt
1421BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001424 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 NULL);
1426}
1427
1428 static PyObject *
1429BufferAppend(PyObject *self, PyObject *args)
1430{
1431 return RBAppend((BufferObject *)(self), args, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001432 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 NULL);
1434}
1435
1436 static PyObject *
1437BufferMark(PyObject *self, PyObject *args)
1438{
1439 pos_T *posp;
1440 char mark;
1441 buf_T *curbuf_save;
1442
1443 if (CheckBuffer((BufferObject *)(self)))
1444 return NULL;
1445
1446 if (!PyArg_ParseTuple(args, "c", &mark))
1447 return NULL;
1448
1449 curbuf_save = curbuf;
1450 curbuf = ((BufferObject *)(self))->buf;
1451 posp = getmark(mark, FALSE);
1452 curbuf = curbuf_save;
1453
1454 if (posp == NULL)
1455 {
1456 PyErr_SetVim(_("invalid mark name"));
1457 return NULL;
1458 }
1459
1460 /* Ckeck for keyboard interrupt */
1461 if (VimErrorCheck())
1462 return NULL;
1463
1464 if (posp->lnum <= 0)
1465 {
1466 /* Or raise an error? */
1467 Py_INCREF(Py_None);
1468 return Py_None;
1469 }
1470
1471 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
1472}
1473
1474 static PyObject *
1475BufferRange(PyObject *self, PyObject *args)
1476{
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001477 PyInt start;
1478 PyInt end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 if (CheckBuffer((BufferObject *)(self)))
1481 return NULL;
1482
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001483 if (!PyArg_ParseTuple(args, Py_ssize_t_fmt Py_ssize_t_fmt, &start, &end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 return NULL;
1485
1486 return RangeNew(((BufferObject *)(self))->buf, start, end);
1487}
1488
1489/* Line range object - Definitions
1490 */
1491
1492static struct PyMethodDef RangeMethods[] = {
1493 /* name, function, calling, documentation */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001494 {"append", RangeAppend, 1, "Append data to the Vim range" },
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 { NULL, NULL, 0, NULL }
1496};
1497
1498static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001499 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001501 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1502 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1503 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1504 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1505 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506};
1507
1508static PyTypeObject RangeType = {
1509 PyObject_HEAD_INIT(0)
1510 0,
1511 "range",
1512 sizeof(RangeObject),
1513 0,
1514
1515 (destructor) RangeDestructor, /* tp_dealloc, refcount==0 */
1516 (printfunc) 0, /* tp_print, print x */
1517 (getattrfunc) RangeGetattr, /* tp_getattr, x.attr */
1518 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1519 (cmpfunc) 0, /* tp_compare, x>y */
1520 (reprfunc) RangeRepr, /* tp_repr, `x`, print x */
1521
1522 0, /* as number */
1523 &RangeAsSeq, /* as sequence */
1524 0, /* as mapping */
1525
1526 (hashfunc) 0, /* tp_hash, dict(x) */
1527 (ternaryfunc) 0, /* tp_call, x() */
1528 (reprfunc) 0, /* tp_str, str(x) */
1529};
1530
1531/* Line range object - Implementation
1532 */
1533
1534 static PyObject *
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001535RangeNew(buf_T *buf, PyInt start, PyInt end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 BufferObject *bufr;
1538 RangeObject *self;
1539 self = PyObject_NEW(RangeObject, &RangeType);
1540 if (self == NULL)
1541 return NULL;
1542
1543 bufr = (BufferObject *)BufferNew(buf);
1544 if (bufr == NULL)
1545 {
Bram Moolenaar658ada62006-10-03 13:02:36 +00001546 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 return NULL;
1548 }
1549 Py_INCREF(bufr);
1550
1551 self->buf = bufr;
1552 self->start = start;
1553 self->end = end;
1554
1555 return (PyObject *)(self);
1556}
1557
1558 static void
1559RangeDestructor(PyObject *self)
1560{
1561 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001562 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563}
1564
1565 static PyObject *
1566RangeGetattr(PyObject *self, char *name)
1567{
1568 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001569 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001571 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 else
1573 return Py_FindMethod(RangeMethods, self, name);
1574}
1575
1576 static PyObject *
1577RangeRepr(PyObject *self)
1578{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001579 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 RangeObject *this = (RangeObject *)(self);
1581
1582 if (this->buf->buf == INVALID_BUFFER_VALUE)
1583 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001584 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
1585 (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return PyString_FromString(repr);
1587 }
1588 else
1589 {
1590 char *name = (char *)this->buf->buf->b_fname;
1591 int len;
1592
1593 if (name == NULL)
1594 name = "";
Bram Moolenaarc236c162008-07-13 17:41:49 +00001595 len = (int)strlen(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
1597 if (len > 45)
1598 name = name + (45 - len);
1599
Bram Moolenaar555b2802005-05-19 21:08:39 +00001600 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 len > 45 ? "..." : "", name,
1602 this->start, this->end);
1603
1604 return PyString_FromString(repr);
1605 }
1606}
1607
1608/****************/
1609
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001610 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611RangeLength(PyObject *self)
1612{
1613 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1614 if (CheckBuffer(((RangeObject *)(self))->buf))
1615 return -1; /* ??? */
1616
1617 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
1618}
1619
1620 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001621RangeItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622{
1623 return RBItem(((RangeObject *)(self))->buf, n,
1624 ((RangeObject *)(self))->start,
1625 ((RangeObject *)(self))->end);
1626}
1627
1628 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001629RangeSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
1631 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
1632 ((RangeObject *)(self))->start,
1633 ((RangeObject *)(self))->end);
1634}
1635
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001636 static PyInt
1637RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 return RBAssItem(((RangeObject *)(self))->buf, n, val,
1640 ((RangeObject *)(self))->start,
1641 ((RangeObject *)(self))->end,
1642 &((RangeObject *)(self))->end);
1643}
1644
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001645 static PyInt
1646RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
1649 ((RangeObject *)(self))->start,
1650 ((RangeObject *)(self))->end,
1651 &((RangeObject *)(self))->end);
1652}
1653
1654 static PyObject *
1655RangeAppend(PyObject *self, PyObject *args)
1656{
1657 return RBAppend(((RangeObject *)(self))->buf, args,
1658 ((RangeObject *)(self))->start,
1659 ((RangeObject *)(self))->end,
1660 &((RangeObject *)(self))->end);
1661}
1662
1663/* Buffer list object - Definitions
1664 */
1665
1666typedef struct
1667{
1668 PyObject_HEAD
1669}
1670BufListObject;
1671
1672static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001673 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001675 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1676 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1677 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1678 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1679 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680};
1681
1682static PyTypeObject BufListType = {
1683 PyObject_HEAD_INIT(0)
1684 0,
1685 "buffer list",
1686 sizeof(BufListObject),
1687 0,
1688
1689 (destructor) 0, /* tp_dealloc, refcount==0 */
1690 (printfunc) 0, /* tp_print, print x */
1691 (getattrfunc) 0, /* tp_getattr, x.attr */
1692 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1693 (cmpfunc) 0, /* tp_compare, x>y */
1694 (reprfunc) 0, /* tp_repr, `x`, print x */
1695
1696 0, /* as number */
1697 &BufListAsSeq, /* as sequence */
1698 0, /* as mapping */
1699
1700 (hashfunc) 0, /* tp_hash, dict(x) */
1701 (ternaryfunc) 0, /* tp_call, x() */
1702 (reprfunc) 0, /* tp_str, str(x) */
1703};
1704
1705/* Buffer list object - Implementation
1706 */
1707
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001708 static PyInt
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001709BufListLength(PyObject *self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 buf_T *b = firstbuf;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001712 PyInt n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714 while (b)
1715 {
1716 ++n;
1717 b = b->b_next;
1718 }
1719
1720 return n;
1721}
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001724BufListItem(PyObject *self UNUSED, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 buf_T *b;
1727
1728 for (b = firstbuf; b; b = b->b_next, --n)
1729 {
1730 if (n == 0)
1731 return BufferNew(b);
1732 }
1733
1734 PyErr_SetString(PyExc_IndexError, _("no such buffer"));
1735 return NULL;
1736}
1737
1738/* Window object - Definitions
1739 */
1740
1741static struct PyMethodDef WindowMethods[] = {
1742 /* name, function, calling, documentation */
1743 { NULL, NULL, 0, NULL }
1744};
1745
1746static PyTypeObject WindowType = {
1747 PyObject_HEAD_INIT(0)
1748 0,
1749 "window",
1750 sizeof(WindowObject),
1751 0,
1752
1753 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1754 (printfunc) 0, /* tp_print, print x */
1755 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1756 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1757 (cmpfunc) 0, /* tp_compare, x>y */
1758 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1759
1760 0, /* as number */
1761 0, /* as sequence */
1762 0, /* as mapping */
1763
1764 (hashfunc) 0, /* tp_hash, dict(x) */
1765 (ternaryfunc) 0, /* tp_call, x() */
1766 (reprfunc) 0, /* tp_str, str(x) */
1767};
1768
1769/* Window object - Implementation
1770 */
1771
1772 static PyObject *
1773WindowNew(win_T *win)
1774{
1775 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001776 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 * then we can get at it in win_free() in vim. We then
1778 * need to create only ONE Python object per window - if
1779 * we try to create a second, just INCREF the existing one
1780 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001781 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 * On a win_free() we set the Python object's win_T* field
1783 * to an invalid value. We trap all uses of a window
1784 * object, and reject them if the win_T* field is invalid.
1785 */
1786
1787 WindowObject *self;
1788
Bram Moolenaare344bea2005-09-01 20:46:49 +00001789 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001791 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 Py_INCREF(self);
1793 }
1794 else
1795 {
1796 self = PyObject_NEW(WindowObject, &WindowType);
1797 if (self == NULL)
1798 return NULL;
1799 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001800 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802
1803 return (PyObject *)(self);
1804}
1805
1806 static void
1807WindowDestructor(PyObject *self)
1808{
1809 WindowObject *this = (WindowObject *)(self);
1810
1811 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001812 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar658ada62006-10-03 13:02:36 +00001814 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815}
1816
1817 static int
1818CheckWindow(WindowObject *this)
1819{
1820 if (this->win == INVALID_WINDOW_VALUE)
1821 {
1822 PyErr_SetVim(_("attempt to refer to deleted window"));
1823 return -1;
1824 }
1825
1826 return 0;
1827}
1828
1829 static PyObject *
1830WindowGetattr(PyObject *self, char *name)
1831{
1832 WindowObject *this = (WindowObject *)(self);
1833
1834 if (CheckWindow(this))
1835 return NULL;
1836
1837 if (strcmp(name, "buffer") == 0)
1838 return (PyObject *)BufferNew(this->win->w_buffer);
1839 else if (strcmp(name, "cursor") == 0)
1840 {
1841 pos_T *pos = &this->win->w_cursor;
1842
1843 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1844 }
1845 else if (strcmp(name, "height") == 0)
1846 return Py_BuildValue("l", (long)(this->win->w_height));
1847#ifdef FEAT_VERTSPLIT
1848 else if (strcmp(name, "width") == 0)
1849 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1850#endif
1851 else if (strcmp(name,"__members__") == 0)
1852 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1853 else
1854 return Py_FindMethod(WindowMethods, self, name);
1855}
1856
1857 static int
1858WindowSetattr(PyObject *self, char *name, PyObject *val)
1859{
1860 WindowObject *this = (WindowObject *)(self);
1861
1862 if (CheckWindow(this))
1863 return -1;
1864
1865 if (strcmp(name, "buffer") == 0)
1866 {
1867 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
1868 return -1;
1869 }
1870 else if (strcmp(name, "cursor") == 0)
1871 {
1872 long lnum;
1873 long col;
Bram Moolenaarbadfde12009-11-03 10:43:27 +00001874 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
1876 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
1877 return -1;
1878
1879 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
1880 {
1881 PyErr_SetVim(_("cursor position outside buffer"));
1882 return -1;
1883 }
1884
1885 /* Check for keyboard interrupts */
1886 if (VimErrorCheck())
1887 return -1;
1888
Bram Moolenaarbadfde12009-11-03 10:43:27 +00001889 /* When column is out of range silently correct it. */
Bram Moolenaar8b9c05f2010-03-02 17:54:33 +01001890 len = (long)STRLEN(ml_get_buf(this->win->w_buffer, lnum, FALSE));
Bram Moolenaarbadfde12009-11-03 10:43:27 +00001891 if (col > len)
1892 col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 this->win->w_cursor.lnum = lnum;
1895 this->win->w_cursor.col = col;
Bram Moolenaarbadfde12009-11-03 10:43:27 +00001896#ifdef FEAT_VIRTUALEDIT
1897 this->win->w_cursor.coladd = 0;
1898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 update_screen(VALID);
1900
1901 return 0;
1902 }
1903 else if (strcmp(name, "height") == 0)
1904 {
1905 int height;
1906 win_T *savewin;
1907
1908 if (!PyArg_Parse(val, "i", &height))
1909 return -1;
1910
1911#ifdef FEAT_GUI
1912 need_mouse_correct = TRUE;
1913#endif
1914 savewin = curwin;
1915 curwin = this->win;
1916 win_setheight(height);
1917 curwin = savewin;
1918
1919 /* Check for keyboard interrupts */
1920 if (VimErrorCheck())
1921 return -1;
1922
1923 return 0;
1924 }
1925#ifdef FEAT_VERTSPLIT
1926 else if (strcmp(name, "width") == 0)
1927 {
1928 int width;
1929 win_T *savewin;
1930
1931 if (!PyArg_Parse(val, "i", &width))
1932 return -1;
1933
1934#ifdef FEAT_GUI
1935 need_mouse_correct = TRUE;
1936#endif
1937 savewin = curwin;
1938 curwin = this->win;
1939 win_setwidth(width);
1940 curwin = savewin;
1941
1942 /* Check for keyboard interrupts */
1943 if (VimErrorCheck())
1944 return -1;
1945
1946 return 0;
1947 }
1948#endif
1949 else
1950 {
1951 PyErr_SetString(PyExc_AttributeError, name);
1952 return -1;
1953 }
1954}
1955
1956 static PyObject *
1957WindowRepr(PyObject *self)
1958{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001959 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 WindowObject *this = (WindowObject *)(self);
1961
1962 if (this->win == INVALID_WINDOW_VALUE)
1963 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001964 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 return PyString_FromString(repr);
1966 }
1967 else
1968 {
1969 int i = 0;
1970 win_T *w;
1971
1972 for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w))
1973 ++i;
1974
1975 if (w == NULL)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001976 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
1977 (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 else
Bram Moolenaar555b2802005-05-19 21:08:39 +00001979 vim_snprintf(repr, 100, _("<window %d>"), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 return PyString_FromString(repr);
1982 }
1983}
1984
1985/* Window list object - Definitions
1986 */
1987
1988typedef struct
1989{
1990 PyObject_HEAD
1991}
1992WinListObject;
1993
1994static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001995 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001997 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1998 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1999 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
2000 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
2001 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002};
2003
2004static PyTypeObject WinListType = {
2005 PyObject_HEAD_INIT(0)
2006 0,
2007 "window list",
2008 sizeof(WinListObject),
2009 0,
2010
2011 (destructor) 0, /* tp_dealloc, refcount==0 */
2012 (printfunc) 0, /* tp_print, print x */
2013 (getattrfunc) 0, /* tp_getattr, x.attr */
2014 (setattrfunc) 0, /* tp_setattr, x.attr=v */
2015 (cmpfunc) 0, /* tp_compare, x>y */
2016 (reprfunc) 0, /* tp_repr, `x`, print x */
2017
2018 0, /* as number */
2019 &WinListAsSeq, /* as sequence */
2020 0, /* as mapping */
2021
2022 (hashfunc) 0, /* tp_hash, dict(x) */
2023 (ternaryfunc) 0, /* tp_call, x() */
2024 (reprfunc) 0, /* tp_str, str(x) */
2025};
2026
2027/* Window list object - Implementation
2028 */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002029 static PyInt
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002030WinListLength(PyObject *self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 win_T *w = firstwin;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002033 PyInt n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
Bram Moolenaarf740b292006-02-16 22:11:02 +00002035 while (w != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037 ++n;
2038 w = W_NEXT(w);
2039 }
2040
2041 return n;
2042}
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002045WinListItem(PyObject *self UNUSED, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 win_T *w;
2048
Bram Moolenaarf740b292006-02-16 22:11:02 +00002049 for (w = firstwin; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (n == 0)
2051 return WindowNew(w);
2052
2053 PyErr_SetString(PyExc_IndexError, _("no such window"));
2054 return NULL;
2055}
2056
2057/* Current items object - Definitions
2058 */
2059
2060typedef struct
2061{
2062 PyObject_HEAD
2063}
2064CurrentObject;
2065
2066static PyTypeObject CurrentType = {
2067 PyObject_HEAD_INIT(0)
2068 0,
2069 "current data",
2070 sizeof(CurrentObject),
2071 0,
2072
2073 (destructor) 0, /* tp_dealloc, refcount==0 */
2074 (printfunc) 0, /* tp_print, print x */
2075 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
2076 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
2077 (cmpfunc) 0, /* tp_compare, x>y */
2078 (reprfunc) 0, /* tp_repr, `x`, print x */
2079
2080 0, /* as number */
2081 0, /* as sequence */
2082 0, /* as mapping */
2083
2084 (hashfunc) 0, /* tp_hash, dict(x) */
2085 (ternaryfunc) 0, /* tp_call, x() */
2086 (reprfunc) 0, /* tp_str, str(x) */
2087};
2088
2089/* Current items object - Implementation
2090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002092CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093{
2094 if (strcmp(name, "buffer") == 0)
2095 return (PyObject *)BufferNew(curbuf);
2096 else if (strcmp(name, "window") == 0)
2097 return (PyObject *)WindowNew(curwin);
2098 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002099 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 else if (strcmp(name, "range") == 0)
2101 return RangeNew(curbuf, RangeStart, RangeEnd);
2102 else if (strcmp(name,"__members__") == 0)
2103 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
2104 else
2105 {
2106 PyErr_SetString(PyExc_AttributeError, name);
2107 return NULL;
2108 }
2109}
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002112CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 if (strcmp(name, "line") == 0)
2115 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002116 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 return -1;
2118
2119 return 0;
2120 }
2121 else
2122 {
2123 PyErr_SetString(PyExc_AttributeError, name);
2124 return -1;
2125 }
2126}
2127
2128/* External interface
2129 */
2130
2131 void
2132python_buffer_free(buf_T *buf)
2133{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002134 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00002136 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002138 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
2140}
2141
2142#if defined(FEAT_WINDOWS) || defined(PROTO)
2143 void
2144python_window_free(win_T *win)
2145{
Bram Moolenaare344bea2005-09-01 20:46:49 +00002146 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00002148 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002150 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
2152}
2153#endif
2154
2155static BufListObject TheBufferList =
2156{
2157 PyObject_HEAD_INIT(&BufListType)
2158};
2159
2160static WinListObject TheWindowList =
2161{
2162 PyObject_HEAD_INIT(&WinListType)
2163};
2164
2165static CurrentObject TheCurrent =
2166{
2167 PyObject_HEAD_INIT(&CurrentType)
2168};
2169
2170 static int
2171PythonMod_Init(void)
2172{
2173 PyObject *mod;
2174 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00002175 /* The special value is removed from sys.path in Python_Init(). */
2176 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 /* Fixups... */
2179 BufferType.ob_type = &PyType_Type;
2180 RangeType.ob_type = &PyType_Type;
2181 WindowType.ob_type = &PyType_Type;
2182 BufListType.ob_type = &PyType_Type;
2183 WinListType.ob_type = &PyType_Type;
2184 CurrentType.ob_type = &PyType_Type;
2185
2186 /* Set sys.argv[] to avoid a crash in warn(). */
2187 PySys_SetArgv(1, argv);
2188
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002189 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 dict = PyModule_GetDict(mod);
2191
2192 VimError = Py_BuildValue("s", "vim.error");
2193
2194 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00002195 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
2196 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
2197 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199 if (PyErr_Occurred())
2200 return -1;
2201
2202 return 0;
2203}
2204
2205/*************************************************************************
2206 * 4. Utility functions for handling the interface between Vim and Python.
2207 */
2208
2209/* Get a line from the specified buffer. The line number is
2210 * in Vim format (1-based). The line is returned as a Python
2211 * string object.
2212 */
2213 static PyObject *
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002214GetBufferLine(buf_T *buf, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
2217}
2218
2219/* Get a list of lines from the specified buffer. The line numbers
2220 * are in Vim format (1-based). The range is from lo up to, but not
2221 * including, hi. The list is returned as a Python list of string objects.
2222 */
2223 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002224GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002226 PyInt i;
2227 PyInt n = hi - lo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 PyObject *list = PyList_New(n);
2229
2230 if (list == NULL)
2231 return NULL;
2232
2233 for (i = 0; i < n; ++i)
2234 {
2235 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
2236
2237 /* Error check - was the Python string creation OK? */
2238 if (str == NULL)
2239 {
2240 Py_DECREF(list);
2241 return NULL;
2242 }
2243
2244 /* Set the list item */
2245 if (PyList_SetItem(list, i, str))
2246 {
2247 Py_DECREF(str);
2248 Py_DECREF(list);
2249 return NULL;
2250 }
2251 }
2252
2253 /* The ownership of the Python list is passed to the caller (ie,
2254 * the caller should Py_DECREF() the object when it is finished
2255 * with it).
2256 */
2257
2258 return list;
2259}
2260
2261/*
2262 * Check if deleting lines made the cursor position invalid.
2263 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
2264 * deleted).
2265 */
2266 static void
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002267py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 if (curwin->w_cursor.lnum >= lo)
2270 {
2271 /* Adjust the cursor position if it's in/after the changed
2272 * lines. */
2273 if (curwin->w_cursor.lnum >= hi)
2274 {
2275 curwin->w_cursor.lnum += extra;
2276 check_cursor_col();
2277 }
2278 else if (extra < 0)
2279 {
2280 curwin->w_cursor.lnum = lo;
2281 check_cursor();
2282 }
Bram Moolenaar454ec052007-03-08 09:20:28 +00002283 else
2284 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 changed_cline_bef_curs();
2286 }
2287 invalidate_botline();
2288}
2289
2290/* Replace a line in the specified buffer. The line number is
2291 * in Vim format (1-based). The replacement line is given as
2292 * a Python string object. The object is checked for validity
2293 * and correct format. Errors are returned as a value of FAIL.
2294 * The return value is OK on success.
2295 * If OK is returned and len_change is not NULL, *len_change
2296 * is set to the change in the buffer length.
2297 */
2298 static int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002299SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
2301 /* First of all, we check the thpe of the supplied Python object.
2302 * There are three cases:
2303 * 1. NULL, or None - this is a deletion.
2304 * 2. A string - this is a replacement.
2305 * 3. Anything else - this is an error.
2306 */
2307 if (line == Py_None || line == NULL)
2308 {
2309 buf_T *savebuf = curbuf;
2310
2311 PyErr_Clear();
2312 curbuf = buf;
2313
2314 if (u_savedel((linenr_T)n, 1L) == FAIL)
2315 PyErr_SetVim(_("cannot save undo information"));
2316 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
2317 PyErr_SetVim(_("cannot delete line"));
2318 else
2319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002321 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002322 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
2325 curbuf = savebuf;
2326
2327 if (PyErr_Occurred() || VimErrorCheck())
2328 return FAIL;
2329
2330 if (len_change)
2331 *len_change = -1;
2332
2333 return OK;
2334 }
2335 else if (PyString_Check(line))
2336 {
2337 char *save = StringToLine(line);
2338 buf_T *savebuf = curbuf;
2339
2340 if (save == NULL)
2341 return FAIL;
2342
2343 /* We do not need to free "save" if ml_replace() consumes it. */
2344 PyErr_Clear();
2345 curbuf = buf;
2346
2347 if (u_savesub((linenr_T)n) == FAIL)
2348 {
2349 PyErr_SetVim(_("cannot save undo information"));
2350 vim_free(save);
2351 }
2352 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
2353 {
2354 PyErr_SetVim(_("cannot replace line"));
2355 vim_free(save);
2356 }
2357 else
2358 changed_bytes((linenr_T)n, 0);
2359
2360 curbuf = savebuf;
2361
Bram Moolenaar454ec052007-03-08 09:20:28 +00002362 /* Check that the cursor is not beyond the end of the line now. */
2363 if (buf == curwin->w_buffer)
2364 check_cursor_col();
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (PyErr_Occurred() || VimErrorCheck())
2367 return FAIL;
2368
2369 if (len_change)
2370 *len_change = 0;
2371
2372 return OK;
2373 }
2374 else
2375 {
2376 PyErr_BadArgument();
2377 return FAIL;
2378 }
2379}
2380
2381/* Replace a range of lines in the specified buffer. The line numbers are in
2382 * Vim format (1-based). The range is from lo up to, but not including, hi.
2383 * The replacement lines are given as a Python list of string objects. The
2384 * list is checked for validity and correct format. Errors are returned as a
2385 * value of FAIL. The return value is OK on success.
2386 * If OK is returned and len_change is not NULL, *len_change
2387 * is set to the change in the buffer length.
2388 */
2389 static int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002390SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392 /* First of all, we check the thpe of the supplied Python object.
2393 * There are three cases:
2394 * 1. NULL, or None - this is a deletion.
2395 * 2. A list - this is a replacement.
2396 * 3. Anything else - this is an error.
2397 */
2398 if (list == Py_None || list == NULL)
2399 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002400 PyInt i;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002401 PyInt n = (int)(hi - lo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 buf_T *savebuf = curbuf;
2403
2404 PyErr_Clear();
2405 curbuf = buf;
2406
2407 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
2408 PyErr_SetVim(_("cannot save undo information"));
2409 else
2410 {
2411 for (i = 0; i < n; ++i)
2412 {
2413 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2414 {
2415 PyErr_SetVim(_("cannot delete line"));
2416 break;
2417 }
2418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002420 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002421 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423
2424 curbuf = savebuf;
2425
2426 if (PyErr_Occurred() || VimErrorCheck())
2427 return FAIL;
2428
2429 if (len_change)
2430 *len_change = -n;
2431
2432 return OK;
2433 }
2434 else if (PyList_Check(list))
2435 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002436 PyInt i;
2437 PyInt new_len = PyList_Size(list);
2438 PyInt old_len = hi - lo;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002439 PyInt extra = 0; /* lines added to text, can be negative */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 char **array;
2441 buf_T *savebuf;
2442
2443 if (new_len == 0) /* avoid allocating zero bytes */
2444 array = NULL;
2445 else
2446 {
2447 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
2448 if (array == NULL)
2449 {
2450 PyErr_NoMemory();
2451 return FAIL;
2452 }
2453 }
2454
2455 for (i = 0; i < new_len; ++i)
2456 {
2457 PyObject *line = PyList_GetItem(list, i);
2458
2459 array[i] = StringToLine(line);
2460 if (array[i] == NULL)
2461 {
2462 while (i)
2463 vim_free(array[--i]);
2464 vim_free(array);
2465 return FAIL;
2466 }
2467 }
2468
2469 savebuf = curbuf;
2470
2471 PyErr_Clear();
2472 curbuf = buf;
2473
2474 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
2475 PyErr_SetVim(_("cannot save undo information"));
2476
2477 /* If the size of the range is reducing (ie, new_len < old_len) we
2478 * need to delete some old_len. We do this at the start, by
2479 * repeatedly deleting line "lo".
2480 */
2481 if (!PyErr_Occurred())
2482 {
2483 for (i = 0; i < old_len - new_len; ++i)
2484 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
2485 {
2486 PyErr_SetVim(_("cannot delete line"));
2487 break;
2488 }
2489 extra -= i;
2490 }
2491
2492 /* For as long as possible, replace the existing old_len with the
2493 * new old_len. This is a more efficient operation, as it requires
2494 * less memory allocation and freeing.
2495 */
2496 if (!PyErr_Occurred())
2497 {
2498 for (i = 0; i < old_len && i < new_len; ++i)
2499 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
2500 == FAIL)
2501 {
2502 PyErr_SetVim(_("cannot replace line"));
2503 break;
2504 }
2505 }
2506 else
2507 i = 0;
2508
2509 /* Now we may need to insert the remaining new old_len. If we do, we
2510 * must free the strings as we finish with them (we can't pass the
2511 * responsibility to vim in this case).
2512 */
2513 if (!PyErr_Occurred())
2514 {
2515 while (i < new_len)
2516 {
2517 if (ml_append((linenr_T)(lo + i - 1),
2518 (char_u *)array[i], 0, FALSE) == FAIL)
2519 {
2520 PyErr_SetVim(_("cannot insert line"));
2521 break;
2522 }
2523 vim_free(array[i]);
2524 ++i;
2525 ++extra;
2526 }
2527 }
2528
2529 /* Free any left-over old_len, as a result of an error */
2530 while (i < new_len)
2531 {
2532 vim_free(array[i]);
2533 ++i;
2534 }
2535
2536 /* Free the array of old_len. All of its contents have now
2537 * been dealt with (either freed, or the responsibility passed
2538 * to vim.
2539 */
2540 vim_free(array);
2541
2542 /* Adjust marks. Invalidate any which lie in the
2543 * changed range, and move any in the remainder of the buffer.
2544 */
2545 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
2546 (long)MAXLNUM, (long)extra);
2547 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
2548
2549 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002550 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 curbuf = savebuf;
2553
2554 if (PyErr_Occurred() || VimErrorCheck())
2555 return FAIL;
2556
2557 if (len_change)
2558 *len_change = new_len - old_len;
2559
2560 return OK;
2561 }
2562 else
2563 {
2564 PyErr_BadArgument();
2565 return FAIL;
2566 }
2567}
2568
2569/* Insert a number of lines into the specified buffer after the specifed line.
2570 * The line number is in Vim format (1-based). The lines to be inserted are
2571 * given as a Python list of string objects or as a single string. The lines
2572 * to be added are checked for validity and correct format. Errors are
2573 * returned as a value of FAIL. The return value is OK on success.
2574 * If OK is returned and len_change is not NULL, *len_change
2575 * is set to the change in the buffer length.
2576 */
2577 static int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00002578InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580 /* First of all, we check the type of the supplied Python object.
2581 * It must be a string or a list, or the call is in error.
2582 */
2583 if (PyString_Check(lines))
2584 {
2585 char *str = StringToLine(lines);
2586 buf_T *savebuf;
2587
2588 if (str == NULL)
2589 return FAIL;
2590
2591 savebuf = curbuf;
2592
2593 PyErr_Clear();
2594 curbuf = buf;
2595
2596 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
2597 PyErr_SetVim(_("cannot save undo information"));
2598 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
2599 PyErr_SetVim(_("cannot insert line"));
2600 else
2601 appended_lines_mark((linenr_T)n, 1L);
2602
2603 vim_free(str);
2604 curbuf = savebuf;
2605 update_screen(VALID);
2606
2607 if (PyErr_Occurred() || VimErrorCheck())
2608 return FAIL;
2609
2610 if (len_change)
2611 *len_change = 1;
2612
2613 return OK;
2614 }
2615 else if (PyList_Check(lines))
2616 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002617 PyInt i;
2618 PyInt size = PyList_Size(lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 char **array;
2620 buf_T *savebuf;
2621
2622 array = (char **)alloc((unsigned)(size * sizeof(char *)));
2623 if (array == NULL)
2624 {
2625 PyErr_NoMemory();
2626 return FAIL;
2627 }
2628
2629 for (i = 0; i < size; ++i)
2630 {
2631 PyObject *line = PyList_GetItem(lines, i);
2632 array[i] = StringToLine(line);
2633
2634 if (array[i] == NULL)
2635 {
2636 while (i)
2637 vim_free(array[--i]);
2638 vim_free(array);
2639 return FAIL;
2640 }
2641 }
2642
2643 savebuf = curbuf;
2644
2645 PyErr_Clear();
2646 curbuf = buf;
2647
2648 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
2649 PyErr_SetVim(_("cannot save undo information"));
2650 else
2651 {
2652 for (i = 0; i < size; ++i)
2653 {
2654 if (ml_append((linenr_T)(n + i),
2655 (char_u *)array[i], 0, FALSE) == FAIL)
2656 {
2657 PyErr_SetVim(_("cannot insert line"));
2658
2659 /* Free the rest of the lines */
2660 while (i < size)
2661 vim_free(array[i++]);
2662
2663 break;
2664 }
2665 vim_free(array[i]);
2666 }
2667 if (i > 0)
2668 appended_lines_mark((linenr_T)n, (long)i);
2669 }
2670
2671 /* Free the array of lines. All of its contents have now
2672 * been freed.
2673 */
2674 vim_free(array);
2675
2676 curbuf = savebuf;
2677 update_screen(VALID);
2678
2679 if (PyErr_Occurred() || VimErrorCheck())
2680 return FAIL;
2681
2682 if (len_change)
2683 *len_change = size;
2684
2685 return OK;
2686 }
2687 else
2688 {
2689 PyErr_BadArgument();
2690 return FAIL;
2691 }
2692}
2693
2694/* Convert a Vim line into a Python string.
2695 * All internal newlines are replaced by null characters.
2696 *
2697 * On errors, the Python exception data is set, and NULL is returned.
2698 */
2699 static PyObject *
2700LineToString(const char *str)
2701{
2702 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002703 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 char *p;
2705
2706 /* Allocate an Python string object, with uninitialised contents. We
2707 * must do it this way, so that we can modify the string in place
2708 * later. See the Python source, Objects/stringobject.c for details.
2709 */
2710 result = PyString_FromStringAndSize(NULL, len);
2711 if (result == NULL)
2712 return NULL;
2713
2714 p = PyString_AsString(result);
2715
2716 while (*str)
2717 {
2718 if (*str == '\n')
2719 *p = '\0';
2720 else
2721 *p = *str;
2722
2723 ++p;
2724 ++str;
2725 }
2726
2727 return result;
2728}
2729
2730/* Convert a Python string into a Vim line.
2731 *
2732 * The result is in allocated memory. All internal nulls are replaced by
2733 * newline characters. It is an error for the string to contain newline
2734 * characters.
2735 *
2736 * On errors, the Python exception data is set, and NULL is returned.
2737 */
2738 static char *
2739StringToLine(PyObject *obj)
2740{
2741 const char *str;
2742 char *save;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00002743 PyInt len;
2744 PyInt i;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002745 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 if (obj == NULL || !PyString_Check(obj))
2748 {
2749 PyErr_BadArgument();
2750 return NULL;
2751 }
2752
2753 str = PyString_AsString(obj);
2754 len = PyString_Size(obj);
2755
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002756 /*
2757 * Error checking: String must not contain newlines, as we
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * are replacing a single line, and we must replace it with
2759 * a single line.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002760 * A trailing newline is removed, so that append(f.readlines()) works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002762 p = memchr(str, '\n', len);
2763 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002765 if (p == str + len - 1)
2766 --len;
2767 else
2768 {
2769 PyErr_SetVim(_("string cannot contain newlines"));
2770 return NULL;
2771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773
2774 /* Create a copy of the string, with internal nulls replaced by
2775 * newline characters, as is the vim convention.
2776 */
2777 save = (char *)alloc((unsigned)(len+1));
2778 if (save == NULL)
2779 {
2780 PyErr_NoMemory();
2781 return NULL;
2782 }
2783
2784 for (i = 0; i < len; ++i)
2785 {
2786 if (str[i] == '\0')
2787 save[i] = '\n';
2788 else
2789 save[i] = str[i];
2790 }
2791
2792 save[i] = '\0';
2793
2794 return save;
2795}
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798/* Don't generate a prototype for the next function, it generates an error on
2799 * newer Python versions. */
2800#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
2801
2802 char *
2803Py_GetProgramName(void)
2804{
2805 return "vim";
2806}
2807#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002808
2809 static void
2810init_structs(void)
2811{
2812 vim_memset(&OutputType, 0, sizeof(OutputType));
2813 OutputType.tp_name = "message";
2814 OutputType.tp_basicsize = sizeof(OutputObject);
2815 OutputType.tp_getattr = OutputGetattr;
2816 OutputType.tp_setattr = OutputSetattr;
2817}