Using a Save Hook for an Automatically Updating "Last Modified"
Timestamp
There are multiple ways that save hooks on GNU Emacs can be used for
automatically updating timestamps. By default, Emacs will update
timestamps in the first eight lines of a file. The lines would look
like either:
Time-stamp: <>
Or:
Time-stamp: ""
In order for the timestamp to automatically change when you save a
document, you need to add the save hook to your ~/.emacs
file:
(add-hook 'before-save-hook 'time-stamp)
This is not the only way your timestamp can work, especially since it
may be a bit awkward to look at an HTML document. Another way you can
display timestamps would be how I do with my own web pages. To do
this, you'll need to add the below code block to your .emacs
file.
(add-hook 'html-mode-hook (lambda ()
(set (make-local-variable 'time-stamp-pattern)
"-8/+%3a %d %3b %:y %02H:%02M:%02S %p %Z")))
The above code will look in the last 8 lines of the document, and will
add the time stamp in between the below comments:
Notes:
-
HTML mode hook based on the example
from Emacs
wiki.
-
The article's and web site's former use
of <!--LASTMOD--> and <!--END--> were
cribbed
from Simon
Tatham's Home Page. They seemed to be logical names to give to
the start and end indicators.
-
Mentions of ~/ for the .emacs file path refer to your user
home directory.
-
I previously, years ago, tried to use a JavaScript script to do a
similar thing to the Emacs save hook (it would've shown "Last
modified: " for
this web page). I simply do not write last modified dates on the
pages anymore. If you would like to see it still, you may use your
browser's debugging tools to view the connection headers.
-
While the JavaScript timestamps may look easier, especially since they
do not require GNU Emacs to work, they do not work on platforms like
GitHub pages that regenerate the entire web site on every tiny change.
See Dr. O'Neil's web site
if you need proof of this.
Anton McClure /
anton@aperture.nonpaged.com