(Fset_text_properties): 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
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].
395Optionally use FORMAT."
396 (or ts-format
397 (setq ts-format time-stamp-format))
398 (if (stringp ts-format)
399 (if (stringp time-stamp-time-zone)
400 (let ((ts-real-time-zone (getenv "TZ")))
401 (unwind-protect
402 (progn
403 (setenv "TZ" time-stamp-time-zone)
404 (format-time-string
405 (time-stamp-string-preprocess ts-format)))
406 (setenv "TZ" ts-real-time-zone)))
407 (format-time-string
408 (time-stamp-string-preprocess ts-format)))
409 ;; handle version 1 compatibility
410 (cond ((or (eq time-stamp-old-format-warn 'error)
411 (and (eq time-stamp-old-format-warn 'ask)
412 (not (y-or-n-p "Use non-string time-stamp-format? "))))
413 (message "Warning: no time-stamp: time-stamp-format not a string")
414 (sit-for 1)
415 nil)
416 (t
417 (cond ((eq time-stamp-old-format-warn 'warn)
418 (message "Obsolescent time-stamp-format type; should be string")
419 (sit-for 1)))
420 (time-stamp-fconcat ts-format " ")))))
03d7856a 421
6946129c
RS
422(defconst time-stamp-no-file "(no file)"
423 "String to use when the buffer is not associated with a file.")
424
fd72ddf6
RS
425;;; time-stamp is transitioning to using the new, expanded capabilities
426;;; of format-time-string. During the process, this function implements
427;;; intermediate, compatible formats and complains about old, soon to
428;;; be unsupported, formats. This function will get a lot (a LOT) shorter
429;;; when the transition is complete and we can just pass most things
430;;; straight through to format-time-string.
431;;; At all times, all the formats recommended in the doc string
432;;; of time-stamp-format will work not only in the current version of
433;;; Emacs, but in all versions that have been released within the past
434;;; two years.
435;;; The : modifier is a temporary conversion feature used to resolve
436;;; ambiguous formats--formats that are changing (over time) incompatibly.
437(defun time-stamp-string-preprocess (format &optional time)
22453b51
DL
438 "Use a FORMAT to format date, time, file, and user information.
439Optional second argument TIME is only for testing.
440Implements non-time extensions to `format-time-string'
441and all time-stamp-format compatibility."
fd72ddf6
RS
442 (let ((fmt-len (length format))
443 (ind 0)
444 cur-char
445 (prev-char nil)
446 (result "")
fd72ddf6
RS
447 field-width
448 field-result
449 alt-form change-case require-padding
450 (paren-level 0))
451 (while (< ind fmt-len)
452 (setq cur-char (aref format ind))
453 (setq
454 result
455 (concat result
456 (cond
457 ((eq cur-char ?%)
458 ;; eat any additional args to allow for future expansion
15ceaba4 459 (setq alt-form nil change-case nil require-padding nil field-width "")
fd72ddf6
RS
460 (while (progn
461 (setq ind (1+ ind))
462 (setq cur-char (if (< ind fmt-len)
463 (aref format ind)
464 ?\0))
465 (or (eq ?. cur-char)
466 (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char)
22453b51 467 (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char)
fd72ddf6
RS
468 (eq ?\ cur-char) (eq ?# cur-char) (eq ?^ cur-char)
469 (and (eq ?\( cur-char)
470 (not (eq prev-char ?\\))
471 (setq paren-level (1+ paren-level)))
472 (if (and (eq ?\) cur-char)
473 (not (eq prev-char ?\\))
474 (> paren-level 0))
475 (setq paren-level (1- paren-level))
476 (and (> paren-level 0)
15ceaba4
KH
477 (< ind fmt-len)))
478 (if (and (<= ?0 cur-char) (>= ?9 cur-char))
479 ;; get format width
480 (let ((field-index ind))
481 (while (progn
482 (setq ind (1+ ind))
483 (setq cur-char (if (< ind fmt-len)
484 (aref format ind)
485 ?\0))
486 (and (<= ?0 cur-char) (>= ?9 cur-char))))
487 (setq field-width (substring format field-index ind))
488 (setq ind (1- ind))
489 t))))
fd72ddf6
RS
490 (setq prev-char cur-char)
491 ;; some characters we actually use
492 (cond ((eq cur-char ?:)
493 (setq alt-form t))
494 ((eq cur-char ?#)
495 (setq change-case t))))
fd72ddf6
RS
496 (setq field-result
497 (cond
498 ((eq cur-char ?%)
55ac4d11 499 "%%")
fd72ddf6
RS
500 ((eq cur-char ?a) ;day of week
501 (if change-case
502 (format-time-string "%#A" time)
503 (or alt-form (not (string-equal field-width ""))
504 (time-stamp-conv-warn "%a" "%:a"))
505 (if (and alt-form (not (string-equal field-width "")))
506 "" ;discourage "%:3a"
507 (format-time-string "%A" time))))
508 ((eq cur-char ?A)
509 (if alt-form
510 (format-time-string "%A" time)
511 (or change-case (not (string-equal field-width ""))
512 (time-stamp-conv-warn "%A" "%#A"))
513 (format-time-string "%#A" time)))
514 ((eq cur-char ?b) ;month name
515 (if change-case
516 (format-time-string "%#B" time)
517 (or alt-form (not (string-equal field-width ""))
518 (time-stamp-conv-warn "%b" "%:b"))
519 (if (and alt-form (not (string-equal field-width "")))
520 "" ;discourage "%:3b"
521 (format-time-string "%B" time))))
522 ((eq cur-char ?B)
523 (if alt-form
524 (format-time-string "%B" time)
525 (or change-case (not (string-equal field-width ""))
526 (time-stamp-conv-warn "%B" "%#B"))
527 (format-time-string "%#B" time)))
528 ((eq cur-char ?d) ;day of month, 1-31
fdac7fba 529 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 530 ((eq cur-char ?H) ;hour, 0-23
fdac7fba 531 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 532 ((eq cur-char ?I) ;hour, 1-12
fdac7fba 533 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 534 ((eq cur-char ?m) ;month number, 1-12
fdac7fba 535 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6 536 ((eq cur-char ?M) ;minute, 0-59
fdac7fba 537 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6
RS
538 ((eq cur-char ?p) ;am or pm
539 (or change-case
540 (time-stamp-conv-warn "%p" "%#p"))
541 (format-time-string "%#p" time))
542 ((eq cur-char ?P) ;AM or PM
543 (format-time-string "%p" time))
544 ((eq cur-char ?S) ;seconds, 00-60
fdac7fba 545 (time-stamp-do-number cur-char alt-form field-width time))
fd72ddf6
RS
546 ((eq cur-char ?w) ;weekday number, Sunday is 0
547 (format-time-string "%w" time))
548 ((eq cur-char ?y) ;year
549 (or alt-form (not (string-equal field-width ""))
550 (time-stamp-conv-warn "%y" "%:y"))
d09b9dbd 551 (string-to-int (format-time-string "%Y" time)))
fd72ddf6 552 ((eq cur-char ?Y) ;4-digit year, new style
d09b9dbd 553 (string-to-int (format-time-string "%Y" time)))
fd72ddf6
RS
554 ((eq cur-char ?z) ;time zone lower case
555 (if change-case
556 "" ;discourage %z variations
557 (format-time-string "%#Z" time)))
558 ((eq cur-char ?Z)
559 (if change-case
560 (format-time-string "%#Z" time)
561 (format-time-string "%Z" time)))
562 ((eq cur-char ?f) ;buffer-file-name, base name only
563 (if buffer-file-name
564 (file-name-nondirectory buffer-file-name)
565 time-stamp-no-file))
566 ((eq cur-char ?F) ;buffer-file-name, full path
567 (or buffer-file-name
568 time-stamp-no-file))
569 ((eq cur-char ?s) ;system name
570 (system-name))
571 ((eq cur-char ?u) ;user name
572 (user-login-name))
2c8d5749
RS
573 ((eq cur-char ?U) ;user full name
574 (user-full-name))
55ac4d11
KH
575 ((eq cur-char ?l) ;logname (undocumented user name alt)
576 (user-login-name))
577 ((eq cur-char ?L) ;(undocumented alt user full name)
578 (user-full-name))
fd72ddf6
RS
579 ((eq cur-char ?h) ;mail host name
580 (time-stamp-mail-host-name))
581 ))
b6735035
GM
582 (let ((padded-result
583 (format (format "%%%s%c"
584 field-width
585 (if (numberp field-result) ?d ?s))
586 (or field-result ""))))
587 (let* ((initial-length (length padded-result))
588 (desired-length (if (string-equal field-width "")
589 initial-length
590 (string-to-int field-width))))
591 (if (> initial-length desired-length)
592 ;; truncate strings on right, years on left
593 (if (stringp field-result)
594 (substring padded-result 0 desired-length)
595 (if (eq cur-char ?y)
596 (substring padded-result (- desired-length))
597 padded-result)) ;non-year numbers don't truncate
598 padded-result))))
fd72ddf6
RS
599 (t
600 (char-to-string cur-char)))))
601 (setq ind (1+ ind)))
602 result))
603
fdac7fba 604(defun time-stamp-do-number (format-char alt-form field-width time)
22453b51
DL
605 "Handle compatible FORMAT-CHAR where only default width/padding will change.
606ALT-FORM is whether `#' specified. FIELD-WIDTH is the string
607width specification or \"\". TIME is the time to convert."
fd72ddf6
RS
608 (let ((format-string (concat "%" (char-to-string format-char))))
609 (and (not alt-form) (string-equal field-width "")
610 (time-stamp-conv-warn format-string
611 (format "%%:%c" format-char)))
612 (if (and alt-form (not (string-equal field-width "")))
613 "" ;discourage "%:2d" and the like
614 (string-to-int (format-time-string format-string time)))))
615
616(defvar time-stamp-conversion-warn t
3b5e21df
GM
617 "Warn about soon-to-be-unsupported forms in `time-stamp-format'.
618If nil, these warnings are disabled, which would be a bad idea!
fd72ddf6
RS
619You really need to update your files instead.
620
621The new formats will work with old versions of Emacs.
55ac4d11
KH
622New formats are being recommended now to allow `time-stamp-format'
623to change in the future to be compatible with `format-time-string'.
fd72ddf6
RS
624The new forms being recommended now will continue to work then.")
625
626
627(defun time-stamp-conv-warn (old-form new-form)
22453b51 628 "Display a warning about a soon-to-be-obsolete format."
fd72ddf6
RS
629 (cond
630 (time-stamp-conversion-warn
631 (save-excursion
632 (set-buffer (get-buffer-create "*Time-stamp-compatibility*"))
633 (goto-char (point-max))
634 (if (bobp)
635 (progn
636 (insert
637 "The formats recognized in time-stamp-format will change in a future release\n"
638 "to be compatible with the new, expanded format-time-string function.\n\n"
639 "The following obsolescent time-stamp-format construct(s) were found:\n\n")))
640 (insert "\"" old-form "\" -- use " new-form "\n"))
641 (display-buffer "*Time-stamp-compatibility*"))))
95772e85 642
fd72ddf6 643
b1defad2 644
b1defad2
RS
645(defun time-stamp-mail-host-name ()
646 "Return the name of the host where the user receives mail.
647This is the value of `mail-host-address' if bound and a string,
55ac4d11 648otherwise the value of the function `system-name'."
b1defad2
RS
649 (or (and (boundp 'mail-host-address)
650 (stringp mail-host-address)
651 mail-host-address)
b1defad2
RS
652 (system-name)))
653
654;;; the rest of this file is for version 1 compatibility
9565745a
RS
655
656(defun time-stamp-fconcat (list sep)
e1f40b28 657 "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
9565745a
RS
658If an element of LIST is a symbol, it is funcalled to get the string to use;
659the separator SEP is used between two strings obtained by funcalling a
660symbol. Otherwise the element itself is inserted; no separator is used
661around literals."
662 (let ((return-string "")
663 (insert-sep-p nil))
664 (while list
665 (cond ((symbolp (car list))
666 (if insert-sep-p
667 (setq return-string (concat return-string sep)))
668 (setq return-string (concat return-string (funcall (car list))))
669 (setq insert-sep-p t))
670 (t
671 (setq return-string (concat return-string (car list)))
672 (setq insert-sep-p nil)))
673 (setq list (cdr list)))
674 return-string))
675
03d7856a 676;;; Some functions used in time-stamp-format
9565745a
RS
677
678;;; Could generate most of a message-id with
e1f40b28 679;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name)
9565745a 680
9565745a
RS
681;;; pretty form, suitable for a title page
682
683(defun time-stamp-month-dd-yyyy ()
e1f40b28 684 "Return the current date as a string in \"Month DD, YYYY\" form."
b8944601 685 (format-time-string "%B %e, %Y"))
9565745a 686
b24173f7
RS
687(defun time-stamp-dd/mm/yyyy ()
688 "Return the current date as a string in \"DD/MM/YYYY\" form."
b8944601 689 (format-time-string "%d/%m/%Y"))
b24173f7 690
9565745a
RS
691;;; same as __DATE__ in ANSI C
692
693(defun time-stamp-mon-dd-yyyy ()
e1f40b28
RS
694 "Return the current date as a string in \"Mon DD YYYY\" form.
695The first character of DD is space if the value is less than 10."
b8944601 696 (format-time-string "%b %d %Y"))
9565745a
RS
697
698;;; RFC 822 date
699
700(defun time-stamp-dd-mon-yy ()
e1f40b28 701 "Return the current date as a string in \"DD Mon YY\" form."
b8944601 702 (format-time-string "%d %b %y"))
9565745a
RS
703
704;;; RCS 3 date
705
706(defun time-stamp-yy/mm/dd ()
e1f40b28 707 "Return the current date as a string in \"YY/MM/DD\" form."
b8944601 708 (format-time-string "%y/%m/%d"))
9565745a
RS
709
710;;; RCS 5 date
711
712(defun time-stamp-yyyy/mm/dd ()
e1f40b28 713 "Return the current date as a string in \"YYYY/MM/DD\" form."
b8944601 714 (format-time-string "%Y/%m/%d"))
9565745a 715
e1f40b28 716;;; ISO 8601 date
9565745a 717
e1f40b28
RS
718(defun time-stamp-yyyy-mm-dd ()
719 "Return the current date as a string in \"YYYY-MM-DD\" form."
b8944601 720 (format-time-string "%Y-%m-%d"))
9565745a 721
e1f40b28
RS
722(defun time-stamp-yymmdd ()
723 "Return the current date as a string in \"YYMMDD\" form."
b8944601 724 (format-time-string "%y%m%d"))
9565745a
RS
725
726(defun time-stamp-hh:mm:ss ()
e1f40b28 727 "Return the current time as a string in \"HH:MM:SS\" form."
b8944601 728 (format-time-string "%T"))
9565745a 729
9565745a 730(defun time-stamp-hhmm ()
e1f40b28 731 "Return the current time as a string in \"HHMM\" form."
b8944601 732 (format-time-string "%H%M"))
9565745a
RS
733
734(provide 'time-stamp)
735
736;;; time-stamp.el ends here