1 #if defined(HAVE_CONFIG_H) && !defined(GEANYPY_WINDOWS)
2 # include "config.h"
3 #endif
4 
5 #include "geanypy.h"
6 
7 
8 /* Bail-out when ScintillaObject being wrapped is NULL. */
9 #define SCI_RET_IF_FAIL(obj) { \
10 	if (!self->sci) { \
11 		PyErr_SetString(PyExc_RuntimeError, \
12 			"Scintilla instance not initialized properly."); \
13 		Py_RETURN_NONE; } }
14 
15 
16 static void
Scintilla_dealloc(Scintilla * self)17 Scintilla_dealloc(Scintilla *self)
18 {
19 	self->ob_type->tp_free((PyObject *) self);
20 }
21 
22 
23 static int
Scintilla_init(Scintilla * self)24 Scintilla_init(Scintilla *self)
25 {
26 	self->sci = NULL;
27 	return 0;
28 }
29 
30 
31 static PyObject *
Scintilla_get_property(Scintilla * self,const gchar * prop_name)32 Scintilla_get_property(Scintilla *self, const gchar *prop_name)
33 {
34 	g_return_val_if_fail(self != NULL, NULL);
35 	g_return_val_if_fail(prop_name != NULL, NULL);
36 
37 	if (!self->sci)
38 	{
39 		PyErr_SetString(PyExc_RuntimeError,
40 			"Scintilla instance not initialized properly");
41 		return NULL;
42 	}
43 
44 	if (g_str_equal(prop_name, "widget"))
45 		return pygobject_new(G_OBJECT(self->sci));
46 
47 	Py_RETURN_NONE;
48 }
49 GEANYPY_PROPS_READONLY(Scintilla);
50 
51 
52 static PyObject *
Scintilla_delete_marker_at_line(Scintilla * self,PyObject * args,PyObject * kwargs)53 Scintilla_delete_marker_at_line(Scintilla *self, PyObject *args, PyObject *kwargs)
54 {
55 	gint line_num, marker;
56 	static gchar *kwlist[] = { "line_number", "marker", NULL };
57 
58 	SCI_RET_IF_FAIL(self);
59 
60 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &line_num, &marker))
61 			sci_delete_marker_at_line(self->sci, line_num, marker);
62 	Py_RETURN_NONE;
63 }
64 
65 
66 static PyObject *
Scintilla_end_undo_action(Scintilla * self)67 Scintilla_end_undo_action(Scintilla *self)
68 {
69 	SCI_RET_IF_FAIL(self);
70 	sci_end_undo_action(self->sci);
71 	Py_RETURN_NONE;
72 }
73 
74 
75 static PyObject *
Scintilla_ensure_line_is_visible(Scintilla * self,PyObject * args,PyObject * kwargs)76 Scintilla_ensure_line_is_visible(Scintilla *self, PyObject *args, PyObject *kwargs)
77 {
78 	gint line = -1;
79 	static gchar *kwlist[] = { "line", NULL };
80 
81 	SCI_RET_IF_FAIL(self);
82 
83 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line))
84 	{
85 		if (line == -1)
86 			line = sci_get_current_line(self->sci);
87 		sci_ensure_line_is_visible(self->sci, line);
88 	}
89 
90 	Py_RETURN_NONE;
91 }
92 
93 
94 static PyObject *
Scintilla_find_matching_brace(Scintilla * self,PyObject * args,PyObject * kwargs)95 Scintilla_find_matching_brace(Scintilla *self, PyObject *args, PyObject *kwargs)
96 {
97 	gint pos, match_pos;
98 	static gchar *kwlist[] = { "pos", NULL };
99 
100 	SCI_RET_IF_FAIL(self);
101 
102 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pos))
103 	{
104 		match_pos = sci_find_matching_brace(self->sci, pos);
105 		return Py_BuildValue("i", match_pos);
106 	}
107 	Py_RETURN_NONE;
108 }
109 
110 
111 static PyObject *
Scintilla_find_text(Scintilla * self,PyObject * args,PyObject * kwargs)112 Scintilla_find_text(Scintilla *self, PyObject *args, PyObject *kwargs)
113 {
114 	gint pos = -1, flags = 0;
115 	glong start_chr = 0, end_chr = 0;
116 	gchar *search_text;
117 	struct Sci_TextToFind ttf = { { 0 } };
118 	static gchar *kwlist[] = { "text", "flags", "start_char", "end_char", NULL };
119 
120 	SCI_RET_IF_FAIL(self);
121 
122 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "s|ill", kwlist,
123 			&search_text, &flags, &start_chr, &end_chr))
124 	{
125 		ttf.chrg.cpMin = start_chr;
126 		ttf.chrg.cpMax = end_chr;
127 		ttf.lpstrText = search_text;
128 		pos = sci_find_text(self->sci, flags, &ttf);
129 		if (pos > -1)
130 			return Py_BuildValue("ll", ttf.chrgText.cpMin, ttf.chrgText.cpMax);
131 	}
132 
133 	Py_RETURN_NONE;
134 }
135 
136 
137 static PyObject *
Scintilla_get_char_at(Scintilla * self,PyObject * args,PyObject * kwargs)138 Scintilla_get_char_at(Scintilla *self, PyObject *args, PyObject *kwargs)
139 {
140 	gint pos;
141 	gchar chr;
142 	static gchar *kwlist[] = { "pos", NULL };
143 
144 	SCI_RET_IF_FAIL(self);
145 
146 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pos))
147 	{
148 		chr = sci_get_char_at(self->sci, pos);
149 		return PyString_FromFormat("%c", chr);
150 	}
151 
152 	Py_RETURN_NONE;
153 }
154 
155 
156 static PyObject *
Scintilla_get_col_from_position(Scintilla * self,PyObject * args,PyObject * kwargs)157 Scintilla_get_col_from_position(Scintilla *self, PyObject *args, PyObject *kwargs)
158 {
159 	gint pos, col;
160 	static gchar *kwlist[] = { "pos", NULL };
161 
162 	SCI_RET_IF_FAIL(self);
163 
164 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pos))
165 	{
166 		col = sci_get_col_from_position(self->sci, pos);
167 		return Py_BuildValue("i", col);
168 	}
169 
170 	Py_RETURN_NONE;
171 }
172 
173 
174 static PyObject *
Scintilla_get_contents(Scintilla * self,PyObject * args,PyObject * kwargs)175 Scintilla_get_contents(Scintilla *self, PyObject *args, PyObject *kwargs)
176 {
177 	gint len = -1;
178 	gchar *text;
179 	PyObject *py_text;
180 	static gchar *kwlist[] = { "len", NULL };
181 
182 	SCI_RET_IF_FAIL(self);
183 
184 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &len))
185 	{
186 		if (len == -1)
187 			len = sci_get_length(self->sci) + 1;
188 		text = sci_get_contents(self->sci, len);
189 		if (text == NULL)
190 			Py_RETURN_NONE;
191 		py_text = PyString_FromString(text);
192 		g_free(text);
193 		return py_text;
194 	}
195 
196 	Py_RETURN_NONE;
197 }
198 
199 
200 static PyObject *
Scintilla_get_contents_range(Scintilla * self,PyObject * args,PyObject * kwargs)201 Scintilla_get_contents_range(Scintilla *self, PyObject *args, PyObject *kwargs)
202 {
203 	gint start = -1, end = -1;
204 	gchar *text;
205 	PyObject *py_text;
206 	static gchar *kwlist[] = { "start", "end", NULL };
207 
208 	SCI_RET_IF_FAIL(self);
209 
210 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, &start, &end))
211 	{
212 		if (start == -1)
213 			start = 0;
214 		if (end == -1)
215 			end = sci_get_length(self->sci) + 1;
216 		text = sci_get_contents_range(self->sci, start, end);
217 		if (text == NULL)
218 			Py_RETURN_NONE;
219 		py_text = PyString_FromString(text);
220 		g_free(text);
221 		return py_text;
222 	}
223 
224 	Py_RETURN_NONE;
225 }
226 
227 
228 static PyObject *
Scintilla_get_current_line(Scintilla * self)229 Scintilla_get_current_line(Scintilla *self)
230 {
231 	gint line;
232 	SCI_RET_IF_FAIL(self);
233 	line = sci_get_current_line(self->sci);
234 	return Py_BuildValue("i", line);
235 }
236 
237 static PyObject *
Scintilla_get_current_position(Scintilla * self)238 Scintilla_get_current_position(Scintilla *self)
239 {
240 	gint pos;
241 	SCI_RET_IF_FAIL(self);
242 	pos = sci_get_current_position(self->sci);
243 	return Py_BuildValue("i", pos);
244 }
245 
246 
247 static PyObject *
Scintilla_get_length(Scintilla * self)248 Scintilla_get_length(Scintilla *self)
249 {
250 	gint len;
251 	SCI_RET_IF_FAIL(self);
252 	len = sci_get_length(self->sci);
253 	return Py_BuildValue("i", len);
254 }
255 
256 
257 static PyObject *
Scintilla_get_line(Scintilla * self,PyObject * args,PyObject * kwargs)258 Scintilla_get_line(Scintilla *self, PyObject *args, PyObject *kwargs)
259 {
260 	gint line_num = -1;
261 	gchar *text;
262 	PyObject *py_text;
263 	static gchar *kwlist[] = { "line_num", NULL };
264 
265 	SCI_RET_IF_FAIL(self);
266 
267 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line_num))
268 	{
269 		if (line_num == -1)
270 			line_num = sci_get_current_line(self->sci);
271 		text = sci_get_line(self->sci, line_num);
272 		if (text == NULL)
273 			Py_RETURN_NONE;
274 		py_text = PyString_FromString(text);
275 		g_free(text);
276 		return py_text;
277 	}
278 
279 	Py_RETURN_NONE;
280 }
281 
282 
283 static PyObject *
Scintilla_get_line_count(Scintilla * self)284 Scintilla_get_line_count(Scintilla *self)
285 {
286 	gint line_count;
287 	SCI_RET_IF_FAIL(self);
288 	line_count = sci_get_line_count(self->sci);
289 	return Py_BuildValue("i", line_count);
290 }
291 
292 
293 static PyObject *
Scintilla_get_line_end_position(Scintilla * self,PyObject * args,PyObject * kwargs)294 Scintilla_get_line_end_position(Scintilla *self, PyObject *args, PyObject *kwargs)
295 {
296 	gint line = -1, line_end_pos;
297 	static gchar *kwlist[] = { "line", NULL };
298 
299 	SCI_RET_IF_FAIL(self);
300 
301 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line))
302 	{
303 		if (line == -1)
304 			line = sci_get_current_line(self->sci);
305 		line_end_pos = sci_get_line_end_position(self->sci, line);
306 		return Py_BuildValue("i", line_end_pos);
307 	}
308 
309 	Py_RETURN_NONE;
310 }
311 
312 
313 static PyObject *
Scintilla_get_line_from_position(Scintilla * self,PyObject * args,PyObject * kwargs)314 Scintilla_get_line_from_position(Scintilla *self, PyObject *args, PyObject *kwargs)
315 {
316 	gint line, pos;
317 	static gchar *kwlist[] = { "pos", NULL };
318 
319 	SCI_RET_IF_FAIL(self);
320 
321 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &pos))
322 	{
323 		if (pos == -1)
324 			pos = sci_get_current_position(self->sci);
325 		line = sci_get_line_from_position(self->sci, pos);
326 		return Py_BuildValue("i", line);
327 	}
328 
329 	Py_RETURN_NONE;
330 }
331 
332 
333 static PyObject *
Scintilla_get_line_indentation(Scintilla * self,PyObject * args,PyObject * kwargs)334 Scintilla_get_line_indentation(Scintilla *self, PyObject *args, PyObject *kwargs)
335 {
336 	gint line = -1, width;
337 	static gchar *kwlist[] = { "line", NULL };
338 
339 	SCI_RET_IF_FAIL(self);
340 
341 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line))
342 	{
343 		if (line == -1)
344 			line = sci_get_current_line(self->sci);
345 		width = sci_get_line_indentation(self->sci, line);
346 		return Py_BuildValue("i", width);
347 	}
348 
349 	Py_RETURN_NONE;
350 }
351 
352 
353 static PyObject *
Scintilla_get_line_is_visible(Scintilla * self,PyObject * args,PyObject * kwargs)354 Scintilla_get_line_is_visible(Scintilla *self, PyObject *args, PyObject *kwargs)
355 {
356 	gint line = -1;
357 	gboolean visible;
358 	static gchar *kwlist[] = { "line", NULL };
359 
360 	SCI_RET_IF_FAIL(self);
361 
362 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line))
363 	{
364 		if (line == -1)
365 			line = sci_get_current_line(self->sci);
366 		visible = sci_get_line_is_visible(self->sci, line);
367 		if (visible)
368 			Py_RETURN_TRUE;
369 		else
370 			Py_RETURN_FALSE;
371 	}
372 
373 	Py_RETURN_NONE;
374 }
375 
376 
377 static PyObject *
Scintilla_get_line_length(Scintilla * self,PyObject * args,PyObject * kwargs)378 Scintilla_get_line_length(Scintilla *self, PyObject *args, PyObject *kwargs)
379 {
380 	gint line = -1, length;
381 	static gchar *kwlist[] = { "line", NULL };
382 
383 	SCI_RET_IF_FAIL(self);
384 
385 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line))
386 	{
387 		if (line == -1)
388 			line = sci_get_current_line(self->sci);
389 		length = sci_get_line_length(self->sci, line);
390 		return Py_BuildValue("i", length);
391 	}
392 
393 	Py_RETURN_NONE;
394 }
395 
396 
397 
398 static PyObject *
Scintilla_get_position_from_line(Scintilla * self,PyObject * args,PyObject * kwargs)399 Scintilla_get_position_from_line(Scintilla *self, PyObject *args, PyObject *kwargs)
400 {
401 	gint line = -1, pos;
402 	static gchar *kwlist[] = { "line", NULL };
403 
404 	SCI_RET_IF_FAIL(self);
405 
406 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line))
407 	{
408 		if (line == -1)
409 			line = sci_get_current_line(self->sci);
410 		pos = sci_get_position_from_line(self->sci, line);
411 		return Py_BuildValue("i", pos);
412 	}
413 
414 	Py_RETURN_NONE;
415 }
416 
417 
418 static PyObject *
Scintilla_get_selected_text_length(Scintilla * self)419 Scintilla_get_selected_text_length(Scintilla *self)
420 {
421 	gint len;
422 	SCI_RET_IF_FAIL(self);
423 	len = sci_get_selected_text_length(self->sci);
424 	return Py_BuildValue("i", len);
425 }
426 
427 
428 static PyObject *
Scintilla_get_selection_contents(Scintilla * self)429 Scintilla_get_selection_contents(Scintilla *self)
430 {
431 	gchar *text;
432 	PyObject *py_text;
433 	SCI_RET_IF_FAIL(self);
434 	text = sci_get_selection_contents(self->sci);
435 	if (text == NULL)
436 		Py_RETURN_NONE;
437 	py_text = PyString_FromString(text);
438 	g_free(text);
439 	return py_text;
440 }
441 
442 
443 static PyObject *
Scintilla_get_selection_end(Scintilla * self)444 Scintilla_get_selection_end(Scintilla *self)
445 {
446 	gint pos;
447 	SCI_RET_IF_FAIL(self);
448 	pos = sci_get_selection_end(self->sci);
449 	return Py_BuildValue("i", pos);
450 }
451 
452 
453 static PyObject *
Scintilla_get_selection_mode(Scintilla * self)454 Scintilla_get_selection_mode(Scintilla *self)
455 {
456 	gint mode;
457 	SCI_RET_IF_FAIL(self);
458 	mode = sci_get_selection_mode(self->sci);
459 	return Py_BuildValue("i", mode);
460 }
461 
462 
463 static PyObject *
Scintilla_get_selection_start(Scintilla * self)464 Scintilla_get_selection_start(Scintilla *self)
465 {
466 	gint pos;
467 	SCI_RET_IF_FAIL(self);
468 	pos = sci_get_selection_start(self->sci);
469 	return Py_BuildValue("i", pos);
470 }
471 
472 
473 static PyObject *
Scintilla_get_style_at(Scintilla * self,PyObject * args,PyObject * kwargs)474 Scintilla_get_style_at(Scintilla *self, PyObject *args, PyObject *kwargs)
475 {
476 	gint pos = -1, style;
477 	static gchar *kwlist[] = { "pos", NULL };
478 
479 	SCI_RET_IF_FAIL(self);
480 
481 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &pos))
482 	{
483 		if (pos == -1)
484 			pos = sci_get_current_position(self->sci);
485 		style = sci_get_style_at(self->sci, pos);
486 		return Py_BuildValue("i", style);
487 	}
488 
489 	Py_RETURN_NONE;
490 }
491 
492 
493 static PyObject *
Scintilla_get_tab_width(Scintilla * self)494 Scintilla_get_tab_width(Scintilla *self)
495 {
496 	gint width;
497 	SCI_RET_IF_FAIL(self);
498 	width = sci_get_tab_width(self->sci);
499 	return Py_BuildValue("i", width);
500 }
501 
502 
503 static PyObject *
Scintilla_goto_line(Scintilla * self,PyObject * args,PyObject * kwargs)504 Scintilla_goto_line(Scintilla *self, PyObject *args, PyObject *kwargs)
505 {
506 	gint line, unfold;
507 	static gchar *kwlist[] = { "line", "unfold", NULL };
508 
509 	SCI_RET_IF_FAIL(self);
510 
511 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &line, &unfold))
512 		sci_goto_line(self->sci, line, (gboolean) unfold);
513 
514 	Py_RETURN_NONE;
515 }
516 
517 
518 static PyObject *
Scintilla_has_selection(Scintilla * self)519 Scintilla_has_selection(Scintilla *self)
520 {
521 	SCI_RET_IF_FAIL(self);
522 	if (sci_has_selection(self->sci))
523 		Py_RETURN_TRUE;
524 	else
525 		Py_RETURN_FALSE;
526 }
527 
528 
529 static PyObject *
Scintilla_indicator_clear(Scintilla * self,PyObject * args,PyObject * kwargs)530 Scintilla_indicator_clear(Scintilla *self, PyObject *args, PyObject *kwargs)
531 {
532 	gint pos, len;
533 	static gchar *kwlist[] = { "pos", "len", NULL };
534 
535 	SCI_RET_IF_FAIL(self);
536 
537 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &pos, &len))
538 		sci_indicator_clear(self->sci, pos, len);
539 
540 	Py_RETURN_NONE;
541 }
542 
543 
544 static PyObject *
Scintilla_indicator_set(Scintilla * self,PyObject * args,PyObject * kwargs)545 Scintilla_indicator_set(Scintilla *self, PyObject *args, PyObject *kwargs)
546 {
547 	gint indic;
548 	static gchar *kwlist[] = { "indic", NULL };
549 
550 	SCI_RET_IF_FAIL(self);
551 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &indic))
552 		sci_indicator_set(self->sci, indic);
553 
554 	Py_RETURN_NONE;
555 }
556 
557 
558 static PyObject *
Scintilla_insert_text(Scintilla * self,PyObject * args,PyObject * kwargs)559 Scintilla_insert_text(Scintilla *self, PyObject *args, PyObject *kwargs)
560 {
561 	gint pos = -1;
562 	gchar *text;
563 	static gchar *kwlist[] = { "text", "pos", NULL };
564 
565 	SCI_RET_IF_FAIL(self);
566 
567 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, &text, &pos))
568 	{
569 		if (pos == -1)
570 			pos = sci_get_current_position(self->sci);
571 		if (text != NULL)
572 			sci_insert_text(self->sci, pos, text);
573 	}
574 
575 	Py_RETURN_NONE;
576 }
577 
578 
579 static PyObject *
Scintilla_is_marker_set_at_line(Scintilla * self,PyObject * args,PyObject * kwargs)580 Scintilla_is_marker_set_at_line(Scintilla *self, PyObject *args, PyObject *kwargs)
581 {
582 	gboolean result;
583 	gint line, marker;
584 	static gchar *kwlist[] = { "line", "marker", NULL };
585 
586 	SCI_RET_IF_FAIL(self);
587 
588 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &line, &marker))
589 	{
590 		result = sci_is_marker_set_at_line(self->sci, line, marker);
591 		if (result)
592 			Py_RETURN_TRUE;
593 		else
594 			Py_RETURN_FALSE;
595 	}
596 
597 	Py_RETURN_NONE;
598 }
599 
600 
601 static PyObject *
Scintilla_replace_sel(Scintilla * self,PyObject * args,PyObject * kwargs)602 Scintilla_replace_sel(Scintilla *self, PyObject *args, PyObject *kwargs)
603 {
604 	gchar *text;
605 	static gchar *kwlist[] = { "text", NULL };
606 	SCI_RET_IF_FAIL(self);
607 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &text))
608 		sci_replace_sel(self->sci, text);
609 	Py_RETURN_NONE;
610 }
611 
612 
613 static PyObject *
Scintilla_scroll_caret(Scintilla * self)614 Scintilla_scroll_caret(Scintilla *self)
615 {
616 	SCI_RET_IF_FAIL(self);
617 	sci_scroll_caret(self->sci);
618 	Py_RETURN_NONE;
619 }
620 
621 
622 static PyObject *
Scintilla_send_command(Scintilla * self,PyObject * args,PyObject * kwargs)623 Scintilla_send_command(Scintilla *self, PyObject *args, PyObject *kwargs)
624 {
625 	gint cmd;
626 	static gchar *kwlist[] = { "cmd", NULL };
627 
628 	SCI_RET_IF_FAIL(self);
629 
630 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &cmd))
631 		sci_send_command(self->sci, cmd);
632 
633 	Py_RETURN_NONE;
634 }
635 
636 
637 static PyObject *
Scintilla_set_current_position(Scintilla * self,PyObject * args,PyObject * kwargs)638 Scintilla_set_current_position(Scintilla *self, PyObject *args, PyObject *kwargs)
639 {
640 	gint pos, stc = FALSE;
641 	static gchar *kwlist[] = { "pos", "scroll_to_caret", NULL };
642 
643 	SCI_RET_IF_FAIL(self);
644 
645 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, &pos, &stc))
646 		sci_set_current_position(self->sci, pos, stc);
647 
648 	Py_RETURN_NONE;
649 }
650 
651 
652 static PyObject *
Scintilla_set_font(Scintilla * self,PyObject * args,PyObject * kwargs)653 Scintilla_set_font(Scintilla *self, PyObject *args, PyObject *kwargs)
654 {
655 	gint style, size;
656 	gchar *font;
657 	static gchar *kwlist[] = { "style", "font", "size", NULL };
658 
659 	SCI_RET_IF_FAIL(self);
660 
661 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "isi", kwlist, &style, &font, &size))
662 		sci_set_font(self->sci, style, font, size);
663 
664 	Py_RETURN_NONE;
665 }
666 
667 
668 static PyObject *
Scintilla_set_line_indentation(Scintilla * self,PyObject * args,PyObject * kwargs)669 Scintilla_set_line_indentation(Scintilla *self, PyObject *args, PyObject *kwargs)
670 {
671 	gint line, indent;
672 	static gchar *kwlist[] = { "line", "indent", NULL };
673 
674 	SCI_RET_IF_FAIL(self);
675 
676 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &line, &indent))
677 		sci_set_line_indentation(self->sci, line, indent);
678 
679 	Py_RETURN_NONE;
680 }
681 
682 
683 static PyObject *
Scintilla_set_marker_at_line(Scintilla * self,PyObject * args,PyObject * kwargs)684 Scintilla_set_marker_at_line(Scintilla *self, PyObject *args, PyObject *kwargs)
685 {
686 	gint line, marker;
687 	static gchar *kwlist[] = { "line", "marker", NULL };
688 
689 	SCI_RET_IF_FAIL(self);
690 
691 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "ii", kwlist, &line, &marker))
692 		sci_set_marker_at_line(self->sci, line, marker);
693 
694 	Py_RETURN_NONE;
695 }
696 
697 
698 static PyObject *
Scintilla_set_selection_end(Scintilla * self,PyObject * args,PyObject * kwargs)699 Scintilla_set_selection_end(Scintilla *self, PyObject *args, PyObject *kwargs)
700 {
701 	gint pos;
702 	static gchar *kwlist[] = { "pos", NULL };
703 
704 	SCI_RET_IF_FAIL(self);
705 
706 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pos))
707 		sci_set_selection_end(self->sci, pos);
708 
709 	Py_RETURN_NONE;
710 }
711 
712 
713 static PyObject *
Scintilla_set_selection_mode(Scintilla * self,PyObject * args,PyObject * kwargs)714 Scintilla_set_selection_mode(Scintilla *self, PyObject *args, PyObject *kwargs)
715 {
716 	gint mode;
717 	static gchar *kwlist[] = { "mode", NULL };
718 
719 	SCI_RET_IF_FAIL(self);
720 
721 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &mode))
722 		sci_set_selection_mode(self->sci, mode);
723 
724 	Py_RETURN_NONE;
725 }
726 
727 
728 static PyObject *
Scintilla_set_selection_start(Scintilla * self,PyObject * args,PyObject * kwargs)729 Scintilla_set_selection_start(Scintilla *self, PyObject *args, PyObject *kwargs)
730 {
731 	gint pos;
732 	static gchar *kwlist[] = { "pos", NULL };
733 
734 	SCI_RET_IF_FAIL(self);
735 
736 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pos))
737 		sci_set_selection_start(self->sci, pos);
738 
739 	Py_RETURN_NONE;
740 }
741 
742 
743 static PyObject *
Scintilla_set_text(Scintilla * self,PyObject * args,PyObject * kwargs)744 Scintilla_set_text(Scintilla *self, PyObject *args, PyObject *kwargs)
745 {
746 	gchar *text;
747 	static gchar *kwlist[] = { "text", NULL };
748 
749 	SCI_RET_IF_FAIL(self);
750 
751 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &text))
752 		sci_set_text(self->sci, text);
753 
754 	Py_RETURN_NONE;
755 }
756 
757 
758 static PyObject *
Scintilla_start_undo_action(Scintilla * self)759 Scintilla_start_undo_action(Scintilla *self)
760 {
761 	SCI_RET_IF_FAIL(self);
762 	sci_start_undo_action(self->sci);
763 	Py_RETURN_NONE;
764 }
765 
766 
767 static PyObject *
Scintilla_send_message(Scintilla * self,PyObject * args,PyObject * kwargs)768 Scintilla_send_message(Scintilla *self, PyObject *args, PyObject *kwargs)
769 {
770 	gint msg;
771 	glong uptr = 0, sptr = 0, ret;
772 	static gchar *kwlist[] = { "msg", "lparam", "wparam", NULL };
773 
774 	SCI_RET_IF_FAIL(self);
775 
776 	if (PyArg_ParseTupleAndKeywords(args, kwargs, "i|ll", kwlist, &msg, &uptr, &sptr))
777 	{
778 		ret = scintilla_send_message(self->sci, msg, uptr, sptr);
779 		return Py_BuildValue("l", ret);
780 	}
781 
782 	Py_RETURN_NONE;
783 }
784 
785 
786 static PyMethodDef Scintilla_methods[] = {
787 	{ "delete_marker_at_line", (PyCFunction) Scintilla_delete_marker_at_line, METH_KEYWORDS,
788 		"Deletes a line marker." },
789 	{ "end_undo_action", (PyCFunction) Scintilla_end_undo_action, METH_NOARGS,
790 		"Ends grouping a set of edits together as one Undo action." },
791 	{ "ensure_line_is_visible", (PyCFunction) Scintilla_ensure_line_is_visible, METH_KEYWORDS,
792 		"Makes line visible (folding may have hidden it)." },
793 	{ "find_matching_brace", (PyCFunction) Scintilla_find_matching_brace, METH_KEYWORDS,
794 		"Finds a matching brace at pos." },
795 	{ "find_text", (PyCFunction) Scintilla_find_text, METH_KEYWORDS,
796 		"Finds text in the document." },
797 	{ "get_char_at", (PyCFunction) Scintilla_get_char_at, METH_KEYWORDS,
798 		"Gets the character at a position." },
799 	{ "get_col_from_position", (PyCFunction) Scintilla_get_col_from_position, METH_KEYWORDS,
800 		"Gets the column number relative to the start of the line that "
801 		"pos is on." },
802 	{ "get_contents", (PyCFunction) Scintilla_get_contents, METH_KEYWORDS,
803 		"Gets all text inside a given text length." },
804 	{ "get_contents_range", (PyCFunction) Scintilla_get_contents_range, METH_KEYWORDS,
805 		"Gets text between start and end." },
806 	{ "get_current_line", (PyCFunction) Scintilla_get_current_line, METH_NOARGS,
807 		"Gets current line number." },
808 	{ "get_current_position", (PyCFunction) Scintilla_get_current_position, METH_NOARGS,
809 		"Gets the cursor position." },
810 	{ "get_length", (PyCFunction) Scintilla_get_length, METH_NOARGS,
811 		"Gets the length of all text." },
812 	{ "get_line", (PyCFunction) Scintilla_get_line, METH_KEYWORDS,
813 		"Gets line contents." },
814 	{ "get_line_count", (PyCFunction) Scintilla_get_line_count, METH_NOARGS,
815 		"Gets the total number of lines." },
816 	{ "get_line_end_position", (PyCFunction) Scintilla_get_line_end_position, METH_KEYWORDS,
817 		"Gets the position at the end of a line." },
818 	{ "get_line_from_position", (PyCFunction) Scintilla_get_line_from_position, METH_KEYWORDS,
819 		"Gets the line number from pos." },
820 	{ "get_line_indentation", (PyCFunction) Scintilla_get_line_indentation, METH_KEYWORDS,
821 		"Gets the indentation width of a line." },
822 	{ "get_line_is_visible", (PyCFunction) Scintilla_get_line_is_visible, METH_KEYWORDS,
823 		"Checks if a line is visible (folding may have hidden it)." },
824 	{ "get_line_length", (PyCFunction) Scintilla_get_line_length, METH_KEYWORDS,
825 		"Gets line length." },
826 	{ "get_position_from_line", (PyCFunction) Scintilla_get_position_from_line, METH_KEYWORDS,
827 		"Gets the position for the start of line." },
828 	{ "get_selected_text_length", (PyCFunction) Scintilla_get_selected_text_length, METH_NOARGS,
829 		"Gets selected text length."},
830 	{ "get_selection_contents", (PyCFunction) Scintilla_get_selection_contents, METH_NOARGS,
831 		"Gets selected text." },
832 	{ "get_selection_end", (PyCFunction) Scintilla_get_selection_end, METH_NOARGS,
833 		"Gets the selection end position." },
834 	{ "get_selection_mode", (PyCFunction) Scintilla_get_selection_mode, METH_NOARGS,
835 		"Gets the selection mode." },
836 	{ "get_selection_start", (PyCFunction) Scintilla_get_selection_start, METH_NOARGS,
837 		"Gets the selection start position." },
838 	{ "get_style_at", (PyCFunction) Scintilla_get_style_at, METH_KEYWORDS,
839 		"Gets the style ID at pos." },
840 	{ "get_tab_width", (PyCFunction) Scintilla_get_tab_width, METH_NOARGS,
841 		"Gets display tab width (this is not indent width, see IndentPrefs)." },
842 	{ "goto_line", (PyCFunction) Scintilla_goto_line, METH_KEYWORDS,
843 		"Jumps to the specified line in the document." },
844 	{ "has_selection", (PyCFunction) Scintilla_has_selection, METH_NOARGS,
845 		"Checks if there's a selection." },
846 	{ "indicator_clear", (PyCFunction) Scintilla_indicator_clear, METH_KEYWORDS,
847 		"Clears the currently set indicator from a range of text." },
848 	{ "indicator_set", (PyCFunction) Scintilla_indicator_set, METH_KEYWORDS,
849 		"Sets the current indicator." },
850 	{ "insert_text", (PyCFunction) Scintilla_insert_text, METH_KEYWORDS,
851 		"Inserts text at pos." },
852 	{ "is_marker_set_at_line", (PyCFunction) Scintilla_is_marker_set_at_line, METH_KEYWORDS,
853 		"Checks if a line has a marker set." },
854 	{ "replace_sel", (PyCFunction) Scintilla_replace_sel, METH_KEYWORDS,
855 		"Replaces selection." },
856 	{ "scroll_caret", (PyCFunction) Scintilla_scroll_caret, METH_NOARGS,
857 		"Scrolls the cursor in view." },
858 	{ "send_command", (PyCFunction) Scintilla_send_command, METH_KEYWORDS,
859 		"Sends Scintilla commands without any parameters (see send_message function)." },
860 	{ "set_current_position", (PyCFunction) Scintilla_set_current_position, METH_KEYWORDS,
861 		"Sets the cursor position." },
862 	{ "set_font", (PyCFunction) Scintilla_set_font, METH_KEYWORDS,
863 		"Sets the font and size for a particular style." },
864 	{ "set_line_indentation", (PyCFunction) Scintilla_set_line_indentation, METH_KEYWORDS,
865 		"Sets the indentation of a line." },
866 	{ "set_marker_at_line", (PyCFunction) Scintilla_set_marker_at_line, METH_KEYWORDS,
867 		"Sets a line marker." },
868 	{ "set_selection_end", (PyCFunction) Scintilla_set_selection_end, METH_KEYWORDS,
869 		"Sets the selection end position." },
870 	{ "set_selection_mode", (PyCFunction) Scintilla_set_selection_mode, METH_KEYWORDS,
871 		"Sets selection mode." },
872 	{ "set_selection_start", (PyCFunction) Scintilla_set_selection_start, METH_KEYWORDS,
873 		"Sets the selection start position." },
874 	{ "set_text", (PyCFunction) Scintilla_set_text, METH_KEYWORDS,
875 		"Sets all text." },
876 	{ "start_undo_action", (PyCFunction) Scintilla_start_undo_action, METH_NOARGS,
877 		"Begins grouping a set of edits together as one Undo action." },
878 	{ "send_message", (PyCFunction) Scintilla_send_message, METH_KEYWORDS,
879 		"Send a message to the Scintilla widget." },
880 	{ NULL }
881 };
882 
883 
884 static PyGetSetDef Scintilla_getseters[] = {
885 	GEANYPY_GETSETDEF(Scintilla, "widget",
886 		"Gets the ScintillaObject as a GTK+ widget."),
887 	{ NULL }
888 };
889 
890 
891 static PyTypeObject ScintillaType = {
892 	PyObject_HEAD_INIT(NULL)
893 	0,												/* ob_size */
894 	"geany.scintilla.Scintilla",					/* tp_name */
895 	sizeof(Scintilla),								/* tp_basicsize */
896 	0,												/* tp_itemsize */
897 	(destructor) Scintilla_dealloc,					/* tp_dealloc */
898 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,		/* tp_print - tp_as_buffer */
899 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,		/* tp_flags */
900 	"Wrapper around a ScintillaObject structure.",	/* tp_doc */
901 	0, 0, 0, 0, 0, 0,								/* tp_traverse - tp_iternext */
902 	Scintilla_methods,								/* tp_methods */
903 	0,												/* tp_members */
904 	Scintilla_getseters,							/* tp_getset */
905 	0, 0, 0, 0, 0,									/* tp_base - tp_dictoffset */
906 	(initproc) Scintilla_init,						/* tp_init */
907 	0, 0,											/* tp_alloc - tp_new */
908 };
909 
910 static PyMethodDef ScintillaModule_methods[] = { { NULL } };
911 
912 
initscintilla(void)913 PyMODINIT_FUNC initscintilla(void)
914 {
915 	PyObject *m;
916 
917 	ScintillaType.tp_new = PyType_GenericNew;
918 	if (PyType_Ready(&ScintillaType) < 0)
919 		return;
920 
921 	NotificationType.tp_new = PyType_GenericNew;
922 	if (PyType_Ready(&NotificationType) < 0)
923 		return;
924 
925 	NotifyHeaderType.tp_new = PyType_GenericNew;
926 	if (PyType_Ready(&NotifyHeaderType) < 0)
927 		return;
928 
929 	m = Py_InitModule("scintilla", ScintillaModule_methods);
930 
931 	Py_INCREF(&ScintillaType);
932 	PyModule_AddObject(m, "Scintilla", (PyObject *)&ScintillaType);
933 
934 	Py_INCREF(&NotificationType);
935 	PyModule_AddObject(m, "Notification", (PyObject *)&NotificationType);
936 
937 	Py_INCREF(&NotifyHeaderType);
938 	PyModule_AddObject(m, "NotifyHeader", (PyObject *)&NotifyHeaderType);
939 
940 
941 	PyModule_AddIntConstant(m, "FLAG_WHOLE_WORD", SCFIND_WHOLEWORD);
942 	PyModule_AddIntConstant(m, "FLAG_MATCH_CASE", SCFIND_MATCHCASE);
943 	PyModule_AddIntConstant(m, "FLAG_WORD_START", SCFIND_WORDSTART);
944 	PyModule_AddIntConstant(m, "FLAG_REGEXP", SCFIND_REGEXP);
945 	PyModule_AddIntConstant(m, "FLAG_POSIX", SCFIND_POSIX);
946 
947 	PyModule_AddIntConstant(m, "UPDATE_CONTENT", SC_UPDATE_CONTENT);
948 	PyModule_AddIntConstant(m, "UPDATE_SELECTION", SC_UPDATE_SELECTION);
949 	PyModule_AddIntConstant(m, "UPDATE_V_SCROLL", SC_UPDATE_V_SCROLL);
950 	PyModule_AddIntConstant(m, "UPDATE_H_SCROLL", SC_UPDATE_H_SCROLL);
951 
952 	PyModule_AddIntConstant(m, "MOD_INSERT_TEXT", SC_MOD_INSERTTEXT);
953 	PyModule_AddIntConstant(m, "MOD_DELETE_TEXT", SC_MOD_DELETETEXT);
954 	PyModule_AddIntConstant(m, "MOD_CHANGE_STYLE", SC_MOD_CHANGESTYLE);
955 	PyModule_AddIntConstant(m, "MOD_CHANGE_FOLD", SC_MOD_CHANGEFOLD);
956 	PyModule_AddIntConstant(m, "PERFORMED_USER", SC_PERFORMED_USER);
957 	PyModule_AddIntConstant(m, "PERFORMED_UNDO", SC_PERFORMED_UNDO);
958 	PyModule_AddIntConstant(m, "PERFORMED_REDO", SC_PERFORMED_REDO);
959 	PyModule_AddIntConstant(m, "MULTI_STEP_UNDO_REDO", SC_MULTISTEPUNDOREDO);
960 	PyModule_AddIntConstant(m, "LAST_STEP_IN_UNDO_REDO", SC_LASTSTEPINUNDOREDO);
961 	PyModule_AddIntConstant(m, "MOD_CHANGE_MARKER", SC_MOD_CHANGEMARKER);
962 	PyModule_AddIntConstant(m, "MOD_BEFORE_INSERT", SC_MOD_BEFOREINSERT);
963 	PyModule_AddIntConstant(m, "MOD_BEFORE_DELETE", SC_MOD_BEFOREDELETE);
964 	PyModule_AddIntConstant(m, "MOD_CHANGE_INDICATOR", SC_MOD_CHANGEINDICATOR);
965 	PyModule_AddIntConstant(m, "MOD_CHANGE_LINE_STATE", SC_MOD_CHANGELINESTATE);
966 	PyModule_AddIntConstant(m, "MOD_LEXER_STATE", SC_MOD_LEXERSTATE);
967 	PyModule_AddIntConstant(m, "MOD_CHANGE_MARGIN", SC_MOD_CHANGEMARGIN);
968 	PyModule_AddIntConstant(m, "MOD_CHANGE_ANNOTATION", SC_MOD_CHANGEANNOTATION);
969 	PyModule_AddIntConstant(m, "MULTILINE_UNDO_REDO", SC_MULTILINEUNDOREDO);
970 	PyModule_AddIntConstant(m, "START_ACTION", SC_STARTACTION);
971 	PyModule_AddIntConstant(m, "MOD_CONTAINER", SC_MOD_CONTAINER);
972 	PyModule_AddIntConstant(m, "MOD_EVENT_MASK_ALL", SC_MODEVENTMASKALL);
973 
974 	PyModule_AddIntConstant(m, "STYLE_NEEDED", SCN_STYLENEEDED);
975 	PyModule_AddIntConstant(m, "CHAR_ADDED", SCN_CHARADDED);
976 	PyModule_AddIntConstant(m, "SAVE_POINT_REACHED", SCN_SAVEPOINTREACHED);
977 	PyModule_AddIntConstant(m, "SAVE_POINT_LEFT", SCN_SAVEPOINTLEFT);
978 	PyModule_AddIntConstant(m, "MODIFY_ATTEMPT_RO", SCN_MODIFYATTEMPTRO);
979 	PyModule_AddIntConstant(m, "KEY", SCN_KEY);
980 	PyModule_AddIntConstant(m, "DOUBLE_CLICK", SCN_DOUBLECLICK);
981 	PyModule_AddIntConstant(m, "UPDATE_UI", SCN_UPDATEUI);
982 	PyModule_AddIntConstant(m, "MODIFIED", SCN_MODIFIED);
983 	PyModule_AddIntConstant(m, "MACRO_RECORD", SCN_MACRORECORD);
984 	PyModule_AddIntConstant(m, "MARGIN_CLICK", SCN_MARGINCLICK);
985 	PyModule_AddIntConstant(m, "NEED_SHOWN", SCN_NEEDSHOWN);
986 	PyModule_AddIntConstant(m, "PAINTED", SCN_PAINTED);
987 	PyModule_AddIntConstant(m, "USER_LIST_SELECTION", SCN_USERLISTSELECTION);
988 	PyModule_AddIntConstant(m, "URI_DROPPED", SCN_URIDROPPED);
989 	PyModule_AddIntConstant(m, "DWELL_START", SCN_DWELLSTART);
990 	PyModule_AddIntConstant(m, "DWELL_END", SCN_DWELLEND);
991 	PyModule_AddIntConstant(m, "ZOOM", SCN_ZOOM);
992 	PyModule_AddIntConstant(m, "HOT_SPOT_CLICK", SCN_HOTSPOTCLICK);
993 	PyModule_AddIntConstant(m, "HOT_SPOT_DOUBLE_CLICK", SCN_HOTSPOTDOUBLECLICK);
994 	PyModule_AddIntConstant(m, "CALL_TIP_CLICK", SCN_CALLTIPCLICK);
995 	PyModule_AddIntConstant(m, "AUTO_C_SELECTION", SCN_AUTOCSELECTION);
996 	PyModule_AddIntConstant(m, "INDICATOR_CLICK", SCN_INDICATORCLICK);
997 	PyModule_AddIntConstant(m, "INDICATOR_RELEASE", SCN_INDICATORRELEASE);
998 	PyModule_AddIntConstant(m, "AUTOC_CANCELLED", SCN_AUTOCCANCELLED);
999 	PyModule_AddIntConstant(m, "AUTOC_CHAR_DELETED", SCN_AUTOCCHARDELETED);
1000 	PyModule_AddIntConstant(m, "HOT_SPOT_RELEASE_CLICK", SCN_HOTSPOTRELEASECLICK);
1001 }
1002 
1003 
Scintilla_create_new_from_scintilla(ScintillaObject * sci)1004 Scintilla *Scintilla_create_new_from_scintilla(ScintillaObject *sci)
1005 {
1006 	Scintilla *self;
1007 	self = (Scintilla *) PyObject_CallObject((PyObject *) &ScintillaType, NULL);
1008 	self->sci = sci;
1009 	return self;
1010 }
1011