1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
3 ;; Copyright 1989, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Maintainer's Time-stamp: <1996-08-13 14:03:17 gildea>
6 ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; A template in a file can be updated with a new time stamp when
27 ;; you save the file. For example:
28 ;; static char *ts = "sdmain.c Time-stamp: <1996-08-13 10:20:51 gildea>";
29 ;; See the top of `time-stamp.el' for another example.
31 ;; To use time-stamping, add this line to your .emacs file:
32 ;; (add-hook 'write-file-hooks 'time-stamp)
33 ;; Now any time-stamp templates in your files will be updated automatically.
35 ;; See the documentation for the functions `time-stamp'
36 ;; and `time-stamp-toggle-active' for details.
40 ;; Originally based on the 19 Dec 88 version of
41 ;; date.el by John Sturdy <mcvax!harlqn.co.uk!jcgs@uunet.uu.net>
42 ;; Version 2, January 1995: replaced functions with %-escapes
43 ;; $Id: time-stamp.el,v 1.19 1996/09/22 22:20:58 kwzh Exp rms $
47 (defvar time-stamp-active t
48 "*Non-nil to enable time-stamping of buffers by \\[time-stamp].
49 Can be toggled by \\[time-stamp-toggle-active].
50 See also the variable `time-stamp-warn-inactive'.")
52 (defvar time-stamp-warn-inactive t
53 "Non-nil to have \\[time-stamp] warn if a buffer did not get time-stamped.
54 A warning is printed if `time-stamp-active' is nil and the buffer contains
55 a time stamp template that would otherwise have been updated.")
57 (defvar time-stamp-old-format-warn 'ask
58 "Action to take if `time-stamp-format' is an old-style list.
59 If 'error, the format is not used. If 'ask, the user is queried about
60 using the time-stamp-format. If 'warn, a warning is displayed.
61 If nil, no notification is given.")
63 (defvar time-stamp-format "%y-%02m-%02d %02H:%02M:%02S %u"
64 "*Format of the string inserted by \\[time-stamp].
65 Value may be a string or a list. (Lists are supported only for
66 backward compatibility; see variable `time-stamp-old-format-warn'.)
67 A string is used verbatim except for character sequences beginning with %:
69 %a weekday name: `Monday'. %A gives uppercase: `MONDAY'
70 %b month name: `January'. %B gives uppercase: `JANUARY'
76 %p `am' or `pm'. %P gives uppercase: `AM' or `PM'
78 %w day number of week, Sunday is 0
80 %z time zone name: `est'. %Z gives uppercase: `EST'
83 %% a literal percent character: `%'
84 %f file name without directory %F gives absolute pathname
89 Decimal digits between the % and the type character specify the
90 field width. Strings are truncated on the right; numbers on the left.
91 A leading zero causes numbers to be zero-filled.
93 For example, to get the format used by the `date' command,
94 use \"%3a %3b %2d %02H:%02M:%02S %Z %y\"")
98 ;;; Do not change time-stamp-line-limit, time-stamp-start, or
99 ;;; time-stamp-end in your .emacs or you will be incompatible
100 ;;; with other people's files! If you must change them,
101 ;;; do so only in the local variables section of the file itself.
104 (defvar time-stamp-line-limit 8 ;Do not change!
105 "Lines of a file searched; positive counts from start, negative from end.
106 The patterns `time-stamp-start' and `time-stamp-end' must be found on one
107 of the first (last) `time-stamp-line-limit' lines of the file for the
108 file to be time-stamped by \\[time-stamp].
110 Do not change `time-stamp-line-limit', `time-stamp-start', or
111 `time-stamp-end' for yourself or you will be incompatible
112 with other people's files! If you must change them for some application,
113 do so in the local variables section of the time-stamped file itself.")
116 (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change!
117 "Regexp after which the time stamp is written by \\[time-stamp].
118 See also the variables `time-stamp-end' and `time-stamp-line-limit'.
120 Do not change `time-stamp-line-limit', `time-stamp-start', or
121 `time-stamp-end' for yourself or you will be incompatible
122 with other people's files! If you must change them for some application,
123 do so in the local variables section of the time-stamped file itself.")
126 (defvar time-stamp-end "\\\\?[\">]" ;Do not change!
127 "Regexp marking the text after the time stamp.
128 \\[time-stamp] deletes the text between the first match of `time-stamp-start'
129 and the following match of `time-stamp-end' on the same line,
130 then writes the time stamp specified by `time-stamp-format' between them.
132 Do not change `time-stamp-line-limit', `time-stamp-start', or
133 `time-stamp-end' for yourself or you will be incompatible
134 with other people's files! If you must change them for some application,
135 do so in the local variables section of the time-stamped file itself.")
141 "Update the time stamp string in the buffer.
142 A template in a file can be automatically updated with a new time stamp
143 every time you save the file. Add this line to your .emacs file:
144 (add-hook 'write-file-hooks 'time-stamp)
145 Normally the template must appear in the first 8 lines of a file and
146 look like one of the following:
149 The time stamp is written between the brackets or quotes:
150 Time-stamp: <1996-07-18 10:20:51 gildea>
151 Only updates the time stamp if the variable `time-stamp-active' is non-nil.
152 The format of the time stamp is set by the variable `time-stamp-format'.
153 The variables `time-stamp-line-limit', `time-stamp-start',
154 and `time-stamp-end' control finding the template."
156 (let ((case-fold-search nil)
163 (cond ((> time-stamp-line-limit 0)
164 (goto-char (setq start (point-min)))
165 (forward-line time-stamp-line-limit)
166 (setq search-limit (point)))
168 (goto-char (setq search-limit (point-max)))
169 (forward-line time-stamp-line-limit)
170 (setq start (point))))
172 (while (and (< (point) search-limit)
174 (re-search-forward time-stamp-start search-limit 'move))
177 (let ((line-end (point)))
179 (if (re-search-forward time-stamp-end line-end 'move)
180 (setq end (match-beginning 0)))))))
183 ;; do all warnings outside save-excursion
185 ((not time-stamp-active)
186 (if time-stamp-warn-inactive
187 ;; don't signal an error in a write-file-hook
189 (message "Warning: time-stamp-active is off; did not time-stamp buffer.")
191 ((not (and (stringp time-stamp-start)
192 (stringp time-stamp-end)))
193 (message "time-stamp-start or time-stamp-end is not a string")
196 (let ((new-time-stamp (time-stamp-string)))
197 (if (stringp new-time-stamp)
201 (delete-region start end)
203 (insert new-time-stamp)
205 ;; remove any tabs used to format time stamp
207 (if (search-forward "\t" end t)
208 (untabify start end)))))))))))
209 ;; be sure to return nil so can be used on write-file-hooks
213 (defun time-stamp-toggle-active (&optional arg)
214 "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer.
215 With arg, turn time stamping on if and only if arg is positive."
217 (setq time-stamp-active
219 (not time-stamp-active)
220 (> (prefix-numeric-value arg) 0)))
221 (message "time-stamp is now %s." (if time-stamp-active "active" "off")))
224 (defun time-stamp-string ()
225 "Generate the new string to be inserted by \\[time-stamp]."
226 (if (stringp time-stamp-format)
227 (time-stamp-strftime time-stamp-format)
228 ;; handle version 1 compatibility
229 (cond ((or (eq time-stamp-old-format-warn 'error)
230 (and (eq time-stamp-old-format-warn 'ask)
231 (not (y-or-n-p "Use non-string time-stamp-format? "))))
232 (message "Warning: no time-stamp: time-stamp-format not a string")
236 (cond ((eq time-stamp-old-format-warn 'warn)
237 (message "Obsolescent time-stamp-format type; should be string")
239 (time-stamp-fconcat time-stamp-format " ")))))
241 (defconst time-stamp-month-numbers
242 '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4) ("May" . 5) ("Jun" . 6)
243 ("Jul" . 7) ("Aug" . 8) ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
244 "Alist of months and their number.")
246 (defconst time-stamp-month-full-names
247 ["(zero)" "January" "February" "March" "April" "May" "June"
248 "July" "August" "September" "October" "November" "December"])
250 (defconst time-stamp-weekday-numbers
251 '(("Sun" . 0) ("Mon" . 1) ("Tue" . 2) ("Wed" . 3)
252 ("Thu" . 4) ("Fri" . 5) ("Sat" . 6))
253 "Alist of weekdays and their number.")
255 (defconst time-stamp-weekday-full-names
256 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"])
258 (defconst time-stamp-am-pm '("am" "pm")
259 "List of strings used to denote morning and afternoon.")
261 (defconst time-stamp-no-file "(no file)"
262 "String to use when the buffer is not associated with a file.")
264 (defun time-stamp-strftime (format &optional time)
265 "Uses a FORMAT to format date, time, file, and user information.
266 Optional second argument TIME will be used instead of the current time.
267 See the documentation of the variable `time-stamp-format' for a description
268 of the format string."
269 (let ((time-string (cond ((stringp time)
272 (current-time-string time))
274 (current-time-string))))
275 (fmt-len (length format))
284 (while (< ind fmt-len)
285 (setq cur-char (aref format ind))
291 (setq field-index (1+ ind))
294 (setq cur-char (if (< ind fmt-len)
297 (and (<= ?0 cur-char) (>= ?9 cur-char))))
298 (setq field-width (substring format field-index ind))
299 ;; eat any additional args to allow for future expansion
300 (while (or (and (<= ?0 cur-char) (>= ?9 cur-char)) (eq ?. cur-char)
301 (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char)
302 (eq ?- cur-char) (eq ?+ cur-char)
303 (eq ?\ cur-char) (eq ?# cur-char)
304 (and (eq ?\( cur-char)
305 (not (eq prev-char ?\\))
306 (setq paren-level (1+ paren-level)))
307 (if (and (eq ?\) cur-char)
308 (not (eq prev-char ?\\))
310 (setq paren-level (1- paren-level))
311 (and (> paren-level 0)
314 (setq prev-char cur-char)
315 (setq cur-char (if (< ind fmt-len)
322 ((or (eq cur-char ?a) ;weekday name
325 (aref time-stamp-weekday-full-names
326 (cdr (assoc (substring time-string 0 3)
327 time-stamp-weekday-numbers)))))
331 ((or (eq cur-char ?b) ;month name
334 (aref time-stamp-month-full-names
335 (cdr (assoc (substring time-string 4 7)
336 time-stamp-month-numbers)))))
340 ((eq cur-char ?d) ;day of month, 1-31
341 (string-to-int (substring time-string 8 10)))
342 ((eq cur-char ?H) ;hour, 0-23
343 (string-to-int (substring time-string 11 13)))
344 ((eq cur-char ?I) ;hour, 1-12
345 (let ((hour (string-to-int (substring time-string 11 13))))
352 ((eq cur-char ?m) ;month number, 1-12
353 (cdr (assoc (substring time-string 4 7)
354 time-stamp-month-numbers)))
355 ((eq cur-char ?M) ;minute, 0-59
356 (string-to-int (substring time-string 14 16)))
357 ((or (eq cur-char ?p) ;am or pm
360 (if (> 12 (string-to-int (substring time-string 11 13)))
361 (car time-stamp-am-pm)
362 (car (cdr time-stamp-am-pm)))))
366 ((eq cur-char ?S) ;seconds, 00-60
367 (string-to-int (substring time-string 17 19)))
368 ((eq cur-char ?w) ;weekday number, Sunday is 0
369 (cdr (assoc (substring time-string 0 3) time-stamp-weekday-numbers)))
370 ((eq cur-char ?y) ;year
371 (string-to-int (substring time-string -4)))
372 ((or (eq cur-char ?z) ;time zone
375 (if (fboundp 'current-time-zone)
376 (car (cdr (current-time-zone time))))))
377 (or name (setq name ""))
381 ((eq cur-char ?f) ;buffer-file-name, base name only
383 (file-name-nondirectory buffer-file-name)
385 ((eq cur-char ?F) ;buffer-file-name, full path
388 ((eq cur-char ?s) ;system name
390 ((eq cur-char ?u) ;user name
392 ((eq cur-char ?h) ;mail host name
393 (time-stamp-mail-host-name))
395 (if (string-equal field-width "")
398 (format (format "%%%s%c"
400 (if (numberp field-result) ?d ?s))
401 (or field-result ""))))
402 (let ((initial-length (length padded-result))
403 (desired-length (string-to-int field-width)))
404 (if (> initial-length desired-length)
405 ;; truncate strings on right, numbers on left
406 (if (stringp field-result)
407 (substring padded-result 0 desired-length)
408 (substring padded-result (- desired-length)))
411 (char-to-string cur-char)))))
415 (defun time-stamp-mail-host-name ()
416 "Return the name of the host where the user receives mail.
417 This is the value of `mail-host-address' if bound and a string,
418 otherwise the value of `time-stamp-mail-host' (for versions of Emacs
419 before 19.29) otherwise the value of the function system-name."
420 (or (and (boundp 'mail-host-address)
421 (stringp mail-host-address)
423 (and (boundp 'time-stamp-mail-host) ;for backward compatibility
424 (stringp time-stamp-mail-host)
425 time-stamp-mail-host)
428 ;;; the rest of this file is for version 1 compatibility
430 (defun time-stamp-fconcat (list sep)
431 "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
432 If an element of LIST is a symbol, it is funcalled to get the string to use;
433 the separator SEP is used between two strings obtained by funcalling a
434 symbol. Otherwise the element itself is inserted; no separator is used
436 (let ((return-string "")
439 (cond ((symbolp (car list))
441 (setq return-string (concat return-string sep)))
442 (setq return-string (concat return-string (funcall (car list))))
443 (setq insert-sep-p t))
445 (setq return-string (concat return-string (car list)))
446 (setq insert-sep-p nil)))
447 (setq list (cdr list)))
450 ;;; Some functions used in time-stamp-format
452 ;;; Could generate most of a message-id with
453 ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name)
455 ;;; pretty form, suitable for a title page
457 (defun time-stamp-month-dd-yyyy ()
458 "Return the current date as a string in \"Month DD, YYYY\" form."
459 (let ((date (current-time-string)))
461 (aref time-stamp-month-full-names
462 (cdr (assoc (substring date 4 7) time-stamp-month-numbers)))
463 (string-to-int (substring date 8 10))
464 (substring date -4))))
466 (defun time-stamp-dd/mm/yyyy ()
467 "Return the current date as a string in \"DD/MM/YYYY\" form."
468 (let ((date (current-time-string)))
469 (format "%02d/%02d/%s"
470 (string-to-int (substring date 8 10))
471 (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
472 (substring date -4) )))
474 ;;; same as __DATE__ in ANSI C
476 (defun time-stamp-mon-dd-yyyy ()
477 "Return the current date as a string in \"Mon DD YYYY\" form.
478 The first character of DD is space if the value is less than 10."
479 (let ((date (current-time-string)))
482 (string-to-int (substring date 8 10))
483 (substring date -4))))
487 (defun time-stamp-dd-mon-yy ()
488 "Return the current date as a string in \"DD Mon YY\" form."
489 (let ((date (current-time-string)))
491 (string-to-int (substring date 8 10))
493 (substring date -2))))
497 (defun time-stamp-yy/mm/dd ()
498 "Return the current date as a string in \"YY/MM/DD\" form."
499 (let ((date (current-time-string)))
500 (format "%s/%02d/%02d"
502 (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
503 (string-to-int (substring date 8 10)))))
507 (defun time-stamp-yyyy/mm/dd ()
508 "Return the current date as a string in \"YYYY/MM/DD\" form."
509 (let ((date (current-time-string)))
510 (format "%s/%02d/%02d"
512 (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
513 (string-to-int (substring date 8 10)))))
517 (defun time-stamp-yyyy-mm-dd ()
518 "Return the current date as a string in \"YYYY-MM-DD\" form."
519 (let ((date (current-time-string)))
520 (format "%s-%02d-%02d"
522 (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
523 (string-to-int (substring date 8 10)))))
525 (defun time-stamp-yymmdd ()
526 "Return the current date as a string in \"YYMMDD\" form."
527 (let ((date (current-time-string)))
530 (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
531 (string-to-int (substring date 8 10)))))
533 (defun time-stamp-hh:mm:ss ()
534 "Return the current time as a string in \"HH:MM:SS\" form."
535 (substring (current-time-string) 11 19))
537 (defun time-stamp-hhmm ()
538 "Return the current time as a string in \"HHMM\" form."
539 (let ((date (current-time-string)))
540 (concat (substring date 11 13)
541 (substring date 14 16))))
543 (provide 'time-stamp)
545 ;;; time-stamp.el ends here