如何在gedit中设置用户定义的函数名称?

时间:2010-11-08 12:45:52

标签: linux gtk syntax-highlighting gedit

我正在尝试调整gedit样式,以便用户定义的函数具有不同的颜色。

我搜索了http://library.gnome.org/devel/gtksourceview-2.0/stable/lang-reference.html,但找不到任何内容。

我认为<style name="def:function" />可能会这样做,但它似乎对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>

任何提示?谢谢!

2 个答案:

答案 0 :(得分:1)

您需要编辑语言定义文件以添加新部分。语言定义为python.lang,对我来说是/usr/share/gtksourceview-2.0/language-specs

首先需要为要创建的样式ID添加样式:

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

然后,您需要在<context-id="python"部分下为此文件添加新的上下文:

<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>

您需要style-inside="true"不对我们匹配的def(应用样式。来自文档:

  

样式内(可选)   如果此属性为“true”,则突出显示样式将应用于开始和结束匹配之间的区域;否则将突出整个背景。

保存,然后重新启动gedit,函数名称的样式应与错误相同,例如AttributeError的文本将是。您可以更改语言文件顶部的map-to行,以更改应用于函数名称的样式。

重用现有样式而不是为类名定义新样式的好处是它将适用于您将来安装的所有gedit主题 - 它们不需要更改以添加特定于python的函数名称部分。


编辑: 正如评论中所指出的那样,这会破坏“def”的造型。将“类名”部分(上面的代码)移动到已存在的“关键字”部分下方,修复此问题,因为它会在层次结构中移动我们的更改。当我有机会时会更新图像。

在: enter image description here

在: enter image description here

答案 1 :(得分:0)

如果我首先正确理解你,你需要在* .lang文件中定义新的样式

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

然后列出所有需要的功能,如

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

然后更新文件末尾的块定义,并添加新的上下文

<context ref="functions"/>

它非常重要的正确顺序,上下文定义顺序应该在所有文件中相同,一个元素一个接一个,不要混淆它们。

最后你需要在styles / * .xml文件中定义样式formmat,在我的情况下它是

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

我使用php应该是你的语言ID,与* .lang文件中的相同。

相关问题