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