Ejemplo n.º 1
0
/* This test imports an RTF file, exports it, and imports it again. The export
operation cannot fail, but if either import operation fails, the test fails. It
then compares the plaintext of the two GtkTextBuffers, and if they differ, the
test fails. Otherwise, the test succeeds.
Comparing the plaintext is for lack of a better way to compare the text buffers'
formatting. */
static void
rtf_write_pass_case(gconstpointer name)
{
    GError *error = NULL;
    GtkTextBuffer *buffer1 = gtk_text_buffer_new(NULL);
    GtkTextBuffer *buffer2 = gtk_text_buffer_new(NULL);
    gchar *filename = build_filename(name);
    
	if(!rtf_text_buffer_import(buffer1, filename, &error))
	    g_test_message("Import error message: %s", error->message);
	g_free(filename);
	g_assert(error == NULL);
	gchar *string = rtf_text_buffer_export_to_string(buffer1);
	if(!rtf_text_buffer_import_from_string(buffer2, string, &error))
	    g_test_message("Export error message: %s", error->message);
	g_assert(error == NULL);
	    
	GtkTextIter start, end;
	gtk_text_buffer_get_bounds(buffer1, &start, &end);
	gchar *text1 = gtk_text_buffer_get_slice(buffer1, &start, &end, TRUE);
	gtk_text_buffer_get_bounds(buffer2, &start, &end);
	gchar *text2 = gtk_text_buffer_get_slice(buffer2, &start, &end, TRUE);
	g_assert_cmpstr(text1, ==, text2);
	
	g_free(text1);
	g_free(text2);
	g_object_unref(buffer1);
	g_object_unref(buffer2);
	g_free(string);
}
Ejemplo n.º 2
0
void Highlight::searchwords_find_fast(GtkTextBuffer * textbuffer, GtkTextIter * beginbound, GtkTextIter * endbound, const ustring & searchword, bool casesensitive, vector < GtkTextIter > &wordstart, vector < GtkTextIter > &wordend)
// Searches for words to highlight. For simple highligthing. 
// Is much faster than the slow routine, see there fore more information.
{
  // Variable.
  GtkTextIter begin;
  GtkTextIter end;
  // Extract the line.
  ustring line = gtk_text_buffer_get_slice(textbuffer, beginbound, endbound, false);
  // Deal with case sensitivity.
  ustring case_considerate_search_word(searchword);
  if (!casesensitive)
    case_considerate_search_word = case_considerate_search_word.casefold();
  // Go through the line looking for matches.
  for (unsigned int i = 0; i < line.length(); i++) {
    if (interrupt_thread)
      continue;
    ustring compareline(line.substr(i, searchword.length()));
    // Deal with case sensitivity.
    if (!casesensitive)
      compareline = compareline.casefold();
    // Now compare.
    if (case_considerate_search_word == compareline) {
      // Get the iterators in the textbuffer that belong to this possible match.
      begin = *beginbound;
      gtk_text_iter_forward_chars(&begin, i);
      end = begin;
      gtk_text_iter_forward_chars(&end, searchword.length());
      // Add the boundaries of the word to highlight.
      wordstart.push_back(begin);
      wordend.push_back(end);
    }
  }
}
Ejemplo n.º 3
0
static void
restrict_message_length (GtkTextBuffer *buffer,
	MessageDetails *details)
{
	gchar *text;
	GtkTextIter start, end;

	gtk_text_buffer_get_bounds (buffer,
			&start, &end);

	text = gtk_text_buffer_get_slice (buffer,
			&start, &end, TRUE);

	if (strlen(text) > details->character_limit)
	  {
		gint cursor_position;

		g_object_get (buffer,
			"cursor-position", &cursor_position,
			NULL);

		gtk_text_buffer_get_iter_at_offset (buffer,
			&start,
			cursor_position-1);

		gtk_text_buffer_get_iter_at_offset (buffer,
			&end,
			cursor_position);

		gtk_text_buffer_delete (buffer,
			&start, &end);
	  }

	g_free (text);
}
Ejemplo n.º 4
0
static void
gbp_spell_navigator_change (GspellNavigator *navigator,
                            const gchar     *word,
                            const gchar     *change_to)
{
  GbpSpellNavigator *self = (GbpSpellNavigator *)navigator;
  GtkTextIter word_start;
  GtkTextIter word_end;
  g_autofree gchar *word_in_buffer = NULL;

  g_assert (GBP_IS_SPELL_NAVIGATOR (self));
  g_assert (GTK_IS_TEXT_MARK (self->word_start));
  g_assert (GTK_IS_TEXT_MARK (self->word_end));

  gtk_text_buffer_get_iter_at_mark (self->buffer, &word_start, self->word_start);
  gtk_text_buffer_get_iter_at_mark (self->buffer, &word_end, self->word_end);

  word_in_buffer = gtk_text_buffer_get_slice (self->buffer, &word_start, &word_end, TRUE);
  g_return_if_fail (word_in_buffer != NULL);
  g_return_if_fail (g_strcmp0 (word_in_buffer, word) == 0);

  gtk_text_buffer_begin_user_action (self->buffer);

  gtk_text_buffer_delete (self->buffer, &word_start, &word_end);
  gtk_text_buffer_insert (self->buffer, &word_start, change_to, -1);

  gtk_text_buffer_end_user_action (self->buffer);
}
Ejemplo n.º 5
0
static gunichar
ev_view_accessible_get_character_at_offset (AtkText *text,
					    gint    offset)
{
	GtkWidget *widget;
	GtkTextIter start, end;
	GtkTextBuffer *buffer;
	gchar *string;
	gunichar unichar;

	widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text));
	if (widget == NULL)
		/* State is defunct */
		return '\0';

	buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget));
	if (!buffer)
		return '\0';

	if (offset >= gtk_text_buffer_get_char_count (buffer))
		return '\0';

	gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
	end = start;
	gtk_text_iter_forward_char (&end);
	string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
	unichar = g_utf8_get_char (string);
	g_free(string);

	return unichar;
}
Ejemplo n.º 6
0
void
save(GtkTextView *widget) {
	GtkTextBuffer *buffer;
	GtkTextIter start, end;
	gchar *text, *errstr;

	if (g_mkdir_with_parents(dir, 0700) != 0) {
		errstr = g_strdup_printf("unable to make directory %s", dir);
		error(errstr);
		g_free(errstr);

	}

	buffer = gtk_text_view_get_buffer(widget);
	gtk_text_buffer_get_start_iter(buffer, &start);
	gtk_text_buffer_get_end_iter(buffer, &end);
	text = gtk_text_buffer_get_slice(buffer, &start, &end, TRUE);

	if (g_file_set_contents(path, text, -1, NULL) == FALSE) {
		errstr = g_strdup_printf("unable to save to %s", path);
		error(errstr);
		g_free(errstr);
	}

	g_free(text);
}
/* FIXME: move in pluma-document.c and share it with pluma-view */
static gboolean
get_selected_text (GtkTextBuffer  *doc,
		   gchar         **selected_text,
		   gint           *len)
{
	GtkTextIter start, end;

	g_return_val_if_fail (selected_text != NULL, FALSE);
	g_return_val_if_fail (*selected_text == NULL, FALSE);

	if (!gtk_text_buffer_get_selection_bounds (doc, &start, &end))
	{
		if (len != NULL)
			len = 0;

		return FALSE;
	}

	*selected_text = gtk_text_buffer_get_slice (doc, &start, &end, TRUE);

	if (len != NULL)
		*len = g_utf8_strlen (*selected_text, -1);

	return TRUE;
}
Ejemplo n.º 8
0
/**
 * gstyle_palette_new_from_buffer:
 * @buffer: a #GtkTextBUffer
 * @begin: (nullable): a begin #GtkTextIter
 * @end: (nullable): a end #GtkTextIter
 * @cancellable: A #GCancellable
 * @error: (nullable): a #GError location or %NULL
 *
 * Create a new #GstylePalette from a text buffer.
 * if @begin is %NULL, the buffer start iter is used.
 * if @end is %NULL, the buffer end is used.
 *
 * Returns: A #GstylePalette or %NULL if an error occur.
 */
