Exemple #1
0
static void
add_general_tag (char* tag_name, Preference text_pref,
                Preference background_pref, Preference font_pref)
{
        GdkRGBA *fg_color, *bg_color;
        char *font;
        char *key;

        /* initialize tag preferences */

        /* text color */
        key = preferences_get_key (text_pref);
        fg_color = preferences_get_color (key);
        preferences_notify_add (key, changed_text, tag_name);
        g_free (key);

        /* base color */
        key = preferences_get_key (background_pref);
        bg_color = preferences_get_color (key);
        preferences_notify_add (key, changed_background, tag_name);
        g_free (key);

        /* font */
        key = preferences_get_key (font_pref);
        font = preferences_get_string (key);
        preferences_notify_add (key, changed_font, tag_name);
        g_free (key);

        highlight_add_tag (tag_name, fg_color, bg_color, font);
}
Exemple #2
0
static void highlight_add_tags (guint id)
{
	/*
	 * don't call this function if you think there might be a tag by this
	 * name already, call highlight_check_tags first if that's the case
	 */
	int i;

        for (i = 0; i < HIGHLIGHT_MATCHES; i++) {
                GdkRGBA *text, *background;
                char *font;

                text = preferences_get_color (
                                preferences_get_highlight_match_key (id, i,
                                        PREF_HIGHLIGHT_MATCH_TEXT_COLOR));
                background = preferences_get_color (
                                preferences_get_highlight_match_key (id, i,
                                        PREF_HIGHLIGHT_MATCH_BASE_COLOR));
                font = preferences_get_string (
                                preferences_get_highlight_match_key (id, i,
                                        PREF_HIGHLIGHT_MATCH_FONT));
                highlight_add_tag (mangle_name (id, i), text, background, font);
        }
}
Exemple #3
0
void
highlight_apply (GtkTextBuffer *buffer, GtkTextIter *start,
				 GtkTextIter *end)
{
	/* Use a finite state machine to highlighting a code. */
	gchar *text;	
	gint i;
	gchar *lex;
	gint lex_len;
	gint state;
	GtkTextIter st, ed;
	
	text = gtk_text_iter_get_text (start, end);
	lex = (gchar *) g_malloc (MAX_LEX_SIZE);
	lex_len = 0;
	state = 0;

	gtk_text_buffer_get_start_iter (buffer, &st);
	gtk_text_buffer_get_end_iter (buffer, &ed);
	gtk_text_buffer_apply_tag_by_name (buffer, CODE_TAG_NONE, &st, &ed);

	for (i = 0; text[i]; i++)
	{		
		if ((!CHAR (text[i]) && !DIGIT (text[i])) || text[i + 1] == 0)
		{			
			gint start_offset;
			gchar *tag;
			
			start_offset = i - lex_len;
			tag = CODE_TAG_NONE;

			switch (state)
			{
				case 0:
					if (text[i] == '\"')
					{
						state = 1;
						lex[lex_len++] = text[i];
					}
					else if (text[i] == '\'')
					{
						state = 2;
						lex[lex_len++] = text[i];
					}
					else if (text[i] == '/')
					{
						state = 3;
						lex[lex_len++] = text[i];
					}
					else if (text[i + 1] == 0 && (CHAR (text[i]) || 
							 DIGIT (text[i])))
						lex[lex_len++] = text[i];
					break;
					
				case 1:
					lex[lex_len++] = text[i];
					if (text[i] == '\"')
					{
						int s = 0;
						int j = lex_len - 2;
						
						for ( ; j >= 0; j--)
							if (lex[j] == '\\')
								s++;
							else
								break;
						
						if (s % 2 == 0) {
							state = 0;
							tag = CODE_TAG_STRING;
						}
					}
					break;
					
				case 2:
					lex[lex_len++] = text[i];
					if (text[i] == '\'')
					{
						int s = 0;
						int j = lex_len - 2;
						
						for ( ; j >= 0; j--)
							if (lex[j] == '\\')
								s++;
							else
								break;
						
						if (s % 2 == 0) {
							state = 0;
							tag = CODE_TAG_STRING;
						}
					}
					break;
				
				case 3:
					lex[lex_len++] = text[i];
					if (text[i] == '/')
						state = 4;
					else if (text[i] == '*')
						state = 5;
					else {
						state = 0;
						tag = CODE_TAG_NONE;
					}
					break;
				
				case 4:
					lex[lex_len++] = text[i];
					if (text[i] == '\n') {
						state = 0;
						tag = CODE_TAG_COMMENT;
					}
					break;
					
				case 5:
					lex[lex_len++] = text[i];
					if (text[i] == '*')
						state = 6;
					break;
					
				case 6:
					lex[lex_len++] = text[i];
					if (text[i] == '/' && text[i - 1] == '*') {
						state = 0;
						tag = CODE_TAG_COMMENT;
					}
					break;
			}
			
			lex[lex_len] = 0;
			
			if (state != 0)
				continue;
			
			if (tag == CODE_TAG_NONE) {
				if (DIGIT (lex[0]))
					tag = CODE_TAG_CONSTANT;
				else if (lex[0] == '#')
					tag = CODE_TAG_PREPROCESSOR;
				else if (CHAR (lex[0]) && lex_len <= MAX_KEYWORD_LENTH && highlight_is_keyword (lex))
					tag = CODE_TAG_KEYWORD;
			}

			highlight_add_tag (buffer, start, start_offset, lex_len, tag);

			lex_len = 0;
		}
		else
			lex[lex_len++] = text[i];
		
		
	}
	
	g_free (lex);
}