【问题标题】:Customize GNU/Emacs nxml-mode indentation自定义 GNU/Emacs nxml-mode 缩进
【发布时间】:2011-12-13 18:26:15
【问题描述】:

作为一些使用 GNU/Emacs 为 Android 开发的人,你肯定知道最新的 Android 工具引入了new xml formatter。我使用出色的 nxml 模式来编辑 xml,因为……我编辑 xml 文件;)我对它非常满意,但是……因为我可以自定义 Nxml 属性缩进变量,文档说:

Indentation for the attributes of an element relative to the start-tag. Hide   
This only applies when the first attribute of a tag starts a line.
In other cases, the first attribute on one line is indented the same
as the first attribute on the previous line.

重要的是当独立属性与元素在同一行时,独立属性与第一个属性对齐的后备。

是否可以更改该行为,以获得与 Android 工具兼容的缩进?我只是在文档中一无所获,谷歌搜索失败了……

更新:

评论帮助我意识到我并不清楚。因此,这是一个 nxml-mode 默认执行的示例:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="org.foo.bar"
          android:versionCode="1"
          android:versionName="1.0">

  <uses-sdk android:minSdkVersion="8" />
  <application
      android:label="@string/app_name"
      android:icon="@drawable/ic_launcher">

    <activity
        android:name="Foo"
        android:label="@string/foo" />

    <activity android:name="Bar"
              android:label="@string/bar" />

  </application>

</manifest>

我想得到什么:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.foo.bar"
    android:versionCode="1"
    android:versionName="1.0">

  <uses-sdk android:minSdkVersion="8" />
  <application
      android:label="@string/app_name"
      android:icon="@drawable/ic_launcher">

    <activity
        android:name="Foo"
        android:label="@string/foo" />

    <activity android:name="Bar"
        android:label="@string/bar" />

  </application>

</manifest>

第一种情况(默认的 nxml 模式缩进行为):

  • manifest 元素的package 属性与xmlns:android decl 对齐
  • Bar activity 元素的android:label 属性与android:name 元素对齐。

第二种情况(预期结果):

  • manifest 元素的 package 属性与父 manifest 元素以及可配置数量的空格对齐
  • Bar activity 元素的android:label 属性与父元素以及可配置数量的空格对齐

我浏览了 nxml-mode 源代码,缩进行为以 nxml-indent-line 开头,但由于我缺乏 lisp 知识,我未能遵循许多子调用来查看应该自定义 defun 的内容。

您可以看到manifest 第二个属性与第一个不对齐

干杯,

Renaud(为了符合 Android 编码和格式规则而感到头疼)

【问题讨论】:

  • 您能展示一些 XML 示例吗?我不明白您所说的“当独立属性与元素位于同一行时,独立属性与第一个属性对齐的后备”是什么意思。
  • 用 XML sn-ps 更新的问题 ;) 对不起,我应该把它们放在第一位……

标签: emacs dot-emacs


【解决方案1】:

这种行为看起来不容易修改,因为它似乎被硬编码到nxml-compute-indent-in-start-tag 函数中。相关的代码块似乎是这样的:

              (let* ((att (car atts))
                     (start (xmltok-attribute-name-start att)))
                (when (< start pos)
                  (goto-char start)
                  (setq off 0))))

您总是可以将该方法复制到您自己的 init 文件中,将这些行注释掉,然后在 nxml 模式加载后加载您的函数定义(这将覆盖原始实现)。

请注意,您可能还想向 gnu emacs 维护人员提交增强请求,以便将来轻松自定义此行为。

【讨论】:

  • 谢谢,我会尝试并报告如果我设法让它工作(并验证你的答案;))
猜你喜欢
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 2011-02-16
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多