GstylePalette *
gstyle_palette_new_from_buffer (GtkTextBuffer  *buffer,
                                GtkTextIter    *begin,
                                GtkTextIter    *end,
                                GCancellable   *cancellable,
                                GError        **error)
{
  g_autofree gchar *text = NULL;
  GstylePalette *palette = NULL;
  g_autofree gchar *name = NULL;
  GtkTextIter real_begin, real_end;
  GtkTextIter buffer_begin, buffer_end;
  GstyleColorItem *item;
  GstyleColor *color;
  GPtrArray *items;
  GError *tmp_error = NULL;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (begin == NULL || gtk_text_iter_get_buffer (begin) == buffer, NULL);
  g_return_val_if_fail (end == NULL || gtk_text_iter_get_buffer (end) == buffer, NULL);

  gtk_text_buffer_get_bounds (buffer, &buffer_begin, &buffer_end);
  real_begin = (begin == NULL) ? buffer_begin : *begin;
  real_end = (end == NULL) ? buffer_end : *end;

  text = gtk_text_buffer_get_slice (buffer, &real_begin, &real_end, FALSE);
  items = gstyle_color_parse (text);
  if (items == NULL)
    {
      g_set_error (error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_PARSE,
                   _("failed to parse\n"));
      return NULL;
    }

  if (items->len > 0)
    {
      /* To translators: always in singular form like in: generated palette number <generated_count> */
      name = g_strdup_printf ("%s %i", _("Generated"), ++generated_count);
      palette = g_object_new (GSTYLE_TYPE_PALETTE,
                              "id", NULL,
                              "name", name,
                              "file", NULL,
                              NULL);

      for (gint i = 0; i < items->len; ++i)
        {
          item = g_ptr_array_index (items, i);
          color = (GstyleColor *)gstyle_color_item_get_color (item);
          gstyle_palette_add (palette, color, &tmp_error);
        }
    }

  g_ptr_array_free (items, TRUE);
  return palette;
}
Ejemplo n.º 9
0
static void
calculate_info (CeditDocument *doc,
                GtkTextIter   *start,
                GtkTextIter   *end,
                gint          *chars,
                gint          *words,
                gint          *white_chars,
                gint          *bytes)
{
    gchar *text;

    cedit_debug (DEBUG_PLUGINS);

    text = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc),
                                      start,
                                      end,
                                      TRUE);

    *chars = g_utf8_strlen (text, -1);
    *bytes = strlen (text);

    if (*chars > 0)
    {
        PangoLogAttr *attrs;
        gint i;

        attrs = g_new0 (PangoLogAttr, *chars + 1);

        pango_get_log_attrs (text,
                             -1,
                             0,
                             pango_language_from_string ("C"),
                             attrs,
                             *chars + 1);

        for (i = 0; i < (*chars); i++)
        {
            if (attrs[i].is_white)
                ++(*white_chars);

            if (attrs[i].is_word_start)
                ++(*words);
        }

        g_free (attrs);
    }
    else
    {
        *white_chars = 0;
        *words = 0;
    }

    g_free (text);
}
static void
gb_color_picker_document_monitor_colorize (GbColorPickerDocumentMonitor *self,
                                           GtkTextBuffer                *buffer,
                                           GtkTextIter                  *begin,
                                           GtkTextIter                  *end)
{
  g_autofree gchar *text = NULL;
  g_autoptr(GPtrArray) items = NULL;
  GstyleColorItem *item;
  GtkTextTag *tag;
  GtkTextIter real_begin;
  GtkTextIter real_end;
  GtkTextIter tag_begin;
  GtkTextIter tag_end;
  gint offset;
  gint len;
  gint pos;

  g_return_if_fail (GB_IS_COLOR_PICKER_DOCUMENT_MONITOR (self));
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));

  if (begin == NULL)
    gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &real_begin);
  else
    real_begin = *begin;

  if (end == NULL)
    gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (buffer), &real_end);
  else
    real_end = *end;

  if (gtk_text_iter_equal (&real_begin, &real_end))
    return;

  offset = gtk_text_iter_get_offset (&real_begin);
  text = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (buffer), &real_begin, &real_end, TRUE);

  items = gstyle_color_parse (text);
  for (guint n = 0; n < items->len; ++n)
    {
      GstyleColor *color;

      item = g_ptr_array_index (items, n);
      pos = offset + gstyle_color_item_get_start (item);
      gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &tag_begin, pos);
      len = gstyle_color_item_get_len (item);
      gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &tag_end, pos + len);
      color = (GstyleColor *)gstyle_color_item_get_color (item);

      tag = gb_color_picker_helper_create_color_tag (GTK_TEXT_BUFFER (buffer), color);
      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag, &tag_begin, &tag_end);
      /* FIXME: is the tag added to the tag table or should we handle a hash table/tag table ourself ? */
    }
}
Ejemplo n.º 11
0
static void
change_cb (GeditSpellCheckerDialog *dlg,
	   const gchar             *word,
	   const gchar             *change,
	   GeditView               *view)
{
	GeditDocument *doc;
	CheckRange *range;
	gchar *w = NULL;
	GtkTextIter start, end;

	gedit_debug (DEBUG_PLUGINS);

	g_return_if_fail (view != NULL);
	g_return_if_fail (word != NULL);
	g_return_if_fail (change != NULL);

	doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
	g_return_if_fail (doc != NULL);

	range = get_check_range (doc);
	g_return_if_fail (range != NULL);

	gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &start, range->mw_start);
	if (range->mw_end < 0)
		gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (doc), &end);
	else
		gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &end, range->mw_end);

	w = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc), &start, &end, TRUE);
	g_return_if_fail (w != NULL);

	if (strcmp (w, word) != 0)
	{
		g_free (w);
		return;
	}

	g_free (w);

	gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER(doc));

	gtk_text_buffer_delete (GTK_TEXT_BUFFER (doc), &start, &end);
	gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc), &start, change, -1);

	gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER(doc));

	update_current (doc, range->mw_start + g_utf8_strlen (change, -1));

	/* go to next misspelled word */
	ignore_cb (dlg, word, view);
}
Ejemplo n.º 12
0
static void
change_all_cb (GeditSpellCheckerDialog *dlg,
	       const gchar             *word,
	       const gchar             *change,
	       GeditView               *view)
{
	GeditDocument *doc;
	CheckRange *range;
	gchar *w = NULL;
	GtkTextIter start, end;
	gint flags = 0;

	gedit_debug (DEBUG_PLUGINS);

	g_return_if_fail (view != NULL);
	g_return_if_fail (word != NULL);
	g_return_if_fail (change != NULL);

	doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
	g_return_if_fail (doc != NULL);

	range = get_check_range (doc);
	g_return_if_fail (range != NULL);

	gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &start, range->mw_start);
	if (range->mw_end < 0)
		gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (doc), &end);
	else
		gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &end, range->mw_end);

	w = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc), &start, &end, TRUE);
	g_return_if_fail (w != NULL);

	if (strcmp (w, word) != 0)
	{
		g_free (w);
		return;
	}

	g_free (w);

	GEDIT_SEARCH_SET_CASE_SENSITIVE (flags, TRUE);
	GEDIT_SEARCH_SET_ENTIRE_WORD (flags, TRUE);

	/* CHECK: currently this function does escaping etc */
	gedit_document_replace_all (doc, word, change, flags);

	update_current (doc, range->mw_start + g_utf8_strlen (change, -1));

	/* go to next misspelled word */
	ignore_cb (dlg, word, view);
}
Ejemplo n.º 13
0
static gchar *
get_current_word (GeditDocument *doc, gint *start, gint *end)
{
	const CheckRange *range;
	GtkTextIter end_iter;
	GtkTextIter current_iter;
	gint range_end;

	gedit_debug (DEBUG_PLUGINS);

	g_return_val_if_fail (doc != NULL, NULL);
	g_return_val_if_fail (start != NULL, NULL);
	g_return_val_if_fail (end != NULL, NULL);

	range = get_check_range (doc);
	g_return_val_if_fail (range != NULL, NULL);

	gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (doc), 
			&end_iter, range->end_mark);

	range_end = gtk_text_iter_get_offset (&end_iter);

	gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (doc), 
			&current_iter, range->current_mark);

	end_iter = current_iter;

	if (!gtk_text_iter_is_end (&end_iter))
	{
		gedit_debug_message (DEBUG_PLUGINS, "Current is not end");

		gtk_text_iter_forward_word_end (&end_iter);
	}

	*start = gtk_text_iter_get_offset (&current_iter);
	*end = MIN (gtk_text_iter_get_offset (&end_iter), range_end);

	gedit_debug_message (DEBUG_PLUGINS, "Current word extends [%d, %d]", *start, *end);

	if (!(*start < *end))
		return NULL;

	return gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc),
					  &current_iter,
					  &end_iter,
					  TRUE);
}
Ejemplo n.º 14
0
static void
message_response (GtkDialog *dialogue,
	gint arg1,
	MessageDetails *details)
{
	switch (arg1)
	  {
		case GTK_RESPONSE_OK:
		case GTK_RESPONSE_ACCEPT:
		  {
			GtkTextBuffer *buffer;
			gchar *text;
			GtkTextIter start, end;

			buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (details->textview));

			gtk_text_buffer_get_bounds (buffer,
				&start, &end);

			text = gtk_text_buffer_get_slice (buffer,
				&start, &end, TRUE);

			details->callback (
				GTK_WINDOW (details->parent),
				text,
				details->service);

			g_free (text);
			gtk_widget_destroy (GTK_WIDGET (dialogue));
		  }
		  break;

		case GTK_RESPONSE_CANCEL:
		case GTK_RESPONSE_REJECT:
		case GTK_RESPONSE_DELETE_EVENT:
			gtk_widget_destroy (GTK_WIDGET (dialogue));
			break;
	  }

	/* g_free (details); */
}
Ejemplo n.º 15
0
static void
change_all_cb (GeditSpellCheckerDialog *dlg,
	       const gchar             *word,
	       const gchar             *change,
	       GeditView               *view)
{
	GeditDocument *doc;
	CheckRange *range;
	gchar *w = NULL;
	GtkTextIter start, end;
	GtkSourceSearchSettings *search_settings;
	GtkSourceSearchContext *search_context;

	gedit_debug (DEBUG_PLUGINS);

	g_return_if_fail (view != NULL);
	g_return_if_fail (word != NULL);
	g_return_if_fail (change != NULL);

	doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
	g_return_if_fail (doc != NULL);

	range = get_check_range (doc);
	g_return_if_fail (range != NULL);

	gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &start, range->mw_start);
	if (range->mw_end < 0)
		gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (doc), &end);
	else
		gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &end, range->mw_end);

	w = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc), &start, &end, TRUE);
	g_return_if_fail (w != NULL);

	if (strcmp (w, word) != 0)
	{
		g_free (w);
		return;
	}

	g_free (w);

	search_settings = gtk_source_search_settings_new ();
	gtk_source_search_settings_set_case_sensitive (search_settings, TRUE);
	gtk_source_search_settings_set_at_word_boundaries (search_settings, TRUE);
	gtk_source_search_settings_set_search_text (search_settings, word);

	search_context = gtk_source_search_context_new (GTK_SOURCE_BUFFER (doc),
							search_settings);

	gtk_source_search_context_set_highlight (search_context, FALSE);

	gtk_source_search_context_replace_all (search_context, change, -1, NULL);

	update_current (doc, range->mw_start + g_utf8_strlen (change, -1));

	/* go to next misspelled word */
	ignore_cb (dlg, word, view);

	g_object_unref (search_settings);
	g_object_unref (search_context);
}
Ejemplo n.º 16
0
void Highlight::searchwords_find_slow(GtkTextBuffer * textbuffer, GtkTextIter * beginbound, GtkTextIter * endbound, const ustring & searchword, bool casesensitive, bool globbing, bool matchbegin, bool matchend, vector < GtkTextIter > &wordstart, vector < GtkTextIter > &wordend)
/*
Searches for words to highlight.
Problem when case insensitive searching:
  Character ffi was changed to ffi after casefolding, and as that one is 2
  characters longer than the original ffi, we ran in problems of searching
  past the line, which gave an exception in Gtk.
The solution was to determine the length of the word from the ones that are 
to highlight, not from the casefolded searchword, but the original one.
*/
{
  // Variable.
  GtkTextIter begin;
  GtkTextIter end;
  // Extract the line.
  ustring line = gtk_text_buffer_get_slice(textbuffer, beginbound, endbound, false);
  // Find all places in this line that have the search word.
  /*
     To do that properly for glob-style pattern matching, for begin/end word
     matching and case (in)sensitivity, we need to open the box of tricks.
     We produce all possible combinations for characters and lengths, e.g.
     We have this text:
     he is
     We then make the following strings from it, and see whether they match:
     "h"
     "he"
     "he "
     "he i"
     "he is"
     "e"
     "e "
     "e i"
     "e is"
     " "
     " i"
     " is"
     "i"
     "is"
     "s"
     Any string matching will then be highlighted.
   */
  // Deal with case sensitivity.
  ustring case_considerate_search_word(searchword);
  if (!casesensitive)
    case_considerate_search_word = case_considerate_search_word.casefold();
  for (unsigned int i = 0; i < line.length(); i++) {
    if (interrupt_thread)
      continue;
    ustring line2(line.substr(0, i + 1));
    for (unsigned int offposition = 0; offposition < line2.length(); offposition++) {
      // Get the line as described above.
      // We use optimization here to get the speed acceptable when we have 
      // long lines. But when globbing is done, because of the characters of 
      // glob-style matching, we don't know how long the searchword might be,
      // we do not use that optimization.
      unsigned int linelength = line2.length() - offposition;
      if (!globbing)
        if (linelength > searchword.length())
          continue;
      ustring compareline(line2.substr(offposition, linelength));
      // Deal with case sensitivity.
      if (!casesensitive)
        compareline = compareline.casefold();
      // Now compare.
      bool match = false;
      if (globbing) {
        if (g_pattern_match_simple(case_considerate_search_word.c_str(), compareline.c_str()))
          match = true;
      } else {
        if (case_considerate_search_word == compareline)
          match = true;
      }
      // Get the iterators in the textbuffer that belong to this possible match.
      if (match) {
        begin = *beginbound;
        gtk_text_iter_forward_chars(&begin, offposition);
        end = begin;
        gtk_text_iter_forward_chars(&end, searchword.length());
      }
      // Deal with begin-word matching.
      if (match) {
        if (matchbegin) {
          if (!gtk_text_iter_starts_word(&begin))
            match = false;
        }
      }
      // Deal with end-word matching.
      if (match) {
        if (matchend) {
          if (!gtk_text_iter_ends_word(&end))
            match = false;
        }
      }
      // Add the boundaries of the word to highlight.
      if (match) {
        wordstart.push_back(begin);
        wordend.push_back(end);
      }
    }
  }
}
Ejemplo n.º 17
0
/*
 * The almighty html parser method
 */
