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