Mercurial > vim
annotate src/if_py_both.h @ 4619:90beab957ba9 v7.3.1057
updated for version 7.3.1057
Problem: Python: not enough compatibilty.
Solution: Python patch 16: Make OutputWritelines support any sequence object
(ZyX) Note: tests fail
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Thu, 30 May 2013 12:26:58 +0200 |
parents | 21a99611149b |
children | 548b889fe3cf |
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 | |
1587 static struct PyMethodDef FunctionMethods[] = { | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1588 {"__call__",(PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1589 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1590 { NULL, NULL, 0, NULL} |
3618 | 1591 }; |
1592 | |
4350 | 1593 /* |
1594 * Options object | |
1595 */ | |
1596 | |
1597 static PyTypeObject OptionsType; | |
1598 | |
1599 typedef int (*checkfun)(void *); | |
1600 | |
1601 typedef struct | |
1602 { | |
1603 PyObject_HEAD | |
1604 int opt_type; | |
1605 void *from; | |
1606 checkfun Check; | |
1607 PyObject *fromObj; | |
1608 } OptionsObject; | |
1609 | |
4433 | 1610 static int |
1611 dummy_check(void *arg UNUSED) | |
1612 { | |
1613 return 0; | |
1614 } | |
1615 | |
1616 static PyObject * | |
1617 OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj) | |
1618 { | |
1619 OptionsObject *self; | |
1620 | |
4500 | 1621 self = PyObject_GC_New(OptionsObject, &OptionsType); |
4433 | 1622 if (self == NULL) |
1623 return NULL; | |
1624 | |
1625 self->opt_type = opt_type; | |
1626 self->from = from; | |
1627 self->Check = Check; | |
1628 self->fromObj = fromObj; | |
1629 if (fromObj) | |
1630 Py_INCREF(fromObj); | |
1631 | |
1632 return (PyObject *)(self); | |
1633 } | |
1634 | |
1635 static void | |
4488 | 1636 OptionsDestructor(OptionsObject *self) |
4433 | 1637 { |
4500 | 1638 PyObject_GC_UnTrack((void *)(self)); |
1639 Py_XDECREF(self->fromObj); | |
1640 PyObject_GC_Del((void *)(self)); | |
4433 | 1641 } |
1642 | |
1643 static int | |
4488 | 1644 OptionsTraverse(OptionsObject *self, visitproc visit, void *arg) |
4433 | 1645 { |
4488 | 1646 Py_VISIT(self->fromObj); |
4433 | 1647 return 0; |
1648 } | |
1649 | |
1650 static int | |
4488 | 1651 OptionsClear(OptionsObject *self) |
4433 | 1652 { |
4488 | 1653 Py_CLEAR(self->fromObj); |
4433 | 1654 return 0; |
1655 } | |
1656 | |
4350 | 1657 static PyObject * |
4488 | 1658 OptionsItem(OptionsObject *self, PyObject *keyObject) |
4350 | 1659 { |
1660 char_u *key; | |
1661 int flags; | |
1662 long numval; | |
1663 char_u *stringval; | |
4359 | 1664 DICTKEY_DECL |
4350 | 1665 |
4488 | 1666 if (self->Check(self->from)) |
4350 | 1667 return NULL; |
1668 | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
1669 DICTKEY_GET(NULL, 0) |
4350 | 1670 |
1671 flags = get_option_value_strict(key, &numval, &stringval, | |
4488 | 1672 self->opt_type, self->from); |
4350 | 1673 |
1674 DICTKEY_UNREF | |
1675 | |
1676 if (flags == 0) | |
1677 { | |
4403 | 1678 PyErr_SetObject(PyExc_KeyError, keyObject); |
4350 | 1679 return NULL; |
1680 } | |
1681 | |
1682 if (flags & SOPT_UNSET) | |
1683 { | |
1684 Py_INCREF(Py_None); | |
1685 return Py_None; | |
1686 } | |
1687 else if (flags & SOPT_BOOL) | |
1688 { | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1689 PyObject *r; |
4350 | 1690 r = numval ? Py_True : Py_False; |
1691 Py_INCREF(r); | |
1692 return r; | |
1693 } | |
1694 else if (flags & SOPT_NUM) | |
1695 return PyInt_FromLong(numval); | |
1696 else if (flags & SOPT_STRING) | |
1697 { | |
1698 if (stringval) | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1699 { |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1700 PyObject *r = PyBytes_FromString((char *) stringval); |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1701 vim_free(stringval); |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1702 return r; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1703 } |
4350 | 1704 else |
1705 { | |
4405 | 1706 PyErr_SetString(PyExc_RuntimeError, |
1707 _("unable to get option value")); | |
4350 | 1708 return NULL; |
1709 } | |
1710 } | |
1711 else | |
1712 { | |
1713 PyErr_SetVim("Internal error: unknown option type. Should not happen"); | |
1714 return NULL; | |
1715 } | |
1716 } | |
1717 | |
1718 static int | |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1719 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
|
1720 char_u *key; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1721 int numval; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1722 char_u *stringval; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1723 int opt_flags; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1724 { |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1725 char_u *errmsg; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1726 |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1727 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
|
1728 { |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1729 if (VimTryEnd()) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1730 return FAIL; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1731 PyErr_SetVim((char *)errmsg); |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1732 return FAIL; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1733 } |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1734 return OK; |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1735 } |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1736 |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1737 static int |
4350 | 1738 set_option_value_for(key, numval, stringval, opt_flags, opt_type, from) |
1739 char_u *key; | |
1740 int numval; | |
1741 char_u *stringval; | |
1742 int opt_flags; | |
1743 int opt_type; | |
1744 void *from; | |
1745 { | |
4509
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1746 win_T *save_curwin = NULL; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1747 tabpage_T *save_curtab = NULL; |
b498224f5b41
updated for version 7.3.1002
Bram Moolenaar <bram@vim.org>
parents:
4500
diff
changeset
|
1748 buf_T *save_curbuf = NULL; |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1749 int r = 0; |
4498 | 1750 |
1751 VimTryStart(); | |
4350 | 1752 switch (opt_type) |
1753 { | |
1754 case SREQ_WIN: | |
4429 | 1755 if (switch_win(&save_curwin, &save_curtab, (win_T *)from, |
1756 win_find_tabpage((win_T *)from)) == FAIL) | |
4350 | 1757 { |
4498 | 1758 if (VimTryEnd()) |
1759 return -1; | |
4350 | 1760 PyErr_SetVim("Problem while switching windows."); |
1761 return -1; | |
1762 } | |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1763 r = set_option_value_err(key, numval, stringval, opt_flags); |
4350 | 1764 restore_win(save_curwin, save_curtab); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1765 if (r == FAIL) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1766 return -1; |
4350 | 1767 break; |
1768 case SREQ_BUF: | |
4429 | 1769 switch_buffer(&save_curbuf, (buf_T *)from); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1770 r = set_option_value_err(key, numval, stringval, opt_flags); |
4429 | 1771 restore_buffer(save_curbuf); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1772 if (r == FAIL) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1773 return -1; |
4350 | 1774 break; |
1775 case SREQ_GLOBAL: | |
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); |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1777 if (r == FAIL) |
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1778 return -1; |
4350 | 1779 break; |
1780 } | |
4498 | 1781 return VimTryEnd(); |
4350 | 1782 } |
1783 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1784 static void * |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1785 py_memsave(void *p, size_t len) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1786 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1787 void *r; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1788 if (!(r = PyMem_Malloc(len))) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1789 return NULL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1790 mch_memmove(r, p, len); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1791 return r; |
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 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1794 #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
|
1795 |
4350 | 1796 static int |
4488 | 1797 OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) |
4350 | 1798 { |
1799 char_u *key; | |
1800 int flags; | |
1801 int opt_flags; | |
1802 int r = 0; | |
4359 | 1803 DICTKEY_DECL |
4350 | 1804 |
4488 | 1805 if (self->Check(self->from)) |
4350 | 1806 return -1; |
1807 | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
1808 DICTKEY_GET(-1, 0) |
4350 | 1809 |
1810 flags = get_option_value_strict(key, NULL, NULL, | |
4488 | 1811 self->opt_type, self->from); |
4350 | 1812 |
1813 if (flags == 0) | |
1814 { | |
4403 | 1815 PyErr_SetObject(PyExc_KeyError, keyObject); |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1816 DICTKEY_UNREF |
4350 | 1817 return -1; |
1818 } | |
1819 | |
1820 if (valObject == NULL) | |
1821 { | |
4488 | 1822 if (self->opt_type == SREQ_GLOBAL) |
4350 | 1823 { |
4405 | 1824 PyErr_SetString(PyExc_ValueError, |
1825 _("unable to unset global option")); | |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1826 DICTKEY_UNREF |
4350 | 1827 return -1; |
1828 } | |
1829 else if (!(flags & SOPT_GLOBAL)) | |
1830 { | |
4405 | 1831 PyErr_SetString(PyExc_ValueError, _("unable to unset option " |
1832 "without global value")); | |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1833 DICTKEY_UNREF |
4350 | 1834 return -1; |
1835 } | |
1836 else | |
1837 { | |
4488 | 1838 unset_global_local_option(key, self->from); |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1839 DICTKEY_UNREF |
4350 | 1840 return 0; |
1841 } | |
1842 } | |
1843 | |
4488 | 1844 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL); |
4350 | 1845 |
1846 if (flags & SOPT_BOOL) | |
1847 { | |
4411 | 1848 int istrue = PyObject_IsTrue(valObject); |
4513
cadb57fbb781
updated for version 7.3.1004
Bram Moolenaar <bram@vim.org>
parents:
4511
diff
changeset
|
1849 |
4411 | 1850 if (istrue == -1) |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1851 r = -1; |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1852 else |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1853 r = set_option_value_for(key, istrue, NULL, |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1854 opt_flags, self->opt_type, self->from); |
4350 | 1855 } |
1856 else if (flags & SOPT_NUM) | |
1857 { | |
1858 int val; | |
1859 | |
1860 #if PY_MAJOR_VERSION < 3 | |
1861 if (PyInt_Check(valObject)) | |
1862 val = PyInt_AsLong(valObject); | |
1863 else | |
1864 #endif | |
1865 if (PyLong_Check(valObject)) | |
1866 val = PyLong_AsLong(valObject); | |
1867 else | |
1868 { | |
4405 | 1869 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
|
1870 DICTKEY_UNREF |
4350 | 1871 return -1; |
1872 } | |
1873 | |
1874 r = set_option_value_for(key, val, NULL, opt_flags, | |
4488 | 1875 self->opt_type, self->from); |
4350 | 1876 } |
1877 else | |
1878 { | |
1879 char_u *val; | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1880 PyObject *todecref; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1881 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1882 if ((val = StringToChars(valObject, &todecref))) |
4350 | 1883 { |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1884 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
|
1885 self->opt_type, self->from); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1886 Py_XDECREF(todecref); |
4350 | 1887 } |
1888 else | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
1889 r = -1; |
4350 | 1890 } |
1891 | |
4587
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1892 DICTKEY_UNREF |
63c9b681c3db
updated for version 7.3.1041
Bram Moolenaar <bram@vim.org>
parents:
4575
diff
changeset
|
1893 |
4350 | 1894 return r; |
1895 } | |
1896 | |
1897 static PyMappingMethods OptionsAsMapping = { | |
1898 (lenfunc) NULL, | |
1899 (binaryfunc) OptionsItem, | |
1900 (objobjargproc) OptionsAssItem, | |
1901 }; | |
1902 | |
4401 | 1903 /* Tabpage object |
1904 */ | |
1905 | |
1906 typedef struct | |
1907 { | |
1908 PyObject_HEAD | |
1909 tabpage_T *tab; | |
1910 } TabPageObject; | |
1911 | |
1912 static PyObject *WinListNew(TabPageObject *tabObject); | |
1913 | |
1914 static PyTypeObject TabPageType; | |
1915 | |
1916 static int | |
4488 | 1917 CheckTabPage(TabPageObject *self) |
4401 | 1918 { |
4488 | 1919 if (self->tab == INVALID_TABPAGE_VALUE) |
4401 | 1920 { |
1921 PyErr_SetVim(_("attempt to refer to deleted tab page")); | |
1922 return -1; | |
1923 } | |
1924 | |
1925 return 0; | |
1926 } | |
1927 | |
1928 static PyObject * | |
1929 TabPageNew(tabpage_T *tab) | |
1930 { | |
1931 TabPageObject *self; | |
1932 | |
1933 if (TAB_PYTHON_REF(tab)) | |
1934 { | |
1935 self = TAB_PYTHON_REF(tab); | |
1936 Py_INCREF(self); | |
1937 } | |
1938 else | |
1939 { | |
1940 self = PyObject_NEW(TabPageObject, &TabPageType); | |
1941 if (self == NULL) | |
1942 return NULL; | |
1943 self->tab = tab; | |
1944 TAB_PYTHON_REF(tab) = self; | |
1945 } | |
1946 | |
1947 return (PyObject *)(self); | |
1948 } | |
1949 | |
1950 static void | |
4488 | 1951 TabPageDestructor(TabPageObject *self) |
4401 | 1952 { |
4488 | 1953 if (self->tab && self->tab != INVALID_TABPAGE_VALUE) |
1954 TAB_PYTHON_REF(self->tab) = NULL; | |
4401 | 1955 |
1956 DESTRUCTOR_FINISH(self); | |
1957 } | |
1958 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1959 static char *TabPageAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1960 "windows", "number", "vars", "window", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1961 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1962 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1963 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1964 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1965 TabPageDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1966 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
1967 return ObjectDir(self, TabPageAttrs); |
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 |
4401 | 1970 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1971 TabPageAttrValid(TabPageObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1972 { |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1973 PyObject *r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1974 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1975 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1976 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1977 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1978 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
|
1979 Py_INCREF(r); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1980 return r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1981 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1982 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
1983 static PyObject * |
4488 | 1984 TabPageAttr(TabPageObject *self, char *name) |
4401 | 1985 { |
1986 if (strcmp(name, "windows") == 0) | |
4488 | 1987 return WinListNew(self); |
4401 | 1988 else if (strcmp(name, "number") == 0) |
4488 | 1989 return PyLong_FromLong((long) get_tab_number(self->tab)); |
4401 | 1990 else if (strcmp(name, "vars") == 0) |
4488 | 1991 return DictionaryNew(self->tab->tp_vars); |
4401 | 1992 else if (strcmp(name, "window") == 0) |
1993 { | |
1994 /* For current tab window.c does not bother to set or update tp_curwin | |
1995 */ | |
4488 | 1996 if (self->tab == curtab) |
4431 | 1997 return WindowNew(curwin, curtab); |
4401 | 1998 else |
4488 | 1999 return WindowNew(self->tab->tp_curwin, self->tab); |
4401 | 2000 } |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2001 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2002 return ObjectDir(NULL, TabPageAttrs); |
4401 | 2003 return NULL; |
2004 } | |
2005 | |
2006 static PyObject * | |
4488 | 2007 TabPageRepr(TabPageObject *self) |
4401 | 2008 { |
2009 static char repr[100]; | |
4488 | 2010 |
2011 if (self->tab == INVALID_TABPAGE_VALUE) | |
4401 | 2012 { |
2013 vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self)); | |
2014 return PyString_FromString(repr); | |
2015 } | |
2016 else | |
2017 { | |
4488 | 2018 int t = get_tab_number(self->tab); |
4401 | 2019 |
2020 if (t == 0) | |
2021 vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"), | |
2022 (self)); | |
2023 else | |
2024 vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1); | |
2025 | |
2026 return PyString_FromString(repr); | |
2027 } | |
2028 } | |
2029 | |
2030 static struct PyMethodDef TabPageMethods[] = { | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2031 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2032 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2033 { NULL, NULL, 0, NULL} |
4401 | 2034 }; |
2035 | |
2036 /* | |
2037 * Window list object | |
2038 */ | |
2039 | |
2040 static PyTypeObject TabListType; | |
2041 static PySequenceMethods TabListAsSeq; | |
2042 | |
2043 typedef struct | |
2044 { | |
2045 PyObject_HEAD | |
2046 } TabListObject; | |
2047 | |
2048 static PyInt | |
2049 TabListLength(PyObject *self UNUSED) | |
2050 { | |
2051 tabpage_T *tp = first_tabpage; | |
2052 PyInt n = 0; | |
2053 | |
2054 while (tp != NULL) | |
2055 { | |
2056 ++n; | |
2057 tp = tp->tp_next; | |
2058 } | |
2059 | |
2060 return n; | |
2061 } | |
2062 | |
2063 static PyObject * | |
2064 TabListItem(PyObject *self UNUSED, PyInt n) | |
2065 { | |
2066 tabpage_T *tp; | |
2067 | |
2068 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n) | |
2069 if (n == 0) | |
2070 return TabPageNew(tp); | |
2071 | |
2072 PyErr_SetString(PyExc_IndexError, _("no such tab page")); | |
2073 return NULL; | |
2074 } | |
2075 | |
4385 | 2076 /* Window object |
2077 */ | |
2078 | |
2079 typedef struct | |
2080 { | |
2081 PyObject_HEAD | |
2082 win_T *win; | |
4431 | 2083 TabPageObject *tabObject; |
4385 | 2084 } WindowObject; |
2085 | |
2086 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
|
2087 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2088 static int |
4488 | 2089 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
|
2090 { |
4488 | 2091 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
|
2092 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2093 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
|
2094 return -1; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2097 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2098 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2099 |
4319 | 2100 static PyObject * |
4431 | 2101 WindowNew(win_T *win, tabpage_T *tab) |
4377 | 2102 { |
2103 /* We need to handle deletion of windows underneath us. | |
2104 * If we add a "w_python*_ref" field to the win_T structure, | |
2105 * then we can get at it in win_free() in vim. We then | |
2106 * need to create only ONE Python object per window - if | |
2107 * we try to create a second, just INCREF the existing one | |
2108 * and return it. The (single) Python object referring to | |
2109 * the window is stored in "w_python*_ref". | |
2110 * On a win_free() we set the Python object's win_T* field | |
2111 * to an invalid value. We trap all uses of a window | |
2112 * object, and reject them if the win_T* field is invalid. | |
2113 * | |
4385 | 2114 * Python2 and Python3 get different fields and different objects: |
4377 | 2115 * w_python_ref and w_python3_ref fields respectively. |
2116 */ | |
2117 | |
2118 WindowObject *self; | |
2119 | |
2120 if (WIN_PYTHON_REF(win)) | |
2121 { | |
2122 self = WIN_PYTHON_REF(win); | |
2123 Py_INCREF(self); | |
2124 } | |
2125 else | |
2126 { | |
4500 | 2127 self = PyObject_GC_New(WindowObject, &WindowType); |
4377 | 2128 if (self == NULL) |
2129 return NULL; | |
2130 self->win = win; | |
2131 WIN_PYTHON_REF(win) = self; | |
2132 } | |
2133 | |
4431 | 2134 self->tabObject = ((TabPageObject *)(TabPageNew(tab))); |
2135 | |
4377 | 2136 return (PyObject *)(self); |
2137 } | |
2138 | |
4385 | 2139 static void |
4488 | 2140 WindowDestructor(WindowObject *self) |
4385 | 2141 { |
4500 | 2142 PyObject_GC_UnTrack((void *)(self)); |
4488 | 2143 if (self->win && self->win != INVALID_WINDOW_VALUE) |
2144 WIN_PYTHON_REF(self->win) = NULL; | |
4500 | 2145 Py_XDECREF(((PyObject *)(self->tabObject))); |
2146 PyObject_GC_Del((void *)(self)); | |
2147 } | |
2148 | |
2149 static int | |
2150 WindowTraverse(WindowObject *self, visitproc visit, void *arg) | |
2151 { | |
2152 Py_VISIT(((PyObject *)(self->tabObject))); | |
2153 return 0; | |
2154 } | |
2155 | |
2156 static int | |
2157 WindowClear(WindowObject *self) | |
2158 { | |
2159 Py_CLEAR(self->tabObject); | |
2160 return 0; | |
4385 | 2161 } |
2162 | |
4431 | 2163 static win_T * |
2164 get_firstwin(TabPageObject *tabObject) | |
2165 { | |
2166 if (tabObject) | |
2167 { | |
2168 if (CheckTabPage(tabObject)) | |
2169 return NULL; | |
2170 /* For current tab window.c does not bother to set or update tp_firstwin | |
2171 */ | |
2172 else if (tabObject->tab == curtab) | |
2173 return firstwin; | |
2174 else | |
2175 return tabObject->tab->tp_firstwin; | |
2176 } | |
2177 else | |
2178 return firstwin; | |
2179 } | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2180 static char *WindowAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2181 "buffer", "cursor", "height", "vars", "options", "number", "row", "col", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2182 "tabpage", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2183 NULL |
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 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2186 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2187 WindowDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2188 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2189 return ObjectDir(self, WindowAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2190 } |
4431 | 2191 |
4377 | 2192 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2193 WindowAttrValid(WindowObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2194 { |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2195 PyObject *r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2196 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2197 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2198 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2199 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2200 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
|
2201 Py_INCREF(r); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2202 return r; |
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 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2205 static PyObject * |
4488 | 2206 WindowAttr(WindowObject *self, char *name) |
4319 | 2207 { |
2208 if (strcmp(name, "buffer") == 0) | |
4488 | 2209 return (PyObject *)BufferNew(self->win->w_buffer); |
4319 | 2210 else if (strcmp(name, "cursor") == 0) |
2211 { | |
4488 | 2212 pos_T *pos = &self->win->w_cursor; |
4319 | 2213 |
2214 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); | |
2215 } | |
2216 else if (strcmp(name, "height") == 0) | |
4488 | 2217 return PyLong_FromLong((long)(self->win->w_height)); |
4383 | 2218 #ifdef FEAT_WINDOWS |
2219 else if (strcmp(name, "row") == 0) | |
4488 | 2220 return PyLong_FromLong((long)(self->win->w_winrow)); |
4383 | 2221 #endif |
4319 | 2222 #ifdef FEAT_VERTSPLIT |
2223 else if (strcmp(name, "width") == 0) | |
4488 | 2224 return PyLong_FromLong((long)(W_WIDTH(self->win))); |
4383 | 2225 else if (strcmp(name, "col") == 0) |
4488 | 2226 return PyLong_FromLong((long)(W_WINCOL(self->win))); |
4319 | 2227 #endif |
4323 | 2228 else if (strcmp(name, "vars") == 0) |
4488 | 2229 return DictionaryNew(self->win->w_vars); |
4350 | 2230 else if (strcmp(name, "options") == 0) |
4488 | 2231 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow, |
2232 (PyObject *) self); | |
4379 | 2233 else if (strcmp(name, "number") == 0) |
4431 | 2234 { |
4488 | 2235 if (CheckTabPage(self->tabObject)) |
4431 | 2236 return NULL; |
2237 return PyLong_FromLong((long) | |
4488 | 2238 get_win_number(self->win, get_firstwin(self->tabObject))); |
4431 | 2239 } |
2240 else if (strcmp(name, "tabpage") == 0) | |
2241 { | |
4488 | 2242 Py_INCREF(self->tabObject); |
2243 return (PyObject *)(self->tabObject); | |
4431 | 2244 } |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2245 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2246 return ObjectDir(NULL, WindowAttrs); |
4319 | 2247 else |
2248 return NULL; | |
2249 } | |
2250 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2251 static int |
4488 | 2252 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
|
2253 { |
4488 | 2254 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
|
2255 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2256 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2257 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
|
2258 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2259 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
|
2260 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2261 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2262 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
|
2263 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2264 long lnum; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2265 long col; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2266 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2267 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
|
2268 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2269 |
4488 | 2270 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
|
2271 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2272 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
|
2273 return -1; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2276 /* Check for keyboard interrupts */ |
4498 | 2277 if (VimCheckInterrupt()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2278 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2279 |
4488 | 2280 self->win->w_cursor.lnum = lnum; |
2281 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
|
2282 #ifdef FEAT_VIRTUALEDIT |
4488 | 2283 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
|
2284 #endif |
2933 | 2285 /* When column is out of range silently correct it. */ |
4488 | 2286 check_cursor_col_win(self->win); |
2933 | 2287 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2288 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2289 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2290 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2291 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
|
2292 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2293 int height; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2294 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2295 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2296 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
|
2297 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2298 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2299 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2300 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
|
2301 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2302 savewin = curwin; |
4488 | 2303 curwin = self->win; |
4498 | 2304 |
2305 VimTryStart(); | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2306 win_setheight(height); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2307 curwin = savewin; |
4498 | 2308 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2309 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2310 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2311 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2312 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2313 #ifdef FEAT_VERTSPLIT |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2314 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
|
2315 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2316 int width; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2317 win_T *savewin; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2318 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2319 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
|
2320 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2321 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2322 #ifdef FEAT_GUI |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2323 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
|
2324 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2325 savewin = curwin; |
4488 | 2326 curwin = self->win; |
4498 | 2327 |
2328 VimTryStart(); | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2329 win_setwidth(width); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2330 curwin = savewin; |
4498 | 2331 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2332 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2333 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2334 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2335 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2336 #endif |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2337 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2338 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2339 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
|
2340 return -1; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2344 static PyObject * |
4488 | 2345 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
|
2346 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2347 static char repr[100]; |
4488 | 2348 |
2349 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
|
2350 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2351 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2352 return PyString_FromString(repr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2353 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2354 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2355 { |
4488 | 2356 int w = get_win_number(self->win, firstwin); |
4379 | 2357 |
2358 if (w == 0) | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2359 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"), |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2360 (self)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2361 else |
4379 | 2362 vim_snprintf(repr, 100, _("<window %d>"), w - 1); |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2363 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2364 return PyString_FromString(repr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2365 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2366 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2367 |
4385 | 2368 static struct PyMethodDef WindowMethods[] = { |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2369 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2370 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
2371 { NULL, NULL, 0, NULL} |
4385 | 2372 }; |
2373 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2374 /* |
4385 | 2375 * 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
|
2376 */ |
4319 | 2377 |
4385 | 2378 static PyTypeObject WinListType; |
2379 static PySequenceMethods WinListAsSeq; | |
2380 | |
4319 | 2381 typedef struct |
2382 { | |
2383 PyObject_HEAD | |
4401 | 2384 TabPageObject *tabObject; |
4319 | 2385 } WinListObject; |
2386 | |
4401 | 2387 static PyObject * |
2388 WinListNew(TabPageObject *tabObject) | |
2389 { | |
2390 WinListObject *self; | |
2391 | |
2392 self = PyObject_NEW(WinListObject, &WinListType); | |
2393 self->tabObject = tabObject; | |
2394 Py_INCREF(tabObject); | |
2395 | |
2396 return (PyObject *)(self); | |
2397 } | |
2398 | |
2399 static void | |
4488 | 2400 WinListDestructor(WinListObject *self) |
4401 | 2401 { |
4488 | 2402 TabPageObject *tabObject = self->tabObject; |
4401 | 2403 |
2404 if (tabObject) | |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
2405 { |
4401 | 2406 Py_DECREF((PyObject *)(tabObject)); |
4523
57393dc4b811
updated for version 7.3.1009
Bram Moolenaar <bram@vim.org>
parents:
4513
diff
changeset
|
2407 } |
4401 | 2408 |
2409 DESTRUCTOR_FINISH(self); | |
2410 } | |
2411 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2412 static PyInt |
4488 | 2413 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
|
2414 { |
4401 | 2415 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
|
2416 PyInt n = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2417 |
4488 | 2418 if (!(w = get_firstwin(self->tabObject))) |
4401 | 2419 return -1; |
2420 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2421 while (w != NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2422 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2423 ++n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2424 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
|
2425 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2426 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2427 return n; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2428 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2429 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2430 static PyObject * |
4488 | 2431 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
|
2432 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2433 win_T *w; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2434 |
4488 | 2435 if (!(w = get_firstwin(self->tabObject))) |
4401 | 2436 return NULL; |
2437 | |
2438 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
|
2439 if (n == 0) |
4488 | 2440 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
|
2441 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2442 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
|
2443 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2444 } |
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 /* 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
|
2447 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2448 * 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
|
2449 * 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
|
2450 * characters. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2451 * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2452 * 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
|
2453 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2454 static char * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2455 StringToLine(PyObject *obj) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2456 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2457 const char *str; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2458 char *save; |
2894 | 2459 PyObject *bytes; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2460 PyInt len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2461 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2462 char *p; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2463 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2464 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
|
2465 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2466 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2467 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2468 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2469 |
2894 | 2470 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
2471 str = PyString_AsString(bytes); | |
2472 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
|
2473 |
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 * 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
|
2476 * 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
|
2477 * a single line. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2478 * 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
|
2479 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2480 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
|
2481 if (p != NULL) |
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 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
|
2484 --len; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2485 else |
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 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
|
2488 return 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 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2491 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2492 /* 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
|
2493 * 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
|
2494 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2495 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
|
2496 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2497 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2498 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2499 return NULL; |
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 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
|
2503 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2504 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
|
2505 save[i] = '\n'; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2506 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2507 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
|
2508 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2509 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2510 save[i] = '\0'; |
2894 | 2511 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
|
2512 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2513 return save; |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2516 /* 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
|
2517 * 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
|
2518 * string object. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2519 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2520 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2521 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
|
2522 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2523 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
|
2524 } |
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 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2527 /* 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
|
2528 * 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
|
2529 * 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
|
2530 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2531 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2532 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
|
2533 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2534 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2535 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
|
2536 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
|
2537 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2538 if (list == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2539 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2540 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2541 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
|
2542 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2543 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
|
2544 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2545 /* 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
|
2546 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2547 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2548 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 /* 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
|
2553 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
|
2554 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2555 Py_DECREF(str); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2556 Py_DECREF(list); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2557 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2558 } |
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 /* 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
|
2562 * 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
|
2563 * with it). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2564 */ |
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 return list; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2567 } |
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 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2570 * 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
|
2571 * 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
|
2572 * deleted). |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2573 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2574 static void |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2575 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
|
2576 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2577 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
|
2578 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2579 /* 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
|
2580 * lines. */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2581 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
|
2582 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2583 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
|
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 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2586 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
|
2587 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2588 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
|
2589 check_cursor(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2590 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2591 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2592 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2593 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
|
2594 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2595 invalidate_botline(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2596 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2597 |
2894 | 2598 /* |
2599 * 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
|
2600 * 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
|
2601 * 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
|
2602 * 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
|
2603 * 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
|
2604 * 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
|
2605 * 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
|
2606 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2607 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2608 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
|
2609 { |
4352 | 2610 /* 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
|
2611 * 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
|
2612 * 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
|
2613 * 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
|
2614 * 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
|
2615 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2616 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
|
2617 { |
4429 | 2618 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
|
2619 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2620 PyErr_Clear(); |
4429 | 2621 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
|
2622 |
4498 | 2623 VimTryStart(); |
2624 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2625 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
|
2626 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
|
2627 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
|
2628 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
|
2629 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2630 { |
4429 | 2631 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
|
2632 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
|
2633 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
|
2634 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2635 |
4429 | 2636 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
|
2637 |
4498 | 2638 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2639 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2640 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2641 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2642 *len_change = -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2643 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2644 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2645 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2646 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
|
2647 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2648 char *save = StringToLine(line); |
4429 | 2649 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
|
2650 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2651 if (save == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2652 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2653 |
4498 | 2654 VimTryStart(); |
2655 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2656 /* 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
|
2657 PyErr_Clear(); |
4429 | 2658 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
|
2659 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2660 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
|
2661 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2662 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
|
2663 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2664 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2665 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
|
2666 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2667 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
|
2668 vim_free(save); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2669 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2670 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2671 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
|
2672 |
4429 | 2673 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
|
2674 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2675 /* Check that the cursor is not beyond the end of the line now. */ |
4429 | 2676 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
|
2677 check_cursor_col(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2678 |
4498 | 2679 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2680 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2681 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2682 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2683 *len_change = 0; |
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 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2686 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2687 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2688 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2689 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2690 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2691 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2692 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2693 |
2894 | 2694 /* Replace a range of lines in the specified buffer. The line numbers are in |
2695 * Vim format (1-based). The range is from lo up to, but not including, hi. | |
2696 * The replacement lines are given as a Python list of string objects. The | |
2697 * list is checked for validity and correct format. Errors are returned as a | |
2698 * value of FAIL. The return value is OK on success. | |
2699 * If OK is returned and len_change is not NULL, *len_change | |
2700 * is set to the change in the buffer length. | |
2701 */ | |
2702 static int | |
2703 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) | |
2704 { | |
4352 | 2705 /* First of all, we check the type of the supplied Python object. |
2894 | 2706 * There are three cases: |
2707 * 1. NULL, or None - this is a deletion. | |
2708 * 2. A list - this is a replacement. | |
2709 * 3. Anything else - this is an error. | |
2710 */ | |
2711 if (list == Py_None || list == NULL) | |
2712 { | |
2713 PyInt i; | |
2714 PyInt n = (int)(hi - lo); | |
4429 | 2715 buf_T *savebuf; |
2894 | 2716 |
2717 PyErr_Clear(); | |
4498 | 2718 VimTryStart(); |
4429 | 2719 switch_buffer(&savebuf, buf); |
2894 | 2720 |
2721 if (u_savedel((linenr_T)lo, (long)n) == FAIL) | |
2722 PyErr_SetVim(_("cannot save undo information")); | |
2723 else | |
2724 { | |
2725 for (i = 0; i < n; ++i) | |
2726 { | |
2727 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2728 { | |
2729 PyErr_SetVim(_("cannot delete line")); | |
2730 break; | |
2731 } | |
2732 } | |
4429 | 2733 if (buf == savebuf) |
2894 | 2734 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
2735 deleted_lines_mark((linenr_T)lo, (long)i); | |
2736 } | |
2737 | |
4429 | 2738 restore_buffer(savebuf); |
2894 | 2739 |
4498 | 2740 if (VimTryEnd()) |
2894 | 2741 return FAIL; |
2742 | |
2743 if (len_change) | |
2744 *len_change = -n; | |
2745 | |
2746 return OK; | |
2747 } | |
2748 else if (PyList_Check(list)) | |
2749 { | |
2750 PyInt i; | |
2751 PyInt new_len = PyList_Size(list); | |
2752 PyInt old_len = hi - lo; | |
2753 PyInt extra = 0; /* lines added to text, can be negative */ | |
2754 char **array; | |
2755 buf_T *savebuf; | |
2756 | |
2757 if (new_len == 0) /* avoid allocating zero bytes */ | |
2758 array = NULL; | |
2759 else | |
2760 { | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2761 array = PyMem_New(char *, new_len); |
2894 | 2762 if (array == NULL) |
2763 { | |
2764 PyErr_NoMemory(); | |
2765 return FAIL; | |
2766 } | |
2767 } | |
2768 | |
2769 for (i = 0; i < new_len; ++i) | |
2770 { | |
2771 PyObject *line = PyList_GetItem(list, i); | |
2772 | |
2773 array[i] = StringToLine(line); | |
2774 if (array[i] == NULL) | |
2775 { | |
2776 while (i) | |
2777 vim_free(array[--i]); | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2778 PyMem_Free(array); |
2894 | 2779 return FAIL; |
2780 } | |
2781 } | |
2782 | |
4498 | 2783 VimTryStart(); |
2894 | 2784 PyErr_Clear(); |
4429 | 2785 |
2786 // START of region without "return". Must call restore_buffer()! | |
2787 switch_buffer(&savebuf, buf); | |
2894 | 2788 |
2789 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) | |
2790 PyErr_SetVim(_("cannot save undo information")); | |
2791 | |
2792 /* If the size of the range is reducing (ie, new_len < old_len) we | |
2793 * need to delete some old_len. We do this at the start, by | |
2794 * repeatedly deleting line "lo". | |
2795 */ | |
2796 if (!PyErr_Occurred()) | |
2797 { | |
2798 for (i = 0; i < old_len - new_len; ++i) | |
2799 if (ml_delete((linenr_T)lo, FALSE) == FAIL) | |
2800 { | |
2801 PyErr_SetVim(_("cannot delete line")); | |
2802 break; | |
2803 } | |
2804 extra -= i; | |
2805 } | |
2806 | |
2807 /* For as long as possible, replace the existing old_len with the | |
2808 * new old_len. This is a more efficient operation, as it requires | |
2809 * less memory allocation and freeing. | |
2810 */ | |
2811 if (!PyErr_Occurred()) | |
2812 { | |
2813 for (i = 0; i < old_len && i < new_len; ++i) | |
2814 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) | |
2815 == FAIL) | |
2816 { | |
2817 PyErr_SetVim(_("cannot replace line")); | |
2818 break; | |
2819 } | |
2820 } | |
2821 else | |
2822 i = 0; | |
2823 | |
2824 /* Now we may need to insert the remaining new old_len. If we do, we | |
2825 * must free the strings as we finish with them (we can't pass the | |
2826 * responsibility to vim in this case). | |
2827 */ | |
2828 if (!PyErr_Occurred()) | |
2829 { | |
2830 while (i < new_len) | |
2831 { | |
2832 if (ml_append((linenr_T)(lo + i - 1), | |
2833 (char_u *)array[i], 0, FALSE) == FAIL) | |
2834 { | |
2835 PyErr_SetVim(_("cannot insert line")); | |
2836 break; | |
2837 } | |
2838 vim_free(array[i]); | |
2839 ++i; | |
2840 ++extra; | |
2841 } | |
2842 } | |
2843 | |
2844 /* Free any left-over old_len, as a result of an error */ | |
2845 while (i < new_len) | |
2846 { | |
2847 vim_free(array[i]); | |
2848 ++i; | |
2849 } | |
2850 | |
2851 /* Free the array of old_len. All of its contents have now | |
2852 * been dealt with (either freed, or the responsibility passed | |
2853 * to vim. | |
2854 */ | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2855 PyMem_Free(array); |
2894 | 2856 |
2857 /* Adjust marks. Invalidate any which lie in the | |
2858 * changed range, and move any in the remainder of the buffer. | |
2859 */ | |
2860 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), | |
2861 (long)MAXLNUM, (long)extra); | |
2862 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); | |
2863 | |
4429 | 2864 if (buf == savebuf) |
2894 | 2865 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
2866 | |
4429 | 2867 // END of region without "return". |
2868 restore_buffer(savebuf); | |
2894 | 2869 |
4498 | 2870 if (VimTryEnd()) |
2894 | 2871 return FAIL; |
2872 | |
2873 if (len_change) | |
2874 *len_change = new_len - old_len; | |
2875 | |
2876 return OK; | |
2877 } | |
2878 else | |
2879 { | |
2880 PyErr_BadArgument(); | |
2881 return FAIL; | |
2882 } | |
2883 } | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2884 |
4352 | 2885 /* 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
|
2886 * 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
|
2887 * 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
|
2888 * 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
|
2889 * 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
|
2890 * 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
|
2891 * 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
|
2892 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2893 static int |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2894 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
|
2895 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2896 /* 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
|
2897 * 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
|
2898 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2899 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
|
2900 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2901 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
|
2902 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2903 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2904 if (str == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2905 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2906 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2907 PyErr_Clear(); |
4498 | 2908 VimTryStart(); |
4429 | 2909 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
|
2910 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2911 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
|
2912 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
|
2913 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
|
2914 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
|
2915 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2916 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
|
2917 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2918 vim_free(str); |
4429 | 2919 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
|
2920 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2921 |
4498 | 2922 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2923 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2924 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2925 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2926 *len_change = 1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2927 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2928 return OK; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2929 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2930 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
|
2931 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2932 PyInt i; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2933 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
|
2934 char **array; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2935 buf_T *savebuf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2936 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2937 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
|
2938 if (array == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2939 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2940 PyErr_NoMemory(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2941 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2942 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2943 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2944 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
|
2945 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2946 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
|
2947 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
|
2948 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2949 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
|
2950 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2951 while (i) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2952 vim_free(array[--i]); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2953 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
|
2954 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2955 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2956 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2957 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2958 PyErr_Clear(); |
4498 | 2959 VimTryStart(); |
4429 | 2960 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
|
2961 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2962 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
|
2963 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
|
2964 else |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2965 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2966 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
|
2967 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2968 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
|
2969 (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
|
2970 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2971 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
|
2972 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2973 /* 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
|
2974 while (i < size) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2975 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
|
2976 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2977 break; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2978 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2979 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
|
2980 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2981 if (i > 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2982 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
|
2983 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2984 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2985 /* 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
|
2986 * been freed. |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2987 */ |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
2988 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
|
2989 |
4429 | 2990 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
|
2991 update_screen(VALID); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2992 |
4498 | 2993 if (VimTryEnd()) |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2994 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2995 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2996 if (len_change) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
2997 *len_change = size; |
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 return OK; |
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 else |
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 PyErr_BadArgument(); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3004 return FAIL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3005 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3006 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3007 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3008 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3009 * 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
|
3010 * ------------------------------------------- |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3011 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3012 |
4385 | 3013 typedef struct |
3014 { | |
3015 PyObject_HEAD | |
3016 buf_T *buf; | |
3017 } BufferObject; | |
3018 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3019 static int |
4488 | 3020 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
|
3021 { |
4488 | 3022 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
|
3023 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3024 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
|
3025 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3026 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3027 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3028 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3029 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3030 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3031 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3032 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
|
3033 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3034 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3035 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3036 |
4387 | 3037 if (end == -1) |
3038 end = self->buf->b_ml.ml_line_count; | |
3039 | |
4389 | 3040 if (n < 0) |
3041 n += end - start + 1; | |
3042 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3043 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
|
3044 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3045 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
|
3046 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3047 } |
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 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
|
3050 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3051 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3052 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3053 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
|
3054 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3055 PyInt size; |
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 (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3058 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3059 |
4387 | 3060 if (end == -1) |
3061 end = self->buf->b_ml.ml_line_count; | |
3062 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3063 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
|
3064 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3065 if (lo < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3066 lo = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3067 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
|
3068 lo = size; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3069 if (hi < 0) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3070 hi = 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3071 if (hi < lo) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3072 hi = lo; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3073 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
|
3074 hi = size; |
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 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
|
3077 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3078 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3079 static PyInt |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3080 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
|
3081 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3082 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3083 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3084 if (CheckBuffer(self)) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3085 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3086 |
4387 | 3087 if (end == -1) |
3088 end = self->buf->b_ml.ml_line_count; | |
3089 | |
4389 | 3090 if (n < 0) |
3091 n += end - start + 1; | |
3092 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3093 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
|
3094 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3095 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
|
3096 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3097 } |
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 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
|
3100 return -1; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3101 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3102 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3103 *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
|
3104 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3105 return 0; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3106 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3107 |
2894 | 3108 static PyInt |
3109 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) | |
3110 { | |
3111 PyInt size; | |
3112 PyInt len_change; | |
3113 | |
3114 /* Self must be a valid buffer */ | |
3115 if (CheckBuffer(self)) | |
3116 return -1; | |
3117 | |
4387 | 3118 if (end == -1) |
3119 end = self->buf->b_ml.ml_line_count; | |
3120 | |
2894 | 3121 /* Sort out the slice range */ |
3122 size = end - start + 1; | |
3123 | |
3124 if (lo < 0) | |
3125 lo = 0; | |
3126 else if (lo > size) | |
3127 lo = size; | |
3128 if (hi < 0) | |
3129 hi = 0; | |
3130 if (hi < lo) | |
3131 hi = lo; | |
3132 else if (hi > size) | |
3133 hi = size; | |
3134 | |
3135 if (SetBufferLineList(self->buf, lo + start, hi + start, | |
3136 val, &len_change) == FAIL) | |
3137 return -1; | |
3138 | |
3139 if (new_end) | |
3140 *new_end = end + len_change; | |
3141 | |
3142 return 0; | |
3143 } | |
3144 | |
2447
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 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3147 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
|
3148 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3149 PyObject *lines; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3150 PyInt len_change; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3151 PyInt max; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3152 PyInt n; |
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 (CheckBuffer(self)) |
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 |
4387 | 3157 if (end == -1) |
3158 end = self->buf->b_ml.ml_line_count; | |
3159 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3160 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
|
3161 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3162 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
|
3163 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3164 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3165 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
|
3166 { |
4405 | 3167 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
|
3168 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3169 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3170 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3171 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
|
3172 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3173 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3174 if (new_end) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3175 *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
|
3176 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3177 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
|
3178 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3179 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3180 |
4385 | 3181 /* Range object |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3182 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3183 |
4319 | 3184 static PyTypeObject RangeType; |
4385 | 3185 static PySequenceMethods RangeAsSeq; |
3186 static PyMappingMethods RangeAsMapping; | |
4319 | 3187 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3188 typedef struct |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3189 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3190 PyObject_HEAD |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3191 BufferObject *buf; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3192 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3193 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3194 } RangeObject; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3195 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3196 static PyObject * |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3197 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
|
3198 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3199 BufferObject *bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3200 RangeObject *self; |
4500 | 3201 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
|
3202 if (self == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3203 return NULL; |
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 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
|
3206 if (bufr == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3207 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3208 Py_DECREF(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3209 return NULL; |
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 Py_INCREF(bufr); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3212 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3213 self->buf = bufr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3214 self->start = start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3215 self->end = end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3216 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3217 return (PyObject *)(self); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3218 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3219 |
4319 | 3220 static void |
4488 | 3221 RangeDestructor(RangeObject *self) |
4319 | 3222 { |
4500 | 3223 PyObject_GC_UnTrack((void *)(self)); |
4498 | 3224 Py_XDECREF(self->buf); |
4500 | 3225 PyObject_GC_Del((void *)(self)); |
3226 } | |
3227 | |
3228 static int | |
3229 RangeTraverse(RangeObject *self, visitproc visit, void *arg) | |
3230 { | |
3231 Py_VISIT(((PyObject *)(self->buf))); | |
3232 return 0; | |
3233 } | |
3234 | |
3235 static int | |
3236 RangeClear(RangeObject *self) | |
3237 { | |
3238 Py_CLEAR(self->buf); | |
3239 return 0; | |
4319 | 3240 } |
3241 | |
4385 | 3242 static PyInt |
4488 | 3243 RangeLength(RangeObject *self) |
4385 | 3244 { |
3245 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 3246 if (CheckBuffer(self->buf)) |
4385 | 3247 return -1; /* ??? */ |
3248 | |
4488 | 3249 return (self->end - self->start + 1); |
4385 | 3250 } |
3251 | |
3252 static PyObject * | |
4488 | 3253 RangeItem(RangeObject *self, PyInt n) |
4385 | 3254 { |
4488 | 3255 return RBItem(self->buf, n, self->start, self->end); |
4385 | 3256 } |
3257 | |
3258 static PyObject * | |
4488 | 3259 RangeSlice(RangeObject *self, PyInt lo, PyInt hi) |
4385 | 3260 { |
4488 | 3261 return RBSlice(self->buf, lo, hi, self->start, self->end); |
4385 | 3262 } |
3263 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3264 static char *RangeAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3265 "start", "end", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3266 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3267 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3268 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3269 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3270 RangeDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3271 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3272 return ObjectDir(self, RangeAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3273 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3274 |
4385 | 3275 static PyObject * |
4488 | 3276 RangeAppend(RangeObject *self, PyObject *args) |
4385 | 3277 { |
4488 | 3278 return RBAppend(self->buf, args, self->start, self->end, &self->end); |
4385 | 3279 } |
3280 | |
3281 static PyObject * | |
4488 | 3282 RangeRepr(RangeObject *self) |
4385 | 3283 { |
3284 static char repr[100]; | |
4488 | 3285 |
3286 if (self->buf->buf == INVALID_BUFFER_VALUE) | |
4385 | 3287 { |
3288 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", | |
3289 (self)); | |
3290 return PyString_FromString(repr); | |
3291 } | |
3292 else | |
3293 { | |
4488 | 3294 char *name = (char *)self->buf->buf->b_fname; |
4385 | 3295 int len; |
3296 | |
3297 if (name == NULL) | |
3298 name = ""; | |
3299 len = (int)strlen(name); | |
3300 | |
3301 if (len > 45) | |
3302 name = name + (45 - len); | |
3303 | |
3304 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", | |
3305 len > 45 ? "..." : "", name, | |
4488 | 3306 self->start, self->end); |
4385 | 3307 |
3308 return PyString_FromString(repr); | |
3309 } | |
3310 } | |
3311 | |
3312 static struct PyMethodDef RangeMethods[] = { | |
4492 | 3313 /* name, function, calling, documentation */ |
3314 {"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
|
3315 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3316 { NULL, NULL, 0, NULL} |
4385 | 3317 }; |
3318 | |
4319 | 3319 static PyTypeObject BufferType; |
3320 static PySequenceMethods BufferAsSeq; | |
3321 static PyMappingMethods BufferAsMapping; | |
3322 | |
3323 static PyObject * | |
4377 | 3324 BufferNew(buf_T *buf) |
3325 { | |
3326 /* We need to handle deletion of buffers underneath us. | |
3327 * If we add a "b_python*_ref" field to the buf_T structure, | |
3328 * then we can get at it in buf_freeall() in vim. We then | |
3329 * need to create only ONE Python object per buffer - if | |
3330 * we try to create a second, just INCREF the existing one | |
3331 * and return it. The (single) Python object referring to | |
3332 * the buffer is stored in "b_python*_ref". | |
3333 * Question: what to do on a buf_freeall(). We'll probably | |
3334 * have to either delete the Python object (DECREF it to | |
3335 * zero - a bad idea, as it leaves dangling refs!) or | |
3336 * set the buf_T * value to an invalid value (-1?), which | |
3337 * means we need checks in all access functions... Bah. | |
3338 * | |
4385 | 3339 * Python2 and Python3 get different fields and different objects: |
4377 | 3340 * b_python_ref and b_python3_ref fields respectively. |
3341 */ | |
3342 | |
3343 BufferObject *self; | |
3344 | |
3345 if (BUF_PYTHON_REF(buf) != NULL) | |
3346 { | |
3347 self = BUF_PYTHON_REF(buf); | |
3348 Py_INCREF(self); | |
3349 } | |
3350 else | |
3351 { | |
3352 self = PyObject_NEW(BufferObject, &BufferType); | |
3353 if (self == NULL) | |
3354 return NULL; | |
3355 self->buf = buf; | |
3356 BUF_PYTHON_REF(buf) = self; | |
3357 } | |
3358 | |
3359 return (PyObject *)(self); | |
3360 } | |
3361 | |
4385 | 3362 static void |
4488 | 3363 BufferDestructor(BufferObject *self) |
4319 | 3364 { |
4488 | 3365 if (self->buf && self->buf != INVALID_BUFFER_VALUE) |
3366 BUF_PYTHON_REF(self->buf) = NULL; | |
4385 | 3367 |
3368 DESTRUCTOR_FINISH(self); | |
4319 | 3369 } |
3370 | |
4377 | 3371 static PyInt |
4488 | 3372 BufferLength(BufferObject *self) |
4377 | 3373 { |
3374 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
4488 | 3375 if (CheckBuffer(self)) |
4377 | 3376 return -1; /* ??? */ |
3377 | |
4488 | 3378 return (PyInt)(self->buf->b_ml.ml_line_count); |
4377 | 3379 } |
3380 | |
3381 static PyObject * | |
4488 | 3382 BufferItem(BufferObject *self, PyInt n) |
4377 | 3383 { |
4488 | 3384 return RBItem(self, n, 1, -1); |
4377 | 3385 } |
3386 | |
3387 static PyObject * | |
4488 | 3388 BufferSlice(BufferObject *self, PyInt lo, PyInt hi) |
4377 | 3389 { |
4488 | 3390 return RBSlice(self, lo, hi, 1, -1); |
4377 | 3391 } |
3392 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3393 static char *BufferAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3394 "name", "number", "vars", "options", "valid", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3395 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3396 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3397 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3398 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3399 BufferDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3400 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3401 return ObjectDir(self, BufferAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3402 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3403 |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3404 static PyObject * |
4593
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3405 BufferAttrValid(BufferObject *self, char *name) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3406 { |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3407 PyObject *r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3408 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3409 if (strcmp(name, "valid") != 0) |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3410 return NULL; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3411 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3412 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
|
3413 Py_INCREF(r); |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3414 return r; |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3415 } |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3416 |
0cf552b325b5
updated for version 7.3.1044
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
3417 static PyObject * |
4488 | 3418 BufferAttr(BufferObject *self, char *name) |
4385 | 3419 { |
3420 if (strcmp(name, "name") == 0) | |
4597
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
3421 return PyString_FromString((self->buf->b_ffname == NULL |
00eecb2f8e3e
updated for version 7.3.1046
Bram Moolenaar <bram@vim.org>
parents:
4595
diff
changeset
|
3422 ? "" : (char *) self->buf->b_ffname)); |
4385 | 3423 else if (strcmp(name, "number") == 0) |
4488 | 3424 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum); |
4385 | 3425 else if (strcmp(name, "vars") == 0) |
4488 | 3426 return DictionaryNew(self->buf->b_vars); |
4385 | 3427 else if (strcmp(name, "options") == 0) |
4488 | 3428 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer, |
3429 (PyObject *) self); | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3430 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3431 return ObjectDir(NULL, BufferAttrs); |
4385 | 3432 else |
3433 return NULL; | |
3434 } | |
3435 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3436 static int |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3437 BufferSetattr(BufferObject *self, char *name, PyObject *valObject) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3438 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3439 if (CheckBuffer(self)) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3440 return -1; |
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 if (strcmp(name, "name") == 0) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3443 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3444 char_u *val; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3445 aco_save_T aco; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3446 int r; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3447 PyObject *todecref; |
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 if (!(val = StringToChars(valObject, &todecref))) |
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 VimTryStart(); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3453 /* 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
|
3454 aucmd_prepbuf(&aco, self->buf); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3455 r = rename_buffer(val); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3456 aucmd_restbuf(&aco); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3457 Py_XDECREF(todecref); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3458 if (VimTryEnd()) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3459 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3460 |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3461 if (r == FAIL) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3462 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3463 PyErr_SetVim(_("failed to rename buffer")); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3464 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3465 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3466 return 0; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3467 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3468 else |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3469 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3470 PyErr_SetString(PyExc_AttributeError, name); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3471 return -1; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3472 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3473 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
3474 |
4385 | 3475 static PyObject * |
4488 | 3476 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
|
3477 { |
4488 | 3478 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
|
3479 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3480 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3481 static PyObject * |
4488 | 3482 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
|
3483 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3484 pos_T *posp; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3485 char *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3486 char mark; |
4429 | 3487 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
|
3488 |
4488 | 3489 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
|
3490 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3491 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3492 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
|
3493 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3494 mark = *pmark; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3495 |
4498 | 3496 VimTryStart(); |
4488 | 3497 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
|
3498 posp = getmark(mark, FALSE); |
4429 | 3499 restore_buffer(savebuf); |
4498 | 3500 if (VimTryEnd()) |
3501 return NULL; | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3502 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3503 if (posp == NULL) |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3504 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3505 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
|
3506 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3507 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3508 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3509 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
|
3510 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3511 /* 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
|
3512 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
|
3513 return Py_None; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3514 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3515 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3516 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
|
3517 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3518 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3519 static PyObject * |
4488 | 3520 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
|
3521 { |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3522 PyInt start; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3523 PyInt end; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3524 |
4488 | 3525 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
|
3526 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3527 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3528 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
|
3529 return NULL; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3530 |
4488 | 3531 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
|
3532 } |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3533 |
4319 | 3534 static PyObject * |
4488 | 3535 BufferRepr(BufferObject *self) |
4319 | 3536 { |
3537 static char repr[100]; | |
4488 | 3538 |
3539 if (self->buf == INVALID_BUFFER_VALUE) | |
4319 | 3540 { |
3541 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); | |
3542 return PyString_FromString(repr); | |
3543 } | |
3544 else | |
3545 { | |
4488 | 3546 char *name = (char *)self->buf->b_fname; |
4319 | 3547 PyInt len; |
3548 | |
3549 if (name == NULL) | |
3550 name = ""; | |
3551 len = strlen(name); | |
3552 | |
3553 if (len > 35) | |
3554 name = name + (35 - len); | |
3555 | |
3556 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); | |
3557 | |
3558 return PyString_FromString(repr); | |
3559 } | |
3560 } | |
3561 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3562 static struct PyMethodDef BufferMethods[] = { |
4492 | 3563 /* name, function, calling, documentation */ |
3564 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, | |
3565 {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" }, | |
3566 {"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
|
3567 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3568 { 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
|
3569 }; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3570 |
4397 | 3571 /* |
3572 * Buffer list object - Implementation | |
3573 */ | |
3574 | |
3575 static PyTypeObject BufMapType; | |
3576 | |
3577 typedef struct | |
3578 { | |
3579 PyObject_HEAD | |
3580 } BufMapObject; | |
3581 | |
3582 static PyInt | |
3583 BufMapLength(PyObject *self UNUSED) | |
3584 { | |
3585 buf_T *b = firstbuf; | |
3586 PyInt n = 0; | |
3587 | |
3588 while (b) | |
3589 { | |
3590 ++n; | |
3591 b = b->b_next; | |
3592 } | |
3593 | |
3594 return n; | |
3595 } | |
3596 | |
3597 static PyObject * | |
3598 BufMapItem(PyObject *self UNUSED, PyObject *keyObject) | |
3599 { | |
3600 buf_T *b; | |
3601 int bnr; | |
3602 | |
3603 #if PY_MAJOR_VERSION < 3 | |
3604 if (PyInt_Check(keyObject)) | |
3605 bnr = PyInt_AsLong(keyObject); | |
3606 else | |
3607 #endif | |
3608 if (PyLong_Check(keyObject)) | |
3609 bnr = PyLong_AsLong(keyObject); | |
3610 else | |
3611 { | |
4405 | 3612 PyErr_SetString(PyExc_TypeError, _("key must be integer")); |
4397 | 3613 return NULL; |
3614 } | |
3615 | |
3616 b = buflist_findnr(bnr); | |
3617 | |
3618 if (b) | |
3619 return BufferNew(b); | |
3620 else | |
3621 { | |
4403 | 3622 PyErr_SetObject(PyExc_KeyError, keyObject); |
4397 | 3623 return NULL; |
3624 } | |
3625 } | |
3626 | |
3627 static void | |
3628 BufMapIterDestruct(PyObject *buffer) | |
3629 { | |
3630 /* Iteration was stopped before all buffers were processed */ | |
3631 if (buffer) | |
3632 { | |
3633 Py_DECREF(buffer); | |
3634 } | |
3635 } | |
3636 | |
4433 | 3637 static int |
3638 BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg) | |
3639 { | |
4500 | 3640 if (buffer) |
3641 Py_VISIT(buffer); | |
4433 | 3642 return 0; |
3643 } | |
3644 | |
3645 static int | |
3646 BufMapIterClear(PyObject **buffer) | |
3647 { | |
4500 | 3648 if (*buffer) |
3649 Py_CLEAR(*buffer); | |
4433 | 3650 return 0; |
3651 } | |
3652 | |
4397 | 3653 static PyObject * |
3654 BufMapIterNext(PyObject **buffer) | |
3655 { | |
3656 PyObject *next; | |
3657 PyObject *r; | |
3658 | |
3659 if (!*buffer) | |
3660 return NULL; | |
3661 | |
3662 r = *buffer; | |
3663 | |
3664 if (CheckBuffer((BufferObject *)(r))) | |
3665 { | |
3666 *buffer = NULL; | |
3667 return NULL; | |
3668 } | |
3669 | |
3670 if (!((BufferObject *)(r))->buf->b_next) | |
3671 next = NULL; | |
3672 else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next))) | |
3673 return NULL; | |
3674 *buffer = next; | |
4438 | 3675 /* Do not increment reference: we no longer hold it (decref), but whoever |
3676 * on other side will hold (incref). Decref+incref = nothing. */ | |
4397 | 3677 return r; |
3678 } | |
3679 | |
3680 static PyObject * | |
3681 BufMapIter(PyObject *self UNUSED) | |
3682 { | |
3683 PyObject *buffer; | |
3684 | |
3685 buffer = BufferNew(firstbuf); | |
3686 return IterNew(buffer, | |
4433 | 3687 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext, |
3688 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear); | |
4397 | 3689 } |
3690 | |
3691 static PyMappingMethods BufMapAsMapping = { | |
3692 (lenfunc) BufMapLength, | |
3693 (binaryfunc) BufMapItem, | |
3694 (objobjargproc) 0, | |
3695 }; | |
3696 | |
4385 | 3697 /* 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
|
3698 */ |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
3699 |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3700 static char *CurrentAttrs[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3701 "buffer", "window", "line", "range", "tabpage", |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3702 NULL |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3703 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3704 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3705 static PyObject * |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3706 CurrentDir(PyObject *self) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3707 { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3708 return ObjectDir(self, CurrentAttrs); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3709 } |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3710 |
4319 | 3711 static PyObject * |
3712 CurrentGetattr(PyObject *self UNUSED, char *name) | |
3713 { | |
3714 if (strcmp(name, "buffer") == 0) | |
3715 return (PyObject *)BufferNew(curbuf); | |
3716 else if (strcmp(name, "window") == 0) | |
4431 | 3717 return (PyObject *)WindowNew(curwin, curtab); |
4401 | 3718 else if (strcmp(name, "tabpage") == 0) |
3719 return (PyObject *)TabPageNew(curtab); | |
4319 | 3720 else if (strcmp(name, "line") == 0) |
3721 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); | |
3722 else if (strcmp(name, "range") == 0) | |
3723 return RangeNew(curbuf, RangeStart, RangeEnd); | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3724 else if (strcmp(name, "__members__") == 0) |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3725 return ObjectDir(NULL, CurrentAttrs); |
4319 | 3726 else |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3727 #if PY_MAJOR_VERSION < 3 |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3728 return Py_FindMethod(WindowMethods, self, name); |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3729 #else |
4319 | 3730 return NULL; |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3731 #endif |
4319 | 3732 } |
3733 | |
3734 static int | |
3735 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) | |
3736 { | |
3737 if (strcmp(name, "line") == 0) | |
3738 { | |
3739 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) | |
3740 return -1; | |
3741 | |
3742 return 0; | |
3743 } | |
4407 | 3744 else if (strcmp(name, "buffer") == 0) |
3745 { | |
3746 int count; | |
3747 | |
3748 if (value->ob_type != &BufferType) | |
3749 { | |
3750 PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object")); | |
3751 return -1; | |
3752 } | |
3753 | |
3754 if (CheckBuffer((BufferObject *)(value))) | |
3755 return -1; | |
3756 count = ((BufferObject *)(value))->buf->b_fnum; | |
3757 | |
4498 | 3758 VimTryStart(); |
4407 | 3759 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) |
3760 { | |
4498 | 3761 if (VimTryEnd()) |
3762 return -1; | |
4407 | 3763 PyErr_SetVim(_("failed to switch to given buffer")); |
3764 return -1; | |
3765 } | |
3766 | |
4498 | 3767 return VimTryEnd(); |
4407 | 3768 } |
3769 else if (strcmp(name, "window") == 0) | |
3770 { | |
3771 int count; | |
3772 | |
3773 if (value->ob_type != &WindowType) | |
3774 { | |
3775 PyErr_SetString(PyExc_TypeError, _("expected vim.window object")); | |
3776 return -1; | |
3777 } | |
3778 | |
3779 if (CheckWindow((WindowObject *)(value))) | |
3780 return -1; | |
3781 count = get_win_number(((WindowObject *)(value))->win, firstwin); | |
3782 | |
3783 if (!count) | |
3784 { | |
3785 PyErr_SetString(PyExc_ValueError, | |
3786 _("failed to find window in the current tab page")); | |
3787 return -1; | |
3788 } | |
3789 | |
4498 | 3790 VimTryStart(); |
4407 | 3791 win_goto(((WindowObject *)(value))->win); |
3792 if (((WindowObject *)(value))->win != curwin) | |
3793 { | |
4498 | 3794 if (VimTryEnd()) |
3795 return -1; | |
4407 | 3796 PyErr_SetString(PyExc_RuntimeError, |
3797 _("did not switch to the specified window")); | |
3798 return -1; | |
3799 } | |
3800 | |
4498 | 3801 return VimTryEnd(); |
4407 | 3802 } |
3803 else if (strcmp(name, "tabpage") == 0) | |
3804 { | |
3805 if (value->ob_type != &TabPageType) | |
3806 { | |
3807 PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object")); | |
3808 return -1; | |
3809 } | |
3810 | |
3811 if (CheckTabPage((TabPageObject *)(value))) | |
3812 return -1; | |
3813 | |
4498 | 3814 VimTryStart(); |
4407 | 3815 goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE); |
3816 if (((TabPageObject *)(value))->tab != curtab) | |
3817 { | |
4498 | 3818 if (VimTryEnd()) |
3819 return -1; | |
4407 | 3820 PyErr_SetString(PyExc_RuntimeError, |
3821 _("did not switch to the specified tab page")); | |
3822 return -1; | |
3823 } | |
3824 | |
4498 | 3825 return VimTryEnd(); |
4407 | 3826 } |
4319 | 3827 else |
3828 { | |
3829 PyErr_SetString(PyExc_AttributeError, name); | |
3830 return -1; | |
3831 } | |
3832 } | |
3833 | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3834 static struct PyMethodDef CurrentMethods[] = { |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3835 /* name, function, calling, documentation */ |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3836 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""}, |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3837 { NULL, NULL, 0, NULL} |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3838 }; |
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
3839 |
3618 | 3840 static void |
4486 | 3841 init_range_cmd(exarg_T *eap) |
3842 { | |
3843 RangeStart = eap->line1; | |
3844 RangeEnd = eap->line2; | |
3845 } | |
3846 | |
3847 static void | |
3848 init_range_eval(typval_T *rettv UNUSED) | |
3849 { | |
3850 RangeStart = (PyInt) curwin->w_cursor.lnum; | |
3851 RangeEnd = RangeStart; | |
3852 } | |
3853 | |
3854 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3855 run_cmd(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3856 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3857 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3858 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3859 ) |
4486 | 3860 { |
3861 PyRun_SimpleString((char *) cmd); | |
3862 } | |
3863 | |
3864 static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n "; | |
3865 static int code_hdr_len = 30; | |
3866 | |
3867 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3868 run_do(const char *cmd, void *arg UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3869 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3870 , PyGILState_STATE *pygilstate |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3871 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3872 ) |
4486 | 3873 { |
3874 PyInt lnum; | |
3875 size_t len; | |
3876 char *code; | |
3877 int status; | |
3878 PyObject *pyfunc, *pymain; | |
3879 | |
4575
626e9ccb7c48
updated for version 7.3.1035
Bram Moolenaar <bram@vim.org>
parents:
4523
diff
changeset
|
3880 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK) |
4486 | 3881 { |
3882 EMSG(_("cannot save undo information")); | |
3883 return; | |
3884 } | |
3885 | |
3886 len = code_hdr_len + STRLEN(cmd); | |
3887 code = PyMem_New(char, len + 1); | |
3888 memcpy(code, code_hdr, code_hdr_len); | |
3889 STRCPY(code + code_hdr_len, cmd); | |
3890 status = PyRun_SimpleString(code); | |
3891 PyMem_Free(code); | |
3892 | |
3893 if (status) | |
3894 { | |
3895 EMSG(_("failed to run the code")); | |
3896 return; | |
3897 } | |
3898 | |
3899 status = 0; | |
3900 pymain = PyImport_AddModule("__main__"); | |
3901 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3902 #ifdef PY_CAN_RECURSE |
4486 | 3903 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3904 #endif |
4486 | 3905 |
3906 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) | |
3907 { | |
3908 PyObject *line, *linenr, *ret; | |
3909 | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3910 #ifdef PY_CAN_RECURSE |
4486 | 3911 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3912 #endif |
4486 | 3913 if (!(line = GetBufferLine(curbuf, lnum))) |
3914 goto err; | |
3915 if (!(linenr = PyInt_FromLong((long) lnum))) | |
3916 { | |
3917 Py_DECREF(line); | |
3918 goto err; | |
3919 } | |
3920 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL); | |
3921 Py_DECREF(line); | |
3922 Py_DECREF(linenr); | |
3923 if (!ret) | |
3924 goto err; | |
3925 | |
3926 if (ret != Py_None) | |
3927 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL) | |
3928 goto err; | |
3929 | |
3930 Py_XDECREF(ret); | |
3931 PythonIO_Flush(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3932 #ifdef PY_CAN_RECURSE |
4486 | 3933 PyGILState_Release(*pygilstate); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3934 #endif |
4486 | 3935 } |
3936 goto out; | |
3937 err: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3938 #ifdef PY_CAN_RECURSE |
4486 | 3939 *pygilstate = PyGILState_Ensure(); |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3940 #endif |
4486 | 3941 PyErr_PrintEx(0); |
3942 PythonIO_Flush(); | |
3943 status = 1; | |
3944 out: | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3945 #ifdef PY_CAN_RECURSE |
4486 | 3946 if (!status) |
3947 *pygilstate = PyGILState_Ensure(); | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3948 #endif |
4486 | 3949 Py_DECREF(pyfunc); |
3950 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL); | |
3951 if (status) | |
3952 return; | |
3953 check_cursor(); | |
3954 update_curbuf(NOT_VALID); | |
3955 } | |
3956 | |
3957 static void | |
4511
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3958 run_eval(const char *cmd, typval_T *rettv |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3959 #ifdef PY_CAN_RECURSE |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3960 , PyGILState_STATE *pygilstate UNUSED |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3961 #endif |
ce94a870b59b
updated for version 7.3.1003
Bram Moolenaar <bram@vim.org>
parents:
4509
diff
changeset
|
3962 ) |
4486 | 3963 { |
3964 PyObject *r; | |
3965 | |
3966 r = PyRun_String((char *) cmd, Py_eval_input, globals, globals); | |
3967 if (r == NULL) | |
3968 { | |
3969 if (PyErr_Occurred() && !msg_silent) | |
3970 PyErr_PrintEx(0); | |
3971 EMSG(_("E858: Eval did not return a valid python object")); | |
3972 } | |
3973 else | |
3974 { | |
3975 if (ConvertFromPyObject(r, rettv) == -1) | |
3976 EMSG(_("E859: Failed to convert returned python object to vim value")); | |
3977 Py_DECREF(r); | |
3978 } | |
3979 PyErr_Clear(); | |
3980 } | |
3981 | |
3982 static void | |
3618 | 3983 set_ref_in_py(const int copyID) |
3984 { | |
3985 pylinkedlist_T *cur; | |
3986 dict_T *dd; | |
3987 list_T *ll; | |
3988 | |
3989 if (lastdict != NULL) | |
3990 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) | |
3991 { | |
3992 dd = ((DictionaryObject *) (cur->pll_obj))->dict; | |
3993 if (dd->dv_copyID != copyID) | |
3994 { | |
3995 dd->dv_copyID = copyID; | |
3996 set_ref_in_ht(&dd->dv_hashtab, copyID); | |
3997 } | |
3998 } | |
3999 | |
4000 if (lastlist != NULL) | |
4001 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) | |
4002 { | |
4003 ll = ((ListObject *) (cur->pll_obj))->list; | |
4004 if (ll->lv_copyID != copyID) | |
4005 { | |
4006 ll->lv_copyID = copyID; | |
4007 set_ref_in_list(ll, copyID); | |
4008 } | |
4009 } | |
4010 } | |
4011 | |
4012 static int | |
4013 set_string_copy(char_u *str, typval_T *tv) | |
4014 { | |
4015 tv->vval.v_string = vim_strsave(str); | |
4016 if (tv->vval.v_string == NULL) | |
4017 { | |
4018 PyErr_NoMemory(); | |
4019 return -1; | |
4020 } | |
4021 return 0; | |
4022 } | |
4023 | |
4385 | 4024 static int |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4025 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
4385 | 4026 { |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4027 dict_T *dict; |
4385 | 4028 char_u *key; |
4029 dictitem_T *di; | |
4030 PyObject *keyObject; | |
4031 PyObject *valObject; | |
4032 Py_ssize_t iter = 0; | |
4033 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4034 if (!(dict = dict_alloc())) |
4385 | 4035 return -1; |
4036 | |
4037 tv->v_type = VAR_DICT; | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4038 tv->vval.v_dict = dict; |
4385 | 4039 |
4040 while (PyDict_Next(obj, &iter, &keyObject, &valObject)) | |
4041 { | |
4042 DICTKEY_DECL | |
4043 | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4044 if (keyObject == NULL || valObject == NULL) |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4045 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4046 dict_unref(dict); |
4385 | 4047 return -1; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4048 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4049 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4050 if (!DICTKEY_SET_KEY) |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4051 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4052 dict_unref(dict); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4053 return -1; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4054 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4055 DICTKEY_CHECK_EMPTY(-1) |
4385 | 4056 |
4057 di = dictitem_alloc(key); | |
4058 | |
4059 DICTKEY_UNREF | |
4060 | |
4061 if (di == NULL) | |
4062 { | |
4063 PyErr_NoMemory(); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4064 dict_unref(dict); |
4385 | 4065 return -1; |
4066 } | |
4067 di->di_tv.v_lock = 0; | |
4068 | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4069 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1) |
4385 | 4070 { |
4071 vim_free(di); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4072 dict_unref(dict); |
4385 | 4073 return -1; |
4074 } | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4075 |
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4076 if (dict_add(dict, di) == FAIL) |
4385 | 4077 { |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4078 clear_tv(&di->di_tv); |
4385 | 4079 vim_free(di); |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4080 dict_unref(dict); |
4385 | 4081 PyErr_SetVim(_("failed to add key to dictionary")); |
4082 return -1; | |
4083 } | |
4084 } | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4085 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4086 --dict->dv_refcount; |
4385 | 4087 return 0; |
4088 } | |
4089 | |
4090 static int | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4091 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
4385 | 4092 { |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4093 dict_T *dict; |
4385 | 4094 char_u *key; |
4095 dictitem_T *di; | |
4096 PyObject *list; | |
4097 PyObject *litem; | |
4098 PyObject *keyObject; | |
4099 PyObject *valObject; | |
4100 Py_ssize_t lsize; | |
4101 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4102 if (!(dict = dict_alloc())) |
4385 | 4103 return -1; |
4104 | |
4105 tv->v_type = VAR_DICT; | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4106 tv->vval.v_dict = dict; |
4385 | 4107 |
4108 list = PyMapping_Items(obj); | |
4109 if (list == NULL) | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4110 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4111 dict_unref(dict); |
4385 | 4112 return -1; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4113 } |
4385 | 4114 lsize = PyList_Size(list); |
4115 while (lsize--) | |
4116 { | |
4117 DICTKEY_DECL | |
4118 | |
4119 litem = PyList_GetItem(list, lsize); | |
4120 if (litem == NULL) | |
4121 { | |
4122 Py_DECREF(list); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4123 dict_unref(dict); |
4385 | 4124 return -1; |
4125 } | |
4126 | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4127 if (!(keyObject = PyTuple_GetItem(litem, 0))) |
4385 | 4128 { |
4129 Py_DECREF(list); | |
4130 Py_DECREF(litem); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4131 dict_unref(dict); |
4385 | 4132 return -1; |
4133 } | |
4134 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4135 if (!DICTKEY_SET_KEY) |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4136 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4137 dict_unref(dict); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4138 Py_DECREF(list); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4139 Py_DECREF(litem); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4140 DICTKEY_UNREF |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4141 return -1; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4142 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4143 DICTKEY_CHECK_EMPTY(-1) |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4144 |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4145 if (!(valObject = PyTuple_GetItem(litem, 1))) |
4385 | 4146 { |
4147 Py_DECREF(list); | |
4148 Py_DECREF(litem); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4149 dict_unref(dict); |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4150 DICTKEY_UNREF |
4385 | 4151 return -1; |
4152 } | |
4153 | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4154 Py_DECREF(litem); |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4155 |
4385 | 4156 di = dictitem_alloc(key); |
4157 | |
4158 DICTKEY_UNREF | |
4159 | |
4160 if (di == NULL) | |
4161 { | |
4162 Py_DECREF(list); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4163 dict_unref(dict); |
4385 | 4164 PyErr_NoMemory(); |
4165 return -1; | |
4166 } | |
4167 di->di_tv.v_lock = 0; | |
4168 | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4169 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1) |
4385 | 4170 { |
4171 vim_free(di); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4172 dict_unref(dict); |
4385 | 4173 Py_DECREF(list); |
4174 return -1; | |
4175 } | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4176 |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4177 if (dict_add(dict, di) == FAIL) |
4385 | 4178 { |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4179 dictitem_free(di); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4180 dict_unref(dict); |
4385 | 4181 Py_DECREF(list); |
4182 PyErr_SetVim(_("failed to add key to dictionary")); | |
4183 return -1; | |
4184 } | |
4185 } | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4186 --dict->dv_refcount; |
4385 | 4187 Py_DECREF(list); |
4188 return 0; | |
4189 } | |
4190 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4191 static list_T * |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4192 py_list_alloc() |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4193 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4194 list_T *r; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4195 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4196 if (!(r = list_alloc())) |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4197 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4198 PyErr_NoMemory(); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4199 return NULL; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4200 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4201 ++r->lv_refcount; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4202 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4203 return r; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4204 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4205 |
4385 | 4206 static int |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4207 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
4385 | 4208 { |
4209 list_T *l; | |
4210 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4211 if (!(l = py_list_alloc())) |
4385 | 4212 return -1; |
4213 | |
4214 tv->v_type = VAR_LIST; | |
4215 tv->vval.v_list = l; | |
4216 | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4217 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
|
4218 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4219 list_unref(l); |
4385 | 4220 return -1; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4221 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4222 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4223 --l->lv_refcount; |
4385 | 4224 return 0; |
4225 } | |
4226 | |
4227 static int | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4228 pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
4385 | 4229 { |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4230 PyObject *iterator; |
4385 | 4231 PyObject *item; |
4232 list_T *l; | |
4233 listitem_T *li; | |
4234 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4235 if (!(l = py_list_alloc())) |
4385 | 4236 return -1; |
4237 | |
4238 tv->vval.v_list = l; | |
4239 tv->v_type = VAR_LIST; | |
4240 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4241 if (!(iterator = PyObject_GetIter(obj))) |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4242 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4243 list_unref(l); |
4385 | 4244 return -1; |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4245 } |
4385 | 4246 |
4605
38276cfe40b2
updated for version 7.3.1050
Bram Moolenaar <bram@vim.org>
parents:
4603
diff
changeset
|
4247 while ((item = PyIter_Next(iterator))) |
4385 | 4248 { |
4249 li = listitem_alloc(); | |
4250 if (li == NULL) | |
4251 { | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4252 list_unref(l); |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4253 Py_DECREF(iterator); |
4385 | 4254 PyErr_NoMemory(); |
4255 return -1; | |
4256 } | |
4257 li->li_tv.v_lock = 0; | |
4258 | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4259 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
|
4260 { |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4261 list_unref(l); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4262 listitem_free(li); |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4263 Py_DECREF(item); |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4264 Py_DECREF(iterator); |
4385 | 4265 return -1; |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4266 } |
4385 | 4267 |
4268 list_append(l, li); | |
4269 | |
4270 Py_DECREF(item); | |
4271 } | |
4272 | |
4273 Py_DECREF(iterator); | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4274 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4275 /* Iterator may have finished due to an exception */ |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4276 if (PyErr_Occurred()) |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4277 { |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4278 list_unref(l); |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4279 return -1; |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4280 } |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4281 |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4282 --l->lv_refcount; |
4385 | 4283 return 0; |
4284 } | |
4285 | |
3618 | 4286 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
4287 | |
4288 static int | |
4289 convert_dl(PyObject *obj, typval_T *tv, | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4290 pytotvfunc py_to_tv, PyObject *lookup_dict) |
3618 | 4291 { |
4292 PyObject *capsule; | |
4293 char hexBuf[sizeof(void *) * 2 + 3]; | |
4294 | |
4295 sprintf(hexBuf, "%p", obj); | |
4296 | |
3638 | 4297 # ifdef PY_USE_CAPSULE |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4298 capsule = PyDict_GetItemString(lookup_dict, hexBuf); |
3648 | 4299 # else |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4300 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf); |
3648 | 4301 # endif |
3618 | 4302 if (capsule == NULL) |
4303 { | |
3638 | 4304 # ifdef PY_USE_CAPSULE |
3618 | 4305 capsule = PyCapsule_New(tv, NULL, NULL); |
3648 | 4306 # else |
4307 capsule = PyCObject_FromVoidPtr(tv, NULL); | |
4308 # endif | |
4607
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4309 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule)) |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4310 { |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4311 Py_DECREF(capsule); |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4312 tv->v_type = VAR_UNKNOWN; |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4313 return -1; |
70600448f9e7
updated for version 7.3.1051
Bram Moolenaar <bram@vim.org>
parents:
4605
diff
changeset
|
4314 } |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4315 if (py_to_tv(obj, tv, lookup_dict) == -1) |
3618 | 4316 { |
4317 tv->v_type = VAR_UNKNOWN; | |
4318 return -1; | |
4319 } | |
4320 /* As we are not using copy_tv which increments reference count we must | |
4321 * do it ourself. */ | |
4322 switch(tv->v_type) | |
4323 { | |
4324 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; | |
4325 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; | |
4326 } | |
4327 } | |
4328 else | |
4329 { | |
3638 | 4330 typval_T *v; |
4331 | |
4332 # ifdef PY_USE_CAPSULE | |
4333 v = PyCapsule_GetPointer(capsule, NULL); | |
4334 # else | |
3648 | 4335 v = PyCObject_AsVoidPtr(capsule); |
3638 | 4336 # endif |
3618 | 4337 copy_tv(v, tv); |
4338 } | |
4339 return 0; | |
4340 } | |
4341 | |
4342 static int | |
4343 ConvertFromPyObject(PyObject *obj, typval_T *tv) | |
4344 { | |
4345 PyObject *lookup_dict; | |
4346 int r; | |
4347 | |
4617
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4348 if (!(lookup_dict = PyDict_New())) |
21a99611149b
updated for version 7.3.1056
Bram Moolenaar <bram@vim.org>
parents:
4611
diff
changeset
|
4349 return -1; |
3618 | 4350 r = _ConvertFromPyObject(obj, tv, lookup_dict); |
4351 Py_DECREF(lookup_dict); | |
4352 return r; | |
4353 } | |
4354 | |
4355 static int | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4356 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict) |
3618 | 4357 { |
4358 if (obj->ob_type == &DictionaryType) | |
4359 { | |
4360 tv->v_type = VAR_DICT; | |
4361 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); | |
4362 ++tv->vval.v_dict->dv_refcount; | |
4363 } | |
4364 else if (obj->ob_type == &ListType) | |
4365 { | |
4366 tv->v_type = VAR_LIST; | |
4367 tv->vval.v_list = (((ListObject *)(obj))->list); | |
4368 ++tv->vval.v_list->lv_refcount; | |
4369 } | |
4370 else if (obj->ob_type == &FunctionType) | |
4371 { | |
4372 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) | |
4373 return -1; | |
4374 | |
4375 tv->v_type = VAR_FUNC; | |
4376 func_ref(tv->vval.v_string); | |
4377 } | |
4378 else if (PyBytes_Check(obj)) | |
4379 { | |
3800 | 4380 char_u *result; |
4381 | |
4382 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) | |
4383 return -1; | |
3618 | 4384 if (result == NULL) |
4385 return -1; | |
4386 | |
4387 if (set_string_copy(result, tv) == -1) | |
4388 return -1; | |
4389 | |
4390 tv->v_type = VAR_STRING; | |
4391 } | |
4392 else if (PyUnicode_Check(obj)) | |
4393 { | |
4394 PyObject *bytes; | |
4395 char_u *result; | |
4396 | |
4397 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); | |
4398 if (bytes == NULL) | |
4399 return -1; | |
4400 | |
3800 | 4401 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
4402 return -1; | |
3618 | 4403 if (result == NULL) |
4404 return -1; | |
4405 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4406 if (set_string_copy(result, tv)) |
3618 | 4407 { |
4408 Py_XDECREF(bytes); | |
4409 return -1; | |
4410 } | |
4411 Py_XDECREF(bytes); | |
4412 | |
4413 tv->v_type = VAR_STRING; | |
4414 } | |
4321 | 4415 #if PY_MAJOR_VERSION < 3 |
3618 | 4416 else if (PyInt_Check(obj)) |
4417 { | |
4418 tv->v_type = VAR_NUMBER; | |
4419 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); | |
4420 } | |
4421 #endif | |
4422 else if (PyLong_Check(obj)) | |
4423 { | |
4424 tv->v_type = VAR_NUMBER; | |
4425 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); | |
4426 } | |
4427 else if (PyDict_Check(obj)) | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4428 return convert_dl(obj, tv, pydict_to_tv, lookup_dict); |
3618 | 4429 #ifdef FEAT_FLOAT |
4430 else if (PyFloat_Check(obj)) | |
4431 { | |
4432 tv->v_type = VAR_FLOAT; | |
4433 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); | |
4434 } | |
4435 #endif | |
4436 else if (PyIter_Check(obj)) | |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4437 return convert_dl(obj, tv, pyiter_to_tv, lookup_dict); |
3618 | 4438 else if (PySequence_Check(obj)) |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4439 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict); |
3618 | 4440 else if (PyMapping_Check(obj)) |
4601
19d406a8509d
updated for version 7.3.1048
Bram Moolenaar <bram@vim.org>
parents:
4599
diff
changeset
|
4441 return convert_dl(obj, tv, pymap_to_tv, lookup_dict); |
3618 | 4442 else |
4443 { | |
4405 | 4444 PyErr_SetString(PyExc_TypeError, |
4445 _("unable to convert to vim structure")); | |
3618 | 4446 return -1; |
4447 } | |
4448 return 0; | |
4449 } | |
4450 | |
4451 static PyObject * | |
4452 ConvertToPyObject(typval_T *tv) | |
4453 { | |
4454 if (tv == NULL) | |
4455 { | |
4456 PyErr_SetVim(_("NULL reference passed")); | |
4457 return NULL; | |
4458 } | |
4459 switch (tv->v_type) | |
4460 { | |
4461 case VAR_STRING: | |
3852 | 4462 return PyBytes_FromString(tv->vval.v_string == NULL |
4463 ? "" : (char *)tv->vval.v_string); | |
3618 | 4464 case VAR_NUMBER: |
4465 return PyLong_FromLong((long) tv->vval.v_number); | |
4466 #ifdef FEAT_FLOAT | |
4467 case VAR_FLOAT: | |
4468 return PyFloat_FromDouble((double) tv->vval.v_float); | |
4469 #endif | |
4470 case VAR_LIST: | |
4471 return ListNew(tv->vval.v_list); | |
4472 case VAR_DICT: | |
4473 return DictionaryNew(tv->vval.v_dict); | |
4474 case VAR_FUNC: | |
3852 | 4475 return FunctionNew(tv->vval.v_string == NULL |
4476 ? (char_u *)"" : tv->vval.v_string); | |
3618 | 4477 case VAR_UNKNOWN: |
4478 Py_INCREF(Py_None); | |
4479 return Py_None; | |
4480 default: | |
4481 PyErr_SetVim(_("internal error: invalid value type")); | |
4482 return NULL; | |
4483 } | |
4484 } | |
4319 | 4485 |
4486 typedef struct | |
4487 { | |
4488 PyObject_HEAD | |
4489 } CurrentObject; | |
4490 static PyTypeObject CurrentType; | |
4491 | |
4492 static void | |
4493 init_structs(void) | |
4494 { | |
4495 vim_memset(&OutputType, 0, sizeof(OutputType)); | |
4496 OutputType.tp_name = "vim.message"; | |
4497 OutputType.tp_basicsize = sizeof(OutputObject); | |
4498 OutputType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4499 OutputType.tp_doc = "vim message object"; | |
4500 OutputType.tp_methods = OutputMethods; | |
4501 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4502 OutputType.tp_getattro = (getattrofunc)OutputGetattro; |
4503 OutputType.tp_setattro = (setattrofunc)OutputSetattro; | |
4319 | 4504 OutputType.tp_alloc = call_PyType_GenericAlloc; |
4505 OutputType.tp_new = call_PyType_GenericNew; | |
4506 OutputType.tp_free = call_PyObject_Free; | |
4507 #else | |
4488 | 4508 OutputType.tp_getattr = (getattrfunc)OutputGetattr; |
4509 OutputType.tp_setattr = (setattrfunc)OutputSetattr; | |
4319 | 4510 #endif |
4511 | |
4397 | 4512 vim_memset(&IterType, 0, sizeof(IterType)); |
4513 IterType.tp_name = "vim.iter"; | |
4514 IterType.tp_basicsize = sizeof(IterObject); | |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
4515 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4397 | 4516 IterType.tp_doc = "generic iterator object"; |
4488 | 4517 IterType.tp_iter = (getiterfunc)IterIter; |
4518 IterType.tp_iternext = (iternextfunc)IterNext; | |
4519 IterType.tp_dealloc = (destructor)IterDestructor; | |
4520 IterType.tp_traverse = (traverseproc)IterTraverse; | |
4521 IterType.tp_clear = (inquiry)IterClear; | |
4397 | 4522 |
4319 | 4523 vim_memset(&BufferType, 0, sizeof(BufferType)); |
4524 BufferType.tp_name = "vim.buffer"; | |
4525 BufferType.tp_basicsize = sizeof(BufferType); | |
4488 | 4526 BufferType.tp_dealloc = (destructor)BufferDestructor; |
4527 BufferType.tp_repr = (reprfunc)BufferRepr; | |
4319 | 4528 BufferType.tp_as_sequence = &BufferAsSeq; |
4529 BufferType.tp_as_mapping = &BufferAsMapping; | |
4530 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4531 BufferType.tp_doc = "vim buffer object"; | |
4532 BufferType.tp_methods = BufferMethods; | |
4533 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4534 BufferType.tp_getattro = (getattrofunc)BufferGetattro; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4535 BufferType.tp_setattro = (setattrofunc)BufferSetattro; |
4319 | 4536 BufferType.tp_alloc = call_PyType_GenericAlloc; |
4537 BufferType.tp_new = call_PyType_GenericNew; | |
4538 BufferType.tp_free = call_PyObject_Free; | |
4539 #else | |
4488 | 4540 BufferType.tp_getattr = (getattrfunc)BufferGetattr; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4587
diff
changeset
|
4541 BufferType.tp_setattr = (setattrfunc)BufferSetattr; |
4319 | 4542 #endif |
4543 | |
4544 vim_memset(&WindowType, 0, sizeof(WindowType)); | |
4545 WindowType.tp_name = "vim.window"; | |
4546 WindowType.tp_basicsize = sizeof(WindowObject); | |
4488 | 4547 WindowType.tp_dealloc = (destructor)WindowDestructor; |
4548 WindowType.tp_repr = (reprfunc)WindowRepr; | |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
4549 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4319 | 4550 WindowType.tp_doc = "vim Window object"; |
4551 WindowType.tp_methods = WindowMethods; | |
4488 | 4552 WindowType.tp_traverse = (traverseproc)WindowTraverse; |
4553 WindowType.tp_clear = (inquiry)WindowClear; | |
4319 | 4554 #if PY_MAJOR_VERSION >= 3 |
4488 | 4555 WindowType.tp_getattro = (getattrofunc)WindowGetattro; |
4556 WindowType.tp_setattro = (setattrofunc)WindowSetattro; | |
4319 | 4557 WindowType.tp_alloc = call_PyType_GenericAlloc; |
4558 WindowType.tp_new = call_PyType_GenericNew; | |
4559 WindowType.tp_free = call_PyObject_Free; | |
4560 #else | |
4488 | 4561 WindowType.tp_getattr = (getattrfunc)WindowGetattr; |
4562 WindowType.tp_setattr = (setattrfunc)WindowSetattr; | |
4319 | 4563 #endif |
4564 | |
4401 | 4565 vim_memset(&TabPageType, 0, sizeof(TabPageType)); |
4566 TabPageType.tp_name = "vim.tabpage"; | |
4567 TabPageType.tp_basicsize = sizeof(TabPageObject); | |
4488 | 4568 TabPageType.tp_dealloc = (destructor)TabPageDestructor; |
4569 TabPageType.tp_repr = (reprfunc)TabPageRepr; | |
4401 | 4570 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT; |
4571 TabPageType.tp_doc = "vim tab page object"; | |
4572 TabPageType.tp_methods = TabPageMethods; | |
4573 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4574 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro; |
4401 | 4575 TabPageType.tp_alloc = call_PyType_GenericAlloc; |
4576 TabPageType.tp_new = call_PyType_GenericNew; | |
4577 TabPageType.tp_free = call_PyObject_Free; | |
4578 #else | |
4488 | 4579 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr; |
4401 | 4580 #endif |
4581 | |
4393 | 4582 vim_memset(&BufMapType, 0, sizeof(BufMapType)); |
4583 BufMapType.tp_name = "vim.bufferlist"; | |
4584 BufMapType.tp_basicsize = sizeof(BufMapObject); | |
4585 BufMapType.tp_as_mapping = &BufMapAsMapping; | |
4586 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4397 | 4587 BufMapType.tp_iter = BufMapIter; |
4319 | 4588 BufferType.tp_doc = "vim buffer list"; |
4589 | |
4590 vim_memset(&WinListType, 0, sizeof(WinListType)); | |
4591 WinListType.tp_name = "vim.windowlist"; | |
4592 WinListType.tp_basicsize = sizeof(WinListType); | |
4593 WinListType.tp_as_sequence = &WinListAsSeq; | |
4594 WinListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4595 WinListType.tp_doc = "vim window list"; | |
4488 | 4596 WinListType.tp_dealloc = (destructor)WinListDestructor; |
4401 | 4597 |
4598 vim_memset(&TabListType, 0, sizeof(TabListType)); | |
4599 TabListType.tp_name = "vim.tabpagelist"; | |
4600 TabListType.tp_basicsize = sizeof(TabListType); | |
4601 TabListType.tp_as_sequence = &TabListAsSeq; | |
4602 TabListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4603 TabListType.tp_doc = "vim tab page list"; | |
4319 | 4604 |
4605 vim_memset(&RangeType, 0, sizeof(RangeType)); | |
4606 RangeType.tp_name = "vim.range"; | |
4607 RangeType.tp_basicsize = sizeof(RangeObject); | |
4488 | 4608 RangeType.tp_dealloc = (destructor)RangeDestructor; |
4609 RangeType.tp_repr = (reprfunc)RangeRepr; | |
4319 | 4610 RangeType.tp_as_sequence = &RangeAsSeq; |
4611 RangeType.tp_as_mapping = &RangeAsMapping; | |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
4612 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4319 | 4613 RangeType.tp_doc = "vim Range object"; |
4614 RangeType.tp_methods = RangeMethods; | |
4500 | 4615 RangeType.tp_traverse = (traverseproc)RangeTraverse; |
4616 RangeType.tp_clear = (inquiry)RangeClear; | |
4319 | 4617 #if PY_MAJOR_VERSION >= 3 |
4488 | 4618 RangeType.tp_getattro = (getattrofunc)RangeGetattro; |
4319 | 4619 RangeType.tp_alloc = call_PyType_GenericAlloc; |
4620 RangeType.tp_new = call_PyType_GenericNew; | |
4621 RangeType.tp_free = call_PyObject_Free; | |
4622 #else | |
4488 | 4623 RangeType.tp_getattr = (getattrfunc)RangeGetattr; |
4319 | 4624 #endif |
4625 | |
4626 vim_memset(&CurrentType, 0, sizeof(CurrentType)); | |
4627 CurrentType.tp_name = "vim.currentdata"; | |
4628 CurrentType.tp_basicsize = sizeof(CurrentObject); | |
4629 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4630 CurrentType.tp_doc = "vim current object"; | |
4599
89bec74fd793
updated for version 7.3.1047
Bram Moolenaar <bram@vim.org>
parents:
4597
diff
changeset
|
4631 CurrentType.tp_methods = CurrentMethods; |
4319 | 4632 #if PY_MAJOR_VERSION >= 3 |
4488 | 4633 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro; |
4634 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro; | |
4319 | 4635 #else |
4488 | 4636 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr; |
4637 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr; | |
4319 | 4638 #endif |
4639 | |
4640 vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); | |
4641 DictionaryType.tp_name = "vim.dictionary"; | |
4642 DictionaryType.tp_basicsize = sizeof(DictionaryObject); | |
4488 | 4643 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor; |
4319 | 4644 DictionaryType.tp_as_mapping = &DictionaryAsMapping; |
4645 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4646 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; | |
4647 DictionaryType.tp_methods = DictionaryMethods; | |
4648 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4649 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro; |
4650 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro; | |
4319 | 4651 #else |
4488 | 4652 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr; |
4653 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr; | |
4319 | 4654 #endif |
4655 | |
4656 vim_memset(&ListType, 0, sizeof(ListType)); | |
4657 ListType.tp_name = "vim.list"; | |
4488 | 4658 ListType.tp_dealloc = (destructor)ListDestructor; |
4319 | 4659 ListType.tp_basicsize = sizeof(ListObject); |
4660 ListType.tp_as_sequence = &ListAsSeq; | |
4661 ListType.tp_as_mapping = &ListAsMapping; | |
4662 ListType.tp_flags = Py_TPFLAGS_DEFAULT; | |
4663 ListType.tp_doc = "list pushing modifications to vim structure"; | |
4664 ListType.tp_methods = ListMethods; | |
4488 | 4665 ListType.tp_iter = (getiterfunc)ListIter; |
4319 | 4666 #if PY_MAJOR_VERSION >= 3 |
4488 | 4667 ListType.tp_getattro = (getattrofunc)ListGetattro; |
4668 ListType.tp_setattro = (setattrofunc)ListSetattro; | |
4319 | 4669 #else |
4488 | 4670 ListType.tp_getattr = (getattrfunc)ListGetattr; |
4671 ListType.tp_setattr = (setattrfunc)ListSetattr; | |
4319 | 4672 #endif |
4673 | |
4674 vim_memset(&FunctionType, 0, sizeof(FunctionType)); | |
4397 | 4675 FunctionType.tp_name = "vim.function"; |
4319 | 4676 FunctionType.tp_basicsize = sizeof(FunctionObject); |
4488 | 4677 FunctionType.tp_dealloc = (destructor)FunctionDestructor; |
4678 FunctionType.tp_call = (ternaryfunc)FunctionCall; | |
4319 | 4679 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; |
4680 FunctionType.tp_doc = "object that calls vim function"; | |
4681 FunctionType.tp_methods = FunctionMethods; | |
4682 #if PY_MAJOR_VERSION >= 3 | |
4488 | 4683 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro; |
4319 | 4684 #else |
4488 | 4685 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr; |
4319 | 4686 #endif |
4687 | |
4350 | 4688 vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
4689 OptionsType.tp_name = "vim.options"; | |
4690 OptionsType.tp_basicsize = sizeof(OptionsObject); | |
4611
49f0fcd9762c
updated for version 7.3.1053
Bram Moolenaar <bram@vim.org>
parents:
4609
diff
changeset
|
4691 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC; |
4350 | 4692 OptionsType.tp_doc = "object for manipulating options"; |
4693 OptionsType.tp_as_mapping = &OptionsAsMapping; | |
4488 | 4694 OptionsType.tp_dealloc = (destructor)OptionsDestructor; |
4695 OptionsType.tp_traverse = (traverseproc)OptionsTraverse; | |
4696 OptionsType.tp_clear = (inquiry)OptionsClear; | |
4350 | 4697 |
4319 | 4698 #if PY_MAJOR_VERSION >= 3 |
4699 vim_memset(&vimmodule, 0, sizeof(vimmodule)); | |
4700 vimmodule.m_name = "vim"; | |
4701 vimmodule.m_doc = "Vim Python interface\n"; | |
4702 vimmodule.m_size = -1; | |
4703 vimmodule.m_methods = VimMethods; | |
4704 #endif | |
4705 } | |
4494 | 4706 |
4707 #define PYTYPE_READY(type) \ | |
4708 if (PyType_Ready(&type)) \ | |
4709 return -1; | |
4710 | |
4711 static int | |
4712 init_types() | |
4713 { | |
4714 PYTYPE_READY(IterType); | |
4715 PYTYPE_READY(BufferType); | |
4716 PYTYPE_READY(RangeType); | |
4717 PYTYPE_READY(WindowType); | |
4718 PYTYPE_READY(TabPageType); | |
4719 PYTYPE_READY(BufMapType); | |
4720 PYTYPE_READY(WinListType); | |
4721 PYTYPE_READY(TabListType); | |
4722 PYTYPE_READY(CurrentType); | |
4723 PYTYPE_READY(DictionaryType); | |
4724 PYTYPE_READY(ListType); | |
4725 PYTYPE_READY(FunctionType); | |
4726 PYTYPE_READY(OptionsType); | |
4727 PYTYPE_READY(OutputType); | |
4728 return 0; | |
4729 } | |
4730 | |
4731 static BufMapObject TheBufferMap = | |
4732 { | |
4733 PyObject_HEAD_INIT(&BufMapType) | |
4734 }; | |
4735 | |
4736 static WinListObject TheWindowList = | |
4737 { | |
4738 PyObject_HEAD_INIT(&WinListType) | |
4739 NULL | |
4740 }; | |
4741 | |
4742 static CurrentObject TheCurrent = | |
4743 { | |
4744 PyObject_HEAD_INIT(&CurrentType) | |
4745 }; | |
4746 | |
4747 static TabListObject TheTabPageList = | |
4748 { | |
4749 PyObject_HEAD_INIT(&TabListType) | |
4750 }; | |
4751 | |
4752 static struct numeric_constant { | |
4753 char *name; | |
4754 int value; | |
4755 } numeric_constants[] = { | |
4756 {"VAR_LOCKED", VAR_LOCKED}, | |
4757 {"VAR_FIXED", VAR_FIXED}, | |
4758 {"VAR_SCOPE", VAR_SCOPE}, | |
4759 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, | |
4760 }; | |
4761 | |
4762 static struct object_constant { | |
4763 char *name; | |
4764 PyObject *value; | |
4765 } object_constants[] = { | |
4766 {"buffers", (PyObject *)(void *)&TheBufferMap}, | |
4767 {"windows", (PyObject *)(void *)&TheWindowList}, | |
4768 {"tabpages", (PyObject *)(void *)&TheTabPageList}, | |
4769 {"current", (PyObject *)(void *)&TheCurrent}, | |
4496 | 4770 |
4771 {"Buffer", (PyObject *)&BufferType}, | |
4772 {"Range", (PyObject *)&RangeType}, | |
4773 {"Window", (PyObject *)&WindowType}, | |
4774 {"TabPage", (PyObject *)&TabPageType}, | |
4775 {"Dictionary", (PyObject *)&DictionaryType}, | |
4776 {"List", (PyObject *)&ListType}, | |
4777 {"Function", (PyObject *)&FunctionType}, | |
4778 {"Options", (PyObject *)&OptionsType}, | |
4494 | 4779 }; |
4780 | |
4781 typedef int (*object_adder)(PyObject *, const char *, PyObject *); | |
4782 | |
4783 #define ADD_OBJECT(m, name, obj) \ | |
4784 if (add_object(m, name, obj)) \ | |
4785 return -1; | |
4786 | |
4787 #define ADD_CHECKED_OBJECT(m, name, obj) \ | |
4788 { \ | |
4789 PyObject *value = obj; \ | |
4790 if (!value) \ | |
4791 return -1; \ | |
4792 ADD_OBJECT(m, name, value); \ | |
4793 } | |
4794 | |
4795 static int | |
4796 populate_module(PyObject *m, object_adder add_object) | |
4797 { | |
4798 int i; | |
4799 | |
4800 for (i = 0; i < (int)(sizeof(numeric_constants) | |
4801 / sizeof(struct numeric_constant)); | |
4802 ++i) | |
4803 ADD_CHECKED_OBJECT(m, numeric_constants[i].name, | |
4804 PyInt_FromLong(numeric_constants[i].value)); | |
4805 | |
4806 for (i = 0; i < (int)(sizeof(object_constants) | |
4807 / sizeof(struct object_constant)); | |
4808 ++i) | |
4809 { | |
4810 PyObject *value; | |
4811 | |
4812 value = object_constants[i].value; | |
4813 Py_INCREF(value); | |
4814 ADD_OBJECT(m, object_constants[i].name, value); | |
4815 } | |
4816 | |
4817 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) | |
4818 return -1; | |
4819 ADD_OBJECT(m, "error", VimError); | |
4820 | |
4821 ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict)); | |
4822 ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict)); | |
4823 ADD_CHECKED_OBJECT(m, "options", | |
4824 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL)); | |
4825 return 0; | |
4826 } |