我目前可以使用 sgml-pretty-print 在 emacs 中漂亮地打印一个 xml 文件,但这是一个手动过程:
- M-<
- C空间
- 米->
- Mx sgml-漂亮的打印
我希望这会自动发生(或者至少有一些选择这样做)。我是 emacs/elisp 的新手,不明白如何:
- 当您打开文件时,emacs 知道要运行什么代码(这是否从 files.el 开始?)
- 如果您想用自己的代码覆盖该代码,该怎么做
这应该为您解决问题:
(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-hook、point-min (-max) 和major-mode。
如果您想了解有关 elisp 的更多信息,可以查看这个问题,它提供了一些关于如何解决问题的指示。
Trey Jackson 的答案的一个稍微简单的替代方案。只需将其添加到您的~/.emacs
文件中:
(add-hook 'sgml-mode-hook #'(lambda ()
(sgml-pretty-print (point-min) (point-max))))