3

I'm trying to tweak a gedit style so that user-defined functions have a different colour.

I've searched through http://library.gnome.org/devel/gtksourceview-2.0/stable/lang-reference.html but I couldn't find anything.

I thought <style name="def:function" /> might do it, but it seems have no effect in gedit.

<?xml version="1.0" ?>
<style-scheme id="wombat" name="Wombat" version="1.0">
        <author/>
        <_description>Wombat theme</_description>
        <style background="#2d2d2d" name="current-line"/>
        <style background="#857b6f" bold="true" foreground="#fff000" name="bracket-match"/>
        <style background="#242424" bold="true" foreground="#fff000" name="search-match"/>
        <style background="#656565" name="cursor"/>
        <style background="#242424" foreground="#f6f3e8" name="text"/>
        <style background="#272727" foreground="#857b6f" name="line-numbers"/>
        <style foreground="#363636" italic="true" name="def:comment"/>
        <style foreground="#e5786d" name="def:constant"/>
        <style foreground="#95e454" italic="true" name="def:string"/>
        <style foreground="#cae682" name="def:identifier"/>
        <style foreground="#000000" name="def:function"/>
        <style foreground="#cae682" name="def:type"/>
        <style foreground="#8ac6f2" name="def:statement"/>
        <style foreground="#8ac6f2" name="def:keyword"/>
        <style foreground="#e5786d" name="def:preprocessor"/>
        <style foreground="#e5786d" name="def:number"/>
        <style foreground="#e7f6da" name="def:specials"/>
    </style-scheme>

Any hints? Thanks!

John Lyon
  • 11,180
  • 4
  • 36
  • 44
Danny King
  • 1,941
  • 6
  • 24
  • 37
  • do you want the `foo` in `def foo(a,b,c):` to be highlighted in a different colour? That's my understanding and also what I'm after. – John Lyon May 30 '12 at 00:23
  • I just noticed that I assumed you're using Python. I'm sure you could come up with a similar rule for whatever language you're trying to do this for. – John Lyon May 30 '12 at 05:04

2 Answers2

1

You need to edit the language definition file to add a new section. The language definition is python.lang and for me is in /usr/share/gtksourceview-2.0/language-specs.

You first need to add a style for the style id you're going to create:

<style id="class-name"    _name="Python Class Name"  map-to="def:type"/>

You then need to add a new context to this file under the <context-id="python" section:

<context id="class-name" style-ref="class-name" style-inside="true">
    <start>\%{string-prefix}def\ </start>
    <end>\(</end>
    <include>
        <context ref="python"/>
        <context ref="format"/>
        <context ref="escaped-char"/>
    </include>
</context>

You need the style-inside="true" to not apply styling to the def or ( that we match on. From the docs:

style-inside (optional) If this attribute is "true", then the highlighting style will be applied to the area between start and end matches; otherwise whole context will be highlighted.

Save that, then restart gedit and the function name should be styled the same as an error, e.g. the text of AttributeError would be. You can change the map-to line at the top of the language file to change the style applied to your function names.

The benefit of reusing an existing style rather than defining a new one for class names is that it will work for all of the gedit themes you install in future - they don't need to be altered to add a python-specific function name section.


Edit: As noted in the comments, this nukes the styling on "def". Moving the "class-name" section (my code above) to below the "keywords" section that already exists, fixes this as it moves our change lower in the hierarchy. Will update the image when I get a chance.

Before: enter image description here

After: enter image description here

John Lyon
  • 11,180
  • 4
  • 36
  • 44
  • You'll notice, however, that this removes the highlighting from the 'def' keyword. Not quite sure how to fix that, but you might want to take a look at Vim or Sublime Text's language definitions. – ptomato May 30 '12 at 08:28
  • The answer offered by @jozzas is good for Gtksourceview 2.0 but allow me to spare you a pounding headache for version 3.0: – venzen Jul 10 '16 at 12:19
  • The – venzen Jul 10 '16 at 12:25
0

if i understand you correctly first of all you need define new style in your *.lang file

<style id="function" _name="Functions"/>

then make list of all needed functions like

<context id="functions" style-ref="function">
    <keyword>abs</keyword>
    <keyword>acos</keyword>
    <keyword>acosh</keyword>
</context>

then update block definitions at the end of the file, and add new context

<context ref="functions"/>

its very important correct order, context definition order should bee the same in all file, one element allways gos after another, dont mix them up.

and at the end you need to define style formmat in your styles/*.xml file, in mine case it was

<style name="php:function" foreground="#0000ee" bold="false"/>

where i used php should be your language id, same as in *.lang file.

  • 1
    No, you would need to match on "def" as I believe the poster wants the `foo` in `def foo(a,b,c):` to be highlighted in a different colour. You can't match `foo` as the user can call functions whatever he wants. Youd want a regex that catches anything *after* `def` and before an open bracket `(`. I'm not sure how to do this. I also want this as it helps finding functions within a file easier. – John Lyon May 30 '12 at 00:22