3

Can Emacs determine the time of the last change (or the last edit) done to the current buffer?

The buffer may has been saved or not.

2 Answers 2

5

Use the standard change hooks to record that information: https://www.gnu.org/software/emacs/manual/html_node/elisp/Change-Hooks.html#Change-Hooks

If you record it, make sure it's in a buffer-local variable, so each buffer can have its own value.

Here's a sample implementation:

(defvar buffer-last-change-time nil "The last change time")
(make-variable-buffer-local 'buffer-last-change-time)

(defun buffer-record-last-change-time (beg end len)
  (setq buffer-last-change-time (current-time))
  (message (format-time-string "Recorded last change time as %F %T"
                               buffer-last-change-time)))

(add-to-list 'after-change-functions 'buffer-record-last-change-time)

Evaluate this in any buffer. Sample output:

Recorded last change time as 2015-02-02 16:00:59
Recorded last change time as 2015-02-02 16:01:00
Recorded last change time as 2015-02-02 16:01:01 [4 times]

This can probably be turned into a more general facility, if you're up to the challenge :)

1
  • All right, code added. I hope it helps, but don't be afraid of playing with Emacs a bit. Commented Feb 2, 2015 at 21:05
2

As far as I know, Emacs does not record this information (automatically). As @TedZlatanov suggested, you can record it yourself, however.

What Emacs does record is the number of changes since the buffer was last saved. See functions buffer-modified-tick and buffer-chars-modified-tick.

(I'm not sure how functions set-buffer-modified-p and not-modified affect the tick count, but my guess is that they reset it to 0. This is not specified in the Elisp manual - you would need to consult the C-code implementation to find out.)

For a buffer visiting a file, you can get the info of the last buffer save, and the last file modification, but not the last buffer modification, using function file-attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.