GtkSourceView: how to hide the markup without hiding the text?


Let’s assume I have a custom lang.def file for GtkSourceView that defines a RegEx which renders *some bold text* as * some bold text* . Is it possible to hide the two ‘*’ while keeping some bold text being displayed bold? I want to be able to hide/show all defined markup elements thus making the document more readable (sort of View mode)…

Thank you!

Hi !

GtkSourceView styles don’t support invisible property, so can’t fully hide parts of the text based on the lang definitions.

With lang definitions, the most you can do is to give the * a transparent foreground color, so it won’t be visible, but you’ll see a gap instead…

For example, the current markdown rule for emphasis (bold) text is:

    <context id="asterisks-emphasis" style-ref="emphasis">
      <match>(?&lt;!\*)\*[^\* \t].*?(?&lt;!\\|\*| |\t)\*(?!\*)</match>

You can give different styles to sub-matches as follow (untested!!):

    <context id="asterisks-emphasis">
      <match>(?&lt;!\*)(\*[^\* \t])(.*?)((?&lt;!\\|\*| |\t)\*)(?!\*)</match>
        <context sub-pattern="2" style-ref="invisible"/>
        <context sub-pattern="3" style-ref="emphasis"/>
        <context sub-pattern="4" style-ref="invisible"/>

Then define an “invisible” style with transparent foreground:

  <style name="invisible" foreground="#rgba(0,0,0,0)"/>


Alternatively, instead of using GtkSourceView, you can write your own parser that apply GtkTextTags to your text. It’s much more flexible, as much more text properties are covered by tags.

1 Like

Thank you very much, @gwillems !

  1. What would be faster / easier - try to add invisible property to GtkSourceView style or to write a custom parser for a Markdown-like syntax and use GtkTextTags with regular GtkTextView?

  2. Is there a way to obtain start/end iters of all the context id="asterisks-emphasis" parts found by GtkSourceView instead of writing own parser? I’d then use those iters with GtkTextTag to set extra properties on those parts of text…

  3. Do you have some sample examples of such parsers integrated into GtkTextView?

  4. Is it a good idea to use GLib MarkupParser to implement a Markdown-like syntax? (I saw that at least in the past it was used only for XML markup).

Thank you again!

  1. GtkSourceView was designed for code highlighting, it’s not adapted for advanced text formatting like you’re trying to do. I would try to avoid it, you’ll quickly end up in dead-end for more complex things.
  2. Not really… There are APIs like gtk_text_iter_forward_to_tag_toggle() to find the start/end of a specific tag, but tags created by sourceview have no names so can’t be easily retrieved from the table…
  3. No, sorry… Usually for markup viewing most people would go with html for rendering, as converters for html already exist. For new languages, maybe try to search if someone already wrote some kind of converter, that could help.
  4. GLib.MarkupParser is for XML-like only. If you want to stick with GLib, better look at GLib.Scanner instead. I’m pretty sure some advanced parsing libraries exist out there, that could be integrated in your app.
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.