Esempio n. 1
0
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);

	}
}
Esempio n. 2
0
void glatex_insert_latex_fontsize(G_GNUC_UNUSED GtkMenuItem * menuitem,
						 G_GNUC_UNUSED gpointer gdata)
{
	gint size = GPOINTER_TO_INT(gdata);
	GeanyDocument *doc = NULL;
	doc = document_get_current();

	if (doc != NULL)
	{
		if (sci_has_selection(doc->editor->sci))
		{
			gchar *selection;
			gchar *replacement = NULL;

			selection = sci_get_selection_contents(doc->editor->sci);

			replacement = g_strconcat("{",glatex_fontsize_pattern[size],
				" ", selection, "}", NULL);

			sci_replace_sel(doc->editor->sci, replacement);
			g_free(selection);
			g_free(replacement);
		}
		else
		{
			sci_start_undo_action(doc->editor->sci);
			glatex_insert_string(glatex_fontsize_pattern[size], TRUE);
			glatex_insert_string(" ", TRUE);
			sci_end_undo_action(doc->editor->sci);
		}
	}
}
Esempio n. 3
0
static void
reindent (GeanyDocument  *doc,
          GrindIndenter  *indenter)
{
  if (DOC_VALID (doc)) {
    int               start;
    int               end;
    ScintillaObject  *sci = doc->editor->sci;
    
    if (sci_has_selection (sci)) {
      start = sci_get_line_from_position (sci, sci_get_selection_start (sci));
      end = sci_get_line_from_position (sci, sci_get_selection_end (sci));
    } else {
      start = 0;
      end = sci_get_line_count (sci);
    }
    
    if (start != end) {
      g_debug ("Using indenter \"%s\" by \"%s\"",
               grind_indenter_get_name (indenter),
               grind_indenter_get_author (indenter));
      
      sci_start_undo_action (sci);
      if (grind_indenter_indent (indenter, doc,
                                 sci_get_position_from_line (sci, start),
                                 sci_get_line_end_position (sci, end))) {
        msgwin_status_add ("Reindented \"%s\"", DOC_FILENAME (doc));
      }
      sci_end_undo_action (sci);
    }
  }
}
Esempio n. 4
0
void sc_speller_check_document(GeanyDocument *doc)
{
	gint i;
	gint first_line, last_line;
	gchar *dict_string = NULL;
	gint suggestions_found = 0;

	g_return_if_fail(sc_speller_dict != NULL);
	g_return_if_fail(doc != NULL);

	ui_progress_bar_start(_("Checking"));

	enchant_dict_describe(sc_speller_dict, dict_describe, &dict_string);

	if (sci_has_selection(doc->editor->sci))
	{
		first_line = sci_get_line_from_position(
			doc->editor->sci, sci_get_selection_start(doc->editor->sci));
		last_line = sci_get_line_from_position(
			doc->editor->sci, sci_get_selection_end(doc->editor->sci));

		if (sc_info->use_msgwin)
			msgwin_msg_add(COLOR_BLUE, -1, NULL,
				_("Checking file \"%s\" (lines %d to %d using %s):"),
				DOC_FILENAME(doc), first_line + 1, last_line + 1, dict_string);
		g_message("Checking file \"%s\" (lines %d to %d using %s):",
			DOC_FILENAME(doc), first_line + 1, last_line + 1, dict_string);
	}
	else
	{
		first_line = 0;
		last_line = sci_get_line_count(doc->editor->sci);
		if (sc_info->use_msgwin)
			msgwin_msg_add(COLOR_BLUE, -1, NULL, _("Checking file \"%s\" (using %s):"),
				DOC_FILENAME(doc), dict_string);
		g_message("Checking file \"%s\" (using %s):", DOC_FILENAME(doc), dict_string);
	}
	g_free(dict_string);

	if (first_line == last_line)
	{
		suggestions_found += sc_speller_process_line(doc, first_line);
	}
	else
	{
		for (i = first_line; i < last_line; i++)
		{
			suggestions_found += sc_speller_process_line(doc, i);

			/* process other GTK events to keep the GUI being responsive */
			while (g_main_context_iteration(NULL, FALSE));
		}
	}
	if (suggestions_found == 0 && sc_info->use_msgwin)
		msgwin_msg_add(COLOR_BLUE, -1, NULL, _("The checked text is spelled correctly."));

	ui_progress_bar_stop();
}
Esempio n. 5
0
static PyObject *
Scintilla_has_selection(Scintilla *self)
{
	SCI_RET_IF_FAIL(self);
	if (sci_has_selection(self->sci))
		Py_RETURN_TRUE;
	else
		Py_RETURN_FALSE;
}
Esempio n. 6
0
static gboolean
automark(gpointer user_data)
{
	GeanyDocument      *doc = (GeanyDocument *)user_data;
	GeanyEditor        *editor = doc->editor;
	static GeanyEditor *editor_cache = NULL;
	ScintillaObject    *sci = editor->sci;
	gchar               text[GEANY_MAX_WORD_LENGTH];
	static gchar        text_cache[GEANY_MAX_WORD_LENGTH] = {0};
	gint                match_flag = SCFIND_MATCHCASE | SCFIND_WHOLEWORD;
	struct              Sci_TextToFind ttf;

	source_id = 0;

	/* during timeout document could be destroyed so check everything again */
	if (!DOC_VALID(doc))
		return FALSE;

	/* Do not highlight while selecting text and allow other markers to work */
	if (sci_has_selection(sci))
		return FALSE;

	get_current_word(editor->sci, text, sizeof(text));

	if (!*text)
	{
		editor_indicator_clear(editor, AUTOMARK_INDICATOR);
		return FALSE;
	}

	if (editor_cache != editor || strcmp(text, text_cache) != 0)
	{
		editor_indicator_clear(editor, AUTOMARK_INDICATOR);
		strcpy(text_cache, text);
		editor_cache = editor;
	}
	
	gint vis_first = SSM(sci, SCI_GETFIRSTVISIBLELINE, 0, 0);
	gint doc_first = SSM(sci, SCI_DOCLINEFROMVISIBLE, vis_first, 0);
	gint vis_last  = SSM(sci, SCI_LINESONSCREEN, 0, 0) + vis_first;
	gint doc_last  = SSM(sci, SCI_DOCLINEFROMVISIBLE, vis_last, 0);
	gint start     = SSM(sci, SCI_POSITIONFROMLINE,   doc_first, 0);
	gint end       = SSM(sci, SCI_GETLINEENDPOSITION, doc_last, 0);

	ttf.chrg.cpMin = start;
	ttf.chrg.cpMax = end;
	ttf.lpstrText  = text;

	search_mark_in_range(editor, match_flag, &ttf);

	return FALSE;
}
Esempio n. 7
0
void vte_send_selection_to_vte(void)
{
	GeanyDocument *doc;
	gchar *text;
	gsize len;

	doc = document_get_current();
	g_return_if_fail(doc != NULL);

	if (sci_has_selection(doc->editor->sci))
	{
		text = g_malloc0(sci_get_selected_text_length(doc->editor->sci) + 1);
		sci_get_selected_text(doc->editor->sci, text);
	}
	else
	{	/* Get the current line */
		gint line_num = sci_get_current_line(doc->editor->sci);
		text = sci_get_line(doc->editor->sci, line_num);
	}

	len = strlen(text);

	if (vc->send_selection_unsafe)
	{	/* Explicitly append a trailing newline character to get the command executed,
		   this is disabled by default as it could cause all sorts of damage. */
		if (text[len-1] != '\n' && text[len-1] != '\r')
		{
			SETPTR(text, g_strconcat(text, "\n", NULL));
			len++;
		}
	}
	else
	{	/* Make sure there is no newline character at the end to prevent unwanted execution */
		while (text[len-1] == '\n' || text[len-1] == '\r')
		{
			text[len-1] = '\0';
			len--;
		}
	}

	vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), text, len);

	/* show the VTE */
	gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
	gtk_widget_grab_focus(vc->vte);
	msgwin_show_hide(TRUE);

	g_free(text);
}
Esempio n. 8
0
void on_delete1_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));

	if (GTK_IS_EDITABLE(focusw))
		gtk_editable_delete_selection(GTK_EDITABLE(focusw));
	else if (IS_SCINTILLA(focusw) && sci_has_selection(SCINTILLA(focusw)))
		sci_clear(SCINTILLA(focusw));
	else if (GTK_IS_TEXT_VIEW(focusw))
	{
		GtkTextBuffer *buffer = gtk_text_view_get_buffer(
			GTK_TEXT_VIEW(focusw));
		gtk_text_buffer_delete_selection(buffer, TRUE, TRUE);
	}
}
Esempio n. 9
0
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);
}
Esempio n. 10
0
static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *event,
	G_GNUC_UNUSED gpointer gdata)
{
	guint mask = GDK_CONTROL_MASK | GDK_SHIFT_MASK | (column_mode ? 0 : GDK_MOD1_MASK);
	guint state = event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);

	if (state == mask)
	{
		const command_key *ck;

		for (ck = command_keys; ck->command; ck++)
		{
			if (event->keyval == ck->key || event->keyval == ck->keypad)
			{
				ScintillaObject *sci = scintilla_get_current();

				if (sci && gtk_window_get_focus(GTK_WINDOW(widget)) == GTK_WIDGET(sci))
				{
					column_mode_command(sci, ck->command);
					return TRUE;
				}
				break;
			}
		}
	}
	else if (!column_mode && state == GDK_SHIFT_MASK)
	{
		const command_key *ck;

		for (ck = command_keys; ck->key; ck++)
		{
			if (event->keyval == ck->key || event->keyval == ck->keypad)
			{
				ScintillaObject *sci = scintilla_get_current();

				if (sci && sci_has_selection(sci) && sci_rectangle_selection(sci) &&
					gtk_window_get_focus(GTK_WINDOW(widget)) == GTK_WIDGET(sci))
				{
					/* not exactly a bug, but... */
					convert_selection(sci, FALSE);
				}
				break;
			}
		}
	}

	return FALSE;
}
Esempio n. 11
0
static void goto_tag(gboolean definition)
{
	GeanyDocument *doc = document_get_current();

	g_return_if_fail(doc != NULL);

	/* update cursor pos for navigating back afterwards */
	if (!sci_has_selection(doc->editor->sci))
		sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE);

	/* use the keybinding callback as it checks for selections as well as current word */
	if (definition)
		keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDEFINITION);
	else
		keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_TAGDECLARATION);
}
Esempio n. 12
0
static gchar *get_selection(void)
{
	gchar *ret = NULL;
	GeanyDocument *doc = document_get_current();
	GeanyEditor *editor;

	if (!doc)
		return NULL;

	editor = doc->editor;

	if (sci_has_selection(editor->sci))
		ret = sci_get_selection_contents(editor->sci);
	else
		ret = editor_get_word_at_pos(editor, -1, GEANY_WORDCHARS);

	return ret;
}
Esempio n. 13
0
static void on_column_mode_toggled(G_GNUC_UNUSED GtkMenuItem *menuitem)
{
	ScintillaObject *sci = scintilla_get_current();

	if (sci)
	{
		column_mode = gtk_check_menu_item_get_active(column_mode_item);
		gtk_widget_set_sensitive(anchor_rect_select_item, !column_mode);

		if (!plugin_internal_callback)
		{
			assign_select_keys(sci);
			g_object_set_data(G_OBJECT(sci), "column_mode", GINT_TO_POINTER(column_mode));
			if (sci_has_selection(sci) && sci_rectangle_selection(sci) != column_mode)
				convert_selection(sci, column_mode);
		}
	}
}
Esempio n. 14
0
gchar *get_selection(void)
{
	GeanyDocument *doc = document_get_current();
	const gchar *wc;

#ifdef G_OS_WIN32
	wc = GEANY_WORDCHARS "./-" "\\";
#else
	wc = GEANY_WORDCHARS "./-";
#endif

	if (!doc)
		return NULL;

	if (sci_has_selection(doc->editor->sci))
		return sci_get_selection_contents(doc->editor->sci);
	else
		return editor_get_word_at_pos(doc->editor, -1, wc);
}
Esempio n. 15
0
void glatex_structure_lvlup(void)
{
	gint i;
	GeanyDocument *doc = NULL;
	gchar *tmp = NULL;
	GString *haystack = NULL;

	doc = document_get_current();

	if (doc == NULL)
		return;

	if (! sci_has_selection(doc->editor->sci))
		return;

	sci_start_undo_action(doc->editor->sci);
	tmp = sci_get_selection_contents(doc->editor->sci);
	haystack = g_string_new(tmp);

	g_free(tmp);
	tmp = NULL;

	for (i = 0; i < GLATEX_STRUCTURE_N_LEVEL; i++)
	{
		if (utils_string_replace_all
			(haystack,
			glatex_structure_values[i],
			glatex_structure_values[glatex_structure_rotate(FALSE, i)]
			) > 0)
		{
			tmp = g_string_free(haystack, FALSE);
			haystack = NULL;
			sci_replace_sel(doc->editor->sci, tmp);
			g_free(tmp);
			sci_end_undo_action(doc->editor->sci);
			break;
		}
	}

	if (haystack != NULL)
		g_string_free(haystack, TRUE);
}
Esempio n. 16
0
void glatex_insert_latex_format(G_GNUC_UNUSED GtkMenuItem * menuitem,
						 G_GNUC_UNUSED gpointer gdata)
{
	gint format = GPOINTER_TO_INT(gdata);
	GeanyDocument *doc = NULL;
	doc = document_get_current();

	if (doc != NULL)
	{
		if (sci_has_selection(doc->editor->sci))
		{
			gchar *selection;
			gchar *replacement = NULL;

			selection = sci_get_selection_contents(doc->editor->sci);

			if (format == LATEX_SMALLCAPS &&
				glatex_lowercase_on_smallcaps == TRUE)
			{
				gchar *new_selection = NULL;
				new_selection = g_utf8_strdown(selection, -1);
				g_free(selection);
				selection = g_strdup(new_selection);
				g_free(new_selection);
			}
			replacement = g_strconcat(glatex_format_pattern[format],"{",
				selection, "}", NULL);

			sci_replace_sel(doc->editor->sci, replacement);
			g_free(selection);
			g_free(replacement);
		}
		else
		{
			sci_start_undo_action(doc->editor->sci);
			glatex_insert_string(glatex_format_pattern[format], TRUE);
			glatex_insert_string("{", TRUE);
			glatex_insert_string("}", FALSE);
			sci_end_undo_action(doc->editor->sci);
		}
	}
}
Esempio n. 17
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;
}
Esempio n. 18
0
static gchar *get_paste_text(GeanyDocument *doc, gsize *text_len)
{
    gsize len;
    gchar *paste_text;

    if (sci_has_selection(doc->editor->sci))
    {
        len = sci_get_selected_text_length(doc->editor->sci) + 1;
        paste_text = sci_get_selection_contents(doc->editor->sci);
    }
    else
    {
        len = sci_get_length(doc->editor->sci) + 1;
        paste_text = sci_get_contents(doc->editor->sci, len);
    }

    if (text_len)
        *text_len = len;

    return paste_text;
}
Esempio n. 19
0
static gboolean can_insert_numbers(void)
{
	GeanyDocument *doc = document_get_current();

	if (doc && !doc->readonly)
	{
		ScintillaObject *sci = doc->editor->sci;

		if (sci_has_selection(sci) && (sci_get_selection_mode(sci) == SC_SEL_RECTANGLE ||
			sci_get_selection_mode(sci) == SC_SEL_THIN))
		{
			start_pos = sci_get_selection_start(sci);
			start_line = sci_get_line_from_position(sci, start_pos);
			end_pos = sci_get_selection_end(sci);
			end_line = sci_get_line_from_position(sci, end_pos);

			return end_line - start_line < MAX_LINES;
		}
	}

	return FALSE;
}
Esempio n. 20
0
void geanypg_load_buffer(gpgme_data_t * buffer)
{
    /* gpgme_data_new_from_mem(buffer, text, size, 0); */
    GeanyDocument * doc = document_get_current();
    char * data = NULL;
    unsigned long size = 0;
    if (sci_has_selection(doc->editor->sci))
    {
        size = scintilla_send_message(doc->editor->sci, SCI_GETSELTEXT, 0, 0) - 1;
        data = (char *) malloc(size + 1);
        scintilla_send_message(doc->editor->sci, SCI_GETSELTEXT, 0, (sptr_t)data);
        gpgme_data_new_from_mem(buffer, data, size, 1);
    }
    else
    {
        size = scintilla_send_message(doc->editor->sci, SCI_GETLENGTH, 0, 0);
        data = (char *) malloc(size + 1);
        scintilla_send_message(doc->editor->sci, SCI_GETTEXT, (uptr_t)(size + 1), (sptr_t)data);
        gpgme_data_new_from_mem(buffer, data, size, 1);
    }
    if (data) /* if there is no text data may still be NULL */
        free(data);
    gpgme_data_set_encoding(*buffer, GPGME_DATA_ENCODING_BINARY);
}
Esempio n. 21
0
void geanypg_write_file(FILE * file)
{
#define BUFSIZE 2048
    unsigned long size;
    char buffer[BUFSIZE] = {0};
    GeanyDocument * doc = document_get_current();
    sci_start_undo_action(doc->editor->sci);
    if (sci_has_selection(doc->editor->sci))
    {   /* replace selected text
         * clear selection, cursor should be at the end or beginneng of the selection */
        scintilla_send_message(doc->editor->sci, SCI_REPLACESEL, 0, (sptr_t)"");
        while ((size = fread(buffer, 1, BUFSIZE, file)))
            /* add at the cursor */
            scintilla_send_message(doc->editor->sci, SCI_ADDTEXT, (uptr_t) size, (sptr_t) buffer);
    }
    else
    {   /* replace complete document */
        scintilla_send_message(doc->editor->sci, SCI_CLEARALL, 0, 0);
        while ((size = fread(buffer, 1, BUFSIZE, file)))
            scintilla_send_message(doc->editor->sci, SCI_APPENDTEXT, (uptr_t) size, (sptr_t) buffer);
    }
    sci_end_undo_action(doc->editor->sci);
#undef BUFSIZE
}
Esempio n. 22
0
static void find_usage(gboolean in_session)
{
	GeanyFindFlags flags;
	gchar *search_text;
	GeanyDocument *doc = document_get_current();

	g_return_if_fail(doc != NULL);

	if (sci_has_selection(doc->editor->sci))
	{	/* take selected text if there is a selection */
		search_text = sci_get_selection_contents(doc->editor->sci);
		flags = GEANY_FIND_MATCHCASE;
	}
	else
	{
		editor_find_current_word_sciwc(doc->editor, -1,
			editor_info.current_word, GEANY_MAX_WORD_LENGTH);
		search_text = g_strdup(editor_info.current_word);
		flags = GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD;
	}

	search_find_usage(search_text, search_text, flags, in_session);
	g_free(search_text);
}
Esempio n. 23
0
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);
}
Esempio n. 24
0
static void on_extra_select_activate(G_GNUC_UNUSED GtkMenuItem *menuitem, gpointer gdata)
{
	gtk_widget_set_sensitive(GTK_WIDGET(gdata), sci_has_selection(scintilla_get_current()));
}
Esempio n. 25
0
/* 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);
		}
	}
}
Esempio n. 26
0
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);
         }

      }
   }
Esempio n. 27
0
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);
         }
      }
   }
Esempio n. 28
0
static void convert_selection(ScintillaObject *sci, gboolean rectangle)
{
	if (sci_has_selection(sci))
		create_selection(sci, sci_get_anchor(sci), sci_get_anchor_space(sci), rectangle);
}
Esempio n. 29
0
void sc_gui_update_editor_menu_cb(GObject *obj, const gchar *word, gint pos,
                                  GeanyDocument *doc, gpointer user_data)
{
    gchar *search_word;

    g_return_if_fail(doc != NULL && doc->is_valid);

    /* hide the submenu in any case, we will reshow it again if we actually found something */
    gtk_widget_hide(sc_info->edit_menu);
    gtk_widget_hide(sc_info->edit_menu_sep);

    if (! sc_info->show_editor_menu_item)
        return;

    /* if we have a selection, prefer it over the current word */
    if (sci_has_selection(doc->editor->sci))
    {
        gint len = sci_get_selected_text_length(doc->editor->sci);
        search_word = g_malloc(len + 1);
        sci_get_selected_text(doc->editor->sci, search_word);
    }
    else
        search_word = g_strdup(word);

    /* ignore numbers or words starting with digits and non-text */
    if (EMPTY(search_word) || isdigit(*search_word) || ! sc_speller_is_text(doc, pos))
    {
        g_free(search_word);
        return;
    }

    /* ignore too long search words */
    if (strlen(search_word) > 100)
    {
        GtkWidget *menu_item;

        init_editor_submenu();
        menu_item = gtk_menu_item_new_with_label(
                        _("Search term is too long to provide\nspelling suggestions in the editor menu."));
        gtk_widget_set_sensitive(menu_item, FALSE);
        gtk_widget_show(menu_item);
        gtk_container_add(GTK_CONTAINER(sc_info->edit_menu_sub), menu_item);

        menu_item = gtk_menu_item_new_with_label(_("Perform Spell Check"));
        gtk_widget_show(menu_item);
        gtk_container_add(GTK_CONTAINER(sc_info->edit_menu_sub), menu_item);
        g_signal_connect(menu_item, "activate", G_CALLBACK(perform_spell_check_cb), doc);

        g_free(search_word);
        return;
    }

    if (sc_speller_dict_check(search_word) != 0)
    {
        GtkWidget *menu_item, *menu;
        gchar *label;
        gsize n_suggs, i;
        gchar **suggs;

        suggs = sc_speller_dict_suggest(search_word, &n_suggs);

        clickinfo.pos = pos;
        clickinfo.doc = doc;
        setptr(clickinfo.word, search_word);

        menu = init_editor_submenu();

        for (i = 0; i < n_suggs; i++)
        {
            if (i > 0 && i % 10 == 0)
            {
                menu_item = gtk_menu_item_new();
                gtk_widget_show(menu_item);
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);

                menu_item = gtk_menu_item_new_with_label(_("More..."));
                gtk_widget_show(menu_item);
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);

                menu = gtk_menu_new();
                gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu);
            }
            menu_item = gtk_menu_item_new_with_label(suggs[i]);
            gtk_widget_show(menu_item);
            gtk_container_add(GTK_CONTAINER(menu), menu_item);
            g_signal_connect(menu_item, "activate",
                             G_CALLBACK(menu_suggestion_item_activate_cb), NULL);
        }
        if (suggs == NULL)
        {
            menu_item = gtk_menu_item_new_with_label(_("(No Suggestions)"));
            gtk_widget_set_sensitive(menu_item, FALSE);
            gtk_widget_show(menu_item);
            gtk_container_add(GTK_CONTAINER(sc_info->edit_menu_sub), menu_item);
        }
        menu_item = gtk_separator_menu_item_new();
        gtk_widget_show(menu_item);
        gtk_container_add(GTK_CONTAINER(sc_info->edit_menu_sub), menu_item);

        label = g_strdup_printf(_("Add \"%s\" to Dictionary"), search_word);
        menu_item = image_menu_item_new(GTK_STOCK_ADD, label);
        gtk_widget_show(menu_item);
        gtk_container_add(GTK_CONTAINER(sc_info->edit_menu_sub), menu_item);
        g_signal_connect(menu_item, "activate",
                         G_CALLBACK(menu_addword_item_activate_cb), GINT_TO_POINTER(FALSE));

        menu_item = image_menu_item_new(GTK_STOCK_REMOVE, _("Ignore All"));
        gtk_widget_show(menu_item);
        gtk_container_add(GTK_CONTAINER(sc_info->edit_menu_sub), menu_item);
        g_signal_connect(menu_item, "activate",
                         G_CALLBACK(menu_addword_item_activate_cb), GINT_TO_POINTER(TRUE));

        if (suggs != NULL)
            sc_speller_dict_free_string_list(suggs);

        g_free(label);
    }
    else
    {
        g_free(search_word);
    }
}