diff options
author | Bram Moolenaar <Bram@vim.org> | 2010-07-24 23:51:45 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2010-07-24 23:51:45 +0200 |
commit | 170bf1aed576cea59a18c9015a3c7d417053c335 (patch) | |
tree | a3d3ee620442f962634a912372c8fea01048cd50 | |
parent | 365bdf7a7b2e28624186eed398af542cb9cc3fdb (diff) |
Move some common code from if_python.c and if_python3.c to if_py_both.h.
-rw-r--r-- | Filelist | 1 | ||||
-rw-r--r-- | src/Makefile | 4 | ||||
-rw-r--r-- | src/if_py_both.h | 256 | ||||
-rw-r--r-- | src/if_python.c | 278 | ||||
-rw-r--r-- | src/if_python3.c | 549 |
5 files changed, 476 insertions, 612 deletions
@@ -217,6 +217,7 @@ SRC_DOS_UNIX = \ src/if_perlsfio.c \ src/if_python.c \ src/if_python3.c \ + src/if_py_both.h \ src/if_ruby.c \ src/if_sniff.h \ src/if_tcl.c \ diff --git a/src/Makefile b/src/Makefile index f0c2316f62..f9120938ce 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2517,10 +2517,10 @@ objects/py3_config.o: $(PYTHON3_CONFDIR)/config.c $(CCC) $(PYTHON3_CFLAGS) -o $@ $(PYTHON3_CONFDIR)/config.c \ -I$(PYTHON3_CONFDIR) -DHAVE_CONFIG_H -DNO_MAIN -objects/if_python.o: if_python.c +objects/if_python.o: if_python.c if_py_both.h $(CCC) $(PYTHON_CFLAGS) $(PYTHON_CFLAGS_EXTRA) -o $@ if_python.c -objects/if_python3.o: if_python3.c +objects/if_python3.o: if_python3.c if_py_both.h $(CCC) $(PYTHON3_CFLAGS) $(PYTHON3_CFLAGS_EXTRA) -o $@ if_python3.c objects/if_ruby.o: if_ruby.c diff --git a/src/if_py_both.h b/src/if_py_both.h new file mode 100644 index 0000000000..8934d95dc1 --- /dev/null +++ b/src/if_py_both.h @@ -0,0 +1,256 @@ +/* vi:set ts=8 sts=4 sw=4: + * + * VIM - Vi IMproved by Bram Moolenaar + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ +/* + * Python extensions by Paul Moore, David Leonard, Roland Puntaier. + * + * Common code for if_python.c and if_python3.c. + */ + +/* + * obtain a lock on the Vim data structures + */ + static void +Python_Lock_Vim(void) +{ +} + +/* + * release a lock on the Vim data structures + */ + static void +Python_Release_Vim(void) +{ +} + +/* Output object definition + */ + +static PyObject *OutputWrite(PyObject *, PyObject *); +static PyObject *OutputWritelines(PyObject *, PyObject *); + +typedef void (*writefn)(char_u *); +static void writer(writefn fn, char_u *str, PyInt n); + +typedef struct +{ + PyObject_HEAD + long softspace; + long error; +} OutputObject; + +static struct PyMethodDef OutputMethods[] = { + /* name, function, calling, documentation */ + {"write", OutputWrite, 1, "" }, + {"writelines", OutputWritelines, 1, "" }, + { NULL, NULL, 0, NULL } +}; + +/*************/ + +/* Output buffer management + */ + + static PyObject * +OutputWrite(PyObject *self, PyObject *args) +{ + int len; + char *str; + int error = ((OutputObject *)(self))->error; + + if (!PyArg_ParseTuple(args, "s#", &str, &len)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + Python_Lock_Vim(); + writer((writefn)(error ? emsg : msg), (char_u *)str, len); + Python_Release_Vim(); + Py_END_ALLOW_THREADS + + Py_INCREF(Py_None); + return Py_None; +} + + static PyObject * +OutputWritelines(PyObject *self, PyObject *args) +{ + PyInt n; + PyInt i; + PyObject *list; + int error = ((OutputObject *)(self))->error; + + if (!PyArg_ParseTuple(args, "O", &list)) + return NULL; + Py_INCREF(list); + + if (!PyList_Check(list)) { + PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); + Py_DECREF(list); + return NULL; + } + + n = PyList_Size(list); + + for (i = 0; i < n; ++i) + { + PyObject *line = PyList_GetItem(list, i); + char *str; + PyInt len; + + if (!PyArg_Parse(line, "s#", &str, &len)) { + PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); + Py_DECREF(list); + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + Python_Lock_Vim(); + writer((writefn)(error ? emsg : msg), (char_u *)str, len); + Python_Release_Vim(); + Py_END_ALLOW_THREADS + } + + Py_DECREF(list); + Py_INCREF(Py_None); + return Py_None; +} + +static char_u *buffer = NULL; +static PyInt buffer_len = 0; +static PyInt buffer_size = 0; + +static writefn old_fn = NULL; + + static void +buffer_ensure(PyInt n) +{ + PyInt new_size; + char_u *new_buffer; + + if (n < buffer_size) + return; + + new_size = buffer_size; + while (new_size < n) + new_size += 80; + + if (new_size != buffer_size) + { + new_buffer = alloc((unsigned)new_size); + if (new_buffer == NULL) + return; + + if (buffer) + { + memcpy(new_buffer, buffer, buffer_len); + vim_free(buffer); + } + + buffer = new_buffer; + buffer_size = new_size; + } +} + + static void +PythonIO_Flush(void) +{ + if (old_fn && buffer_len) + { + buffer[buffer_len] = 0; + old_fn(buffer); + } + + buffer_len = 0; +} + + static void +writer(writefn fn, char_u *str, PyInt n) +{ + char_u *ptr; + + if (fn != old_fn && old_fn != NULL) + PythonIO_Flush(); + + old_fn = fn; + + while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) + { + PyInt len = ptr - str; + + buffer_ensure(buffer_len + len + 1); + + memcpy(buffer + buffer_len, str, len); + buffer_len += len; + buffer[buffer_len] = 0; + fn(buffer); + str = ptr + 1; + n -= len + 1; + buffer_len = 0; + } + + /* Put the remaining text into the buffer for later printing */ + buffer_ensure(buffer_len + n + 1); + memcpy(buffer + buffer_len, str, n); + buffer_len += n; +} + +/***************/ + +static PyTypeObject OutputType; + +static OutputObject Output = +{ + PyObject_HEAD_INIT(&OutputType) + 0, + 0 +}; + +static OutputObject Error = +{ + PyObject_HEAD_INIT(&OutputType) + 0, + 1 +}; + + static int +PythonIO_Init_io(void) +{ + PySys_SetObject("stdout", (PyObject *)(void *)&Output); + PySys_SetObject("stderr", (PyObject *)(void *)&Error); + + if (PyErr_Occurred()) + { + EMSG(_("E264: Python: Error initialising I/O objects")); + return -1; + } + + return 0; +} + + +static PyObject *VimError; + +/* Check to see whether a Vim error has been reported, or a keyboard + * interrupt has been detected. + */ + static int +VimErrorCheck(void) +{ + if (got_int) + { + PyErr_SetNone(PyExc_KeyboardInterrupt); + return 1; + } + else if (did_emsg && !PyErr_Occurred()) + { + PyErr_SetNone(VimError); + return 1; + } + + return 0; +} diff --git a/src/if_python.c b/src/if_python.c index 95be357245..70fd0b8e51 100644 --- a/src/if_python.c +++ b/src/if_python.c @@ -54,6 +54,8 @@ #undef main /* Defined in python.h - aargh */ #undef HAVE_FCNTL_H /* Clash with os_win32.h */ +static void init_structs(void); + #if !defined(FEAT_PYTHON) && defined(PROTO) /* Use this to be able to generate prototypes without python being used. */ # define PyObject Py_ssize_t @@ -412,6 +414,12 @@ get_exceptions() } #endif /* DYNAMIC_PYTHON */ +/* + * Include the code shared with if_python3.c + */ +#include "if_py_both.h" + + /****************************************************** * Internal function prototypes. */ @@ -437,8 +445,6 @@ static int InsertBufferLines(buf_T *, PyInt, PyObject *, PyInt *); static PyObject *LineToString(const char *); static char *StringToLine(PyObject *); -static int VimErrorCheck(void); - #define PyErr_SetVim(str) PyErr_SetString(VimError, str) /****************************************************** @@ -486,20 +492,6 @@ Python_RestoreThread(void) #endif } -/* - * obtain a lock on the Vim data structures - */ -static void Python_Lock_Vim(void) -{ -} - -/* - * release a lock on the Vim data structures - */ -static void Python_Release_Vim(void) -{ -} - void python_end() { @@ -550,6 +542,8 @@ Python_Init(void) } #endif + init_structs(); + #if !defined(MACOS) || defined(MACOS_X_UNIX) Py_Initialize(); #else @@ -743,52 +737,6 @@ ex_pyfile(exarg_T *eap) static PyObject *OutputGetattr(PyObject *, char *); static int OutputSetattr(PyObject *, char *, PyObject *); -static PyObject *OutputWrite(PyObject *, PyObject *); -static PyObject *OutputWritelines(PyObject *, PyObject *); - -typedef void (*writefn)(char_u *); -static void writer(writefn fn, char_u *str, PyInt n); - -/* Output object definition - */ - -typedef struct -{ - PyObject_HEAD - long softspace; - long error; -} OutputObject; - -static struct PyMethodDef OutputMethods[] = { - /* name, function, calling, documentation */ - {"write", OutputWrite, 1, "" }, - {"writelines", OutputWritelines, 1, "" }, - { NULL, NULL, 0, NULL } -}; - -static PyTypeObject OutputType = { - PyObject_HEAD_INIT(0) - 0, - "message", - sizeof(OutputObject), - 0, - - (destructor) 0, - (printfunc) 0, - (getattrfunc) OutputGetattr, - (setattrfunc) OutputSetattr, - (cmpfunc) 0, - (reprfunc) 0, - - 0, /* as number */ - 0, /* as sequence */ - 0, /* as mapping */ - - (hashfunc) 0, - (ternaryfunc) 0, - (reprfunc) 0 -}; - /*************/ static PyObject * @@ -823,186 +771,15 @@ OutputSetattr(PyObject *self, char *name, PyObject *val) return -1; } -/*************/ - - static PyObject * -OutputWrite(PyObject *self, PyObject *args) -{ - int len; - char *str; - int error = ((OutputObject *)(self))->error; - - if (!PyArg_ParseTuple(args, "s#", &str, &len)) - return NULL; - - Py_BEGIN_ALLOW_THREADS - Python_Lock_Vim(); - writer((writefn)(error ? emsg : msg), (char_u *)str, len); - Python_Release_Vim(); - Py_END_ALLOW_THREADS - - Py_INCREF(Py_None); - return Py_None; -} - - static PyObject * -OutputWritelines(PyObject *self, PyObject *args) -{ - PyInt n; - PyInt i; - PyObject *list; - int error = ((OutputObject *)(self))->error; - - if (!PyArg_ParseTuple(args, "O", &list)) - return NULL; - Py_INCREF(list); - - if (!PyList_Check(list)) { - PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); - Py_DECREF(list); - return NULL; - } - - n = PyList_Size(list); - - for (i = 0; i < n; ++i) - { - PyObject *line = PyList_GetItem(list, i); - char *str; - PyInt len; - - if (!PyArg_Parse(line, "s#", &str, &len)) { - PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); - Py_DECREF(list); - return NULL; - } - - Py_BEGIN_ALLOW_THREADS - Python_Lock_Vim(); - writer((writefn)(error ? emsg : msg), (char_u *)str, len); - Python_Release_Vim(); - Py_END_ALLOW_THREADS - } - - Py_DECREF(list); - Py_INCREF(Py_None); - return Py_None; -} - -/* Output buffer management - */ - -static char_u *buffer = NULL; -static PyInt buffer_len = 0; -static PyInt buffer_size = 0; - -static writefn old_fn = NULL; - - static void -buffer_ensure(PyInt n) -{ - PyInt new_size; - char_u *new_buffer; - - if (n < buffer_size) - return; - - new_size = buffer_size; - while (new_size < n) - new_size += 80; - - if (new_size != buffer_size) - { - new_buffer = alloc((unsigned)new_size); - if (new_buffer == NULL) - return; - - if (buffer) - { - memcpy(new_buffer, buffer, buffer_len); - vim_free(buffer); - } - - buffer = new_buffer; - buffer_size = new_size; - } -} - - static void -PythonIO_Flush(void) -{ - if (old_fn && buffer_len) - { - buffer[buffer_len] = 0; - old_fn(buffer); - } - - buffer_len = 0; -} - - static void -writer(writefn fn, char_u *str, PyInt n) -{ - char_u *ptr; - - if (fn != old_fn && old_fn != NULL) - PythonIO_Flush(); - - old_fn = fn; - - while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) - { - PyInt len = ptr - str; - - buffer_ensure(buffer_len + len + 1); - - memcpy(buffer + buffer_len, str, len); - buffer_len += len; - buffer[buffer_len] = 0; - fn(buffer); - str = ptr + 1; - n -= len + 1; - buffer_len = 0; - } - - /* Put the remaining text into the buffer for later printing */ - buffer_ensure(buffer_len + n + 1); - memcpy(buffer + buffer_len, str, n); - buffer_len += n; -} - /***************/ -static OutputObject Output = -{ - PyObject_HEAD_INIT(&OutputType) - 0, - 0 -}; - -static OutputObject Error = -{ - PyObject_HEAD_INIT(&OutputType) - 0, - 1 -}; - static int PythonIO_Init(void) { /* Fixups... */ OutputType.ob_type = &PyType_Type; - PySys_SetObject("stdout", (PyObject *)(void *)&Output); - PySys_SetObject("stderr", (PyObject *)(void *)&Error); - - if (PyErr_Occurred()) - { - EMSG(_("E264: Python: Error initialising I/O objects")); - return -1; - } - - return 0; + return PythonIO_Init_io(); } /****************************************************** @@ -1013,8 +790,6 @@ PythonIO_Init(void) * ------------------------------------- */ -static PyObject *VimError; - static PyObject *VimCommand(PyObject *, PyObject *); static PyObject *VimEval(PyObject *, PyObject *); @@ -1326,6 +1101,7 @@ VimEval(PyObject *self UNUSED, PyObject *args) /* Common routines for buffers and line ranges * ------------------------------------------- */ + static int CheckBuffer(BufferObject *this) { @@ -3018,26 +2794,6 @@ StringToLine(PyObject *obj) return save; } -/* Check to see whether a Vim error has been reported, or a keyboard - * interrupt has been detected. - */ - static int -VimErrorCheck(void) -{ - if (got_int) - { - PyErr_SetNone(PyExc_KeyboardInterrupt); - return 1; - } - else if (did_emsg && !PyErr_Occurred()) - { - PyErr_SetNone(VimError); - return 1; - } - - return 0; -} - /* Don't generate a prototype for the next function, it generates an error on * newer Python versions. */ @@ -3049,3 +2805,13 @@ Py_GetProgramName(void) return "vim"; } #endif /* Python 1.4 */ + + static void +init_structs(void) +{ + vim_memset(&OutputType, 0, sizeof(OutputType)); + OutputType.tp_name = "message"; + OutputType.tp_basicsize = sizeof(OutputObject); + OutputType.tp_getattr = OutputGetattr; + OutputType.tp_setattr = OutputSetattr; +} diff --git a/src/if_python3.c b/src/if_python3.c index 8958e0ae29..bb0763ab0e 100644 --- a/src/if_python3.c +++ b/src/if_python3.c @@ -68,6 +68,8 @@ static void init_structs(void); +#define PyInt Py_ssize_t + #if defined(DYNAMIC_PYTHON3) #ifndef WIN3264 @@ -304,7 +306,8 @@ static struct /* * Free python.dll */ -static void end_dynamic_python3(void) + static void +end_dynamic_python3(void) { if (hinstPy3 != 0) { @@ -318,7 +321,8 @@ static void end_dynamic_python3(void) * Parameter 'libname' provides name of DLL. * Return OK or FAIL. */ -static int py3_runtime_link_init(char *libname, int verbose) + static int +py3_runtime_link_init(char *libname, int verbose) { int i; void *ucs_from_string, *ucs_from_string_and_size; @@ -390,7 +394,8 @@ static int py3_runtime_link_init(char *libname, int verbose) * If python is enabled (there is installed python on Windows system) return * TRUE, else FALSE. */ -int python3_enabled(int verbose) + int +python3_enabled(int verbose) { return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; } @@ -400,7 +405,8 @@ int python3_enabled(int verbose) */ static void get_py3_exceptions __ARGS((void)); -static void get_py3_exceptions() + static void +get_py3_exceptions() { PyObject *exmod = PyImport_ImportModule("builtins"); PyObject *exdict = PyModule_GetDict(exmod); @@ -418,7 +424,13 @@ static void get_py3_exceptions() } #endif /* DYNAMIC_PYTHON3 */ -static void call_PyObject_Free(void *p) +/* + * Include the code shared with if_python.c + */ +#include "if_py_both.h" + + static void +call_PyObject_Free(void *p) { #ifdef Py_DEBUG _PyObject_DebugFree(p); @@ -426,11 +438,15 @@ static void call_PyObject_Free(void *p) PyObject_Free(p); #endif } -static PyObject* call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) + + static PyObject * +call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) { return PyType_GenericNew(type,args,kwds); } -static PyObject* call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) + + static PyObject * +call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) { return PyType_GenericAlloc(type,nitems); } @@ -460,8 +476,6 @@ static PyObject *GetBufferLineList(buf_T *buf, Py_ssize_t lo, Py_ssize_t hi); static PyObject *LineToString(const char *); static char *StringToLine(PyObject *); -static int VimErrorCheck(void); - #define PyErr_SetVim(str) PyErr_SetString(VimError, str) /****************************************************** @@ -473,21 +487,8 @@ static int py3initialised = 0; static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; -/* - * obtain a lock on the Vim data structures - */ -static void Python_Lock_Vim(void) -{ -} - -/* - * release a lock on the Vim data structures - */ -static void Python_Release_Vim(void) -{ -} - -void python3_end() + void +python3_end() { static int recurse = 0; @@ -524,7 +525,8 @@ python3_loaded() } #endif -static int Python3_Init(void) + static int +Python3_Init(void) { if (!py3initialised) { @@ -588,7 +590,8 @@ fail: /* * External interface */ -static void DoPy3Command(exarg_T *eap, const char *cmd) + static void +DoPy3Command(exarg_T *eap, const char *cmd) { #if defined(MACOS) && !defined(MACOS_X_UNIX) GrafPtr oldPort; @@ -650,7 +653,8 @@ theend: /* * ":py3" */ -void ex_py3(exarg_T *eap) + void +ex_py3(exarg_T *eap) { char_u *script; @@ -731,34 +735,10 @@ ex_py3file(exarg_T *eap) static PyObject *OutputGetattro(PyObject *, PyObject *); static int OutputSetattro(PyObject *, PyObject *, PyObject *); -static PyObject *OutputWrite(PyObject *, PyObject *); -static PyObject *OutputWritelines(PyObject *, PyObject *); - -typedef void (*writefn)(char_u *); -static void writer(writefn fn, char_u *str, Py_ssize_t n); - -/* Output object definition - */ - -typedef struct -{ - PyObject_HEAD - long softspace; - long error; -} OutputObject; - -static struct PyMethodDef OutputMethods[] = { - /* name, function, calling, documentation */ - {"write", OutputWrite, 1, "" }, - {"writelines", OutputWritelines, 1, "" }, - { NULL, NULL, 0, NULL } -}; - -static PyTypeObject OutputType; - /*************/ -static PyObject * OutputGetattro(PyObject *self, PyObject *nameobj) + static PyObject * +OutputGetattro(PyObject *self, PyObject *nameobj) { char *name = ""; if (PyUnicode_Check(nameobj)) @@ -770,7 +750,8 @@ static PyObject * OutputGetattro(PyObject *self, PyObject *nameobj) return PyObject_GenericGetAttr(self, nameobj); } -static int OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) + static int +OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) { char *name = ""; if (PyUnicode_Check(nameobj)) @@ -796,181 +777,17 @@ static int OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) return -1; } -/*************/ - -static PyObject * OutputWrite(PyObject *self, PyObject *args) -{ - int len; - char *str; - int error = ((OutputObject *)(self))->error; - - if (!PyArg_ParseTuple(args, "s#", &str, &len)) - return NULL; - - Py_BEGIN_ALLOW_THREADS - Python_Lock_Vim(); - writer((writefn)(error ? emsg : msg), (char_u *)str, len); - Python_Release_Vim(); - Py_END_ALLOW_THREADS - - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * OutputWritelines(PyObject *self, PyObject *args) -{ - Py_ssize_t n; - Py_ssize_t i; - PyObject *list; - int error = ((OutputObject *)(self))->error; - - if (!PyArg_ParseTuple(args, "O", &list)) - return NULL; - Py_INCREF(list); - - if (!PyList_Check(list)) { - PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); - Py_DECREF(list); - return NULL; - } - - n = PyList_Size(list); - - for (i = 0; i < n; ++i) - { - PyObject *line = PyList_GetItem(list, i); - char *str; - Py_ssize_t len; - - if (!PyArg_Parse(line, "s#", &str, &len)) { - PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); - Py_DECREF(list); - return NULL; - } - - Py_BEGIN_ALLOW_THREADS - Python_Lock_Vim(); - writer((writefn)(error ? emsg : msg), (char_u *)str, len); - Python_Release_Vim(); - Py_END_ALLOW_THREADS - } - - Py_DECREF(list); - Py_INCREF(Py_None); - return Py_None; -} - -/* Output buffer management - */ - -static char_u *buffer = NULL; -static Py_ssize_t buffer_len = 0; -static Py_ssize_t buffer_size = 0; - -static writefn old_fn = NULL; - -static void buffer_ensure(Py_ssize_t n) -{ - Py_ssize_t new_size; - char_u *new_buffer; - - if (n < buffer_size) - return; - - new_size = buffer_size; - while (new_size < n) - new_size += 80; - - if (new_size != buffer_size) - { - new_buffer = alloc((unsigned)new_size); - if (new_buffer == NULL) - return; - - if (buffer) - { - memcpy(new_buffer, buffer, buffer_len); - vim_free(buffer); - } - - buffer = new_buffer; - buffer_size = new_size; - } -} - -static void PythonIO_Flush(void) -{ - if (old_fn && buffer_len) - { - buffer[buffer_len] = 0; - old_fn(buffer); - } - - buffer_len = 0; -} - -static void writer(writefn fn, char_u *str, Py_ssize_t n) -{ - char_u *ptr; - - if (fn != old_fn && old_fn != NULL) - PythonIO_Flush(); - - old_fn = fn; - - while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) - { - Py_ssize_t len = ptr - str; - - buffer_ensure(buffer_len + len + 1); - - memcpy(buffer + buffer_len, str, len); - buffer_len += len; - buffer[buffer_len] = 0; - fn(buffer); - str = ptr + 1; - n -= len + 1; - buffer_len = 0; - } - - /* Put the remaining text into the buffer for later printing */ - buffer_ensure(buffer_len + n + 1); - memcpy(buffer + buffer_len, str, n); - buffer_len += n; -} - /***************/ -static OutputObject Output = -{ - PyObject_HEAD_INIT(&OutputType) - 0, - 0 -}; - -static OutputObject Error = -{ - PyObject_HEAD_INIT(&OutputType) - 0, - 1 -}; - -static int PythonIO_Init(void) + static int +PythonIO_Init(void) { PyType_Ready(&OutputType); - - PySys_SetObject("stdout", (PyObject *)(void *)&Output); - PySys_SetObject("stderr", (PyObject *)(void *)&Error); - - if (PyErr_Occurred()) - { - EMSG(_("E264: Python: Error initialising I/O objects")); - return -1; - } - - return 0; + return PythonIO_Init_io(); } -static void PythonIO_Fini(void) + + static void +PythonIO_Fini(void) { PySys_SetObject("stdout", NULL); PySys_SetObject("stderr", NULL); @@ -984,8 +801,6 @@ static void PythonIO_Fini(void) * ------------------------------------- */ -static PyObject *VimError; - static PyObject *VimCommand(PyObject *, PyObject *); static PyObject *VimEval(PyObject *, PyObject *); @@ -1096,15 +911,15 @@ static int CurrentSetattro(PyObject *, PyObject *, PyObject *); static struct PyMethodDef VimMethods[] = { /* name, function, calling, documentation */ - {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, + {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, { NULL, NULL, 0, NULL } }; /* Vim module - Implementation */ -/*ARGSUSED*/ -static PyObject * VimCommand(PyObject *self UNUSED, PyObject *args) + static PyObject * +VimCommand(PyObject *self UNUSED, PyObject *args) { char *cmd; PyObject *result; @@ -1140,10 +955,11 @@ static PyObject * VimCommand(PyObject *self UNUSED, PyObject *args) * The depth parameter is to avoid infinite recursion, set it to 1 when * you call VimToPython. */ -static PyObject * VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) + static PyObject * +VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) { - PyObject *result; - PyObject *newObj; + PyObject *result; + PyObject *newObj; char ptrBuf[NUMBUFLEN]; /* Avoid infinite recursion */ @@ -1216,18 +1032,18 @@ static PyObject * VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) if (our_tv->vval.v_dict != NULL) { - hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; - long_u t = ht->ht_used; - hashitem_T *hi; - dictitem_T *di; + hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; + long_u todo = ht->ht_used; + hashitem_T *hi; + dictitem_T *di; PyDict_SetItemString(lookupDict, ptrBuf, result); - for (hi = ht->ht_array; t > 0; ++hi) + for (hi = ht->ht_array; todo > 0; ++hi) { if (!HASHITEM_EMPTY(hi)) { - --t; + --todo; di = dict_lookup(hi); newObj = VimToPython(&di->di_tv, depth + 1, lookupDict); @@ -1247,13 +1063,13 @@ static PyObject * VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) } #endif -/*ARGSUSED*/ -static PyObject * VimEval(PyObject *self UNUSED, PyObject *args) + static PyObject * +VimEval(PyObject *self UNUSED, PyObject *args) { #ifdef FEAT_EVAL char *expr; - typval_T *our_tv; - PyObject *result; + typval_T *our_tv; + PyObject *result; PyObject *lookup_dict; if (!PyArg_ParseTuple(args, "s", &expr)) @@ -1296,7 +1112,8 @@ static PyObject * VimEval(PyObject *self UNUSED, PyObject *args) * ------------------------------------------- */ -static int CheckBuffer(BufferObject *this) + static int +CheckBuffer(BufferObject *this) { if (this->buf == INVALID_BUFFER_VALUE) { @@ -1307,7 +1124,8 @@ static int CheckBuffer(BufferObject *this) return 0; } -static PyObject * RBItem(BufferObject *self, Py_ssize_t n, Py_ssize_t start, Py_ssize_t end) + static PyObject * +RBItem(BufferObject *self, Py_ssize_t n, Py_ssize_t start, Py_ssize_t end) { if (CheckBuffer(self)) return NULL; @@ -1321,29 +1139,8 @@ static PyObject * RBItem(BufferObject *self, Py_ssize_t n, Py_ssize_t start, Py_ return GetBufferLine(self->buf, n+start); } -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) -{ - Py_ssize_t len_change; - - if (CheckBuffer(self)) - return -1; - - if (n < 0 || n > end - start) - { - PyErr_SetString(PyExc_IndexError, _("line number out of range")); - return -1; - } - - if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) - return -1; - - if (new_end) - *new_end = end + len_change; - - return 0; -} - -static PyObject * RBSlice(BufferObject *self, Py_ssize_t lo, Py_ssize_t hi, Py_ssize_t start, Py_ssize_t end) + static PyObject * +RBSlice(BufferObject *self, Py_ssize_t lo, Py_ssize_t hi, Py_ssize_t start, Py_ssize_t end) { Py_ssize_t size; @@ -1366,7 +1163,31 @@ static PyObject * RBSlice(BufferObject *self, Py_ssize_t lo, Py_ssize_t hi, Py_s return GetBufferLineList(self->buf, lo+start, hi+start); } -static PyObject * RBAppend(BufferObject *self, PyObject *args, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) + 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) +{ + Py_ssize_t len_change; + + if (CheckBuffer(self)) + return -1; + + if (n < 0 || n > end - start) + { + PyErr_SetString(PyExc_IndexError, _("line number out of range")); + return -1; + } + + if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) + return -1; + + if (new_end) + *new_end = end + len_change; + + return 0; +} + + static PyObject * +RBAppend(BufferObject *self, PyObject *args, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) { PyObject *lines; Py_ssize_t len_change; @@ -1398,6 +1219,9 @@ static PyObject * RBAppend(BufferObject *self, PyObject *args, Py_ssize_t start, } +/* Buffer object - Definitions + */ + static struct PyMethodDef BufferMethods[] = { /* name, function, calling, documentation */ {"append", BufferAppend, 1, "Append data to Vim buffer" }, @@ -1431,7 +1255,8 @@ PyMappingMethods BufferAsMapping = { static PyTypeObject BufferType; -static PyObject * BufferNew(buf_T *buf) + static PyObject * +BufferNew(buf_T *buf) { /* We need to handle deletion of buffers underneath us. * If we add a "b_python3_ref" field to the buf_T structure, @@ -1466,7 +1291,8 @@ static PyObject * BufferNew(buf_T *buf) return (PyObject *)(self); } -static void BufferDestructor(PyObject *self) + static void +BufferDestructor(PyObject *self) { BufferObject *this = (BufferObject *)(self); @@ -1474,7 +1300,8 @@ static void BufferDestructor(PyObject *self) this->buf->b_python3_ref = NULL; } -static PyObject * BufferGetattro(PyObject *self, PyObject*nameobj) + static PyObject * +BufferGetattro(PyObject *self, PyObject*nameobj) { BufferObject *this = (BufferObject *)(self); @@ -1495,7 +1322,8 @@ static PyObject * BufferGetattro(PyObject *self, PyObject*nameobj) return PyObject_GenericGetAttr(self, nameobj); } -static PyObject * BufferRepr(PyObject *self) + static PyObject * +BufferRepr(PyObject *self) { static char repr[100]; BufferObject *this = (BufferObject *)(self); @@ -1525,7 +1353,8 @@ static PyObject * BufferRepr(PyObject *self) /******************/ -static Py |