Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:39:42+00:00 2026-06-09T21:39:42+00:00

I’m experimenting with Glib::Regex and Gtk::TextView with Gtk::TextBuffer-s, and I’m trying to do syntax

  • 0

I’m experimenting with Glib::Regex and Gtk::TextView with Gtk::TextBuffer-s, and I’m trying to do syntax highlighting with Gtk::TextTag-s.

My code for updating syntax (it receives iterators at the start and end of the line)

void MainWindow::update_syntax(const Gtk::TextBuffer::iterator& start, const Gtk::TextBuffer::iterator& end)    {
  std::vector<Glib::ustring> keywords;
  keywords.push_back("class");
  keywords.push_back("struct");
  Glib::MatchInfo info;
  auto regex = Glib::Regex::create(R"((\w+))");
  auto ok = regex->match(start.get_visible_text(end), info);
  std::map<Glib::ustring, std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark>>> marks;
  do {
    std::cout << "word: " << info.fetch(1) << std::endl;
    for (auto kw : keywords) {
      if (info.fetch(1) == kw) {
        int start_offset, end_offset;
        info.fetch_pos(1, start_offset, end_offset);
        std::cout << info.fetch(1) << " (at: [" << start_offset << ";" << end_offset << "])" << std::endl;
        marks["keyword"] = std::make_pair(
          this->m_buffer->create_mark(
            this->m_buffer->get_iter_at_offset(start.get_offset() + start_offset)
          ),
          this->m_buffer->create_mark(
            this->m_buffer->get_iter_at_offset(start.get_offset() + end_offset)
          )
        );
      }
    }
  } while(info.next());

  for (auto mark : marks) {
    this->m_buffer->apply_tag_by_name(mark.first, 
      mark.second.first->get_iter(), mark.second.second->get_iter());
  }
}

So the flow is that I create a simple regexp that should match every word in that line, and then create a map of marks that will later give ranges for which tags will be set. I’m using Gtk::Mark here, because iterators are invalidated with each modification to the buffer.

To illustrate what’s wrong here, I will post some debug output from this function, and a slot on_insert before;

void MainWindow::on_insert(const Gtk::TextBuffer::iterator& pos,
  const Glib::ustring& text, int bytes)
{
  std::cout << text << " (added at[" << pos.get_offset() <<
  "]; with [" << bytes << "]bytes)" << std::endl << std::endl;

So the output of writing class class to the TextView results in first one being highlighted, and the second one not picked up, log:

c (added at[1]; with [1]bytes)

word: c
l (added at[2]; with [1]bytes)

word: cl
a (added at[3]; with [1]bytes)

word: cla
s (added at[4]; with [1]bytes)

word: clas
s (added at[5]; with [1]bytes)

word: class
class (keyword at: [0;5])
  (added at[6]; with [1]bytes)

word: class
class (keyword at: [0;5])
word: r
c (added at[7]; with [1]bytes)

word: class
class (keyword at: [0;5])
word: rd
l (added at[8]; with [1]bytes)

word: class
class (keyword at: [0;5])
word: rd
a (added at[9]; with [1]bytes)

word: class
class (keyword at: [0;5])
word: rd
word: a
s (added at[10]; with [1]bytes)

word: class
class (keyword at: [0;5])
word: rd
word: as
s (added at[11]; with [1]bytes)

word: class
class (keyword at: [0;5])
word: rd
word: ass

It’s easy to notice that the last line shows that it’s moved by two offsets. Could it be that the tag is applied. Also, what is not clear here, the: word: rd. I use keyword as the name of the tag. And back when this code was still using iterators, the info.fetch(1) returned "keyword", so could it be that regex is matching tags also?

I hope someone with experience in Glib and Gtk knows the answer, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:39:44+00:00Added an answer on June 9, 2026 at 9:39 pm

    I’ve not used this particular API yet1, but I think you have an issue with object lifetime. iter->get_visible_text() is returning a string object which is destroyed after the call to regex->match(). This is a problem because Glib::MatchInfo::next() expects that string to still exist2. This is probably why you get garbage for the second match. I think you’ll be safe doing something like this:

    ....
    auto visbuf = start.get_visible_text(end); 
    auto ok = regex->match(visbuf, info);  // existing line
    ...
    
    1. So I could be full of crap.
    2. From Glib::MatchInfo::next() documentation: The match is done on the string passed to the match function, so you cannot free it before calling this function.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.