info bars: Explicitely move focus back to the editor when closing
[geany-mirror.git] / src / sciwrappers.c
blobc76f491e1c54d187dc5837acea7b14bddeb0256b
1 /*
2 * sciwrappers.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /** @file sciwrappers.h
23 * Wrapper functions for the Scintilla editor widget @c SCI_* messages.
24 * You should also check the http://scintilla.org documentation, as it is more detailed.
26 * To get Scintilla notifications, use the
27 * @link pluginsignals.c @c "editor-notify" signal @endlink.
29 * @note These functions were originally from the cssed project
30 * (http://cssed.sf.net, thanks).
31 * @see scintilla_send_message().
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
38 #include "sciwrappers.h"
40 #include "utils.h"
42 #include <string.h>
45 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
47 /* line numbers visibility */
48 void sci_set_line_numbers(ScintillaObject *sci, gboolean set)
50 if (set)
52 gchar tmp_str[15];
53 gint len = (gint) SSM(sci, SCI_GETLINECOUNT, 0, 0);
54 gint width;
56 g_snprintf(tmp_str, 15, "_%d", len);
57 width = sci_text_width(sci, STYLE_LINENUMBER, tmp_str);
58 SSM(sci, SCI_SETMARGINWIDTHN, 0, width);
59 SSM(sci, SCI_SETMARGINSENSITIVEN, 0, FALSE); /* use default behaviour */
61 else
63 SSM(sci, SCI_SETMARGINWIDTHN, 0, 0);
68 void sci_set_mark_long_lines(ScintillaObject *sci, gint type, gint column, const gchar *colour)
70 glong colour_val = utils_parse_color_to_bgr(colour); /* Scintilla uses a "long" value */
72 if (column == 0)
73 type = 2;
74 switch (type)
76 case 0:
78 SSM(sci, SCI_SETEDGEMODE, EDGE_LINE, 0);
79 break;
81 case 1:
83 SSM(sci, SCI_SETEDGEMODE, EDGE_BACKGROUND, 0);
84 break;
86 case 2:
88 SSM(sci, SCI_SETEDGEMODE, EDGE_NONE, 0);
89 return;
92 SSM(sci, SCI_SETEDGECOLUMN, (uptr_t) column, 0);
93 SSM(sci, SCI_SETEDGECOLOUR, (uptr_t) colour_val, 0);
97 /* symbol margin visibility */
98 void sci_set_symbol_margin(ScintillaObject *sci, gboolean set)
100 if (set)
102 SSM(sci, SCI_SETMARGINWIDTHN, 1, 16);
103 SSM(sci, SCI_SETMARGINSENSITIVEN, 1, TRUE);
105 else
107 SSM(sci, SCI_SETMARGINWIDTHN, 1, 0);
108 SSM(sci, SCI_SETMARGINSENSITIVEN, 1, FALSE);
113 /* folding margin visibility */
114 void sci_set_folding_margin_visible(ScintillaObject *sci, gboolean set)
116 if (set)
118 SSM(sci, SCI_SETMARGINWIDTHN, 2, 12);
119 SSM(sci, SCI_SETMARGINSENSITIVEN, 2, TRUE);
121 else
123 SSM(sci, SCI_SETMARGINSENSITIVEN, 2, FALSE);
124 SSM(sci, SCI_SETMARGINWIDTHN, 2, 0);
129 /* end of lines */
130 void sci_set_visible_eols(ScintillaObject *sci, gboolean set)
132 SSM(sci, SCI_SETVIEWEOL, set != FALSE, 0);
136 void sci_set_visible_white_spaces(ScintillaObject *sci, gboolean set)
138 if (set)
139 SSM(sci, SCI_SETVIEWWS, SCWS_VISIBLEALWAYS, 0);
140 else
141 SSM(sci, SCI_SETVIEWWS, SCWS_INVISIBLE, 0);
145 void sci_set_lines_wrapped(ScintillaObject *sci, gboolean set)
147 if (set)
148 SSM(sci, SCI_SETWRAPMODE, SC_WRAP_WORD, 0);
149 else
150 SSM(sci, SCI_SETWRAPMODE, SC_WRAP_NONE, 0);
154 gint sci_get_eol_mode(ScintillaObject *sci)
156 return (gint) SSM(sci, SCI_GETEOLMODE, 0, 0);
160 void sci_set_eol_mode(ScintillaObject *sci, gint eolmode)
162 SSM(sci, SCI_SETEOLMODE, (uptr_t) eolmode, 0);
166 void sci_convert_eols(ScintillaObject *sci, gint eolmode)
168 SSM(sci, SCI_CONVERTEOLS, (uptr_t) eolmode, 0);
172 void sci_add_text(ScintillaObject *sci, const gchar *text)
174 if (text != NULL)
175 { /* if null text is passed scintilla will segfault */
176 SSM(sci, SCI_ADDTEXT, strlen(text), (sptr_t) text);
181 /** Sets all text.
182 * @param sci Scintilla widget.
183 * @param text Text. */
184 void sci_set_text(ScintillaObject *sci, const gchar *text)
186 if( text != NULL ){ /* if null text is passed to scintilla will segfault */
187 SSM(sci, SCI_SETTEXT, 0, (sptr_t) text);
192 gboolean sci_can_undo(ScintillaObject *sci)
194 return SSM(sci, SCI_CANUNDO, 0, 0) != FALSE;
198 gboolean sci_can_redo(ScintillaObject *sci)
200 return SSM(sci, SCI_CANREDO, 0, 0) != FALSE;
204 void sci_undo(ScintillaObject *sci)
206 if (sci_can_undo(sci))
207 SSM(sci, SCI_UNDO, 0, 0);
211 void sci_redo(ScintillaObject *sci)
213 if (sci_can_redo(sci))
214 SSM(sci, SCI_REDO, 0, 0);
218 /** Begins grouping a set of edits together as one Undo action.
219 * You must call sci_end_undo_action() after making your edits.
220 * @param sci Scintilla @c GtkWidget. */
221 void sci_start_undo_action(ScintillaObject *sci)
223 SSM(sci, SCI_BEGINUNDOACTION, 0, 0);
227 /** Ends grouping a set of edits together as one Undo action.
228 * @param sci Scintilla @c GtkWidget.
229 * @see sci_start_undo_action(). */
230 void sci_end_undo_action(ScintillaObject *sci)
232 SSM(sci, SCI_ENDUNDOACTION, 0, 0);
236 void sci_set_undo_collection(ScintillaObject *sci, gboolean set)
238 SSM(sci, SCI_SETUNDOCOLLECTION, set != FALSE, 0);
242 void sci_empty_undo_buffer(ScintillaObject *sci)
244 SSM(sci, SCI_EMPTYUNDOBUFFER, 0, 0);
248 gboolean sci_is_modified(ScintillaObject *sci)
250 return (SSM(sci, SCI_GETMODIFY, 0, 0) != 0);
254 void sci_zoom_in(ScintillaObject *sci)
256 SSM(sci, SCI_ZOOMIN, 0, 0);
260 void sci_zoom_out(ScintillaObject *sci)
262 SSM(sci, SCI_ZOOMOUT, 0, 0);
266 void sci_zoom_off(ScintillaObject *sci)
268 SSM(sci, SCI_SETZOOM, 0, 0);
272 gint sci_get_zoom(ScintillaObject *sci)
274 return (gint) SSM(sci, SCI_GETZOOM, 0, 0);
278 /** Sets a line marker.
279 * @param sci Scintilla widget.
280 * @param line_number Line number.
281 * @param marker Marker number. */
282 void sci_set_marker_at_line(ScintillaObject *sci, gint line_number, gint marker)
284 SSM(sci, SCI_MARKERADD, (uptr_t) line_number, marker);
288 /** Deletes a line marker.
289 * @param sci Scintilla widget.
290 * @param line_number Line number.
291 * @param marker Marker number. */
292 void sci_delete_marker_at_line(ScintillaObject *sci, gint line_number, gint marker)
294 SSM(sci, SCI_MARKERDELETE, (uptr_t) line_number, marker);
298 /** Checks if a line has a marker set.
299 * @param sci Scintilla widget.
300 * @param line Line number.
301 * @param marker Marker number.
302 * @return Whether it's set. */
303 gboolean sci_is_marker_set_at_line(ScintillaObject *sci, gint line, gint marker)
305 gint state;
307 state = (gint) SSM(sci, SCI_MARKERGET, (uptr_t) line, 0);
308 return (state & (1 << marker));
312 void sci_toggle_marker_at_line(ScintillaObject *sci, gint line, gint marker)
314 gboolean set = sci_is_marker_set_at_line(sci, line, marker);
316 if (!set)
317 sci_set_marker_at_line(sci, line, marker);
318 else
319 sci_delete_marker_at_line(sci, line, marker);
323 /* Returns the line number of the next marker that matches marker_mask, or -1.
324 * marker_mask is a bitor of 1 << marker_index. (See MarkerHandleSet::MarkValue()).
325 * Note: If there is a marker on the line, it returns the same line. */
326 gint sci_marker_next(ScintillaObject *sci, gint line, gint marker_mask, gboolean wrap)
328 gint marker_line;
330 marker_line = (gint) SSM(sci, SCI_MARKERNEXT, (uptr_t) line, marker_mask);
331 if (wrap && marker_line == -1)
332 marker_line = (gint) SSM(sci, SCI_MARKERNEXT, 0, marker_mask);
333 return marker_line;
337 /* Returns the line number of the previous marker that matches marker_mask, or -1.
338 * marker_mask is a bitor of 1 << marker_index. (See MarkerHandleSet::MarkValue()).
339 * Note: If there is a marker on the line, it returns the same line. */
340 gint sci_marker_previous(ScintillaObject *sci, gint line, gint marker_mask, gboolean wrap)
342 gint marker_line;
344 marker_line = (gint) SSM(sci, SCI_MARKERPREVIOUS, (uptr_t) line, marker_mask);
345 if (wrap && marker_line == -1)
347 gint len = sci_get_length(sci);
348 gint last_line = sci_get_line_from_position(sci, len - 1);
350 marker_line = (gint) SSM(sci, SCI_MARKERPREVIOUS, (uptr_t) last_line, marker_mask);
352 return marker_line;
356 /** Gets the line number from @a position.
357 * @param sci Scintilla widget.
358 * @param position Position.
359 * @return The line. */
360 gint sci_get_line_from_position(ScintillaObject *sci, gint position)
362 return (gint) SSM(sci, SCI_LINEFROMPOSITION, (uptr_t) position, 0);
366 /** Gets the column number relative to the start of the line that @a position is on.
367 * @param sci Scintilla widget.
368 * @param position Position.
369 * @return The column. */
370 gint sci_get_col_from_position(ScintillaObject *sci, gint position)
372 return (gint) SSM(sci, SCI_GETCOLUMN, (uptr_t) position, 0);
376 gint sci_get_position_from_col(ScintillaObject *sci, gint line, gint col)
378 return (gint) SSM(sci, SCI_FINDCOLUMN, line, col);
382 /** Gets the position for the start of @a line.
383 * @param sci Scintilla widget.
384 * @param line Line.
385 * @return Position. */
386 gint sci_get_position_from_line(ScintillaObject *sci, gint line)
388 return (gint) SSM(sci, SCI_POSITIONFROMLINE, (uptr_t) line, 0);
392 /** Gets the cursor position.
393 * @param sci Scintilla widget.
394 * @return Position. */
395 gint sci_get_current_position(ScintillaObject *sci)
397 return (gint) SSM(sci, SCI_GETCURRENTPOS, 0, 0);
401 /** Sets the cursor position.
402 * @param sci Scintilla widget.
403 * @param position Position.
404 * @param scroll_to_caret Whether to scroll the cursor in view. */
405 void sci_set_current_position(ScintillaObject *sci, gint position, gboolean scroll_to_caret)
407 if (scroll_to_caret)
408 SSM(sci, SCI_GOTOPOS, (uptr_t) position, 0);
409 else
411 SSM(sci, SCI_SETCURRENTPOS, (uptr_t) position, 0);
412 SSM(sci, SCI_SETANCHOR, (uptr_t) position, 0); /* to avoid creation of a selection */
414 SSM(sci, SCI_CHOOSECARETX, 0, 0);
418 /* Set the cursor line without scrolling the view.
419 * Use sci_goto_line() to also scroll. */
420 void sci_set_current_line(ScintillaObject *sci, gint line)
422 gint pos = sci_get_position_from_line(sci, line);
423 sci_set_current_position(sci, pos, FALSE);
427 /** Gets the total number of lines.
428 * @param sci Scintilla widget.
429 * @return The line count. */
430 gint sci_get_line_count(ScintillaObject *sci)
432 return (gint) SSM(sci, SCI_GETLINECOUNT, 0, 0);
436 /** Sets the selection start position.
437 * @param sci Scintilla widget.
438 * @param position Position. */
439 void sci_set_selection_start(ScintillaObject *sci, gint position)
441 SSM(sci, SCI_SETSELECTIONSTART, (uptr_t) position, 0);
445 /** Sets the selection end position.
446 * @param sci Scintilla widget.
447 * @param position Position. */
448 void sci_set_selection_end(ScintillaObject *sci, gint position)
450 SSM(sci, SCI_SETSELECTIONEND, (uptr_t) position, 0);
454 void sci_set_selection(ScintillaObject *sci, gint anchorPos, gint currentPos)
456 SSM(sci, SCI_SETSEL, (uptr_t) anchorPos, currentPos);
460 /** Gets the position at the end of a line
461 * @param sci Scintilla widget.
462 * @param line Line.
463 * @return The position at the end of the line. */
464 gint sci_get_line_end_position(ScintillaObject *sci, gint line)
466 return (gint) SSM(sci, SCI_GETLINEENDPOSITION, (uptr_t) line, 0);
470 void sci_cut(ScintillaObject *sci)
472 SSM(sci, SCI_CUT, 0, 0);
476 void sci_copy(ScintillaObject *sci)
478 SSM(sci, SCI_COPY, 0, 0);
482 void sci_paste(ScintillaObject *sci)
484 SSM(sci, SCI_PASTE, 0, 0);
488 void sci_clear(ScintillaObject *sci)
490 SSM(sci, SCI_CLEAR, 0, 0);
494 /** Gets the selection start position.
495 * @param sci Scintilla widget.
496 * @return Position. */
497 gint sci_get_selection_start(ScintillaObject *sci)
499 return (gint) SSM(sci, SCI_GETSELECTIONSTART, 0, 0);
503 /** Gets the selection end position.
504 * @param sci Scintilla widget.
505 * @return Position. */
506 gint sci_get_selection_end(ScintillaObject *sci)
508 return (gint) SSM(sci, SCI_GETSELECTIONEND, 0, 0);
512 /** Replaces selection.
513 * @param sci Scintilla widget.
514 * @param text Text. */
515 void sci_replace_sel(ScintillaObject *sci, const gchar *text)
517 SSM(sci, SCI_REPLACESEL, 0, (sptr_t) text);
521 /** Gets the length of all text.
522 * @param sci Scintilla widget.
523 * @return Length. */
524 gint sci_get_length(ScintillaObject *sci)
526 return (gint) SSM(sci, SCI_GETLENGTH, 0, 0);
530 /** Gets the currently used lexer
531 * @param sci Scintilla widget.
532 * @returns The lexer ID
534 gint sci_get_lexer(ScintillaObject *sci)
536 return (gint) SSM(sci, SCI_GETLEXER, 0, 0);
540 void sci_set_lexer(ScintillaObject *sci, guint lexer_id)
542 gint old = sci_get_lexer(sci);
544 SSM(sci, SCI_SETLEXER, lexer_id, 0);
546 if (old != (gint)lexer_id)
547 SSM(sci, SCI_CLEARDOCUMENTSTYLE, 0, 0);
551 /** Gets line length.
552 * @param sci Scintilla widget.
553 * @param line Line number.
554 * @return Length. */
555 gint sci_get_line_length(ScintillaObject *sci, gint line)
557 return (gint) SSM(sci, SCI_LINELENGTH, (uptr_t) line, 0);
561 /* safe way to read Scintilla string into new memory.
562 * works with any string buffer messages that follow the Windows message convention. */
563 gchar *sci_get_string(ScintillaObject *sci, guint msg, gulong wParam)
565 gint size = (gint) SSM(sci, msg, wParam, 0);
566 gchar *str = g_malloc(size + 1);
568 SSM(sci, msg, wParam, (sptr_t) str);
569 str[size] = '\0'; /* ensure termination, needed for SCI_GETLINE */
570 return str;
574 /** Gets line contents.
575 * @param sci Scintilla widget.
576 * @param line_num Line number.
577 * @return A @c NULL-terminated copy of the line text. */
578 gchar *sci_get_line(ScintillaObject *sci, gint line_num)
580 return sci_get_string(sci, SCI_GETLINE, (gulong) line_num);
584 /** Gets all text.
585 * @deprecated sci_get_text is deprecated and should not be used in newly-written code.
586 * Use sci_get_contents() instead.
588 * @param sci Scintilla widget.
589 * @param len Length of @a text buffer, usually sci_get_length() + 1.
590 * @param text Text buffer; must be allocated @a len + 1 bytes for null-termination. */
591 void sci_get_text(ScintillaObject *sci, gint len, gchar *text)
593 SSM(sci, SCI_GETTEXT, (uptr_t) len, (sptr_t) text);
597 /** Allocates and fills a buffer with text from the start of the document.
598 * @param sci Scintilla widget.
599 * @param buffer_len Buffer length to allocate, including the terminating
600 * null char, e.g. sci_get_length() + 1. Alternatively use @c -1 to get all
601 * text (since Geany 1.23).
602 * @return A copy of the text. Should be freed when no longer needed.
604 * @since 1.23 (0.17)
606 gchar *sci_get_contents(ScintillaObject *sci, gint buffer_len)
608 gchar *text;
610 if (buffer_len < 0)
611 buffer_len = sci_get_length(sci) + 1;
613 text = g_malloc(buffer_len);
614 SSM(sci, SCI_GETTEXT, (uptr_t) buffer_len, (sptr_t) text);
615 return text;
619 /** Gets selected text.
620 * @deprecated sci_get_selected_text is deprecated and should not be used in newly-written code.
621 * Use sci_get_selection_contents() instead.
623 * @param sci Scintilla widget.
624 * @param text Text buffer; must be allocated sci_get_selected_text_length() + 1 bytes
625 * for null-termination. */
626 void sci_get_selected_text(ScintillaObject *sci, gchar *text)
628 SSM(sci, SCI_GETSELTEXT, 0, (sptr_t) text);
632 /** Gets selected text.
633 * @param sci Scintilla widget.
635 * @return The selected text. Should be freed when no longer needed.
637 * @since 0.17
639 gchar *sci_get_selection_contents(ScintillaObject *sci)
641 return sci_get_string(sci, SCI_GETSELTEXT, 0);
645 /** Gets selected text length.
646 * @param sci Scintilla widget.
647 * @return Length. */
648 gint sci_get_selected_text_length(ScintillaObject *sci)
650 return (gint) SSM(sci, SCI_GETSELTEXT, 0, 0);
654 gint sci_get_position_from_xy(ScintillaObject *sci, gint x, gint y, gboolean nearby)
656 /* for nearby return -1 if there is no character near to the x,y point. */
657 return (gint) SSM(sci, (nearby) ? SCI_POSITIONFROMPOINTCLOSE : SCI_POSITIONFROMPOINT, (uptr_t) x, y);
661 /** Checks if a line is visible (folding may have hidden it).
662 * @param sci Scintilla widget.
663 * @param line Line number.
664 * @return Whether @a line will be drawn on the screen. */
665 gboolean sci_get_line_is_visible(ScintillaObject *sci, gint line)
667 return SSM(sci, SCI_GETLINEVISIBLE, (uptr_t) line, 0) != FALSE;
671 /** Makes @a line visible (folding may have hidden it).
672 * @param sci Scintilla widget.
673 * @param line Line number. */
674 void sci_ensure_line_is_visible(ScintillaObject *sci, gint line)
676 SSM(sci, SCI_ENSUREVISIBLE, (uptr_t) line, 0);
680 gint sci_get_fold_level(ScintillaObject *sci, gint line)
682 return (gint) SSM(sci, SCI_GETFOLDLEVEL, (uptr_t) line, 0);
686 /* Get the line number of the fold point before start_line, or -1 if there isn't one */
687 gint sci_get_fold_parent(ScintillaObject *sci, gint start_line)
689 return (gint) SSM(sci, SCI_GETFOLDPARENT, (uptr_t) start_line, 0);
693 void sci_toggle_fold(ScintillaObject *sci, gint line)
695 SSM(sci, SCI_TOGGLEFOLD, (uptr_t) line, 0);
699 gboolean sci_get_fold_expanded(ScintillaObject *sci, gint line)
701 return SSM(sci, SCI_GETFOLDEXPANDED, (uptr_t) line, 0) != FALSE;
705 void sci_colourise(ScintillaObject *sci, gint start, gint end)
707 SSM(sci, SCI_COLOURISE, (uptr_t) start, end);
711 void sci_clear_all(ScintillaObject *sci)
713 SSM(sci, SCI_CLEARALL, 0, 0);
717 gint sci_get_end_styled(ScintillaObject *sci)
719 return (gint) SSM(sci, SCI_GETENDSTYLED, 0, 0);
723 void sci_set_tab_width(ScintillaObject *sci, gint width)
725 SSM(sci, SCI_SETTABWIDTH, (uptr_t) width, 0);
729 /** Gets display tab width (this is not indent width, see GeanyIndentPrefs).
730 * @param sci Scintilla widget.
731 * @return Width.
733 * @since 0.15
735 gint sci_get_tab_width(ScintillaObject *sci)
737 return (gint) SSM(sci, SCI_GETTABWIDTH, 0, 0);
741 /** Gets a character.
742 * @param sci Scintilla widget.
743 * @param pos Position.
744 * @return Char. */
745 gchar sci_get_char_at(ScintillaObject *sci, gint pos)
747 return (gchar) SSM(sci, SCI_GETCHARAT, (uptr_t) pos, 0);
751 void sci_set_savepoint(ScintillaObject *sci)
753 SSM(sci, SCI_SETSAVEPOINT, 0, 0);
757 void sci_set_indentation_guides(ScintillaObject *sci, gint mode)
759 SSM(sci, SCI_SETINDENTATIONGUIDES, (uptr_t) mode, 0);
763 void sci_use_popup(ScintillaObject *sci, gboolean enable)
765 SSM(sci, SCI_USEPOPUP, enable != FALSE, 0);
769 /** Checks if there's a selection.
770 * @param sci Scintilla widget.
771 * @return Whether a selection is present.
773 * @since 0.15
775 gboolean sci_has_selection(ScintillaObject *sci)
777 if (SSM(sci, SCI_GETSELECTIONEND, 0, 0) - SSM(sci, SCI_GETSELECTIONSTART, 0, 0))
778 return TRUE;
779 else
780 return FALSE;
784 void sci_goto_pos(ScintillaObject *sci, gint pos, gboolean unfold)
786 if (unfold) SSM(sci, SCI_ENSUREVISIBLE, (uptr_t) SSM(sci, SCI_LINEFROMPOSITION, (uptr_t) pos, 0), 0);
787 SSM(sci, SCI_GOTOPOS, (uptr_t) pos, 0);
791 void sci_set_search_anchor(ScintillaObject *sci)
793 SSM(sci, SCI_SEARCHANCHOR, 0, 0);
797 /* removes a selection if pos < 0 */
798 void sci_set_anchor(ScintillaObject *sci, gint pos)
800 if (pos < 0)
801 pos = sci_get_current_position(sci);
803 SSM(sci, SCI_SETANCHOR, (uptr_t) pos, 0);
807 /** Scrolls the cursor in view.
808 * @param sci Scintilla widget. */
809 void sci_scroll_caret(ScintillaObject *sci)
811 SSM(sci, SCI_SCROLLCARET, 0, 0);
815 void sci_scroll_lines(ScintillaObject *sci, gint lines)
817 SSM(sci, SCI_LINESCROLL, 0, lines);
821 void sci_scroll_columns(ScintillaObject *sci, gint columns)
823 SSM(sci, SCI_LINESCROLL, (uptr_t) columns, 0);
827 gint sci_search_next(ScintillaObject *sci, gint flags, const gchar *text)
829 /* FIXME: SCI_SEACHNEXT() actually returns long */
830 return (gint) SSM(sci, SCI_SEARCHNEXT, (uptr_t) flags, (sptr_t) text);
834 gint sci_search_prev(ScintillaObject *sci, gint flags, const gchar *text)
836 /* FIXME: SCI_SEACHPREV() actually returns long */
837 return (gint) SSM(sci, SCI_SEARCHPREV, (uptr_t) flags, (sptr_t) text);
841 /** Finds text in the document.
842 * The @a ttf argument should be a pointer to a Sci_TextToFind structure which contains
843 * the text to find and the range in which the text should be searched.
845 * Please refer to the Scintilla documentation for a more detailed description.
847 * @param sci Scintilla widget.
848 * @param flags Bitmask of Scintilla search flags (@c SCFIND_*, see Scintilla documentation).
849 * @param ttf Pointer to a TextToFind structure which contains the text to find and the range.
850 * @return The position of the start of the found text if it succeeds, otherwise @c -1.
851 * The @c chrgText.cpMin and @c chrgText.cpMax members of @c TextToFind are filled in
852 * with the start and end positions of the found text.
854 gint sci_find_text(ScintillaObject *sci, gint flags, struct Sci_TextToFind *ttf)
856 return (gint) SSM(sci, SCI_FINDTEXT, (uptr_t) flags, (sptr_t) ttf);
860 /** Sets the font for a particular style.
861 * @param sci Scintilla widget.
862 * @param style The style.
863 * @param font The font name.
864 * @param size The font size. */
865 void sci_set_font(ScintillaObject *sci, gint style, const gchar *font, gint size)
867 SSM(sci, SCI_STYLESETFONT, (uptr_t) style, (sptr_t) font);
868 SSM(sci, SCI_STYLESETSIZE, (uptr_t) style, size);
872 /** Jumps to the specified line in the document.
873 * If @a unfold is set and @a line is hidden by a fold, it is unfolded
874 * first to ensure it is visible.
875 * @param sci Scintilla widget.
876 * @param line Line.
877 * @param unfold Whether to unfold first.
879 void sci_goto_line(ScintillaObject *sci, gint line, gboolean unfold)
881 if (unfold) SSM(sci, SCI_ENSUREVISIBLE, (uptr_t) line, 0);
882 SSM(sci, SCI_GOTOLINE, (uptr_t) line, 0);
886 void sci_marker_delete_all(ScintillaObject *sci, gint marker)
888 SSM(sci, SCI_MARKERDELETEALL, (uptr_t) marker, 0);
892 /** Gets style ID at @a position.
893 * @param sci Scintilla widget.
894 * @param position Position.
895 * @return Style ID. */
896 gint sci_get_style_at(ScintillaObject *sci, gint position)
898 return (gint) SSM(sci, SCI_GETSTYLEAT, (uptr_t) position, 0);
902 void sci_set_codepage(ScintillaObject *sci, gint cp)
904 g_return_if_fail(cp == 0 || cp == SC_CP_UTF8);
905 SSM(sci, SCI_SETCODEPAGE, (uptr_t) cp, 0);
909 void sci_assign_cmdkey(ScintillaObject *sci, gint key, gint command)
911 SSM(sci, SCI_ASSIGNCMDKEY, (uptr_t) key, command);
915 void sci_clear_cmdkey(ScintillaObject *sci, gint key)
917 SSM(sci, SCI_CLEARCMDKEY, (uptr_t) key, 0);
921 /** Gets text between @a start and @a end.
922 * @deprecated sci_get_text_range is deprecated and should not be used in newly-written code.
923 * Use sci_get_contents_range() instead.
925 * @param sci Scintilla widget.
926 * @param start Start.
927 * @param end End.
928 * @param text Text will be zero terminated and must be allocated (end - start + 1) bytes. */
929 void sci_get_text_range(ScintillaObject *sci, gint start, gint end, gchar *text)
931 struct Sci_TextRange tr;
932 tr.chrg.cpMin = start;
933 tr.chrg.cpMax = end;
934 tr.lpstrText = text;
935 SSM(sci, SCI_GETTEXTRANGE, 0, (long) &tr);
939 /** Gets text between @a start and @a end.
940 * @param sci Scintilla widget.
941 * @param start Start position.
942 * @param end End position.
943 * @return The text inside the given range. Should be freed when no longer needed.
945 * @since 0.17
947 gchar *sci_get_contents_range(ScintillaObject *sci, gint start, gint end)
949 gchar *text;
951 g_return_val_if_fail(start < end, NULL);
953 text = g_malloc((gsize) (end - start) + 1);
954 sci_get_text_range(sci, start, end, text);
955 return text;
959 void sci_line_duplicate(ScintillaObject *sci)
961 SSM(sci, SCI_LINEDUPLICATE, 0, 0);
965 void sci_selection_duplicate(ScintillaObject *sci)
967 SSM(sci, SCI_SELECTIONDUPLICATE, 0, 0);
971 /** Inserts text.
972 * @param sci Scintilla widget.
973 * @param pos Position, or -1 for current.
974 * @param text Text. */
975 void sci_insert_text(ScintillaObject *sci, gint pos, const gchar *text)
977 SSM(sci, SCI_INSERTTEXT, (uptr_t) pos, (sptr_t) text);
981 void sci_target_from_selection(ScintillaObject *sci)
983 SSM(sci, SCI_TARGETFROMSELECTION, 0, 0);
987 void sci_set_target_start(ScintillaObject *sci, gint start)
989 SSM(sci, SCI_SETTARGETSTART, (uptr_t) start, 0);
993 void sci_set_target_end(ScintillaObject *sci, gint end)
995 SSM(sci, SCI_SETTARGETEND, (uptr_t) end, 0);
999 gint sci_replace_target(ScintillaObject *sci, const gchar *text, gboolean regex)
1001 return (gint) SSM(sci, (regex) ? SCI_REPLACETARGETRE : SCI_REPLACETARGET, (uptr_t) -1, (sptr_t) text);
1005 void sci_set_keywords(ScintillaObject *sci, guint k, const gchar *text)
1007 SSM(sci, SCI_SETKEYWORDS, k, (sptr_t) text);
1011 void sci_set_readonly(ScintillaObject *sci, gboolean readonly)
1013 SSM(sci, SCI_SETREADONLY, readonly != FALSE, 0);
1017 /** Sends Scintilla commands without any parameters.
1018 * @param sci The Scintilla @c GtkWidget.
1019 * @param cmd @c SCI_COMMAND.
1020 * @see http://scintilla.org for the documentation.
1022 * @since 0.16
1024 void sci_send_command(ScintillaObject *sci, gint cmd)
1026 SSM(sci, cmd, 0, 0);
1030 /** Gets current line number.
1031 * @param sci Scintilla widget.
1032 * @return Line number. */
1033 gint sci_get_current_line(ScintillaObject *sci)
1035 return (gint) SSM(sci, SCI_LINEFROMPOSITION, (uptr_t) SSM(sci, SCI_GETCURRENTPOS, 0, 0), 0);
1039 /* Get number of lines partially or fully selected.
1040 * Returns 1 if there is a partial selection on the same line.
1041 * Returns 2 if a whole line is selected including the line break char(s). */
1042 gint sci_get_lines_selected(ScintillaObject *sci)
1044 gint start = (gint) SSM(sci, SCI_GETSELECTIONSTART, 0, 0);
1045 gint end = (gint) SSM(sci, SCI_GETSELECTIONEND, 0, 0);
1046 gint line_start;
1047 gint line_end;
1049 if (start == end)
1050 return 0; /* no selection */
1052 line_start = (gint) SSM(sci, SCI_LINEFROMPOSITION, (uptr_t) start, 0);
1053 line_end = (gint) SSM(sci, SCI_LINEFROMPOSITION, (uptr_t) end, 0);
1055 return line_end - line_start + 1;
1059 gint sci_get_first_visible_line(ScintillaObject *sci)
1061 return (gint) SSM(sci, SCI_GETFIRSTVISIBLELINE, 0, 0);
1066 * Sets the current indicator. This is necessary to define an indicator for a range of text or
1067 * clearing indicators for a range of text.
1069 * @param sci Scintilla widget.
1070 * @param indic The indicator number to set.
1072 * @see sci_indicator_clear
1074 * @since 0.16
1076 void sci_indicator_set(ScintillaObject *sci, gint indic)
1078 SSM(sci, SCI_SETINDICATORCURRENT, (uptr_t) indic, 0);
1082 void sci_indicator_fill(ScintillaObject *sci, gint pos, gint len)
1084 SSM(sci, SCI_INDICATORFILLRANGE, (uptr_t) pos, len);
1089 * Clears the currently set indicator from a range of text.
1090 * Starting at @a pos, @a len characters long.
1091 * In order to make this function properly, you need to set the current indicator before with
1092 * @ref sci_indicator_set().
1094 * @param sci Scintilla widget.
1095 * @param pos Starting position.
1096 * @param len Length.
1098 * @since 0.16
1100 void sci_indicator_clear(ScintillaObject *sci, gint pos, gint len)
1102 SSM(sci, SCI_INDICATORCLEARRANGE, (uptr_t) pos, len);
1106 void sci_select_all(ScintillaObject *sci)
1108 SSM(sci, SCI_SELECTALL, 0, 0);
1112 gint sci_get_line_indent_position(ScintillaObject *sci, gint line)
1114 return (gint) SSM(sci, SCI_GETLINEINDENTPOSITION, (uptr_t) line, 0);
1118 void sci_set_autoc_max_height(ScintillaObject *sci, gint val)
1120 SSM(sci, SCI_AUTOCSETMAXHEIGHT, (uptr_t) val, 0);
1124 /** Finds a matching brace at @a pos.
1125 * @param sci Scintilla widget.
1126 * @param pos Position.
1127 * @return Matching brace position.
1129 * @since 0.15
1131 gint sci_find_matching_brace(ScintillaObject *sci, gint pos)
1133 return (gint) SSM(sci, SCI_BRACEMATCH, (uptr_t) pos, 0);
1137 gint sci_get_overtype(ScintillaObject *sci)
1139 return (gint) SSM(sci, SCI_GETOVERTYPE, 0, 0);
1143 void sci_set_tab_indents(ScintillaObject *sci, gboolean set)
1145 SSM(sci, SCI_SETTABINDENTS, set != FALSE, 0);
1149 void sci_set_use_tabs(ScintillaObject *sci, gboolean set)
1151 SSM(sci, SCI_SETUSETABS, set != FALSE, 0);
1155 gint sci_get_pos_at_line_sel_start(ScintillaObject *sci, gint line)
1157 return (gint) SSM(sci, SCI_GETLINESELSTARTPOSITION, (uptr_t) line, 0);
1161 gint sci_get_pos_at_line_sel_end(ScintillaObject *sci, gint line)
1163 return (gint) SSM(sci, SCI_GETLINESELENDPOSITION, (uptr_t) line, 0);
1167 /** Gets selection mode.
1168 * @param sci Scintilla widget.
1169 * @return Selection mode. */
1170 gint sci_get_selection_mode(ScintillaObject *sci)
1172 return (gint) SSM(sci, SCI_GETSELECTIONMODE, 0, 0);
1176 /** Sets selection mode.
1177 * @param sci Scintilla widget.
1178 * @param mode Mode. */
1179 void sci_set_selection_mode(ScintillaObject *sci, gint mode)
1181 SSM(sci, SCI_SETSELECTIONMODE, (uptr_t) mode, 0);
1185 void sci_set_scrollbar_mode(ScintillaObject *sci, gboolean visible)
1187 SSM(sci, SCI_SETHSCROLLBAR, visible != FALSE, 0);
1188 SSM(sci, SCI_SETVSCROLLBAR, visible != FALSE, 0);
1192 /** Sets the indentation of a line.
1193 * @param sci Scintilla widget.
1194 * @param line Line to indent.
1195 * @param indent Indentation width.
1197 * @since 0.19
1199 void sci_set_line_indentation(ScintillaObject *sci, gint line, gint indent)
1201 SSM(sci, SCI_SETLINEINDENTATION, (uptr_t) line, indent);
1205 /** Gets the indentation width of a line.
1206 * @param sci Scintilla widget.
1207 * @param line Line to get the indentation from.
1208 * @return Indentation width.
1210 * @since 0.19
1212 gint sci_get_line_indentation(ScintillaObject *sci, gint line)
1214 return (gint) SSM(sci, SCI_GETLINEINDENTATION, (uptr_t) line, 0);
1218 void sci_set_caret_policy_x(ScintillaObject *sci, gint policy, gint slop)
1220 SSM(sci, SCI_SETXCARETPOLICY, (uptr_t) policy, slop);
1224 void sci_set_caret_policy_y(ScintillaObject *sci, gint policy, gint slop)
1226 SSM(sci, SCI_SETYCARETPOLICY, (uptr_t) policy, slop);
1230 void sci_set_scroll_stop_at_last_line(ScintillaObject *sci, gboolean set)
1232 SSM(sci, SCI_SETENDATLASTLINE, set != FALSE, 0);
1236 void sci_cancel(ScintillaObject *sci)
1238 SSM(sci, SCI_CANCEL, 0, 0);
1242 gint sci_get_target_end(ScintillaObject *sci)
1244 return (gint) SSM(sci, SCI_GETTARGETEND, 0, 0);
1248 gint sci_get_position_after(ScintillaObject *sci, gint start)
1250 return (gint) SSM(sci, SCI_POSITIONAFTER, (uptr_t) start, 0);
1254 void sci_lines_join(ScintillaObject *sci)
1256 SSM(sci, SCI_LINESJOIN, 0, 0);
1260 gint sci_text_width(ScintillaObject *sci, gint styleNumber, const gchar *text)
1262 return (gint) SSM(sci, SCI_TEXTWIDTH, (uptr_t) styleNumber, (sptr_t) text);
1266 void sci_move_selected_lines_down(ScintillaObject *sci)
1268 SSM(sci, SCI_MOVESELECTEDLINESDOWN, 0, 0);
1272 void sci_move_selected_lines_up(ScintillaObject *sci)
1274 SSM(sci, SCI_MOVESELECTEDLINESUP, 0, 0);
1278 gint sci_word_start_position(ScintillaObject *sci, gint position, gboolean onlyWordCharacters)
1280 return SSM(sci, SCI_WORDSTARTPOSITION, position, onlyWordCharacters);
1284 gint sci_word_end_position(ScintillaObject *sci, gint position, gboolean onlyWordCharacters)
1286 return SSM(sci, SCI_WORDENDPOSITION, position, onlyWordCharacters);