我刚刚在文本文件中丢失了笔记的大部分内容,当我使用emacs时,有一个错误,但我忽略了错误消息,并强制保存文件。几个小时后,我发现我新保存的文件只剩下几个字节了!我在文件中的大部分笔记都已消失。
我希望在emacs中保存文件时,我已经设置了文件大小更改的警告。我以前在emacs之前看过这样的消息,但我不知道我现在怎么设置它?请与我分享指向解决方案的任何指针。最糟糕的是,应该可以在钩子中添加一些检查以保存文件以实现它。
提前致谢!
答案 0 :(得分:6)
根据您想要的警告类型,这可以解决您的问题。当前的增量为8M,显然您可以根据需要自定义。如果您想要更具侵入性的警告,可以取消注释'y-or-n-p
,并删除该消息。
(defvar check-buffer-size-delta (* 8 1024 1024)
"Delta in size over which the user will be warned when saving.")
(defun check-buffer-size-on-save ()
"Warn user if buffer has changed by more than CHECK-BUFFER-SIZE-DELTA since last save"
(let ((delta (abs (- (buffer-size) buffer-saved-size))))
(when (> delta check-buffer-size-delta)
(beep)
;; (y-or-n-p "WARNING: Buffer has changed by %d bytes since last save. Do you know what you're doing? "
(message "WARNING: Buffer has changed by %d bytes since last save." delta))))
(add-hook 'before-save-hook 'check-buffer-size-on-save)
答案 1 :(得分:4)