1 #if defined(HAVE_CONFIG_H) && !defined(GEANYPY_WINDOWS)
2 # include "config.h"
3 #endif
4
5 #include "geanypy.h"
6
7
8 typedef struct
9 {
10 PyObject_HEAD
11 const GeanyLexerStyle *lexer_style;
12 } LexerStyle;
13
14
15 static void
LexerStyle_dealloc(LexerStyle * self)16 LexerStyle_dealloc(LexerStyle *self)
17 {
18 self->ob_type->tp_free((PyObject *) self);
19 }
20
21
22 static int
LexerStyle_init(LexerStyle * self,PyObject * args,PyObject * kwds)23 LexerStyle_init(LexerStyle *self, PyObject *args, PyObject *kwds)
24 {
25 self->lexer_style = NULL;
26 return 0;
27 }
28
29
30 static PyObject *
LexerStyle_get_property(LexerStyle * self,const gchar * prop_name)31 LexerStyle_get_property(LexerStyle *self, const gchar *prop_name)
32 {
33 g_return_val_if_fail(self != NULL, NULL);
34 g_return_val_if_fail(prop_name != NULL, NULL);
35
36 if (!self->lexer_style)
37 {
38 PyErr_SetString(PyExc_RuntimeError,
39 "LexerStyle instance not initialized properly");
40 return NULL;
41 }
42
43 if (g_str_equal(prop_name, "background"))
44 {
45 guint16 red, green, blue;
46 red = self->lexer_style->background & 255;
47 green = (self->lexer_style->background >> 8) & 255;
48 blue = (self->lexer_style->background >> 16) & 255;
49 return Py_BuildValue("iii", red, green, blue);
50 }
51 else if (g_str_equal(prop_name, "foreground"))
52 {
53 guint16 red, green, blue;
54 red = self->lexer_style->foreground & 255;
55 green = (self->lexer_style->foreground >> 8) & 255;
56 blue = (self->lexer_style->foreground >> 16) & 255;
57 return Py_BuildValue("iii", red, green, blue);
58 }
59 else if (g_str_equal(prop_name, "bold"))
60 {
61 if (self->lexer_style->bold)
62 Py_RETURN_TRUE;
63 else
64 Py_RETURN_FALSE;
65 }
66 else if (g_str_equal(prop_name, "italic"))
67 {
68 if (self->lexer_style->italic)
69 Py_RETURN_TRUE;
70 else
71 Py_RETURN_FALSE;
72 }
73
74 Py_RETURN_NONE;
75 }
76 GEANYPY_PROPS_READONLY(LexerStyle);
77
78
79 static PyGetSetDef LexerStyle_getseters[] = {
80 GEANYPY_GETSETDEF(LexerStyle, "background",
81 "Background color of text, as an (R,G,B) tuple."),
82 GEANYPY_GETSETDEF(LexerStyle, "foreground",
83 "Foreground color of text, as an (R,G,B) tuple."),
84 GEANYPY_GETSETDEF(LexerStyle, "bold",
85 "Whether the text is bold or not."),
86 GEANYPY_GETSETDEF(LexerStyle, "italic",
87 "Whether the text is italic or not."),
88 { NULL }
89 };
90
91
92 static PyTypeObject LexerStyleType = {
93 PyObject_HEAD_INIT(NULL)
94 0, /*ob_size*/
95 "geany.highlighting.LexerStyle", /*tp_name*/
96 sizeof(Editor), /*tp_basicsize*/
97 0, /*tp_itemsize*/
98 (destructor) LexerStyle_dealloc, /*tp_dealloc*/
99 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* tp_print - tp_as_buffer */
100 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
101 "Wrapper around a GeanyLexerStyle structure.", /* tp_doc */
102 0, 0, 0, 0, 0, 0, 0, 0, /* tp_traverse - tp_members */
103 LexerStyle_getseters, /* tp_getset */
104 0, 0, 0, 0, 0, /* tp_base - tp_dictoffset */
105 (initproc) LexerStyle_init, /* tp_init */
106 0, 0, /* tp_alloc - tp_new */
107 };
108
109
110 static PyObject *
Highlighting_get_style(PyObject * module,PyObject * args,PyObject * kwargs)111 Highlighting_get_style(PyObject *module, PyObject *args, PyObject *kwargs)
112 {
113 gint ft_id, style_id;
114 LexerStyle *lexer_style;
115 const GeanyLexerStyle *ls;
116 static gchar *kwlist[] = { "filetype_id", "style_id", NULL };
117
118 if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &ft_id, &style_id))
119 {
120 ls = highlighting_get_style(ft_id, style_id);
121 if (ls != NULL)
122 {
123 lexer_style = (LexerStyle *) PyObject_CallObject((PyObject *) &LexerStyleType, NULL);
124 lexer_style->lexer_style = ls;
125 return (PyObject *) lexer_style;
126 }
127 }
128
129 Py_RETURN_NONE;
130 }
131
132
133 static PyObject *
Highlighting_is_code_style(PyObject * module,PyObject * args,PyObject * kwargs)134 Highlighting_is_code_style(PyObject *module, PyObject *args, PyObject *kwargs)
135 {
136 gint lexer, style;
137 static gchar *kwlist[] = { "lexer", "style", NULL };
138
139 if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &lexer, &style))
140 {
141 if (highlighting_is_code_style(lexer, style))
142 Py_RETURN_TRUE;
143 else
144 Py_RETURN_FALSE;
145 }
146
147 Py_RETURN_NONE;
148 }
149
150
151 static PyObject *
Highlighting_is_comment_style(PyObject * module,PyObject * args,PyObject * kwargs)152 Highlighting_is_comment_style(PyObject *module, PyObject *args, PyObject *kwargs)
153 {
154 gint lexer, style;
155 static gchar *kwlist[] = { "lexer", "style", NULL };
156
157 if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &lexer, &style))
158 {
159 if (highlighting_is_comment_style(lexer, style))
160 Py_RETURN_TRUE;
161 else
162 Py_RETURN_FALSE;
163 }
164
165 Py_RETURN_NONE;
166 }
167
168
169 static PyObject *
Highlighting_is_string_style(PyObject * module,PyObject * args,PyObject * kwargs)170 Highlighting_is_string_style(PyObject *module, PyObject *args, PyObject *kwargs)
171 {
172 gint lexer, style;
173 static gchar *kwlist[] = { "lexer", "style", NULL };
174
175 if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &lexer, &style))
176 {
177 if (highlighting_is_string_style(lexer, style))
178 Py_RETURN_TRUE;
179 else
180 Py_RETURN_FALSE;
181 }
182
183 Py_RETURN_NONE;
184 }
185
186
187 static PyObject *
Highlighting_set_styles(PyObject * module,PyObject * args,PyObject * kwargs)188 Highlighting_set_styles(PyObject *module, PyObject *args, PyObject *kwargs)
189 {
190 PyObject *py_sci, *py_ft;
191 Scintilla *sci;
192 Filetype *ft;
193 static gchar *kwlist[] = { "sci", "filetype", NULL };
194
195 if (PyArg_ParseTupleAndKeywords(args, kwargs, "OO", kwlist, &py_sci, &py_ft))
196 {
197 if (py_sci != Py_None && py_ft != Py_None)
198 {
199 sci = (Scintilla *) py_sci;
200 ft = (Filetype *) py_ft;
201 highlighting_set_styles(sci->sci, ft->ft);
202 }
203 }
204
205 Py_RETURN_NONE;
206 }
207
208
209 static
210 PyMethodDef EditorModule_methods[] = {
211 { "get_style", (PyCFunction) Highlighting_get_style, METH_KEYWORDS },
212 { "is_code_style", (PyCFunction) Highlighting_is_code_style, METH_KEYWORDS },
213 { "is_comment_style", (PyCFunction) Highlighting_is_comment_style, METH_KEYWORDS },
214 { "is_string_style", (PyCFunction) Highlighting_is_string_style, METH_KEYWORDS },
215 { "set_styles", (PyCFunction) Highlighting_set_styles, METH_KEYWORDS },
216 { NULL }
217 };
218
219
220 PyMODINIT_FUNC
inithighlighting(void)221 inithighlighting(void)
222 {
223 PyObject *m;
224
225 LexerStyleType.tp_new = PyType_GenericNew;
226 if (PyType_Ready(&LexerStyleType) < 0)
227 return;
228
229 m = Py_InitModule3("highlighting", EditorModule_methods,
230 "Highlighting information and management.");
231
232 Py_INCREF(&LexerStyleType);
233 PyModule_AddObject(m, "LexerStyle", (PyObject *)&LexerStyleType);
234 }
235