Writestamps
Inserting the current date and time automatically and in such a configurable format is pretty neat and probably beyond the ken of most text editors, but its usefulness is limited. Undoubtedly more useful would be the ability to store a writestamp in a file; that is, the date and/or time the file was last written to disk. A writestamp updates itself each time the file is saved anew.
Updating Writestamps
The first thing we'll need is a way to run our writestamp-updating code each time the file is saved. As we discovered in the section Hooks in Chapter 2, the best way to associate some code with a common action (such as saving a file) is by adding a function to a hook variable, provided that a suitable hook variable exists. Using M-x apropos RET hook RET, we discover four promising hook variables: after-save-hook, local-write-file-hooks, write-contents-hooks, and write-file-hooks.
We can discard after-save-hook right away. We don't want our code executed, modifying writestamps, after the file is saved, because then it will be impossible to save an up-to-date version of the file!
The differences between the remaining candidates are subtle:
write-file-hooksCode to execute for any buffer each time it is saved.
local-write-file-hooksA buffer-local version of
write-file-hooks. Recall from the Hooks section of Chapter 2 that a buffer-local variable is one that can have different values in different buffers. Whereaswrite-file-hookspertains to every buffer,local-write-file-hooks
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access