1/* 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
22#include <limits.h>
23
24/* Python.h defines _POSIX_THREADS itself (if needed) */
25#ifdef _POSIX_THREADS
26# undef _POSIX_THREADS
27#endif
28
29#if defined(_WIN32) && defined(HAVE_FCNTL_H)
30# 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
40#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
46
47#define PY_SSIZE_T_CLEAN
48
49#include <Python/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
57static void init_structs(void);
58
59#if !defined(FEAT_PYTHON) && defined(PROTO)
60/* Use this to be able to generate prototypes without python being used. */
61# 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
66#endif
67
68#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
75# define Py_ssize_t_fmt "n"
76#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
83# define Py_ssize_t_fmt "i"
84#endif
85
86/* 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
98#   define HINSTANCE long_u		/* for generating prototypes */
99#  endif
100
101# ifndef WIN3264
102#  include <dlfcn.h>
103#  define FARPROC void*
104#  define HINSTANCE void*
105#  ifdef PY_NO_RTLD_GLOBAL
106#   define load_dll(n) dlopen((n), RTLD_LAZY)
107#  else
108#   define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
109#  endif
110#  define close_dll dlclose
111#  define symbol_from_dll dlsym
112# else
113#  define load_dll LoadLibrary
114#  define close_dll FreeLibrary
115#  define symbol_from_dll GetProcAddress
116# endif
117
118/* This makes if_python.c compile without warnings against Python 2.5
119 * on Win32 and Win64. */
120# undef PyRun_SimpleString
121# undef PyArg_Parse
122# undef PyArg_ParseTuple
123# undef Py_BuildValue
124# undef Py_InitModule4
125# undef Py_InitModule4_64
126
127/*
128 * Wrapper defines
129 */
130# define PyArg_Parse dll_PyArg_Parse
131# define PyArg_ParseTuple dll_PyArg_ParseTuple
132# define PyDict_SetItemString dll_PyDict_SetItemString
133# define PyErr_BadArgument dll_PyErr_BadArgument
134# define PyErr_Clear dll_PyErr_Clear
135# define PyErr_NoMemory dll_PyErr_NoMemory
136# define PyErr_Occurred dll_PyErr_Occurred
137# define PyErr_SetNone dll_PyErr_SetNone
138# define PyErr_SetString dll_PyErr_SetString
139# define PyEval_InitThreads dll_PyEval_InitThreads
140# define PyEval_RestoreThread dll_PyEval_RestoreThread
141# define PyEval_SaveThread dll_PyEval_SaveThread
142# ifdef PY_CAN_RECURSE
143#  define PyGILState_Ensure dll_PyGILState_Ensure
144#  define PyGILState_Release dll_PyGILState_Release
145# endif
146# define PyInt_AsLong dll_PyInt_AsLong
147# define PyInt_FromLong dll_PyInt_FromLong
148# define PyInt_Type (*dll_PyInt_Type)
149# define PyList_GetItem dll_PyList_GetItem
150# define PyList_Append dll_PyList_Append
151# define PyList_New dll_PyList_New
152# define PyList_SetItem dll_PyList_SetItem
153# define PyList_Size dll_PyList_Size
154# define PyList_Type (*dll_PyList_Type)
155# define PyImport_ImportModule dll_PyImport_ImportModule
156# define PyDict_New dll_PyDict_New
157# define PyDict_GetItemString dll_PyDict_GetItemString
158# define PyModule_GetDict dll_PyModule_GetDict
159# define PyRun_SimpleString dll_PyRun_SimpleString
160# define PyString_AsString dll_PyString_AsString
161# define PyString_FromString dll_PyString_FromString
162# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
163# define PyString_Size dll_PyString_Size
164# define PyString_Type (*dll_PyString_Type)
165# define PySys_SetObject dll_PySys_SetObject
166# define PySys_SetArgv dll_PySys_SetArgv
167# define PyType_Type (*dll_PyType_Type)
168# define Py_BuildValue dll_Py_BuildValue
169# define Py_FindMethod dll_Py_FindMethod
170# define Py_InitModule4 dll_Py_InitModule4
171# define Py_Initialize dll_Py_Initialize
172# define Py_Finalize dll_Py_Finalize
173# define Py_IsInitialized dll_Py_IsInitialized
174# define _PyObject_New dll__PyObject_New
175# define _Py_NoneStruct (*dll__Py_NoneStruct)
176# define PyObject_Init dll__PyObject_Init
177# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
178#  define PyType_IsSubtype dll_PyType_IsSubtype
179# endif
180# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
181#  define PyObject_Malloc dll_PyObject_Malloc
182#  define PyObject_Free dll_PyObject_Free
183# endif
184
185/*
186 * Pointers for dynamic link
187 */
188static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
189static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
190static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
191static int(*dll_PyErr_BadArgument)(void);
192static void(*dll_PyErr_Clear)(void);
193static PyObject*(*dll_PyErr_NoMemory)(void);
194static PyObject*(*dll_PyErr_Occurred)(void);
195static void(*dll_PyErr_SetNone)(PyObject *);
196static void(*dll_PyErr_SetString)(PyObject *, const char *);
197static void(*dll_PyEval_InitThreads)(void);
198static void(*dll_PyEval_RestoreThread)(PyThreadState *);
199static PyThreadState*(*dll_PyEval_SaveThread)(void);
200# ifdef PY_CAN_RECURSE
201static PyGILState_STATE	(*dll_PyGILState_Ensure)(void);
202static void (*dll_PyGILState_Release)(PyGILState_STATE);
203#endif
204static long(*dll_PyInt_AsLong)(PyObject *);
205static PyObject*(*dll_PyInt_FromLong)(long);
206static PyTypeObject* dll_PyInt_Type;
207static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
208static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
209static PyObject*(*dll_PyList_New)(PyInt size);
210static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
211static PyInt(*dll_PyList_Size)(PyObject *);
212static PyTypeObject* dll_PyList_Type;
213static PyObject*(*dll_PyImport_ImportModule)(const char *);
214static PyObject*(*dll_PyDict_New)(void);
215static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
216static PyObject*(*dll_PyModule_GetDict)(PyObject *);
217static int(*dll_PyRun_SimpleString)(char *);
218static char*(*dll_PyString_AsString)(PyObject *);
219static PyObject*(*dll_PyString_FromString)(const char *);
220static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
221static PyInt(*dll_PyString_Size)(PyObject *);
222static PyTypeObject* dll_PyString_Type;
223static int(*dll_PySys_SetObject)(char *, PyObject *);
224static int(*dll_PySys_SetArgv)(int, char **);
225static PyTypeObject* dll_PyType_Type;
226static PyObject*(*dll_Py_BuildValue)(char *, ...);
227static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
228static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
229static void(*dll_Py_Initialize)(void);
230static void(*dll_Py_Finalize)(void);
231static int(*dll_Py_IsInitialized)(void);
232static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
233static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
234static PyObject* dll__Py_NoneStruct;
235# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
236static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
237# endif
238# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
239static void* (*dll_PyObject_Malloc)(size_t);
240static void (*dll_PyObject_Free)(void*);
241# endif
242
243static HINSTANCE hinstPython = 0; /* Instance of python.dll */
244
245/* Imported exception objects */
246static PyObject *imp_PyExc_AttributeError;
247static PyObject *imp_PyExc_IndexError;
248static PyObject *imp_PyExc_KeyboardInterrupt;
249static PyObject *imp_PyExc_TypeError;
250static PyObject *imp_PyExc_ValueError;
251
252# define PyExc_AttributeError imp_PyExc_AttributeError
253# define PyExc_IndexError imp_PyExc_IndexError
254# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
255# define PyExc_TypeError imp_PyExc_TypeError
256# define PyExc_ValueError imp_PyExc_ValueError
257
258/*
259 * Table of name to function pointer of python.
260 */
261# define PYTHON_PROC FARPROC
262static struct
263{
264    char *name;
265    PYTHON_PROC *ptr;
266} python_funcname_table[] =
267{
268    {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
269    {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
270    {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
271    {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
272    {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
273    {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
274    {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
275    {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
276    {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
277    {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
278    {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
279    {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
280# ifdef PY_CAN_RECURSE
281    {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
282    {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
283# endif
284    {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
285    {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
286    {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
287    {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
288    {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
289    {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
290    {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
291    {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
292    {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
293    {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
294    {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
295    {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
296    {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
297    {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
298    {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
299    {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
300    {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
301    {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
302    {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
303    {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
304    {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
305    {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
306    {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
307    {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
308# if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT
309    {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
310# else
311    {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
312# endif
313    {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
314    {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
315    {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
316    {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
317    {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
318    {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
319# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
320    {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
321# endif
322# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
323    {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
324    {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
325# endif
326    {"", NULL},
327};
328
329/*
330 * Free python.dll
331 */
332    static void
333end_dynamic_python(void)
334{
335    if (hinstPython)
336    {
337	close_dll(hinstPython);
338	hinstPython = 0;
339    }
340}
341
342/*
343 * Load library and get all pointers.
344 * Parameter 'libname' provides name of DLL.
345 * Return OK or FAIL.
346 */
347    static int
348python_runtime_link_init(char *libname, int verbose)
349{
350    int i;
351
352#if !defined(PY_NO_RTLD_GLOBAL) && defined(UNIX) && defined(FEAT_PYTHON3)
353    /* Can't have Python and Python3 loaded at the same time.
354     * It cause a crash, because RTLD_GLOBAL is needed for
355     * standard C extension libraries of one or both python versions. */
356    if (python3_loaded())
357    {
358	EMSG(_("E836: This Vim cannot execute :python after using :py3"));
359	return FAIL;
360    }
361#endif
362
363    if (hinstPython)
364	return OK;
365    hinstPython = load_dll(libname);
366    if (!hinstPython)
367    {
368	if (verbose)
369	    EMSG2(_(e_loadlib), libname);
370	return FAIL;
371    }
372
373    for (i = 0; python_funcname_table[i].ptr; ++i)
374    {
375	if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
376			python_funcname_table[i].name)) == NULL)
377	{
378	    close_dll(hinstPython);
379	    hinstPython = 0;
380	    if (verbose)
381		EMSG2(_(e_loadfunc), python_funcname_table[i].name);
382	    return FAIL;
383	}
384    }
385    return OK;
386}
387
388/*
389 * If python is enabled (there is installed python on Windows system) return
390 * TRUE, else FALSE.
391 */
392    int
393python_enabled(int verbose)
394{
395    return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
396}
397
398/*
399 * Load the standard Python exceptions - don't import the symbols from the
400 * DLL, as this can cause errors (importing data symbols is not reliable).
401 */
402    static void
403get_exceptions(void)
404{
405    PyObject *exmod = PyImport_ImportModule("exceptions");
406    PyObject *exdict = PyModule_GetDict(exmod);
407    imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
408    imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
409    imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
410    imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
411    imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
412    Py_XINCREF(imp_PyExc_AttributeError);
413    Py_XINCREF(imp_PyExc_IndexError);
414    Py_XINCREF(imp_PyExc_KeyboardInterrupt);
415    Py_XINCREF(imp_PyExc_TypeError);
416    Py_XINCREF(imp_PyExc_ValueError);
417    Py_XDECREF(exmod);
418}
419#endif /* DYNAMIC_PYTHON */
420
421static PyObject *BufferNew (buf_T *);
422static PyObject *WindowNew(win_T *);
423static PyObject *LineToString(const char *);
424
425static PyTypeObject RangeType;
426
427/*
428 * Include the code shared with if_python3.c
429 */
430#include "if_py_both.h"
431
432
433/******************************************************
434 * Internal function prototypes.
435 */
436
437static PyInt RangeStart;
438static PyInt RangeEnd;
439
440static void PythonIO_Flush(void);
441static int PythonIO_Init(void);
442static int PythonMod_Init(void);
443
444/* Utility functions for the vim/python interface
445 * ----------------------------------------------
446 */
447
448static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
449
450
451/******************************************************
452 * 1. Python interpreter main program.
453 */
454
455static int initialised = 0;
456
457#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
458typedef PyObject PyThreadState;
459#endif
460
461#ifdef PY_CAN_RECURSE
462static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
463#else
464static PyThreadState *saved_python_thread = NULL;
465#endif
466
467/*
468 * Suspend a thread of the Python interpreter, other threads are allowed to
469 * run.
470 */
471    static void
472Python_SaveThread(void)
473{
474#ifdef PY_CAN_RECURSE
475    PyGILState_Release(pygilstate);
476#else
477    saved_python_thread = PyEval_SaveThread();
478#endif
479}
480
481/*
482 * Restore a thread of the Python interpreter, waits for other threads to
483 * block.
484 */
485    static void
486Python_RestoreThread(void)
487{
488#ifdef PY_CAN_RECURSE
489    pygilstate = PyGILState_Ensure();
490#else
491    PyEval_RestoreThread(saved_python_thread);
492    saved_python_thread = NULL;
493#endif
494}
495
496    void
497python_end()
498{
499    static int recurse = 0;
500
501    /* If a crash occurs while doing this, don't try again. */
502    if (recurse != 0)
503	return;
504
505    ++recurse;
506
507#ifdef DYNAMIC_PYTHON
508    if (hinstPython && Py_IsInitialized())
509    {
510	Python_RestoreThread();	    /* enter python */
511	Py_Finalize();
512    }
513    end_dynamic_python();
514#else
515    if (Py_IsInitialized())
516    {
517	Python_RestoreThread();	    /* enter python */
518	Py_Finalize();
519    }
520#endif
521
522    --recurse;
523}
524
525#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
526    int
527python_loaded()
528{
529    return (hinstPython != 0);
530}
531#endif
532
533    static int
534Python_Init(void)
535{
536    if (!initialised)
537    {
538#ifdef DYNAMIC_PYTHON
539	if (!python_enabled(TRUE))
540	{
541	    EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
542	    goto fail;
543	}
544#endif
545
546	init_structs();
547
548#if !defined(MACOS) || defined(MACOS_X_UNIX)
549	Py_Initialize();
550#else
551	PyMac_Initialize();
552#endif
553	/* initialise threads */
554	PyEval_InitThreads();
555
556#ifdef DYNAMIC_PYTHON
557	get_exceptions();
558#endif
559
560	if (PythonIO_Init())
561	    goto fail;
562
563	if (PythonMod_Init())
564	    goto fail;
565
566	/* Remove the element from sys.path that was added because of our
567	 * argv[0] value in PythonMod_Init().  Previously we used an empty
568	 * string, but dependinding on the OS we then get an empty entry or
569	 * the current directory in sys.path. */
570	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
571
572	/* the first python thread is vim's, release the lock */
573	Python_SaveThread();
574
575	initialised = 1;
576    }
577
578    return 0;
579
580fail:
581    /* We call PythonIO_Flush() here to print any Python errors.
582     * This is OK, as it is possible to call this function even
583     * if PythonIO_Init() has not completed successfully (it will
584     * not do anything in this case).
585     */
586    PythonIO_Flush();
587    return -1;
588}
589
590/*
591 * External interface
592 */
593    static void
594DoPythonCommand(exarg_T *eap, const char *cmd)
595{
596#ifndef PY_CAN_RECURSE
597    static int		recursive = 0;
598#endif
599#if defined(MACOS) && !defined(MACOS_X_UNIX)
600    GrafPtr		oldPort;
601#endif
602#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
603    char		*saved_locale;
604#endif
605
606#ifndef PY_CAN_RECURSE
607    if (recursive)
608    {
609	EMSG(_("E659: Cannot invoke Python recursively"));
610	return;
611    }
612    ++recursive;
613#endif
614
615#if defined(MACOS) && !defined(MACOS_X_UNIX)
616    GetPort(&oldPort);
617    /* Check if the Python library is available */
618    if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
619	goto theend;
620#endif
621    if (Python_Init())
622	goto theend;
623
624    RangeStart = eap->line1;
625    RangeEnd = eap->line2;
626    Python_Release_Vim();	    /* leave vim */
627
628#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
629    /* Python only works properly when the LC_NUMERIC locale is "C". */
630    saved_locale = setlocale(LC_NUMERIC, NULL);
631    if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
632	saved_locale = NULL;
633    else
634    {
635	/* Need to make a copy, value may change when setting new locale. */
636	saved_locale = (char *)vim_strsave((char_u *)saved_locale);
637	(void)setlocale(LC_NUMERIC, "C");
638    }
639#endif
640
641    Python_RestoreThread();	    /* enter python */
642
643    PyRun_SimpleString((char *)(cmd));
644
645    Python_SaveThread();	    /* leave python */
646
647#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
648    if (saved_locale != NULL)
649    {
650	(void)setlocale(LC_NUMERIC, saved_locale);
651	vim_free(saved_locale);
652    }
653#endif
654
655    Python_Lock_Vim();		    /* enter vim */
656    PythonIO_Flush();
657#if defined(MACOS) && !defined(MACOS_X_UNIX)
658    SetPort(oldPort);
659#endif
660
661theend:
662#ifndef PY_CAN_RECURSE
663    --recursive;
664#endif
665    return;	    /* keeps lint happy */
666}
667
668/*
669 * ":python"
670 */
671    void
672ex_python(exarg_T *eap)
673{
674    char_u *script;
675
676    script = script_get(eap, eap->arg);
677    if (!eap->skip)
678    {
679	if (script == NULL)
680	    DoPythonCommand(eap, (char *)eap->arg);
681	else
682	    DoPythonCommand(eap, (char *)script);
683    }
684    vim_free(script);
685}
686
687#define BUFFER_SIZE 1024
688
689/*
690 * ":pyfile"
691 */
692    void
693ex_pyfile(exarg_T *eap)
694{
695    static char buffer[BUFFER_SIZE];
696    const char *file = (char *)eap->arg;
697    char *p;
698
699    /* Have to do it like this. PyRun_SimpleFile requires you to pass a
700     * stdio file pointer, but Vim and the Python DLL are compiled with
701     * different options under Windows, meaning that stdio pointers aren't
702     * compatible between the two. Yuk.
703     *
704     * Put the string "execfile('file')" into buffer. But, we need to
705     * escape any backslashes or single quotes in the file name, so that
706     * Python won't mangle the file name.
707     */
708    strcpy(buffer, "execfile('");
709    p = buffer + 10; /* size of "execfile('" */
710
711    while (*file && p < buffer + (BUFFER_SIZE - 3))
712    {
713	if (*file == '\\' || *file == '\'')
714	    *p++ = '\\';
715	*p++ = *file++;
716    }
717
718    /* If we didn't finish the file name, we hit a buffer overflow */
719    if (*file != '\0')
720	return;
721
722    /* Put in the terminating "')" and a null */
723    *p++ = '\'';
724    *p++ = ')';
725    *p++ = '\0';
726
727    /* Execute the file */
728    DoPythonCommand(eap, buffer);
729}
730
731/******************************************************
732 * 2. Python output stream: writes output via [e]msg().
733 */
734
735/* Implementation functions
736 */
737
738    static PyObject *
739OutputGetattr(PyObject *self, char *name)
740{
741    if (strcmp(name, "softspace") == 0)
742	return PyInt_FromLong(((OutputObject *)(self))->softspace);
743
744    return Py_FindMethod(OutputMethods, self, name);
745}
746
747    static int
748OutputSetattr(PyObject *self, char *name, PyObject *val)
749{
750    if (val == NULL) {
751	PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
752	return -1;
753    }
754
755    if (strcmp(name, "softspace") == 0)
756    {
757	if (!PyInt_Check(val)) {
758	    PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
759	    return -1;
760	}
761
762	((OutputObject *)(self))->softspace = PyInt_AsLong(val);
763	return 0;
764    }
765
766    PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
767    return -1;
768}
769
770/***************/
771
772    static int
773PythonIO_Init(void)
774{
775    /* Fixups... */
776    OutputType.ob_type = &PyType_Type;
777
778    return PythonIO_Init_io();
779}
780
781/******************************************************
782 * 3. Implementation of the Vim module for Python
783 */
784
785/* Window type - Implementation functions
786 * --------------------------------------
787 */
788
789#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
790
791static void WindowDestructor(PyObject *);
792static PyObject *WindowGetattr(PyObject *, char *);
793
794/* Buffer type - Implementation functions
795 * --------------------------------------
796 */
797
798#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
799
800static void BufferDestructor(PyObject *);
801static PyObject *BufferGetattr(PyObject *, char *);
802static PyObject *BufferRepr(PyObject *);
803
804static PyInt BufferLength(PyObject *);
805static PyObject *BufferItem(PyObject *, PyInt);
806static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
807static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
808static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
809
810/* Line range type - Implementation functions
811 * --------------------------------------
812 */
813
814#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
815
816static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
817static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
818
819/* Current objects type - Implementation functions
820 * -----------------------------------------------
821 */
822
823static PyObject *CurrentGetattr(PyObject *, char *);
824static int CurrentSetattr(PyObject *, char *, PyObject *);
825
826/* Common routines for buffers and line ranges
827 * -------------------------------------------
828 */
829
830    static PyInt
831RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
832{
833    PyInt size;
834    PyInt len_change;
835
836    /* Self must be a valid buffer */
837    if (CheckBuffer(self))
838	return -1;
839
840    /* Sort out the slice range */
841    size = end - start + 1;
842
843    if (lo < 0)
844	lo = 0;
845    else if (lo > size)
846	lo = size;
847    if (hi < 0)
848	hi = 0;
849    if (hi < lo)
850	hi = lo;
851    else if (hi > size)
852	hi = size;
853
854    if (SetBufferLineList(self->buf, lo + start, hi + start,
855						    val, &len_change) == FAIL)
856	return -1;
857
858    if (new_end)
859	*new_end = end + len_change;
860
861    return 0;
862}
863
864static PySequenceMethods BufferAsSeq = {
865    (PyInquiry)		BufferLength,	    /* sq_length,    len(x)   */
866    (binaryfunc)	0, /* BufferConcat, */	     /* sq_concat,    x+y      */
867    (PyIntArgFunc)	0, /* BufferRepeat, */	     /* sq_repeat,    x*n      */
868    (PyIntArgFunc)	BufferItem,	    /* sq_item,      x[i]     */
869    (PyIntIntArgFunc)	BufferSlice,	    /* sq_slice,     x[i:j]   */
870    (PyIntObjArgProc)	BufferAssItem,	    /* sq_ass_item,  x[i]=v   */
871    (PyIntIntObjArgProc)	BufferAssSlice,     /* sq_ass_slice, x[i:j]=v */
872};
873
874static PyTypeObject BufferType = {
875    PyObject_HEAD_INIT(0)
876    0,
877    "buffer",
878    sizeof(BufferObject),
879    0,
880
881    (destructor)    BufferDestructor,	/* tp_dealloc,	refcount==0  */
882    (printfunc)     0,			/* tp_print,	print x      */
883    (getattrfunc)   BufferGetattr,	/* tp_getattr,	x.attr	     */
884    (setattrfunc)   0,			/* tp_setattr,	x.attr=v     */
885    (cmpfunc)	    0,			/* tp_compare,	x>y	     */
886    (reprfunc)	    BufferRepr,		/* tp_repr,	`x`, print x */
887
888    0,		    /* as number */
889    &BufferAsSeq,   /* as sequence */
890    0,		    /* as mapping */
891
892    (hashfunc) 0,			/* tp_hash, dict(x) */
893    (ternaryfunc) 0,			/* tp_call, x()     */
894    (reprfunc) 0,			/* tp_str,  str(x)  */
895};
896
897/* Buffer object - Implementation
898 */
899
900    static PyObject *
901BufferNew(buf_T *buf)
902{
903    /* We need to handle deletion of buffers underneath us.
904     * If we add a "b_python_ref" field to the buf_T structure,
905     * then we can get at it in buf_freeall() in vim. We then
906     * need to create only ONE Python object per buffer - if
907     * we try to create a second, just INCREF the existing one
908     * and return it. The (single) Python object referring to
909     * the buffer is stored in "b_python_ref".
910     * Question: what to do on a buf_freeall(). We'll probably
911     * have to either delete the Python object (DECREF it to
912     * zero - a bad idea, as it leaves dangling refs!) or
913     * set the buf_T * value to an invalid value (-1?), which
914     * means we need checks in all access functions... Bah.
915     */
916
917    BufferObject *self;
918
919    if (buf->b_python_ref != NULL)
920    {
921	self = buf->b_python_ref;
922	Py_INCREF(self);
923    }
924    else
925    {
926	self = PyObject_NEW(BufferObject, &BufferType);
927	if (self == NULL)
928	    return NULL;
929	self->buf = buf;
930	buf->b_python_ref = self;
931    }
932
933    return (PyObject *)(self);
934}
935
936    static void
937BufferDestructor(PyObject *self)
938{
939    BufferObject *this = (BufferObject *)(self);
940
941    if (this->buf && this->buf != INVALID_BUFFER_VALUE)
942	this->buf->b_python_ref = NULL;
943
944    Py_DECREF(self);
945}
946
947    static PyObject *
948BufferGetattr(PyObject *self, char *name)
949{
950    BufferObject *this = (BufferObject *)(self);
951
952    if (CheckBuffer(this))
953	return NULL;
954
955    if (strcmp(name, "name") == 0)
956	return Py_BuildValue("s", this->buf->b_ffname);
957    else if (strcmp(name, "number") == 0)
958	return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
959    else if (strcmp(name,"__members__") == 0)
960	return Py_BuildValue("[ss]", "name", "number");
961    else
962	return Py_FindMethod(BufferMethods, self, name);
963}
964
965    static PyObject *
966BufferRepr(PyObject *self)
967{
968    static char repr[100];
969    BufferObject *this = (BufferObject *)(self);
970
971    if (this->buf == INVALID_BUFFER_VALUE)
972    {
973	vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
974	return PyString_FromString(repr);
975    }
976    else
977    {
978	char *name = (char *)this->buf->b_fname;
979	PyInt len;
980
981	if (name == NULL)
982	    name = "";
983	len = strlen(name);
984
985	if (len > 35)
986	    name = name + (35 - len);
987
988	vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
989
990	return PyString_FromString(repr);
991    }
992}
993
994/******************/
995
996    static PyInt
997BufferLength(PyObject *self)
998{
999    /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1000    if (CheckBuffer((BufferObject *)(self)))
1001	return -1; /* ??? */
1002
1003    return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1004}
1005
1006    static PyObject *
1007BufferItem(PyObject *self, PyInt n)
1008{
1009    return RBItem((BufferObject *)(self), n, 1,
1010		  (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1011}
1012
1013    static PyObject *
1014BufferSlice(PyObject *self, PyInt lo, PyInt hi)
1015{
1016    return RBSlice((BufferObject *)(self), lo, hi, 1,
1017		   (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1018}
1019
1020    static PyInt
1021BufferAssItem(PyObject *self, PyInt n, PyObject *val)
1022{
1023    return RBAsItem((BufferObject *)(self), n, val, 1,
1024		     (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1025		     NULL);
1026}
1027
1028    static PyInt
1029BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1030{
1031    return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
1032		      (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1033		      NULL);
1034}
1035
1036static PySequenceMethods RangeAsSeq = {
1037    (PyInquiry)		RangeLength,	    /* sq_length,    len(x)   */
1038    (binaryfunc)	0, /* RangeConcat, */	     /* sq_concat,    x+y      */
1039    (PyIntArgFunc)	0, /* RangeRepeat, */	     /* sq_repeat,    x*n      */
1040    (PyIntArgFunc)	RangeItem,	    /* sq_item,      x[i]     */
1041    (PyIntIntArgFunc)	RangeSlice,	    /* sq_slice,     x[i:j]   */
1042    (PyIntObjArgProc)	RangeAssItem,	    /* sq_ass_item,  x[i]=v   */
1043    (PyIntIntObjArgProc)	RangeAssSlice,	    /* sq_ass_slice, x[i:j]=v */
1044};
1045
1046/* Line range object - Implementation
1047 */
1048
1049    static void
1050RangeDestructor(PyObject *self)
1051{
1052    Py_DECREF(((RangeObject *)(self))->buf);
1053    Py_DECREF(self);
1054}
1055
1056    static PyObject *
1057RangeGetattr(PyObject *self, char *name)
1058{
1059    if (strcmp(name, "start") == 0)
1060	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
1061    else if (strcmp(name, "end") == 0)
1062	return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
1063    else
1064	return Py_FindMethod(RangeMethods, self, name);
1065}
1066
1067/****************/
1068
1069    static PyInt
1070RangeAssItem(PyObject *self, PyInt n, PyObject *val)
1071{
1072    return RBAsItem(((RangeObject *)(self))->buf, n, val,
1073		     ((RangeObject *)(self))->start,
1074		     ((RangeObject *)(self))->end,
1075		     &((RangeObject *)(self))->end);
1076}
1077
1078    static PyInt
1079RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
1080{
1081    return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
1082		      ((RangeObject *)(self))->start,
1083		      ((RangeObject *)(self))->end,
1084		      &((RangeObject *)(self))->end);
1085}
1086
1087/* Buffer list object - Definitions
1088 */
1089
1090typedef struct
1091{
1092    PyObject_HEAD
1093} BufListObject;
1094
1095static PySequenceMethods BufListAsSeq = {
1096    (PyInquiry)		BufListLength,	    /* sq_length,    len(x)   */
1097    (binaryfunc)	0,		    /* sq_concat,    x+y      */
1098    (PyIntArgFunc)	0,		    /* sq_repeat,    x*n      */
1099    (PyIntArgFunc)	BufListItem,	    /* sq_item,      x[i]     */
1100    (PyIntIntArgFunc)	0,		    /* sq_slice,     x[i:j]   */
1101    (PyIntObjArgProc)	0,		    /* sq_ass_item,  x[i]=v   */
1102    (PyIntIntObjArgProc)	0,		    /* sq_ass_slice, x[i:j]=v */
1103};
1104
1105static PyTypeObject BufListType = {
1106    PyObject_HEAD_INIT(0)
1107    0,
1108    "buffer list",
1109    sizeof(BufListObject),
1110    0,
1111
1112    (destructor)    0,			/* tp_dealloc,	refcount==0  */
1113    (printfunc)     0,			/* tp_print,	print x      */
1114    (getattrfunc)   0,			/* tp_getattr,	x.attr	     */
1115    (setattrfunc)   0,			/* tp_setattr,	x.attr=v     */
1116    (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1117    (reprfunc)	    0,			/* tp_repr,	`x`, print x */
1118
1119    0,		    /* as number */
1120    &BufListAsSeq,  /* as sequence */
1121    0,		    /* as mapping */
1122
1123    (hashfunc) 0,			/* tp_hash, dict(x) */
1124    (ternaryfunc) 0,			/* tp_call, x()     */
1125    (reprfunc) 0,			/* tp_str,  str(x)  */
1126};
1127
1128/* Window object - Definitions
1129 */
1130
1131static struct PyMethodDef WindowMethods[] = {
1132    /* name,	    function,		calling,    documentation */
1133    { NULL,	    NULL,		0,	    NULL }
1134};
1135
1136static PyTypeObject WindowType = {
1137    PyObject_HEAD_INIT(0)
1138    0,
1139    "window",
1140    sizeof(WindowObject),
1141    0,
1142
1143    (destructor)    WindowDestructor,	/* tp_dealloc,	refcount==0  */
1144    (printfunc)     0,			/* tp_print,	print x      */
1145    (getattrfunc)   WindowGetattr,	/* tp_getattr,	x.attr	     */
1146    (setattrfunc)   WindowSetattr,	/* tp_setattr,	x.attr=v     */
1147    (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1148    (reprfunc)	    WindowRepr,		/* tp_repr,	`x`, print x */
1149
1150    0,		    /* as number */
1151    0,		    /* as sequence */
1152    0,		    /* as mapping */
1153
1154    (hashfunc) 0,			/* tp_hash, dict(x) */
1155    (ternaryfunc) 0,			/* tp_call, x()     */
1156    (reprfunc) 0,			/* tp_str,  str(x)  */
1157};
1158
1159/* Window object - Implementation
1160 */
1161
1162    static PyObject *
1163WindowNew(win_T *win)
1164{
1165    /* We need to handle deletion of windows underneath us.
1166     * If we add a "w_python_ref" field to the win_T structure,
1167     * then we can get at it in win_free() in vim. We then
1168     * need to create only ONE Python object per window - if
1169     * we try to create a second, just INCREF the existing one
1170     * and return it. The (single) Python object referring to
1171     * the window is stored in "w_python_ref".
1172     * On a win_free() we set the Python object's win_T* field
1173     * to an invalid value. We trap all uses of a window
1174     * object, and reject them if the win_T* field is invalid.
1175     */
1176
1177    WindowObject *self;
1178
1179    if (win->w_python_ref)
1180    {
1181	self = win->w_python_ref;
1182	Py_INCREF(self);
1183    }
1184    else
1185    {
1186	self = PyObject_NEW(WindowObject, &WindowType);
1187	if (self == NULL)
1188	    return NULL;
1189	self->win = win;
1190	win->w_python_ref = self;
1191    }
1192
1193    return (PyObject *)(self);
1194}
1195
1196    static void
1197WindowDestructor(PyObject *self)
1198{
1199    WindowObject *this = (WindowObject *)(self);
1200
1201    if (this->win && this->win != INVALID_WINDOW_VALUE)
1202	this->win->w_python_ref = NULL;
1203
1204    Py_DECREF(self);
1205}
1206
1207    static PyObject *
1208WindowGetattr(PyObject *self, char *name)
1209{
1210    WindowObject *this = (WindowObject *)(self);
1211
1212    if (CheckWindow(this))
1213	return NULL;
1214
1215    if (strcmp(name, "buffer") == 0)
1216	return (PyObject *)BufferNew(this->win->w_buffer);
1217    else if (strcmp(name, "cursor") == 0)
1218    {
1219	pos_T *pos = &this->win->w_cursor;
1220
1221	return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1222    }
1223    else if (strcmp(name, "height") == 0)
1224	return Py_BuildValue("l", (long)(this->win->w_height));
1225#ifdef FEAT_VERTSPLIT
1226    else if (strcmp(name, "width") == 0)
1227	return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1228#endif
1229    else if (strcmp(name,"__members__") == 0)
1230	return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1231    else
1232	return Py_FindMethod(WindowMethods, self, name);
1233}
1234
1235/* Window list object - Definitions
1236 */
1237
1238typedef struct
1239{
1240    PyObject_HEAD
1241}
1242WinListObject;
1243
1244static PySequenceMethods WinListAsSeq = {
1245    (PyInquiry)		WinListLength,	    /* sq_length,    len(x)   */
1246    (binaryfunc)	0,		    /* sq_concat,    x+y      */
1247    (PyIntArgFunc)	0,		    /* sq_repeat,    x*n      */
1248    (PyIntArgFunc)	WinListItem,	    /* sq_item,      x[i]     */
1249    (PyIntIntArgFunc)	0,		    /* sq_slice,     x[i:j]   */
1250    (PyIntObjArgProc)	0,		    /* sq_ass_item,  x[i]=v   */
1251    (PyIntIntObjArgProc)	0,		    /* sq_ass_slice, x[i:j]=v */
1252};
1253
1254static PyTypeObject WinListType = {
1255    PyObject_HEAD_INIT(0)
1256    0,
1257    "window list",
1258    sizeof(WinListObject),
1259    0,
1260
1261    (destructor)    0,			/* tp_dealloc,	refcount==0  */
1262    (printfunc)     0,			/* tp_print,	print x      */
1263    (getattrfunc)   0,			/* tp_getattr,	x.attr	     */
1264    (setattrfunc)   0,			/* tp_setattr,	x.attr=v     */
1265    (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1266    (reprfunc)	    0,			/* tp_repr,	`x`, print x */
1267
1268    0,		    /* as number */
1269    &WinListAsSeq,  /* as sequence */
1270    0,		    /* as mapping */
1271
1272    (hashfunc) 0,			/* tp_hash, dict(x) */
1273    (ternaryfunc) 0,			/* tp_call, x()     */
1274    (reprfunc) 0,			/* tp_str,  str(x)  */
1275};
1276
1277/* Current items object - Definitions
1278 */
1279
1280typedef struct
1281{
1282    PyObject_HEAD
1283} CurrentObject;
1284
1285static PyTypeObject CurrentType = {
1286    PyObject_HEAD_INIT(0)
1287    0,
1288    "current data",
1289    sizeof(CurrentObject),
1290    0,
1291
1292    (destructor)    0,			/* tp_dealloc,	refcount==0  */
1293    (printfunc)     0,			/* tp_print,	print x      */
1294    (getattrfunc)   CurrentGetattr,	/* tp_getattr,	x.attr	     */
1295    (setattrfunc)   CurrentSetattr,	/* tp_setattr,	x.attr=v     */
1296    (cmpfunc)	    0,			/* tp_compare,	x>y	     */
1297    (reprfunc)	    0,			/* tp_repr,	`x`, print x */
1298
1299    0,		    /* as number */
1300    0,		    /* as sequence */
1301    0,		    /* as mapping */
1302
1303    (hashfunc) 0,			/* tp_hash, dict(x) */
1304    (ternaryfunc) 0,			/* tp_call, x()     */
1305    (reprfunc) 0,			/* tp_str,  str(x)  */
1306};
1307
1308/* Current items object - Implementation
1309 */
1310    static PyObject *
1311CurrentGetattr(PyObject *self UNUSED, char *name)
1312{
1313    if (strcmp(name, "buffer") == 0)
1314	return (PyObject *)BufferNew(curbuf);
1315    else if (strcmp(name, "window") == 0)
1316	return (PyObject *)WindowNew(curwin);
1317    else if (strcmp(name, "line") == 0)
1318	return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
1319    else if (strcmp(name, "range") == 0)
1320	return RangeNew(curbuf, RangeStart, RangeEnd);
1321    else if (strcmp(name,"__members__") == 0)
1322	return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1323    else
1324    {
1325	PyErr_SetString(PyExc_AttributeError, name);
1326	return NULL;
1327    }
1328}
1329
1330    static int
1331CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
1332{
1333    if (strcmp(name, "line") == 0)
1334    {
1335	if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
1336	    return -1;
1337
1338	return 0;
1339    }
1340    else
1341    {
1342	PyErr_SetString(PyExc_AttributeError, name);
1343	return -1;
1344    }
1345}
1346
1347/* External interface
1348 */
1349
1350    void
1351python_buffer_free(buf_T *buf)
1352{
1353    if (buf->b_python_ref != NULL)
1354    {
1355	BufferObject *bp = buf->b_python_ref;
1356	bp->buf = INVALID_BUFFER_VALUE;
1357	buf->b_python_ref = NULL;
1358    }
1359}
1360
1361#if defined(FEAT_WINDOWS) || defined(PROTO)
1362    void
1363python_window_free(win_T *win)
1364{
1365    if (win->w_python_ref != NULL)
1366    {
1367	WindowObject *wp = win->w_python_ref;
1368	wp->win = INVALID_WINDOW_VALUE;
1369	win->w_python_ref = NULL;
1370    }
1371}
1372#endif
1373
1374static BufListObject TheBufferList =
1375{
1376    PyObject_HEAD_INIT(&BufListType)
1377};
1378
1379static WinListObject TheWindowList =
1380{
1381    PyObject_HEAD_INIT(&WinListType)
1382};
1383
1384static CurrentObject TheCurrent =
1385{
1386    PyObject_HEAD_INIT(&CurrentType)
1387};
1388
1389    static int
1390PythonMod_Init(void)
1391{
1392    PyObject *mod;
1393    PyObject *dict;
1394    /* The special value is removed from sys.path in Python_Init(). */
1395    static char *(argv[2]) = {"/must>not&exist/foo", NULL};
1396
1397    /* Fixups... */
1398    BufferType.ob_type = &PyType_Type;
1399    RangeType.ob_type = &PyType_Type;
1400    WindowType.ob_type = &PyType_Type;
1401    BufListType.ob_type = &PyType_Type;
1402    WinListType.ob_type = &PyType_Type;
1403    CurrentType.ob_type = &PyType_Type;
1404
1405    /* Set sys.argv[] to avoid a crash in warn(). */
1406    PySys_SetArgv(1, argv);
1407
1408    mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
1409    dict = PyModule_GetDict(mod);
1410
1411    VimError = Py_BuildValue("s", "vim.error");
1412
1413    PyDict_SetItemString(dict, "error", VimError);
1414    PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1415    PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1416    PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
1417
1418    if (PyErr_Occurred())
1419	return -1;
1420
1421    return 0;
1422}
1423
1424/*************************************************************************
1425 * 4. Utility functions for handling the interface between Vim and Python.
1426 */
1427
1428/* Replace a range of lines in the specified buffer. The line numbers are in
1429 * Vim format (1-based). The range is from lo up to, but not including, hi.
1430 * The replacement lines are given as a Python list of string objects. The
1431 * list is checked for validity and correct format. Errors are returned as a
1432 * value of FAIL.  The return value is OK on success.
1433 * If OK is returned and len_change is not NULL, *len_change
1434 * is set to the change in the buffer length.
1435 */
1436    static int
1437SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
1438{
1439    /* First of all, we check the thpe of the supplied Python object.
1440     * There are three cases:
1441     *	  1. NULL, or None - this is a deletion.
1442     *	  2. A list	   - this is a replacement.
1443     *	  3. Anything else - this is an error.
1444     */
1445    if (list == Py_None || list == NULL)
1446    {
1447	PyInt	i;
1448	PyInt	n = (int)(hi - lo);
1449	buf_T	*savebuf = curbuf;
1450
1451	PyErr_Clear();
1452	curbuf = buf;
1453
1454	if (u_savedel((linenr_T)lo, (long)n) == FAIL)
1455	    PyErr_SetVim(_("cannot save undo information"));
1456	else
1457	{
1458	    for (i = 0; i < n; ++i)
1459	    {
1460		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1461		{
1462		    PyErr_SetVim(_("cannot delete line"));
1463		    break;
1464		}
1465	    }
1466	    if (buf == curwin->w_buffer)
1467		py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
1468	    deleted_lines_mark((linenr_T)lo, (long)i);
1469	}
1470
1471	curbuf = savebuf;
1472
1473	if (PyErr_Occurred() || VimErrorCheck())
1474	    return FAIL;
1475
1476	if (len_change)
1477	    *len_change = -n;
1478
1479	return OK;
1480    }
1481    else if (PyList_Check(list))
1482    {
1483	PyInt	i;
1484	PyInt	new_len = PyList_Size(list);
1485	PyInt	old_len = hi - lo;
1486	PyInt	extra = 0;	/* lines added to text, can be negative */
1487	char	**array;
1488	buf_T	*savebuf;
1489
1490	if (new_len == 0)	/* avoid allocating zero bytes */
1491	    array = NULL;
1492	else
1493	{
1494	    array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
1495	    if (array == NULL)
1496	    {
1497		PyErr_NoMemory();
1498		return FAIL;
1499	    }
1500	}
1501
1502	for (i = 0; i < new_len; ++i)
1503	{
1504	    PyObject *line = PyList_GetItem(list, i);
1505
1506	    array[i] = StringToLine(line);
1507	    if (array[i] == NULL)
1508	    {
1509		while (i)
1510		    vim_free(array[--i]);
1511		vim_free(array);
1512		return FAIL;
1513	    }
1514	}
1515
1516	savebuf = curbuf;
1517
1518	PyErr_Clear();
1519	curbuf = buf;
1520
1521	if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
1522	    PyErr_SetVim(_("cannot save undo information"));
1523
1524	/* If the size of the range is reducing (ie, new_len < old_len) we
1525	 * need to delete some old_len. We do this at the start, by
1526	 * repeatedly deleting line "lo".
1527	 */
1528	if (!PyErr_Occurred())
1529	{
1530	    for (i = 0; i < old_len - new_len; ++i)
1531		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1532		{
1533		    PyErr_SetVim(_("cannot delete line"));
1534		    break;
1535		}
1536	    extra -= i;
1537	}
1538
1539	/* For as long as possible, replace the existing old_len with the
1540	 * new old_len. This is a more efficient operation, as it requires
1541	 * less memory allocation and freeing.
1542	 */
1543	if (!PyErr_Occurred())
1544	{
1545	    for (i = 0; i < old_len && i < new_len; ++i)
1546		if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
1547								      == FAIL)
1548		{
1549		    PyErr_SetVim(_("cannot replace line"));
1550		    break;
1551		}
1552	}
1553	else
1554	    i = 0;
1555
1556	/* Now we may need to insert the remaining new old_len. If we do, we
1557	 * must free the strings as we finish with them (we can't pass the
1558	 * responsibility to vim in this case).
1559	 */
1560	if (!PyErr_Occurred())
1561	{
1562	    while (i < new_len)
1563	    {
1564		if (ml_append((linenr_T)(lo + i - 1),
1565					(char_u *)array[i], 0, FALSE) == FAIL)
1566		{
1567		    PyErr_SetVim(_("cannot insert line"));
1568		    break;
1569		}
1570		vim_free(array[i]);
1571		++i;
1572		++extra;
1573	    }
1574	}
1575
1576	/* Free any left-over old_len, as a result of an error */
1577	while (i < new_len)
1578	{
1579	    vim_free(array[i]);
1580	    ++i;
1581	}
1582
1583	/* Free the array of old_len. All of its contents have now
1584	 * been dealt with (either freed, or the responsibility passed
1585	 * to vim.
1586	 */
1587	vim_free(array);
1588
1589	/* Adjust marks. Invalidate any which lie in the
1590	 * changed range, and move any in the remainder of the buffer.
1591	 */
1592	mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
1593						  (long)MAXLNUM, (long)extra);
1594	changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
1595
1596	if (buf == curwin->w_buffer)
1597	    py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
1598
1599	curbuf = savebuf;
1600
1601	if (PyErr_Occurred() || VimErrorCheck())
1602	    return FAIL;
1603
1604	if (len_change)
1605	    *len_change = new_len - old_len;
1606
1607	return OK;
1608    }
1609    else
1610    {
1611	PyErr_BadArgument();
1612	return FAIL;
1613    }
1614}
1615
1616/* Convert a Vim line into a Python string.
1617 * All internal newlines are replaced by null characters.
1618 *
1619 * On errors, the Python exception data is set, and NULL is returned.
1620 */
1621    static PyObject *
1622LineToString(const char *str)
1623{
1624    PyObject *result;
1625    PyInt len = strlen(str);
1626    char *p;
1627
1628    /* Allocate an Python string object, with uninitialised contents. We
1629     * must do it this way, so that we can modify the string in place
1630     * later. See the Python source, Objects/stringobject.c for details.
1631     */
1632    result = PyString_FromStringAndSize(NULL, len);
1633    if (result == NULL)
1634	return NULL;
1635
1636    p = PyString_AsString(result);
1637
1638    while (*str)
1639    {
1640	if (*str == '\n')
1641	    *p = '\0';
1642	else
1643	    *p = *str;
1644
1645	++p;
1646	++str;
1647    }
1648
1649    return result;
1650}
1651
1652
1653/* Don't generate a prototype for the next function, it generates an error on
1654 * newer Python versions. */
1655#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1656
1657    char *
1658Py_GetProgramName(void)
1659{
1660    return "vim";
1661}
1662#endif /* Python 1.4 */
1663
1664    static void
1665init_structs(void)
1666{
1667    vim_memset(&OutputType, 0, sizeof(OutputType));
1668    OutputType.tp_name = "message";
1669    OutputType.tp_basicsize = sizeof(OutputObject);
1670    OutputType.tp_getattr = OutputGetattr;
1671    OutputType.tp_setattr = OutputSetattr;
1672
1673    vim_memset(&RangeType, 0, sizeof(RangeType));
1674    RangeType.tp_name = "range";
1675    RangeType.tp_basicsize = sizeof(RangeObject);
1676    RangeType.tp_dealloc = RangeDestructor;
1677    RangeType.tp_getattr = RangeGetattr;
1678    RangeType.tp_repr = RangeRepr;
1679    RangeType.tp_as_sequence = &RangeAsSeq;
1680}
1681