Пример #1
0
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);
}
Пример #2
0
static PyObject *
Scintilla_get_selected_text_length(Scintilla *self)
{
	gint len;
	SCI_RET_IF_FAIL(self);
	len = sci_get_selected_text_length(self->sci);
	return Py_BuildValue("i", len);
}
Пример #3
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;
}
Пример #4
0
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;
}
Пример #5
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);
}
Пример #6
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;
}
Пример #7
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;
}
Пример #8
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);
    }
}
Пример #9
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);
         }

      }
   }
Пример #10
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);
         }
      }
   }
Пример #11
0
			return FAIL_STRING_ARG(1);
		}
		txt = lua_tostring(L, 1);
		sci_set_text(doc->editor->sci, txt);
		return 0;
	}
}


/* Get or Set the selection text of the currently active Geany document */
static gint glspi_selection(lua_State* L)
{

	DOC_REQUIRED
	if (0 == lua_gettop(L)) { /* Called with no args, GET the selection */
		gint len = sci_get_selected_text_length(doc->editor->sci);
		gchar *txt = NULL;
		if (len>0) {
			txt = g_malloc0((guint)(len+1));
			sci_get_selected_text(doc->editor->sci, txt);
			lua_pushstring(L, (const gchar *) txt);
			g_free(txt);
		} else {
			lua_pushstring(L, "");
		}
		return 1;
	} else { /* Called with one arg, SET the selection */
		const gchar*txt=NULL;
		if (!lua_isstring(L, 1)) { return FAIL_STRING_ARG(1); }
		txt = lua_tostring(L, 1);
		sci_replace_sel(doc->editor->sci, txt);
Пример #12
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);
    }
}