GTK TextBuffer , finding whole words

261 Views Asked by At

I am trying to compare words isolated in GTK textview against a list of keywords and cant seem to figure out how to do that. I am working(learning) in C.

gchar word[][10] = {"auto", "continue", "enum".....

I am moving iters word by word through the text buffer, then trying

gtk_text_iter_get_text(&start,&end);

so can end up with word[1] as "auto" and the iters finding the word "auto" in the textview. but cant find a way to compare them to confirm a match. attempts to use gtk_text_iter_forward_search() results in things like highlighting the "auto" in automobile.

I have tried 'strcmp()' but that always returns true no matter what is in the text view.

I am working on syntax highlighting kind of thing and have comments , single/double quotes, escape sequence/format specifiers and numbers all able to highlight but am stuck on getting highlighting on words, any nudge in the right direction is appreciated.

1

There are 1 best solutions below

3
Arthur On

I found a solution to this by checking if the textiters where at the start and end of a word.

  while (1){
     
     if(gtk_text_iter_forward_search(&end, word[loop], GTK_TEXT_SEARCH_TEXT_ONLY,
                                                            &start, &end, &stop)){
        if (gtk_text_iter_starts_word(&start) && gtk_text_iter_ends_word(&end)){
           gtk_text_buffer_apply_tag(buffer, word_tag, &start, &end);
        }
        else{
           gtk_text_iter_forward_word_end(&end);
           gtk_text_buffer_remove_tag(buffer, word_tag, &start, &end);
        }
     }
     else
        break;
  }

inside a for loop that iterates the array of C keywords, so now if I type 'auto' it will highlight , but if I carry on to 'automobile', the text tag is removed. the while loop scans the line the cursor is in and the line above word by word. I am only a month into learning C and GTK so not very advanced but maybe posting the solution I found will help some one else that is just learning too.