Added a test.

> Please do not remove arguments from the public functions. This may
> break
> code outside Org mode.
> 

Hm, sure, I assumed it's okay for this niche thing. Can we deprecate
the argument somehow?

Best,
Tim
From b886446be8ea02f38d9be6cccf6899d6de396d06 Mon Sep 17 00:00:00 2001
From: Tim Ruffing <cry...@timruffing.de>
Date: Tue, 13 Feb 2024 10:57:29 +0100
Subject: [PATCH] org-agenda: Make sure skipping warning/delay days never
 increases their number

* lisp/org-agenda.el (org-agenda-get-deadlines, org-agenda-get-scheduled):
Use minimum of warning/delay days specified in timestamp cookie and the
limit specified by `org-agenda-skip-deadline-prewarning-if-scheduled' or
`org-agenda-skip-scheduled-delay-if-deadline`, respectively.
* testing/lisp/test-org-agenda.el (test-org-agenda/skip-deadline-prewarning-if-scheduled):
New test
---
 lisp/org-agenda.el              | 15 +++++-------
 testing/lisp/test-org-agenda.el | 41 +++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 514359b62..53878e441 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -6399,14 +6399,14 @@ specification like [h]h:mm."
 		       (org-agenda--timestamp-to-absolute
 		        s base 'future (current-buffer) pos)))))
 	          (diff (- deadline current))
-	          (suppress-prewarning
+	          (max-wdays
 		   (let ((scheduled
 		          (and org-agenda-skip-deadline-prewarning-if-scheduled
                                (org-element-property
                                 :raw-value
                                 (org-element-property :scheduled el)))))
 		     (cond
-		      ((not scheduled) nil)
+		      ((not scheduled) most-positive-fixnum)
 		      ;; The current item has a scheduled date, so
 		      ;; evaluate its prewarning lead time.
 		      ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
@@ -6420,7 +6420,7 @@ specification like [h]h:mm."
 			    org-deadline-warning-days))
 		      ;; Set pre-warning to deadline.
 		      (t 0))))
-	          (wdays (or suppress-prewarning (org-get-wdays s))))
+	          (wdays (min max-wdays (org-get-wdays s))))
 	     (cond
 	      ;; Only display deadlines at their base date, at future
 	      ;; repeat occurrences or in today agenda.
@@ -6610,13 +6610,13 @@ scheduled items with an hour specification like [h]h:mm."
 	          (futureschedp (> schedule today))
 	          (habitp (and (fboundp 'org-is-habit-p)
                                (string= "habit" (org-element-property :STYLE el))))
-	          (suppress-delay
+	          (max-ddays
 		   (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
                                         (org-element-property
                                          :raw-value
                                          (org-element-property :deadline el)))))
 		     (cond
-		      ((not deadline) nil)
+		      ((not deadline) most-positive-fixnum)
 		      ;; The current item has a deadline date, so
 		      ;; evaluate its delay time.
 		      ((integerp org-agenda-skip-scheduled-delay-if-deadline)
@@ -6636,10 +6636,7 @@ scheduled items with an hour specification like [h]h:mm."
 		    ((and (string-match-p "--[0-9]+[hdwmy]" s)
 		          (> schedule (org-agenda--timestamp-to-absolute s)))
 		     0)
-		    (suppress-delay
-		     (let ((org-scheduled-delay-days suppress-delay))
-		       (org-get-wdays s t t)))
-		    (t (org-get-wdays s t)))))
+		    (t (min max-ddays (org-get-wdays s t))))))
 	     ;; Display scheduled items at base date (SCHEDULE), today if
 	     ;; scheduled before the current date, and at any repeat past
 	     ;; today.  However, skip delayed items and items that have
diff --git a/testing/lisp/test-org-agenda.el b/testing/lisp/test-org-agenda.el
index f98214b25..3348db473 100644
--- a/testing/lisp/test-org-agenda.el
+++ b/testing/lisp/test-org-agenda.el
@@ -687,6 +687,47 @@ Sunday      7 January 2024
     (should-not (org-agenda-files)))
   (org-test-agenda--kill-all-agendas))
 
+(ert-deftest test-org-agenda/skip-deadline-prewarning-if-scheduled ()
+  "Test `org-agenda-skip-deadline-prewarning-if-scheduled'."
+  (org-test-at-time
+   "2024-01-15"
+   (let ((org-agenda-skip-deadline-prewarning-if-scheduled t))
+     (org-test-agenda-with-agenda
+      "* TODO foo\nDEADLINE: <2024-01-20 Sat> SCHEDULED: <2024-01-19 Fri>"
+      (org-agenda-list nil nil 1)
+      (should-not (search-forward "In " nil t))))
+   (let ((org-agenda-skip-deadline-prewarning-if-scheduled 10))
+     (org-test-agenda-with-agenda
+      "* TODO foo\nDEADLINE: <2024-01-20 Sat> SCHEDULED: <2024-01-19 Fri>"
+      (org-agenda-list nil nil 1)
+      (should (search-forward "In " nil t))))
+   ;; Custom prewarning cookie "-3d", so there should be no warning anyway.
+   (let ((org-agenda-skip-deadline-prewarning-if-scheduled 10))
+     (org-test-agenda-with-agenda
+      "* TODO foo\nDEADLINE: <2024-01-20 Sat -3d> SCHEDULED: <2024-01-19 Fri>"
+      (org-agenda-list nil nil 1)
+      (should-not (search-forward "In " nil t))))
+   (let ((org-agenda-skip-deadline-prewarning-if-scheduled 3))
+     (org-test-agenda-with-agenda
+      "* TODO foo\nDEADLINE: <2024-01-20 Sat> SCHEDULED: <2024-01-19 Fri>"
+      (org-agenda-list nil nil 1)
+      (should-not (search-forward "In " nil t))))
+   (let ((org-agenda-skip-deadline-prewarning-if-scheduled nil))
+     (org-test-agenda-with-agenda
+      "* TODO foo\nDEADLINE: <2024-01-20 Sat> SCHEDULED: <2024-01-19 Fri>"
+      (org-agenda-list nil nil 1)
+      (should (search-forward "In " nil t))))
+   (let ((org-agenda-skip-deadline-prewarning-if-scheduled 'pre-scheduled))
+     (org-test-agenda-with-agenda
+      "* TODO foo\nDEADLINE: <2024-01-20 Sat> SCHEDULED: <2024-01-16 Tue>"
+      (org-agenda-list nil nil 1)
+      (should-not (search-forward "In " nil t))))
+   (let ((org-agenda-skip-deadline-prewarning-if-scheduled 'pre-scheduled))
+     (org-test-agenda-with-agenda
+      "* TODO foo\nDEADLINE: <2024-01-20 Sat> SCHEDULED: <2024-01-15 Mon>"
+      (org-agenda-list nil nil 1)
+      (should (search-forward "In " nil t))))))
+
 
 ;; agenda redo
 
-- 
2.43.1

Reply via email to