3

我目前可以使用 sgml-pretty-print 在 emacs 中漂亮地打印一个 xml 文件,但这是一个手动过程:

  1. M-<
  2. C空间
  3. 米->
  4. Mx sgml-漂亮的打印

我希望这会自动发生(或者至少有一些选择这样做)。我是 emacs/elisp 的新手,不明白如何:

  1. 当您打开文件时,emacs 知道要运行什么代码(这是否从 files.el 开始?)
  2. 如果您想用自己的代码覆盖该代码,该怎么做
4

2 回答 2

6

这应该为您解决问题:

(add-hook 'find-file-hook 'my-sgml-find-file-hook)
(defun my-sgml-find-file-hook ()
  "run sgml pretty-print on the file when it's opened (if it's sgml)"
  (when (eq major-mode 'sgml-mode)
    (sgml-pretty-print (point-min) (point-max))))

关键信息是find-file-hookpoint-min (-max) 和major-mode

如果您想了解有关 elisp 的更多信息,可以查看这个问题,它提供了一些关于如何解决问题的指示。

于 2009-05-20T19:12:17.030 回答
4

Trey Jackson 的答案的一个稍微简单的替代方案。只需将其添加到您的~/.emacs文件中:

(add-hook 'sgml-mode-hook #'(lambda ()
  (sgml-pretty-print (point-min) (point-max))))
于 2009-05-20T19:16:59.187 回答