(find-file): Doc fix.
[bpt/emacs.git] / lisp / time-stamp.el
CommitLineData
9565745a 1;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
9565745a 2
fd72ddf6 3;; Copyright 1989, 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
b578f267 4
f7769aa5 5;; Maintainer's Time-stamp: <1999-01-06 11:06:03 gildea>
622b7ede 6;; Maintainer: Stephen Gildea <gildea@alum.mit.edu>
9565745a
RS
7;; Keywords: tools
8
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)
12;; any later version.
13
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.
18
19;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
9565745a
RS
23
24;;; Commentary:
25
03d7856a
KH
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.
30
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.
34
35;; See the documentation for the functions `time-stamp'
36;; and `time-stamp-toggle-active' for details.
b578f267 37
9565745a
RS
38;;; Code:
39
cb35a83c
RS
40(defgroup time-stamp nil
41 "Maintain last change time stamps in files edited by Emacs."
42 :group 'data
43 :group 'extensions)
44
fd72ddf6 45(defcustom time-stamp-format "%:y-%02m-%02d %02H:%02M:%02S %u"
03d7856a 46 "*Format of the string inserted by \\[time-stamp].
c9c0e4bb
RS
47The value may be a string or a list. Lists are supported only for
48backward compatibility; see variable `time-stamp-old-format-warn'.
f8d35bf3 49
fd72ddf6
RS
50A string is used verbatim except for character sequences beginning with %:
51
52%:a weekday name: `Monday'. %#A gives uppercase: `MONDAY'
53%3a abbreviated weekday: `Mon'. %3A gives uppercase: `MON'
54%:b month name: `January'. %#B gives uppercase: `JANUARY'
55%3b abbreviated month: `Jan'. %3B gives uppercase: `JAN'
56%02d day of month
57%02H 24-hour clock hour
58%02I 12-hour clock hour
59%02m month number
60%02M minute
61%#p `am' or `pm'. %P gives uppercase: `AM' or `PM'
62%02S seconds
63%w day number of week, Sunday is 0
64%02y 2-digit year: `97' %:y 4-digit year: `1997'
65%z time zone name: `est'. %Z gives uppercase: `EST'
66
67Non-date items:
68%% a literal percent character: `%'
69%f file name without directory %F gives absolute pathname
70%s system name
f7769aa5 71%u user's login name %U user's full name
fd72ddf6
RS
72%h mail host name
73
74Decimal digits between the % and the type character specify the
75field width. Strings are truncated on the right; years on the left.
76A leading zero causes numbers to be zero-filled.
622b7ede
RS
77
78For example, to get the format used by the `date' command,
fd72ddf6
RS
79use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\".
80
81In the future these formats will be aligned more with format-time-string.
82Because of this transition, the default padding for numeric formats will
83change in a future version. Therefore either a padding width should be
84specified, or the : modifier should be used to explicitly request the
85historical default."
cb35a83c
RS
86 :type 'string
87 :group 'time-stamp)
03d7856a 88
95772e85
RS
89(defcustom time-stamp-active t
90 "*Non-nil to enable time-stamping of buffers by \\[time-stamp].
91Can be toggled by \\[time-stamp-toggle-active].
92See also the variable `time-stamp-warn-inactive'."
93 :type 'boolean
94 :group 'time-stamp)
95
96(defcustom time-stamp-warn-inactive t
97 "Non-nil to have \\[time-stamp] warn if a buffer did not get time-stamped.
98A warning is printed if `time-stamp-active' is nil and the buffer contains
99a time stamp template that would otherwise have been updated."
100 :type 'boolean
101 :group 'time-stamp)
102
103(defcustom time-stamp-old-format-warn 'ask
104 "Action to take if `time-stamp-format' is an old-style list.
105If `error', the format is not used. If `ask', the user is queried about
106using the time-stamp-format. If `warn', a warning is displayed.
107If nil, no notification is given."
108 :type '(choice (const :tag "No notification" nil)
109 (const :tag "Don't use the format" error)
110 (const ask) (const warn))
111 :group 'time-stamp)
112
113(defcustom time-stamp-time-zone nil
114 "If non-nil, a string naming the timezone to be used by \\[time-stamp].
115Format is the same as that used by the environment variable TZ on your system."
7967f8ab 116 :type '(choice (const nil) string)
95772e85 117 :group 'time-stamp)
622b7ede
RS
118
119
b4b33e01
RS
120;;; Do not change time-stamp-line-limit, time-stamp-start,
121;;; time-stamp-end, or time-stamp-pattern in your .emacs
122;;; or you will be incompatible with other people's files!
123;;; If you must change them, do so only in the local variables
124;;; section of the file itself.
9565745a 125
03d7856a 126
e1f40b28 127(defvar time-stamp-line-limit 8 ;Do not change!
c7c4ddd4 128 "Lines of a file searched; positive counts from start, negative from end.
e1f40b28 129The patterns `time-stamp-start' and `time-stamp-end' must be found on one
c7c4ddd4 130of the first (last) `time-stamp-line-limit' lines of the file for the
f7769aa5
RS
131file to be time-stamped by \\[time-stamp]. A value of 0 searches the
132entire buffer (use with care).
e1f40b28
RS
133
134Do not change `time-stamp-line-limit', `time-stamp-start', or
135`time-stamp-end' for yourself or you will be incompatible
136with other people's files! If you must change them for some application,
137do so in the local variables section of the time-stamped file itself.")
138
9565745a 139
36081614 140(defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change!
9565745a 141 "Regexp after which the time stamp is written by \\[time-stamp].
e1f40b28 142See also the variables `time-stamp-end' and `time-stamp-line-limit'.
9565745a 143
e1f40b28
RS
144Do not change `time-stamp-line-limit', `time-stamp-start', or
145`time-stamp-end' for yourself or you will be incompatible
9565745a
RS
146with other people's files! If you must change them for some application,
147do so in the local variables section of the time-stamped file itself.")
148
149
e1f40b28 150(defvar time-stamp-end "\\\\?[\">]" ;Do not change!
9565745a 151 "Regexp marking the text after the time stamp.
e1f40b28
RS
152\\[time-stamp] deletes the text between the first match of `time-stamp-start'
153and the following match of `time-stamp-end' on the same line,
154then writes the time stamp specified by `time-stamp-format' between them.
155
156Do not change `time-stamp-line-limit', `time-stamp-start', or
157`time-stamp-end' for yourself or you will be incompatible
158with other people's files! If you must change them for some application,
159do so in the local variables section of the time-stamped file itself.")
160
9565745a 161
b4b33e01
RS
162(defvar time-stamp-pattern "%%" ;Do not change!
163 "Convenience variable setting all time-stamp location and format variables.
164This string has four parts, each of which is optional.
165These four parts set time-stamp-line-limit, time-stamp-start,
166time-stamp-format, and time-stamp-end. See the documentation
167for each of these variables for details.
168
169The first part is a number followed by a slash; the number sets the number
170of lines at the beginning (negative counts from end) of the file searched
171for the time-stamp. The number and the slash may be omitted to use the
172normal value.
173
174The second part is a regexp identifying the pattern preceding the time stamp.
175This part may be omitted to use the normal pattern.
176
177The third part specifies the format of the time-stamp inserted. See
178the documentation for time-stamp-format for details. Specify this
179part as \"%%\" to use the normal format.
180
181The fourth part is a regexp identifying the pattern following the time stamp.
182This part may be omitted to use the normal pattern.
183
184As an example, the default behavior can be specified something like this:
185\"8/Time-stamp: [\\\"<]%:y-%02m-%02d %02H:%02M:%02S %u[\\\">]\"
186
187Do not change `time-stamp-pattern' for yourself or you will be incompatible
188with other people's files! Set it only in the local variables section
189of the time-stamped file itself.")
190
191
03d7856a 192
59b644e8 193;;;###autoload
9565745a
RS
194(defun time-stamp ()
195 "Update the time stamp string in the buffer.
03d7856a
KH
196A template in a file can be automatically updated with a new time stamp
197every time you save the file. Add this line to your .emacs file:
198 (add-hook 'write-file-hooks 'time-stamp)
199Normally the template must appear in the first 8 lines of a file and
200look like one of the following:
201 Time-stamp: <>
202 Time-stamp: \" \"
203The time stamp is written between the brackets or quotes:
b4b33e01 204 Time-stamp: <1998-02-18 10:20:51 gildea>
622b7ede 205The time stamp is updated only if the variable `time-stamp-active' is non-nil.
03d7856a
KH
206The format of the time stamp is set by the variable `time-stamp-format'.
207The variables `time-stamp-line-limit', `time-stamp-start',
208and `time-stamp-end' control finding the template."
9565745a 209 (interactive)
fe8287c6 210 (let ((case-fold-search nil)
03d7856a
KH
211 (start nil)
212 (end nil)
622b7ede 213 search-limit
b4b33e01
RS
214 (line-limit time-stamp-line-limit)
215 (ts-start time-stamp-start)
216 (ts-format time-stamp-format)
217 (ts-end time-stamp-end))
218 (if (stringp time-stamp-pattern)
219 (progn
15ceaba4 220 (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(.\\|\n\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern)
b4b33e01
RS
221 (and (match-beginning 2)
222 (setq line-limit
223 (string-to-int (match-string 2 time-stamp-pattern))))
224 (and (match-beginning 3)
225 (setq ts-start (match-string 3 time-stamp-pattern)))
226 (and (match-beginning 4)
227 (not (string-equal (match-string 4 time-stamp-pattern) "%%"))
228 (setq ts-format (match-string 4 time-stamp-pattern)))
15ceaba4
KH
229 (and (match-beginning 6)
230 (setq ts-end (match-string 6 time-stamp-pattern)))))
622b7ede
RS
231 (cond ((not (integerp line-limit))
232 (setq line-limit 8)
b4b33e01 233 (message "time-stamp-line-limit is not an integer")
622b7ede
RS
234 (sit-for 1)))
235 (save-excursion
236 (save-restriction
237 (widen)
238 (cond ((> line-limit 0)
239 (goto-char (setq start (point-min)))
240 (forward-line line-limit)
241 (setq search-limit (point)))
f7769aa5 242 ((< line-limit 0)
622b7ede
RS
243 (goto-char (setq search-limit (point-max)))
244 (forward-line line-limit)
f7769aa5
RS
245 (setq start (point)))
246 (t ;0 => no limit (use with care!)
247 (setq start (point-min))
248 (setq search-limit (point-max))))
622b7ede
RS
249 (goto-char start)
250 (while (and (< (point) search-limit)
251 (not end)
b4b33e01 252 (re-search-forward ts-start search-limit 'move))
622b7ede
RS
253 (setq start (point))
254 (end-of-line)
255 (let ((line-end (point)))
c7c4ddd4 256 (goto-char start)
b4b33e01 257 (if (re-search-forward ts-end line-end 'move)
622b7ede 258 (setq end (match-beginning 0)))))))
b4b33e01
RS
259 (if end
260 (progn
261 ;; do all warnings outside save-excursion
262 (cond
263 ((not time-stamp-active)
264 (if time-stamp-warn-inactive
265 ;; don't signal an error in a write-file-hook
266 (progn
267 (message "Warning: time-stamp-active is off; did not time-stamp buffer.")
268 (sit-for 1))))
269 ((not (and (stringp ts-start)
270 (stringp ts-end)))
271 (message "time-stamp-start or time-stamp-end is not a string")
272 (sit-for 1))
273 (t
274 (let ((new-time-stamp (time-stamp-string ts-format)))
f7769aa5
RS
275 (if (and (stringp new-time-stamp)
276 (not (string-equal (buffer-substring start end)
277 new-time-stamp)))
b4b33e01
RS
278 (save-excursion
279 (save-restriction
280 (widen)
281 (delete-region start end)
282 (goto-char start)
283 (insert-and-inherit new-time-stamp)
284 (setq end (point))
285 ;; remove any tabs used to format time stamp
286 (goto-char start)
287 (if (search-forward "\t" end t)
288 (untabify start end)))))))))))
9565745a
RS
289 ;; be sure to return nil so can be used on write-file-hooks
290 nil)
291
b1defad2
RS
292;;;###autoload
293(defun time-stamp-toggle-active (&optional arg)
03d7856a 294 "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer.
b1defad2
RS
295With arg, turn time stamping on if and only if arg is positive."
296 (interactive "P")
297 (setq time-stamp-active
298 (if (null arg)
299 (not time-stamp-active)
300 (> (prefix-numeric-value arg) 0)))
301 (message "time-stamp is now %s." (if time-stamp-active "active" "off")))
03d7856a 302
6946129c
RS
303(defconst time-stamp-no-file "(no file)"
304 "String to use when the buffer is not associated with a file.")
305
fd72ddf6
RS
306;;; time-stamp is transitioning to using the new, expanded capabilities
307;;; of format-time-string. During the process, this function implements
308;;; intermediate, compatible formats and complains about old, soon to
309;;; be unsupported, formats. This function will get a lot (a LOT) shorter
310;;; when the transition is complete and we can just pass most things
311;;; straight through to format-time-string.
312;;; At all times, all the formats recommended in the doc string
313;;; of time-stamp-format will work not only in the current version of
314;;; Emacs, but in all versions that have been released within the past
315;;; two years.
316;;; The : modifier is a temporary conversion feature used to resolve
317;;; ambiguous formats--formats that are changing (over time) incompatibly.
318(defun time-stamp-string-preprocess (format &optional time)
319 ;; Uses a FORMAT to format date, time, file, and user information.
320 ;; Optional second argument TIME is only for testing.
321 ;; Implements non-time extensions to format-time-string
322 ;; and all time-stamp-format compatibility.
323 (let ((fmt-len (length format))
324 (ind 0)
325 cur-char
326 (prev-char nil)
327 (result "")
fd72ddf6
RS
328 field-width
329 field-result
330 alt-form change-case require-padding
331 (paren-level 0))
332 (while (< ind fmt-len)
333 (setq cur-char (aref format ind))
334 (setq
335 result
336 (concat result
337 (cond
338 ((eq cur-char ?%)
339 ;; eat any additional args to allow for future expansion
15ceaba4 340 (setq alt-form nil change-case nil require-padding nil field-width "")
fd72ddf6
RS
341 (while (progn
342 (setq ind (1+ ind))
343 (setq cur-char (if (< ind fmt-len)
344 (aref format ind)
345 ?\0))
346 (or (eq ?. cur-char)
347 (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char)
348 (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char)
349 (eq ?\ cur-char) (eq ?# cur-char) (eq ?^ cur-char)
350 (and (eq ?\( cur-char)
351 (not (eq prev-char ?\\))
352 (setq paren-level (1+ paren-level)))
353 (if (and (eq ?\) cur-char)
354 (not (eq prev-char ?\\))
355 (> paren-level 0))
356 (setq paren-level (1- paren-level))
357 (and (> paren-level 0)
15ceaba4
KH
358 (< ind fmt-len)))
359 (if (and (<= ?0 cur-char) (>= ?9 cur-char))
360 ;; get format width
361 (let ((field-index ind))
362 (while (progn
363 (setq ind (1+ ind))
364 (setq cur-char (if (< ind fmt-len)
365 (aref format ind)
366 ?\0))
367 (and (<= ?0 cur-char) (>= ?9 cur-char))))
368 (setq field-width (substring format field-index ind))
369 (setq ind (1- ind))
370 t))))
fd72ddf6
RS
371 (setq prev-char cur-char)
372 ;; some characters we actually use
373 (cond ((eq cur-char ?:)
374 (setq alt-form t))
375 ((eq cur-char ?#)
376 (setq change-case t))))
fd72ddf6
RS
377 (setq field-result
378 (cond
379 ((eq cur-char ?%)
380 "%")
381 ((eq cur-char ?a) ;day of week
382 (if change-case
383 (format-time-string "%#A" time)
384 (or alt-form (not (string-equal field-width ""))
385 (time-stamp-conv-warn "%a" "%:a"))
386 (if (and alt-form (not (string-equal field-width "")))
387 "" ;discourage "%:3a"
388 (format-time-string "%A" time))))
389 ((eq cur-char ?A)
390 (if alt-form
391 (format-time-string "%A" time)
392 (or change-case (not (string-equal field-width ""))
393 (time-stamp-conv-warn "%A" "%#A"))
394 (format-time-string "%#A" time)))
395 ((eq cur-char ?b) ;month name
396 (if change-case
397 (format-time-string "%#B" time)
398 (or alt-form (not (string-equal field-width ""))
399 (time-stamp-conv-warn "%b" "%:b"))
400 (if (and alt-form (not (string-equal field-width "")))
401 "" ;discourage "%:3b"
402 (format-time-string "%B" time))))
403 ((eq cur-char ?B)
404 (if alt-form
405 (format-time-string "%B" time)
406 (or change-case (not (string-equal field-width ""))
407 (time-stamp-conv-warn "%B" "%#B"))
408 (format-time-string "%#B" time)))
409 ((eq cur-char ?d) ;day of month, 1-31
fdac7fba 410 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 411 ((eq cur-char ?H) ;hour, 0-23
fdac7fba 412 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 413 ((eq cur-char ?I) ;hour, 1-12
fdac7fba 414 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 415 ((eq cur-char ?m) ;month number, 1-12
fdac7fba 416 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 417 ((eq cur-char ?M) ;minute, 0-59
fdac7fba 418 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6
RS
419 ((eq cur-char ?p) ;am or pm
420 (or change-case
421 (time-stamp-conv-warn "%p" "%#p"))
422 (format-time-string "%#p" time))
423 ((eq cur-char ?P) ;AM or PM
424 (format-time-string "%p" time))
425 ((eq cur-char ?S) ;seconds, 00-60
fdac7fba 426 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6
RS
427 ((eq cur-char ?w) ;weekday number, Sunday is 0
428 (format-time-string "%w" time))
429 ((eq cur-char ?y) ;year
430 (or alt-form (not (string-equal field-width ""))
431 (time-stamp-conv-warn "%y" "%:y"))
432 (string-to-int (format-time-string "%Y" time)))
433 ((eq cur-char ?Y) ;4-digit year, new style
434 (string-to-int (format-time-string "%Y" time)))
435 ((eq cur-char ?z) ;time zone lower case
436 (if change-case
437 "" ;discourage %z variations
438 (format-time-string "%#Z" time)))
439 ((eq cur-char ?Z)
440 (if change-case
441 (format-time-string "%#Z" time)
442 (format-time-string "%Z" time)))
443 ((eq cur-char ?f) ;buffer-file-name, base name only
444 (if buffer-file-name
445 (file-name-nondirectory buffer-file-name)
446 time-stamp-no-file))
447 ((eq cur-char ?F) ;buffer-file-name, full path
448 (or buffer-file-name
449 time-stamp-no-file))
450 ((eq cur-char ?s) ;system name
451 (system-name))
452 ((eq cur-char ?u) ;user name
453 (user-login-name))
2c8d5749
RS
454 ((eq cur-char ?U) ;user full name
455 (user-full-name))
fd72ddf6
RS
456 ((eq cur-char ?h) ;mail host name
457 (time-stamp-mail-host-name))
458 ))
459 (if (string-equal field-width "")
460 field-result
461 (let ((padded-result
462 (format (format "%%%s%c"
463 field-width
464 (if (numberp field-result) ?d ?s))
465 (or field-result ""))))
466 (let ((initial-length (length padded-result))
467 (desired-length (string-to-int field-width)))
468 (if (> initial-length desired-length)
469 ;; truncate strings on right, years on left
470 (if (stringp field-result)
471 (substring padded-result 0 desired-length)
472 (if (eq cur-char ?y)
473 (substring padded-result (- desired-length))
474 padded-result)) ;non-year numbers don't truncate
475 padded-result)))))
476 (t
477 (char-to-string cur-char)))))
478 (setq ind (1+ ind)))
479 result))
480
fdac7fba
RS
481(defun time-stamp-do-number (format-char alt-form field-width time)
482 ;; Handle a compatible FORMAT-CHAR where only
fd72ddf6 483 ;; the default width/padding will change.
fdac7fba
RS
484 ;; ALT-FORM is whether `#' specified. FIELD-WIDTH is the string
485 ;; width specification or "". TIME is the time to convert.
fd72ddf6
RS
486 (let ((format-string (concat "%" (char-to-string format-char))))
487 (and (not alt-form) (string-equal field-width "")
488 (time-stamp-conv-warn format-string
489 (format "%%:%c" format-char)))
490 (if (and alt-form (not (string-equal field-width "")))
491 "" ;discourage "%:2d" and the like
492 (string-to-int (format-time-string format-string time)))))
493
494(defvar time-stamp-conversion-warn t
495 "Non-nil to warn about soon-to-be-unsupported forms in time-stamp-format.
496In would be a bad idea to disable these warnings!
497You really need to update your files instead.
498
499The new formats will work with old versions of Emacs.
500New formats are being recommended now to allow time-stamp-format
501to change in the future to be compatible with format-time-string.
502The new forms being recommended now will continue to work then.")
503
504
505(defun time-stamp-conv-warn (old-form new-form)
506 ;; Display a warning about a soon-to-be-obsolete format.
507 (cond
508 (time-stamp-conversion-warn
509 (save-excursion
510 (set-buffer (get-buffer-create "*Time-stamp-compatibility*"))
511 (goto-char (point-max))
512 (if (bobp)
513 (progn
514 (insert
515 "The formats recognized in time-stamp-format will change in a future release\n"
516 "to be compatible with the new, expanded format-time-string function.\n\n"
517 "The following obsolescent time-stamp-format construct(s) were found:\n\n")))
518 (insert "\"" old-form "\" -- use " new-form "\n"))
519 (display-buffer "*Time-stamp-compatibility*"))))
95772e85 520
fd72ddf6 521
b1defad2 522
b4b33e01
RS
523(defun time-stamp-string (&optional ts-format)
524 "Generate the new string to be inserted by \\[time-stamp].
525Optionally use FORMAT."
526 (or ts-format
527 (setq ts-format time-stamp-format))
528 (if (stringp ts-format)
95772e85
RS
529 (if (stringp time-stamp-time-zone)
530 (let ((real-time-zone (getenv "TZ")))
531 (unwind-protect
532 (progn
533 (setenv "TZ" time-stamp-time-zone)
534 (format-time-string
b4b33e01 535 (time-stamp-string-preprocess ts-format)))
95772e85
RS
536 (setenv "TZ" real-time-zone)))
537 (format-time-string
b4b33e01 538 (time-stamp-string-preprocess ts-format)))
03d7856a
KH
539 ;; handle version 1 compatibility
540 (cond ((or (eq time-stamp-old-format-warn 'error)
541 (and (eq time-stamp-old-format-warn 'ask)
542 (not (y-or-n-p "Use non-string time-stamp-format? "))))
543 (message "Warning: no time-stamp: time-stamp-format not a string")
544 (sit-for 1)
545 nil)
546 (t
547 (cond ((eq time-stamp-old-format-warn 'warn)
548 (message "Obsolescent time-stamp-format type; should be string")
549 (sit-for 1)))
b4b33e01 550 (time-stamp-fconcat ts-format " ")))))
b1defad2 551
c7c4ddd4
KH
552(defconst time-stamp-no-file "(no file)"
553 "String to use when the buffer is not associated with a file.")
554
b1defad2
RS
555(defun time-stamp-mail-host-name ()
556 "Return the name of the host where the user receives mail.
557This is the value of `mail-host-address' if bound and a string,
e654f4b0 558otherwise the value of the function system-name."
b1defad2
RS
559 (or (and (boundp 'mail-host-address)
560 (stringp mail-host-address)
561 mail-host-address)
b1defad2
RS
562 (system-name)))
563
564;;; the rest of this file is for version 1 compatibility
9565745a
RS
565
566(defun time-stamp-fconcat (list sep)
e1f40b28 567 "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
9565745a
RS
568If an element of LIST is a symbol, it is funcalled to get the string to use;
569the separator SEP is used between two strings obtained by funcalling a
570symbol. Otherwise the element itself is inserted; no separator is used
571around literals."
572 (let ((return-string "")
573 (insert-sep-p nil))
574 (while list
575 (cond ((symbolp (car list))
576 (if insert-sep-p
577 (setq return-string (concat return-string sep)))
578 (setq return-string (concat return-string (funcall (car list))))
579 (setq insert-sep-p t))
580 (t
581 (setq return-string (concat return-string (car list)))
582 (setq insert-sep-p nil)))
583 (setq list (cdr list)))
584 return-string))
585
03d7856a 586;;; Some functions used in time-stamp-format
9565745a
RS
587
588;;; Could generate most of a message-id with
e1f40b28 589;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name)
9565745a 590
9565745a
RS
591;;; pretty form, suitable for a title page
592
593(defun time-stamp-month-dd-yyyy ()
e1f40b28 594 "Return the current date as a string in \"Month DD, YYYY\" form."
b8944601 595 (format-time-string "%B %e, %Y"))
9565745a 596
b24173f7
RS
597(defun time-stamp-dd/mm/yyyy ()
598 "Return the current date as a string in \"DD/MM/YYYY\" form."
b8944601 599 (format-time-string "%d/%m/%Y"))
b24173f7 600
9565745a
RS
601;;; same as __DATE__ in ANSI C
602
603(defun time-stamp-mon-dd-yyyy ()
e1f40b28
RS
604 "Return the current date as a string in \"Mon DD YYYY\" form.
605The first character of DD is space if the value is less than 10."
b8944601 606 (format-time-string "%b %d %Y"))
9565745a
RS
607
608;;; RFC 822 date
609
610(defun time-stamp-dd-mon-yy ()
e1f40b28 611 "Return the current date as a string in \"DD Mon YY\" form."
b8944601 612 (format-time-string "%d %b %y"))
9565745a
RS
613
614;;; RCS 3 date
615
616(defun time-stamp-yy/mm/dd ()
e1f40b28 617 "Return the current date as a string in \"YY/MM/DD\" form."
b8944601 618 (format-time-string "%y/%m/%d"))
9565745a
RS
619
620;;; RCS 5 date
621
622(defun time-stamp-yyyy/mm/dd ()
e1f40b28 623 "Return the current date as a string in \"YYYY/MM/DD\" form."
b8944601 624 (format-time-string "%Y/%m/%d"))
9565745a 625
e1f40b28 626;;; ISO 8601 date
9565745a 627
e1f40b28
RS
628(defun time-stamp-yyyy-mm-dd ()
629 "Return the current date as a string in \"YYYY-MM-DD\" form."
b8944601 630 (format-time-string "%Y-%m-%d"))
9565745a 631
e1f40b28
RS
632(defun time-stamp-yymmdd ()
633 "Return the current date as a string in \"YYMMDD\" form."
b8944601 634 (format-time-string "%y%m%d"))
9565745a
RS
635
636(defun time-stamp-hh:mm:ss ()
e1f40b28 637 "Return the current time as a string in \"HH:MM:SS\" form."
b8944601 638 (format-time-string "%T"))
9565745a 639
9565745a 640(defun time-stamp-hhmm ()
e1f40b28 641 "Return the current time as a string in \"HHMM\" form."
b8944601 642 (format-time-string "%H%M"))
9565745a
RS
643
644(provide 'time-stamp)
645
646;;; time-stamp.el ends here