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