1

I'm working with source code on an SMB NAS, and due to network delay, the actual mtime is sometimes a bit later than when Emacs initiated the save when saving. This caused Emacs to mistake the file as being modified by another program. (I don't know the actual mtime is, but I suspect this may be the problem.)

The problem is that: when I save the file, I often repeatedly press C-x C-s sequence quickly (I'm having CUA enabled) - a habit I have to be certain that the files are saved. And the minibuffer prompts that the buffer was modified, and ask me "yes/no" question of whether to overwrite the file, which I'm certain that I can safely do.

Is there a way I can set grace period (e.g. 2 seconds) that Emacs do not consider it modified by an external program?

6
  • There is a lot of interpretation in your question but no hard facts: what exactly was the problem? Was there a problem at all or you just noticed that the mtime of the file is a second later than it should be? How do you know when Emacs saved the file? Providing any error message(s) that Emacs printed out or describing exactly what happened would go a long way towards giving us an idea about what the problem is. The best way to ask a question is to describe exactly what you did, the result you got and the result you expected. That way, we can try to reproduce the problem on our own. Commented Jul 24 at 2:02
  • 1
    Added a bit of detail, I'm not sure if you can reproduce this on every NAS you have, I'm using UGREEN-DX4600-Plus @NickD Commented Jul 24 at 2:07
  • If you're comfortable writing your own solution, (info "(elisp)Modification Time") provides the relevant documentation. Commented Jul 24 at 8:05
  • @phils I may try that first. Could you tell me where might I add a hook or something (e.g. called before file save)? What are the relevant API reference pages? Commented Jul 24 at 10:31
  • (info "(elisp)Saving Buffers") Commented Jul 24 at 10:58

1 Answer 1

2

Managed it.

Add this to your customization file:

(defun mtime-grace-delay () "If the mtime of the file and buffer are within 1 seconds' difference, clear the buffer's modtime." (when (time-less-p (visited-file-modtime) (time-add (nth 5 (file-attributes (buffer-file-name))) 1)) (clear-visited-file-modtime))) (add-hook 'before-save-hook 'mtime-grace-delay) 

You can change the 1 at time-add (... (buffer-file-name))) >>1<<)) to any delay you want (in seconds).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.