time-stamp: fix search end, more compatibility suggestions

* lisp/time-stamp.el (time-stamp): Use a marker for the
search end limit so we don't insert past it.
(time-stamp-conv-warn): Include a second suggested conversion
that might be what was intended.
Include a link to variable 'time-stamp-format'.
* test/lisp/time-stamp-tests.el: more tests
This commit is contained in:
Stephen Gildea 2024-12-14 10:23:04 -08:00
parent 567566ca08
commit 33532c5899
2 changed files with 62 additions and 19 deletions

View file

@ -374,20 +374,21 @@ The time stamp is updated only if `time-stamp-active' is non-nil."
(cond ((> line-limit 0)
(goto-char (setq start (point-min)))
(forward-line line-limit)
(setq search-limit (point)))
(setq search-limit (point-marker)))
((< line-limit 0)
(goto-char (setq search-limit (point-max)))
(goto-char (setq search-limit (point-max-marker)))
(forward-line line-limit)
(setq start (point)))
(t ;0 => no limit (use with care!)
(setq start (point-min))
(setq search-limit (point-max))))))
(setq search-limit (point-max-marker))))))
(while (and start
(< start search-limit)
(> ts-count 0))
(setq start (time-stamp-once start search-limit ts-start ts-end
ts-format format-lines end-lines))
(setq ts-count (1- ts-count))))
(setq ts-count (1- ts-count)))
(set-marker search-limit nil))
nil)
(defun time-stamp-once (start search-limit ts-start ts-end
@ -598,7 +599,8 @@ and all `time-stamp-format' compatibility."
(string-equal field-width ""))
(time-stamp--format "%A" time)
(time-stamp-conv-warn (format "%%%sA" field-width)
(format "%%#%sA" field-width))
(format "%%#%sA" field-width)
(format "%%:%sA" field-width))
(time-stamp--format "%#A" time)))))
((eq cur-char ?b) ;month name
(if (> alt-form 0)
@ -623,7 +625,8 @@ and all `time-stamp-format' compatibility."
(string-equal field-width ""))
(time-stamp--format "%B" time)
(time-stamp-conv-warn (format "%%%sB" field-width)
(format "%%#%sB" field-width))
(format "%%#%sB" field-width)
(format "%%:%sB" field-width))
(time-stamp--format "%#B" time)))))
((eq cur-char ?d) ;day of month, 1-31
(time-stamp-do-number cur-char alt-form field-width time))
@ -686,7 +689,7 @@ and all `time-stamp-format' compatibility."
(not flag-pad-with-spaces)
(not flag-pad-with-zeros)
(= field-width-num 0))
(time-stamp-conv-warn "%z" "%#Z")
(time-stamp-conv-warn "%z" "%#Z" "%5z")
(time-stamp--format "%#Z" time))
(t (time-stamp-formatz-from-parsed-options
flag-minimize
@ -766,20 +769,34 @@ to change in the future to be compatible with `format-time-string'.
The new forms being recommended now will continue to work then.")
(defun time-stamp-conv-warn (old-form new-form)
(defun time-stamp-conv-warn (old-form new-form &optional standard-form)
"Display a warning about a soon-to-be-obsolete format.
Suggests replacing OLD-FORM with NEW-FORM."
Suggests replacing OLD-FORM with NEW-FORM (same effect, but stable)
or (if provided) STANDARD-FORM (the effect the user may have expected
if they didn't read the documentation)."
(cond
(time-stamp-conversion-warn
(with-current-buffer (get-buffer-create "*Time-stamp-compatibility*")
(goto-char (point-max))
(if (bobp)
(progn
(insert
"The formats recognized in time-stamp-format will change in a future release\n"
"to be more compatible with the format-time-string function.\n\n"
"The following obsolescent time-stamp-format construct(s) were found:\n\n")))
(insert "\"" old-form "\" -- use " new-form "\n"))
(cond
((bobp)
(insert
(substitute-quotes
(concat
"The conversions recognized in `time-stamp-format' will change in a future\n"
"release to be more compatible with the function `format-time-string'.\n"
(cond
(standard-form
(concat
"Conversions that are changing are ambiguous and should be replaced by\n"
"stable conversions that makes your intention clear.\n")))
"\n"
"The following obsolescent `time-stamp-format' conversion(s) were found:\n\n")))))
(insert old-form " -- use " new-form)
(if standard-form
(insert " or " standard-form))
(insert "\n")
(help-make-xrefs))
(display-buffer "*Time-stamp-compatibility*"))))

View file

@ -36,7 +36,7 @@
(ref-time3 '(21377 34956)) ;Sunday, May 25, 2014, 06:07:08 AM
(time-stamp-time-zone t)) ;use UTC
(cl-letf (((symbol-function 'time-stamp-conv-warn)
(lambda (old-format _new)
(lambda (old-format _new &optional _newer)
(ert-fail
(format "Unexpected format warning for '%s'" old-format)))))
;; Not all reference times are used in all tests;
@ -66,7 +66,7 @@
(declare (debug t))
`(let ((warning-count 0))
(cl-letf (((symbol-function 'time-stamp-conv-warn)
(lambda (_old _new)
(lambda (_old _new &optional _newer)
(setq warning-count (1+ warning-count)))))
(should ,form)
(if (not (= warning-count 1))
@ -264,6 +264,28 @@
(time-stamp)
(should (equal (buffer-string) buffer-expected-twice)))))))
(ert-deftest time-stamp-custom-limit ()
"Test that `time-stamp' can expand two templates near the line limit."
(with-time-stamp-test-env
(let ((time-stamp-start "TS: ")
(time-stamp-format "%Y-%m-%d")
(time-stamp-end "$")
(time-stamp-count 2)
(time-stamp-line-limit 1) ;changed later in the test
(buffer-starts-as "TS: \nTS: ")
(buffer-expected-1 "TS: 2006-01-02\nTS: ")
(buffer-expected-2 "TS: 2006-01-02\nTS: 2006-01-02"))
(with-time-stamp-test-time ref-time1
(with-temp-buffer
(insert buffer-starts-as)
(time-stamp)
(should (equal (buffer-string) buffer-expected-1)))
(with-temp-buffer
(insert buffer-starts-as)
(setq time-stamp-line-limit 2)
(time-stamp)
(should (equal (buffer-string) buffer-expected-2)))))))
;;; Tests of time-stamp-string formatting
(ert-deftest time-stamp-format-day-of-week ()
@ -728,6 +750,10 @@
(should (equal (time-stamp-string "%03d" ref-time3) "025"))
(should (equal (time-stamp-string "%3d" ref-time3) " 25"))
(should (equal (time-stamp-string "%_3d" ref-time3) " 25"))
(should (equal (time-stamp-string "%99z" ref-time1)
(time-stamp-string "%100z" ref-time1)))
(should (equal (time-stamp-string "%099Y" ref-time1)
(time-stamp-string "%0100Y" ref-time1)))
;; since 2024
(should (equal (time-stamp-string "%0d" ref-time1) "02"))
(should (equal (time-stamp-string "%0d" ref-time2) "18"))
@ -839,7 +865,7 @@ and is called by some low-level `time-stamp' \"%z\" unit tests."
(- (fz-make+zone h m s)))
(defmacro formatz-should-equal (zone expect)
"Format ZONE and compares it to EXPECT.
"Format ZONE and compare it to EXPECT.
Use the free variables `form-string' and `pattern-mod'.
The functions in `pattern-mod' are composed left to right."
(declare (debug t))