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