在emacs中打开xml文件时运行sgml-pretty-print?

Jon*_*Yee 3 emacs elisp

我目前可以使用的SGML漂亮地打印到相当打印在Emacs一个XML文件,但它是一个手动过程:

  1. M- <
  2. C-空间
  3. M->
  4. Mx sgml-pretty-print

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

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

Tre*_*son 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))))
Run Code Online (Sandbox Code Playgroud)

关键信息是find-file-hook,point-min(-max)和major-mode.

如果您想了解更多有关elisp的,你可以看看这个问题,这给如何理出头绪一些指点.