aboutsummaryrefslogtreecommitdiff
path: root/chime-org-contacts.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-04-04 12:57:47 -0500
committerCraig Jennings <c@cjennings.net>2026-04-04 12:57:47 -0500
commit8b2b1ecf2d1334531b04f630560525600e483a3a (patch)
treec83bd1271d1d49ddb67aa32b2eeeeedca6a0d7e3 /chime-org-contacts.el
parent2103b2cd2823f340a9587ee1b2143c93b84fd536 (diff)
downloadchime-8b2b1ecf2d1334531b04f630560525600e483a3a.tar.gz
chime-8b2b1ecf2d1334531b04f630560525600e483a3a.zip
Consolidate birthday utilities into convert-org-contacts-birthdays
chime-org-contacts.el duplicated parse, format, and insert-after-drawer functions from convert-org-contacts-birthdays.el. Remove the duplicates and have chime-org-contacts require the convert module instead. Removes ~35 lines of duplicated code. The finalize function now delegates to chime--insert-birthday-timestamp-after-drawer with condition-case for graceful handling of invalid birthday formats during capture.
Diffstat (limited to 'chime-org-contacts.el')
-rw-r--r--chime-org-contacts.el55
1 files changed, 12 insertions, 43 deletions
diff --git a/chime-org-contacts.el b/chime-org-contacts.el
index 33d6010..5f532f6 100644
--- a/chime-org-contacts.el
+++ b/chime-org-contacts.el
@@ -86,60 +86,29 @@ New contacts will be filed under this heading in `chime-org-contacts-file'."
;;; Implementation
-(defun chime-org-contacts--parse-birthday (birthday-string)
- "Parse BIRTHDAY-STRING into (YEAR MONTH DAY) list.
-YEAR may be current year if not present in the string.
-Returns nil if parsing fails."
- (cond
- ;; Format: YYYY-MM-DD
- ((string-match "^\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)$" birthday-string)
- (list (string-to-number (match-string 1 birthday-string))
- (string-to-number (match-string 2 birthday-string))
- (string-to-number (match-string 3 birthday-string))))
- ;; Format: MM-DD (use current year)
- ((string-match "^\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)$" birthday-string)
- (list (nth 5 (decode-time))
- (string-to-number (match-string 1 birthday-string))
- (string-to-number (match-string 2 birthday-string))))
- (t nil)))
-
-(defun chime-org-contacts--format-timestamp (year month day)
- "Format YEAR MONTH DAY as yearly repeating org timestamp."
- (let* ((time (encode-time 0 0 0 day month year))
- (dow (format-time-string "%a" time)))
- (format "<%04d-%02d-%02d %s +1y>" year month day dow)))
-
-(defun chime-org-contacts--insert-timestamp-after-drawer (timestamp)
- "Insert TIMESTAMP after properties drawer if not already present."
- (let ((heading-end (save-excursion (outline-next-heading) (point))))
- (when (re-search-forward "^[ \t]*:END:[ \t]*$" heading-end t)
- (let ((end-pos (point)))
- ;; Only insert if no yearly timestamp already exists
- (unless (save-excursion
- (goto-char end-pos)
- (re-search-forward "<[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^>]*\\+1y>" heading-end t))
- (goto-char end-pos)
- (end-of-line)
- (insert "\n" timestamp))))))
+;; Birthday parsing, formatting, and insertion are provided by
+;; convert-org-contacts-birthdays.el to avoid duplication.
+(require 'convert-org-contacts-birthdays
+ (expand-file-name "convert-org-contacts-birthdays.el"
+ (file-name-directory (or load-file-name buffer-file-name))))
(defun chime-org-contacts--finalize-birthday-timestamp ()
"Add yearly repeating timestamp after properties drawer if BIRTHDAY is set.
This function is called during org-capture finalization to automatically
insert a plain timestamp for birthdays, enabling them to appear in org-agenda
-without requiring org-contacts to be loaded in the async subprocess."
+without requiring org-contacts to be loaded in the async subprocess.
+
+Delegates to `chime--insert-birthday-timestamp-after-drawer' for the
+actual parsing, formatting, and insertion."
(when (string= (plist-get org-capture-plist :key) chime-org-contacts-capture-key)
(save-excursion
(goto-char (point-min))
(let ((birthday (org-entry-get (point) "BIRTHDAY")))
(when (and birthday (not (string-blank-p birthday)))
- (let ((parsed (chime-org-contacts--parse-birthday birthday)))
- (when parsed
- (let* ((year (nth 0 parsed))
- (month (nth 1 parsed))
- (day (nth 2 parsed))
- (timestamp (chime-org-contacts--format-timestamp year month day)))
- (chime-org-contacts--insert-timestamp-after-drawer timestamp)))))))))
+ (condition-case nil
+ (chime--insert-birthday-timestamp-after-drawer birthday)
+ (user-error nil)))))))
(defun chime-org-contacts--setup-capture-template ()
"Add org-capture template for contacts with birthday timestamps.