Exemple #1
0
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;
}
Exemple #2
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);
}
Exemple #3
0
static void menu_suggestion_item_activate_cb(GtkMenuItem *menuitem, gpointer gdata)
{
    const gchar *sugg;
    gint startword, endword;
    ScintillaObject *sci = clickinfo.doc->editor->sci;

    g_return_if_fail(clickinfo.doc != NULL && clickinfo.pos != -1);

    startword = scintilla_send_message(sci, SCI_WORDSTARTPOSITION, clickinfo.pos, 0);
    endword = scintilla_send_message(sci, SCI_WORDENDPOSITION, clickinfo.pos, 0);

    if (startword != endword)
    {
        gchar *word;

        sci_set_selection_start(sci, startword);
        sci_set_selection_end(sci, endword);

        /* retrieve the old text */
        word = g_malloc(sci_get_selected_text_length(sci) + 1);
        sci_get_selected_text(sci, word);

        /* retrieve the new text */
        sugg = gtk_label_get_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menuitem))));

        /* replace the misspelled word with the chosen suggestion */
        sci_replace_sel(sci, sugg);

        /* store the replacement for future checks */
        sc_speller_store_replacement(word, sugg);

        /* remove indicator */
        sci_indicator_clear(sci, startword, endword - startword);

        g_free(word);
    }
}
Exemple #4
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);
         }

      }
   }
Exemple #5
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);
         }
      }
   }
Exemple #6
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);
    }
}