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