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