Bladeren bron

org-notify.el: Limit the number of notifications

(org-notify-max-notifications-per-run): New option for limiting the
notifications.
(org-notify-process): Use it.
Peter Münster 5 jaren geleden
bovenliggende
commit
b01332c87e
1 gewijzigde bestanden met toevoegingen van 36 en 27 verwijderingen
  1. 36 27
      contrib/lisp/org-notify.el

+ 36 - 27
contrib/lisp/org-notify.el

@@ -74,6 +74,11 @@
   :type 'boolean
   :type 'boolean
   :group 'org-notify)
   :group 'org-notify)
 
 
+(defcustom org-notify-max-notifications-per-run 3
+  "Maximum number of notifications per run of `org-notify-process'."
+  :type 'integer
+  :group 'org-notify)
+
 (defconst org-notify-actions
 (defconst org-notify-actions
   '("show" "show" "done" "done" "hour" "one hour later" "day" "one day later"
   '("show" "show" "done" "done" "hour" "one hour later" "day" "one day later"
     "week" "one week later")
     "week" "one week later")
@@ -158,33 +163,37 @@ PERIOD."
 (cl-defun org-notify-process ()
 (cl-defun org-notify-process ()
   "Process the todo-list, and possibly notify user about upcoming or
   "Process the todo-list, and possibly notify user about upcoming or
 forgotten tasks."
 forgotten tasks."
-  (cl-macrolet ((prm (k) `(plist-get prms ,k))  (td (k) `(plist-get todo ,k)))
-    (dolist (todo (org-notify-todo-list))
-      (let* ((deadline (td :deadline))  (heading (td :heading))
-             (uid (td :uid))            (last-run-sym
-                                         (intern (concat ":last-run-" uid))))
-        (cl-dolist (prms (plist-get org-notify-map (td :notify)))
-          (when (< deadline (org-notify-string->seconds (prm :time)))
-            (let ((period (org-notify-string->seconds (prm :period)))
-                  (last-run (prm last-run-sym))  (now (float-time))
-                  (actions (prm :actions))       diff  plist)
-              (when (or (not last-run)
-                        (and period (< period (setq diff (- now last-run)))
-                             (org-notify-maybe-too-late diff period heading)))
-                (setq prms (plist-put prms last-run-sym now)
-                      plist (append todo prms))
-                (if (if (plist-member prms :audible)
-                        (prm :audible)
-                      org-notify-audible)
-                    (ding))
-                (unless (listp actions)
-                  (setq actions (list actions)))
-                (dolist (action actions)
-                  (funcall (if (fboundp action) action
-                             (intern (concat "org-notify-action"
-                                             (symbol-name action))))
-                           plist))))
-            (cl-return)))))))
+  (let ((notification-cnt 0))
+    (cl-macrolet ((prm (k) `(plist-get prms ,k))  (td (k) `(plist-get todo ,k)))
+      (dolist (todo (org-notify-todo-list))
+	(let* ((deadline (td :deadline))  (heading (td :heading))
+               (uid (td :uid))            (last-run-sym
+                                           (intern (concat ":last-run-" uid))))
+          (cl-dolist (prms (plist-get org-notify-map (td :notify)))
+            (when (< deadline (org-notify-string->seconds (prm :time)))
+              (let ((period (org-notify-string->seconds (prm :period)))
+                    (last-run (prm last-run-sym))  (now (float-time))
+                    (actions (prm :actions))       diff  plist)
+		(when (or (not last-run)
+                          (and period (< period (setq diff (- now last-run)))
+                               (org-notify-maybe-too-late diff period heading)))
+                  (setq prms (plist-put prms last-run-sym now)
+			plist (append todo prms))
+                  (if (if (plist-member prms :audible)
+                          (prm :audible)
+			org-notify-audible)
+                      (ding))
+                  (unless (listp actions)
+                    (setq actions (list actions)))
+		  (cl-incf notification-cnt)
+                  (dolist (action actions)
+                    (funcall (if (fboundp action) action
+                               (intern (concat "org-notify-action"
+                                               (symbol-name action))))
+			     plist))
+		  (when (>= notification-cnt org-notify-max-notifications-per-run)
+		    (cl-return-from org-notify-process)))
+		(cl-return)))))))))
 
 
 (defun org-notify-add (name &rest params)
 (defun org-notify-add (name &rest params)
   "Add a new notification type.
   "Add a new notification type.