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