From 4075662c298da7fa7e2ba19e0b8b36c58852cc0f Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Wed, 21 Sep 2022 12:42:34 +0800 Subject: [PATCH] org-diary-sexp-entry: Cache results * lisp/org.el (org--diary-sexp-entry-cache): New variable holding cached return values of `org-diary-sexp-entry'. (org-diary-sexp-entry): Use `org--diary-sexp-entry-cache'. --- lisp/org.el | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index c0b505e0aa..98fbd95e13 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -14425,34 +14425,39 @@ D may be an absolute day number, or a calendar-type list (month day year)." (let ((hl (calendar-check-holidays org-agenda-current-date))) (and hl (mapconcat #'identity hl "; ")))) +(defvar org--diary-sexp-entry-cache (make-hash-table :test #'equal) + "Hash table holding return values of `org-diary-sexp-entry'.") (defun org-diary-sexp-entry (sexp entry d) "Process a SEXP diary ENTRY for date D." (require 'diary-lib) ;; `org-anniversary' and alike expect ENTRY and DATE to be bound ;; dynamically. - (let* ((sexp `(let ((entry ,entry) - (date ',d)) - ,(car (read-from-string sexp)))) - ;; FIXME: Do not use (eval ... t) in the following sexp as - ;; diary vars are still using dynamic scope. - (result (if calendar-debug-sexp (eval sexp) - (condition-case nil - (eval sexp) - (error - (beep) - (message "Bad sexp at line %d in %s: %s" - (org-current-line) - (buffer-file-name) sexp) - (sleep-for 2)))))) - (cond ((stringp result) (split-string result "; ")) - ((and (consp result) - (not (consp (cdr result))) - (stringp (cdr result))) - (cdr result)) - ((and (consp result) - (stringp (car result))) - result) - (result entry)))) + (or (gethash (list sexp entry d) org--diary-sexp-entry-cache) + (puthash (list sexp entry d) + (let* ((sexp `(let ((entry ,entry) + (date ',d)) + ,(car (read-from-string sexp)))) + ;; FIXME: Do not use (eval ... t) in the following sexp as + ;; diary vars are still using dynamic scope. + (result (if calendar-debug-sexp (eval sexp) + (condition-case nil + (eval sexp) + (error + (beep) + (message "Bad sexp at line %d in %s: %s" + (org-current-line) + (buffer-file-name) sexp) + (sleep-for 2)))))) + (cond ((stringp result) (split-string result "; ")) + ((and (consp result) + (not (consp (cdr result))) + (stringp (cdr result))) + (cdr result)) + ((and (consp result) + (stringp (car result))) + result) + (result entry))) + org--diary-sexp-entry-cache))) (defun org-diary-to-ical-string (frombuf) "Get iCalendar entries from diary entries in buffer FROMBUF.