修改 gedit 语法高亮文件

Osc*_*eig 10 gedit syntax-highlighting

我正在尝试从 Gedit 更改突出显示文件。我修改了文件 /usr/share/gtksourceview-3.0/language-specs/fortran.lang 因为我想改变编辑器将语句作为注释的情况。我遇到的问题是,当我选择新的突出显示方案时,没有任何突出显示,它只是纯文本。

文件 fortran.lang 是用 su 权限打开的,我只是将所有内容复制粘贴到一个新的 Gedit 文件中,然后将其保存为同一文件夹中的 fortran_enhanced.lang。我对原始文件所做的更改如下:

原始 fortran.lang 文件:

<language id="fortran" _name="Fortran 95" version="2.0" _section="Sources">
  <metadata>
    <property name="mimetypes">text/x-fortran</property>
    <property name="globs">*.f;*.f90;*.f95;*.for</property>
    <property name="line-comment-start">!</property>
  </metadata>
  <styles>
    <style id="comment" _name="Comment" map-to="def:comment"/>
    <style id="floating-point" _name="Floating Point" map-to="def:floating-point"/>
    <style id="keyword" _name="Keyword" map-to="def:keyword"/>
    <style id="intrinsic" _name="Intrinsic function" map-to="def:builtin"/>
    <style id="boz-literal" _name="BOZ Literal" map-to="def:base-n-integer"/>
    <style id="decimal" _name="Decimal" map-to="def:decimal"/>
    <style id="type" _name="Data Type" map-to="def:type"/>
  </styles>
  <default-regex-options case-sensitive="false"/>
  <definitions>
    <!-- Note: contains an hack to avoid considering ^COMMON a comment -->
    <context id="line-comment" style-ref="comment" end-at-line-end="true" class="comment" class-disabled="no-spell-check">
      <start>!|(^[Cc](\b|[^OoAaYy]))</start>
      <include>
        <context ref="def:escape"/>
        <context ref="def:in-line-comment"/>
      </include>
    </context>
(...)
Run Code Online (Sandbox Code Playgroud)

修改了 fortran_enhanced.lang 文件:

                     <!-- Note: changed language id and name -->
<language id="fortran_enhanced" _name="Fortran 95 2.0" version="2.0" _section="Sources">
  <metadata>
    <property name="mimetypes">text/x-fortran</property>
                     <!-- Note: removed *.f and *.for from file extensions -->
    <property name="globs">*.f90;*.f95;</property>
    <property name="line-comment-start">!</property>
  </metadata>
  <styles>
    <style id="comment" _name="Comment" map-to="def:comment"/>
    <style id="floating-point" _name="Floating Point" map-to="def:floating-point"/>
    <style id="keyword" _name="Keyword" map-to="def:keyword"/>
    <style id="intrinsic" _name="Intrinsic function" map-to="def:builtin"/>
    <style id="boz-literal" _name="BOZ Literal" map-to="def:base-n-integer"/>
    <style id="decimal" _name="Decimal" map-to="def:decimal"/>
    <style id="type" _name="Data Type" map-to="def:type"/>
  </styles>
  <default-regex-options case-sensitive="false"/>
  <definitions>
                     <!-- Note: I want comments only beginning with !, not C -->
    <context id="line-comment" style-ref="comment" end-at-line-end="true" class="comment" class-disabled="no-spell-check">
      <start>!</start>
      <include>
        <context ref="def:escape"/>
        <context ref="def:in-line-comment"/>
      </include>
    </context>
(...)
Run Code Online (Sandbox Code Playgroud)

我已经阅读了这个问题 [傻瓜的自定义 gedit 语法突出显示?] 我试图使新的 fortran_enhanced.lang 文件可读

$ cd /usr/share/gtksourceview-3.0/language-specs
$ sudo chmod 0644 fortran_enhanced.lang
Run Code Online (Sandbox Code Playgroud)

但它没有任何区别。

我不得不说我以前从来没有做过这样的事情,我什至不理解大部分语言文件,所以我对每一个批评持开放态度,因为我纯粹是由直觉引导的。

先谢谢了!

zpe*_*pea 4

我想我发现你出了什么问题:

解决方案

id您更改了标签中的(and _name) ,这很好,也是正确的<language ...>。毕竟这是您创建的新突出显示方案。

但是,您还必须更改文件中使用此 id 的其他位置。在语言定义的下面(在问题的引用中省略)您会发现:

<context id="fortran" class="no-spell-check">

显然,您必须有一个与您的语言具有相同 id 的上下文,包括/引用所有使用的上下文定义,以便 gedit/GtkSourceView 在选择特定方案时使用它。

我是怎么找到它的

我根本不是这方面的专家。我的全部资格是我以前见过 XML 文件;)所以我只能做出“有根据的”猜测。

给我提示的是从终端窗口启动 gedit 时显示的警告

(gedit:6786):GtkSourceView-警告**:无法加载'/usr/local/share/gtksourceview-3.0/language-specs/frtrn.lang':缺少主语言定义(id =“frtrn”。)

(我在测试时使用“ frtrn ”作为 ID、名称和文件扩展名,您应该会收到与“ fortran_enhanced ”相同的警告)

这让我产生了足够的怀疑,在文件的其余部分中搜索原始 ID。尝试上述解决方案后,我还发现以下行来支持我的解释:

[定义] 这里我们应该定义一个主上下文,即我们在文件开头输入的上下文:为此,我们使用标签,其 id 等于元素的 id [...]

它来自GtkSourceView 文档中的语言定义文件教程。