void enclose_text_action (guint key_id) { gint selection_end; gchar insert_chars [2] = {0, 0}; ScintillaObject *sci_obj; if (!enclose_enabled) return; sci_obj = document_get_current ()->editor->sci; if (sci_get_selected_text_length (sci_obj) < 2) return; key_id -= 4; selection_end = sci_get_selection_end (sci_obj); sci_start_undo_action (sci_obj); insert_chars [0] = *enclose_chars [key_id]; sci_insert_text (sci_obj, sci_get_selection_start (sci_obj), insert_chars); insert_chars [0] = *(enclose_chars [key_id] + 1); sci_insert_text (sci_obj, selection_end + 1, insert_chars); sci_set_current_position (sci_obj, selection_end + 2, TRUE); sci_end_undo_action (sci_obj); }
gboolean on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data) { gint selection_end; gchar insert_chars [4] = {0, 0, 0, 0}; ScintillaObject *sci_obj; if (!auto_enabled) return FALSE; sci_obj = document_get_current ()->editor->sci; if (sci_get_selected_text_length (sci_obj) < 2) return FALSE; switch (event->keyval) { case '(': insert_chars [0] = '('; insert_chars [2] = ')'; break; case '[': insert_chars [0] = '['; insert_chars [2] = ']'; break; case '{': insert_chars [0] = '{'; insert_chars [2] = '}'; break; case '\'': insert_chars [0] = '\''; insert_chars [2] = '\''; break; case '\"': insert_chars [0] = '\"'; insert_chars [2] = '\"'; break; case '`': insert_chars [0] = '`'; insert_chars [2] = '`'; break; default: return FALSE; } selection_end = sci_get_selection_end (sci_obj); sci_start_undo_action (sci_obj); sci_insert_text (sci_obj, sci_get_selection_start (sci_obj), insert_chars); sci_insert_text (sci_obj, selection_end + 1, insert_chars+2); sci_set_current_position (sci_obj, selection_end + 2, TRUE); sci_end_undo_action (sci_obj); return TRUE; }
static void on_comments_function_activate(GtkMenuItem *menuitem, gpointer user_data) { GeanyDocument *doc = document_get_current(); gchar *text; const gchar *cur_tag = NULL; gint line = -1, pos = 0; if (doc == NULL || doc->file_type == NULL) { ui_set_statusbar(FALSE, _("Please set the filetype for the current file before using this function.")); return; } /* symbols_get_current_function returns -1 on failure, so sci_get_position_from_line * returns the current position, so it should be safe */ line = symbols_get_current_function(doc, &cur_tag); pos = sci_get_position_from_line(doc->editor->sci, line); text = templates_get_template_function(doc, cur_tag); sci_start_undo_action(doc->editor->sci); sci_insert_text(doc->editor->sci, pos, text); sci_end_undo_action(doc->editor->sci); g_free(text); }
static PyObject * ZenEditor_replace_content(ZenEditor *self, PyObject *args) { PyObject *result; gint sel_start = -1, sel_end = -1, ph_pos; gchar *text, *ph, *tmp, *tmp2; ScintillaObject *sci; print_called(); py_return_none_if_null(sci = ZenEditor_get_scintilla(self)); if (PyArg_ParseTuple(args, "s|ii", &text, &sel_start, &sel_end)) { tmp = ZenEditor_replace_caret_placeholder(self->caret_placeholder, text, &ph_pos); tmp2 = ZenEditor_replace_range(tmp); g_free(tmp); if (sel_start == -1 && sel_end == -1) { /* replace whole editor content */ sci_set_text(sci, tmp2); } else if (sel_start != -1 && sel_end == -1) { /* insert text at sel_start */ sci_insert_text(sci, sel_start, tmp2); } else if (sel_start != -1 && sel_end != -1) { /* replace from sel_start to sel_end */ sci_set_selection_start(sci, sel_start); sci_set_selection_end(sci, sel_end); sci_replace_sel(sci, tmp2); } else { dbgf("Invalid arguments were supplied."); g_free(tmp2); Py_RETURN_NONE; } g_free(tmp2); /* Move cursor to first placeholder position, if found */ if (ph_pos > -1) sci_set_current_position(sci, sel_start + ph_pos, TRUE); } else { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); } } Py_RETURN_NONE; }
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 void define_format_line(ScintillaObject *sci, gint current_line) { gint length; gint first_line; gint first_end; gint max = geany_data->editor_prefs->long_line_column; if(!inside_define(sci, current_line, FALSE)) return; first_line = current_line; first_end = get_line_end(sci, first_line); for (first_end--; sci_get_char_at(sci, first_end - 1) == ' '; first_end--) {} SSM(sci, SCI_DELETERANGE, first_end, sci_get_line_end_position(sci, first_line) - first_end); length = first_end - get_indent_pos(sci, first_line) + sci_get_line_indentation(sci, first_line); for(; length < max - 1; length++, first_end++) sci_insert_text(sci, first_end, " "); sci_insert_text(sci, first_end, "\\"); }
static void define_format_newline(ScintillaObject *sci) { gint end_pos; gint line; line = sci_get_current_line(sci); if(!inside_define(sci, line, TRUE)) return; line--; end_pos = sci_get_line_end_position(sci, line); sci_insert_text(sci, end_pos, "\\"); line += 2; dprintf("Added line: %d\n", line); g_array_append_val(lines_stack, line); }
static void sci_text_set_range(ScintillaObject* sci, unsigned int i, int n, const char* src) { unsigned int length = sci_get_length(sci); if (i > length) { i = length; } if (n < 0) { n = length; } if (i + n > length) { n = length - i; } if (n) { // TODO: Restore current position in sci. sci_replace_text(sci, i, i+n, src); } else { // Use insert because it handles current position properly. sci_insert_text(sci, i, src); } }
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 insert_numbers(gboolean *cancel) { /* editor */ ScintillaObject *sci = document_get_current()->editor->sci; gint xinsert = sci_point_x_from_position(sci, start_pos); gint xend = sci_point_x_from_position(sci, end_pos); gint *line_pos = g_new(gint, end_line - start_line + 1); gint line, i; /* generator */ gint64 start = start_value; gint64 value; unsigned count = 0; size_t prefix_len = 0; int plus = 0, minus; size_t length, lend; char pad, aax; gchar *buffer; if (xend < xinsert) xinsert = xend; ui_progress_bar_start(_("Counting...")); /* lines shorter than the current selection are skipped */ for (line = start_line, i = 0; line <= end_line; line++, i++) { if (sci_point_x_from_position(sci, scintilla_send_message(sci, SCI_GETLINEENDPOSITION, line, 0)) >= xinsert) { line_pos[i] = sci_get_pos_at_line_sel_start(sci, line) - sci_get_position_from_line(sci, line); count++; } else line_pos[i] = -1; if (cancel && i % 2500 == 0) { update_display(); if (*cancel) { ui_progress_bar_stop(); g_free(line_pos); return; } } } switch (base_value * base_prefix) { case 8 : prefix_len = 1; break; case 16 : prefix_len = 2; break; case 10 : plus++; } value = start + (count - 1) * step_value; minus = start < 0 || value < 0; lend = plus || (pad_zeros ? minus : value < 0); while (value /= base_value) lend++; value = start; length = plus || (pad_zeros ? minus : value < 0); while (value /= base_value) length++; length = prefix_len + (length > lend ? length : lend) + 1; buffer = g_new(gchar, length + 1); buffer[length] = '\0'; pad = pad_zeros ? '0' : ' '; aax = (lower_case ? 'a' : 'A') - 10; gtk_progress_bar_set_text(GTK_PROGRESS_BAR(geany->main_widgets->progressbar), _("Preparing...")); update_display(); sci_start_undo_action(sci); sci_replace_sel(sci, ""); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(geany->main_widgets->progressbar), _("Inserting...")); for (line = start_line, i = 0; line <= end_line; line++, i++) { gchar *beg, *end; gint insert_pos; if (line_pos[i] < 0) continue; beg = buffer; end = buffer + length; value = ABS(start); do { unsigned digit = value % base_value; *--end = digit + (digit < 10 ? '0' : aax); } while (value /= base_value); if (pad_zeros) { if (start < 0) *beg++ = '-'; else if (plus) *beg++ = '+'; else if (minus) *beg++ = ' '; memcpy(beg, "0x", prefix_len); beg += prefix_len; } else { if (start < 0) *--end = '-'; else if (plus) *--end = '+'; end -= prefix_len; memcpy(end, "0x", prefix_len); } memset(beg, pad, end - beg); insert_pos = sci_get_position_from_line(sci, line) + line_pos[i]; sci_insert_text(sci, insert_pos, buffer); start += step_value; if (cancel && i % 1000 == 0) { update_display(); if (*cancel) { scintilla_send_message(sci, SCI_GOTOPOS, insert_pos + length, 0); break; } } } sci_end_undo_action(sci); g_free(buffer); g_free(line_pos); ui_progress_bar_stop(); }
static void shift_left_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata){ gchar *txt; gchar *txt_i; gchar char_before; gint txt_len; gint startpos; gint endpos; gint startline; gint endline; gint line_iter; gint linepos; gint linelen; gint startcol; gint endcol; gint i; gint n_spaces; gchar *spaces; ScintillaObject *sci; /* get a pointer to the scintilla object */ sci = document_get_current()->editor->sci; if (sci_has_selection(sci)){ startpos = sci_get_selection_start(sci); endpos = sci_get_selection_end(sci); /* sanity check -- we dont care which way the block was selected */ if(startpos > endpos){ i = endpos; endpos = startpos; startpos = i; } startline = sci_get_line_from_position(sci, startpos); /* Setting also start point for 1st line */ linepos = sci_get_position_from_line(sci, startline); endline = sci_get_line_from_position(sci, endpos); /* normal mode */ if(startline == endline){ /* get the text in question */ txt_len = endpos - startpos; txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_before = sci_get_char_at(sci, startpos - 1); /* set up new text buf */ (void) g_sprintf(txt, "%s%c", txt_i, char_before); /* start undo */ sci_start_undo_action(sci); /* put the new text in */ sci_set_selection_start(sci, startpos - 1); sci_replace_sel(sci, txt); /* select the right bit again */ sci_set_selection_start(sci, startpos - 1); sci_set_selection_end(sci, endpos - 1); /* end undo */ sci_end_undo_action(sci); g_free(txt); g_free(txt_i); } /* rectangle mode (we hope!) */ else{ startcol = sci_get_col_from_position(sci, startpos); endcol = sci_get_col_from_position(sci, endpos); /* return early for the trivial case */ if(startcol == 0 || startcol == endcol){ return; } /* start undo */ sci_start_undo_action(sci); for(line_iter = startline; line_iter <= endline; line_iter++){ linepos = sci_get_position_from_line(sci, line_iter); linelen = sci_get_line_length(sci, line_iter); /* do we need to do something */ if(linelen >= startcol - 1 ){ /* if between the two columns */ /* pad to the end first */ if(linelen <= endcol){ /* bung in some spaces -- sorry, I dont like tabs */ n_spaces = endcol - linelen + 1; spaces = g_malloc(sizeof(gchar) * (n_spaces + 1)); for(i = 0; i < n_spaces; i++){ spaces[i] = ' '; } spaces[i] = '\0'; sci_insert_text(sci, linepos + linelen - 1, spaces); g_free(spaces); } /* now move the text itself */ sci_set_selection_mode(sci, 0); sci_set_selection_start(sci, linepos + startcol); sci_set_selection_end(sci, linepos + endcol); txt_len = sci_get_selected_text_length(sci); txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_before = sci_get_char_at(sci, linepos + startcol - 1); /* set up new text buf */ (void) g_sprintf(txt, "%s%c", txt_i, char_before); /* put the new text in */ sci_set_selection_start(sci, linepos + startcol - 1); sci_replace_sel(sci, txt); g_free(txt); g_free(txt_i); } } /* put the selection box back */ /* here we rely upon the last result of linepos */ sci_set_selection_mode(sci, 1); sci_set_selection_start(sci, startpos - 1); sci_set_selection_end(sci, linepos + endcol - 1); /* end undo action */ sci_end_undo_action(sci); } } }
static void shift_right_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata){ gchar *txt; gchar *txt_i; gchar char_after; gint txt_len; gint startpos; gint endpos; gint startline; gint endline; gint line_iter; gint linepos; gint linelen; gint startcol; gint endcol; gint i; ScintillaObject *sci; /* get a pointer to the scintilla object */ sci = document_get_current()->editor->sci; if (sci_has_selection(sci)){ startpos = sci_get_selection_start(sci); endpos = sci_get_selection_end(sci); /* sanity check -- we dont care which way the block was selected */ if(startpos > endpos){ i = endpos; endpos = startpos; startpos = i; } startline = sci_get_line_from_position(sci, startpos); linepos = sci_get_position_from_line(sci, startline); endline = sci_get_line_from_position(sci, endpos); /* normal mode */ if(startline == endline){ /* get the text in question */ txt_len = endpos - startpos; txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_after = sci_get_char_at(sci, endpos); /* set up new text buf */ (void) g_sprintf(txt, "%c%s", char_after, txt_i); /* start undo */ sci_start_undo_action(sci); /* put the new text in */ sci_set_selection_end(sci, endpos + 1); sci_replace_sel(sci, txt); /* select the right bit again */ sci_set_selection_start(sci, startpos + 1); sci_set_selection_end(sci, endpos + 1); /* end undo */ sci_end_undo_action(sci); g_free(txt); g_free(txt_i); } /* rectangle mode (we hope!) */ else{ startcol = sci_get_col_from_position(sci, startpos); endcol = sci_get_col_from_position(sci, endpos); /* start undo */ sci_start_undo_action(sci); for(line_iter = startline; line_iter <= endline; line_iter++){ linepos = sci_get_position_from_line(sci, line_iter); linelen = sci_get_line_length(sci, line_iter); /* do we need to do something */ if(linelen >= startcol - 1 ){ /* if between the two columns or at the end */ /* add in a space */ if(linelen <= endcol || linelen - 1 == endcol){ txt = g_malloc(sizeof(gchar) * 2); sprintf(txt, " "); sci_insert_text(sci, linepos + startcol, txt); g_free(txt); } else{ /* move the text itself */ sci_set_selection_mode(sci, 0); sci_set_selection_start(sci, linepos + startcol); sci_set_selection_end(sci, linepos + endcol); txt_len = sci_get_selected_text_length(sci); txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_after = sci_get_char_at(sci, linepos + endcol); /* set up new text buf */ (void) g_sprintf(txt, "%c%s", char_after, txt_i); /* put the new text in */ sci_set_selection_end(sci, linepos + endcol + 1); sci_replace_sel(sci, txt); g_free(txt); g_free(txt_i); } } } /* put the selection box back */ /* here we rely upon the last result of linepos */ sci_set_selection_mode(sci, 1); sci_set_selection_start(sci, startpos + 1); sci_set_selection_end(sci, linepos + endcol + 1); /* end undo action */ sci_end_undo_action(sci); } } }