annotate src/if_py_both.h @ 3792:73557eda7027 v7.3.654

updated for version 7.3.654 Problem: When creating a Vim dictionary from Python objects an empty key might be used. Solution: Do not use empty keys, throw an IndexError. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Wed, 05 Sep 2012 18:45:28 +0200
parents 5edf3c51aa93
children 15cdcb8ddcfb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
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 /*
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10 * Python extensions by Paul Moore, David Leonard, Roland Puntaier.
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 *
76f0c4918f5c Move 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 * 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
13 */
76f0c4918f5c Move 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
3734
956b1ed19282 updated for version 7.3.626
Bram Moolenaar <bram@vim.org>
parents: 3703
diff changeset
15 #if PY_VERSION_HEX < 0x02050000
956b1ed19282 updated for version 7.3.626
Bram Moolenaar <bram@vim.org>
parents: 3703
diff changeset
16 typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
956b1ed19282 updated for version 7.3.626
Bram Moolenaar <bram@vim.org>
parents: 3703
diff changeset
17 #endif
956b1ed19282 updated for version 7.3.626
Bram Moolenaar <bram@vim.org>
parents: 3703
diff changeset
18
2919
b67d3a44262a updated for version 7.3.232
Bram Moolenaar <bram@vim.org>
parents: 2894
diff changeset
19 #ifdef FEAT_MBYTE
b67d3a44262a updated for version 7.3.232
Bram Moolenaar <bram@vim.org>
parents: 2894
diff changeset
20 # define ENC_OPT p_enc
b67d3a44262a updated for version 7.3.232
Bram Moolenaar <bram@vim.org>
parents: 2894
diff changeset
21 #else
b67d3a44262a updated for version 7.3.232
Bram Moolenaar <bram@vim.org>
parents: 2894
diff changeset
22 # define ENC_OPT "latin1"
b67d3a44262a updated for version 7.3.232
Bram Moolenaar <bram@vim.org>
parents: 2894
diff changeset
23 #endif
b67d3a44262a updated for version 7.3.232
Bram Moolenaar <bram@vim.org>
parents: 2894
diff changeset
24
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
25 /*
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 * 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
27 */
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 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
29 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
30 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
32
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33 /*
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 * 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
35 */
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 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
37 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
38 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41 /* Output object definition
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42 */
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44 static PyObject *OutputWrite(PyObject *, PyObject *);
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45 static PyObject *OutputWritelines(PyObject *, PyObject *);
2727
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
46 static PyObject *OutputFlush(PyObject *, PyObject *);
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
47
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
48 /* 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
49 typedef void (*writefn)(char_u *);
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50 static void writer(writefn fn, char_u *str, 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
51
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 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
53 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
54 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
55 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
56 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
57 } 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
58
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 static struct PyMethodDef OutputMethods[] = {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 /* name, function, calling, documentation */
2727
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
61 {"write", OutputWrite, 1, ""},
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
62 {"writelines", OutputWritelines, 1, ""},
3638
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
63 {"flush", OutputFlush, 1, ""},
2727
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
64 { NULL, NULL, 0, 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
65 };
76f0c4918f5c Move 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
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
67 #define PyErr_SetVim(str) PyErr_SetString(VimError, str)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
68
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
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 /* Output buffer management
76f0c4918f5c Move 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
76f0c4918f5c Move 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 static PyObject *
76f0c4918f5c Move 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 OutputWrite(PyObject *self, PyObject *args)
76f0c4918f5c Move 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 {
3788
5edf3c51aa93 updated for version 7.3.652
Bram Moolenaar <bram@vim.org>
parents: 3734
diff changeset
77 Py_ssize_t len;
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
78 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
79 int error = ((OutputObject *)(self))->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
80
3056
2665b456ee59 updated for version 7.3.300
Bram Moolenaar <bram@vim.org>
parents: 2933
diff changeset
81 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
82 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
83
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84 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
85 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
86 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
87 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
88 Py_END_ALLOW_THREADS
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
89 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
90
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 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
92 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
93 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95 static PyObject *
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 OutputWritelines(PyObject *self, PyObject *args)
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98 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
99 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
100 PyObject *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
101 int error = ((OutputObject *)(self))->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
102
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103 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
104 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
105 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
106
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
107 if (!PyList_Check(list))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
108 {
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
109 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
110 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
111 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
112 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 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
115
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116 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
117 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 PyObject *line = PyList_GetItem(list, i);
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
119 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
120 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
121
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
122 if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
123 {
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
124 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
125 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
126 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
127 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 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
130 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
131 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
132 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
133 Py_END_ALLOW_THREADS
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
134 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
135 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137 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
138 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
139 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
140 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
141
2727
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
142 static PyObject *
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
143 OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED)
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
144 {
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
145 /* do nothing */
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
146 Py_INCREF(Py_None);
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
147 return Py_None;
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
148 }
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
149
62e8d93241cd updated for version 7.3.142
Bram Moolenaar <bram@vim.org>
parents: 2615
diff changeset
150
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
151 /* Buffer IO, we write one whole line at a time. */
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
152 static garray_T io_ga = {0, 0, 1, 80, 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
153 static writefn old_fn = 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
154
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
155 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
156 PythonIO_Flush(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
157 {
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
158 if (old_fn != NULL && io_ga.ga_len > 0)
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
159 {
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
160 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
161 old_fn((char_u *)io_ga.ga_data);
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
162 }
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
163 io_ga.ga_len = 0;
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
164 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
165
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
166 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
167 writer(writefn fn, char_u *str, 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
168 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
169 char_u *ptr;
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
170
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
171 /* Flush when switching output function. */
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
172 if (fn != old_fn)
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
173 PythonIO_Flush();
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
174 old_fn = fn;
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
175
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
176 /* Write each NL separated line. Text after the last NL is kept for
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
177 * writing later. */
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
178 while (n > 0 && (ptr = memchr(str, '\n', n)) != 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
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 PyInt len = ptr - str;
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
181
2615
c6fe65c000d2 updated for version 7.3.037
Bram Moolenaar <bram@vim.org>
parents: 2582
diff changeset
182 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
183 break;
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
184
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
185 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
186 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
187 fn((char_u *)io_ga.ga_data);
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
188 str = ptr + 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
189 n -= len + 1;
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
190 io_ga.ga_len = 0;
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
191 }
76f0c4918f5c Move 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
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
193 /* Put the remaining text into io_ga for later printing. */
2615
c6fe65c000d2 updated for version 7.3.037
Bram Moolenaar <bram@vim.org>
parents: 2582
diff changeset
194 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
195 {
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
196 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
2615
c6fe65c000d2 updated for version 7.3.037
Bram Moolenaar <bram@vim.org>
parents: 2582
diff changeset
197 io_ga.ga_len += (int)n;
2582
2cdbc91795ca updated for version 7.3.007
Bram Moolenaar <bram@vim.org>
parents: 2520
diff changeset
198 }
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
199 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
200
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
201 /***************/
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
202
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
203 static PyTypeObject 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
204
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
205 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
206 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
207 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
208 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
209 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
210 };
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
211
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
212 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
213 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
214 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
215 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
216 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
217 };
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
218
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
219 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
220 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
221 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
222 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
223 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
224
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
225 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
226 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
227 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
228 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
229 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
230
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
231 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
232 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
233
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
234
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
235 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
236
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
237 /* 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
238 * 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
239 */
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
240 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
241 VimErrorCheck(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
242 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
243 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
244 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
245 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
246 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
247 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
248 else if (did_emsg && !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
249 {
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
250 PyErr_SetNone(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
251 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
252 }
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
253
76f0c4918f5c Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
254 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
255 }
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
256
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
257 /* Vim module - Implementation
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
258 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
259 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
260 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
261 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
262 char *cmd;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
263 PyObject *result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
264
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
265 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
266 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
267
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
268 PyErr_Clear();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
269
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
270 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
271 Python_Lock_Vim();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
272
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
273 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
274 update_screen(VALID);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
275
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
276 Python_Release_Vim();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
277 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
278
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
279 if (VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
280 result = NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
281 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
282 result = Py_None;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
283
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
284 Py_XINCREF(result);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
285 return result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
286 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
287
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
288 #ifdef FEAT_EVAL
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
289 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
290 * 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
291 * 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
292 *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
293 * 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
294 * you call VimToPython.
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
295 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
296 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
297 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
298 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
299 PyObject *result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
300 PyObject *newObj;
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
301 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
302
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
303 /* Avoid infinite recursion */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
304 if (depth > 100)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
305 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
306 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
307 result = Py_None;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
308 return result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
309 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
310
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
311 /* Check if we run into a recursive loop. The item must be in lookupDict
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
312 * 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
313 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
314 || (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
315 {
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
316 sprintf(ptrBuf, "%p",
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
317 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
318 : (void *)our_tv->vval.v_dict);
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
319 result = PyDict_GetItemString(lookupDict, ptrBuf);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
320 if (result != NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
321 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
322 Py_INCREF(result);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
323 return result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
324 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
325 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
326
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
327 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
328 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
329 result = Py_BuildValue("s", our_tv->vval.v_string);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
330 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
331 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
332 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
333 char buf[NUMBUFLEN];
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
334
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
335 /* 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
336 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
337 result = Py_BuildValue("s", buf);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
338 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
339 # ifdef FEAT_FLOAT
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
340 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
341 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
342 char buf[NUMBUFLEN];
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
343
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
344 sprintf(buf, "%f", our_tv->vval.v_float);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
345 result = Py_BuildValue("s", buf);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
346 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
347 # endif
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
348 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
349 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
350 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
351 listitem_T *curr;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
352
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
353 result = PyList_New(0);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
354
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
355 if (list != NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
356 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
357 PyDict_SetItemString(lookupDict, ptrBuf, result);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
358
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
359 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
360 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
361 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
362 PyList_Append(result, newObj);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
363 Py_DECREF(newObj);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
364 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
365 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
366 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
367 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
368 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
369 result = PyDict_New();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
370
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
371 if (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
372 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
373 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
374 long_u todo = ht->ht_used;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
375 hashitem_T *hi;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
376 dictitem_T *di;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
377
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
378 PyDict_SetItemString(lookupDict, ptrBuf, result);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
379
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
380 for (hi = ht->ht_array; todo > 0; ++hi)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
381 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
382 if (!HASHITEM_EMPTY(hi))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
383 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
384 --todo;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
385
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
386 di = dict_lookup(hi);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
387 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
388 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
389 Py_DECREF(newObj);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
390 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
391 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
392 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
393 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
394 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
395 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
396 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
397 result = Py_None;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
398 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
399
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
400 return result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
401 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
402 #endif
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
403
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
404 static PyObject *
2520
6768ebd0bc04 Remove unused code.
Bram Moolenaar <bram@vim.org>
parents: 2447
diff changeset
405 VimEval(PyObject *self UNUSED, PyObject *args UNUSED)
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
406 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
407 #ifdef FEAT_EVAL
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
408 char *expr;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
409 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
410 PyObject *result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
411 PyObject *lookup_dict;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
412
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
413 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
414 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
415
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
416 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
417 Python_Lock_Vim();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
418 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
419
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
420 Python_Release_Vim();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
421 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
422
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
423 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
424 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
425 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
426 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
427 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
428
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
429 /* 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
430 * 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
431 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
432 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
433 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
434
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
435
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
436 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
437 Python_Lock_Vim();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
438 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
439 Python_Release_Vim();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
440 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
441
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
442 return result;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
443 #else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
444 PyErr_SetVim(_("expressions disabled at compile time"));
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
445 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
446 #endif
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
447 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
448
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
449 static PyObject *ConvertToPyObject(typval_T *);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
450
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
451 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
452 VimEvalPy(PyObject *self UNUSED, PyObject *args UNUSED)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
453 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
454 #ifdef FEAT_EVAL
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
455 char *expr;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
456 typval_T *our_tv;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
457 PyObject *result;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
458
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
459 if (!PyArg_ParseTuple(args, "s", &expr))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
460 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
461
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
462 Py_BEGIN_ALLOW_THREADS
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
463 Python_Lock_Vim();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
464 our_tv = eval_expr((char_u *)expr, NULL);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
465
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
466 Python_Release_Vim();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
467 Py_END_ALLOW_THREADS
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
468
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
469 if (our_tv == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
470 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
471 PyErr_SetVim(_("invalid expression"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
472 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
473 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
474
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
475 result = ConvertToPyObject(our_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
476 Py_BEGIN_ALLOW_THREADS
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
477 Python_Lock_Vim();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
478 free_tv(our_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
479 Python_Release_Vim();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
480 Py_END_ALLOW_THREADS
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
481
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
482 return result;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
483 #else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
484 PyErr_SetVim(_("expressions disabled at compile time"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
485 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
486 #endif
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
487 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
488
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
489 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
490 VimStrwidth(PyObject *self UNUSED, PyObject *args)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
491 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
492 char *expr;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
493
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
494 if (!PyArg_ParseTuple(args, "s", &expr))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
495 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
496
3640
f02b6ad168ae updated for version 7.3.580
Bram Moolenaar <bram@vim.org>
parents: 3638
diff changeset
497 return PyLong_FromLong(mb_string2cells((char_u *)expr, (int)STRLEN(expr)));
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
498 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
499
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
500 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
501 * Vim module - Definitions
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
502 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
503
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
504 static struct PyMethodDef VimMethods[] = {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
505 /* name, function, calling, documentation */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
506 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
507 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
3638
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
508 {"bindeval", VimEvalPy, 1, "Like eval(), but returns objects attached to vim ones"},
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
509 {"strwidth", VimStrwidth, 1, "Screen string width, counts <Tab> as having width 1"},
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
510 { NULL, NULL, 0, NULL }
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 typedef struct
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
514 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
515 PyObject_HEAD
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
516 buf_T *buf;
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
517 } BufferObject;
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
518
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
519 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
520
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
521 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
522 * Buffer list object - Implementation
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
523 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
524
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
525 static PyInt
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
526 BufListLength(PyObject *self UNUSED)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
527 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
528 buf_T *b = firstbuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
529 PyInt n = 0;
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 while (b)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
532 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
533 ++n;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
534 b = b->b_next;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
535 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
536
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
537 return n;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
538 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
539
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
540 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
541 BufListItem(PyObject *self UNUSED, PyInt n)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
542 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
543 buf_T *b;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
544
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
545 for (b = firstbuf; b; b = b->b_next, --n)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
546 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
547 if (n == 0)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
548 return BufferNew(b);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
549 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
550
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
551 PyErr_SetString(PyExc_IndexError, _("no such buffer"));
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
552 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
553 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
554
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
555 typedef struct
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
556 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
557 PyObject_HEAD
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
558 win_T *win;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
559 } WindowObject;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
560
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
561 static int ConvertFromPyObject(PyObject *, typval_T *);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
562 static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
563
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
564 typedef struct pylinkedlist_S {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
565 struct pylinkedlist_S *pll_next;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
566 struct pylinkedlist_S *pll_prev;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
567 PyObject *pll_obj;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
568 } pylinkedlist_T;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
569
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
570 static pylinkedlist_T *lastdict = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
571 static pylinkedlist_T *lastlist = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
572
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
573 static void
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
574 pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
575 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
576 if (ref->pll_prev == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
577 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
578 if (ref->pll_next == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
579 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
580 *last = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
581 return;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
582 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
583 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
584 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
585 ref->pll_prev->pll_next = ref->pll_next;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
586
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
587 if (ref->pll_next == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
588 *last = ref->pll_prev;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
589 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
590 ref->pll_next->pll_prev = ref->pll_prev;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
591 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
592
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
593 static void
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
594 pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
595 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
596 if (*last == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
597 ref->pll_prev = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
598 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
599 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
600 (*last)->pll_next = ref;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
601 ref->pll_prev = *last;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
602 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
603 ref->pll_next = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
604 ref->pll_obj = self;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
605 *last = ref;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
606 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
607
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
608 static PyTypeObject DictionaryType;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
609
3792
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
610 #define DICTKEY_GET_NOTEMPTY(err) \
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
611 DICTKEY_GET(err) \
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
612 if (*key == NUL) \
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
613 { \
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
614 PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
615 return err; \
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
616 }
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
617
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
618 typedef struct
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
619 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
620 PyObject_HEAD
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
621 dict_T *dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
622 pylinkedlist_T ref;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
623 } DictionaryObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
624
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
625 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
626 DictionaryNew(dict_T *dict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
627 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
628 DictionaryObject *self;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
629
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
630 self = PyObject_NEW(DictionaryObject, &DictionaryType);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
631 if (self == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
632 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
633 self->dict = dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
634 ++dict->dv_refcount;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
635
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
636 pyll_add((PyObject *)(self), &self->ref, &lastdict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
637
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
638 return (PyObject *)(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
639 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
640
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
641 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
642 pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
643 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
644 dict_T *d;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
645 char_u *key;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
646 dictitem_T *di;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
647 PyObject *keyObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
648 PyObject *valObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
649 Py_ssize_t iter = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
650
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
651 d = dict_alloc();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
652 if (d == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
653 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
654 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
655 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
656 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
657
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
658 tv->v_type = VAR_DICT;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
659 tv->vval.v_dict = d;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
660
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
661 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
662 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
663 DICTKEY_DECL
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
664
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
665 if (keyObject == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
666 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
667 if (valObject == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
668 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
669
3792
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
670 DICTKEY_GET_NOTEMPTY(-1)
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
671
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
672 di = dictitem_alloc(key);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
673
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
674 DICTKEY_UNREF
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
675
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
676 if (di == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
677 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
678 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
679 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
680 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
681 di->di_tv.v_lock = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
682
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
683 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
684 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
685 vim_free(di);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
686 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
687 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
688 if (dict_add(d, di) == FAIL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
689 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
690 vim_free(di);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
691 PyErr_SetVim(_("failed to add key to dictionary"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
692 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
693 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
694 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
695 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
696 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
697
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
698 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
699 pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
700 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
701 dict_T *d;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
702 char_u *key;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
703 dictitem_T *di;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
704 PyObject *list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
705 PyObject *litem;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
706 PyObject *keyObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
707 PyObject *valObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
708 Py_ssize_t lsize;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
709
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
710 d = dict_alloc();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
711 if (d == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
712 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
713 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
714 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
715 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
716
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
717 tv->v_type = VAR_DICT;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
718 tv->vval.v_dict = d;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
719
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
720 list = PyMapping_Items(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
721 lsize = PyList_Size(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
722 while (lsize--)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
723 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
724 DICTKEY_DECL
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
725
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
726 litem = PyList_GetItem(list, lsize);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
727 if (litem == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
728 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
729 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
730 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
731 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
732
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
733 keyObject = PyTuple_GetItem(litem, 0);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
734 if (keyObject == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
735 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
736 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
737 Py_DECREF(litem);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
738 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
739 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
740
3792
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
741 DICTKEY_GET_NOTEMPTY(-1)
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
742
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
743 valObject = PyTuple_GetItem(litem, 1);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
744 if (valObject == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
745 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
746 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
747 Py_DECREF(litem);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
748 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
749 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
750
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
751 di = dictitem_alloc(key);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
752
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
753 DICTKEY_UNREF
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
754
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
755 if (di == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
756 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
757 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
758 Py_DECREF(litem);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
759 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
760 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
761 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
762 di->di_tv.v_lock = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
763
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
764 if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
765 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
766 vim_free(di);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
767 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
768 Py_DECREF(litem);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
769 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
770 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
771 if (dict_add(d, di) == FAIL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
772 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
773 vim_free(di);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
774 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
775 Py_DECREF(litem);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
776 PyErr_SetVim(_("failed to add key to dictionary"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
777 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
778 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
779 Py_DECREF(litem);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
780 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
781 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
782 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
783 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
784
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
785 static PyInt
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
786 DictionaryLength(PyObject *self)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
787 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
788 return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used)));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
789 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
790
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
791 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
792 DictionaryItem(PyObject *self, PyObject *keyObject)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
793 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
794 char_u *key;
3792
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
795 dictitem_T *di;
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
796 DICTKEY_DECL
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
797
3792
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
798 DICTKEY_GET_NOTEMPTY(NULL)
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
799
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
800 di = dict_find(((DictionaryObject *) (self))->dict, key, -1);
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
801
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
802 if (di == NULL)
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
803 {
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
804 PyErr_SetString(PyExc_IndexError, _("no such key in dictionary"));
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
805 return NULL;
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
806 }
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
807
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
808 DICTKEY_UNREF
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
809
3792
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
810 return ConvertToPyObject(&di->di_tv);
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
811 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
812
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
813 static PyInt
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
814 DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
815 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
816 char_u *key;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
817 typval_T tv;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
818 dict_T *d = ((DictionaryObject *)(self))->dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
819 dictitem_T *di;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
820 DICTKEY_DECL
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
821
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
822 if (d->dv_lock)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
823 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
824 PyErr_SetVim(_("dict is locked"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
825 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
826 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
827
3792
73557eda7027 updated for version 7.3.654
Bram Moolenaar <bram@vim.org>
parents: 3788
diff changeset
828 DICTKEY_GET_NOTEMPTY(-1)
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
829
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
830 di = dict_find(d, key, -1);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
831
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
832 if (valObject == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
833 {
3636
0e9b2622c94a updated for version 7.3.578
Bram Moolenaar <bram@vim.org>
parents: 3618
diff changeset
834 hashitem_T *hi;
0e9b2622c94a updated for version 7.3.578
Bram Moolenaar <bram@vim.org>
parents: 3618
diff changeset
835
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
836 if (di == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
837 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
838 PyErr_SetString(PyExc_IndexError, _("no such key in dictionary"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
839 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
840 }
3636
0e9b2622c94a updated for version 7.3.578
Bram Moolenaar <bram@vim.org>
parents: 3618
diff changeset
841 hi = hash_find(&d->dv_hashtab, di->di_key);
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
842 hash_remove(&d->dv_hashtab, hi);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
843 dictitem_free(di);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
844 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
845 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
846
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
847 if (ConvertFromPyObject(valObject, &tv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
848 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
849
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
850 if (di == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
851 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
852 di = dictitem_alloc(key);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
853 if (di == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
854 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
855 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
856 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
857 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
858 di->di_tv.v_lock = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
859
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
860 if (dict_add(d, di) == FAIL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
861 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
862 vim_free(di);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
863 PyErr_SetVim(_("failed to add key to dictionary"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
864 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
865 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
866 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
867 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
868 clear_tv(&di->di_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
869
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
870 DICTKEY_UNREF
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
871
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
872 copy_tv(&tv, &di->di_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
873 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
874 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
875
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
876 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
877 DictionaryListKeys(PyObject *self)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
878 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
879 dict_T *dict = ((DictionaryObject *)(self))->dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
880 long_u todo = dict->dv_hashtab.ht_used;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
881 Py_ssize_t i = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
882 PyObject *r;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
883 hashitem_T *hi;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
884
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
885 r = PyList_New(todo);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
886 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
887 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
888 if (!HASHITEM_EMPTY(hi))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
889 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
890 PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key)));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
891 --todo;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
892 ++i;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
893 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
894 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
895 return r;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
896 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
897
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
898 static struct PyMethodDef DictionaryMethods[] = {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
899 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
900 { NULL, NULL, 0, NULL }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
901 };
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
902
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
903 static PyTypeObject ListType;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
904
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
905 typedef struct
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
906 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
907 PyObject_HEAD
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
908 list_T *list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
909 pylinkedlist_T ref;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
910 } ListObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
911
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
912 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
913 ListNew(list_T *list)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
914 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
915 ListObject *self;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
916
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
917 self = PyObject_NEW(ListObject, &ListType);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
918 if (self == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
919 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
920 self->list = list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
921 ++list->lv_refcount;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
922
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
923 pyll_add((PyObject *)(self), &self->ref, &lastlist);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
924
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
925 return (PyObject *)(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
926 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
927
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
928 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
929 list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
930 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
931 Py_ssize_t i;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
932 Py_ssize_t lsize = PySequence_Size(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
933 PyObject *litem;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
934 listitem_T *li;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
935
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
936 for(i=0; i<lsize; i++)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
937 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
938 li = listitem_alloc();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
939 if (li == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
940 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
941 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
942 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
943 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
944 li->li_tv.v_lock = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
945
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
946 litem = PySequence_GetItem(obj, i);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
947 if (litem == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
948 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
949 if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
950 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
951
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
952 list_append(l, li);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
953 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
954 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
955 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
956
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
957 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
958 pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
959 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
960 list_T *l;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
961
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
962 l = list_alloc();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
963 if (l == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
964 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
965 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
966 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
967 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
968
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
969 tv->v_type = VAR_LIST;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
970 tv->vval.v_list = l;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
971
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
972 if (list_py_concat(l, obj, lookupDict) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
973 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
974
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
975 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
976 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
977
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
978 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
979 pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
980 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
981 PyObject *iterator = PyObject_GetIter(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
982 PyObject *item;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
983 list_T *l;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
984 listitem_T *li;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
985
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
986 l = list_alloc();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
987
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
988 if (l == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
989 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
990 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
991 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
992 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
993
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
994 tv->vval.v_list = l;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
995 tv->v_type = VAR_LIST;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
996
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
997
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
998 if (iterator == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
999 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1000
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1001 while ((item = PyIter_Next(obj)))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1002 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1003 li = listitem_alloc();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1004 if (li == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1005 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1006 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1007 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1008 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1009 li->li_tv.v_lock = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1010
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1011 if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1012 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1013
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1014 list_append(l, li);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1015
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1016 Py_DECREF(item);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1017 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1018
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1019 Py_DECREF(iterator);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1020 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1021 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1022
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1023 static PyInt
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1024 ListLength(PyObject *self)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1025 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1026 return ((PyInt) (((ListObject *) (self))->list->lv_len));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1027 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1028
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1029 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1030 ListItem(PyObject *self, Py_ssize_t index)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1031 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1032 listitem_T *li;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1033
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1034 if (index>=ListLength(self))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1035 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1036 PyErr_SetString(PyExc_IndexError, "list index out of range");
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1037 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1038 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1039 li = list_find(((ListObject *) (self))->list, (long) index);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1040 if (li == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1041 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1042 PyErr_SetVim(_("internal error: failed to get vim list item"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1043 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1044 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1045 return ConvertToPyObject(&li->li_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1046 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1047
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1048 #define PROC_RANGE \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1049 if (last < 0) {\
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1050 if (last < -size) \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1051 last = 0; \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1052 else \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1053 last += size; \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1054 } \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1055 if (first < 0) \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1056 first = 0; \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1057 if (first > size) \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1058 first = size; \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1059 if (last > size) \
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1060 last = size;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1061
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1062 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1063 ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1064 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1065 PyInt i;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1066 PyInt size = ListLength(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1067 PyInt n;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1068 PyObject *list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1069 int reversed = 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1070
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1071 PROC_RANGE
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1072 if (first >= last)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1073 first = last;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1074
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1075 n = last-first;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1076 list = PyList_New(n);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1077 if (list == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1078 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1079
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1080 for (i = 0; i < n; ++i)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1081 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1082 PyObject *item = ListItem(self, i);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1083 if (item == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1084 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1085 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1086 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1087 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1088
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1089 if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item)))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1090 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1091 Py_DECREF(item);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1092 Py_DECREF(list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1093 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1094 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1095 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1096
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1097 return list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1098 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1099
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1100 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1101 ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1102 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1103 typval_T tv;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1104 list_T *l = ((ListObject *) (self))->list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1105 listitem_T *li;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1106 Py_ssize_t length = ListLength(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1107
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1108 if (l->lv_lock)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1109 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1110 PyErr_SetVim(_("list is locked"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1111 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1112 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1113 if (index>length || (index==length && obj==NULL))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1114 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1115 PyErr_SetString(PyExc_IndexError, "list index out of range");
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1116 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1117 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1118
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1119 if (obj == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1120 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1121 li = list_find(l, (long) index);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1122 list_remove(l, li, li);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1123 clear_tv(&li->li_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1124 vim_free(li);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1125 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1126 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1127
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1128 if (ConvertFromPyObject(obj, &tv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1129 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1130
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1131 if (index == length)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1132 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1133 if (list_append_tv(l, &tv) == FAIL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1134 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1135 PyErr_SetVim(_("Failed to add item to list"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1136 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1137 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1138 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1139 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1140 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1141 li = list_find(l, (long) index);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1142 clear_tv(&li->li_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1143 copy_tv(&tv, &li->li_tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1144 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1145 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1146 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1147
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1148 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1149 ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1150 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1151 PyInt size = ListLength(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1152 Py_ssize_t i;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1153 Py_ssize_t lsize;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1154 PyObject *litem;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1155 listitem_T *li;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1156 listitem_T *next;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1157 typval_T v;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1158 list_T *l = ((ListObject *) (self))->list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1159
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1160 if (l->lv_lock)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1161 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1162 PyErr_SetVim(_("list is locked"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1163 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1164 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1165
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1166 PROC_RANGE
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1167
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1168 if (first == size)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1169 li = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1170 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1171 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1172 li = list_find(l, (long) first);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1173 if (li == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1174 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1175 PyErr_SetVim(_("internal error: no vim list item"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1176 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1177 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1178 if (last > first)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1179 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1180 i = last - first;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1181 while (i-- && li != NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1182 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1183 next = li->li_next;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1184 listitem_remove(l, li);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1185 li = next;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1186 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1187 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1188 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1189
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1190 if (obj == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1191 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1192
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1193 if (!PyList_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1194 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1195 PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1196 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1197 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1198
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1199 lsize = PyList_Size(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1200
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1201 for(i=0; i<lsize; i++)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1202 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1203 litem = PyList_GetItem(obj, i);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1204 if (litem == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1205 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1206 if (ConvertFromPyObject(litem, &v) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1207 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1208 if (list_insert_tv(l, &v, li) == FAIL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1209 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1210 PyErr_SetVim(_("internal error: failed to add item to list"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1211 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1212 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1213 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1214 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1215 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1216
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1217 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1218 ListConcatInPlace(PyObject *self, PyObject *obj)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1219 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1220 list_T *l = ((ListObject *) (self))->list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1221 PyObject *lookup_dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1222
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1223 if (l->lv_lock)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1224 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1225 PyErr_SetVim(_("list is locked"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1226 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1227 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1228
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1229 if (!PySequence_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1230 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1231 PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1232 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1233 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1234
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1235 lookup_dict = PyDict_New();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1236 if (list_py_concat(l, obj, lookup_dict) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1237 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1238 Py_DECREF(lookup_dict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1239 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1240 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1241 Py_DECREF(lookup_dict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1242
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1243 Py_INCREF(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1244 return self;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1245 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1246
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1247 static struct PyMethodDef ListMethods[] = {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1248 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1249 { NULL, NULL, 0, NULL }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1250 };
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1251
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1252 typedef struct
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1253 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1254 PyObject_HEAD
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1255 char_u *name;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1256 } FunctionObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1257
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1258 static PyTypeObject FunctionType;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1259
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1260 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1261 FunctionNew(char_u *name)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1262 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1263 FunctionObject *self;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1264
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1265 self = PyObject_NEW(FunctionObject, &FunctionType);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1266 if (self == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1267 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1268 self->name = PyMem_New(char_u, STRLEN(name) + 1);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1269 if (self->name == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1270 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1271 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1272 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1273 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1274 STRCPY(self->name, name);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1275 func_ref(name);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1276 return (PyObject *)(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1277 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1278
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1279 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1280 FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1281 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1282 FunctionObject *this = (FunctionObject *)(self);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1283 char_u *name = this->name;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1284 typval_T args;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1285 typval_T selfdicttv;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1286 typval_T rettv;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1287 dict_T *selfdict = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1288 PyObject *selfdictObject;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1289 PyObject *result;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1290 int error;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1291
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1292 if (ConvertFromPyObject(argsObject, &args) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1293 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1294
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1295 if (kwargs != NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1296 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1297 selfdictObject = PyDict_GetItemString(kwargs, "self");
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1298 if (selfdictObject != NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1299 {
3703
e13f2f3568e1 updated for version 7.3.611
Bram Moolenaar <bram@vim.org>
parents: 3648
diff changeset
1300 if (!PyMapping_Check(selfdictObject))
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1301 {
3703
e13f2f3568e1 updated for version 7.3.611
Bram Moolenaar <bram@vim.org>
parents: 3648
diff changeset
1302 PyErr_SetString(PyExc_TypeError,
e13f2f3568e1 updated for version 7.3.611
Bram Moolenaar <bram@vim.org>
parents: 3648
diff changeset
1303 _("'self' argument must be a dictionary"));
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1304 clear_tv(&args);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1305 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1306 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1307 if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1308 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1309 selfdict = selfdicttv.vval.v_dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1310 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1311 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1312
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1313 error = func_call(name, &args, selfdict, &rettv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1314 if (error != OK)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1315 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1316 result = NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1317 PyErr_SetVim(_("failed to run function"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1318 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1319 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1320 result = ConvertToPyObject(&rettv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1321
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1322 /* FIXME Check what should really be cleared. */
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1323 clear_tv(&args);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1324 clear_tv(&rettv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1325 /*
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1326 * if (selfdict!=NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1327 * clear_tv(selfdicttv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1328 */
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1329
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1330 return result;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1331 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1332
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1333 static struct PyMethodDef FunctionMethods[] = {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1334 {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""},
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1335 { NULL, NULL, 0, NULL }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1336 };
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
1337
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1338 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1339
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1340 static int
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1341 CheckWindow(WindowObject *this)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1342 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1343 if (this->win == INVALID_WINDOW_VALUE)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1344 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1345 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
1346 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1347 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1348
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1349 return 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1350 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1351
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1352 static int WindowSetattr(PyObject *, char *, PyObject *);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1353 static PyObject *WindowRepr(PyObject *);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1354
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1355 static int
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1356 WindowSetattr(PyObject *self, char *name, PyObject *val)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1357 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1358 WindowObject *this = (WindowObject *)(self);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1359
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1360 if (CheckWindow(this))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1361 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1362
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1363 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
1364 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1365 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
1366 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1367 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1368 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
1369 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1370 long lnum;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1371 long col;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1372
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1373 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
1374 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1375
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1376 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1377 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1378 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
1379 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1380 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1381
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1382 /* Check for keyboard interrupts */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1383 if (VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1384 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1385
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1386 this->win->w_cursor.lnum = lnum;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1387 this->win->w_cursor.col = col;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1388 #ifdef FEAT_VIRTUALEDIT
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1389 this->win->w_cursor.coladd = 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1390 #endif
2933
0bef86c5c985 updated for version 7.3.239
Bram Moolenaar <bram@vim.org>
parents: 2919
diff changeset
1391 /* When column is out of range silently correct it. */
0bef86c5c985 updated for version 7.3.239
Bram Moolenaar <bram@vim.org>
parents: 2919
diff changeset
1392 check_cursor_col_win(this->win);
0bef86c5c985 updated for version 7.3.239
Bram Moolenaar <bram@vim.org>
parents: 2919
diff changeset
1393
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1394 update_screen(VALID);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1395 return 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1396 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1397 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
1398 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1399 int height;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1400 win_T *savewin;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1401
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1402 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
1403 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1404
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1405 #ifdef FEAT_GUI
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1406 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
1407 #endif
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1408 savewin = curwin;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1409 curwin = this->win;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1410 win_setheight(height);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1411 curwin = savewin;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1412
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1413 /* Check for keyboard interrupts */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1414 if (VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1415 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1416
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1417 return 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1418 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1419 #ifdef FEAT_VERTSPLIT
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1420 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
1421 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1422 int width;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1423 win_T *savewin;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1424
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1425 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
1426 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1427
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1428 #ifdef FEAT_GUI
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1429 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
1430 #endif
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1431 savewin = curwin;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1432 curwin = this->win;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1433 win_setwidth(width);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1434 curwin = savewin;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1435
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1436 /* Check for keyboard interrupts */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1437 if (VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1438 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1439
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1440 return 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1441 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1442 #endif
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1443 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1444 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1445 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
1446 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1447 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1448 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1449
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1450 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1451 WindowRepr(PyObject *self)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1452 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1453 static char repr[100];
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1454 WindowObject *this = (WindowObject *)(self);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1455
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1456 if (this->win == INVALID_WINDOW_VALUE)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1457 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1458 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
1459 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
1460 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1461 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1462 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1463 int i = 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1464 win_T *w;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1465
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1466 for (w = firstwin; w != NULL && w != this->win; 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
1467 ++i;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1468
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1469 if (w == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1470 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
1471 (self));
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1472 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1473 vim_snprintf(repr, 100, _("<window %d>"), i);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1474
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1475 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
1476 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1477 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1478
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1479 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1480 * Window list object - Implementation
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1481 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1482 static PyInt
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1483 WinListLength(PyObject *self UNUSED)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1484 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1485 win_T *w = firstwin;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1486 PyInt n = 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1487
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1488 while (w != NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1489 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1490 ++n;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1491 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
1492 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1493
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1494 return n;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1495 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1496
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1497 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1498 WinListItem(PyObject *self UNUSED, PyInt n)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1499 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1500 win_T *w;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1501
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1502 for (w = firstwin; w != NULL; w = W_NEXT(w), --n)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1503 if (n == 0)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1504 return WindowNew(w);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1505
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1506 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
1507 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1508 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1509
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1510 /* 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
1511 *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1512 * 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
1513 * 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
1514 * characters.
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1515 *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1516 * 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
1517 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1518 static char *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1519 StringToLine(PyObject *obj)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1520 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1521 const char *str;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1522 char *save;
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1523 PyObject *bytes;
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1524 PyInt len;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1525 PyInt i;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1526 char *p;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1527
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1528 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
1529 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1530 PyErr_BadArgument();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1531 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1532 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1533
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1534 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1535 str = PyString_AsString(bytes);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1536 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
1537
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1538 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1539 * 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
1540 * 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
1541 * a single line.
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1542 * 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
1543 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1544 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
1545 if (p != NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1546 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1547 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
1548 --len;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1549 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1550 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1551 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
1552 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1553 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1554 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1555
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1556 /* 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
1557 * 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
1558 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1559 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
1560 if (save == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1561 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1562 PyErr_NoMemory();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1563 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1564 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1565
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1566 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
1567 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1568 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
1569 save[i] = '\n';
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1570 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1571 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
1572 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1573
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1574 save[i] = '\0';
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1575 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
1576
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1577 return save;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1578 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1579
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1580 /* 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
1581 * 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
1582 * string object.
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1583 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1584 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1585 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
1586 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1587 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
1588 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1589
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1590
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1591 /* 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
1592 * 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
1593 * 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
1594 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1595 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1596 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
1597 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1598 PyInt i;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1599 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
1600 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
1601
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1602 if (list == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1603 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1604
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1605 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
1606 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1607 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
1608
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1609 /* 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
1610 if (str == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1611 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1612 Py_DECREF(list);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1613 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1614 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1615
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1616 /* 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
1617 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
1618 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1619 Py_DECREF(str);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1620 Py_DECREF(list);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1621 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1622 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1623 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1624
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1625 /* 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
1626 * 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
1627 * with it).
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1628 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1629
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1630 return list;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1631 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1632
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1633 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1634 * 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
1635 * 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
1636 * deleted).
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1637 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1638 static void
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1639 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
1640 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1641 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
1642 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1643 /* 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
1644 * lines. */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1645 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
1646 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1647 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
1648 check_cursor_col();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1649 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1650 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
1651 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1652 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
1653 check_cursor();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1654 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1655 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1656 check_cursor_col();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1657 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
1658 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1659 invalidate_botline();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1660 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1661
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1662 /*
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1663 * 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
1664 * 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
1665 * 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
1666 * 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
1667 * 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
1668 * 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
1669 * 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
1670 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1671 static int
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1672 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
1673 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1674 /* First of all, we check the thpe 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
1675 * 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
1676 * 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
1677 * 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
1678 * 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
1679 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1680 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
1681 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1682 buf_T *savebuf = curbuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1683
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1684 PyErr_Clear();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1685 curbuf = buf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1686
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1687 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
1688 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
1689 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
1690 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
1691 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1692 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1693 if (buf == curwin->w_buffer)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1694 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
1695 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
1696 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1697
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1698 curbuf = savebuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1699
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1700 if (PyErr_Occurred() || VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1701 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1702
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1703 if (len_change)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1704 *len_change = -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1705
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1706 return OK;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1707 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1708 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
1709 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1710 char *save = StringToLine(line);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1711 buf_T *savebuf = curbuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1712
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1713 if (save == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1714 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1715
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1716 /* 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
1717 PyErr_Clear();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1718 curbuf = buf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1719
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1720 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
1721 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1722 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
1723 vim_free(save);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1724 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1725 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
1726 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1727 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
1728 vim_free(save);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1729 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1730 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1731 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
1732
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1733 curbuf = savebuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1734
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1735 /* Check that the cursor is not beyond the end of the line now. */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1736 if (buf == curwin->w_buffer)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1737 check_cursor_col();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1738
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1739 if (PyErr_Occurred() || VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1740 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1741
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1742 if (len_change)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1743 *len_change = 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1744
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1745 return OK;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1746 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1747 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1748 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1749 PyErr_BadArgument();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1750 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1751 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1752 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1753
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1754 /* Replace a range of lines in the specified buffer. The line numbers are in
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1755 * Vim format (1-based). The range is from lo up to, but not including, hi.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1756 * The replacement lines are given as a Python list of string objects. The
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1757 * list is checked for validity and correct format. Errors are returned as a
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1758 * value of FAIL. The return value is OK on success.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1759 * If OK is returned and len_change is not NULL, *len_change
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1760 * is set to the change in the buffer length.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1761 */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1762 static int
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1763 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1764 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1765 /* First of all, we check the thpe of the supplied Python object.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1766 * There are three cases:
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1767 * 1. NULL, or None - this is a deletion.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1768 * 2. A list - this is a replacement.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1769 * 3. Anything else - this is an error.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1770 */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1771 if (list == Py_None || list == NULL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1772 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1773 PyInt i;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1774 PyInt n = (int)(hi - lo);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1775 buf_T *savebuf = curbuf;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1776
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1777 PyErr_Clear();
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1778 curbuf = buf;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1779
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1780 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1781 PyErr_SetVim(_("cannot save undo information"));
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1782 else
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1783 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1784 for (i = 0; i < n; ++i)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1785 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1786 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1787 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1788 PyErr_SetVim(_("cannot delete line"));
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1789 break;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1790 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1791 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1792 if (buf == curwin->w_buffer)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1793 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1794 deleted_lines_mark((linenr_T)lo, (long)i);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1795 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1796
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1797 curbuf = savebuf;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1798
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1799 if (PyErr_Occurred() || VimErrorCheck())
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1800 return FAIL;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1801
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1802 if (len_change)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1803 *len_change = -n;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1804
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1805 return OK;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1806 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1807 else if (PyList_Check(list))
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1808 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1809 PyInt i;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1810 PyInt new_len = PyList_Size(list);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1811 PyInt old_len = hi - lo;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1812 PyInt extra = 0; /* lines added to text, can be negative */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1813 char **array;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1814 buf_T *savebuf;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1815
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1816 if (new_len == 0) /* avoid allocating zero bytes */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1817 array = NULL;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1818 else
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1819 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1820 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1821 if (array == NULL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1822 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1823 PyErr_NoMemory();
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1824 return FAIL;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1825 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1826 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1827
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1828 for (i = 0; i < new_len; ++i)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1829 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1830 PyObject *line = PyList_GetItem(list, i);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1831
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1832 array[i] = StringToLine(line);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1833 if (array[i] == NULL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1834 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1835 while (i)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1836 vim_free(array[--i]);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1837 vim_free(array);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1838 return FAIL;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1839 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1840 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1841
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1842 savebuf = curbuf;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1843
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1844 PyErr_Clear();
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1845 curbuf = buf;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1846
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1847 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1848 PyErr_SetVim(_("cannot save undo information"));
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1849
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1850 /* If the size of the range is reducing (ie, new_len < old_len) we
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1851 * need to delete some old_len. We do this at the start, by
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1852 * repeatedly deleting line "lo".
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1853 */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1854 if (!PyErr_Occurred())
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1855 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1856 for (i = 0; i < old_len - new_len; ++i)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1857 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1858 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1859 PyErr_SetVim(_("cannot delete line"));
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1860 break;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1861 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1862 extra -= i;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1863 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1864
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1865 /* For as long as possible, replace the existing old_len with the
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1866 * new old_len. This is a more efficient operation, as it requires
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1867 * less memory allocation and freeing.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1868 */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1869 if (!PyErr_Occurred())
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1870 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1871 for (i = 0; i < old_len && i < new_len; ++i)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1872 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1873 == FAIL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1874 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1875 PyErr_SetVim(_("cannot replace line"));
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1876 break;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1877 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1878 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1879 else
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1880 i = 0;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1881
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1882 /* Now we may need to insert the remaining new old_len. If we do, we
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1883 * must free the strings as we finish with them (we can't pass the
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1884 * responsibility to vim in this case).
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1885 */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1886 if (!PyErr_Occurred())
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1887 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1888 while (i < new_len)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1889 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1890 if (ml_append((linenr_T)(lo + i - 1),
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1891 (char_u *)array[i], 0, FALSE) == FAIL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1892 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1893 PyErr_SetVim(_("cannot insert line"));
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1894 break;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1895 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1896 vim_free(array[i]);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1897 ++i;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1898 ++extra;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1899 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1900 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1901
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1902 /* Free any left-over old_len, as a result of an error */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1903 while (i < new_len)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1904 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1905 vim_free(array[i]);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1906 ++i;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1907 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1908
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1909 /* Free the array of old_len. All of its contents have now
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1910 * been dealt with (either freed, or the responsibility passed
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1911 * to vim.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1912 */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1913 vim_free(array);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1914
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1915 /* Adjust marks. Invalidate any which lie in the
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1916 * changed range, and move any in the remainder of the buffer.
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1917 */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1918 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1919 (long)MAXLNUM, (long)extra);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1920 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1921
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1922 if (buf == curwin->w_buffer)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1923 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1924
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1925 curbuf = savebuf;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1926
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1927 if (PyErr_Occurred() || VimErrorCheck())
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1928 return FAIL;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1929
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1930 if (len_change)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1931 *len_change = new_len - old_len;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1932
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1933 return OK;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1934 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1935 else
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1936 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1937 PyErr_BadArgument();
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1938 return FAIL;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1939 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
1940 }
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1941
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1942 /* Insert a number of lines into the specified buffer after the specifed line.
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1943 * 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
1944 * 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
1945 * 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
1946 * 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
1947 * 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
1948 * 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
1949 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1950 static int
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1951 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
1952 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1953 /* 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
1954 * 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
1955 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1956 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
1957 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1958 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
1959 buf_T *savebuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1960
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1961 if (str == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1962 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1963
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1964 savebuf = curbuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1965
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1966 PyErr_Clear();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1967 curbuf = buf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1968
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1969 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
1970 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
1971 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
1972 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
1973 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1974 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
1975
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1976 vim_free(str);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1977 curbuf = savebuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1978 update_screen(VALID);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1979
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1980 if (PyErr_Occurred() || VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1981 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1982
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1983 if (len_change)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1984 *len_change = 1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1985
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1986 return OK;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1987 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1988 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
1989 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1990 PyInt i;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1991 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
1992 char **array;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1993 buf_T *savebuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1994
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1995 array = (char **)alloc((unsigned)(size * sizeof(char *)));
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1996 if (array == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1997 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1998 PyErr_NoMemory();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
1999 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2000 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2001
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2002 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
2003 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2004 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
2005 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
2006
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2007 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
2008 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2009 while (i)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2010 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
2011 vim_free(array);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2012 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2013 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2014 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2015
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2016 savebuf = curbuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2017
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2018 PyErr_Clear();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2019 curbuf = buf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2020
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2021 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
2022 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
2023 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2024 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2025 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
2026 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2027 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
2028 (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
2029 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2030 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
2031
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2032 /* 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
2033 while (i < size)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2034 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
2035
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2036 break;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2037 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2038 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
2039 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2040 if (i > 0)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2041 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
2042 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2043
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2044 /* 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
2045 * been freed.
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2046 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2047 vim_free(array);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2048
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2049 curbuf = savebuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2050 update_screen(VALID);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2051
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2052 if (PyErr_Occurred() || VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2053 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2054
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2055 if (len_change)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2056 *len_change = size;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2057
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2058 return OK;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2059 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2060 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2061 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2062 PyErr_BadArgument();
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2063 return FAIL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2064 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2065 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2066
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2067 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2068 * 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
2069 * -------------------------------------------
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2070 */
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
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2073 CheckBuffer(BufferObject *this)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2074 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2075 if (this->buf == INVALID_BUFFER_VALUE)
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 buffer"));
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
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2084 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2085 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
2086 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2087 if (CheckBuffer(self))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2088 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2089
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2090 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
2091 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2092 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
2093 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2094 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2095
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2096 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
2097 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2098
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2099 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2100 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
2101 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2102 PyInt size;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2103
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2104 if (CheckBuffer(self))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2105 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2106
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2107 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
2108
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2109 if (lo < 0)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2110 lo = 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2111 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
2112 lo = size;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2113 if (hi < 0)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2114 hi = 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2115 if (hi < lo)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2116 hi = lo;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2117 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
2118 hi = size;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2119
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2120 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
2121 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2122
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2123 static PyInt
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2124 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
2125 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2126 PyInt len_change;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2127
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2128 if (CheckBuffer(self))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2129 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2130
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2131 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
2132 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2133 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
2134 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2135 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2136
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2137 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
2138 return -1;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2139
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2140 if (new_end)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2141 *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
2142
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2143 return 0;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2144 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2145
2894
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2146 static PyInt
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2147 RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2148 {
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2149 PyInt size;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2150 PyInt len_change;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2151
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2152 /* Self must be a valid buffer */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2153 if (CheckBuffer(self))
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2154 return -1;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2155
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2156 /* Sort out the slice range */
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2157 size = end - start + 1;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2158
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2159 if (lo < 0)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2160 lo = 0;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2161 else if (lo > size)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2162 lo = size;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2163 if (hi < 0)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2164 hi = 0;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2165 if (hi < lo)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2166 hi = lo;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2167 else if (hi > size)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2168 hi = size;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2169
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2170 if (SetBufferLineList(self->buf, lo + start, hi + start,
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2171 val, &len_change) == FAIL)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2172 return -1;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2173
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2174 if (new_end)
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2175 *new_end = end + len_change;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2176
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2177 return 0;
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2178 }
fe9c7da98b5e updated for version 7.3.220
Bram Moolenaar <bram@vim.org>
parents: 2727
diff changeset
2179
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2180
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2181 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2182 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
2183 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2184 PyObject *lines;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2185 PyInt len_change;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2186 PyInt max;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2187 PyInt n;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2188
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2189 if (CheckBuffer(self))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2190 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2191
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2192 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
2193
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2194 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
2195 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2196
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2197 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
2198 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2199 PyErr_SetString(PyExc_ValueError, _("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
2200 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2201 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2202
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2203 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
2204 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2205
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2206 if (new_end)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2207 *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
2208
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2209 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
2210 return Py_None;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2211 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2212
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2213
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2214 /* Buffer object - Definitions
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2215 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2216
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2217 typedef struct
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2218 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2219 PyObject_HEAD
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2220 BufferObject *buf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2221 PyInt start;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2222 PyInt end;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2223 } RangeObject;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2224
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2225 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2226 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
2227 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2228 BufferObject *bufr;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2229 RangeObject *self;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2230 self = PyObject_NEW(RangeObject, &RangeType);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2231 if (self == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2232 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2233
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2234 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
2235 if (bufr == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2236 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2237 Py_DECREF(self);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2238 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2239 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2240 Py_INCREF(bufr);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2241
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2242 self->buf = bufr;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2243 self->start = start;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2244 self->end = end;
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 return (PyObject *)(self);
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
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2249 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2250 BufferAppend(PyObject *self, PyObject *args)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2251 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2252 return RBAppend((BufferObject *)(self), args, 1,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2253 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2254 NULL);
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
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2257 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2258 BufferMark(PyObject *self, PyObject *args)
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 pos_T *posp;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2261 char *pmark;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2262 char mark;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2263 buf_T *curbuf_save;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2264
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2265 if (CheckBuffer((BufferObject *)(self)))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2266 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2267
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2268 if (!PyArg_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
2269 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2270 mark = *pmark;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2271
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2272 curbuf_save = curbuf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2273 curbuf = ((BufferObject *)(self))->buf;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2274 posp = getmark(mark, FALSE);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2275 curbuf = curbuf_save;
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 if (posp == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2278 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2279 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
2280 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2281 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2282
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2283 /* Ckeck for keyboard interrupt */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2284 if (VimErrorCheck())
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2285 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2286
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2287 if (posp->lnum <= 0)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2288 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2289 /* 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
2290 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
2291 return Py_None;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2292 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2293
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2294 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
2295 }
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 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2298 BufferRange(PyObject *self, PyObject *args)
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 PyInt start;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2301 PyInt end;
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 (CheckBuffer((BufferObject *)(self)))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2304 return NULL;
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 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
2307 return NULL;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2308
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2309 return RangeNew(((BufferObject *)(self))->buf, start, end);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2310 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2311
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2312 static struct PyMethodDef BufferMethods[] = {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2313 /* name, function, calling, documentation */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2314 {"append", BufferAppend, 1, "Append data to Vim buffer" },
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2315 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2316 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" },
3310
60f6df978a41 updated for version 7.3.422
Bram Moolenaar <bram@vim.org>
parents: 3056
diff changeset
2317 #if PY_VERSION_HEX >= 0x03000000
60f6df978a41 updated for version 7.3.422
Bram Moolenaar <bram@vim.org>
parents: 3056
diff changeset
2318 {"__dir__", BufferDir, 4, "List its attributes" },
60f6df978a41 updated for version 7.3.422
Bram Moolenaar <bram@vim.org>
parents: 3056
diff changeset
2319 #endif
2447
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2320 { NULL, NULL, 0, NULL }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2321 };
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2322
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2323 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2324 RangeAppend(PyObject *self, PyObject *args)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2325 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2326 return RBAppend(((RangeObject *)(self))->buf, args,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2327 ((RangeObject *)(self))->start,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2328 ((RangeObject *)(self))->end,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2329 &((RangeObject *)(self))->end);
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
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2332 static PyInt
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2333 RangeLength(PyObject *self)
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 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2336 if (CheckBuffer(((RangeObject *)(self))->buf))
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2337 return -1; /* ??? */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2338
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2339 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2340 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2341
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2342 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2343 RangeItem(PyObject *self, PyInt n)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2344 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2345 return RBItem(((RangeObject *)(self))->buf, n,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2346 ((RangeObject *)(self))->start,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2347 ((RangeObject *)(self))->end);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2348 }
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 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2351 RangeRepr(PyObject *self)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2352 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2353 static char repr[100];
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2354 RangeObject *this = (RangeObject *)(self);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2355
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2356 if (this->buf->buf == INVALID_BUFFER_VALUE)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2357 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2358 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2359 (self));
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2360 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
2361 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2362 else
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2363 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2364 char *name = (char *)this->buf->buf->b_fname;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2365 int len;
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2366
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2367 if (name == NULL)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2368 name = "";
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2369 len = (int)strlen(name);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2370
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2371 if (len > 45)
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2372 name = name + (45 - len);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2373
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2374 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2375 len > 45 ? "..." : "", name,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2376 this->start, this->end);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2377
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2378 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
2379 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2380 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2381
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2382 static PyObject *
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2383 RangeSlice(PyObject *self, 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
2384 {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2385 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2386 ((RangeObject *)(self))->start,
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2387 ((RangeObject *)(self))->end);
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2388 }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2389
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2390 /*
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2391 * Line range object - Definitions
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2392 */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2393
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2394 static struct PyMethodDef RangeMethods[] = {
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2395 /* name, function, calling, documentation */
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2396 {"append", RangeAppend, 1, "Append data to the Vim range" },
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2397 { NULL, NULL, 0, NULL }
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2398 };
84d353762845 Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents: 2399
diff changeset
2399
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2400 static void
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2401 set_ref_in_py(const int copyID)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2402 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2403 pylinkedlist_T *cur;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2404 dict_T *dd;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2405 list_T *ll;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2406
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2407 if (lastdict != NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2408 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2409 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2410 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2411 if (dd->dv_copyID != copyID)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2412 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2413 dd->dv_copyID = copyID;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2414 set_ref_in_ht(&dd->dv_hashtab, copyID);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2415 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2416 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2417
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2418 if (lastlist != NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2419 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2420 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2421 ll = ((ListObject *) (cur->pll_obj))->list;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2422 if (ll->lv_copyID != copyID)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2423 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2424 ll->lv_copyID = copyID;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2425 set_ref_in_list(ll, copyID);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2426 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2427 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2428 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2429
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2430 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2431 set_string_copy(char_u *str, typval_T *tv)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2432 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2433 tv->vval.v_string = vim_strsave(str);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2434 if (tv->vval.v_string == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2435 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2436 PyErr_NoMemory();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2437 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2438 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2439 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2440 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2441
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2442 #ifdef FEAT_EVAL
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2443 typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2444
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2445 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2446 convert_dl(PyObject *obj, typval_T *tv,
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2447 pytotvfunc py_to_tv, PyObject *lookupDict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2448 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2449 PyObject *capsule;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2450 char hexBuf[sizeof(void *) * 2 + 3];
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2451
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2452 sprintf(hexBuf, "%p", obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2453
3638
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2454 # ifdef PY_USE_CAPSULE
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2455 capsule = PyDict_GetItemString(lookupDict, hexBuf);
3648
2d107086903a updated for version 7.3.584
Bram Moolenaar <bram@vim.org>
parents: 3640
diff changeset
2456 # else
2d107086903a updated for version 7.3.584
Bram Moolenaar <bram@vim.org>
parents: 3640
diff changeset
2457 capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf);
2d107086903a updated for version 7.3.584
Bram Moolenaar <bram@vim.org>
parents: 3640
diff changeset
2458 # endif
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2459 if (capsule == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2460 {
3638
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2461 # ifdef PY_USE_CAPSULE
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2462 capsule = PyCapsule_New(tv, NULL, NULL);
3648
2d107086903a updated for version 7.3.584
Bram Moolenaar <bram@vim.org>
parents: 3640
diff changeset
2463 # else
2d107086903a updated for version 7.3.584
Bram Moolenaar <bram@vim.org>
parents: 3640
diff changeset
2464 capsule = PyCObject_FromVoidPtr(tv, NULL);
2d107086903a updated for version 7.3.584
Bram Moolenaar <bram@vim.org>
parents: 3640
diff changeset
2465 # endif
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2466 PyDict_SetItemString(lookupDict, hexBuf, capsule);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2467 Py_DECREF(capsule);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2468 if (py_to_tv(obj, tv, lookupDict) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2469 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2470 tv->v_type = VAR_UNKNOWN;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2471 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2472 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2473 /* As we are not using copy_tv which increments reference count we must
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2474 * do it ourself. */
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2475 switch(tv->v_type)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2476 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2477 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2478 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2479 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2480 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2481 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2482 {
3638
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2483 typval_T *v;
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2484
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2485 # ifdef PY_USE_CAPSULE
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2486 v = PyCapsule_GetPointer(capsule, NULL);
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2487 # else
3648
2d107086903a updated for version 7.3.584
Bram Moolenaar <bram@vim.org>
parents: 3640
diff changeset
2488 v = PyCObject_AsVoidPtr(capsule);
3638
80ed6aa7b9eb updated for version 7.3.579
Bram Moolenaar <bram@vim.org>
parents: 3636
diff changeset
2489 # endif
3618
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2490 copy_tv(v, tv);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2491 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2492 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2493 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2494
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2495 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2496 ConvertFromPyObject(PyObject *obj, typval_T *tv)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2497 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2498 PyObject *lookup_dict;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2499 int r;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2500
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2501 lookup_dict = PyDict_New();
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2502 r = _ConvertFromPyObject(obj, tv, lookup_dict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2503 Py_DECREF(lookup_dict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2504 return r;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2505 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2506
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2507 static int
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2508 _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2509 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2510 if (obj->ob_type == &DictionaryType)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2511 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2512 tv->v_type = VAR_DICT;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2513 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2514 ++tv->vval.v_dict->dv_refcount;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2515 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2516 else if (obj->ob_type == &ListType)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2517 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2518 tv->v_type = VAR_LIST;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2519 tv->vval.v_list = (((ListObject *)(obj))->list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2520 ++tv->vval.v_list->lv_refcount;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2521 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2522 else if (obj->ob_type == &FunctionType)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2523 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2524 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2525 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2526
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2527 tv->v_type = VAR_FUNC;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2528 func_ref(tv->vval.v_string);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2529 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2530 #if PY_MAJOR_VERSION >= 3
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2531 else if (PyBytes_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2532 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2533 char_u *result = (char_u *) PyBytes_AsString(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2534
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2535 if (result == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2536 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2537
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2538 if (set_string_copy(result, tv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2539 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2540
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2541 tv->v_type = VAR_STRING;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2542 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2543 else if (PyUnicode_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2544 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2545 PyObject *bytes;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2546 char_u *result;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2547
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2548 bytes = PyString_AsBytes(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2549 if (bytes == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2550 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2551
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2552 result = (char_u *) PyBytes_AsString(bytes);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2553 if (result == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2554 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2555
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2556 if (set_string_copy(result, tv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2557 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2558 Py_XDECREF(bytes);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2559 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2560 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2561 Py_XDECREF(bytes);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2562
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2563 tv->v_type = VAR_STRING;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2564 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2565 #else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2566 else if (PyUnicode_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2567 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2568 PyObject *bytes;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2569 char_u *result;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2570
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2571 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2572 if (bytes == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2573 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2574
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2575 result=(char_u *) PyString_AsString(bytes);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2576 if (result == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2577 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2578
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2579 if (set_string_copy(result, tv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2580 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2581 Py_XDECREF(bytes);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2582 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2583 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2584 Py_XDECREF(bytes);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2585
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2586 tv->v_type = VAR_STRING;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2587 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2588 else if (PyString_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2589 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2590 char_u *result = (char_u *) PyString_AsString(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2591
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2592 if (result == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2593 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2594
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2595 if (set_string_copy(result, tv) == -1)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2596 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2597
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2598 tv->v_type = VAR_STRING;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2599 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2600 else if (PyInt_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2601 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2602 tv->v_type = VAR_NUMBER;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2603 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2604 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2605 #endif
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2606 else if (PyLong_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2607 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2608 tv->v_type = VAR_NUMBER;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2609 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2610 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2611 else if (PyDict_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2612 return convert_dl(obj, tv, pydict_to_tv, lookupDict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2613 #ifdef FEAT_FLOAT
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2614 else if (PyFloat_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2615 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2616 tv->v_type = VAR_FLOAT;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2617 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2618 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2619 #endif
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2620 else if (PyIter_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2621 return convert_dl(obj, tv, pyiter_to_tv, lookupDict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2622 else if (PySequence_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2623 return convert_dl(obj, tv, pyseq_to_tv, lookupDict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2624 else if (PyMapping_Check(obj))
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2625 return convert_dl(obj, tv, pymap_to_tv, lookupDict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2626 else
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2627 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2628 PyErr_SetString(PyExc_TypeError, _("unable to convert to vim structure"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2629 return -1;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2630 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2631 return 0;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2632 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2633
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2634 static PyObject *
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2635 ConvertToPyObject(typval_T *tv)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2636 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2637 if (tv == NULL)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2638 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2639 PyErr_SetVim(_("NULL reference passed"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2640 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2641 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2642 switch (tv->v_type)
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2643 {
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2644 case VAR_STRING:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2645 return PyBytes_FromString((char *) tv->vval.v_string);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2646 case VAR_NUMBER:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2647 return PyLong_FromLong((long) tv->vval.v_number);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2648 #ifdef FEAT_FLOAT
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2649 case VAR_FLOAT:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2650 return PyFloat_FromDouble((double) tv->vval.v_float);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2651 #endif
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2652 case VAR_LIST:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2653 return ListNew(tv->vval.v_list);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2654 case VAR_DICT:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2655 return DictionaryNew(tv->vval.v_dict);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2656 case VAR_FUNC:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2657 return FunctionNew(tv->vval.v_string);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2658 case VAR_UNKNOWN:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2659 Py_INCREF(Py_None);
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2660 return Py_None;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2661 default:
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2662 PyErr_SetVim(_("internal error: invalid value type"));
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2663 return NULL;
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2664 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2665 }
c052f3b79b99 updated for version 7.3.569
Bram Moolenaar <bram@vim.org>
parents: 3466
diff changeset
2666 #endif