【发布时间】:2021-02-18 01:32:24
【问题描述】:
我正在尝试编写自己的before-save-hook 绑定函数来格式化我的代码。我找到了用于格式化的命令行应用程序。
所以我正在尝试
- 将当前缓冲区复制到临时文件。
- 运行命令行应用程序来格式化临时文件。
- 将其复制回(覆盖)到当前缓冲区。
所以我写了这个函数,但不知道如何将文件内容复制回当前缓冲区。
(defun my-format-before-save ()
(interactive)
(let* ((filename buffer-file-name)
(tempfilename (concat filename ".tmp")))
(unwind-protect
(progn (write-region (point-min) (point-max) tempfilename)
(shell-command-to-string (format "some-format %s" tempfilename))
;;(???) ;; how to copy and over write current buffer with tempfile?
)
(delete-file tempfilename))))
emacs 有这个功能来完全重写当前缓冲区吗? 是否有另一种简洁/elisp 风格的方式来达到我的目标?
谢谢
【问题讨论】:
-
在我看来,Replacing buffer content without losing point 对你来说很有价值。