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