Mercurial > vim
annotate src/if_py_both.h @ 4599:89bec74fd793 v7.3.1047
updated for version 7.3.1047
Problem: Python: dir() does not work properly.
Solution: Python patch 8. Add __dir__ method to all objects with custom
tp_getattr supplemented by __members__ attribute for at least
python-2* versions. __members__ is not mentioned in python-3*
dir() output even if it is accessible. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 29 May 2013 22:36:10 +0200 |
parents | 00eecb2f8e3e |
children | 19d406a8509d |
rev | line source |
---|---|
3618 | 1 /* vi:set ts=8 sts=4 sw=4 noet: |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2 * |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
3 * VIM - Vi IMproved by Bram Moolenaar |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
4 * |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
5 * Do ":help uganda" in Vim to read copying and usage conditions. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
6 * Do ":help credits" in Vim to see a list of people who contributed. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
7 * See README.txt for an overview of the Vim source code. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
8 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
9 /* |
4385 | 10 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay |
11 * Pavlov. | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
12 * |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
13 * Common code for if_python.c and if_python3.c. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
14 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
15 |
3734 | 16 #if PY_VERSION_HEX < 0x02050000 |
17 typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ | |
18 #endif | |
19 | |
2919 | 20 #ifdef FEAT_MBYTE |
21 # define ENC_OPT p_enc | |
22 #else | |
23 # define ENC_OPT "latin1" | |
24 #endif | |
4435 | 25 #define DOPY_FUNC "_vim_pydo" |
2919 | 26 |
4385 | 27 #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
28 | |
29 #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) | |
30 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) | |
4401 | 31 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) |
4385 | 32 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
33 #define DICTKEY_DECL \ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
34 PyObject *dictkey_todecref; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
35 #define DICTKEY_GET(err) \ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
36 if (!(key = StringToChars(keyObject, &dictkey_todecref))) \ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
37 return err; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
38 #define DICTKEY_UNREF \ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
39 Py_XDECREF(dictkey_todecref); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
40 |
4486 | 41 typedef void (*rangeinitializer)(void *); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
42 typedef void (*runner)(const char *, void * |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
43 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
44 , PyGILState_STATE * |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
45 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
46 ); |
4486 | 47 |
4385 | 48 static int ConvertFromPyObject(PyObject *, typval_T *); |
49 static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); | |
4431 | 50 static PyObject *WindowNew(win_T *, tabpage_T *); |
51 static PyObject *BufferNew (buf_T *); | |
52 static PyObject *LineToString(const char *); | |
4385 | 53 |
54 static PyInt RangeStart; | |
55 static PyInt RangeEnd; | |
56 | |
4486 | 57 static PyObject *globals; |
58 | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
59 /* |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
60 * obtain a lock on the Vim data structures |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
61 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
62 static void |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
63 Python_Lock_Vim(void) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
64 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
65 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
66 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
67 /* |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
68 * release a lock on the Vim data structures |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
69 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
70 static void |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
71 Python_Release_Vim(void) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
72 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
73 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
74 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
75 /* |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
76 * The "todecref" argument holds a pointer to PyObject * that must be |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
77 * DECREF'ed after returned char_u * is no longer needed or NULL if all what |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
78 * was needed to generate returned value is object. |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
79 * |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
80 * Use Py_XDECREF to decrement reference count. |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
81 */ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
82 static char_u * |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
83 StringToChars(PyObject *object, PyObject **todecref) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
84 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
85 char_u *p; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
86 PyObject *bytes = NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
87 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
88 if (PyBytes_Check(object)) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
89 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
90 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
91 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
92 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
93 if (p == NULL) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
94 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
95 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
96 *todecref = NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
97 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
98 else if (PyUnicode_Check(object)) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
99 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
100 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
101 if (bytes == NULL) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
102 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
103 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
104 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
105 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
106 if (p == NULL) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
107 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
108 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
109 *todecref = bytes; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
110 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
111 else |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
112 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
113 PyErr_SetString(PyExc_TypeError, _("object must be string")); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
114 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
115 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
116 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
117 return (char_u *) p; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
118 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
119 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
120 static int |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
121 add_string(PyObject *list, char *s) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
122 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
123 PyObject *string; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
124 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
125 if (!(string = PyString_FromString(s))) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
126 return -1; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
127 if (PyList_Append(list, string)) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
128 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
129 Py_DECREF(string); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
130 return -1; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
131 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
132 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
133 Py_DECREF(string); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
134 return 0; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
135 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
136 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
137 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
138 ObjectDir(PyObject *self, char **attributes) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
139 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
140 PyMethodDef *method; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
141 char **attr; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
142 PyObject *r; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
143 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
144 if (!(r = PyList_New(0))) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
145 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
146 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
147 if (self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
148 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
149 if (add_string(r, (char *) method->ml_name)) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
150 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
151 Py_DECREF(r); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
152 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
153 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
154 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
155 for (attr = attributes ; *attr ; ++attr) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
156 if (add_string(r, *attr)) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
157 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
158 Py_DECREF(r); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
159 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
160 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
161 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
162 #if PY_MAJOR_VERSION < 3 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
163 if (add_string(r, "__members__")) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
164 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
165 Py_DECREF(r); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
166 return NULL; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
167 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
168 #endif |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
169 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
170 return r; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
171 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
172 |
4385 | 173 /* Output buffer management |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
174 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
175 |
2582 | 176 /* Function to write a line, points to either msg() or emsg(). */ |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
177 typedef void (*writefn)(char_u *); |
4385 | 178 |
179 static PyTypeObject OutputType; | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
180 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
181 typedef struct |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
182 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
183 PyObject_HEAD |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
184 long softspace; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
185 long error; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
186 } OutputObject; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
187 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
188 static char *OutputAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
189 "softspace", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
190 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
191 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
192 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
193 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
194 OutputDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
195 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
196 return ObjectDir(self, OutputAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
197 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
198 |
3826 | 199 static int |
4488 | 200 OutputSetattr(OutputObject *self, char *name, PyObject *val) |
3826 | 201 { |
202 if (val == NULL) | |
203 { | |
4405 | 204 PyErr_SetString(PyExc_AttributeError, |
205 _("can't delete OutputObject attributes")); | |
3826 | 206 return -1; |
207 } | |
208 | |
209 if (strcmp(name, "softspace") == 0) | |
210 { | |
211 if (!PyInt_Check(val)) | |
212 { | |
213 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); | |
214 return -1; | |
215 } | |
216 | |
4488 | 217 self->softspace = PyInt_AsLong(val); |
3826 | 218 return 0; |
219 } | |
220 | |
221 PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); | |
222 return -1; | |
223 } | |
224 | |
4385 | 225 /* Buffer IO, we write one whole line at a time. */ |
226 static garray_T io_ga = {0, 0, 1, 80, NULL}; | |
227 static writefn old_fn = NULL; | |
228 | |
229 static void | |
230 PythonIO_Flush(void) | |
231 { | |
232 if (old_fn != NULL && io_ga.ga_len > 0) | |
233 { | |
234 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL; | |
235 old_fn((char_u *)io_ga.ga_data); | |
236 } | |
237 io_ga.ga_len = 0; | |
238 } | |
239 | |
240 static void | |
241 writer(writefn fn, char_u *str, PyInt n) | |
242 { | |
243 char_u *ptr; | |
244 | |
245 /* Flush when switching output function. */ | |
246 if (fn != old_fn) | |
247 PythonIO_Flush(); | |
248 old_fn = fn; | |
249 | |
250 /* Write each NL separated line. Text after the last NL is kept for | |
251 * writing later. */ | |
252 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) | |
253 { | |
254 PyInt len = ptr - str; | |
255 | |
256 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL) | |
257 break; | |
258 | |
259 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len); | |
260 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL; | |
261 fn((char_u *)io_ga.ga_data); | |
262 str = ptr + 1; | |
263 n -= len + 1; | |
264 io_ga.ga_len = 0; | |
265 } | |
266 | |
267 /* Put the remaining text into io_ga for later printing. */ | |
268 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK) | |
269 { | |
270 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n); | |
271 io_ga.ga_len += (int)n; | |
272 } | |
273 } | |
274 | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
275 static PyObject * |
4488 | 276 OutputWrite(OutputObject *self, PyObject *args) |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
277 { |
3806 | 278 Py_ssize_t len = 0; |
2894 | 279 char *str = NULL; |
4488 | 280 int error = self->error; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
281 |
3056 | 282 if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len)) |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
283 return NULL; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
284 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
285 Py_BEGIN_ALLOW_THREADS |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
286 Python_Lock_Vim(); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
287 writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
288 Python_Release_Vim(); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
289 Py_END_ALLOW_THREADS |
2894 | 290 PyMem_Free(str); |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
291 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
292 Py_INCREF(Py_None); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
293 return Py_None; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
294 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
295 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
296 static PyObject * |
4488 | 297 OutputWritelines(OutputObject *self, PyObject *args) |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
298 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
299 PyInt n; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
300 PyInt i; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
301 PyObject *list; |
4488 | 302 int error = self->error; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
303 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
304 if (!PyArg_ParseTuple(args, "O", &list)) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
305 return NULL; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
306 Py_INCREF(list); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
307 |
3618 | 308 if (!PyList_Check(list)) |
309 { | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
310 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
311 Py_DECREF(list); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
312 return NULL; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
313 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
314 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
315 n = PyList_Size(list); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
316 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
317 for (i = 0; i < n; ++i) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
318 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
319 PyObject *line = PyList_GetItem(list, i); |
2894 | 320 char *str = NULL; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
321 PyInt len; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
322 |
3618 | 323 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len)) |
324 { | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
325 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
326 Py_DECREF(list); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
327 return NULL; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
328 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
329 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
330 Py_BEGIN_ALLOW_THREADS |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
331 Python_Lock_Vim(); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
332 writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
333 Python_Release_Vim(); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
334 Py_END_ALLOW_THREADS |
2894 | 335 PyMem_Free(str); |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
336 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
337 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
338 Py_DECREF(list); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
339 Py_INCREF(Py_None); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
340 return Py_None; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
341 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
342 |
2727 | 343 static PyObject * |
4492 | 344 OutputFlush(PyObject *self UNUSED) |
2727 | 345 { |
346 /* do nothing */ | |
347 Py_INCREF(Py_None); | |
348 return Py_None; | |
349 } | |
350 | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
351 /***************/ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
352 |
4385 | 353 static struct PyMethodDef OutputMethods[] = { |
4492 | 354 /* name, function, calling, doc */ |
355 {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""}, | |
356 {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""}, | |
357 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""}, | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
358 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""}, |
4492 | 359 { NULL, NULL, 0, NULL} |
4385 | 360 }; |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
361 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
362 static OutputObject Output = |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
363 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
364 PyObject_HEAD_INIT(&OutputType) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
365 0, |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
366 0 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
367 }; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
368 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
369 static OutputObject Error = |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
370 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
371 PyObject_HEAD_INIT(&OutputType) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
372 0, |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
373 1 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
374 }; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
375 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
376 static int |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
377 PythonIO_Init_io(void) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
378 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
379 PySys_SetObject("stdout", (PyObject *)(void *)&Output); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
380 PySys_SetObject("stderr", (PyObject *)(void *)&Error); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
381 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
382 if (PyErr_Occurred()) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
383 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
384 EMSG(_("E264: Python: Error initialising I/O objects")); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
385 return -1; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
386 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
387 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
388 return 0; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
389 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
390 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
391 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
392 static PyObject *VimError; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
393 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
394 /* Check to see whether a Vim error has been reported, or a keyboard |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
395 * interrupt has been detected. |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
396 */ |
4498 | 397 |
398 static void | |
399 VimTryStart(void) | |
400 { | |
401 ++trylevel; | |
402 } | |
403 | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
404 static int |
4498 | 405 VimTryEnd(void) |
406 { | |
407 --trylevel; | |
408 if (got_int) | |
409 { | |
410 PyErr_SetNone(PyExc_KeyboardInterrupt); | |
411 return 1; | |
412 } | |
413 else if (!did_throw) | |
414 return 0; | |
415 else if (PyErr_Occurred()) | |
416 return 1; | |
417 else | |
418 { | |
419 PyErr_SetVim((char *) current_exception->value); | |
420 discard_current_exception(); | |
421 return 1; | |
422 } | |
423 } | |
424 | |
425 static int | |
426 VimCheckInterrupt(void) | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
427 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
428 if (got_int) |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
429 { |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
430 PyErr_SetNone(PyExc_KeyboardInterrupt); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
431 return 1; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
432 } |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
433 return 0; |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
434 } |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
435 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
436 /* Vim module - Implementation |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
437 */ |
4385 | 438 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
439 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
440 VimCommand(PyObject *self UNUSED, PyObject *args) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
441 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
442 char *cmd; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
443 PyObject *result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
444 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
445 if (!PyArg_ParseTuple(args, "s", &cmd)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
446 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
447 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
448 PyErr_Clear(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
449 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
450 Py_BEGIN_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
451 Python_Lock_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
452 |
4498 | 453 VimTryStart(); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
454 do_cmdline_cmd((char_u *)cmd); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
455 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
456 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
457 Python_Release_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
458 Py_END_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
459 |
4498 | 460 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
461 result = NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
462 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
463 result = Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
464 |
4498 | 465 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
466 Py_XINCREF(result); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
467 return result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
468 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
469 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
470 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
471 * Function to translate a typval_T into a PyObject; this will recursively |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
472 * translate lists/dictionaries into their Python equivalents. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
473 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
474 * The depth parameter is to avoid infinite recursion, set it to 1 when |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
475 * you call VimToPython. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
476 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
477 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
478 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
479 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
480 PyObject *result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
481 PyObject *newObj; |
3618 | 482 char ptrBuf[sizeof(void *) * 2 + 3]; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
483 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
484 /* Avoid infinite recursion */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
485 if (depth > 100) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
486 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
487 Py_INCREF(Py_None); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
488 result = Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
489 return result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
490 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
491 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
492 /* Check if we run into a recursive loop. The item must be in lookupDict |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
493 * then and we can use it again. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
494 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
495 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
496 { |
3618 | 497 sprintf(ptrBuf, "%p", |
498 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list | |
499 : (void *)our_tv->vval.v_dict); | |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
500 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
501 if ((result = PyDict_GetItemString(lookupDict, ptrBuf))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
502 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
503 Py_INCREF(result); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
504 return result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
505 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
506 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
507 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
508 if (our_tv->v_type == VAR_STRING) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
509 { |
4597
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
510 result = PyString_FromString(our_tv->vval.v_string == NULL |
3852 | 511 ? "" : (char *)our_tv->vval.v_string); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
512 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
513 else if (our_tv->v_type == VAR_NUMBER) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
514 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
515 char buf[NUMBUFLEN]; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
516 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
517 /* For backwards compatibility numbers are stored as strings. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
518 sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
4597
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
519 result = PyString_FromString((char *) buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
520 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
521 # ifdef FEAT_FLOAT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
522 else if (our_tv->v_type == VAR_FLOAT) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
523 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
524 char buf[NUMBUFLEN]; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
525 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
526 sprintf(buf, "%f", our_tv->vval.v_float); |
4597
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
527 result = PyString_FromString((char *) buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
528 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
529 # endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
530 else if (our_tv->v_type == VAR_LIST) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
531 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
532 list_T *list = our_tv->vval.v_list; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
533 listitem_T *curr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
534 |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
535 if (list == NULL) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
536 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
537 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
538 if (!(result = PyList_New(0))) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
539 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
540 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
541 if (PyDict_SetItemString(lookupDict, ptrBuf, result)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
542 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
543 Py_DECREF(result); |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
544 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
545 } |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
546 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
547 for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
548 { |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
549 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
550 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
551 Py_DECREF(result); |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
552 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
553 } |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
554 if (PyList_Append(result, newObj)) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
555 { |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
556 Py_DECREF(newObj); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
557 Py_DECREF(result); |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
558 return NULL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
559 } |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
560 Py_DECREF(newObj); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
561 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
562 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
563 else if (our_tv->v_type == VAR_DICT) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
564 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
565 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
566 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
567 long_u todo = ht->ht_used; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
568 hashitem_T *hi; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
569 dictitem_T *di; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
570 if (our_tv->vval.v_dict == NULL) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
571 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
572 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
573 if (!(result = PyDict_New())) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
574 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
575 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
576 if (PyDict_SetItemString(lookupDict, ptrBuf, result)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
577 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
578 Py_DECREF(result); |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
579 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
580 } |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
581 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
582 for (hi = ht->ht_array; todo > 0; ++hi) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
583 { |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
584 if (!HASHITEM_EMPTY(hi)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
585 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
586 --todo; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
587 |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
588 di = dict_lookup(hi); |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
589 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookupDict))) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
590 { |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
591 Py_DECREF(result); |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
592 return NULL; |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
593 } |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
594 if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj)) |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
595 { |
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
596 Py_DECREF(result); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
597 Py_DECREF(newObj); |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
598 return NULL; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
599 } |
4595
29b2be2bba0d
updated for version 7.3.1045
Bram Moolenaar <bram@vim.org>
parents:
4593
diff
changeset
|
600 Py_DECREF(newObj); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
601 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
602 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
603 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
604 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
605 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
606 Py_INCREF(Py_None); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
607 result = Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
608 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
609 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
610 return result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
611 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
612 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
613 static PyObject * |
4500 | 614 VimEval(PyObject *self UNUSED, PyObject *args) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
615 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
616 char *expr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
617 typval_T *our_tv; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
618 PyObject *result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
619 PyObject *lookup_dict; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
620 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
621 if (!PyArg_ParseTuple(args, "s", &expr)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
622 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
623 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
624 Py_BEGIN_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
625 Python_Lock_Vim(); |
4498 | 626 VimTryStart(); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
627 our_tv = eval_expr((char_u *)expr, NULL); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
628 Python_Release_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
629 Py_END_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
630 |
4498 | 631 if (VimTryEnd()) |
632 return NULL; | |
633 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
634 if (our_tv == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
635 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
636 PyErr_SetVim(_("invalid expression")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
637 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
638 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
639 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
640 /* Convert the Vim type into a Python type. Create a dictionary that's |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
641 * used to check for recursive loops. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
642 lookup_dict = PyDict_New(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
643 result = VimToPython(our_tv, 1, lookup_dict); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
644 Py_DECREF(lookup_dict); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
645 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
646 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
647 Py_BEGIN_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
648 Python_Lock_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
649 free_tv(our_tv); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
650 Python_Release_Vim(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
651 Py_END_ALLOW_THREADS |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
652 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
653 return result; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
654 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
655 |
3618 | 656 static PyObject *ConvertToPyObject(typval_T *); |
657 | |
658 static PyObject * | |
4438 | 659 VimEvalPy(PyObject *self UNUSED, PyObject *args) |
3618 | 660 { |
661 char *expr; | |
662 typval_T *our_tv; | |
663 PyObject *result; | |
664 | |
665 if (!PyArg_ParseTuple(args, "s", &expr)) | |
666 return NULL; | |
667 | |
668 Py_BEGIN_ALLOW_THREADS | |
669 Python_Lock_Vim(); | |
4498 | 670 VimTryStart(); |
3618 | 671 our_tv = eval_expr((char_u *)expr, NULL); |
672 Python_Release_Vim(); | |
673 Py_END_ALLOW_THREADS | |
674 | |
4498 | 675 if (VimTryEnd()) |
676 return NULL; | |
677 | |
3618 | 678 if (our_tv == NULL) |
679 { | |
680 PyErr_SetVim(_("invalid expression")); | |
681 return NULL; | |
682 } | |
683 | |
684 result = ConvertToPyObject(our_tv); | |
685 Py_BEGIN_ALLOW_THREADS | |
686 Python_Lock_Vim(); | |
687 free_tv(our_tv); | |
688 Python_Release_Vim(); | |
689 Py_END_ALLOW_THREADS | |
690 | |
691 return result; | |
692 } | |
693 | |
694 static PyObject * | |
695 VimStrwidth(PyObject *self UNUSED, PyObject *args) | |
696 { | |
697 char *expr; | |
698 | |
699 if (!PyArg_ParseTuple(args, "s", &expr)) | |
700 return NULL; | |
701 | |
3988 | 702 return PyLong_FromLong( |
703 #ifdef FEAT_MBYTE | |
704 mb_string2cells((char_u *)expr, (int)STRLEN(expr)) | |
705 #else | |
706 STRLEN(expr) | |
707 #endif | |
708 ); | |
3618 | 709 } |
710 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
711 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
712 * Vim module - Definitions |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
713 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
714 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
715 static struct PyMethodDef VimMethods[] = { |
4492 | 716 /* name, function, calling, documentation */ |
717 {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" }, | |
718 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" }, | |
719 {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"}, | |
720 {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"}, | |
721 { NULL, NULL, 0, NULL } | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
722 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
723 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
724 /* |
4397 | 725 * Generic iterator object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
726 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
727 |
4397 | 728 static PyTypeObject IterType; |
729 | |
730 typedef PyObject *(*nextfun)(void **); | |
731 typedef void (*destructorfun)(void *); | |
4433 | 732 typedef int (*traversefun)(void *, visitproc, void *); |
733 typedef int (*clearfun)(void **); | |
4397 | 734 |
4438 | 735 /* Main purpose of this object is removing the need for do python |
736 * initialization (i.e. PyType_Ready and setting type attributes) for a big | |
737 * bunch of objects. */ | |
4385 | 738 |
4319 | 739 typedef struct |
740 { | |
741 PyObject_HEAD | |
4397 | 742 void *cur; |
743 nextfun next; | |
744 destructorfun destruct; | |
4433 | 745 traversefun traverse; |
746 clearfun clear; | |
4397 | 747 } IterObject; |
748 | |
749 static PyObject * | |
4433 | 750 IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse, |
751 clearfun clear) | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
752 { |
4397 | 753 IterObject *self; |
754 | |
4500 | 755 self = PyObject_GC_New(IterObject, &IterType); |
4397 | 756 self->cur = start; |
757 self->next = next; | |
758 self->destruct = destruct; | |
4433 | 759 self->traverse = traverse; |
760 self->clear = clear; | |
4397 | 761 |
762 return (PyObject *)(self); | |
763 } | |
764 | |
765 static void | |
4488 | 766 IterDestructor(IterObject *self) |
4397 | 767 { |
4500 | 768 PyObject_GC_UnTrack((void *)(self)); |
4488 | 769 self->destruct(self->cur); |
4500 | 770 PyObject_GC_Del((void *)(self)); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
771 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
772 |
4433 | 773 static int |
4488 | 774 IterTraverse(IterObject *self, visitproc visit, void *arg) |
4433 | 775 { |
4488 | 776 if (self->traverse != NULL) |
777 return self->traverse(self->cur, visit, arg); | |
4433 | 778 else |
779 return 0; | |
780 } | |
781 | |
4438 | 782 /* Mac OSX defines clear() somewhere. */ |
783 #ifdef clear | |
784 # undef clear | |
785 #endif | |
786 | |
4433 | 787 static int |
4488 | 788 IterClear(IterObject *self) |
4433 | 789 { |
4488 | 790 if (self->clear != NULL) |
791 return self->clear(&self->cur); | |
4433 | 792 else |
793 return 0; | |
794 } | |
795 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
796 static PyObject * |
4488 | 797 IterNext(IterObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
798 { |
4488 | 799 return self->next(&self->cur); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
800 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
801 |
4397 | 802 static PyObject * |
803 IterIter(PyObject *self) | |
804 { | |
805 return self; | |
806 } | |
4393 | 807 |
3618 | 808 typedef struct pylinkedlist_S { |
809 struct pylinkedlist_S *pll_next; | |
810 struct pylinkedlist_S *pll_prev; | |
811 PyObject *pll_obj; | |
812 } pylinkedlist_T; | |
813 | |
814 static pylinkedlist_T *lastdict = NULL; | |
815 static pylinkedlist_T *lastlist = NULL; | |
816 | |
817 static void | |
818 pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last) | |
819 { | |
820 if (ref->pll_prev == NULL) | |
821 { | |
822 if (ref->pll_next == NULL) | |
823 { | |
824 *last = NULL; | |
825 return; | |
826 } | |
827 } | |
828 else | |
829 ref->pll_prev->pll_next = ref->pll_next; | |
830 | |
831 if (ref->pll_next == NULL) | |
832 *last = ref->pll_prev; | |
833 else | |
834 ref->pll_next->pll_prev = ref->pll_prev; | |
835 } | |
836 | |
837 static void | |
838 pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last) | |
839 { | |
840 if (*last == NULL) | |
841 ref->pll_prev = NULL; | |
842 else | |
843 { | |
844 (*last)->pll_next = ref; | |
845 ref->pll_prev = *last; | |
846 } | |
847 ref->pll_next = NULL; | |
848 ref->pll_obj = self; | |
849 *last = ref; | |
850 } | |
851 | |
852 static PyTypeObject DictionaryType; | |
853 | |
3792 | 854 #define DICTKEY_GET_NOTEMPTY(err) \ |
855 DICTKEY_GET(err) \ | |
856 if (*key == NUL) \ | |
857 { \ | |
858 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \ | |
859 return err; \ | |
860 } | |
861 | |
3618 | 862 typedef struct |
863 { | |
864 PyObject_HEAD | |
865 dict_T *dict; | |
866 pylinkedlist_T ref; | |
867 } DictionaryObject; | |
868 | |
869 static PyObject * | |
870 DictionaryNew(dict_T *dict) | |
871 { | |
872 DictionaryObject *self; | |
873 | |
874 self = PyObject_NEW(DictionaryObject, &DictionaryType); | |
875 if (self == NULL) | |
876 return NULL; | |
877 self->dict = dict; | |
878 ++dict->dv_refcount; | |
879 | |
880 pyll_add((PyObject *)(self), &self->ref, &lastdict); | |
881 | |
882 return (PyObject *)(self); | |
883 } | |
884 | |
4319 | 885 static void |
4488 | 886 DictionaryDestructor(DictionaryObject *self) |
4319 | 887 { |
4488 | 888 pyll_remove(&self->ref, &lastdict); |
889 dict_unref(self->dict); | |
4319 | 890 |
891 DESTRUCTOR_FINISH(self); | |
892 } | |
893 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
894 static char *DictionaryAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
895 "locked", "scope", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
896 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
897 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
898 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
899 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
900 DictionaryDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
901 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
902 return ObjectDir(self, DictionaryAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
903 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
904 |
3618 | 905 static int |
4488 | 906 DictionarySetattr(DictionaryObject *self, char *name, PyObject *val) |
3828 | 907 { |
908 if (val == NULL) | |
909 { | |
910 PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); | |
911 return -1; | |
912 } | |
913 | |
914 if (strcmp(name, "locked") == 0) | |
915 { | |
4488 | 916 if (self->dict->dv_lock == VAR_FIXED) |
3828 | 917 { |
918 PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary")); | |
919 return -1; | |
920 } | |
921 else | |
922 { | |
4411 | 923 int istrue = PyObject_IsTrue(val); |
924 if (istrue == -1) | |
925 return -1; | |
926 else if (istrue) | |
4488 | 927 self->dict->dv_lock = VAR_LOCKED; |
3828 | 928 else |
4488 | 929 self->dict->dv_lock = 0; |
3828 | 930 } |
931 return 0; | |
932 } | |
933 else | |
934 { | |
935 PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute")); | |
936 return -1; | |
937 } | |
938 } | |
939 | |
940 static PyInt | |
4488 | 941 DictionaryLength(DictionaryObject *self) |
3618 | 942 { |
4488 | 943 return ((PyInt) (self->dict->dv_hashtab.ht_used)); |
3618 | 944 } |
945 | |
946 static PyObject * | |
4488 | 947 DictionaryItem(DictionaryObject *self, PyObject *keyObject) |
3618 | 948 { |
949 char_u *key; | |
3792 | 950 dictitem_T *di; |
3618 | 951 DICTKEY_DECL |
952 | |
3792 | 953 DICTKEY_GET_NOTEMPTY(NULL) |
954 | |
4488 | 955 di = dict_find(self->dict, key, -1); |
3792 | 956 |
3824 | 957 DICTKEY_UNREF |
958 | |
3792 | 959 if (di == NULL) |
960 { | |
4403 | 961 PyErr_SetObject(PyExc_KeyError, keyObject); |
3792 | 962 return NULL; |
963 } | |
3618 | 964 |
3792 | 965 return ConvertToPyObject(&di->di_tv); |
3618 | 966 } |
967 | |
968 static PyInt | |
4488 | 969 DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject) |
3618 | 970 { |
971 char_u *key; | |
972 typval_T tv; | |
4488 | 973 dict_T *d = self->dict; |
3618 | 974 dictitem_T *di; |
975 DICTKEY_DECL | |
976 | |
977 if (d->dv_lock) | |
978 { | |
979 PyErr_SetVim(_("dict is locked")); | |
980 return -1; | |
981 } | |
982 | |
3792 | 983 DICTKEY_GET_NOTEMPTY(-1) |
3618 | 984 |
985 di = dict_find(d, key, -1); | |
986 | |
987 if (valObject == NULL) | |
988 { | |
3636 | 989 hashitem_T *hi; |
990 | |
3618 | 991 if (di == NULL) |
992 { | |
3824 | 993 DICTKEY_UNREF |
4403 | 994 PyErr_SetObject(PyExc_KeyError, keyObject); |
3618 | 995 return -1; |
996 } | |
3636 | 997 hi = hash_find(&d->dv_hashtab, di->di_key); |
3618 | 998 hash_remove(&d->dv_hashtab, hi); |
999 dictitem_free(di); | |
1000 return 0; | |
1001 } | |
1002 | |
1003 if (ConvertFromPyObject(valObject, &tv) == -1) | |
1004 return -1; | |
1005 | |
1006 if (di == NULL) | |
1007 { | |
1008 di = dictitem_alloc(key); | |
1009 if (di == NULL) | |
1010 { | |
1011 PyErr_NoMemory(); | |
1012 return -1; | |
1013 } | |
1014 di->di_tv.v_lock = 0; | |
1015 | |
1016 if (dict_add(d, di) == FAIL) | |
1017 { | |
3824 | 1018 DICTKEY_UNREF |
3618 | 1019 vim_free(di); |
1020 PyErr_SetVim(_("failed to add key to dictionary")); | |
1021 return -1; | |
1022 } | |
1023 } | |
1024 else | |
1025 clear_tv(&di->di_tv); | |
1026 | |
1027 DICTKEY_UNREF | |
1028 | |
1029 copy_tv(&tv, &di->di_tv); | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1030 clear_tv(&tv); |
3618 | 1031 return 0; |
1032 } | |
1033 | |
1034 static PyObject * | |
4488 | 1035 DictionaryListKeys(DictionaryObject *self) |
3618 | 1036 { |
4488 | 1037 dict_T *dict = self->dict; |
3618 | 1038 long_u todo = dict->dv_hashtab.ht_used; |
1039 Py_ssize_t i = 0; | |
1040 PyObject *r; | |
1041 hashitem_T *hi; | |
1042 | |
1043 r = PyList_New(todo); | |
1044 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) | |
1045 { | |
1046 if (!HASHITEM_EMPTY(hi)) | |
1047 { | |
1048 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key))); | |
1049 --todo; | |
1050 ++i; | |
1051 } | |
1052 } | |
1053 return r; | |
1054 } | |
1055 | |
4385 | 1056 static PyMappingMethods DictionaryAsMapping = { |
1057 (lenfunc) DictionaryLength, | |
1058 (binaryfunc) DictionaryItem, | |
1059 (objobjargproc) DictionaryAssItem, | |
1060 }; | |
1061 | |
3618 | 1062 static struct PyMethodDef DictionaryMethods[] = { |
4492 | 1063 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""}, |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1064 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1065 { NULL, NULL, 0, NULL} |
3618 | 1066 }; |
1067 | |
1068 static PyTypeObject ListType; | |
4319 | 1069 static PySequenceMethods ListAsSeq; |
1070 static PyMappingMethods ListAsMapping; | |
3618 | 1071 |
1072 typedef struct | |
1073 { | |
1074 PyObject_HEAD | |
1075 list_T *list; | |
1076 pylinkedlist_T ref; | |
1077 } ListObject; | |
1078 | |
1079 static PyObject * | |
1080 ListNew(list_T *list) | |
1081 { | |
1082 ListObject *self; | |
1083 | |
1084 self = PyObject_NEW(ListObject, &ListType); | |
1085 if (self == NULL) | |
1086 return NULL; | |
1087 self->list = list; | |
1088 ++list->lv_refcount; | |
1089 | |
1090 pyll_add((PyObject *)(self), &self->ref, &lastlist); | |
1091 | |
1092 return (PyObject *)(self); | |
1093 } | |
1094 | |
4319 | 1095 static void |
4488 | 1096 ListDestructor(ListObject *self) |
4319 | 1097 { |
4488 | 1098 pyll_remove(&self->ref, &lastlist); |
1099 list_unref(self->list); | |
4319 | 1100 |
1101 DESTRUCTOR_FINISH(self); | |
1102 } | |
1103 | |
3618 | 1104 static int |
1105 list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict) | |
1106 { | |
1107 Py_ssize_t i; | |
1108 Py_ssize_t lsize = PySequence_Size(obj); | |
1109 PyObject *litem; | |
1110 listitem_T *li; | |
1111 | |
1112 for(i=0; i<lsize; i++) | |
1113 { | |
1114 li = listitem_alloc(); | |
1115 if (li == NULL) | |
1116 { | |
1117 PyErr_NoMemory(); | |
1118 return -1; | |
1119 } | |
1120 li->li_tv.v_lock = 0; | |
1121 | |
1122 litem = PySequence_GetItem(obj, i); | |
1123 if (litem == NULL) | |
1124 return -1; | |
1125 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1) | |
1126 return -1; | |
1127 | |
1128 list_append(l, li); | |
1129 } | |
1130 return 0; | |
1131 } | |
1132 | |
1133 static PyInt | |
4488 | 1134 ListLength(ListObject *self) |
3618 | 1135 { |
4488 | 1136 return ((PyInt) (self->list->lv_len)); |
3618 | 1137 } |
1138 | |
1139 static PyObject * | |
4488 | 1140 ListItem(ListObject *self, Py_ssize_t index) |
3618 | 1141 { |
1142 listitem_T *li; | |
1143 | |
4488 | 1144 if (index >= ListLength(self)) |
3618 | 1145 { |
4405 | 1146 PyErr_SetString(PyExc_IndexError, _("list index out of range")); |
3618 | 1147 return NULL; |
1148 } | |
4488 | 1149 li = list_find(self->list, (long) index); |
3618 | 1150 if (li == NULL) |
1151 { | |
1152 PyErr_SetVim(_("internal error: failed to get vim list item")); | |
1153 return NULL; | |
1154 } | |
1155 return ConvertToPyObject(&li->li_tv); | |
1156 } | |
1157 | |
1158 #define PROC_RANGE \ | |
1159 if (last < 0) {\ | |
1160 if (last < -size) \ | |
1161 last = 0; \ | |
1162 else \ | |
1163 last += size; \ | |
1164 } \ | |
1165 if (first < 0) \ | |
1166 first = 0; \ | |
1167 if (first > size) \ | |
1168 first = size; \ | |
1169 if (last > size) \ | |
1170 last = size; | |
1171 | |
1172 static PyObject * | |
4488 | 1173 ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last) |
3618 | 1174 { |
1175 PyInt i; | |
1176 PyInt size = ListLength(self); | |
1177 PyInt n; | |
1178 PyObject *list; | |
1179 int reversed = 0; | |
1180 | |
1181 PROC_RANGE | |
1182 if (first >= last) | |
1183 first = last; | |
1184 | |
1185 n = last-first; | |
1186 list = PyList_New(n); | |
1187 if (list == NULL) | |
1188 return NULL; | |
1189 | |
1190 for (i = 0; i < n; ++i) | |
1191 { | |
4260 | 1192 PyObject *item = ListItem(self, first + i); |
3618 | 1193 if (item == NULL) |
1194 { | |
1195 Py_DECREF(list); | |
1196 return NULL; | |
1197 } | |
1198 | |
1199 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item))) | |
1200 { | |
1201 Py_DECREF(item); | |
1202 Py_DECREF(list); | |
1203 return NULL; | |
1204 } | |
1205 } | |
1206 | |
1207 return list; | |
1208 } | |
1209 | |
4397 | 1210 typedef struct |
1211 { | |
1212 listwatch_T lw; | |
1213 list_T *list; | |
1214 } listiterinfo_T; | |
1215 | |
1216 static void | |
1217 ListIterDestruct(listiterinfo_T *lii) | |
1218 { | |
1219 list_rem_watch(lii->list, &lii->lw); | |
1220 PyMem_Free(lii); | |
1221 } | |
1222 | |
1223 static PyObject * | |
1224 ListIterNext(listiterinfo_T **lii) | |
1225 { | |
1226 PyObject *r; | |
1227 | |
1228 if (!((*lii)->lw.lw_item)) | |
1229 return NULL; | |
1230 | |
1231 if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv)))) | |
1232 return NULL; | |
1233 | |
1234 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next; | |
1235 | |
1236 return r; | |
1237 } | |
1238 | |
1239 static PyObject * | |
4488 | 1240 ListIter(ListObject *self) |
4397 | 1241 { |
1242 listiterinfo_T *lii; | |
4488 | 1243 list_T *l = self->list; |
4397 | 1244 |
1245 if (!(lii = PyMem_New(listiterinfo_T, 1))) | |
1246 { | |
1247 PyErr_NoMemory(); | |
1248 return NULL; | |
1249 } | |
1250 | |
1251 list_add_watch(l, &lii->lw); | |
1252 lii->lw.lw_item = l->lv_first; | |
1253 lii->list = l; | |
1254 | |
1255 return IterNew(lii, | |
4433 | 1256 (destructorfun) ListIterDestruct, (nextfun) ListIterNext, |
1257 NULL, NULL); | |
4397 | 1258 } |
1259 | |
3618 | 1260 static int |
4488 | 1261 ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj) |
3618 | 1262 { |
1263 typval_T tv; | |
4488 | 1264 list_T *l = self->list; |
3618 | 1265 listitem_T *li; |
1266 Py_ssize_t length = ListLength(self); | |
1267 | |
1268 if (l->lv_lock) | |
1269 { | |
1270 PyErr_SetVim(_("list is locked")); | |
1271 return -1; | |
1272 } | |
1273 if (index>length || (index==length && obj==NULL)) | |
1274 { | |
4405 | 1275 PyErr_SetString(PyExc_IndexError, _("list index out of range")); |
3618 | 1276 return -1; |
1277 } | |
1278 | |
1279 if (obj == NULL) | |
1280 { | |
1281 li = list_find(l, (long) index); | |
1282 list_remove(l, li, li); | |
1283 clear_tv(&li->li_tv); | |
1284 vim_free(li); | |
1285 return 0; | |
1286 } | |
1287 | |
1288 if (ConvertFromPyObject(obj, &tv) == -1) | |
1289 return -1; | |
1290 | |
1291 if (index == length) | |
1292 { | |
1293 if (list_append_tv(l, &tv) == FAIL) | |
1294 { | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1295 clear_tv(&tv); |
3618 | 1296 PyErr_SetVim(_("Failed to add item to list")); |
1297 return -1; | |
1298 } | |
1299 } | |
1300 else | |
1301 { | |
1302 li = list_find(l, (long) index); | |
1303 clear_tv(&li->li_tv); | |
1304 copy_tv(&tv, &li->li_tv); | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1305 clear_tv(&tv); |
3618 | 1306 } |
1307 return 0; | |
1308 } | |
1309 | |
1310 static int | |
4488 | 1311 ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj) |
3618 | 1312 { |
1313 PyInt size = ListLength(self); | |
1314 Py_ssize_t i; | |
1315 Py_ssize_t lsize; | |
1316 PyObject *litem; | |
1317 listitem_T *li; | |
1318 listitem_T *next; | |
1319 typval_T v; | |
4488 | 1320 list_T *l = self->list; |
3618 | 1321 |
1322 if (l->lv_lock) | |
1323 { | |
1324 PyErr_SetVim(_("list is locked")); | |
1325 return -1; | |
1326 } | |
1327 | |
1328 PROC_RANGE | |
1329 | |
1330 if (first == size) | |
1331 li = NULL; | |
1332 else | |
1333 { | |
1334 li = list_find(l, (long) first); | |
1335 if (li == NULL) | |
1336 { | |
1337 PyErr_SetVim(_("internal error: no vim list item")); | |
1338 return -1; | |
1339 } | |
1340 if (last > first) | |
1341 { | |
1342 i = last - first; | |
1343 while (i-- && li != NULL) | |
1344 { | |
1345 next = li->li_next; | |
1346 listitem_remove(l, li); | |
1347 li = next; | |
1348 } | |
1349 } | |
1350 } | |
1351 | |
1352 if (obj == NULL) | |
1353 return 0; | |
1354 | |
1355 if (!PyList_Check(obj)) | |
1356 { | |
1357 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice")); | |
1358 return -1; | |
1359 } | |
1360 | |
1361 lsize = PyList_Size(obj); | |
1362 | |
1363 for(i=0; i<lsize; i++) | |
1364 { | |
1365 litem = PyList_GetItem(obj, i); | |
1366 if (litem == NULL) | |
1367 return -1; | |
1368 if (ConvertFromPyObject(litem, &v) == -1) | |
1369 return -1; | |
1370 if (list_insert_tv(l, &v, li) == FAIL) | |
1371 { | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1372 clear_tv(&v); |
3618 | 1373 PyErr_SetVim(_("internal error: failed to add item to list")); |
1374 return -1; | |
1375 } | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1376 clear_tv(&v); |
3618 | 1377 } |
1378 return 0; | |
1379 } | |
1380 | |
1381 static PyObject * | |
4488 | 1382 ListConcatInPlace(ListObject *self, PyObject *obj) |
3618 | 1383 { |
4488 | 1384 list_T *l = self->list; |
3618 | 1385 PyObject *lookup_dict; |
1386 | |
1387 if (l->lv_lock) | |
1388 { | |
1389 PyErr_SetVim(_("list is locked")); | |
1390 return NULL; | |
1391 } | |
1392 | |
1393 if (!PySequence_Check(obj)) | |
1394 { | |
1395 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists")); | |
1396 return NULL; | |
1397 } | |
1398 | |
1399 lookup_dict = PyDict_New(); | |
1400 if (list_py_concat(l, obj, lookup_dict) == -1) | |
1401 { | |
1402 Py_DECREF(lookup_dict); | |
1403 return NULL; | |
1404 } | |
1405 Py_DECREF(lookup_dict); | |
1406 | |
1407 Py_INCREF(self); | |
4488 | 1408 return (PyObject *)(self); |
3618 | 1409 } |
1410 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1411 static char *ListAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1412 "locked", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1413 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1414 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1415 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1416 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1417 ListDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1418 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1419 return ObjectDir(self, ListAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1420 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1421 |
3828 | 1422 static int |
4488 | 1423 ListSetattr(ListObject *self, char *name, PyObject *val) |
3828 | 1424 { |
1425 if (val == NULL) | |
1426 { | |
4405 | 1427 PyErr_SetString(PyExc_AttributeError, |
1428 _("cannot delete vim.dictionary attributes")); | |
3828 | 1429 return -1; |
1430 } | |
1431 | |
1432 if (strcmp(name, "locked") == 0) | |
1433 { | |
4488 | 1434 if (self->list->lv_lock == VAR_FIXED) |
3828 | 1435 { |
4405 | 1436 PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list")); |
3828 | 1437 return -1; |
1438 } | |
1439 else | |
1440 { | |
4411 | 1441 int istrue = PyObject_IsTrue(val); |
1442 if (istrue == -1) | |
1443 return -1; | |
1444 else if (istrue) | |
4488 | 1445 self->list->lv_lock = VAR_LOCKED; |
3828 | 1446 else |
4488 | 1447 self->list->lv_lock = 0; |
3828 | 1448 } |
1449 return 0; | |
1450 } | |
1451 else | |
1452 { | |
4405 | 1453 PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute")); |
3828 | 1454 return -1; |
1455 } | |
1456 } | |
1457 | |
3618 | 1458 static struct PyMethodDef ListMethods[] = { |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1459 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1460 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1461 { NULL, NULL, 0, NULL} |
3618 | 1462 }; |
1463 | |
1464 typedef struct | |
1465 { | |
1466 PyObject_HEAD | |
1467 char_u *name; | |
1468 } FunctionObject; | |
1469 | |
1470 static PyTypeObject FunctionType; | |
1471 | |
1472 static PyObject * | |
1473 FunctionNew(char_u *name) | |
1474 { | |
1475 FunctionObject *self; | |
1476 | |
1477 self = PyObject_NEW(FunctionObject, &FunctionType); | |
1478 if (self == NULL) | |
1479 return NULL; | |
1480 self->name = PyMem_New(char_u, STRLEN(name) + 1); | |
1481 if (self->name == NULL) | |
1482 { | |
1483 PyErr_NoMemory(); | |
1484 return NULL; | |
1485 } | |
1486 STRCPY(self->name, name); | |
1487 func_ref(name); | |
1488 return (PyObject *)(self); | |
1489 } | |
1490 | |
4319 | 1491 static void |
4488 | 1492 FunctionDestructor(FunctionObject *self) |
4319 | 1493 { |
4488 | 1494 func_unref(self->name); |
1495 PyMem_Free(self->name); | |
4319 | 1496 |
1497 DESTRUCTOR_FINISH(self); | |
1498 } | |
1499 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1500 static char *FunctionAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1501 "softspace", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1502 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1503 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1504 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1505 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1506 FunctionDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1507 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1508 return ObjectDir(self, FunctionAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1509 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1510 |
3618 | 1511 static PyObject * |
4488 | 1512 FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs) |
3618 | 1513 { |
4488 | 1514 char_u *name = self->name; |
3618 | 1515 typval_T args; |
1516 typval_T selfdicttv; | |
1517 typval_T rettv; | |
1518 dict_T *selfdict = NULL; | |
1519 PyObject *selfdictObject; | |
1520 PyObject *result; | |
1521 int error; | |
1522 | |
1523 if (ConvertFromPyObject(argsObject, &args) == -1) | |
1524 return NULL; | |
1525 | |
1526 if (kwargs != NULL) | |
1527 { | |
1528 selfdictObject = PyDict_GetItemString(kwargs, "self"); | |
1529 if (selfdictObject != NULL) | |
1530 { | |
3703 | 1531 if (!PyMapping_Check(selfdictObject)) |
3618 | 1532 { |
3703 | 1533 PyErr_SetString(PyExc_TypeError, |
1534 _("'self' argument must be a dictionary")); | |
3618 | 1535 clear_tv(&args); |
1536 return NULL; | |
1537 } | |
1538 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1) | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1539 { |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1540 clear_tv(&args); |
3618 | 1541 return NULL; |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1542 } |
3618 | 1543 selfdict = selfdicttv.vval.v_dict; |
1544 } | |
1545 } | |
1546 | |
4415 | 1547 Py_BEGIN_ALLOW_THREADS |
1548 Python_Lock_Vim(); | |
1549 | |
4498 | 1550 VimTryStart(); |
3618 | 1551 error = func_call(name, &args, selfdict, &rettv); |
4415 | 1552 |
1553 Python_Release_Vim(); | |
1554 Py_END_ALLOW_THREADS | |
1555 | |
4498 | 1556 if (VimTryEnd()) |
1557 result = NULL; | |
1558 else if (error != OK) | |
3618 | 1559 { |
1560 result = NULL; | |
1561 PyErr_SetVim(_("failed to run function")); | |
1562 } | |
1563 else | |
1564 result = ConvertToPyObject(&rettv); | |
1565 | |
1566 clear_tv(&args); | |
1567 clear_tv(&rettv); | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1568 if (selfdict != NULL) |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1569 clear_tv(&selfdicttv); |
3618 | 1570 |
1571 return result; | |
1572 } | |
1573 | |
1574 static struct PyMethodDef FunctionMethods[] = { | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1575 {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1576 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1577 { NULL, NULL, 0, NULL} |
3618 | 1578 }; |
1579 | |
4350 | 1580 /* |
1581 * Options object | |
1582 */ | |
1583 | |
1584 static PyTypeObject OptionsType; | |
1585 | |
1586 typedef int (*checkfun)(void *); | |
1587 | |
1588 typedef struct | |
1589 { | |
1590 PyObject_HEAD | |
1591 int opt_type; | |
1592 void *from; | |
1593 checkfun Check; | |
1594 PyObject *fromObj; | |
1595 } OptionsObject; | |
1596 | |
4433 | 1597 static int |
1598 dummy_check(void *arg UNUSED) | |
1599 { | |
1600 return 0; | |
1601 } | |
1602 | |
1603 static PyObject * | |
1604 OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj) | |
1605 { | |
1606 OptionsObject *self; | |
1607 | |
4500 | 1608 self = PyObject_GC_New(OptionsObject, &OptionsType); |
4433 | 1609 if (self == NULL) |
1610 return NULL; | |
1611 | |
1612 self->opt_type = opt_type; | |
1613 self->from = from; | |
1614 self->Check = Check; | |
1615 self->fromObj = fromObj; | |
1616 if (fromObj) | |
1617 Py_INCREF(fromObj); | |
1618 | |
1619 return (PyObject *)(self); | |
1620 } | |
1621 | |
1622 static void | |
4488 | 1623 OptionsDestructor(OptionsObject *self) |
4433 | 1624 { |
4500 | 1625 PyObject_GC_UnTrack((void *)(self)); |
1626 Py_XDECREF(self->fromObj); | |
1627 PyObject_GC_Del((void *)(self)); | |
4433 | 1628 } |
1629 | |
1630 static int | |
4488 | 1631 OptionsTraverse(OptionsObject *self, visitproc visit, void *arg) |
4433 | 1632 { |
4488 | 1633 Py_VISIT(self->fromObj); |
4433 | 1634 return 0; |
1635 } | |
1636 | |
1637 static int | |
4488 | 1638 OptionsClear(OptionsObject *self) |
4433 | 1639 { |
4488 | 1640 Py_CLEAR(self->fromObj); |
4433 | 1641 return 0; |
1642 } | |
1643 | |
4350 | 1644 static PyObject * |
4488 | 1645 OptionsItem(OptionsObject *self, PyObject *keyObject) |
4350 | 1646 { |
1647 char_u *key; | |
1648 int flags; | |
1649 long numval; | |
1650 char_u *stringval; | |
4359 | 1651 DICTKEY_DECL |
4350 | 1652 |
4488 | 1653 if (self->Check(self->from)) |
4350 | 1654 return NULL; |
1655 | |
1656 DICTKEY_GET_NOTEMPTY(NULL) | |
1657 | |
1658 flags = get_option_value_strict(key, &numval, &stringval, | |
4488 | 1659 self->opt_type, self->from); |
4350 | 1660 |
1661 DICTKEY_UNREF | |
1662 | |
1663 if (flags == 0) | |
1664 { | |
4403 | 1665 PyErr_SetObject(PyExc_KeyError, keyObject); |
4350 | 1666 return NULL; |
1667 } | |
1668 | |
1669 if (flags & SOPT_UNSET) | |
1670 { | |
1671 Py_INCREF(Py_None); | |
1672 return Py_None; | |
1673 } | |
1674 else if (flags & SOPT_BOOL) | |
1675 { | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1676 PyObject *r; |
4350 | 1677 r = numval ? Py_True : Py_False; |
1678 Py_INCREF(r); | |
1679 return r; | |
1680 } | |
1681 else if (flags & SOPT_NUM) | |
1682 return PyInt_FromLong(numval); | |
1683 else if (flags & SOPT_STRING) | |
1684 { | |
1685 if (stringval) | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1686 { |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1687 PyObject *r = PyBytes_FromString((char *) stringval); |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1688 vim_free(stringval); |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1689 return r; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1690 } |
4350 | 1691 else |
1692 { | |
4405 | 1693 PyErr_SetString(PyExc_RuntimeError, |
1694 _("unable to get option value")); | |
4350 | 1695 return NULL; |
1696 } | |
1697 } | |
1698 else | |
1699 { | |
1700 PyErr_SetVim("Internal error: unknown option type. Should not happen"); | |
1701 return NULL; | |
1702 } | |
1703 } | |
1704 | |
1705 static int | |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1706 set_option_value_err(key, numval, stringval, opt_flags) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1707 char_u *key; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1708 int numval; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1709 char_u *stringval; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1710 int opt_flags; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1711 { |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1712 char_u *errmsg; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1713 |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1714 if ((errmsg = set_option_value(key, numval, stringval, opt_flags))) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1715 { |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1716 if (VimTryEnd()) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1717 return FAIL; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1718 PyErr_SetVim((char *)errmsg); |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1719 return FAIL; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1720 } |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1721 return OK; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1722 } |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1723 |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1724 static int |
4350 | 1725 set_option_value_for(key, numval, stringval, opt_flags, opt_type, from) |
1726 char_u *key; | |
1727 int numval; | |
1728 char_u *stringval; | |
1729 int opt_flags; | |
1730 int opt_type; | |
1731 void *from; | |
1732 { | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1733 win_T *save_curwin = NULL; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1734 tabpage_T *save_curtab = NULL; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1735 buf_T *save_curbuf = NULL; |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1736 int r = 0; |
4498 | 1737 |
1738 VimTryStart(); | |
4350 | 1739 switch (opt_type) |
1740 { | |
1741 case SREQ_WIN: | |
4429 | 1742 if (switch_win(&save_curwin, &save_curtab, (win_T *)from, |
1743 win_find_tabpage((win_T *)from)) == FAIL) | |
4350 | 1744 { |
4498 | 1745 if (VimTryEnd()) |
1746 return -1; | |
4350 | 1747 PyErr_SetVim("Problem while switching windows."); |
1748 return -1; | |
1749 } | |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1750 r = set_option_value_err(key, numval, stringval, opt_flags); |
4350 | 1751 restore_win(save_curwin, save_curtab); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1752 if (r == FAIL) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1753 return -1; |
4350 | 1754 break; |
1755 case SREQ_BUF: | |
4429 | 1756 switch_buffer(&save_curbuf, (buf_T *)from); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1757 r = set_option_value_err(key, numval, stringval, opt_flags); |
4429 | 1758 restore_buffer(save_curbuf); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1759 if (r == FAIL) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1760 return -1; |
4350 | 1761 break; |
1762 case SREQ_GLOBAL: | |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1763 r = set_option_value_err(key, numval, stringval, opt_flags); |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1764 if (r == FAIL) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1765 return -1; |
4350 | 1766 break; |
1767 } | |
4498 | 1768 return VimTryEnd(); |
4350 | 1769 } |
1770 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1771 static void * |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1772 py_memsave(void *p, size_t len) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1773 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1774 void *r; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1775 if (!(r = PyMem_Malloc(len))) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1776 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1777 mch_memmove(r, p, len); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1778 return r; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1779 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1780 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1781 #define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1)) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1782 |
4350 | 1783 static int |
4488 | 1784 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) |
4350 | 1785 { |
1786 char_u *key; | |
1787 int flags; | |
1788 int opt_flags; | |
1789 int r = 0; | |
4359 | 1790 DICTKEY_DECL |
4350 | 1791 |
4488 | 1792 if (self->Check(self->from)) |
4350 | 1793 return -1; |
1794 | |
1795 DICTKEY_GET_NOTEMPTY(-1) | |
1796 | |
1797 flags = get_option_value_strict(key, NULL, NULL, | |
4488 | 1798 self->opt_type, self->from); |
4350 | 1799 |
1800 if (flags == 0) | |
1801 { | |
4403 | 1802 PyErr_SetObject(PyExc_KeyError, keyObject); |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1803 DICTKEY_UNREF |
4350 | 1804 return -1; |
1805 } | |
1806 | |
1807 if (valObject == NULL) | |
1808 { | |
4488 | 1809 if (self->opt_type == SREQ_GLOBAL) |
4350 | 1810 { |
4405 | 1811 PyErr_SetString(PyExc_ValueError, |
1812 _("unable to unset global option")); | |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1813 DICTKEY_UNREF |
4350 | 1814 return -1; |
1815 } | |
1816 else if (!(flags & SOPT_GLOBAL)) | |
1817 { | |
4405 | 1818 PyErr_SetString(PyExc_ValueError, _("unable to unset option " |
1819 "without global value")); | |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1820 DICTKEY_UNREF |
4350 | 1821 return -1; |
1822 } | |
1823 else | |
1824 { | |
4488 | 1825 unset_global_local_option(key, self->from); |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1826 DICTKEY_UNREF |
4350 | 1827 return 0; |
1828 } | |
1829 } | |
1830 | |
4488 | 1831 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL); |
4350 | 1832 |
1833 if (flags & SOPT_BOOL) | |
1834 { | |
4411 | 1835 int istrue = PyObject_IsTrue(valObject); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1836 |
4411 | 1837 if (istrue == -1) |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1838 r = -1; |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1839 else |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1840 r = set_option_value_for(key, istrue, NULL, |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1841 opt_flags, self->opt_type, self->from); |
4350 | 1842 } |
1843 else if (flags & SOPT_NUM) | |
1844 { | |
1845 int val; | |
1846 | |
1847 #if PY_MAJOR_VERSION < 3 | |
1848 if (PyInt_Check(valObject)) | |
1849 val = PyInt_AsLong(valObject); | |
1850 else | |
1851 #endif | |
1852 if (PyLong_Check(valObject)) | |
1853 val = PyLong_AsLong(valObject); | |
1854 else | |
1855 { | |
4405 | 1856 PyErr_SetString(PyExc_TypeError, _("object must be integer")); |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1857 DICTKEY_UNREF |
4350 | 1858 return -1; |
1859 } | |
1860 | |
1861 r = set_option_value_for(key, val, NULL, opt_flags, | |
4488 | 1862 self->opt_type, self->from); |
4350 | 1863 } |
1864 else | |
1865 { | |
1866 char_u *val; | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1867 PyObject *todecref; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1868 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1869 if ((val = StringToChars(valObject, &todecref))) |
4350 | 1870 { |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1871 r = set_option_value_for(key, 0, val, opt_flags, |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1872 self->opt_type, self->from); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1873 Py_XDECREF(todecref); |
4350 | 1874 } |
1875 else | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1876 r = -1; |
4350 | 1877 } |
1878 | |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1879 DICTKEY_UNREF |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1880 |
4350 | 1881 return r; |
1882 } | |
1883 | |
1884 static PyMappingMethods OptionsAsMapping = { | |
1885 (lenfunc) NULL, | |
1886 (binaryfunc) OptionsItem, | |
1887 (objobjargproc) OptionsAssItem, | |
1888 }; | |
1889 | |
4401 | 1890 /* Tabpage object |
1891 */ | |
1892 | |
1893 typedef struct | |
1894 { | |
1895 PyObject_HEAD | |
1896 tabpage_T *tab; | |
1897 } TabPageObject; | |
1898 | |
1899 static PyObject *WinListNew(TabPageObject *tabObject); | |
1900 | |
1901 static PyTypeObject TabPageType; | |
1902 | |
1903 static int | |
4488 | 1904 CheckTabPage(TabPageObject *self) |
4401 | 1905 { |
4488 | 1906 if (self->tab == INVALID_TABPAGE_VALUE) |
4401 | 1907 { |
1908 PyErr_SetVim(_("attempt to refer to deleted tab page")); | |
1909 return -1; | |
1910 } | |
1911 | |
1912 return 0; | |
1913 } | |
1914 | |
1915 static PyObject * | |
1916 TabPageNew(tabpage_T *tab) | |
1917 { | |
1918 TabPageObject *self; | |
1919 | |
1920 if (TAB_PYTHON_REF(tab)) | |
1921 { | |
1922 self = TAB_PYTHON_REF(tab); | |
1923 Py_INCREF(self); | |
1924 } | |
1925 else | |
1926 { | |
1927 self = PyObject_NEW(TabPageObject, &TabPageType); | |
1928 if (self == NULL) | |
1929 return NULL; | |
1930 self->tab = tab; | |
1931 TAB_PYTHON_REF(tab) = self; | |
1932 } | |
1933 | |
1934 return (PyObject *)(self); | |
1935 } | |
1936 | |
1937 static void | |
4488 | 1938 TabPageDestructor(TabPageObject *self) |
4401 | 1939 { |
4488 | 1940 if (self->tab && self->tab != INVALID_TABPAGE_VALUE) |
1941 TAB_PYTHON_REF(self->tab) = NULL; | |
4401 | 1942 |
1943 DESTRUCTOR_FINISH(self); | |
1944 } | |
1945 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1946 static char *TabPageAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1947 "windows", "number", "vars", "window", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1948 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1949 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1950 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1951 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1952 TabPageDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1953 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1954 return ObjectDir(self, TabPageAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1955 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1956 |
4401 | 1957 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1958 TabPageAttrValid(TabPageObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1959 { |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1960 PyObject *r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1961 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1962 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1963 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1964 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1965 r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1966 Py_INCREF(r); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1967 return r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1968 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1969 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1970 static PyObject * |
4488 | 1971 TabPageAttr(TabPageObject *self, char *name) |
4401 | 1972 { |
1973 if (strcmp(name, "windows") == 0) | |
4488 | 1974 return WinListNew(self); |
4401 | 1975 else if (strcmp(name, "number") == 0) |
4488 | 1976 return PyLong_FromLong((long) get_tab_number(self->tab)); |
4401 | 1977 else if (strcmp(name, "vars") == 0) |
4488 | 1978 return DictionaryNew(self->tab->tp_vars); |
4401 | 1979 else if (strcmp(name, "window") == 0) |
1980 { | |
1981 /* For current tab window.c does not bother to set or update tp_curwin | |
1982 */ | |
4488 | 1983 if (self->tab == curtab) |
4431 | 1984 return WindowNew(curwin, curtab); |
4401 | 1985 else |
4488 | 1986 return WindowNew(self->tab->tp_curwin, self->tab); |
4401 | 1987 } |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1988 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1989 return ObjectDir(NULL, TabPageAttrs); |
4401 | 1990 return NULL; |
1991 } | |
1992 | |
1993 static PyObject * | |
4488 | 1994 TabPageRepr(TabPageObject *self) |
4401 | 1995 { |
1996 static char repr[100]; | |
4488 | 1997 |
1998 if (self->tab == INVALID_TABPAGE_VALUE) | |
4401 | 1999 { |
2000 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self)); | |
2001 return PyString_FromString(repr); | |
2002 } | |
2003 else | |
2004 { | |
4488 | 2005 int t = get_tab_number(self->tab); |
4401 | 2006 |
2007 if (t == 0) | |
2008 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"), | |
2009 (self)); | |
2010 else | |
2011 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1); | |
2012 | |
2013 return PyString_FromString(repr); | |
2014 } | |
2015 } | |
2016 | |
2017 static struct PyMethodDef TabPageMethods[] = { | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2018 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2019 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2020 { NULL, NULL, 0, NULL} |
4401 | 2021 }; |
2022 | |
2023 /* | |
2024 * Window list object | |
2025 */ | |
2026 | |
2027 static PyTypeObject TabListType; | |
2028 static PySequenceMethods TabListAsSeq; | |
2029 | |
2030 typedef struct | |
2031 { | |
2032 PyObject_HEAD | |
2033 } TabListObject; | |
2034 | |
2035 static PyInt | |
2036 TabListLength(PyObject *self UNUSED) | |
2037 { | |
2038 tabpage_T *tp = first_tabpage; | |
2039 PyInt n = 0; | |
2040 | |
2041 while (tp != NULL) | |
2042 { | |
2043 ++n; | |
2044 tp = tp->tp_next; | |
2045 } | |
2046 | |
2047 return n; | |
2048 } | |
2049 | |
2050 static PyObject * | |
2051 TabListItem(PyObject *self UNUSED, PyInt n) | |
2052 { | |
2053 tabpage_T *tp; | |
2054 | |
2055 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n) | |
2056 if (n == 0) | |
2057 return TabPageNew(tp); | |
2058 | |
2059 PyErr_SetString(PyExc_IndexError, _("no such tab page")); | |
2060 return NULL; | |
2061 } | |
2062 | |
4385 | 2063 /* Window object |
2064 */ | |
2065 | |
2066 typedef struct | |
2067 { | |
2068 PyObject_HEAD | |
2069 win_T *win; | |
4431 | 2070 TabPageObject *tabObject; |
4385 | 2071 } WindowObject; |
2072 | |
2073 static PyTypeObject WindowType; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2074 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2075 static int |
4488 | 2076 CheckWindow(WindowObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2077 { |
4488 | 2078 if (self->win == INVALID_WINDOW_VALUE) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2079 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2080 PyErr_SetVim(_("attempt to refer to deleted window")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2081 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2082 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2083 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2084 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2085 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2086 |
4319 | 2087 static PyObject * |
4431 | 2088 WindowNew(win_T *win, tabpage_T *tab) |
4377 | 2089 { |
2090 /* We need to handle deletion of windows underneath us. | |
2091 * If we add a "w_python*_ref" field to the win_T structure, | |
2092 * then we can get at it in win_free() in vim. We then | |
2093 * need to create only ONE Python object per window - if | |
2094 * we try to create a second, just INCREF the existing one | |
2095 * and return it. The (single) Python object referring to | |
2096 * the window is stored in "w_python*_ref". | |
2097 * On a win_free() we set the Python object's win_T* field | |
2098 * to an invalid value. We trap all uses of a window | |
2099 * object, and reject them if the win_T* field is invalid. | |
2100 * | |
4385 | 2101 * Python2 and Python3 get different fields and different objects: |
4377 | 2102 * w_python_ref and w_python3_ref fields respectively. |
2103 */ | |
2104 | |
2105 WindowObject *self; | |
2106 | |
2107 if (WIN_PYTHON_REF(win)) | |
2108 { | |
2109 self = WIN_PYTHON_REF(win); | |
2110 Py_INCREF(self); | |
2111 } | |
2112 else | |
2113 { | |
4500 | 2114 self = PyObject_GC_New(WindowObject, &WindowType); |
4377 | 2115 if (self == NULL) |
2116 return NULL; | |
2117 self->win = win; | |
2118 WIN_PYTHON_REF(win) = self; | |
2119 } | |
2120 | |
4431 | 2121 self->tabObject = ((TabPageObject *)(TabPageNew(tab))); |
2122 | |
4377 | 2123 return (PyObject *)(self); |
2124 } | |
2125 | |
4385 | 2126 static void |
4488 | 2127 WindowDestructor(WindowObject *self) |
4385 | 2128 { |
4500 | 2129 PyObject_GC_UnTrack((void *)(self)); |
4488 | 2130 if (self->win && self->win != INVALID_WINDOW_VALUE) |
2131 WIN_PYTHON_REF(self->win) = NULL; | |
4500 | 2132 Py_XDECREF(((PyObject *)(self->tabObject))); |
2133 PyObject_GC_Del((void *)(self)); | |
2134 } | |
2135 | |
2136 static int | |
2137 WindowTraverse(WindowObject *self, visitproc visit, void *arg) | |
2138 { | |
2139 Py_VISIT(((PyObject *)(self->tabObject))); | |
2140 return 0; | |
2141 } | |
2142 | |
2143 static int | |
2144 WindowClear(WindowObject *self) | |
2145 { | |
2146 Py_CLEAR(self->tabObject); | |
2147 return 0; | |
4385 | 2148 } |
2149 | |
4431 | 2150 static win_T * |
2151 get_firstwin(TabPageObject *tabObject) | |
2152 { | |
2153 if (tabObject) | |
2154 { | |
2155 if (CheckTabPage(tabObject)) | |
2156 return NULL; | |
2157 /* For current tab window.c does not bother to set or update tp_firstwin | |
2158 */ | |
2159 else if (tabObject->tab == curtab) | |
2160 return firstwin; | |
2161 else | |
2162 return tabObject->tab->tp_firstwin; | |
2163 } | |
2164 else | |
2165 return firstwin; | |
2166 } | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2167 static char *WindowAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2168 "buffer", "cursor", "height", "vars", "options", "number", "row", "col", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2169 "tabpage", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2170 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2171 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2172 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2173 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2174 WindowDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2175 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2176 return ObjectDir(self, WindowAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2177 } |
4431 | 2178 |
4377 | 2179 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2180 WindowAttrValid(WindowObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2181 { |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2182 PyObject *r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2183 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2184 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2185 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2186 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2187 r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2188 Py_INCREF(r); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2189 return r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2190 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2191 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2192 static PyObject * |
4488 | 2193 WindowAttr(WindowObject *self, char *name) |
4319 | 2194 { |
2195 if (strcmp(name, "buffer") == 0) | |
4488 | 2196 return (PyObject *)BufferNew(self->win->w_buffer); |
4319 | 2197 else if (strcmp(name, "cursor") == 0) |
2198 { | |
4488 | 2199 pos_T *pos = &self->win->w_cursor; |
4319 | 2200 |
2201 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); | |
2202 } | |
2203 else if (strcmp(name, "height") == 0) | |
4488 | 2204 return PyLong_FromLong((long)(self->win->w_height)); |
4383 | 2205 #ifdef FEAT_WINDOWS |
2206 else if (strcmp(name, "row") == 0) | |
4488 | 2207 return PyLong_FromLong((long)(self->win->w_winrow)); |
4383 | 2208 #endif |
4319 | 2209 #ifdef FEAT_VERTSPLIT |
2210 else if (strcmp(name, "width") == 0) | |
4488 | 2211 return PyLong_FromLong((long)(W_WIDTH(self->win))); |
4383 | 2212 else if (strcmp(name, "col") == 0) |
4488 | 2213 return PyLong_FromLong((long)(W_WINCOL(self->win))); |
4319 | 2214 #endif |
4323 | 2215 else if (strcmp(name, "vars") == 0) |
4488 | 2216 return DictionaryNew(self->win->w_vars); |
4350 | 2217 else if (strcmp(name, "options") == 0) |
4488 | 2218 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow, |
2219 (PyObject *) self); | |
4379 | 2220 else if (strcmp(name, "number") == 0) |
4431 | 2221 { |
4488 | 2222 if (CheckTabPage(self->tabObject)) |
4431 | 2223 return NULL; |
2224 return PyLong_FromLong((long) | |
4488 | 2225 get_win_number(self->win, get_firstwin(self->tabObject))); |
4431 | 2226 } |
2227 else if (strcmp(name, "tabpage") == 0) | |
2228 { | |
4488 | 2229 Py_INCREF(self->tabObject); |
2230 return (PyObject *)(self->tabObject); | |
4431 | 2231 } |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2232 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2233 return ObjectDir(NULL, WindowAttrs); |
4319 | 2234 else |
2235 return NULL; | |
2236 } | |
2237 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2238 static int |
4488 | 2239 WindowSetattr(WindowObject *self, char *name, PyObject *val) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2240 { |
4488 | 2241 if (CheckWindow(self)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2242 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2243 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2244 if (strcmp(name, "buffer") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2245 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2246 PyErr_SetString(PyExc_TypeError, _("readonly attribute")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2247 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2248 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2249 else if (strcmp(name, "cursor") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2250 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2251 long lnum; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2252 long col; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2253 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2254 if (!PyArg_Parse(val, "(ll)", &lnum, &col)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2255 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2256 |
4488 | 2257 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2258 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2259 PyErr_SetVim(_("cursor position outside buffer")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2260 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2261 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2262 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2263 /* Check for keyboard interrupts */ |
4498 | 2264 if (VimCheckInterrupt()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2265 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2266 |
4488 | 2267 self->win->w_cursor.lnum = lnum; |
2268 self->win->w_cursor.col = col; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2269 #ifdef FEAT_VIRTUALEDIT |
4488 | 2270 self->win->w_cursor.coladd = 0; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2271 #endif |
2933 | 2272 /* When column is out of range silently correct it. */ |
4488 | 2273 check_cursor_col_win(self->win); |
2933 | 2274 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2275 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2276 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2277 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2278 else if (strcmp(name, "height") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2279 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2280 int height; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2281 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2282 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2283 if (!PyArg_Parse(val, "i", &height)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2284 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2285 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2286 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2287 need_mouse_correct = TRUE; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2288 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2289 savewin = curwin; |
4488 | 2290 curwin = self->win; |
4498 | 2291 |
2292 VimTryStart(); | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2293 win_setheight(height); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2294 curwin = savewin; |
4498 | 2295 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2296 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2297 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2298 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2299 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2300 #ifdef FEAT_VERTSPLIT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2301 else if (strcmp(name, "width") == 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2302 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2303 int width; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2304 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2305 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2306 if (!PyArg_Parse(val, "i", &width)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2307 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2308 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2309 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2310 need_mouse_correct = TRUE; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2311 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2312 savewin = curwin; |
4488 | 2313 curwin = self->win; |
4498 | 2314 |
2315 VimTryStart(); | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2316 win_setwidth(width); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2317 curwin = savewin; |
4498 | 2318 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2319 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2320 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2321 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2322 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2323 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2324 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2325 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2326 PyErr_SetString(PyExc_AttributeError, name); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2327 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2328 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2329 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2330 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2331 static PyObject * |
4488 | 2332 WindowRepr(WindowObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2333 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2334 static char repr[100]; |
4488 | 2335 |
2336 if (self->win == INVALID_WINDOW_VALUE) | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2337 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2338 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2339 return PyString_FromString(repr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2340 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2341 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2342 { |
4488 | 2343 int w = get_win_number(self->win, firstwin); |
4379 | 2344 |
2345 if (w == 0) | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2346 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"), |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2347 (self)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2348 else |
4379 | 2349 vim_snprintf(repr, 100, _("<window %d>"), w - 1); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2350 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2351 return PyString_FromString(repr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2352 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2353 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2354 |
4385 | 2355 static struct PyMethodDef WindowMethods[] = { |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2356 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2357 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2358 { NULL, NULL, 0, NULL} |
4385 | 2359 }; |
2360 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2361 /* |
4385 | 2362 * Window list object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2363 */ |
4319 | 2364 |
4385 | 2365 static PyTypeObject WinListType; |
2366 static PySequenceMethods WinListAsSeq; | |
2367 | |
4319 | 2368 typedef struct |
2369 { | |
2370 PyObject_HEAD | |
4401 | 2371 TabPageObject *tabObject; |
4319 | 2372 } WinListObject; |
2373 | |
4401 | 2374 static PyObject * |
2375 WinListNew(TabPageObject *tabObject) | |
2376 { | |
2377 WinListObject *self; | |
2378 | |
2379 self = PyObject_NEW(WinListObject, &WinListType); | |
2380 self->tabObject = tabObject; | |
2381 Py_INCREF(tabObject); | |
2382 | |
2383 return (PyObject *)(self); | |
2384 } | |
2385 | |
2386 static void | |
4488 | 2387 WinListDestructor(WinListObject *self) |
4401 | 2388 { |
4488 | 2389 TabPageObject *tabObject = self->tabObject; |
4401 | 2390 |
2391 if (tabObject) | |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
2392 { |
4401 | 2393 Py_DECREF((PyObject *)(tabObject)); |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
2394 } |
4401 | 2395 |
2396 DESTRUCTOR_FINISH(self); | |
2397 } | |
2398 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2399 static PyInt |
4488 | 2400 WinListLength(WinListObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2401 { |
4401 | 2402 win_T *w; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2403 PyInt n = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2404 |
4488 | 2405 if (!(w = get_firstwin(self->tabObject))) |
4401 | 2406 return -1; |
2407 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2408 while (w != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2409 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2410 ++n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2411 w = W_NEXT(w); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2412 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2413 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2414 return n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2415 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2416 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2417 static PyObject * |
4488 | 2418 WinListItem(WinListObject *self, PyInt n) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2419 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2420 win_T *w; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2421 |
4488 | 2422 if (!(w = get_firstwin(self->tabObject))) |
4401 | 2423 return NULL; |
2424 | |
2425 for (; w != NULL; w = W_NEXT(w), --n) | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2426 if (n == 0) |
4488 | 2427 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2428 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2429 PyErr_SetString(PyExc_IndexError, _("no such window")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2430 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2431 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2432 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2433 /* Convert a Python string into a Vim line. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2434 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2435 * The result is in allocated memory. All internal nulls are replaced by |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2436 * newline characters. It is an error for the string to contain newline |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2437 * characters. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2438 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2439 * On errors, the Python exception data is set, and NULL is returned. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2440 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2441 static char * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2442 StringToLine(PyObject *obj) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2443 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2444 const char *str; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2445 char *save; |
2894 | 2446 PyObject *bytes; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2447 PyInt len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2448 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2449 char *p; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2450 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2451 if (obj == NULL || !PyString_Check(obj)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2452 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2453 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2454 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2455 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2456 |
2894 | 2457 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
2458 str = PyString_AsString(bytes); | |
2459 len = PyString_Size(bytes); | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2460 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2461 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2462 * Error checking: String must not contain newlines, as we |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2463 * are replacing a single line, and we must replace it with |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2464 * a single line. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2465 * A trailing newline is removed, so that append(f.readlines()) works. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2466 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2467 p = memchr(str, '\n', len); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2468 if (p != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2469 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2470 if (p == str + len - 1) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2471 --len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2472 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2473 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2474 PyErr_SetVim(_("string cannot contain newlines")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2475 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2476 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2477 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2478 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2479 /* Create a copy of the string, with internal nulls replaced by |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2480 * newline characters, as is the vim convention. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2481 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2482 save = (char *)alloc((unsigned)(len+1)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2483 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2484 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2485 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2486 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2487 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2488 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2489 for (i = 0; i < len; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2490 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2491 if (str[i] == '\0') |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2492 save[i] = '\n'; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2493 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2494 save[i] = str[i]; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2495 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2496 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2497 save[i] = '\0'; |
2894 | 2498 PyString_FreeBytes(bytes); /* Python 2 does nothing here */ |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2499 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2500 return save; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2501 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2502 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2503 /* Get a line from the specified buffer. The line number is |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2504 * in Vim format (1-based). The line is returned as a Python |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2505 * string object. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2506 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2507 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2508 GetBufferLine(buf_T *buf, PyInt n) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2509 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2510 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2511 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2512 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2513 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2514 /* Get a list of lines from the specified buffer. The line numbers |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2515 * are in Vim format (1-based). The range is from lo up to, but not |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2516 * including, hi. The list is returned as a Python list of string objects. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2517 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2518 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2519 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2520 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2521 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2522 PyInt n = hi - lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2523 PyObject *list = PyList_New(n); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2524 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2525 if (list == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2526 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2527 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2528 for (i = 0; i < n; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2529 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2530 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2531 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2532 /* Error check - was the Python string creation OK? */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2533 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2534 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2535 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2536 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2537 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2538 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2539 /* Set the list item */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2540 if (PyList_SetItem(list, i, str)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2541 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2542 Py_DECREF(str); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2543 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2544 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2545 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2546 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2547 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2548 /* The ownership of the Python list is passed to the caller (ie, |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2549 * the caller should Py_DECREF() the object when it is finished |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2550 * with it). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2551 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2552 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2553 return list; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2554 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2555 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2556 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2557 * Check if deleting lines made the cursor position invalid. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2558 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2559 * deleted). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2560 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2561 static void |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2562 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2563 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2564 if (curwin->w_cursor.lnum >= lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2565 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2566 /* Adjust the cursor position if it's in/after the changed |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2567 * lines. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2568 if (curwin->w_cursor.lnum >= hi) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2569 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2570 curwin->w_cursor.lnum += extra; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2571 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2572 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2573 else if (extra < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2574 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2575 curwin->w_cursor.lnum = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2576 check_cursor(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2577 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2578 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2579 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2580 changed_cline_bef_curs(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2581 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2582 invalidate_botline(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2583 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2584 |
2894 | 2585 /* |
2586 * Replace a line in the specified buffer. The line number is | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2587 * in Vim format (1-based). The replacement line is given as |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2588 * a Python string object. The object is checked for validity |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2589 * and correct format. Errors are returned as a value of FAIL. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2590 * The return value is OK on success. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2591 * If OK is returned and len_change is not NULL, *len_change |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2592 * is set to the change in the buffer length. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2593 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2594 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2595 SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2596 { |
4352 | 2597 /* First of all, we check the type of the supplied Python object. |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2598 * There are three cases: |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2599 * 1. NULL, or None - this is a deletion. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2600 * 2. A string - this is a replacement. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2601 * 3. Anything else - this is an error. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2602 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2603 if (line == Py_None || line == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2604 { |
4429 | 2605 buf_T *savebuf; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2606 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2607 PyErr_Clear(); |
4429 | 2608 switch_buffer(&savebuf, buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2609 |
4498 | 2610 VimTryStart(); |
2611 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2612 if (u_savedel((linenr_T)n, 1L) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2613 PyErr_SetVim(_("cannot save undo information")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2614 else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2615 PyErr_SetVim(_("cannot delete line")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2616 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2617 { |
4429 | 2618 if (buf == savebuf) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2619 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2620 deleted_lines_mark((linenr_T)n, 1L); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2621 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2622 |
4429 | 2623 restore_buffer(savebuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2624 |
4498 | 2625 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2626 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2627 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2628 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2629 *len_change = -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2630 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2631 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2632 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2633 else if (PyString_Check(line)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2634 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2635 char *save = StringToLine(line); |
4429 | 2636 buf_T *savebuf; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2637 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2638 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2639 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2640 |
4498 | 2641 VimTryStart(); |
2642 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2643 /* We do not need to free "save" if ml_replace() consumes it. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2644 PyErr_Clear(); |
4429 | 2645 switch_buffer(&savebuf, buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2646 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2647 if (u_savesub((linenr_T)n) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2648 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2649 PyErr_SetVim(_("cannot save undo information")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2650 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2651 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2652 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2653 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2654 PyErr_SetVim(_("cannot replace line")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2655 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2656 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2657 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2658 changed_bytes((linenr_T)n, 0); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2659 |
4429 | 2660 restore_buffer(savebuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2661 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2662 /* Check that the cursor is not beyond the end of the line now. */ |
4429 | 2663 if (buf == savebuf) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2664 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2665 |
4498 | 2666 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2667 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2668 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2669 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2670 *len_change = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2671 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2672 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2673 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2674 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2675 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2676 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2677 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2678 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2679 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2680 |
2894 | 2681 /* Replace a range of lines in the specified buffer. The line numbers are in |
2682 * Vim format (1-based). The range is from lo up to, but not including, hi. | |
2683 * The replacement lines are given as a Python list of string objects. The | |
2684 * list is checked for validity and correct format. Errors are returned as a | |
2685 * value of FAIL. The return value is OK on success. | |
2686 * If OK is returned and len_change is not NULL, *len_change | |
2687 * is set to the change in the buffer length. | |
2688 */ | |
2689 static int | |
2690 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) | |
2691 { | |
4352 | 2692 /* First of all, we check the type of the supplied Python object. |
2894 | 2693 * There are three cases: |
2694 * 1. NULL, or None - this is a deletion. | |
2695 * 2. A list - this is a replacement. | |
2696 * 3. Anything else - this is an error. | |
2697 */ | |
2698 if (list == Py_None || list == NULL) | |
2699 { | |
2700 PyInt i; | |
2701 PyInt n = (int)(hi - lo); | |
4429 | 2702 buf_T *savebuf; |
2894 | 2703 |
2704 PyErr_Clear(); | |
4498 | 2705 VimTryStart(); |
4429 | 2706 switch_buffer(&savebuf, buf); |
2894 | 2707 |
2708 if (u_savedel((linenr_T)lo, (long)n) == FAIL) | |
2709 PyErr_SetVim(_("cannot save undo information")); | |
2710 else | |
2711 { | |
2712 for (i = 0; i < n; ++i) | |
2713 { | |
2714 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2715 { | |
2716 PyErr_SetVim(_("cannot delete line")); | |
2717 break; | |
2718 } | |
2719 } | |
4429 | 2720 if (buf == savebuf) |
2894 | 2721 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
2722 deleted_lines_mark((linenr_T)lo, (long)i); | |
2723 } | |
2724 | |
4429 | 2725 restore_buffer(savebuf); |
2894 | 2726 |
4498 | 2727 if (VimTryEnd()) |
2894 | 2728 return FAIL; |
2729 | |
2730 if (len_change) | |
2731 *len_change = -n; | |
2732 | |
2733 return OK; | |
2734 } | |
2735 else if (PyList_Check(list)) | |
2736 { | |
2737 PyInt i; | |
2738 PyInt new_len = PyList_Size(list); | |
2739 PyInt old_len = hi - lo; | |
2740 PyInt extra = 0; /* lines added to text, can be negative */ | |
2741 char **array; | |
2742 buf_T *savebuf; | |
2743 | |
2744 if (new_len == 0) /* avoid allocating zero bytes */ | |
2745 array = NULL; | |
2746 else | |
2747 { | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2748 array = PyMem_New(char *, new_len); |
2894 | 2749 if (array == NULL) |
2750 { | |
2751 PyErr_NoMemory(); | |
2752 return FAIL; | |
2753 } | |
2754 } | |
2755 | |
2756 for (i = 0; i < new_len; ++i) | |
2757 { | |
2758 PyObject *line = PyList_GetItem(list, i); | |
2759 | |
2760 array[i] = StringToLine(line); | |
2761 if (array[i] == NULL) | |
2762 { | |
2763 while (i) | |
2764 vim_free(array[--i]); | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2765 PyMem_Free(array); |
2894 | 2766 return FAIL; |
2767 } | |
2768 } | |
2769 | |
4498 | 2770 VimTryStart(); |
2894 | 2771 PyErr_Clear(); |
4429 | 2772 |
2773 // START of region without "return". Must call restore_buffer()! | |
2774 switch_buffer(&savebuf, buf); | |
2894 | 2775 |
2776 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) | |
2777 PyErr_SetVim(_("cannot save undo information")); | |
2778 | |
2779 /* If the size of the range is reducing (ie, new_len < old_len) we | |
2780 * need to delete some old_len. We do this at the start, by | |
2781 * repeatedly deleting line "lo". | |
2782 */ | |
2783 if (!PyErr_Occurred()) | |
2784 { | |
2785 for (i = 0; i < old_len - new_len; ++i) | |
2786 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2787 { | |
2788 PyErr_SetVim(_("cannot delete line")); | |
2789 break; | |
2790 } | |
2791 extra -= i; | |
2792 } | |
2793 | |
2794 /* For as long as possible, replace the existing old_len with the | |
2795 * new old_len. This is a more efficient operation, as it requires | |
2796 * less memory allocation and freeing. | |
2797 */ | |
2798 if (!PyErr_Occurred()) | |
2799 { | |
2800 for (i = 0; i < old_len && i < new_len; ++i) | |
2801 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) | |
2802 == FAIL) | |
2803 { | |
2804 PyErr_SetVim(_("cannot replace line")); | |
2805 break; | |
2806 } | |
2807 } | |
2808 else | |
2809 i = 0; | |
2810 | |
2811 /* Now we may need to insert the remaining new old_len. If we do, we | |
2812 * must free the strings as we finish with them (we can't pass the | |
2813 * responsibility to vim in this case). | |
2814 */ | |
2815 if (!PyErr_Occurred()) | |
2816 { | |
2817 while (i < new_len) | |
2818 { | |
2819 if (ml_append((linenr_T)(lo + i - 1), | |
2820 (char_u *)array[i], 0, FALSE) == FAIL) | |
2821 { | |
2822 PyErr_SetVim(_("cannot insert line")); | |
2823 break; | |
2824 } | |
2825 vim_free(array[i]); | |
2826 ++i; | |
2827 ++extra; | |
2828 } | |
2829 } | |
2830 | |
2831 /* Free any left-over old_len, as a result of an error */ | |
2832 while (i < new_len) | |
2833 { | |
2834 vim_free(array[i]); | |
2835 ++i; | |
2836 } | |
2837 | |
2838 /* Free the array of old_len. All of its contents have now | |
2839 * been dealt with (either freed, or the responsibility passed | |
2840 * to vim. | |
2841 */ | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2842 PyMem_Free(array); |
2894 | 2843 |
2844 /* Adjust marks. Invalidate any which lie in the | |
2845 * changed range, and move any in the remainder of the buffer. | |
2846 */ | |
2847 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), | |
2848 (long)MAXLNUM, (long)extra); | |
2849 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); | |
2850 | |
4429 | 2851 if (buf == savebuf) |
2894 | 2852 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
2853 | |
4429 | 2854 // END of region without "return". |
2855 restore_buffer(savebuf); | |
2894 | 2856 |
4498 | 2857 if (VimTryEnd()) |
2894 | 2858 return FAIL; |
2859 | |
2860 if (len_change) | |
2861 *len_change = new_len - old_len; | |
2862 | |
2863 return OK; | |
2864 } | |
2865 else | |
2866 { | |
2867 PyErr_BadArgument(); | |
2868 return FAIL; | |
2869 } | |
2870 } | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2871 |
4352 | 2872 /* Insert a number of lines into the specified buffer after the specified line. |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2873 * The line number is in Vim format (1-based). The lines to be inserted are |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2874 * given as a Python list of string objects or as a single string. The lines |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2875 * to be added are checked for validity and correct format. Errors are |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2876 * returned as a value of FAIL. The return value is OK on success. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2877 * If OK is returned and len_change is not NULL, *len_change |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2878 * is set to the change in the buffer length. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2879 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2880 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2881 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2882 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2883 /* First of all, we check the type of the supplied Python object. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2884 * It must be a string or a list, or the call is in error. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2885 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2886 if (PyString_Check(lines)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2887 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2888 char *str = StringToLine(lines); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2889 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2890 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2891 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2892 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2893 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2894 PyErr_Clear(); |
4498 | 2895 VimTryStart(); |
4429 | 2896 switch_buffer(&savebuf, buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2897 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2898 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2899 PyErr_SetVim(_("cannot save undo information")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2900 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2901 PyErr_SetVim(_("cannot insert line")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2902 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2903 appended_lines_mark((linenr_T)n, 1L); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2904 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2905 vim_free(str); |
4429 | 2906 restore_buffer(savebuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2907 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2908 |
4498 | 2909 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2910 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2911 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2912 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2913 *len_change = 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2914 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2915 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2916 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2917 else if (PyList_Check(lines)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2918 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2919 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2920 PyInt size = PyList_Size(lines); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2921 char **array; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2922 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2923 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2924 array = PyMem_New(char *, size); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2925 if (array == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2926 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2927 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2928 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2929 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2930 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2931 for (i = 0; i < size; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2932 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2933 PyObject *line = PyList_GetItem(lines, i); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2934 array[i] = StringToLine(line); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2935 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2936 if (array[i] == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2937 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2938 while (i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2939 vim_free(array[--i]); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2940 PyMem_Free(array); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2941 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2942 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2943 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2944 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2945 PyErr_Clear(); |
4498 | 2946 VimTryStart(); |
4429 | 2947 switch_buffer(&savebuf, buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2948 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2949 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2950 PyErr_SetVim(_("cannot save undo information")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2951 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2952 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2953 for (i = 0; i < size; ++i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2954 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2955 if (ml_append((linenr_T)(n + i), |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2956 (char_u *)array[i], 0, FALSE) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2957 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2958 PyErr_SetVim(_("cannot insert line")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2959 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2960 /* Free the rest of the lines */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2961 while (i < size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2962 vim_free(array[i++]); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2963 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2964 break; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2965 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2966 vim_free(array[i]); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2967 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2968 if (i > 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2969 appended_lines_mark((linenr_T)n, (long)i); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2970 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2971 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2972 /* Free the array of lines. All of its contents have now |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2973 * been freed. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2974 */ |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2975 PyMem_Free(array); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2976 |
4429 | 2977 restore_buffer(savebuf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2978 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2979 |
4498 | 2980 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2981 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2982 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2983 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2984 *len_change = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2985 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2986 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2987 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2988 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2989 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2990 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2991 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2992 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2993 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2994 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2995 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2996 * Common routines for buffers and line ranges |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2997 * ------------------------------------------- |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2998 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2999 |
4385 | 3000 typedef struct |
3001 { | |
3002 PyObject_HEAD | |
3003 buf_T *buf; | |
3004 } BufferObject; | |
3005 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3006 static int |
4488 | 3007 CheckBuffer(BufferObject *self) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3008 { |
4488 | 3009 if (self->buf == INVALID_BUFFER_VALUE) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3010 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3011 PyErr_SetVim(_("attempt to refer to deleted buffer")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3012 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3013 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3014 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3015 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3016 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3017 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3018 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3019 RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3020 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3021 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3022 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3023 |
4387 | 3024 if (end == -1) |
3025 end = self->buf->b_ml.ml_line_count; | |
3026 | |
4389 | 3027 if (n < 0) |
3028 n += end - start + 1; | |
3029 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3030 if (n < 0 || n > end - start) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3031 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3032 PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3033 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3034 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3035 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3036 return GetBufferLine(self->buf, n+start); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3037 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3038 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3039 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3040 RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3041 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3042 PyInt size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3043 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3044 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3045 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3046 |
4387 | 3047 if (end == -1) |
3048 end = self->buf->b_ml.ml_line_count; | |
3049 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3050 size = end - start + 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3051 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3052 if (lo < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3053 lo = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3054 else if (lo > size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3055 lo = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3056 if (hi < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3057 hi = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3058 if (hi < lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3059 hi = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3060 else if (hi > size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3061 hi = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3062 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3063 return GetBufferLineList(self->buf, lo+start, hi+start); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3064 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3065 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3066 static PyInt |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3067 RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3068 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3069 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3070 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3071 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3072 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3073 |
4387 | 3074 if (end == -1) |
3075 end = self->buf->b_ml.ml_line_count; | |
3076 | |
4389 | 3077 if (n < 0) |
3078 n += end - start + 1; | |
3079 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3080 if (n < 0 || n > end - start) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3081 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3082 PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3083 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3084 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3085 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3086 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3087 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3088 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3089 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3090 *new_end = end + len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3091 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3092 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3093 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3094 |
2894 | 3095 static PyInt |
3096 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) | |
3097 { | |
3098 PyInt size; | |
3099 PyInt len_change; | |
3100 | |
3101 /* Self must be a valid buffer */ | |
3102 if (CheckBuffer(self)) | |
3103 return -1; | |
3104 | |
4387 | 3105 if (end == -1) |
3106 end = self->buf->b_ml.ml_line_count; | |
3107 | |
2894 | 3108 /* Sort out the slice range */ |
3109 size = end - start + 1; | |
3110 | |
3111 if (lo < 0) | |
3112 lo = 0; | |
3113 else if (lo > size) | |
3114 lo = size; | |
3115 if (hi < 0) | |
3116 hi = 0; | |
3117 if (hi < lo) | |
3118 hi = lo; | |
3119 else if (hi > size) | |
3120 hi = size; | |
3121 | |
3122 if (SetBufferLineList(self->buf, lo + start, hi + start, | |
3123 val, &len_change) == FAIL) | |
3124 return -1; | |
3125 | |
3126 if (new_end) | |
3127 *new_end = end + len_change; | |
3128 | |
3129 return 0; | |
3130 } | |
3131 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3132 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3133 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3134 RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3135 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3136 PyObject *lines; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3137 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3138 PyInt max; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3139 PyInt n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3140 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3141 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3142 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3143 |
4387 | 3144 if (end == -1) |
3145 end = self->buf->b_ml.ml_line_count; | |
3146 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3147 max = n = end - start + 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3148 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3149 if (!PyArg_ParseTuple(args, "O|n", &lines, &n)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3150 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3151 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3152 if (n < 0 || n > max) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3153 { |
4405 | 3154 PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3155 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3156 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3157 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3158 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3159 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3160 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3161 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3162 *new_end = end + len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3163 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3164 Py_INCREF(Py_None); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3165 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3166 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3167 |
4385 | 3168 /* Range object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3169 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3170 |
4319 | 3171 static PyTypeObject RangeType; |
4385 | 3172 static PySequenceMethods RangeAsSeq; |
3173 static PyMappingMethods RangeAsMapping; | |
4319 | 3174 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3175 typedef struct |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3176 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3177 PyObject_HEAD |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3178 BufferObject *buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3179 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3180 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3181 } RangeObject; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3182 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3183 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3184 RangeNew(buf_T *buf, PyInt start, PyInt end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3185 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3186 BufferObject *bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3187 RangeObject *self; |
4500 | 3188 self = PyObject_GC_New(RangeObject, &RangeType); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3189 if (self == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3190 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3191 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3192 bufr = (BufferObject *)BufferNew(buf); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3193 if (bufr == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3194 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3195 Py_DECREF(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3196 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3197 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3198 Py_INCREF(bufr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3199 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3200 self->buf = bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3201 self->start = start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3202 self->end = end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3203 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3204 return (PyObject *)(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3205 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3206 |
4319 | 3207 static void |
4488 | 3208 RangeDestructor(RangeObject *self) |
4319 | 3209 { |
4500 | 3210 PyObject_GC_UnTrack((void *)(self)); |
4498 | 3211 Py_XDECREF(self->buf); |
4500 | 3212 PyObject_GC_Del((void *)(self)); |
3213 } | |
3214 | |
3215 static int | |
3216 RangeTraverse(RangeObject *self, visitproc visit, void *arg) | |
3217 { | |
3218 Py_VISIT(((PyObject *)(self->buf))); | |
3219 return 0; | |
3220 } | |
3221 | |
3222 static int | |
3223 RangeClear(RangeObject *self) | |
3224 { | |
3225 Py_CLEAR(self->buf); | |
3226 return 0; | |
4319 | 3227 } |
3228 | |
4385 | 3229 static PyInt |
4488 | 3230 RangeLength(RangeObject *self) |
4385 | 3231 { |
3232 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 3233 if (CheckBuffer(self->buf)) |
4385 | 3234 return -1; /* ??? */ |
3235 | |
4488 | 3236 return (self->end - self->start + 1); |
4385 | 3237 } |
3238 | |
3239 static PyObject * | |
4488 | 3240 RangeItem(RangeObject *self, PyInt n) |
4385 | 3241 { |
4488 | 3242 return RBItem(self->buf, n, self->start, self->end); |
4385 | 3243 } |
3244 | |
3245 static PyObject * | |
4488 | 3246 RangeSlice(RangeObject *self, PyInt lo, PyInt hi) |
4385 | 3247 { |
4488 | 3248 return RBSlice(self->buf, lo, hi, self->start, self->end); |
4385 | 3249 } |
3250 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3251 static char *RangeAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3252 "start", "end", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3253 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3254 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3255 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3256 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3257 RangeDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3258 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3259 return ObjectDir(self, RangeAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3260 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3261 |
4385 | 3262 static PyObject * |
4488 | 3263 RangeAppend(RangeObject *self, PyObject *args) |
4385 | 3264 { |
4488 | 3265 return RBAppend(self->buf, args, self->start, self->end, &self->end); |
4385 | 3266 } |
3267 | |
3268 static PyObject * | |
4488 | 3269 RangeRepr(RangeObject *self) |
4385 | 3270 { |
3271 static char repr[100]; | |
4488 | 3272 |
3273 if (self->buf->buf == INVALID_BUFFER_VALUE) | |
4385 | 3274 { |
3275 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", | |
3276 (self)); | |
3277 return PyString_FromString(repr); | |
3278 } | |
3279 else | |
3280 { | |
4488 | 3281 char *name = (char *)self->buf->buf->b_fname; |
4385 | 3282 int len; |
3283 | |
3284 if (name == NULL) | |
3285 name = ""; | |
3286 len = (int)strlen(name); | |
3287 | |
3288 if (len > 45) | |
3289 name = name + (45 - len); | |
3290 | |
3291 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", | |
3292 len > 45 ? "..." : "", name, | |
4488 | 3293 self->start, self->end); |
4385 | 3294 |
3295 return PyString_FromString(repr); | |
3296 } | |
3297 } | |
3298 | |
3299 static struct PyMethodDef RangeMethods[] = { | |
4492 | 3300 /* name, function, calling, documentation */ |
3301 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" }, | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3302 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3303 { NULL, NULL, 0, NULL} |
4385 | 3304 }; |
3305 | |
4319 | 3306 static PyTypeObject BufferType; |
3307 static PySequenceMethods BufferAsSeq; | |
3308 static PyMappingMethods BufferAsMapping; | |
3309 | |
3310 static PyObject * | |
4377 | 3311 BufferNew(buf_T *buf) |
3312 { | |
3313 /* We need to handle deletion of buffers underneath us. | |
3314 * If we add a "b_python*_ref" field to the buf_T structure, | |
3315 * then we can get at it in buf_freeall() in vim. We then | |
3316 * need to create only ONE Python object per buffer - if | |
3317 * we try to create a second, just INCREF the existing one | |
3318 * and return it. The (single) Python object referring to | |
3319 * the buffer is stored in "b_python*_ref". | |
3320 * Question: what to do on a buf_freeall(). We'll probably | |
3321 * have to either delete the Python object (DECREF it to | |
3322 * zero - a bad idea, as it leaves dangling refs!) or | |
3323 * set the buf_T * value to an invalid value (-1?), which | |
3324 * means we need checks in all access functions... Bah. | |
3325 * | |
4385 | 3326 * Python2 and Python3 get different fields and different objects: |
4377 | 3327 * b_python_ref and b_python3_ref fields respectively. |
3328 */ | |
3329 | |
3330 BufferObject *self; | |
3331 | |
3332 if (BUF_PYTHON_REF(buf) != NULL) | |
3333 { | |
3334 self = BUF_PYTHON_REF(buf); | |
3335 Py_INCREF(self); | |
3336 } | |
3337 else | |
3338 { | |
3339 self = PyObject_NEW(BufferObject, &BufferType); | |
3340 if (self == NULL) | |
3341 return NULL; | |
3342 self->buf = buf; | |
3343 BUF_PYTHON_REF(buf) = self; | |
3344 } | |
3345 | |
3346 return (PyObject *)(self); | |
3347 } | |
3348 | |
4385 | 3349 static void |
4488 | 3350 BufferDestructor(BufferObject *self) |
4319 | 3351 { |
4488 | 3352 if (self->buf && self->buf != INVALID_BUFFER_VALUE) |
3353 BUF_PYTHON_REF(self->buf) = NULL; | |
4385 | 3354 |
3355 DESTRUCTOR_FINISH(self); | |
4319 | 3356 } |
3357 | |
4377 | 3358 static PyInt |
4488 | 3359 BufferLength(BufferObject *self) |
4377 | 3360 { |
3361 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 3362 if (CheckBuffer(self)) |
4377 | 3363 return -1; /* ??? */ |
3364 | |
4488 | 3365 return (PyInt)(self->buf->b_ml.ml_line_count); |
4377 | 3366 } |
3367 | |
3368 static PyObject * | |
4488 | 3369 BufferItem(BufferObject *self, PyInt n) |
4377 | 3370 { |
4488 | 3371 return RBItem(self, n, 1, -1); |
4377 | 3372 } |
3373 | |
3374 static PyObject * | |
4488 | 3375 BufferSlice(BufferObject *self, PyInt lo, PyInt hi) |
4377 | 3376 { |
4488 | 3377 return RBSlice(self, lo, hi, 1, -1); |
4377 | 3378 } |
3379 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3380 static char *BufferAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3381 "name", "number", "vars", "options", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3382 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3383 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3384 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3385 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3386 BufferDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3387 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3388 return ObjectDir(self, BufferAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3389 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3390 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3391 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3392 BufferAttrValid(BufferObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3393 { |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3394 PyObject *r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3395 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3396 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3397 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3398 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3399 r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3400 Py_INCREF(r); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3401 return r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3402 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3403 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3404 static PyObject * |
4488 | 3405 BufferAttr(BufferObject *self, char *name) |
4385 | 3406 { |
3407 if (strcmp(name, "name") == 0) | |
4597
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
3408 return PyString_FromString((self->buf->b_ffname == NULL |
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
3409 ? "" : (char *) self->buf->b_ffname)); |
4385 | 3410 else if (strcmp(name, "number") == 0) |
4488 | 3411 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum); |
4385 | 3412 else if (strcmp(name, "vars") == 0) |
4488 | 3413 return DictionaryNew(self->buf->b_vars); |
4385 | 3414 else if (strcmp(name, "options") == 0) |
4488 | 3415 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer, |
3416 (PyObject *) self); | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3417 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3418 return ObjectDir(NULL, BufferAttrs); |
4385 | 3419 else |
3420 return NULL; | |
3421 } | |
3422 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3423 static int |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3424 BufferSetattr(BufferObject *self, char *name, PyObject *valObject) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3425 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3426 if (CheckBuffer(self)) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3427 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3428 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3429 if (strcmp(name, "name") == 0) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3430 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3431 char_u *val; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3432 aco_save_T aco; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3433 int r; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3434 PyObject *todecref; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3435 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3436 if (!(val = StringToChars(valObject, &todecref))) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3437 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3438 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3439 VimTryStart(); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3440 /* Using aucmd_*: autocommands will be executed by rename_buffer */ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3441 aucmd_prepbuf(&aco, self->buf); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3442 r = rename_buffer(val); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3443 aucmd_restbuf(&aco); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3444 Py_XDECREF(todecref); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3445 if (VimTryEnd()) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3446 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3447 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3448 if (r == FAIL) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3449 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3450 PyErr_SetVim(_("failed to rename buffer")); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3451 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3452 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3453 return 0; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3454 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3455 else |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3456 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3457 PyErr_SetString(PyExc_AttributeError, name); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3458 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3459 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3460 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3461 |
4385 | 3462 static PyObject * |
4488 | 3463 BufferAppend(BufferObject *self, PyObject *args) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3464 { |
4488 | 3465 return RBAppend(self, args, 1, -1, NULL); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3466 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3467 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3468 static PyObject * |
4488 | 3469 BufferMark(BufferObject *self, PyObject *args) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3470 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3471 pos_T *posp; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3472 char *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3473 char mark; |
4429 | 3474 buf_T *savebuf; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3475 |
4488 | 3476 if (CheckBuffer(self)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3477 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3478 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3479 if (!PyArg_ParseTuple(args, "s", &pmark)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3480 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3481 mark = *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3482 |
4498 | 3483 VimTryStart(); |
4488 | 3484 switch_buffer(&savebuf, self->buf); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3485 posp = getmark(mark, FALSE); |
4429 | 3486 restore_buffer(savebuf); |
4498 | 3487 if (VimTryEnd()) |
3488 return NULL; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3489 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3490 if (posp == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3491 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3492 PyErr_SetVim(_("invalid mark name")); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3493 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3494 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3495 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3496 if (posp->lnum <= 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3497 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3498 /* Or raise an error? */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3499 Py_INCREF(Py_None); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3500 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3501 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3502 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3503 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3504 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3505 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3506 static PyObject * |
4488 | 3507 BufferRange(BufferObject *self, PyObject *args) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3508 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3509 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3510 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3511 |
4488 | 3512 if (CheckBuffer(self)) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3513 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3514 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3515 if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3516 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3517 |
4488 | 3518 return RangeNew(self->buf, start, end); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3519 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3520 |
4319 | 3521 static PyObject * |
4488 | 3522 BufferRepr(BufferObject *self) |
4319 | 3523 { |
3524 static char repr[100]; | |
4488 | 3525 |
3526 if (self->buf == INVALID_BUFFER_VALUE) | |
4319 | 3527 { |
3528 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); | |
3529 return PyString_FromString(repr); | |
3530 } | |
3531 else | |
3532 { | |
4488 | 3533 char *name = (char *)self->buf->b_fname; |
4319 | 3534 PyInt len; |
3535 | |
3536 if (name == NULL) | |
3537 name = ""; | |
3538 len = strlen(name); | |
3539 | |
3540 if (len > 35) | |
3541 name = name + (35 - len); | |
3542 | |
3543 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); | |
3544 | |
3545 return PyString_FromString(repr); | |
3546 } | |
3547 } | |
3548 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3549 static struct PyMethodDef BufferMethods[] = { |
4492 | 3550 /* name, function, calling, documentation */ |
3551 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, | |
3552 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" }, | |
3553 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3554 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3555 { NULL, NULL, 0, NULL} |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3556 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3557 |
4397 | 3558 /* |
3559 * Buffer list object - Implementation | |
3560 */ | |
3561 | |
3562 static PyTypeObject BufMapType; | |
3563 | |
3564 typedef struct | |
3565 { | |
3566 PyObject_HEAD | |
3567 } BufMapObject; | |
3568 | |
3569 static PyInt | |
3570 BufMapLength(PyObject *self UNUSED) | |
3571 { | |
3572 buf_T *b = firstbuf; | |
3573 PyInt n = 0; | |
3574 | |
3575 while (b) | |
3576 { | |
3577 ++n; | |
3578 b = b->b_next; | |
3579 } | |
3580 | |
3581 return n; | |
3582 } | |
3583 | |
3584 static PyObject * | |
3585 BufMapItem(PyObject *self UNUSED, PyObject *keyObject) | |
3586 { | |
3587 buf_T *b; | |
3588 int bnr; | |
3589 | |
3590 #if PY_MAJOR_VERSION < 3 | |
3591 if (PyInt_Check(keyObject)) | |
3592 bnr = PyInt_AsLong(keyObject); | |
3593 else | |
3594 #endif | |
3595 if (PyLong_Check(keyObject)) | |
3596 bnr = PyLong_AsLong(keyObject); | |
3597 else | |
3598 { | |
4405 | 3599 PyErr_SetString(PyExc_TypeError, _("key must be integer")); |
4397 | 3600 return NULL; |
3601 } | |
3602 | |
3603 b = buflist_findnr(bnr); | |
3604 | |
3605 if (b) | |
3606 return BufferNew(b); | |
3607 else | |
3608 { | |
4403 | 3609 PyErr_SetObject(PyExc_KeyError, keyObject); |
4397 | 3610 return NULL; |
3611 } | |
3612 } | |
3613 | |
3614 static void | |
3615 BufMapIterDestruct(PyObject *buffer) | |
3616 { | |
3617 /* Iteration was stopped before all buffers were processed */ | |
3618 if (buffer) | |
3619 { | |
3620 Py_DECREF(buffer); | |
3621 } | |
3622 } | |
3623 | |
4433 | 3624 static int |
3625 BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg) | |
3626 { | |
4500 | 3627 if (buffer) |
3628 Py_VISIT(buffer); | |
4433 | 3629 return 0; |
3630 } | |
3631 | |
3632 static int | |
3633 BufMapIterClear(PyObject **buffer) | |
3634 { | |
4500 | 3635 if (*buffer) |
3636 Py_CLEAR(*buffer); | |
4433 | 3637 return 0; |
3638 } | |
3639 | |
4397 | 3640 static PyObject * |
3641 BufMapIterNext(PyObject **buffer) | |
3642 { | |
3643 PyObject *next; | |
3644 PyObject *r; | |
3645 | |
3646 if (!*buffer) | |
3647 return NULL; | |
3648 | |
3649 r = *buffer; | |
3650 | |
3651 if (CheckBuffer((BufferObject *)(r))) | |
3652 { | |
3653 *buffer = NULL; | |
3654 return NULL; | |
3655 } | |
3656 | |
3657 if (!((BufferObject *)(r))->buf->b_next) | |
3658 next = NULL; | |
3659 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next))) | |
3660 return NULL; | |
3661 *buffer = next; | |
4438 | 3662 /* Do not increment reference: we no longer hold it (decref), but whoever |
3663 * on other side will hold (incref). Decref+incref = nothing. */ | |
4397 | 3664 return r; |
3665 } | |
3666 | |
3667 static PyObject * | |
3668 BufMapIter(PyObject *self UNUSED) | |
3669 { | |
3670 PyObject *buffer; | |
3671 | |
3672 buffer = BufferNew(firstbuf); | |
3673 return IterNew(buffer, | |
4433 | 3674 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext, |
3675 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear); | |
4397 | 3676 } |
3677 | |
3678 static PyMappingMethods BufMapAsMapping = { | |
3679 (lenfunc) BufMapLength, | |
3680 (binaryfunc) BufMapItem, | |
3681 (objobjargproc) 0, | |
3682 }; | |
3683 | |
4385 | 3684 /* Current items object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3685 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3686 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3687 static char *CurrentAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3688 "buffer", "window", "line", "range", "tabpage", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3689 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3690 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3691 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3692 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3693 CurrentDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3694 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3695 return ObjectDir(self, CurrentAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3696 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3697 |
4319 | 3698 static PyObject * |
3699 CurrentGetattr(PyObject *self UNUSED, char *name) | |
3700 { | |
3701 if (strcmp(name, "buffer") == 0) | |
3702 return (PyObject *)BufferNew(curbuf); | |
3703 else if (strcmp(name, "window") == 0) | |
4431 | 3704 return (PyObject *)WindowNew(curwin, curtab); |
4401 | 3705 else if (strcmp(name, "tabpage") == 0) |
3706 return (PyObject *)TabPageNew(curtab); | |
4319 | 3707 else if (strcmp(name, "line") == 0) |
3708 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); | |
3709 else if (strcmp(name, "range") == 0) | |
3710 return RangeNew(curbuf, RangeStart, RangeEnd); | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3711 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3712 return ObjectDir(NULL, CurrentAttrs); |
4319 | 3713 else |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3714 #if PY_MAJOR_VERSION < 3 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3715 return Py_FindMethod(WindowMethods, self, name); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3716 #else |
4319 | 3717 return NULL; |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3718 #endif |
4319 | 3719 } |
3720 | |
3721 static int | |
3722 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) | |
3723 { | |
3724 if (strcmp(name, "line") == 0) | |
3725 { | |
3726 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) | |
3727 return -1; | |
3728 | |
3729 return 0; | |
3730 } | |
4407 | 3731 else if (strcmp(name, "buffer") == 0) |
3732 { | |
3733 int count; | |
3734 | |
3735 if (value->ob_type != &BufferType) | |
3736 { | |
3737 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object")); | |
3738 return -1; | |
3739 } | |
3740 | |
3741 if (CheckBuffer((BufferObject *)(value))) | |
3742 return -1; | |
3743 count = ((BufferObject *)(value))->buf->b_fnum; | |
3744 | |
4498 | 3745 VimTryStart(); |
4407 | 3746 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) |
3747 { | |
4498 | 3748 if (VimTryEnd()) |
3749 return -1; | |
4407 | 3750 PyErr_SetVim(_("failed to switch to given buffer")); |
3751 return -1; | |
3752 } | |
3753 | |
4498 | 3754 return VimTryEnd(); |
4407 | 3755 } |
3756 else if (strcmp(name, "window") == 0) | |
3757 { | |
3758 int count; | |
3759 | |
3760 if (value->ob_type != &WindowType) | |
3761 { | |
3762 PyErr_SetString(PyExc_TypeError, _("expected vim.window object")); | |
3763 return -1; | |
3764 } | |
3765 | |
3766 if (CheckWindow((WindowObject *)(value))) | |
3767 return -1; | |
3768 count = get_win_number(((WindowObject *)(value))->win, firstwin); | |
3769 | |
3770 if (!count) | |
3771 { | |
3772 PyErr_SetString(PyExc_ValueError, | |
3773 _("failed to find window in the current tab page")); | |
3774 return -1; | |
3775 } | |
3776 | |
4498 | 3777 VimTryStart(); |
4407 | 3778 win_goto(((WindowObject *)(value))->win); |
3779 if (((WindowObject *)(value))->win != curwin) | |
3780 { | |
4498 | 3781 if (VimTryEnd()) |
3782 return -1; | |
4407 | 3783 PyErr_SetString(PyExc_RuntimeError, |
3784 _("did not switch to the specified window")); | |
3785 return -1; | |
3786 } | |
3787 | |
4498 | 3788 return VimTryEnd(); |
4407 | 3789 } |
3790 else if (strcmp(name, "tabpage") == 0) | |
3791 { | |
3792 if (value->ob_type != &TabPageType) | |
3793 { | |
3794 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object")); | |
3795 return -1; | |
3796 } | |
3797 | |
3798 if (CheckTabPage((TabPageObject *)(value))) | |
3799 return -1; | |
3800 | |
4498 | 3801 VimTryStart(); |
4407 | 3802 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE); |
3803 if (((TabPageObject *)(value))->tab != curtab) | |
3804 { | |
4498 | 3805 if (VimTryEnd()) |
3806 return -1; | |
4407 | 3807 PyErr_SetString(PyExc_RuntimeError, |
3808 _("did not switch to the specified tab page")); | |
3809 return -1; | |
3810 } | |
3811 | |
4498 | 3812 return VimTryEnd(); |
4407 | 3813 } |
4319 | 3814 else |
3815 { | |
3816 PyErr_SetString(PyExc_AttributeError, name); | |
3817 return -1; | |
3818 } | |
3819 } | |
3820 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3821 static struct PyMethodDef CurrentMethods[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3822 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3823 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3824 { NULL, NULL, 0, NULL} |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3825 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3826 |
3618 | 3827 static void |
4486 | 3828 init_range_cmd(exarg_T *eap) |
3829 { | |
3830 RangeStart = eap->line1; | |
3831 RangeEnd = eap->line2; | |
3832 } | |
3833 | |
3834 static void | |
3835 init_range_eval(typval_T *rettv UNUSED) | |
3836 { | |
3837 RangeStart = (PyInt) curwin->w_cursor.lnum; | |
3838 RangeEnd = RangeStart; | |
3839 } | |
3840 | |
3841 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3842 run_cmd(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3843 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3844 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3845 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3846 ) |
4486 | 3847 { |
3848 PyRun_SimpleString((char *) cmd); | |
3849 } | |
3850 | |
3851 static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n "; | |
3852 static int code_hdr_len = 30; | |
3853 | |
3854 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3855 run_do(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3856 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3857 , PyGILState_STATE *pygilstate |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3858 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3859 ) |
4486 | 3860 { |
3861 PyInt lnum; | |
3862 size_t len; | |
3863 char *code; | |
3864 int status; | |
3865 PyObject *pyfunc, *pymain; | |
3866 | |
4575
626e9ccb7c48
updated for version 7.3.1035
Bram Moolenaar <bram@vim.org>
parents:
4523
diff
changeset
|
3867 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK) |
4486 | 3868 { |
3869 EMSG(_("cannot save undo information")); | |
3870 return; | |
3871 } | |
3872 | |
3873 len = code_hdr_len + STRLEN(cmd); | |
3874 code = PyMem_New(char, len + 1); | |
3875 memcpy(code, code_hdr, code_hdr_len); | |
3876 STRCPY(code + code_hdr_len, cmd); | |
3877 status = PyRun_SimpleString(code); | |
3878 PyMem_Free(code); | |
3879 | |
3880 if (status) | |
3881 { | |
3882 EMSG(_("failed to run the code")); | |
3883 return; | |
3884 } | |
3885 | |
3886 status = 0; | |
3887 pymain = PyImport_AddModule("__main__"); | |
3888 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3889 #ifdef PY_CAN_RECURSE |
4486 | 3890 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3891 #endif |
4486 | 3892 |
3893 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) | |
3894 { | |
3895 PyObject *line, *linenr, *ret; | |
3896 | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3897 #ifdef PY_CAN_RECURSE |
4486 | 3898 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3899 #endif |
4486 | 3900 if (!(line = GetBufferLine(curbuf, lnum))) |
3901 goto err; | |
3902 if (!(linenr = PyInt_FromLong((long) lnum))) | |
3903 { | |
3904 Py_DECREF(line); | |
3905 goto err; | |
3906 } | |
3907 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL); | |
3908 Py_DECREF(line); | |
3909 Py_DECREF(linenr); | |
3910 if (!ret) | |
3911 goto err; | |
3912 | |
3913 if (ret != Py_None) | |
3914 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL) | |
3915 goto err; | |
3916 | |
3917 Py_XDECREF(ret); | |
3918 PythonIO_Flush(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3919 #ifdef PY_CAN_RECURSE |
4486 | 3920 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3921 #endif |
4486 | 3922 } |
3923 goto out; | |
3924 err: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3925 #ifdef PY_CAN_RECURSE |
4486 | 3926 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3927 #endif |
4486 | 3928 PyErr_PrintEx(0); |
3929 PythonIO_Flush(); | |
3930 status = 1; | |
3931 out: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3932 #ifdef PY_CAN_RECURSE |
4486 | 3933 if (!status) |
3934 *pygilstate = PyGILState_Ensure(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3935 #endif |
4486 | 3936 Py_DECREF(pyfunc); |
3937 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL); | |
3938 if (status) | |
3939 return; | |
3940 check_cursor(); | |
3941 update_curbuf(NOT_VALID); | |
3942 } | |
3943 | |
3944 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3945 run_eval(const char *cmd, typval_T *rettv |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3946 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3947 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3948 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3949 ) |
4486 | 3950 { |
3951 PyObject *r; | |
3952 | |
3953 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals); | |
3954 if (r == NULL) | |
3955 { | |
3956 if (PyErr_Occurred() && !msg_silent) | |
3957 PyErr_PrintEx(0); | |
3958 EMSG(_("E858: Eval did not return a valid python object")); | |
3959 } | |
3960 else | |
3961 { | |
3962 if (ConvertFromPyObject(r, rettv) == -1) | |
3963 EMSG(_("E859: Failed to convert returned python object to vim value")); | |
3964 Py_DECREF(r); | |
3965 } | |
3966 PyErr_Clear(); | |
3967 } | |
3968 | |
3969 static void | |
3618 | 3970 set_ref_in_py(const int copyID) |
3971 { | |
3972 pylinkedlist_T *cur; | |
3973 dict_T *dd; | |
3974 list_T *ll; | |
3975 | |
3976 if (lastdict != NULL) | |
3977 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) | |
3978 { | |
3979 dd = ((DictionaryObject *) (cur->pll_obj))->dict; | |
3980 if (dd->dv_copyID != copyID) | |
3981 { | |
3982 dd->dv_copyID = copyID; | |
3983 set_ref_in_ht(&dd->dv_hashtab, copyID); | |
3984 } | |
3985 } | |
3986 | |
3987 if (lastlist != NULL) | |
3988 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) | |
3989 { | |
3990 ll = ((ListObject *) (cur->pll_obj))->list; | |
3991 if (ll->lv_copyID != copyID) | |
3992 { | |
3993 ll->lv_copyID = copyID; | |
3994 set_ref_in_list(ll, copyID); | |
3995 } | |
3996 } | |
3997 } | |
3998 | |
3999 static int | |
4000 set_string_copy(char_u *str, typval_T *tv) | |
4001 { | |
4002 tv->vval.v_string = vim_strsave(str); | |
4003 if (tv->vval.v_string == NULL) | |
4004 { | |
4005 PyErr_NoMemory(); | |
4006 return -1; | |
4007 } | |
4008 return 0; | |
4009 } | |
4010 | |
4385 | 4011 static int |
4012 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
4013 { | |
4014 dict_T *d; | |
4015 char_u *key; | |
4016 dictitem_T *di; | |
4017 PyObject *keyObject; | |
4018 PyObject *valObject; | |
4019 Py_ssize_t iter = 0; | |
4020 | |
4021 d = dict_alloc(); | |
4022 if (d == NULL) | |
4023 { | |
4024 PyErr_NoMemory(); | |
4025 return -1; | |
4026 } | |
4027 | |
4028 tv->v_type = VAR_DICT; | |
4029 tv->vval.v_dict = d; | |
4030 | |
4031 while (PyDict_Next(obj, &iter, &keyObject, &valObject)) | |
4032 { | |
4033 DICTKEY_DECL | |
4034 | |
4035 if (keyObject == NULL) | |
4036 return -1; | |
4037 if (valObject == NULL) | |
4038 return -1; | |
4039 | |
4040 DICTKEY_GET_NOTEMPTY(-1) | |
4041 | |
4042 di = dictitem_alloc(key); | |
4043 | |
4044 DICTKEY_UNREF | |
4045 | |
4046 if (di == NULL) | |
4047 { | |
4048 PyErr_NoMemory(); | |
4049 return -1; | |
4050 } | |
4051 di->di_tv.v_lock = 0; | |
4052 | |
4053 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
4054 { | |
4055 vim_free(di); | |
4056 return -1; | |
4057 } | |
4058 if (dict_add(d, di) == FAIL) | |
4059 { | |
4060 vim_free(di); | |
4061 PyErr_SetVim(_("failed to add key to dictionary")); | |
4062 return -1; | |
4063 } | |
4064 } | |
4065 return 0; | |
4066 } | |
4067 | |
4068 static int | |
4069 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
4070 { | |
4071 dict_T *d; | |
4072 char_u *key; | |
4073 dictitem_T *di; | |
4074 PyObject *list; | |
4075 PyObject *litem; | |
4076 PyObject *keyObject; | |
4077 PyObject *valObject; | |
4078 Py_ssize_t lsize; | |
4079 | |
4080 d = dict_alloc(); | |
4081 if (d == NULL) | |
4082 { | |
4083 PyErr_NoMemory(); | |
4084 return -1; | |
4085 } | |
4086 | |
4087 tv->v_type = VAR_DICT; | |
4088 tv->vval.v_dict = d; | |
4089 | |
4090 list = PyMapping_Items(obj); | |
4091 if (list == NULL) | |
4092 return -1; | |
4093 lsize = PyList_Size(list); | |
4094 while (lsize--) | |
4095 { | |
4096 DICTKEY_DECL | |
4097 | |
4098 litem = PyList_GetItem(list, lsize); | |
4099 if (litem == NULL) | |
4100 { | |
4101 Py_DECREF(list); | |
4102 return -1; | |
4103 } | |
4104 | |
4105 keyObject = PyTuple_GetItem(litem, 0); | |
4106 if (keyObject == NULL) | |
4107 { | |
4108 Py_DECREF(list); | |
4109 Py_DECREF(litem); | |
4110 return -1; | |
4111 } | |
4112 | |
4113 DICTKEY_GET_NOTEMPTY(-1) | |
4114 | |
4115 valObject = PyTuple_GetItem(litem, 1); | |
4116 if (valObject == NULL) | |
4117 { | |
4118 Py_DECREF(list); | |
4119 Py_DECREF(litem); | |
4120 return -1; | |
4121 } | |
4122 | |
4123 di = dictitem_alloc(key); | |
4124 | |
4125 DICTKEY_UNREF | |
4126 | |
4127 if (di == NULL) | |
4128 { | |
4129 Py_DECREF(list); | |
4130 Py_DECREF(litem); | |
4131 PyErr_NoMemory(); | |
4132 return -1; | |
4133 } | |
4134 di->di_tv.v_lock = 0; | |
4135 | |
4136 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) | |
4137 { | |
4138 vim_free(di); | |
4139 Py_DECREF(list); | |
4140 Py_DECREF(litem); | |
4141 return -1; | |
4142 } | |
4143 if (dict_add(d, di) == FAIL) | |
4144 { | |
4145 vim_free(di); | |
4146 Py_DECREF(list); | |
4147 Py_DECREF(litem); | |
4148 PyErr_SetVim(_("failed to add key to dictionary")); | |
4149 return -1; | |
4150 } | |
4151 Py_DECREF(litem); | |
4152 } | |
4153 Py_DECREF(list); | |
4154 return 0; | |
4155 } | |
4156 | |
4157 static int | |
4158 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
4159 { | |
4160 list_T *l; | |
4161 | |
4162 l = list_alloc(); | |
4163 if (l == NULL) | |
4164 { | |
4165 PyErr_NoMemory(); | |
4166 return -1; | |
4167 } | |
4168 | |
4169 tv->v_type = VAR_LIST; | |
4170 tv->vval.v_list = l; | |
4171 | |
4172 if (list_py_concat(l, obj, lookupDict) == -1) | |
4173 return -1; | |
4174 | |
4175 return 0; | |
4176 } | |
4177 | |
4178 static int | |
4179 pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
4180 { | |
4181 PyObject *iterator = PyObject_GetIter(obj); | |
4182 PyObject *item; | |
4183 list_T *l; | |
4184 listitem_T *li; | |
4185 | |
4186 l = list_alloc(); | |
4187 | |
4188 if (l == NULL) | |
4189 { | |
4190 PyErr_NoMemory(); | |
4191 return -1; | |
4192 } | |
4193 | |
4194 tv->vval.v_list = l; | |
4195 tv->v_type = VAR_LIST; | |
4196 | |
4197 | |
4198 if (iterator == NULL) | |
4199 return -1; | |
4200 | |
4201 while ((item = PyIter_Next(obj))) | |
4202 { | |
4203 li = listitem_alloc(); | |
4204 if (li == NULL) | |
4205 { | |
4206 PyErr_NoMemory(); | |
4207 return -1; | |
4208 } | |
4209 li->li_tv.v_lock = 0; | |
4210 | |
4211 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1) | |
4212 return -1; | |
4213 | |
4214 list_append(l, li); | |
4215 | |
4216 Py_DECREF(item); | |
4217 } | |
4218 | |
4219 Py_DECREF(iterator); | |
4220 return 0; | |
4221 } | |
4222 | |
3618 | 4223 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
4224 | |
4225 static int | |
4226 convert_dl(PyObject *obj, typval_T *tv, | |
4227 pytotvfunc py_to_tv, PyObject *lookupDict) | |
4228 { | |
4229 PyObject *capsule; | |
4230 char hexBuf[sizeof(void *) * 2 + 3]; | |
4231 | |
4232 sprintf(hexBuf, "%p", obj); | |
4233 | |
3638 | 4234 # ifdef PY_USE_CAPSULE |
3618 | 4235 capsule = PyDict_GetItemString(lookupDict, hexBuf); |
3648 | 4236 # else |
4237 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf); | |
4238 # endif | |
3618 | 4239 if (capsule == NULL) |
4240 { | |
3638 | 4241 # ifdef PY_USE_CAPSULE |
3618 | 4242 capsule = PyCapsule_New(tv, NULL, NULL); |
3648 | 4243 # else |
4244 capsule = PyCObject_FromVoidPtr(tv, NULL); | |
4245 # endif | |
3618 | 4246 PyDict_SetItemString(lookupDict, hexBuf, capsule); |
4247 Py_DECREF(capsule); | |
4248 if (py_to_tv(obj, tv, lookupDict) == -1) | |
4249 { | |
4250 tv->v_type = VAR_UNKNOWN; | |
4251 return -1; | |
4252 } | |
4253 /* As we are not using copy_tv which increments reference count we must | |
4254 * do it ourself. */ | |
4255 switch(tv->v_type) | |
4256 { | |
4257 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; | |
4258 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; | |
4259 } | |
4260 } | |
4261 else | |
4262 { | |
3638 | 4263 typval_T *v; |
4264 | |
4265 # ifdef PY_USE_CAPSULE | |
4266 v = PyCapsule_GetPointer(capsule, NULL); | |
4267 # else | |
3648 | 4268 v = PyCObject_AsVoidPtr(capsule); |
3638 | 4269 # endif |
3618 | 4270 copy_tv(v, tv); |
4271 } | |
4272 return 0; | |
4273 } | |
4274 | |
4275 static int | |
4276 ConvertFromPyObject(PyObject *obj, typval_T *tv) | |
4277 { | |
4278 PyObject *lookup_dict; | |
4279 int r; | |
4280 | |
4281 lookup_dict = PyDict_New(); | |
4282 r = _ConvertFromPyObject(obj, tv, lookup_dict); | |
4283 Py_DECREF(lookup_dict); | |
4284 return r; | |
4285 } | |
4286 | |
4287 static int | |
4288 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict) | |
4289 { | |
4290 if (obj->ob_type == &DictionaryType) | |
4291 { | |
4292 tv->v_type = VAR_DICT; | |
4293 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); | |
4294 ++tv->vval.v_dict->dv_refcount; | |
4295 } | |
4296 else if (obj->ob_type == &ListType) | |
4297 { | |
4298 tv->v_type = VAR_LIST; | |
4299 tv->vval.v_list = (((ListObject *)(obj))->list); | |
4300 ++tv->vval.v_list->lv_refcount; | |
4301 } | |
4302 else if (obj->ob_type == &FunctionType) | |
4303 { | |
4304 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) | |
4305 return -1; | |
4306 | |
4307 tv->v_type = VAR_FUNC; | |
4308 func_ref(tv->vval.v_string); | |
4309 } | |
4310 else if (PyBytes_Check(obj)) | |
4311 { | |
3800 | 4312 char_u *result; |
4313 | |
4314 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) | |
4315 return -1; | |
3618 | 4316 if (result == NULL) |
4317 return -1; | |
4318 | |
4319 if (set_string_copy(result, tv) == -1) | |
4320 return -1; | |
4321 | |
4322 tv->v_type = VAR_STRING; | |
4323 } | |
4324 else if (PyUnicode_Check(obj)) | |
4325 { | |
4326 PyObject *bytes; | |
4327 char_u *result; | |
4328 | |
4329 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); | |
4330 if (bytes == NULL) | |
4331 return -1; | |
4332 | |
3800 | 4333 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
4334 return -1; | |
3618 | 4335 if (result == NULL) |
4336 return -1; | |
4337 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4338 if (set_string_copy(result, tv)) |
3618 | 4339 { |
4340 Py_XDECREF(bytes); | |
4341 return -1; | |
4342 } | |
4343 Py_XDECREF(bytes); | |
4344 | |
4345 tv->v_type = VAR_STRING; | |
4346 } | |
4321 | 4347 #if PY_MAJOR_VERSION < 3 |
3618 | 4348 else if (PyInt_Check(obj)) |
4349 { | |
4350 tv->v_type = VAR_NUMBER; | |
4351 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); | |
4352 } | |
4353 #endif | |
4354 else if (PyLong_Check(obj)) | |
4355 { | |
4356 tv->v_type = VAR_NUMBER; | |
4357 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); | |
4358 } | |
4359 else if (PyDict_Check(obj)) | |
4360 return convert_dl(obj, tv, pydict_to_tv, lookupDict); | |
4361 #ifdef FEAT_FLOAT | |
4362 else if (PyFloat_Check(obj)) | |
4363 { | |
4364 tv->v_type = VAR_FLOAT; | |
4365 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); | |
4366 } | |
4367 #endif | |
4368 else if (PyIter_Check(obj)) | |
4369 return convert_dl(obj, tv, pyiter_to_tv, lookupDict); | |
4370 else if (PySequence_Check(obj)) | |
4371 return convert_dl(obj, tv, pyseq_to_tv, lookupDict); | |
4372 else if (PyMapping_Check(obj)) | |
4373 return convert_dl(obj, tv, pymap_to_tv, lookupDict); | |
4374 else | |
4375 { | |
4405 | 4376 PyErr_SetString(PyExc_TypeError, |
4377 _("unable to convert to vim structure")); | |
3618 | 4378 return -1; |
4379 } | |
4380 return 0; | |
4381 } | |
4382 | |
4383 static PyObject * | |
4384 ConvertToPyObject(typval_T *tv) | |
4385 { | |
4386 if (tv == NULL) | |
4387 { | |
4388 PyErr_SetVim(_("NULL reference passed")); | |
4389 return NULL; | |
4390 } | |
4391 switch (tv->v_type) | |
4392 { | |
4393 case VAR_STRING: | |
3852 | 4394 return PyBytes_FromString(tv->vval.v_string == NULL |
4395 ? "" : (char *)tv->vval.v_string); | |
3618 | 4396 case VAR_NUMBER: |
4397 return PyLong_FromLong((long) tv->vval.v_number); | |
4398 #ifdef FEAT_FLOAT | |
4399 case VAR_FLOAT: | |
4400 return PyFloat_FromDouble((double) tv->vval.v_float); | |
4401 #endif | |
4402 case VAR_LIST: | |
4403 return ListNew(tv->vval.v_list); | |
4404 case VAR_DICT: | |
4405 return DictionaryNew(tv->vval.v_dict); | |
4406 case VAR_FUNC: | |
3852 | 4407 return FunctionNew(tv->vval.v_string == NULL |
4408 ? (char_u *)"" : tv->vval.v_string); | |
3618 | 4409 case VAR_UNKNOWN: |
4410 Py_INCREF(Py_None); | |
4411 return Py_None; | |
4412 default: | |
4413 PyErr_SetVim(_("internal error: invalid value type")); | |
4414 return NULL; | |
4415 } | |
4416 } | |
4319 | 4417 |
4418 typedef struct | |
4419 { | |
4420 PyObject_HEAD | |
4421 } CurrentObject; | |
4422 static PyTypeObject CurrentType; | |
4423 | |
4424 static void | |
4425 init_structs(void) | |
4426 { | |
4427 vim_memset(&OutputType, 0, sizeof(OutputType)); | |
4428 OutputType.tp_name = "vim.message"; | |
4429 OutputType.tp_basicsize = sizeof(OutputObject); | |
4430 OutputType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4431 OutputType.tp_doc = "vim message object"; | |
4432 OutputType.tp_methods = OutputMethods; | |
4433 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4434 OutputType.tp_getattro = (getattrofunc)OutputGetattro; |
4435 OutputType.tp_setattro = (setattrofunc)OutputSetattro; | |
4319 | 4436 OutputType.tp_alloc = call_PyType_GenericAlloc; |
4437 OutputType.tp_new = call_PyType_GenericNew; | |
4438 OutputType.tp_free = call_PyObject_Free; | |
4439 #else | |
4488 | 4440 OutputType.tp_getattr = (getattrfunc)OutputGetattr; |
4441 OutputType.tp_setattr = (setattrfunc)OutputSetattr; | |
4319 | 4442 #endif |
4443 | |
4397 | 4444 vim_memset(&IterType, 0, sizeof(IterType)); |
4445 IterType.tp_name = "vim.iter"; | |
4446 IterType.tp_basicsize = sizeof(IterObject); | |
4447 IterType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4448 IterType.tp_doc = "generic iterator object"; | |
4488 | 4449 IterType.tp_iter = (getiterfunc)IterIter; |
4450 IterType.tp_iternext = (iternextfunc)IterNext; | |
4451 IterType.tp_dealloc = (destructor)IterDestructor; | |
4452 IterType.tp_traverse = (traverseproc)IterTraverse; | |
4453 IterType.tp_clear = (inquiry)IterClear; | |
4397 | 4454 |
4319 | 4455 vim_memset(&BufferType, 0, sizeof(BufferType)); |
4456 BufferType.tp_name = "vim.buffer"; | |
4457 BufferType.tp_basicsize = sizeof(BufferType); | |
4488 | 4458 BufferType.tp_dealloc = (destructor)BufferDestructor; |
4459 BufferType.tp_repr = (reprfunc)BufferRepr; | |
4319 | 4460 BufferType.tp_as_sequence = &BufferAsSeq; |
4461 BufferType.tp_as_mapping = &BufferAsMapping; | |
4462 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4463 BufferType.tp_doc = "vim buffer object"; | |
4464 BufferType.tp_methods = BufferMethods; | |
4465 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4466 BufferType.tp_getattro = (getattrofunc)BufferGetattro; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4467 BufferType.tp_setattro = (setattrofunc)BufferSetattro; |
4319 | 4468 BufferType.tp_alloc = call_PyType_GenericAlloc; |
4469 BufferType.tp_new = call_PyType_GenericNew; | |
4470 BufferType.tp_free = call_PyObject_Free; | |
4471 #else | |
4488 | 4472 BufferType.tp_getattr = (getattrfunc)BufferGetattr; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4473 BufferType.tp_setattr = (setattrfunc)BufferSetattr; |
4319 | 4474 #endif |
4475 | |
4476 vim_memset(&WindowType, 0, sizeof(WindowType)); | |
4477 WindowType.tp_name = "vim.window"; | |
4478 WindowType.tp_basicsize = sizeof(WindowObject); | |
4488 | 4479 WindowType.tp_dealloc = (destructor)WindowDestructor; |
4480 WindowType.tp_repr = (reprfunc)WindowRepr; | |
4319 | 4481 WindowType.tp_flags = Py_TPFLAGS_DEFAULT; |
4482 WindowType.tp_doc = "vim Window object"; | |
4483 WindowType.tp_methods = WindowMethods; | |
4488 | 4484 WindowType.tp_traverse = (traverseproc)WindowTraverse; |
4485 WindowType.tp_clear = (inquiry)WindowClear; | |
4319 | 4486 #if PY_MAJOR_VERSION >= 3 |
4488 | 4487 WindowType.tp_getattro = (getattrofunc)WindowGetattro; |
4488 WindowType.tp_setattro = (setattrofunc)WindowSetattro; | |
4319 | 4489 WindowType.tp_alloc = call_PyType_GenericAlloc; |
4490 WindowType.tp_new = call_PyType_GenericNew; | |
4491 WindowType.tp_free = call_PyObject_Free; | |
4492 #else | |
4488 | 4493 WindowType.tp_getattr = (getattrfunc)WindowGetattr; |
4494 WindowType.tp_setattr = (setattrfunc)WindowSetattr; | |
4319 | 4495 #endif |
4496 | |
4401 | 4497 vim_memset(&TabPageType, 0, sizeof(TabPageType)); |
4498 TabPageType.tp_name = "vim.tabpage"; | |
4499 TabPageType.tp_basicsize = sizeof(TabPageObject); | |
4488 | 4500 TabPageType.tp_dealloc = (destructor)TabPageDestructor; |
4501 TabPageType.tp_repr = (reprfunc)TabPageRepr; | |
4401 | 4502 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT; |
4503 TabPageType.tp_doc = "vim tab page object"; | |
4504 TabPageType.tp_methods = TabPageMethods; | |
4505 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4506 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro; |
4401 | 4507 TabPageType.tp_alloc = call_PyType_GenericAlloc; |
4508 TabPageType.tp_new = call_PyType_GenericNew; | |
4509 TabPageType.tp_free = call_PyObject_Free; | |
4510 #else | |
4488 | 4511 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr; |
4401 | 4512 #endif |
4513 | |
4393 | 4514 vim_memset(&BufMapType, 0, sizeof(BufMapType)); |
4515 BufMapType.tp_name = "vim.bufferlist"; | |
4516 BufMapType.tp_basicsize = sizeof(BufMapObject); | |
4517 BufMapType.tp_as_mapping = &BufMapAsMapping; | |
4518 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4397 | 4519 BufMapType.tp_iter = BufMapIter; |
4319 | 4520 BufferType.tp_doc = "vim buffer list"; |
4521 | |
4522 vim_memset(&WinListType, 0, sizeof(WinListType)); | |
4523 WinListType.tp_name = "vim.windowlist"; | |
4524 WinListType.tp_basicsize = sizeof(WinListType); | |
4525 WinListType.tp_as_sequence = &WinListAsSeq; | |
4526 WinListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4527 WinListType.tp_doc = "vim window list"; | |
4488 | 4528 WinListType.tp_dealloc = (destructor)WinListDestructor; |
4401 | 4529 |
4530 vim_memset(&TabListType, 0, sizeof(TabListType)); | |
4531 TabListType.tp_name = "vim.tabpagelist"; | |
4532 TabListType.tp_basicsize = sizeof(TabListType); | |
4533 TabListType.tp_as_sequence = &TabListAsSeq; | |
4534 TabListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4535 TabListType.tp_doc = "vim tab page list"; | |
4319 | 4536 |
4537 vim_memset(&RangeType, 0, sizeof(RangeType)); | |
4538 RangeType.tp_name = "vim.range"; | |
4539 RangeType.tp_basicsize = sizeof(RangeObject); | |
4488 | 4540 RangeType.tp_dealloc = (destructor)RangeDestructor; |
4541 RangeType.tp_repr = (reprfunc)RangeRepr; | |
4319 | 4542 RangeType.tp_as_sequence = &RangeAsSeq; |
4543 RangeType.tp_as_mapping = &RangeAsMapping; | |
4544 RangeType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4545 RangeType.tp_doc = "vim Range object"; | |
4546 RangeType.tp_methods = RangeMethods; | |
4500 | 4547 RangeType.tp_traverse = (traverseproc)RangeTraverse; |
4548 RangeType.tp_clear = (inquiry)RangeClear; | |
4319 | 4549 #if PY_MAJOR_VERSION >= 3 |
4488 | 4550 RangeType.tp_getattro = (getattrofunc)RangeGetattro; |
4319 | 4551 RangeType.tp_alloc = call_PyType_GenericAlloc; |
4552 RangeType.tp_new = call_PyType_GenericNew; | |
4553 RangeType.tp_free = call_PyObject_Free; | |
4554 #else | |
4488 | 4555 RangeType.tp_getattr = (getattrfunc)RangeGetattr; |
4319 | 4556 #endif |
4557 | |
4558 vim_memset(&CurrentType, 0, sizeof(CurrentType)); | |
4559 CurrentType.tp_name = "vim.currentdata"; | |
4560 CurrentType.tp_basicsize = sizeof(CurrentObject); | |
4561 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4562 CurrentType.tp_doc = "vim current object"; | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4563 CurrentType.tp_methods = CurrentMethods; |
4319 | 4564 #if PY_MAJOR_VERSION >= 3 |
4488 | 4565 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro; |
4566 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro; | |
4319 | 4567 #else |
4488 | 4568 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr; |
4569 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr; | |
4319 | 4570 #endif |
4571 | |
4572 vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); | |
4573 DictionaryType.tp_name = "vim.dictionary"; | |
4574 DictionaryType.tp_basicsize = sizeof(DictionaryObject); | |
4488 | 4575 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor; |
4319 | 4576 DictionaryType.tp_as_mapping = &DictionaryAsMapping; |
4577 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4578 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; | |
4579 DictionaryType.tp_methods = DictionaryMethods; | |
4580 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4581 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro; |
4582 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro; | |
4319 | 4583 #else |
4488 | 4584 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr; |
4585 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr; | |
4319 | 4586 #endif |
4587 | |
4588 vim_memset(&ListType, 0, sizeof(ListType)); | |
4589 ListType.tp_name = "vim.list"; | |
4488 | 4590 ListType.tp_dealloc = (destructor)ListDestructor; |
4319 | 4591 ListType.tp_basicsize = sizeof(ListObject); |
4592 ListType.tp_as_sequence = &ListAsSeq; | |
4593 ListType.tp_as_mapping = &ListAsMapping; | |
4594 ListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4595 ListType.tp_doc = "list pushing modifications to vim structure"; | |
4596 ListType.tp_methods = ListMethods; | |
4488 | 4597 ListType.tp_iter = (getiterfunc)ListIter; |
4319 | 4598 #if PY_MAJOR_VERSION >= 3 |
4488 | 4599 ListType.tp_getattro = (getattrofunc)ListGetattro; |
4600 ListType.tp_setattro = (setattrofunc)ListSetattro; | |
4319 | 4601 #else |
4488 | 4602 ListType.tp_getattr = (getattrfunc)ListGetattr; |
4603 ListType.tp_setattr = (setattrfunc)ListSetattr; | |
4319 | 4604 #endif |
4605 | |
4606 vim_memset(&FunctionType, 0, sizeof(FunctionType)); | |
4397 | 4607 FunctionType.tp_name = "vim.function"; |
4319 | 4608 FunctionType.tp_basicsize = sizeof(FunctionObject); |
4488 | 4609 FunctionType.tp_dealloc = (destructor)FunctionDestructor; |
4610 FunctionType.tp_call = (ternaryfunc)FunctionCall; | |
4319 | 4611 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; |
4612 FunctionType.tp_doc = "object that calls vim function"; | |
4613 FunctionType.tp_methods = FunctionMethods; | |
4614 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4615 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro; |
4319 | 4616 #else |
4488 | 4617 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr; |
4319 | 4618 #endif |
4619 | |
4350 | 4620 vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
4621 OptionsType.tp_name = "vim.options"; | |
4622 OptionsType.tp_basicsize = sizeof(OptionsObject); | |
4623 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4624 OptionsType.tp_doc = "object for manipulating options"; | |
4625 OptionsType.tp_as_mapping = &OptionsAsMapping; | |
4488 | 4626 OptionsType.tp_dealloc = (destructor)OptionsDestructor; |
4627 OptionsType.tp_traverse = (traverseproc)OptionsTraverse; | |
4628 OptionsType.tp_clear = (inquiry)OptionsClear; | |
4350 | 4629 |
4319 | 4630 #if PY_MAJOR_VERSION >= 3 |
4631 vim_memset(&vimmodule, 0, sizeof(vimmodule)); | |
4632 vimmodule.m_name = "vim"; | |
4633 vimmodule.m_doc = "Vim Python interface\n"; | |
4634 vimmodule.m_size = -1; | |
4635 vimmodule.m_methods = VimMethods; | |
4636 #endif | |
4637 } | |
4494 | 4638 |
4639 #define PYTYPE_READY(type) \ | |
4640 if (PyType_Ready(&type)) \ | |
4641 return -1; | |
4642 | |
4643 static int | |
4644 init_types() | |
4645 { | |
4646 PYTYPE_READY(IterType); | |
4647 PYTYPE_READY(BufferType); | |
4648 PYTYPE_READY(RangeType); | |
4649 PYTYPE_READY(WindowType); | |
4650 PYTYPE_READY(TabPageType); | |
4651 PYTYPE_READY(BufMapType); | |
4652 PYTYPE_READY(WinListType); | |
4653 PYTYPE_READY(TabListType); | |
4654 PYTYPE_READY(CurrentType); | |
4655 PYTYPE_READY(DictionaryType); | |
4656 PYTYPE_READY(ListType); | |
4657 PYTYPE_READY(FunctionType); | |
4658 PYTYPE_READY(OptionsType); | |
4659 PYTYPE_READY(OutputType); | |
4660 return 0; | |
4661 } | |
4662 | |
4663 static BufMapObject TheBufferMap = | |
4664 { | |
4665 PyObject_HEAD_INIT(&BufMapType) | |
4666 }; | |
4667 | |
4668 static WinListObject TheWindowList = | |
4669 { | |
4670 PyObject_HEAD_INIT(&WinListType) | |
4671 NULL | |
4672 }; | |
4673 | |
4674 static CurrentObject TheCurrent = | |
4675 { | |
4676 PyObject_HEAD_INIT(&CurrentType) | |
4677 }; | |
4678 | |
4679 static TabListObject TheTabPageList = | |
4680 { | |
4681 PyObject_HEAD_INIT(&TabListType) | |
4682 }; | |
4683 | |
4684 static struct numeric_constant { | |
4685 char *name; | |
4686 int value; | |
4687 } numeric_constants[] = { | |
4688 {"VAR_LOCKED", VAR_LOCKED}, | |
4689 {"VAR_FIXED", VAR_FIXED}, | |
4690 {"VAR_SCOPE", VAR_SCOPE}, | |
4691 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, | |
4692 }; | |
4693 | |
4694 static struct object_constant { | |
4695 char *name; | |
4696 PyObject *value; | |
4697 } object_constants[] = { | |
4698 {"buffers", (PyObject *)(void *)&TheBufferMap}, | |
4699 {"windows", (PyObject *)(void *)&TheWindowList}, | |
4700 {"tabpages", (PyObject *)(void *)&TheTabPageList}, | |
4701 {"current", (PyObject *)(void *)&TheCurrent}, | |
4496 | 4702 |
4703 {"Buffer", (PyObject *)&BufferType}, | |
4704 {"Range", (PyObject *)&RangeType}, | |
4705 {"Window", (PyObject *)&WindowType}, | |
4706 {"TabPage", (PyObject *)&TabPageType}, | |
4707 {"Dictionary", (PyObject *)&DictionaryType}, | |
4708 {"List", (PyObject *)&ListType}, | |
4709 {"Function", (PyObject *)&FunctionType}, | |
4710 {"Options", (PyObject *)&OptionsType}, | |
4494 | 4711 }; |
4712 | |
4713 typedef int (*object_adder)(PyObject *, const char *, PyObject *); | |
4714 | |
4715 #define ADD_OBJECT(m, name, obj) \ | |
4716 if (add_object(m, name, obj)) \ | |
4717 return -1; | |
4718 | |
4719 #define ADD_CHECKED_OBJECT(m, name, obj) \ | |
4720 { \ | |
4721 PyObject *value = obj; \ | |
4722 if (!value) \ | |
4723 return -1; \ | |
4724 ADD_OBJECT(m, name, value); \ | |
4725 } | |
4726 | |
4727 static int | |
4728 populate_module(PyObject *m, object_adder add_object) | |
4729 { | |
4730 int i; | |
4731 | |
4732 for (i = 0; i < (int)(sizeof(numeric_constants) | |
4733 / sizeof(struct numeric_constant)); | |
4734 ++i) | |
4735 ADD_CHECKED_OBJECT(m, numeric_constants[i].name, | |
4736 PyInt_FromLong(numeric_constants[i].value)); | |
4737 | |
4738 for (i = 0; i < (int)(sizeof(object_constants) | |
4739 / sizeof(struct object_constant)); | |
4740 ++i) | |
4741 { | |
4742 PyObject *value; | |
4743 | |
4744 value = object_constants[i].value; | |
4745 Py_INCREF(value); | |
4746 ADD_OBJECT(m, object_constants[i].name, value); | |
4747 } | |
4748 | |
4749 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) | |
4750 return -1; | |
4751 ADD_OBJECT(m, "error", VimError); | |
4752 | |
4753 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict)); | |
4754 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict)); | |
4755 ADD_CHECKED_OBJECT(m, "options", | |
4756 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL)); | |
4757 return 0; | |
4758 } |