void parse_html(GtkTextView *text_view, GtkTextMark html_start, int ignore)
{
	GtkTextBuffer *html_buffer = gtk_text_view_get_buffer(text_view);

	tag *last_tag;
	GList *tag_list = NULL;

	int tagid = 0;
	GtkTextIter start_iter, end_iter;
	GtkTextMark *end_mark;
	GtkTextIter tag_start_iter, tag_end_iter;

	gtk_text_buffer_get_iter_at_mark(html_buffer, &start_iter, &html_start);
	gtk_text_buffer_get_end_iter(html_buffer, &end_iter);

	end_mark =
		gtk_text_buffer_create_mark(html_buffer, NULL, &end_iter, TRUE);

	gtk_text_buffer_get_iter_at_mark(html_buffer, &tag_start_iter,
		&html_start);
	gtk_text_buffer_get_iter_at_mark(html_buffer, &tag_end_iter,
		&html_start);

	/* Check if < and > exist in that order */
	while (search_char(&tag_start_iter, '<') &&
		search_char(&tag_end_iter, '>')) {
		gchar *tag_string;
		GtkTextMark *tag_start_mark = NULL, *next_start_mark = NULL;

		if (gtk_text_iter_compare(&tag_start_iter, &tag_end_iter) > 0) {
			gtk_text_iter_forward_char(&tag_end_iter);
			tag_start_iter = tag_end_iter;
			continue;
		}

		gtk_text_iter_forward_char(&tag_end_iter);

		tag_start_mark = gtk_text_buffer_create_mark(html_buffer, NULL,
			&tag_start_iter, TRUE);

		next_start_mark = gtk_text_buffer_create_mark(html_buffer, NULL,
			&tag_end_iter, TRUE);

		tag_string =
			gtk_text_buffer_get_slice(html_buffer, &tag_start_iter,
			&tag_end_iter, TRUE);

		/* Get rid of the < and > and clean up the tag string */
		tag_string = strstr(tag_string, "<") + 1;

		if (tag_string && strstr(tag_string, ">"))
			*(strstr(tag_string, ">")) = '\0';

		g_strstrip(tag_string);

		if (*tag_string == '/' && tag_is_valid(++tag_string)) {
			int found_match = 0;
			last_tag = NULL;

			/* Now get rid of the tag from the text */
			gtk_text_buffer_delete(html_buffer, &tag_start_iter,
				&tag_end_iter);

			/* 
			 * This is an end tag. So now we must apply the tag to
			 * the enclosed text
			 */
			do {
				last_tag =
					g_list_nth_data(g_list_last(tag_list),
					0);
				if (last_tag == NULL)
					break;

				if (!g_ascii_strncasecmp(tag_string,
						last_tag->name,
						strlen(tag_string))) {
					last_tag->end = *tag_start_mark;
					found_match = 1;
				} else {
					last_tag->end = *end_mark;
				}

				apply_tag(text_view, *last_tag, ignore);

				tag_list = g_list_remove(tag_list, last_tag);
			}
			while (!found_match);
		} else if (tag_is_valid(tag_string)) {
			tag *cur;

			/* Now get rid of the tag from the text */
			gtk_text_buffer_delete(html_buffer, &tag_start_iter,
				&tag_end_iter);

			/* This is a start tag. So put this into the list */
			cur = (tag *)malloc(sizeof(tag));
			bzero(cur->id, 8);

			sprintf(cur->id, "%d%d", messageid, tagid++);
			cur->name = strdup(tag_string);
			cur->start = *tag_start_mark;

			/* 
			 * Insert into the tag list only if it's a
			 * closing type tag
			 */
			if (!(ay_strcasestr(tag_string, "smiley") == tag_string
					|| ay_strcasestr(tag_string,
						"br") == tag_string
					|| ay_strcasestr(tag_string,
						"img") == tag_string
					|| ay_strcasestr(tag_string,
						"hr") == tag_string)) {
				tag_list = g_list_append(tag_list, cur);
			} else {
				apply_tag(text_view, *cur, ignore);
				free(cur);
			}

		}

		/* Re-initialize the string to get new positions */
		gtk_text_buffer_get_end_iter(html_buffer, &end_iter);

		end_mark =
			gtk_text_buffer_create_mark(html_buffer, NULL,
			&end_iter, TRUE);

		gtk_text_buffer_get_iter_at_mark(html_buffer, &tag_start_iter,
			next_start_mark);
		gtk_text_buffer_get_iter_at_mark(html_buffer, &tag_end_iter,
			next_start_mark);
	}

	while ((last_tag = g_list_nth_data(g_list_last(tag_list), 0))) {
		last_tag->end = *end_mark;
		apply_tag(text_view, *last_tag, ignore);
		tag_list = g_list_remove(tag_list, last_tag);
	}

	g_list_free(tag_list);

	unescape_html(html_buffer, html_start);
	messageid++;
}
Ejemplo n.º 18
0
/*
 * Unescapes escape sequences for some characters like &, >, <
 */
