static void insert_string(GeanyDocument *doc, const gchar *string) { if (doc != NULL) { gint pos = sci_get_current_position(doc->editor->sci); sci_insert_text(doc->editor->sci, pos, string); } }
static gchar * get_current_word() { gchar *txt; GeanyDocument *doc; gint pos; gint cstart, cend; gchar c; gint text_len; doc = document_get_current(); g_return_val_if_fail(doc != NULL && doc->file_name != NULL, NULL); text_len = sci_get_selected_text_length(doc->editor->sci); if (text_len > 1) { txt = g_malloc(text_len + 1); sci_get_selected_text(doc->editor->sci, txt); return txt; } pos = sci_get_current_position(doc->editor->sci); if (pos > 0) pos--; cstart = pos; c = sci_get_char_at(doc->editor->sci, cstart); if (!word_check_left(c)) return NULL; while (word_check_left(c)) { cstart--; if (cstart >= 0) c = sci_get_char_at(doc->editor->sci, cstart); else break; } cstart++; cend = pos; c = sci_get_char_at(doc->editor->sci, cend); while (word_check_right(c) && cend < sci_get_length(doc->editor->sci)) { cend++; c = sci_get_char_at(doc->editor->sci, cend); } if (cstart == cend) return NULL; txt = g_malloc0(cend - cstart + 1); sci_get_text_range(doc->editor->sci, cstart, cend, txt); return txt; }
void on_toggle_case1_activate(GtkMenuItem *menuitem, gpointer user_data) { GeanyDocument *doc = document_get_current(); ScintillaObject *sci; gchar *text; gboolean keep_sel = TRUE; g_return_if_fail(doc != NULL); sci = doc->editor->sci; if (! sci_has_selection(sci)) { keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_WORD); keep_sel = FALSE; } /* either we already had a selection or we created one for current word */ if (sci_has_selection(sci)) { gchar *result = NULL; gint cmd = SCI_LOWERCASE; gboolean rectsel = (gboolean) scintilla_send_message(sci, SCI_SELECTIONISRECTANGLE, 0, 0); text = sci_get_selection_contents(sci); if (utils_str_has_upper(text)) { if (rectsel) cmd = SCI_LOWERCASE; else result = g_utf8_strdown(text, -1); } else { if (rectsel) cmd = SCI_UPPERCASE; else result = g_utf8_strup(text, -1); } if (result != NULL) { sci_replace_sel(sci, result); g_free(result); if (keep_sel) sci_set_selection_start(sci, sci_get_current_position(sci) - strlen(text)); } else sci_send_command(sci, cmd); g_free(text); } }
void on_show_color_chooser1_activate(GtkMenuItem *menuitem, gpointer user_data) { gchar colour[9]; GeanyDocument *doc = document_get_current(); gint pos; g_return_if_fail(doc != NULL); pos = sci_get_current_position(doc->editor->sci); editor_find_current_word(doc->editor, pos, colour, sizeof colour, GEANY_WORDCHARS"#"); tools_color_chooser(colour); }
static void on_set_anchor_activate(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata) { ScintillaObject *sci = scintilla_get_current(); if (sci) { select_anchor = sci_get_current_position(sci); select_space = sci_get_cursor_space(sci); save_selection(sci); } }
static void send_complete(GeanyEditor* editor, int flag) { if (completion_framework == NULL) { return; } if (!is_completion_file_now()) { return; } int pos = get_completion_position(); if (pos == 0) { return; } // nothing to complete int line = sci_get_line_from_position(editor->sci, pos); int ls_pos = sci_get_position_from_line(editor->sci, line); int byte_line_len = pos - ls_pos; if (byte_line_len < 0) { return; } char* content = sci_get_contents(editor->sci, sci_get_length(editor->sci) + 1 + 1); content[sci_get_length(editor->sci)] = ' '; // replace null -> virtual space for clang content[sci_get_length(editor->sci)] = '\0'; // TODO clang's col is byte? character? completion_framework->complete_async(editor->document->file_name, content, line + 1, byte_line_len + 1); edit_tracker.valid = true; edit_tracker.start_pos = pos; edit_tracker.text.clear(); if (pos != sci_get_current_position(editor->sci)) { int len = sci_get_current_position(editor->sci) - pos; edit_tracker.text.append(content + pos, len); } g_free(content); }
static void doit_and_select(guint group_id, guint key_id) { ScintillaObject *sci = scintilla_get_current(); if (sci) { int before = sci_get_current_position(sci), after; if (key_id != GEANY_KEYS_GOTO_LINE || !geany_data->toolbar_prefs->visible) keybindings_send_command(group_id, key_id); else if (go_to_line1_item) g_signal_emit_by_name(go_to_line1_item, "activate"); else { if (geany_data->prefs->beep_on_errors) gdk_beep(); return; } after = sci_get_current_position(sci); if (before != after) sci_set_anchor(sci, before); } }
static PyObject * ZenEditor_get_caret_pos(ZenEditor *self, PyObject *args) { PyObject *result; gint pos; ScintillaObject *sci; print_called(); py_return_none_if_null(sci = ZenEditor_get_scintilla(self)); pos = sci_get_current_position(sci); result = Py_BuildValue("i", pos); return result; }
static gchar * current_word(void) { GeanyDocument *doc; gint pos; gint cstart, cend; gchar c; doc = document_get_current(); g_return_val_if_fail(doc != NULL && doc->file_name != NULL, NULL); if (sci_has_selection(doc->editor->sci)) return sci_get_selection_contents(doc->editor->sci); pos = sci_get_current_position(doc->editor->sci); if (pos > 0) pos--; cstart = pos; c = sci_get_char_at(doc->editor->sci, cstart); if (!word_check_left(c)) return NULL; while (word_check_left(c)) { cstart--; if (cstart >= 0) c = sci_get_char_at(doc->editor->sci, cstart); else break; } cstart++; cend = pos; c = sci_get_char_at(doc->editor->sci, cend); while (word_check_right(c) && cend < sci_get_length(doc->editor->sci)) { cend++; c = sci_get_char_at(doc->editor->sci, cend); } if (cstart == cend) return NULL; return sci_get_contents_range(doc->editor->sci, cstart, cend); }
/* based on editor_find_current_word_sciwc from editor.c */ static void get_current_word(ScintillaObject *sci, gchar *word, gsize wordlen) { gint pos = sci_get_current_position(sci); gint start = SSM(sci, SCI_WORDSTARTPOSITION, pos, TRUE); gint end = SSM(sci, SCI_WORDENDPOSITION, pos, TRUE); if (start == end) *word = 0; else { if ((guint)(end - start) >= wordlen) end = start + (wordlen - 1); sci_get_text_range(sci, start, end, word); } }
static gboolean editor_notify_cb(GObject *object, GeanyEditor *editor, SCNotification *nt, gpointer data) { gint lexer, pos, style, min, size; gchar sel[512]; if (nt->nmhdr.code == SCN_CHARADDED && nt->ch == '>') { lexer = sci_get_lexer(editor->sci); if (lexer == SCLEX_XML || lexer == SCLEX_HTML) { pos = sci_get_current_position(editor->sci); style = sci_get_style_at(editor->sci, pos); if ((style <= SCE_H_XCCOMMENT || highlighting_is_string_style(lexer, style)) && !highlighting_is_comment_style(lexer, style)) { CompletionInfo c; InputInfo i; /* Grab the last 512 characters or so */ min = pos - sizeof(sel); if (min < 0) min = 0; size = pos - min; sci_get_text_range(editor->sci, min, pos, sel); if (get_completion(editor, sel, size, &c, &i)) { /* Remove typed opening tag */ sci_set_selection_start(editor->sci, min + i.tag_start); sci_set_selection_end(editor->sci, pos); sci_replace_sel(editor->sci, ""); pos -= (size - i.tag_start); /* pos has changed while deleting */ /* Insert the completion */ editor_insert_snippet(editor, pos, c.completion); sci_scroll_caret(editor->sci); g_free((gchar *)c.completion); return TRUE; } } } } return FALSE; }
static PyObject * Scintilla_get_style_at(Scintilla *self, PyObject *args, PyObject *kwargs) { gint pos = -1, style; static gchar *kwlist[] = { "pos", NULL }; SCI_RET_IF_FAIL(self); if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &pos)) { if (pos == -1) pos = sci_get_current_position(self->sci); style = sci_get_style_at(self->sci, pos); return Py_BuildValue("i", style); } Py_RETURN_NONE; }
static PyObject * Scintilla_insert_text(Scintilla *self, PyObject *args, PyObject *kwargs) { gint pos = -1; gchar *text; static gchar *kwlist[] = { "text", "pos", NULL }; SCI_RET_IF_FAIL(self); if (PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, &text, &pos)) { if (pos == -1) pos = sci_get_current_position(self->sci); if (text != NULL) sci_insert_text(self->sci, pos, text); } Py_RETURN_NONE; }
static void on_kb_goto_matching_tag (guint key_id) { gint cur_line; gint jump_line = 0; if(highlightedBrackets[0] != highlightedBrackets[2] && highlightedBrackets[0] != 0){ GeanyDocument *doc = document_get_current(); cur_line = sci_get_current_position(doc->editor->sci); if(cur_line >= highlightedBrackets[0] && cur_line <= highlightedBrackets[1]){ jump_line = highlightedBrackets[2]; } else if(cur_line >= highlightedBrackets[2] && cur_line <= highlightedBrackets[3]){ jump_line = highlightedBrackets[0]; } if(jump_line != 0){ sci_set_current_position(doc->editor->sci, jump_line, TRUE); } } }
static int get_completion_position(int* flag = NULL) { const char* stop_token = "{}[]#()<>%:;.?*+-/^&∼!=,\\\"\'\t\n "; float xxx; GeanyDocument* doc = document_get_current(); ScintillaObject* sci = doc->editor->sci; int cur_pos = sci_get_current_position(sci); if (cur_pos == 0) return 0; int cur_token_started_pos = 0; // g_print("char at (%d) %c ", sci_get_char_at(sci, cur_pos), sci_get_char_at(sci, cur_pos)); for (int pos = cur_pos - 1; pos >= 0; pos--) { if (strchr(stop_token, sci_get_char_at(sci, pos))) { cur_token_started_pos = pos + 1; break; } } // g_print("cpos %d token %d", cur_pos, cur_token_started_pos); return cur_token_started_pos; }
static void sync_to_current(ScintillaObject *sci, ScintillaObject *current) { gpointer sdoc; gint pos; /* set the new sci widget to view the existing Scintilla document */ sdoc = (gpointer) scintilla_send_message(current, SCI_GETDOCPOINTER, 0, 0); scintilla_send_message(sci, SCI_SETDOCPOINTER, 0, (sptr_t) sdoc); highlighting_set_styles(sci, edit_window.editor->document->file_type); pos = sci_get_current_position(current); sci_set_current_position(sci, pos, TRUE); /* override some defaults */ set_line_numbers(sci, geany->editor_prefs->show_linenumber_margin); /* marker margin */ scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 1, scintilla_send_message(current, SCI_GETMARGINWIDTHN, 1, 0)); if (!geany->editor_prefs->folding) scintilla_send_message(sci, SCI_SETMARGINWIDTHN, 2, 0); }
/* Note: this is NOT the Geany function, only similar */ gchar *plugme_editor_get_default_selection(GeanyEditor *editor, gboolean use_current_word, const gchar *wordchars) { ScintillaObject *sci = editor->sci; gchar *text = NULL; if (sci_has_selection(sci)) { if (sci_get_selected_text_length(sci) < GEANY_MAX_WORD_LENGTH) { text = sci_get_selection_contents(sci); if (strchr(text, '\n') != NULL) *strchr(text, '\n') = '\0'; } } else if (use_current_word) text = editor_get_word_at_pos(editor, sci_get_current_position(sci), wordchars); return text; }
static void run_tag_highlighter(ScintillaObject *sci) { gint position = sci_get_current_position(sci); gint lineNumber = sci_get_current_line(sci); gint lineStart = sci_get_position_from_line(sci, lineNumber); gint lineEnd = sci_get_line_end_position(sci, lineNumber); gint openingBracket = findBracket(sci, position, lineStart, '<', '>', FALSE); gint closingBracket = findBracket(sci, position, lineEnd, '>', '<', TRUE); int i; if(-1 == openingBracket || -1 == closingBracket) { clear_previous_highlighting(sci, highlightedBrackets[0], highlightedBrackets[1]); clear_previous_highlighting(sci, highlightedBrackets[2], highlightedBrackets[3]); for(i=0; i<3; i++) highlightedBrackets[i] = 0; return; } /* If the cursor jumps from one tag into another, clear * previous highlighted tags*/ if(openingBracket != highlightedBrackets[0] || closingBracket != highlightedBrackets[1]) { clear_previous_highlighting(sci, highlightedBrackets[0], highlightedBrackets[1]); clear_previous_highlighting(sci, highlightedBrackets[2], highlightedBrackets[3]); } /* Don't run search on empty brackets <> */ if (closingBracket - openingBracket > 1) { highlightedBrackets[0] = openingBracket; highlightedBrackets[1] = closingBracket; findMatchingTag(sci, openingBracket, closingBracket); } }
static void insert_multiline_comment(GeanyDocument *doc, gint pos) { g_return_if_fail(doc != NULL); g_return_if_fail(pos == -1 || pos >= 0); if (doc->file_type == NULL) { ui_set_statusbar(FALSE, _("Please set the filetype for the current file before using this function.")); return; } if (doc->file_type->comment_open || doc->file_type->comment_single) { /* editor_insert_multiline_comment() uses editor_info.click_pos */ if (pos == -1) editor_info.click_pos = sci_get_current_position(doc->editor->sci); else editor_info.click_pos = pos; editor_insert_multiline_comment(doc->editor); } else utils_beep(); }
static gboolean handle_backspace( AutocloseUserData *data, ScintillaObject *sci, gchar ch, gchar *ch_left, gchar *ch_right, GdkEventKey *event, gint indent_width) { gint pos = sci_get_current_position(sci); gint end_pos; gint line_start, line_end, line; gint i; if (!ac_info->delete_pairing_brace) return AC_CONTINUE_ACTION; ch = char_at(sci, pos - 1); if (!check_chars(sci, ch, ch_left, ch_right)) return AC_CONTINUE_ACTION; if (event->state & GDK_SHIFT_MASK) { if ((ch_left[0] == ch || ch_right[0] == ch) && ac_info->bcksp_remove_pair) { end_pos = sci_find_matching_brace(sci, pos - 1); if (-1 == end_pos) return AC_CONTINUE_ACTION; sci_start_undo_action(sci); line_start = sci_get_line_from_position(sci, pos); line_end = sci_get_line_from_position(sci, end_pos); SSM(sci, SCI_DELETERANGE, end_pos, 1); if (end_pos < pos) pos--; SSM(sci, SCI_DELETERANGE, pos - 1, 1); /* remove indentation magick */ if (char_is_curly_bracket(ch)) { if (line_start == line_end) goto final; if (line_start > line_end) { line = line_end; line_end = line_start; line_start = line; } if (blank_line(sci, line_start)) { delete_line(sci, line_start); line_end--; } else line_start++; if (blank_line(sci, line_end)) delete_line(sci, line_end); line_end--; /* unindent */ for (i = line_start; i <= line_end; i++) { unindent_line(sci, i, indent_width); } } final: sci_end_undo_action(sci); return AC_STOP_ACTION; }
static void do_format(GeanyDocument *doc, bool entire_doc, bool autof) { GString *formatted; ScintillaObject *sci; size_t offset = 0, length = 0, sci_len; size_t cursor_pos, old_first_line, new_first_line, line_delta; const char *sci_buf; bool changed = true; bool was_changed; if (doc == NULL) doc = document_get_current(); if (!DOC_VALID(doc)) { g_warning("Cannot format with no documents open"); return; } sci = doc->editor->sci; was_changed = doc->changed; // FIXME: instead of failing, ask user to save the document once if (!doc->real_path) { g_warning("Cannot format document that's never been saved"); return; } if (!entire_doc) { if (sci_has_selection(sci)) { // format selection offset = sci_get_selection_start(sci); length = sci_get_selection_end(sci) - offset; } else { // format current line size_t cur_line = sci_get_current_line(sci); offset = sci_get_position_from_line(sci, cur_line); length = sci_get_line_end_position(sci, cur_line) - offset; } } else { // format entire document offset = 0; length = sci_get_length(sci); } cursor_pos = sci_get_current_position(sci); sci_len = sci_get_length(sci); sci_buf = (const char *)scintilla_send_message(sci, SCI_GETCHARACTERPOINTER, 0, 0); formatted = fmt_clang_format(doc->file_name, sci_buf, sci_len, &cursor_pos, offset, length, false); // FIXME: handle better if (formatted == NULL) return; if (!autof) { changed = (formatted->len != sci_len) || (g_strcmp0(formatted->str, sci_buf) != 0); } old_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); // Replace document text and move cursor to new position scintilla_send_message(sci, SCI_BEGINUNDOACTION, 0, 0); scintilla_send_message(sci, SCI_CLEARALL, 0, 0); scintilla_send_message(sci, SCI_ADDTEXT, formatted->len, (sptr_t)formatted->str); scintilla_send_message(sci, SCI_GOTOPOS, cursor_pos, 0); new_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); line_delta = new_first_line - old_first_line; scintilla_send_message(sci, SCI_LINESCROLL, 0, -line_delta); scintilla_send_message(sci, SCI_ENDUNDOACTION, 0, 0); document_set_text_changed(doc, (was_changed || changed)); g_string_free(formatted, true); }
/* if type == -1 then we will try to autodetect the type */ void glatex_insert_environment(const gchar *environment, gint type) { GeanyDocument *doc = NULL; doc = document_get_current(); /* Only do anything if it is realy needed to */ if (doc != NULL && environment != NULL) { if (sci_has_selection(doc->editor->sci)) { gchar *selection = NULL; gchar *replacement = NULL; selection = sci_get_selection_contents(doc->editor->sci); sci_start_undo_action(doc->editor->sci); if (utils_str_equal(environment, "block") == TRUE) { replacement = g_strconcat("\\begin{", environment, "}{}\n", selection, "\n\\end{", environment, "}\n", NULL); } else { replacement = g_strconcat("\\begin{", environment, "}\n", selection, "\n\\end{", environment, "}\n", NULL); } sci_replace_sel(doc->editor->sci, replacement); sci_end_undo_action(doc->editor->sci); g_free(selection); g_free(replacement); } else { gint indent, pos; GString *tmpstring = NULL; gchar *tmp = NULL; static const GeanyIndentPrefs *indention_prefs = NULL; if (type == -1) { gint i; /* First, we check whether we have a known list over here * an reset type to fit new value*/ for (i = 0; i < GLATEX_LIST_END; i++) { if (utils_str_equal(glatex_list_environments[i], environment) == TRUE) { type = GLATEX_ENVIRONMENT_TYPE_LIST; break; } } } pos = sci_get_current_position(doc->editor->sci); sci_start_undo_action(doc->editor->sci); tmpstring = g_string_new("\\begin{"); g_string_append(tmpstring, environment); if (utils_str_equal(environment, "block") == TRUE) { g_string_append(tmpstring, "}{}"); } else { g_string_append(tmpstring, "}"); } g_string_append(tmpstring, "\n"); if (type == GLATEX_ENVIRONMENT_TYPE_LIST) { g_string_append(tmpstring, "\t\\item "); } tmp = g_string_free(tmpstring, FALSE); glatex_insert_string(tmp, TRUE); g_free(tmp); indent = sci_get_line_indentation(doc->editor->sci, sci_get_line_from_position(doc->editor->sci, pos)); tmp = g_strdup_printf("\n\\end{%s}", environment); glatex_insert_string(tmp, FALSE); g_free(tmp); indention_prefs = editor_get_indent_prefs(doc->editor); if (type == GLATEX_ENVIRONMENT_TYPE_LIST) { sci_set_line_indentation(doc->editor->sci, sci_get_current_line(doc->editor->sci), indent + indention_prefs->width); } sci_set_line_indentation(doc->editor->sci, sci_get_current_line(doc->editor->sci) + 1, indent); sci_end_undo_action(doc->editor->sci); } } }
static gboolean editor_notify_cb(GObject *object, GeanyEditor *editor, SCNotification *nt, gpointer data) { gint i = 0, val; gint old_position = 0; gint old_lposition = 0; gint old_line = 0; gint pos; if(NULL == editor || NULL == editor->sci) return FALSE; if(nt->nmhdr.code == SCN_CHARADDED) { if('\n' == nt->ch) define_format_newline(editor->sci); } if(nt->nmhdr.code == SCN_UPDATEUI) { if(g_array_index(lines_stack, gint, 0)) { /* save current position */ old_line = sci_get_current_line(editor->sci); old_lposition = sci_get_line_end_position(editor->sci, old_line) - sci_get_line_length(editor->sci, old_line); old_position = sci_get_current_position(editor->sci); sci_start_undo_action(editor->sci); } while((val = g_array_index(lines_stack, gint, i))) { i++; define_format_line(editor->sci, val - 1); dprintf("Removed from stack: %d\n", val); } if(i > 0) { sci_end_undo_action(editor->sci); g_array_remove_range(lines_stack, 0, i); /* restore current position */ pos = sci_get_line_end_position(editor->sci, old_line) - sci_get_line_length(editor->sci, old_line); sci_set_current_position(editor->sci, old_position + pos - old_lposition, FALSE); } } if(nt->nmhdr.code == SCN_MODIFIED) { if(nt->modificationType & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT)) { if(nt->modificationType & (SC_PERFORMED_UNDO | SC_PERFORMED_REDO)) return FALSE; gint line = sci_get_line_from_position(editor->sci, nt->position) + 1; if(sci_get_char_at(editor->sci, get_line_end(editor->sci, line - 1) - 1) == '\\') { gboolean found = FALSE; while((val = g_array_index(lines_stack, gint, i))) { if(val == line) { found = TRUE; break; } i++; } if(!found) { dprintf("Added line: %d\n", line); g_array_append_val(lines_stack, line); } } } } return FALSE; }