Don't require isearch-update before isearch-done
[emacs.git] / lisp / time-stamp.el
blobc053ea6e924ae5c6bd8c2ac1dac195b18f9ee58f
1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
3 ;; Copyright (C) 1989, 1993-1995, 1997, 2000-2016 Free Software
4 ;; Foundation, Inc.
6 ;; This file is part of GNU Emacs.
8 ;; Maintainer: Stephen Gildea <gildea@stop.mail-abuse.org>
9 ;; Keywords: tools
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; A template in a file can be updated with a new time stamp when
27 ;; you save the file. For example:
28 ;; static char *ts = "sdmain.c Time-stamp: <2001-08-13 10:20:51 gildea>";
30 ;; To use time-stamping, add this line to your init file:
31 ;; (add-hook 'before-save-hook 'time-stamp)
32 ;; Now any time-stamp templates in your files will be updated automatically.
34 ;; See the documentation for the functions `time-stamp'
35 ;; and `time-stamp-toggle-active' for details.
37 ;;; Code:
39 (defgroup time-stamp nil
40 "Maintain last change time stamps in files edited by Emacs."
41 :group 'data
42 :group 'extensions)
44 (defcustom time-stamp-format "%:y-%02m-%02d %02H:%02M:%02S %u"
45 "Format of the string inserted by \\[time-stamp].
46 This is a string, used verbatim except for character sequences beginning
47 with %, as follows. The values of non-numeric formatted items depend
48 on the locale setting recorded in `system-time-locale' and
49 `locale-coding-system'. The examples here are for the default
50 \(`C') locale.
52 %:a weekday name: `Monday'. %#A gives uppercase: `MONDAY'
53 %3a abbreviated weekday: `Mon'. %3A gives uppercase: `MON'
54 %:b month name: `January'. %#B gives uppercase: `JANUARY'
55 %3b abbreviated month: `Jan'. %3B gives uppercase: `JAN'
56 %02d day of month
57 %02H 24-hour clock hour
58 %02I 12-hour clock hour
59 %02m month number
60 %02M minute
61 %#p `am' or `pm'. %P gives uppercase: `AM' or `PM'
62 %02S seconds
63 %w day number of week, Sunday is 0
64 %02y 2-digit year: `03' %:y 4-digit year: `2003'
65 %z time zone name: `est'. %Z gives uppercase: `EST'
67 Non-date items:
68 %% a literal percent character: `%'
69 %f file name without directory %F gives absolute pathname
70 %s system name
71 %u user's login name %U user's full name
72 %h mail host name
74 Decimal digits between the % and the type character specify the
75 field width. Strings are truncated on the right; years on the left.
76 A leading zero in the field width zero-fills a number.
78 For example, to get the format used by the `date' command,
79 use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\".
81 In the future these formats will be aligned more with `format-time-string'.
82 Because of this transition, the default padding for numeric formats will
83 change in a future version. Therefore either a padding width should be
84 specified, or the : modifier should be used to explicitly request the
85 historical default."
86 :type 'string
87 :group 'time-stamp
88 :version "20.1")
89 ;;;###autoload(put 'time-stamp-format 'safe-local-variable 'stringp)
91 (defcustom time-stamp-active t
92 "Non-nil to enable time-stamping of buffers by \\[time-stamp].
93 Can be toggled by \\[time-stamp-toggle-active].
94 See also the variable `time-stamp-warn-inactive'."
95 :type 'boolean
96 :group 'time-stamp)
98 (defcustom time-stamp-warn-inactive t
99 "Have \\[time-stamp] warn if a buffer did not get time-stamped.
100 If non-nil, a warning is displayed if `time-stamp-active' has
101 deactivated time stamping and the buffer contains a template that
102 otherwise would have been updated."
103 :type 'boolean
104 :group 'time-stamp
105 :version "19.29")
107 (defcustom time-stamp-time-zone nil
108 "The time zone to be used by \\[time-stamp].
109 Its format is that of the ZONE argument of the `format-time-string' function."
110 :type '(choice (const :tag "Emacs local time" nil)
111 (const :tag "Universal Time" t)
112 (const :tag "system wall clock time" wall)
113 (string :tag "TZ environment variable value"))
114 :group 'time-stamp
115 :version "20.1")
116 ;;;###autoload(put 'time-stamp-time-zone 'safe-local-variable 'string-or-null-p)
118 ;;; Do not change time-stamp-line-limit, time-stamp-start,
119 ;;; time-stamp-end, time-stamp-pattern, time-stamp-inserts-lines,
120 ;;; or time-stamp-count in your .emacs or you will be incompatible
121 ;;; with other people's files! If you must change them, do so only
122 ;;; in the local variables section of the file itself.
125 (defvar time-stamp-line-limit 8 ;Do not change!
126 "Lines of a file searched; positive counts from start, negative from end.
127 The patterns `time-stamp-start' and `time-stamp-end' must be found in
128 the first (last) `time-stamp-line-limit' lines of the file for the
129 file to be time-stamped by \\[time-stamp]. A value of 0 searches the
130 entire buffer (use with care).
132 This value can also be set with the variable `time-stamp-pattern'.
134 Do not change `time-stamp-line-limit', `time-stamp-start',
135 `time-stamp-end', or `time-stamp-pattern' for yourself or you will be
136 incompatible with other people's files! If you must change them for some
137 application, do so in the local variables section of the time-stamped file
138 itself.")
139 ;;;###autoload(put 'time-stamp-line-limit 'safe-local-variable 'integerp)
141 (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change!
142 "Regexp after which the time stamp is written by \\[time-stamp].
143 See also the variables `time-stamp-end' and `time-stamp-line-limit'.
145 This value can also be set with the variable `time-stamp-pattern'.
147 Do not change `time-stamp-line-limit', `time-stamp-start',
148 `time-stamp-end', or `time-stamp-pattern' for yourself or you will be
149 incompatible with other people's files! If you must change them for some
150 application, do so in the local variables section of the time-stamped file
151 itself.")
152 ;;;###autoload(put 'time-stamp-start 'safe-local-variable 'stringp)
154 (defvar time-stamp-end "\\\\?[\">]" ;Do not change!
155 "Regexp marking the text after the time stamp.
156 \\[time-stamp] deletes the text between the first match of `time-stamp-start'
157 and the following match of `time-stamp-end', then writes the
158 time stamp specified by `time-stamp-format' between them.
160 This value can also be set with the variable `time-stamp-pattern'.
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-start', `time-stamp-end', `time-stamp-pattern',
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.")
172 ;;;###autoload(put 'time-stamp-end 'safe-local-variable 'stringp)
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.")
189 ;;;###autoload(put 'time-stamp-inserts-lines 'safe-local-variable 'symbolp)
192 (defvar time-stamp-count 1 ;Do not change!
193 "How many templates \\[time-stamp] will look for in a buffer.
194 The same time stamp will be written in each case.
196 Do not change `time-stamp-count' for yourself or you will be
197 incompatible with other people's files! If you must change it for
198 some application, do so in the local variables section of the
199 time-stamped file itself.")
200 ;;;###autoload(put 'time-stamp-count 'safe-local-variable 'integerp)
203 (defvar time-stamp-pattern nil ;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 Examples:
226 \"-10/\"
227 \"-9/^Last modified: %%$\"
228 \"@set Time-stamp: %:b %:d, %:y$\"
229 \"newcommand{\\\\\\\\timestamp}{%%}\"
231 Do not change `time-stamp-pattern' `time-stamp-line-limit',
232 `time-stamp-start', or `time-stamp-end' for yourself or you will be
233 incompatible with other people's files! If you must change them for
234 some application, do so only in the local variables section of the
235 time-stamped file itself.")
236 ;;;###autoload(put 'time-stamp-pattern 'safe-local-variable 'stringp)
240 ;;;###autoload
241 (defun time-stamp ()
242 "Update the time stamp string(s) in the buffer.
243 A template in a file can be automatically updated with a new time stamp
244 every time you save the file. Add this line to your init file:
245 (add-hook \\='before-save-hook \\='time-stamp)
246 or customize `before-save-hook' through Custom.
247 Normally the template must appear in the first 8 lines of a file and
248 look like one of the following:
249 Time-stamp: <>
250 Time-stamp: \" \"
251 The time stamp is written between the brackets or quotes:
252 Time-stamp: <2001-02-18 10:20:51 gildea>
253 The time stamp is updated only if the variable `time-stamp-active' is non-nil.
254 The format of the time stamp is set by the variable `time-stamp-pattern' or
255 `time-stamp-format'. The variables `time-stamp-pattern',
256 `time-stamp-line-limit', `time-stamp-start', `time-stamp-end',
257 `time-stamp-count', and `time-stamp-inserts-lines' control finding
258 the template."
259 (interactive)
260 (let ((line-limit time-stamp-line-limit)
261 (ts-start time-stamp-start)
262 (ts-format time-stamp-format)
263 (ts-end time-stamp-end)
264 (ts-count time-stamp-count)
265 (format-lines 0)
266 (end-lines 1)
267 (start nil)
268 search-limit)
269 (if (stringp time-stamp-pattern)
270 (progn
271 (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(%[-.,:@+_ #^()0-9]*[A-Za-z%][^%]*\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern)
272 (and (match-beginning 2)
273 (setq line-limit
274 (string-to-number (match-string 2 time-stamp-pattern))))
275 (and (match-beginning 3)
276 (setq ts-start (match-string 3 time-stamp-pattern)))
277 (and (match-beginning 4)
278 (not (string-equal (match-string 4 time-stamp-pattern) "%%"))
279 (setq ts-format (match-string 4 time-stamp-pattern)))
280 (and (match-beginning 6)
281 (setq ts-end (match-string 6 time-stamp-pattern)))))
282 (cond ((not (integerp line-limit))
283 (setq line-limit 8)
284 (message "time-stamp-line-limit is not an integer")
285 (sit-for 1)))
286 (cond ((not (integerp ts-count))
287 (setq ts-count 1)
288 (message "time-stamp-count is not an integer")
289 (sit-for 1))
290 ((< ts-count 1)
291 ;; We need to call time-stamp-once at least once
292 ;; to output any warnings about time-stamp not being active.
293 (setq ts-count 1)))
294 ;; Figure out what lines the end should be on.
295 (if (stringp ts-format)
296 (let ((nl-start 0))
297 (while (string-match "\n" ts-format nl-start)
298 (setq format-lines (1+ format-lines) nl-start (match-end 0)))))
299 (let ((nl-start 0))
300 (while (string-match "\n" ts-end nl-start)
301 (setq end-lines (1+ end-lines) nl-start (match-end 0))))
302 ;; Find overall what lines to look at
303 (save-excursion
304 (save-restriction
305 (widen)
306 (cond ((> line-limit 0)
307 (goto-char (setq start (point-min)))
308 (forward-line line-limit)
309 (setq search-limit (point)))
310 ((< line-limit 0)
311 (goto-char (setq search-limit (point-max)))
312 (forward-line line-limit)
313 (setq start (point)))
314 (t ;0 => no limit (use with care!)
315 (setq start (point-min))
316 (setq search-limit (point-max))))))
317 (while (and start
318 (< start search-limit)
319 (> ts-count 0))
320 (setq start (time-stamp-once start search-limit ts-start ts-end
321 ts-format format-lines end-lines))
322 (setq ts-count (1- ts-count))))
323 nil)
325 (defun time-stamp-once (start search-limit ts-start ts-end
326 ts-format format-lines end-lines)
327 "Update one time stamp. Internal routine called by \\[time-stamp].
328 Returns the end point, which is where `time-stamp' begins the next search."
329 (let ((case-fold-search nil)
330 (end nil)
331 end-search-start
332 (end-length nil))
333 (save-excursion
334 (save-restriction
335 (widen)
336 ;; Find the location of the time stamp.
337 (while (and (< (goto-char start) search-limit)
338 (not end)
339 (re-search-forward ts-start search-limit 'move))
340 (setq start (point))
341 (if (not time-stamp-inserts-lines)
342 (forward-line format-lines))
343 (setq end-search-start (max start (point)))
344 (if (= (forward-line end-lines) 0)
345 (progn
346 (and (bolp) (backward-char))
347 (let ((line-end (min (point) search-limit)))
348 (if (>= line-end end-search-start)
349 (progn
350 (goto-char end-search-start)
351 (if (re-search-forward ts-end line-end t)
352 (progn
353 (setq end (match-beginning 0))
354 (setq end-length (- (match-end 0) end))))))))))))
355 (if end
356 (progn
357 ;; do all warnings outside save-excursion
358 (cond
359 ((not time-stamp-active)
360 (if time-stamp-warn-inactive
361 ;; don't signal an error in a write-file-hook
362 (progn
363 (message "Warning: time-stamp-active is off; did not time-stamp buffer.")
364 (sit-for 1))))
365 ((not (and (stringp ts-start)
366 (stringp ts-end)))
367 (message "time-stamp-start or time-stamp-end is not a string")
368 (sit-for 1))
370 (let ((new-time-stamp (time-stamp-string ts-format)))
371 (if (and (stringp new-time-stamp)
372 (not (string-equal (buffer-substring start end)
373 new-time-stamp)))
374 (save-excursion
375 (save-restriction
376 (widen)
377 (delete-region start end)
378 (goto-char start)
379 (insert-and-inherit new-time-stamp)
380 (setq end (point))
381 ;; remove any tabs used to format time stamp
382 (if (search-backward "\t" start t)
383 (progn
384 (untabify start end)
385 (setq end (point))))))))))))
386 ;; return the location after this time stamp, if there was one
387 (and end end-length
388 (+ end end-length))))
391 ;;;###autoload
392 (defun time-stamp-toggle-active (&optional arg)
393 "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer.
394 With ARG, turn time stamping on if and only if arg is positive."
395 (interactive "P")
396 (setq time-stamp-active
397 (if (null arg)
398 (not time-stamp-active)
399 (> (prefix-numeric-value arg) 0)))
400 (message "time-stamp is now %s." (if time-stamp-active "active" "off")))
402 (defun time-stamp--format (format time)
403 (format-time-string format time time-stamp-time-zone))
405 (defun time-stamp-string (&optional ts-format)
406 "Generate the new string to be inserted by \\[time-stamp].
407 Optionally use format TS-FORMAT instead of `time-stamp-format' to
408 format the string."
409 (if (stringp (or ts-format (setq ts-format time-stamp-format)))
410 (time-stamp--format (time-stamp-string-preprocess ts-format) nil)))
413 (defconst time-stamp-no-file "(no file)"
414 "String to use when the buffer is not associated with a file.")
416 ;;; FIXME This comment was written in 1996!
417 ;;; time-stamp is transitioning to using the new, expanded capabilities
418 ;;; of format-time-string. During the process, this function implements
419 ;;; intermediate, compatible formats and complains about old, soon to
420 ;;; be unsupported, formats. This function will get a lot (a LOT) shorter
421 ;;; when the transition is complete and we can just pass most things
422 ;;; straight through to format-time-string.
423 ;;; At all times, all the formats recommended in the doc string
424 ;;; of time-stamp-format will work not only in the current version of
425 ;;; Emacs, but in all versions that have been released within the past
426 ;;; two years.
427 ;;; The : modifier is a temporary conversion feature used to resolve
428 ;;; ambiguous formats--formats that are changing (over time) incompatibly.
429 (defun time-stamp-string-preprocess (format &optional time)
430 "Use a FORMAT to format date, time, file, and user information.
431 Optional second argument TIME is only for testing.
432 Implements non-time extensions to `format-time-string'
433 and all `time-stamp-format' compatibility."
434 (let ((fmt-len (length format))
435 (ind 0)
436 cur-char
437 (prev-char nil)
438 (result "")
439 field-width
440 field-result
441 alt-form change-case
442 (paren-level 0))
443 (while (< ind fmt-len)
444 (setq cur-char (aref format ind))
445 (setq
446 result
447 (concat result
448 (cond
449 ((eq cur-char ?%)
450 ;; eat any additional args to allow for future expansion
451 (setq alt-form nil change-case nil field-width "")
452 (while (progn
453 (setq ind (1+ ind))
454 (setq cur-char (if (< ind fmt-len)
455 (aref format ind)
456 ?\0))
457 (or (eq ?. cur-char)
458 (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char)
459 (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char)
460 (eq ?\s cur-char) (eq ?# cur-char) (eq ?^ cur-char)
461 (and (eq ?\( cur-char)
462 (not (eq prev-char ?\\))
463 (setq paren-level (1+ paren-level)))
464 (if (and (eq ?\) cur-char)
465 (not (eq prev-char ?\\))
466 (> paren-level 0))
467 (setq paren-level (1- paren-level))
468 (and (> paren-level 0)
469 (< ind fmt-len)))
470 (if (and (<= ?0 cur-char) (>= ?9 cur-char))
471 ;; get format width
472 (let ((field-index ind))
473 (while (progn
474 (setq ind (1+ ind))
475 (setq cur-char (if (< ind fmt-len)
476 (aref format ind)
477 ?\0))
478 (and (<= ?0 cur-char) (>= ?9 cur-char))))
479 (setq field-width (substring format field-index ind))
480 (setq ind (1- ind))
481 t))))
482 (setq prev-char cur-char)
483 ;; some characters we actually use
484 (cond ((eq cur-char ?:)
485 (setq alt-form t))
486 ((eq cur-char ?#)
487 (setq change-case t))))
488 (setq field-result
489 (cond
490 ((eq cur-char ?%)
491 "%%")
492 ((eq cur-char ?a) ;day of week
493 (if change-case
494 (time-stamp--format "%#a" time)
495 (or alt-form (not (string-equal field-width ""))
496 (time-stamp-conv-warn "%a" "%:a"))
497 (if (and alt-form (not (string-equal field-width "")))
498 "" ;discourage "%:3a"
499 (time-stamp--format "%A" time))))
500 ((eq cur-char ?A)
501 (if alt-form
502 (time-stamp--format "%A" time)
503 (or change-case (not (string-equal field-width ""))
504 (time-stamp-conv-warn "%A" "%#A"))
505 (time-stamp--format "%#A" time)))
506 ((eq cur-char ?b) ;month name
507 (if change-case
508 (time-stamp--format "%#b" time)
509 (or alt-form (not (string-equal field-width ""))
510 (time-stamp-conv-warn "%b" "%:b"))
511 (if (and alt-form (not (string-equal field-width "")))
512 "" ;discourage "%:3b"
513 (time-stamp--format "%B" time))))
514 ((eq cur-char ?B)
515 (if alt-form
516 (time-stamp--format "%B" time)
517 (or change-case (not (string-equal field-width ""))
518 (time-stamp-conv-warn "%B" "%#B"))
519 (time-stamp--format "%#B" time)))
520 ((eq cur-char ?d) ;day of month, 1-31
521 (time-stamp-do-number cur-char alt-form field-width time))
522 ((eq cur-char ?H) ;hour, 0-23
523 (time-stamp-do-number cur-char alt-form field-width time))
524 ((eq cur-char ?I) ;hour, 1-12
525 (time-stamp-do-number cur-char alt-form field-width time))
526 ((eq cur-char ?m) ;month number, 1-12
527 (time-stamp-do-number cur-char alt-form field-width time))
528 ((eq cur-char ?M) ;minute, 0-59
529 (time-stamp-do-number cur-char alt-form field-width time))
530 ((eq cur-char ?p) ;am or pm
531 (or change-case
532 (time-stamp-conv-warn "%p" "%#p"))
533 (time-stamp--format "%#p" time))
534 ((eq cur-char ?P) ;AM or PM
535 (time-stamp--format "%p" time))
536 ((eq cur-char ?S) ;seconds, 00-60
537 (time-stamp-do-number cur-char alt-form field-width time))
538 ((eq cur-char ?w) ;weekday number, Sunday is 0
539 (time-stamp--format "%w" time))
540 ((eq cur-char ?y) ;year
541 (or alt-form (not (string-equal field-width ""))
542 (time-stamp-conv-warn "%y" "%:y"))
543 (string-to-number (time-stamp--format "%Y" time)))
544 ((eq cur-char ?Y) ;4-digit year, new style
545 (string-to-number (time-stamp--format "%Y" time)))
546 ((eq cur-char ?z) ;time zone lower case
547 (if change-case
548 "" ;discourage %z variations
549 (time-stamp--format "%#Z" time)))
550 ((eq cur-char ?Z)
551 (if change-case
552 (time-stamp--format "%#Z" time)
553 (time-stamp--format "%Z" time)))
554 ((eq cur-char ?f) ;buffer-file-name, base name only
555 (if buffer-file-name
556 (file-name-nondirectory buffer-file-name)
557 time-stamp-no-file))
558 ((eq cur-char ?F) ;buffer-file-name, full path
559 (or buffer-file-name
560 time-stamp-no-file))
561 ((eq cur-char ?s) ;system name
562 (system-name))
563 ((eq cur-char ?u) ;user name
564 (user-login-name))
565 ((eq cur-char ?U) ;user full name
566 (user-full-name))
567 ((eq cur-char ?l) ;logname (undocumented user name alt)
568 (user-login-name))
569 ((eq cur-char ?L) ;(undocumented alt user full name)
570 (user-full-name))
571 ((eq cur-char ?h) ;mail host name
572 (time-stamp-mail-host-name))
573 ((eq cur-char ?q) ;(undocumented unqual hostname)
574 (let ((qualname (system-name)))
575 (if (string-match "\\." qualname)
576 (substring qualname 0 (match-beginning 0))
577 qualname)))
578 ((eq cur-char ?Q) ;(undocumented fully-qualified host)
579 (system-name))
581 (let ((padded-result
582 (format (format "%%%s%c"
583 field-width
584 (if (numberp field-result) ?d ?s))
585 (or field-result ""))))
586 (let* ((initial-length (length padded-result))
587 (desired-length (if (string-equal field-width "")
588 initial-length
589 (string-to-number field-width))))
590 (if (> initial-length desired-length)
591 ;; truncate strings on right, years on left
592 (if (stringp field-result)
593 (substring padded-result 0 desired-length)
594 (if (eq cur-char ?y)
595 (substring padded-result (- desired-length))
596 padded-result)) ;non-year numbers don't truncate
597 padded-result))))
599 (char-to-string cur-char)))))
600 (setq ind (1+ ind)))
601 result))
603 (defun time-stamp-do-number (format-char alt-form field-width time)
604 "Handle compatible FORMAT-CHAR where only default width/padding will change.
605 ALT-FORM is whether `#' specified. FIELD-WIDTH is the string
606 width specification or \"\". TIME is the time to convert."
607 (let ((format-string (concat "%" (char-to-string format-char))))
608 (and (not alt-form) (string-equal field-width "")
609 (time-stamp-conv-warn format-string
610 (format "%%:%c" format-char)))
611 (if (and alt-form (not (string-equal field-width "")))
612 "" ;discourage "%:2d" and the like
613 (string-to-number (time-stamp--format format-string time)))))
615 (defvar time-stamp-conversion-warn t
616 "Warn about soon-to-be-unsupported forms in `time-stamp-format'.
617 If nil, these warnings are disabled, which would be a bad idea!
618 You really need to update your files instead.
620 The new formats will work with old versions of Emacs.
621 New formats are being recommended now to allow `time-stamp-format'
622 to change in the future to be compatible with `format-time-string'.
623 The new forms being recommended now will continue to work then.")
626 (defun time-stamp-conv-warn (old-form new-form)
627 "Display a warning about a soon-to-be-obsolete format.
628 Suggests replacing OLD-FORM with NEW-FORM."
629 (cond
630 (time-stamp-conversion-warn
631 (with-current-buffer (get-buffer-create "*Time-stamp-compatibility*")
632 (goto-char (point-max))
633 (if (bobp)
634 (progn
635 (insert
636 "The formats recognized in time-stamp-format will change in a future release\n"
637 "to be compatible with the new, expanded format-time-string function.\n\n"
638 "The following obsolescent time-stamp-format construct(s) were found:\n\n")))
639 (insert "\"" old-form "\" -- use " new-form "\n"))
640 (display-buffer "*Time-stamp-compatibility*"))))
644 (defun time-stamp-mail-host-name ()
645 "Return the name of the host where the user receives mail.
646 This is the value of `mail-host-address' if bound and a string,
647 otherwise the value of the function `system-name'."
648 (or (and (boundp 'mail-host-address)
649 (stringp mail-host-address)
650 mail-host-address)
651 (system-name)))
653 (provide 'time-stamp)
655 ;;; time-stamp.el ends here