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