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