annotate src/if_python3.c @ 2331:3840b7508835 vim73

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