void unescape_html(GtkTextBuffer *buffer, GtkTextMark html_start)
{
	GtkTextIter start, end;
	GtkTextMark *start_mark = NULL, *end_mark = NULL;
	int html_found = 0;

	gtk_text_buffer_get_iter_at_mark(buffer, &start, &html_start);
	gtk_text_buffer_get_iter_at_mark(buffer, &end, &html_start);

	while (search_char(&start, '&') &&
		search_char(&end, ';') &&
		gtk_text_iter_compare(&start, &end) < 0) {
		gchar *code;

		gtk_text_iter_forward_char(&end);

		code = gtk_text_buffer_get_slice(buffer, &start, &end, TRUE);
		start_mark =
			gtk_text_buffer_create_mark(buffer, NULL, &start, TRUE);
		end_mark =
			gtk_text_buffer_create_mark(buffer, NULL, &end, TRUE);

		if (!g_ascii_strncasecmp(code, "&gt;", 4)) {
			gtk_text_buffer_delete(buffer, &start, &end);
			gtk_text_buffer_get_iter_at_mark(buffer, &start,
				start_mark);
			gtk_text_buffer_insert(buffer, &start, ">", -1);
			html_found = 1;
		}
		if (!g_ascii_strncasecmp(code, "&lt;", 4)) {
			gtk_text_buffer_delete(buffer, &start, &end);
			gtk_text_buffer_get_iter_at_mark(buffer, &start,
				start_mark);
			gtk_text_buffer_insert(buffer, &start, "<", -1);
			html_found = 1;
		}
		if (!g_ascii_strncasecmp(code, "&amp;", 5)) {
			gtk_text_buffer_delete(buffer, &start, &end);
			gtk_text_buffer_get_iter_at_mark(buffer, &start,
				start_mark);
			gtk_text_buffer_insert(buffer, &start, "&", -1);
			html_found = 1;
		}
		if (!g_ascii_strncasecmp(code, "&#8212;", 7)) {
			gtk_text_buffer_delete(buffer, &start, &end);
			gtk_text_buffer_get_iter_at_mark(buffer, &start,
				start_mark);
			gtk_text_buffer_insert(buffer, &start, "--", -1);
			html_found = 1;
		}
		if (!g_ascii_strncasecmp(code, "&nbsp;", 6)) {
			gtk_text_buffer_delete(buffer, &start, &end);
			gtk_text_buffer_get_iter_at_mark(buffer, &start,
				start_mark);
			gtk_text_buffer_insert(buffer, &start, " ", -1);
			html_found = 1;
		}
		if (!g_ascii_strncasecmp(code, "&quot;", 6)) {
			gtk_text_buffer_delete(buffer, &start, &end);
			gtk_text_buffer_get_iter_at_mark(buffer, &start,
				start_mark);
			gtk_text_buffer_insert(buffer, &start, "\"", -1);
			html_found = 1;
		}

		if (html_found) {
			gtk_text_buffer_get_iter_at_mark(buffer, &start,
				start_mark);
			gtk_text_buffer_get_iter_at_mark(buffer, &end,
				start_mark);
			gtk_text_iter_forward_char(&end);
		}

		gtk_text_iter_forward_char(&start);
	}
}
Ejemplo n.º 19
0
static void gtk_mate_exporter_scope_to_html (GtkMateExporter* self, GString* _result_, GtkMateScope* scope) {
	char* names;
	gboolean _tmp0_;
	GtkTextIter position;
	gboolean opened_inner;
	gboolean closed_inner;
	gboolean _tmp26_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (_result_ != NULL);
	g_return_if_fail (scope != NULL);
	names = gtk_mate_exporter_css_names (self, scope, FALSE);
	_tmp0_ = FALSE;
	if (_vala_strcmp0 (names, "") != 0) {
		_tmp0_ = names != NULL;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		char* _tmp2_;
		char* _tmp1_;
		_tmp2_ = NULL;
		_tmp1_ = NULL;
		g_string_append (_result_, _tmp2_ = g_strconcat (_tmp1_ = g_strconcat ("<span class=\"", names, NULL), "\">", NULL));
		_tmp2_ = (g_free (_tmp2_), NULL);
		_tmp1_ = (g_free (_tmp1_), NULL);
	}
	position = gtk_mate_scope_start_iter (scope);
	opened_inner = FALSE;
	closed_inner = FALSE;
	if (g_sequence_get_length (gtk_mate_scope_get_children (scope)) > 0) {
		GSequenceIter* iter;
		GtkMateScope* child;
		gboolean _tmp15_;
		iter = g_sequence_get_begin_iter (gtk_mate_scope_get_children (scope));
		child = NULL;
		while (!g_sequence_iter_is_end (iter)) {
			GtkMateScope* _tmp8_;
			GtkMateScope* _tmp7_;
			GtkTextIter _tmp9_ = {0};
			if (gtk_mate_exporter_scope_has_inner (self, scope)) {
				gboolean _tmp3_;
				GtkTextIter _tmp4_ = {0};
				_tmp3_ = FALSE;
				if (gtk_text_iter_compare (&position, (_tmp4_ = gtk_mate_scope_inner_start_iter (scope), &_tmp4_)) >= 0) {
					_tmp3_ = !opened_inner;
				} else {
					_tmp3_ = FALSE;
				}
				if (_tmp3_) {
					char* inner_names;
					char* _tmp6_;
					char* _tmp5_;
					inner_names = gtk_mate_exporter_css_names (self, scope, TRUE);
					_tmp6_ = NULL;
					_tmp5_ = NULL;
					g_string_append (_result_, _tmp6_ = g_strconcat (_tmp5_ = g_strconcat ("<span class=\"", inner_names, NULL), "\">", NULL));
					_tmp6_ = (g_free (_tmp6_), NULL);
					_tmp5_ = (g_free (_tmp5_), NULL);
					opened_inner = TRUE;
					inner_names = (g_free (inner_names), NULL);
				}
			}
			_tmp8_ = NULL;
			_tmp7_ = NULL;
			child = (_tmp8_ = (_tmp7_ = (GtkMateScope*) g_sequence_get (iter), (_tmp7_ == NULL) ? NULL : g_object_ref (_tmp7_)), (child == NULL) ? NULL : (child = (g_object_unref (child), NULL)), _tmp8_);
			if (gtk_text_iter_compare ((_tmp9_ = gtk_mate_scope_start_iter (child), &_tmp9_), &position) > 0) {
				char* _tmp12_;
				GtkTextIter _tmp11_ = {0};
				GtkMateBuffer* _tmp10_;
				_tmp12_ = NULL;
				_tmp10_ = NULL;
				g_string_append (_result_, _tmp12_ = g_markup_escape_text (gtk_text_buffer_get_slice ((GtkTextBuffer*) (_tmp10_ = gtk_mate_exporter_buffer (self)), &position, (_tmp11_ = gtk_mate_scope_start_iter (child), &_tmp11_), TRUE), -1));
				_tmp12_ = (g_free (_tmp12_), NULL);
				(_tmp10_ == NULL) ? NULL : (_tmp10_ = (g_object_unref (_tmp10_), NULL));
				position = gtk_mate_scope_start_iter (child);
			}
			if (gtk_mate_exporter_scope_has_inner (self, scope)) {
				gboolean _tmp13_;
				GtkTextIter _tmp14_ = {0};
				_tmp13_ = FALSE;
				if (gtk_text_iter_compare (&position, (_tmp14_ = gtk_mate_scope_inner_end_iter (scope), &_tmp14_)) >= 0) {
					_tmp13_ = !closed_inner;
				} else {
					_tmp13_ = FALSE;
				}
				if (_tmp13_) {
					g_string_append (_result_, "</span>");
					closed_inner = TRUE;
				}
			}
			gtk_mate_exporter_scope_to_html (self, _result_, child);
			position = gtk_mate_scope_end_iter (child);
			iter = g_sequence_iter_next (iter);
		}
		_tmp15_ = FALSE;
		if (child != NULL) {
			GtkTextIter _tmp17_ = {0};
			GtkTextIter _tmp16_ = {0};
			_tmp15_ = gtk_text_iter_compare ((_tmp16_ = gtk_mate_scope_end_iter (child), &_tmp16_), (_tmp17_ = gtk_mate_scope_end_iter (scope), &_tmp17_)) < 0;
		} else {
			_tmp15_ = FALSE;
		}
		if (_tmp15_) {
			char* _tmp21_;
			GtkTextIter _tmp20_ = {0};
			GtkTextIter _tmp19_ = {0};
			GtkMateBuffer* _tmp18_;
			_tmp21_ = NULL;
			_tmp18_ = NULL;
			g_string_append (_result_, _tmp21_ = g_markup_escape_text (gtk_text_buffer_get_slice ((GtkTextBuffer*) (_tmp18_ = gtk_mate_exporter_buffer (self)), (_tmp19_ = gtk_mate_scope_end_iter (child), &_tmp19_), (_tmp20_ = gtk_mate_scope_end_iter (scope), &_tmp20_), TRUE), -1));
			_tmp21_ = (g_free (_tmp21_), NULL);
			(_tmp18_ == NULL) ? NULL : (_tmp18_ = (g_object_unref (_tmp18_), NULL));
		}
		(child == NULL) ? NULL : (child = (g_object_unref (child), NULL));
	} else {
		char* _tmp25_;
		GtkTextIter _tmp24_ = {0};
		GtkTextIter _tmp23_ = {0};
		GtkMateBuffer* _tmp22_;
		_tmp25_ = NULL;
		_tmp22_ = NULL;
		g_string_append (_result_, _tmp25_ = g_markup_escape_text (gtk_text_buffer_get_slice ((GtkTextBuffer*) (_tmp22_ = gtk_mate_exporter_buffer (self)), (_tmp23_ = gtk_mate_scope_start_iter (scope), &_tmp23_), (_tmp24_ = gtk_mate_scope_end_iter (scope), &_tmp24_), TRUE), -1));
		_tmp25_ = (g_free (_tmp25_), NULL);
		(_tmp22_ == NULL) ? NULL : (_tmp22_ = (g_object_unref (_tmp22_), NULL));
	}
	_tmp26_ = FALSE;
	if (_vala_strcmp0 (names, "") != 0) {
		_tmp26_ = names != NULL;
	} else {
		_tmp26_ = FALSE;
	}
	if (_tmp26_) {
		g_string_append (_result_, "</span>");
	}
	names = (g_free (names), NULL);
	return;
}