1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
3 ;; Copyright 1989, 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
5 ;; Maintainer's Time-stamp: <1997-06-08 16:45:41 gildea>
6 ;; Maintainer: Stephen Gildea <gildea@alum.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 (defgroup time-stamp nil
41 "Maintain last change time stamps in files edited by Emacs."
45 (defcustom time-stamp-active t
46 "*Non-nil to enable time-stamping of buffers by \\[time-stamp].
47 Can be toggled by \\[time-stamp-toggle-active].
48 See also the variable `time-stamp-warn-inactive'."
52 (defcustom 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."
59 (defcustom time-stamp-old-format-warn
'ask
60 "Action to take if `time-stamp-format' is an old-style list.
61 If `error', the format is not used. If `ask', the user is queried about
62 using the time-stamp-format. If `warn', a warning is displayed.
63 If nil, no notification is given."
64 :type
'(choice (const :tag
"No notification" nil
)
65 (const :tag
"Don't use the format" error
)
66 (const ask
) (const warn
))
69 (defcustom time-stamp-format
"%:y-%02m-%02d %02H:%02M:%02S %u"
70 "*Format of the string inserted by \\[time-stamp].
71 The value may be a string or a list. Lists are supported only for
72 backward compatibility; see variable `time-stamp-old-format-warn'.
74 A string is used verbatim except for character sequences beginning with %:
76 %:a weekday name: `Monday'. %#A gives uppercase: `MONDAY'
77 %3a abbreviated weekday: `Mon'. %3A gives uppercase: `MON'
78 %:b month name: `January'. %#B gives uppercase: `JANUARY'
79 %3b abbreviated month: `Jan'. %3B gives uppercase: `JAN'
81 %02H 24-hour clock hour
82 %02I 12-hour clock hour
85 %#p `am' or `pm'. %P gives uppercase: `AM' or `PM'
87 %w day number of week, Sunday is 0
88 %02y 2-digit year: `97' %:y 4-digit year: `1997'
89 %z time zone name: `est'. %Z gives uppercase: `EST'
92 %% a literal percent character: `%'
93 %f file name without directory %F gives absolute pathname
98 Decimal digits between the % and the type character specify the
99 field width. Strings are truncated on the right; years on the left.
100 A leading zero causes numbers to be zero-filled.
102 For example, to get the format used by the `date' command,
103 use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\".
105 In the future these formats will be aligned more with format-time-string.
106 Because of this transition, the default padding for numeric formats will
107 change in a future version. Therefore either a padding width should be
108 specified, or the : modifier should be used to explicitly request the
115 ;;; Do not change time-stamp-line-limit, time-stamp-start, or
116 ;;; time-stamp-end in your .emacs or you will be incompatible
117 ;;; with other people's files! If you must change them,
118 ;;; do so only in the local variables section of the file itself.
121 (defvar time-stamp-line-limit
8 ;Do not change!
122 "Lines of a file searched; positive counts from start, negative from end.
123 The patterns `time-stamp-start' and `time-stamp-end' must be found on one
124 of the first (last) `time-stamp-line-limit' lines of the file for the
125 file to be time-stamped by \\[time-stamp].
127 Do not change `time-stamp-line-limit', `time-stamp-start', or
128 `time-stamp-end' for yourself or you will be incompatible
129 with other people's files! If you must change them for some application,
130 do so in the local variables section of the time-stamped file itself.")
133 (defvar time-stamp-start
"Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change!
134 "Regexp after which the time stamp is written by \\[time-stamp].
135 See also the variables `time-stamp-end' and `time-stamp-line-limit'.
137 Do not change `time-stamp-line-limit', `time-stamp-start', or
138 `time-stamp-end' for yourself or you will be incompatible
139 with other people's files! If you must change them for some application,
140 do so in the local variables section of the time-stamped file itself.")
143 (defvar time-stamp-end
"\\\\?[\">]" ;Do not change!
144 "Regexp marking the text after the time stamp.
145 \\[time-stamp] deletes the text between the first match of `time-stamp-start'
146 and the following match of `time-stamp-end' on the same line,
147 then writes the time stamp specified by `time-stamp-format' between them.
149 Do not change `time-stamp-line-limit', `time-stamp-start', or
150 `time-stamp-end' for yourself or you will be incompatible
151 with other people's files! If you must change them for some application,
152 do so in the local variables section of the time-stamped file itself.")
158 "Update the time stamp string in the buffer.
159 A template in a file can be automatically updated with a new time stamp
160 every time you save the file. Add this line to your .emacs file:
161 (add-hook 'write-file-hooks 'time-stamp)
162 Normally the template must appear in the first 8 lines of a file and
163 look like one of the following:
166 The time stamp is written between the brackets or quotes:
167 Time-stamp: <1996-07-18 10:20:51 gildea>
168 The time stamp is updated only if the variable `time-stamp-active' is non-nil.
169 The format of the time stamp is set by the variable `time-stamp-format'.
170 The variables `time-stamp-line-limit', `time-stamp-start',
171 and `time-stamp-end' control finding the template."
173 (let ((case-fold-search nil
)
177 (line-limit time-stamp-line-limit
))
178 (cond ((not (integerp line-limit
))
180 (message "time-stamp-line-limit is not a number")
185 (cond ((> line-limit
0)
186 (goto-char (setq start
(point-min)))
187 (forward-line line-limit
)
188 (setq search-limit
(point)))
190 (goto-char (setq search-limit
(point-max)))
191 (forward-line line-limit
)
192 (setq start
(point))))
194 (while (and (< (point) search-limit
)
196 (re-search-forward time-stamp-start search-limit
'move
))
199 (let ((line-end (point)))
201 (if (re-search-forward time-stamp-end line-end
'move
)
202 (setq end
(match-beginning 0)))))))
205 ;; do all warnings outside save-excursion
207 ((not time-stamp-active
)
208 (if time-stamp-warn-inactive
209 ;; don't signal an error in a write-file-hook
211 (message "Warning: time-stamp-active is off; did not time-stamp buffer.")
213 ((not (and (stringp time-stamp-start
)
214 (stringp time-stamp-end
)))
215 (message "time-stamp-start or time-stamp-end is not a string")
218 (let ((new-time-stamp (time-stamp-string)))
219 (if (stringp new-time-stamp
)
223 (delete-region start end
)
225 (insert new-time-stamp
)
227 ;; remove any tabs used to format time stamp
229 (if (search-forward "\t" end t
)
230 (untabify start end
)))))))))))
231 ;; be sure to return nil so can be used on write-file-hooks
235 (defun time-stamp-toggle-active (&optional arg
)
236 "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer.
237 With arg, turn time stamping on if and only if arg is positive."
239 (setq time-stamp-active
241 (not time-stamp-active
)
242 (> (prefix-numeric-value arg
) 0)))
243 (message "time-stamp is now %s." (if time-stamp-active
"active" "off")))
245 (defconst time-stamp-no-file
"(no file)"
246 "String to use when the buffer is not associated with a file.")
248 ;;; time-stamp is transitioning to using the new, expanded capabilities
249 ;;; of format-time-string. During the process, this function implements
250 ;;; intermediate, compatible formats and complains about old, soon to
251 ;;; be unsupported, formats. This function will get a lot (a LOT) shorter
252 ;;; when the transition is complete and we can just pass most things
253 ;;; straight through to format-time-string.
254 ;;; At all times, all the formats recommended in the doc string
255 ;;; of time-stamp-format will work not only in the current version of
256 ;;; Emacs, but in all versions that have been released within the past
258 ;;; The : modifier is a temporary conversion feature used to resolve
259 ;;; ambiguous formats--formats that are changing (over time) incompatibly.
260 (defun time-stamp-string-preprocess (format &optional time
)
261 ;; Uses a FORMAT to format date, time, file, and user information.
262 ;; Optional second argument TIME is only for testing.
263 ;; Implements non-time extensions to format-time-string
264 ;; and all time-stamp-format compatibility.
265 (let ((fmt-len (length format
))
273 alt-form change-case require-padding
275 (while (< ind fmt-len
)
276 (setq cur-char
(aref format ind
))
282 ;; eat any additional args to allow for future expansion
283 (setq alt-form nil change-case nil require-padding nil
)
286 (setq cur-char
(if (< ind fmt-len
)
290 (eq ?
, cur-char
) (eq ?
: cur-char
) (eq ?
@ cur-char
)
291 (eq ?- cur-char
) (eq ?
+ cur-char
) (eq ?_ cur-char
)
292 (eq ?\ cur-char
) (eq ?
# cur-char
) (eq ?^ cur-char
)
293 (and (eq ?\
( cur-char
)
294 (not (eq prev-char ?
\\))
295 (setq paren-level
(1+ paren-level
)))
296 (if (and (eq ?\
) cur-char
)
297 (not (eq prev-char ?
\\))
299 (setq paren-level
(1- paren-level
))
300 (and (> paren-level
0)
302 (setq prev-char cur-char
)
303 ;; some characters we actually use
304 (cond ((eq cur-char ?
:)
307 (setq change-case t
))))
309 (setq field-index ind
)
313 (setq cur-char
(if (< ind fmt-len
)
316 (and (<= ?
0 cur-char
) (>= ?
9 cur-char
))))
317 (setq field-width
(substring format field-index ind
))
322 ((eq cur-char ?a
) ;day of week
324 (format-time-string "%#A" time
)
325 (or alt-form
(not (string-equal field-width
""))
326 (time-stamp-conv-warn "%a" "%:a"))
327 (if (and alt-form
(not (string-equal field-width
"")))
328 "" ;discourage "%:3a"
329 (format-time-string "%A" time
))))
332 (format-time-string "%A" time
)
333 (or change-case
(not (string-equal field-width
""))
334 (time-stamp-conv-warn "%A" "%#A"))
335 (format-time-string "%#A" time
)))
336 ((eq cur-char ?b
) ;month name
338 (format-time-string "%#B" time
)
339 (or alt-form
(not (string-equal field-width
""))
340 (time-stamp-conv-warn "%b" "%:b"))
341 (if (and alt-form
(not (string-equal field-width
"")))
342 "" ;discourage "%:3b"
343 (format-time-string "%B" time
))))
346 (format-time-string "%B" time
)
347 (or change-case
(not (string-equal field-width
""))
348 (time-stamp-conv-warn "%B" "%#B"))
349 (format-time-string "%#B" time
)))
350 ((eq cur-char ?d
) ;day of month, 1-31
351 (time-stamp-do-number cur-char alt-form field-width time
))
352 ((eq cur-char ?H
) ;hour, 0-23
353 (time-stamp-do-number cur-char alt-form field-width time
))
354 ((eq cur-char ?I
) ;hour, 1-12
355 (time-stamp-do-number cur-char alt-form field-width time
))
356 ((eq cur-char ?m
) ;month number, 1-12
357 (time-stamp-do-number cur-char alt-form field-width time
))
358 ((eq cur-char ?M
) ;minute, 0-59
359 (time-stamp-do-number cur-char alt-form field-width time
))
360 ((eq cur-char ?p
) ;am or pm
362 (time-stamp-conv-warn "%p" "%#p"))
363 (format-time-string "%#p" time
))
364 ((eq cur-char ?P
) ;AM or PM
365 (format-time-string "%p" time
))
366 ((eq cur-char ?S
) ;seconds, 00-60
367 (time-stamp-do-number cur-char alt-form field-width time
))
368 ((eq cur-char ?w
) ;weekday number, Sunday is 0
369 (format-time-string "%w" time
))
370 ((eq cur-char ?y
) ;year
371 (or alt-form
(not (string-equal field-width
""))
372 (time-stamp-conv-warn "%y" "%:y"))
373 (string-to-int (format-time-string "%Y" time
)))
374 ((eq cur-char ?Y
) ;4-digit year, new style
375 (string-to-int (format-time-string "%Y" time
)))
376 ((eq cur-char ?z
) ;time zone lower case
378 "" ;discourage %z variations
379 (format-time-string "%#Z" time
)))
382 (format-time-string "%#Z" time
)
383 (format-time-string "%Z" time
)))
384 ((eq cur-char ?f
) ;buffer-file-name, base name only
386 (file-name-nondirectory buffer-file-name
)
388 ((eq cur-char ?F
) ;buffer-file-name, full path
391 ((eq cur-char ?s
) ;system name
393 ((eq cur-char ?u
) ;user name
395 ((eq cur-char ?h
) ;mail host name
396 (time-stamp-mail-host-name))
398 (if (string-equal field-width
"")
401 (format (format "%%%s%c"
403 (if (numberp field-result
) ?d ?s
))
404 (or field-result
""))))
405 (let ((initial-length (length padded-result
))
406 (desired-length (string-to-int field-width
)))
407 (if (> initial-length desired-length
)
408 ;; truncate strings on right, years on left
409 (if (stringp field-result
)
410 (substring padded-result
0 desired-length
)
412 (substring padded-result
(- desired-length
))
413 padded-result
)) ;non-year numbers don't truncate
416 (char-to-string cur-char
)))))
420 (defun time-stamp-do-number (format-char alt-form field-width time
)
421 ;; Handle a compatible FORMAT-CHAR where only
422 ;; the default width/padding will change.
423 ;; ALT-FORM is whether `#' specified. FIELD-WIDTH is the string
424 ;; width specification or "". TIME is the time to convert.
425 (let ((format-string (concat "%" (char-to-string format-char
))))
426 (and (not alt-form
) (string-equal field-width
"")
427 (time-stamp-conv-warn format-string
428 (format "%%:%c" format-char
)))
429 (if (and alt-form
(not (string-equal field-width
"")))
430 "" ;discourage "%:2d" and the like
431 (string-to-int (format-time-string format-string time
)))))
433 (defvar time-stamp-conversion-warn t
434 "Non-nil to warn about soon-to-be-unsupported forms in time-stamp-format.
435 In would be a bad idea to disable these warnings!
436 You really need to update your files instead.
438 The new formats will work with old versions of Emacs.
439 New formats are being recommended now to allow time-stamp-format
440 to change in the future to be compatible with format-time-string.
441 The new forms being recommended now will continue to work then.")
444 (defun time-stamp-conv-warn (old-form new-form
)
445 ;; Display a warning about a soon-to-be-obsolete format.
447 (time-stamp-conversion-warn
449 (set-buffer (get-buffer-create "*Time-stamp-compatibility*"))
450 (goto-char (point-max))
454 "The formats recognized in time-stamp-format will change in a future release\n"
455 "to be compatible with the new, expanded format-time-string function.\n\n"
456 "The following obsolescent time-stamp-format construct(s) were found:\n\n")))
457 (insert "\"" old-form
"\" -- use " new-form
"\n"))
458 (display-buffer "*Time-stamp-compatibility*"))))
462 (defun time-stamp-string ()
463 "Generate the new string to be inserted by \\[time-stamp]."
464 (if (stringp time-stamp-format
)
465 (format-time-string (time-stamp-string-preprocess time-stamp-format
))
466 ;; handle version 1 compatibility
467 (cond ((or (eq time-stamp-old-format-warn
'error
)
468 (and (eq time-stamp-old-format-warn
'ask
)
469 (not (y-or-n-p "Use non-string time-stamp-format? "))))
470 (message "Warning: no time-stamp: time-stamp-format not a string")
474 (cond ((eq time-stamp-old-format-warn
'warn
)
475 (message "Obsolescent time-stamp-format type; should be string")
477 (time-stamp-fconcat time-stamp-format
" ")))))
479 (defconst time-stamp-no-file
"(no file)"
480 "String to use when the buffer is not associated with a file.")
482 (defun time-stamp-mail-host-name ()
483 "Return the name of the host where the user receives mail.
484 This is the value of `mail-host-address' if bound and a string,
485 otherwise the value of the function system-name."
486 (or (and (boundp 'mail-host-address
)
487 (stringp mail-host-address
)
491 ;;; the rest of this file is for version 1 compatibility
493 (defun time-stamp-fconcat (list sep
)
494 "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
495 If an element of LIST is a symbol, it is funcalled to get the string to use;
496 the separator SEP is used between two strings obtained by funcalling a
497 symbol. Otherwise the element itself is inserted; no separator is used
499 (let ((return-string "")
502 (cond ((symbolp (car list
))
504 (setq return-string
(concat return-string sep
)))
505 (setq return-string
(concat return-string
(funcall (car list
))))
506 (setq insert-sep-p t
))
508 (setq return-string
(concat return-string
(car list
)))
509 (setq insert-sep-p nil
)))
510 (setq list
(cdr list
)))
513 ;;; Some functions used in time-stamp-format
515 ;;; Could generate most of a message-id with
516 ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name)
518 ;;; pretty form, suitable for a title page
520 (defun time-stamp-month-dd-yyyy ()
521 "Return the current date as a string in \"Month DD, YYYY\" form."
522 (format-time-string "%B %e, %Y"))
524 (defun time-stamp-dd/mm
/yyyy
()
525 "Return the current date as a string in \"DD/MM/YYYY\" form."
526 (format-time-string "%d/%m/%Y"))
528 ;;; same as __DATE__ in ANSI C
530 (defun time-stamp-mon-dd-yyyy ()
531 "Return the current date as a string in \"Mon DD YYYY\" form.
532 The first character of DD is space if the value is less than 10."
533 (format-time-string "%b %d %Y"))
537 (defun time-stamp-dd-mon-yy ()
538 "Return the current date as a string in \"DD Mon YY\" form."
539 (format-time-string "%d %b %y"))
543 (defun time-stamp-yy/mm
/dd
()
544 "Return the current date as a string in \"YY/MM/DD\" form."
545 (format-time-string "%y/%m/%d"))
549 (defun time-stamp-yyyy/mm
/dd
()
550 "Return the current date as a string in \"YYYY/MM/DD\" form."
551 (format-time-string "%Y/%m/%d"))
555 (defun time-stamp-yyyy-mm-dd ()
556 "Return the current date as a string in \"YYYY-MM-DD\" form."
557 (format-time-string "%Y-%m-%d"))
559 (defun time-stamp-yymmdd ()
560 "Return the current date as a string in \"YYMMDD\" form."
561 (format-time-string "%y%m%d"))
563 (defun time-stamp-hh:mm
:ss
()
564 "Return the current time as a string in \"HH:MM:SS\" form."
565 (format-time-string "%T"))
567 (defun time-stamp-hhmm ()
568 "Return the current time as a string in \"HHMM\" form."
569 (format-time-string "%H%M"))
571 (provide 'time-stamp
)
573 ;;; time-stamp.el ends here