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