(set_frame_menubar): Run acivate-menu-bar-hook with
[emacs.git] / lisp / time-stamp.el
blob1de7e5ad5ec112d519e190c069591cfceefe0bbb
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 `locale-coding-system'. The
54 examples here are for the default (`C') locale.
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'
71 Non-date items:
72 %% a literal percent character: `%'
73 %f file name without directory %F gives absolute pathname
74 %s system name
75 %u user's login name %U user's full name
76 %h mail host name
78 Decimal digits between the % and the type character specify the
79 field width. Strings are truncated on the right; years on the left.
80 A leading zero in the field width zero-fills a number.
82 For example, to get the format used by the `date' command,
83 use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\".
85 In the future these formats will be aligned more with `format-time-string'.
86 Because of this transition, the default padding for numeric formats will
87 change in a future version. Therefore either a padding width should be
88 specified, or the : modifier should be used to explicitly request the
89 historical default."
90 :type 'string
91 :group 'time-stamp)
93 (defcustom time-stamp-active t
94 "*Non-nil to enable time-stamping of buffers by \\[time-stamp].
95 Can be toggled by \\[time-stamp-toggle-active].
96 See also the variable `time-stamp-warn-inactive'."
97 :type 'boolean
98 :group 'time-stamp)
100 (defcustom time-stamp-warn-inactive t
101 "Have \\[time-stamp] warn if a buffer did not get time-stamped.
102 If non-nil, a warning is displayed if `time-stamp-active' disables time
103 stamping and the buffer contains a template that would otherwise have
104 been updated."
105 :type 'boolean
106 :group 'time-stamp)
108 (defcustom time-stamp-old-format-warn 'ask
109 "Action if `time-stamp-format' is an old-style list.
110 If `error', the format is not used. If `ask', the user is queried about
111 using the time-stamp-format. If `warn', a warning is displayed.
112 If nil, no notification is given."
113 :type '(choice (const :tag "Don't use the format" error)
114 (const ask)
115 (const warn)
116 (const :tag "No notification" nil))
117 :group 'time-stamp)
119 (defcustom time-stamp-time-zone nil
120 "If non-nil, a string naming the timezone to be used by \\[time-stamp].
121 Format is the same as that used by the environment variable TZ on your system."
122 :type '(choice (const nil) string)
123 :group 'time-stamp)
126 ;;; Do not change time-stamp-line-limit, time-stamp-start,
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.
133 (defvar time-stamp-line-limit 8 ;Do not change!
134 "Lines of a file searched; positive counts from start, negative from end.
135 The patterns `time-stamp-start' and `time-stamp-end' must be found in
136 the first (last) `time-stamp-line-limit' lines of the file for the
137 file to be time-stamped by \\[time-stamp]. A value of 0 searches the
138 entire buffer (use with care).
140 Do not change `time-stamp-line-limit', `time-stamp-start', or
141 `time-stamp-end' for yourself or you will be incompatible
142 with other people's files! If you must change them for some application,
143 do so in the local variables section of the time-stamped file itself.")
146 (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change!
147 "Regexp after which the time stamp is written by \\[time-stamp].
148 See also the variables `time-stamp-end' and `time-stamp-line-limit'.
150 Do not change `time-stamp-line-limit', `time-stamp-start', or
151 `time-stamp-end' for yourself or you will be incompatible
152 with other people's files! If you must change them for some application,
153 do so in the local variables section of the time-stamped file itself.")
156 (defvar time-stamp-end "\\\\?[\">]" ;Do not change!
157 "Regexp marking the text after the time stamp.
158 \\[time-stamp] deletes the text between the first match of `time-stamp-start'
159 and the following match of `time-stamp-end', then writes the
160 time stamp specified by `time-stamp-format' between them.
162 The end text normally starts on the same line as the start text ends,
163 but if there are any newlines in `time-stamp-format', the same number
164 of newlines must separate the start and end. \\[time-stamp] tries
165 to not change the number of lines in the buffer. `time-stamp-inserts-lines'
166 controls this behavior.
168 Do not change `time-stamp-line-limit', `time-stamp-start', `time-stamp-end',
169 or `time-stamp-inserts-lines' for yourself or you will be incompatible
170 with other people's files! If you must change them for some application,
171 do so in the local variables section of the time-stamped file itself.")
174 (defvar time-stamp-inserts-lines nil ;Do not change!
175 "Whether time-stamp can change the number of lines in a file.
176 If nil, \\[time-stamp] skips as many lines as there are newlines in
177 `time-stamp-format' before looking for the `time-stamp-end' pattern,
178 thus it tries not to change the number of lines in the buffer.
179 If non-nil, \\[time-stamp] starts looking for the end pattern
180 immediately after the start pattern. This behavior can cause
181 unexpected changes in the buffer if used carelessly, but it is useful
182 for generating repeated time stamps.
184 Do not change `time-stamp-end' or `time-stamp-inserts-lines' for
185 yourself or you will be incompatible with other people's files!
186 If you must change them for some application, do so in the local
187 variables section of the time-stamped file itself.")
190 (defvar time-stamp-count 1 ;Do not change!
191 "How many templates \\[time-stamp] will look for in a buffer.
192 The same time-stamp will be written in each case.
194 Do not change `time-stamp-count' for yourself or you will be
195 incompatible with other people's files! If you must change it for
196 some application, do so in the local variables section of the
197 time-stamped file itself.")
200 (defvar time-stamp-pattern "%%" ;Do not change!
201 "Convenience variable setting all `time-stamp' location and format values.
202 This string has four parts, each of which is optional.
203 These four parts set `time-stamp-line-limit', `time-stamp-start',
204 `time-stamp-format', and `time-stamp-end'. See the documentation
205 for each of these variables for details.
207 The first part is a number followed by a slash; the number sets the number
208 of lines at the beginning (negative counts from end) of the file searched
209 for the time-stamp. The number and the slash may be omitted to use the
210 normal value.
212 The second part is a regexp identifying the pattern preceding the time stamp.
213 This part may be omitted to use the normal pattern.
215 The third part specifies the format of the time-stamp inserted. See
216 the documentation for `time-stamp-format' for details. Specify this
217 part as \"%%\" to use the normal format.
219 The fourth part is a regexp identifying the pattern following the time stamp.
220 This part may be omitted to use the normal pattern.
222 As an example, the default behavior can be specified something like this:
223 \"8/Time-stamp: [\\\"<]%:y-%02m-%02d %02H:%02M:%02S %u[\\\">]\"
225 Do not change `time-stamp-pattern' for yourself or you will be incompatible
226 with other people's files! Set it only in the local variables section
227 of the time-stamped file itself.")
231 ;;;###autoload
232 (defun time-stamp ()
233 "Update the time stamp string(s) in the buffer.
234 A template in a file can be automatically updated with a new time stamp
235 every time you save the file. Add this line to your .emacs file:
236 (add-hook 'write-file-hooks 'time-stamp)
237 Normally the template must appear in the first 8 lines of a file and
238 look like one of the following:
239 Time-stamp: <>
240 Time-stamp: \" \"
241 The time stamp is written between the brackets or quotes:
242 Time-stamp: <1998-02-18 10:20:51 gildea>
243 The time stamp is updated only if the variable `time-stamp-active' is non-nil.
244 The format of the time stamp is set by the variable `time-stamp-format'.
245 The variables `time-stamp-line-limit', `time-stamp-start', `time-stamp-end',
246 `time-stamp-count', and `time-stamp-inserts-lines' control finding the
247 template."
248 (interactive)
249 (let ((line-limit time-stamp-line-limit)
250 (ts-start time-stamp-start)
251 (ts-format time-stamp-format)
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)
258 (if (stringp time-stamp-pattern)
259 (progn
260 (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(.\\|\n\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern)
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)))
269 (and (match-beginning 6)
270 (setq ts-end (match-string 6 time-stamp-pattern)))))
271 (cond ((not (integerp line-limit))
272 (setq line-limit 8)
273 (message "time-stamp-line-limit is not an integer")
274 (sit-for 1)))
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.
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)))))
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
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)))
299 ((< line-limit 0)
300 (goto-char (setq search-limit (point-max)))
301 (forward-line line-limit)
302 (setq start (point)))
303 (t ;0 => no limit (use with care!)
304 (setq start (point-min))
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)
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].
318 Returns 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)
328 (not end)
329 (re-search-forward ts-start search-limit 'move))
330 (setq start (point))
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))))))))))))
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))
360 (let ((new-time-stamp (time-stamp-string ts-format)))
361 (if (and (stringp new-time-stamp)
362 (not (string-equal (buffer-substring start end)
363 new-time-stamp)))
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
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))))
381 ;;;###autoload
382 (defun time-stamp-toggle-active (&optional arg)
383 "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer.
384 With 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)))
390 (message "time-stamp is now %s." (if time-stamp-active "active" "off")))
393 (defun time-stamp-string (&optional ts-format)
394 "Generate the new string to be inserted by \\[time-stamp].
395 Optionally use format TS-FORMAT instead of `time-stamp-format' to
396 format the string."
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)
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 " ")))))
423 (defconst time-stamp-no-file "(no file)"
424 "String to use when the buffer is not associated with a file.")
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)
439 "Use a FORMAT to format date, time, file, and user information.
440 Optional second argument TIME is only for testing.
441 Implements non-time extensions to `format-time-string'
442 and all time-stamp-format compatibility."
443 (let ((fmt-len (length format))
444 (ind 0)
445 cur-char
446 (prev-char nil)
447 (result "")
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
460 (setq alt-form nil change-case nil require-padding nil field-width "")
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)
468 (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char)
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)
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))))
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))))
497 (setq field-result
498 (cond
499 ((eq cur-char ?%)
500 "%%")
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
530 (time-stamp-do-number cur-char alt-form field-width time))
531 ((eq cur-char ?H) ;hour, 0-23
532 (time-stamp-do-number cur-char alt-form field-width time))
533 ((eq cur-char ?I) ;hour, 1-12
534 (time-stamp-do-number cur-char alt-form field-width time))
535 ((eq cur-char ?m) ;month number, 1-12
536 (time-stamp-do-number cur-char alt-form field-width time))
537 ((eq cur-char ?M) ;minute, 0-59
538 (time-stamp-do-number cur-char alt-form field-width time))
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
546 (time-stamp-do-number cur-char alt-form field-width time))
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"))
552 (string-to-int (format-time-string "%Y" time)))
553 ((eq cur-char ?Y) ;4-digit year, new style
554 (string-to-int (format-time-string "%Y" time)))
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))
574 ((eq cur-char ?U) ;user full name
575 (user-full-name))
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))
580 ((eq cur-char ?h) ;mail host name
581 (time-stamp-mail-host-name))
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))))
601 (char-to-string cur-char)))))
602 (setq ind (1+ ind)))
603 result))
605 (defun time-stamp-do-number (format-char alt-form field-width time)
606 "Handle compatible FORMAT-CHAR where only default width/padding will change.
607 ALT-FORM is whether `#' specified. FIELD-WIDTH is the string
608 width specification or \"\". TIME is the time to convert."
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)))))
617 (defvar time-stamp-conversion-warn t
618 "Warn about soon-to-be-unsupported forms in `time-stamp-format'.
619 If nil, these warnings are disabled, which would be a bad idea!
620 You really need to update your files instead.
622 The new formats will work with old versions of Emacs.
623 New formats are being recommended now to allow `time-stamp-format'
624 to change in the future to be compatible with `format-time-string'.
625 The new forms being recommended now will continue to work then.")
628 (defun time-stamp-conv-warn (old-form new-form)
629 "Display a warning about a soon-to-be-obsolete format."
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*"))))
646 (defun time-stamp-mail-host-name ()
647 "Return the name of the host where the user receives mail.
648 This is the value of `mail-host-address' if bound and a string,
649 otherwise the value of the function `system-name'."
650 (or (and (boundp 'mail-host-address)
651 (stringp mail-host-address)
652 mail-host-address)
653 (system-name)))
655 ;;; the rest of this file is for version 1 compatibility
657 (defun time-stamp-fconcat (list sep)
658 "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
659 If an element of LIST is a symbol, it is funcalled to get the string to use;
660 the separator SEP is used between two strings obtained by funcalling a
661 symbol. Otherwise the element itself is inserted; no separator is used
662 around 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))
672 (setq return-string (concat return-string (car list)))
673 (setq insert-sep-p nil)))
674 (setq list (cdr list)))
675 return-string))
677 ;;; Some functions used in time-stamp-format
679 ;;; Could generate most of a message-id with
680 ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name)
682 ;;; pretty form, suitable for a title page
684 (defun time-stamp-month-dd-yyyy ()
685 "Return the current date as a string in \"Month DD, YYYY\" form."
686 (format-time-string "%B %e, %Y"))
688 (defun time-stamp-dd/mm/yyyy ()
689 "Return the current date as a string in \"DD/MM/YYYY\" form."
690 (format-time-string "%d/%m/%Y"))
692 ;;; same as __DATE__ in ANSI C
694 (defun time-stamp-mon-dd-yyyy ()
695 "Return the current date as a string in \"Mon DD YYYY\" form.
696 The first character of DD is space if the value is less than 10."
697 (format-time-string "%b %d %Y"))
699 ;;; RFC 822 date
701 (defun time-stamp-dd-mon-yy ()
702 "Return the current date as a string in \"DD Mon YY\" form."
703 (format-time-string "%d %b %y"))
705 ;;; RCS 3 date
707 (defun time-stamp-yy/mm/dd ()
708 "Return the current date as a string in \"YY/MM/DD\" form."
709 (format-time-string "%y/%m/%d"))
711 ;;; RCS 5 date
713 (defun time-stamp-yyyy/mm/dd ()
714 "Return the current date as a string in \"YYYY/MM/DD\" form."
715 (format-time-string "%Y/%m/%d"))
717 ;;; ISO 8601 date
719 (defun time-stamp-yyyy-mm-dd ()
720 "Return the current date as a string in \"YYYY-MM-DD\" form."
721 (format-time-string "%Y-%m-%d"))
723 (defun time-stamp-yymmdd ()
724 "Return the current date as a string in \"YYMMDD\" form."
725 (format-time-string "%y%m%d"))
727 (defun time-stamp-hh:mm:ss ()
728 "Return the current time as a string in \"HH:MM:SS\" form."
729 (format-time-string "%T"))
731 (defun time-stamp-hhmm ()
732 "Return the current time as a string in \"HHMM\" form."
733 (format-time-string "%H%M"))
735 (provide 'time-stamp)
737 ;;; time-stamp.el ends here