ol-bbdb.el (20632B)
1 ;;; ol-bbdb.el --- Links to BBDB entries -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2004-2024 Free Software Foundation, Inc. 4 5 ;; Authors: Carsten Dominik <carsten.dominik@gmail.com> 6 ;; Thomas Baumann <thomas dot baumann at ch dot tum dot de> 7 ;; Keywords: outlines, hypermedia, calendar, text 8 ;; URL: https://orgmode.org 9 ;; 10 ;; This file is part of GNU Emacs. 11 ;; 12 ;; GNU Emacs is free software: you can redistribute it and/or modify 13 ;; it under the terms of the GNU General Public License as published by 14 ;; the Free Software Foundation, either version 3 of the License, or 15 ;; (at your option) any later version. 16 17 ;; GNU Emacs is distributed in the hope that it will be useful, 18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 ;; GNU General Public License for more details. 21 22 ;; You should have received a copy of the GNU General Public License 23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 25 ;; 26 ;;; Commentary: 27 28 ;; This file implements links to BBDB database entries from within Org. 29 ;; Org mode loads this module by default - if this is not what you want, 30 ;; configure the variable `org-modules'. 31 32 ;; It also implements an interface (based on Ivar Rummelhoff's 33 ;; bbdb-anniv.el) for those Org users, who do not use the diary 34 ;; but who do want to include the anniversaries stored in the BBDB 35 ;; into the org-agenda. If you already include the `diary' into the 36 ;; agenda, you might want to prefer to include the anniversaries in 37 ;; the diary using bbdb-anniv.el. 38 ;; 39 ;; Put the following in /somewhere/at/home/diary.org and make sure 40 ;; that this file is in `org-agenda-files'. 41 ;; 42 ;; %%(org-bbdb-anniversaries) 43 ;; 44 ;; For example my diary.org looks like: 45 ;; * Anniversaries 46 ;; #+CATEGORY: Anniv 47 ;; %%(org-bbdb-anniversaries) 48 ;; 49 ;; 50 ;; To add an anniversary to a BBDB record, press `C-o' in the record. 51 ;; You will be prompted for the field name, in this case it must be 52 ;; "anniversary". If this is the first time you are using this field, 53 ;; you need to confirm that it should be created. 54 ;; 55 ;; The format of an anniversary field stored in BBDB is the following 56 ;; (items in {} are optional): 57 ;; 58 ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING} 59 ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}... 60 ;; 61 ;; CLASS-OR-FORMAT-STRING is one of two things: 62 ;; 63 ;; - an identifier for a class of anniversaries (e.g. birthday or 64 ;; wedding) from `org-bbdb-anniversary-format-alist' which then 65 ;; defines the format string for this class 66 ;; - the (format) string displayed in the diary. 67 ;; 68 ;; You can enter multiple anniversaries for a single BBDB record by 69 ;; separating them with a newline character. At the BBDB prompt for 70 ;; the field value, type `C-q C-j' to enter a newline between two 71 ;; anniversaries. 72 ;; 73 ;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the 74 ;; value of `org-bbdb-default-anniversary-format' ("birthday" by 75 ;; default). 76 ;; 77 ;; The substitutions in the format string are (in order): 78 ;; - the name of the record containing this anniversary 79 ;; - the number of years 80 ;; - an ordinal suffix (st, nd, rd, th) for the year 81 ;; 82 ;; See the documentation of `org-bbdb-anniversary-format-alist' for 83 ;; further options. 84 ;; 85 ;; Example 86 ;; 87 ;; 1973-06-22 88 ;; 20??-??-?? wedding 89 ;; 1998-03-12 %s created bbdb-anniv.el %d years ago 90 ;; 91 ;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB 92 ;; link from which the entry at point originates. 93 ;; 94 ;;; Code: 95 96 (require 'org-macs) 97 (org-assert-version) 98 99 (require 'cl-lib) 100 (require 'org-compat) 101 (require 'org-macs) 102 (require 'ol) 103 104 ;;; Declare functions and variables 105 106 (declare-function bbdb "ext:bbdb-com" (string elidep)) 107 (declare-function bbdb-company "ext:bbdb-com" (string elidep)) 108 (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying)) 109 (declare-function bbdb-name "ext:bbdb-com" (string elidep)) 110 (declare-function bbdb-completing-read-record "ext:bbdb-com" (prompt &optional omit-records)) 111 (declare-function bbdb-record-field "ext:bbdb" (record field)) 112 (declare-function bbdb-record-getprop "ext:bbdb" (record property)) 113 (declare-function bbdb-record-name "ext:bbdb" (record)) 114 (declare-function bbdb-records "ext:bbdb" (&optional dont-check-disk already-in-db-buffer)) 115 (declare-function bbdb-split "ext:bbdb" (string separators)) 116 (declare-function bbdb-string-trim "ext:bbdb" (string)) 117 (declare-function bbdb-record-get-field "ext:bbdb" (record field)) 118 (declare-function bbdb-search-name "ext:bbdb-com" (regexp &optional layout)) 119 (declare-function bbdb-search-organization "ext:bbdb-com" (regexp &optional layout)) 120 121 ;; `bbdb-record-note' was part of BBDB v3.x 122 (declare-function bbdb-record-note "ext:bbdb" (record label)) 123 ;; `bbdb-record-xfield' replaces it in recent BBDB v3.x+ 124 (declare-function bbdb-record-xfield "ext:bbdb" (record label)) 125 126 (declare-function calendar-absolute-from-gregorian "calendar" (date)) 127 (declare-function calendar-gregorian-from-absolute "calendar" (date)) 128 (declare-function calendar-leap-year-p "calendar" (year)) 129 130 (declare-function diary-ordinal-suffix "diary-lib" (n)) 131 132 (with-no-warnings (defvar date)) ; unprefixed, from calendar.el 133 134 ;;; Customization 135 136 (defgroup org-bbdb-anniversaries nil 137 "Customizations for including anniversaries from BBDB into Agenda." 138 :group 'org-agenda) 139 140 (defcustom org-bbdb-default-anniversary-format "birthday" 141 "Default anniversary class." 142 :type 'string 143 :group 'org-bbdb-anniversaries 144 :require 'bbdb) 145 146 (defcustom org-bbdb-general-anniversary-description-after 7 147 "When to switch anniversary descriptions to a more general format. 148 149 Anniversary descriptions include the point in time, when the 150 anniversary appears. This is, in its most general form, just the 151 date of the anniversary. Or more specific terms, like \"today\", 152 \"tomorrow\" or \"in n days\" are used to describe the time span. 153 154 If the anniversary happens in less than that number of days, the 155 specific description is used. Otherwise, the general one is 156 used." 157 :group 'org-bbdb-anniversaries 158 :version "26.1" 159 :package-version '(Org . "9.1") 160 :type 'integer 161 :require 'bbdb 162 :safe #'integerp) 163 164 (defcustom org-bbdb-anniversary-format-alist 165 '(("birthday" . 166 (lambda (name years suffix) 167 (concat "Birthday: [[bbdb:" name "][" name " (" 168 (format "%s" years) ; handles numbers as well as strings 169 suffix ")]]"))) 170 ("wedding" . 171 (lambda (name years suffix) 172 (concat "[[bbdb:" name "][" name "'s " 173 (format "%s" years) 174 suffix " wedding anniversary]]")))) 175 "How different types of anniversaries should be formatted. 176 An alist of elements (STRING . FORMAT) where STRING is the name of an 177 anniversary class and format is either: 178 1) A format string with the following substitutions (in order): 179 - the name of the record containing this anniversary 180 - the number of years 181 - an ordinal suffix (st, nd, rd, th) for the year 182 183 2) A function to be called with three arguments: NAME YEARS SUFFIX 184 (string int string) returning a string for the diary or nil. 185 186 3) An Emacs Lisp form that should evaluate to a string (or nil) in the 187 scope of variables NAME, YEARS and SUFFIX (among others)." 188 :type '(alist :key-type (string :tag "Class") 189 :value-type (function :tag "Function")) 190 :group 'org-bbdb-anniversaries 191 :require 'bbdb) 192 193 (defcustom org-bbdb-anniversary-field 'anniversary 194 "The BBDB field which contains anniversaries. 195 The anniversaries are stored in the following format 196 197 YYYY-MM-DD Class-or-Format-String 198 199 where class is one of the customized classes for anniversaries; 200 birthday and wedding are predefined. Format-String can take three 201 substitutions 1) the name of the record containing this 202 anniversary, 2) the number of years, and 3) an ordinal suffix for 203 the year. 204 205 Multiple anniversaries can be separated by \\n." 206 :type 'symbol 207 :group 'org-bbdb-anniversaries 208 :require 'bbdb) 209 210 (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date 211 "How to retrieve `month date year' from the anniversary field. 212 213 Customize if you have already filled your BBDB with dates 214 different from YYYY-MM-DD. The function must return a list (month 215 date year)." 216 :type 'function 217 :group 'org-bbdb-anniversaries 218 :require 'bbdb) 219 220 ;; Install the link type 221 (org-link-set-parameters "bbdb" 222 :follow #'org-bbdb-open 223 :export #'org-bbdb-export 224 :complete #'org-bbdb-complete-link 225 :store #'org-bbdb-store-link) 226 227 ;;; Implementation 228 229 (defun org-bbdb-store-link (&optional _interactive?) 230 "Store a link to a BBDB database entry." 231 (when (eq major-mode 'bbdb-mode) 232 ;; This is BBDB, we make this link! 233 (let* ((rec (bbdb-current-record)) 234 (name (bbdb-record-name rec)) 235 (company (if (fboundp 'bbdb-record-getprop) 236 (bbdb-record-getprop rec 'company) 237 (car (bbdb-record-field rec 'organization)))) 238 (link (concat "bbdb:" name))) 239 (org-link-store-props :type "bbdb" :name name :company company 240 :link link :description name) 241 link))) 242 243 (defun org-bbdb-export (path desc format _) 244 "Create the export version of a BBDB link specified by PATH or DESC. 245 If exporting to either HTML or LaTeX FORMAT the link will be 246 italicized, in all other cases it is left unchanged." 247 (when (string= desc (format "bbdb:%s" path)) 248 (setq desc path)) 249 (cond 250 ((eq format 'html) (format "<i>%s</i>" desc)) 251 ((eq format 'latex) (format "\\textit{%s}" desc)) 252 ((eq format 'odt) 253 (format "<text:span text:style-name=\"Emphasis\">%s</text:span>" desc)) 254 (t desc))) 255 256 (defun org-bbdb-open (name _) 257 "Follow a BBDB link to NAME." 258 (org-require-package 'bbdb-com "bbdb") 259 (let ((inhibit-redisplay (not debug-on-error))) 260 (if (fboundp 'bbdb-name) 261 (org-bbdb-open-old name) 262 (org-bbdb-open-new name)))) 263 264 (defun org-bbdb-open-old (name) 265 (catch 'exit 266 ;; Exact match on name 267 (bbdb-name (concat "\\`" name "\\'") nil) 268 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 269 ;; Exact match on name 270 (bbdb-company (concat "\\`" name "\\'") nil) 271 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 272 ;; Partial match on name 273 (bbdb-name name nil) 274 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 275 ;; Partial match on company 276 (bbdb-company name nil) 277 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 278 ;; General match including network address and notes 279 (bbdb name nil) 280 (when (= 0 (buffer-size (get-buffer "*BBDB*"))) 281 (delete-window (get-buffer-window "*BBDB*")) 282 (error "No matching BBDB record")))) 283 284 (defun org-bbdb-open-new (name) 285 (catch 'exit 286 ;; Exact match on name 287 (bbdb-search-name (concat "\\`" name "\\'") nil) 288 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 289 ;; Exact match on name 290 (bbdb-search-organization (concat "\\`" name "\\'") nil) 291 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 292 ;; Partial match on name 293 (bbdb-search-name name nil) 294 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 295 ;; Partial match on company 296 (bbdb-search-organization name nil) 297 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil)) 298 ;; General match including network address and notes 299 (bbdb name nil) 300 (when (= 0 (buffer-size (get-buffer "*BBDB*"))) 301 (delete-window (get-buffer-window "*BBDB*")) 302 (error "No matching BBDB record")))) 303 304 (defun org-bbdb-anniv-extract-date (time-str) 305 "Convert YYYY-MM-DD to (month date year). 306 Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted 307 it will be considered unknown." 308 (pcase (org-split-string time-str "-") 309 (`(,a ,b) (list (string-to-number a) (string-to-number b) nil)) 310 (`(,a ,b ,c) (list (string-to-number b) 311 (string-to-number c) 312 (string-to-number a))))) 313 314 (defun org-bbdb-anniv-split (str) 315 "Split multiple entries in the BBDB anniversary field. 316 Argument STR is the anniversary field in BBDB." 317 (let ((pos (string-match "[ \t]" str))) 318 (if pos (list (substring str 0 pos) 319 (bbdb-string-trim (substring str pos))) 320 (list str nil)))) 321 322 (defvar org-bbdb-anniv-hash nil 323 "A hash holding anniversaries extracted from BBDB. 324 The hash table is created on first use.") 325 326 (defvar org-bbdb-updated-p t 327 "This is non-nil if BBDB has been updated since we last built the hash.") 328 329 (defun org-bbdb-make-anniv-hash () 330 "Create a hash with anniversaries extracted from BBDB, for fast access. 331 The anniversaries are assumed to be stored `org-bbdb-anniversary-field'." 332 (let ((old-bbdb (fboundp 'bbdb-record-getprop)) 333 (record-func (if (fboundp 'bbdb-record-xfield) 334 'bbdb-record-xfield 335 'bbdb-record-note)) 336 split tmp annivs) 337 (clrhash org-bbdb-anniv-hash) 338 (dolist (rec (bbdb-records)) 339 (when (setq annivs (if old-bbdb 340 (bbdb-record-getprop 341 rec org-bbdb-anniversary-field) 342 (funcall record-func 343 rec org-bbdb-anniversary-field))) 344 (setq annivs (if old-bbdb 345 (bbdb-split annivs "\n") 346 ;; parameter order is reversed in new bbdb 347 (bbdb-split "\n" annivs))) 348 (while annivs 349 (setq split (org-bbdb-anniv-split (pop annivs))) 350 (pcase-let ((`(,m ,d ,y) (funcall org-bbdb-extract-date-fun 351 (car split)))) 352 (setq tmp (gethash (list m d) org-bbdb-anniv-hash)) 353 (puthash (list m d) (cons (list y 354 (bbdb-record-name rec) 355 (cadr split)) 356 tmp) 357 org-bbdb-anniv-hash)))))) 358 (setq org-bbdb-updated-p nil)) 359 360 (defun org-bbdb-updated (_rec) 361 "Record the fact that BBDB has been updated. 362 This is used by Org to re-create the anniversary hash table." 363 (setq org-bbdb-updated-p t)) 364 365 (add-hook 'bbdb-after-change-hook 'org-bbdb-updated) 366 367 ;;;###autoload 368 (defun org-bbdb-anniversaries () 369 "Extract anniversaries from BBDB for display in the agenda. 370 When called programmatically, this function expects the `date' 371 variable to be globally bound." 372 (org-require-package 'bbdb) 373 (require 'diary-lib) 374 (unless (hash-table-p org-bbdb-anniv-hash) 375 (setq org-bbdb-anniv-hash 376 (make-hash-table :test 'equal :size 366))) 377 378 (when (or org-bbdb-updated-p 379 (= 0 (hash-table-count org-bbdb-anniv-hash))) 380 (org-bbdb-make-anniv-hash)) 381 382 (let* ((m (car date)) ; month 383 (d (nth 1 date)) ; day 384 (y (nth 2 date)) ; year 385 (annivs (gethash (list m d) org-bbdb-anniv-hash)) 386 (text ()) 387 rec recs) 388 389 ;; We don't want to miss people born on Feb. 29th 390 (when (and (= m 3) (= d 1) 391 (not (null (gethash (list 2 29) org-bbdb-anniv-hash))) 392 (not (calendar-leap-year-p y))) 393 (setq recs (gethash (list 2 29) org-bbdb-anniv-hash)) 394 (while (setq rec (pop recs)) 395 (push rec annivs))) 396 397 (when annivs 398 (while (setq rec (pop annivs)) 399 (when rec 400 (let* ((class (or (nth 2 rec) 401 org-bbdb-default-anniversary-format)) 402 (form (or (cdr (assoc-string 403 class org-bbdb-anniversary-format-alist t)) 404 class)) ; (as format string) 405 (name (nth 1 rec)) 406 (years (if (eq (car rec) nil) 407 "unknown" 408 (- y (car rec)))) 409 (suffix (if (eq (car rec) nil) 410 "" 411 (diary-ordinal-suffix years))) 412 (tmp (cond 413 ((functionp form) 414 (funcall form name years suffix)) 415 ((listp form) (eval form)) 416 (t (format form name years suffix))))) 417 (org-add-props tmp nil 'org-bbdb-name name) 418 (if text 419 (setq text (append text (list tmp))) 420 (setq text (list tmp))))) 421 )) 422 text)) 423 424 ;;; Return the list of anniversaries for today and the next n-1 425 ;;; (default: n=7) days. This is meant to be used in an org file 426 ;;; instead of org-bbdb-anniversaries: 427 ;;; 428 ;;; %%(org-bbdb-anniversaries-future) 429 ;;; 430 ;;; or 431 ;;; 432 ;;; %%(org-bbdb-anniversaries-future 3) 433 ;;; 434 ;;; to override the 7-day default. 435 436 (defun org-bbdb-date-list (d n) 437 "Return list of dates in (m d y) format from the given date D to n-1 days hence." 438 (let ((abs (calendar-absolute-from-gregorian d))) 439 (mapcar (lambda (i) (calendar-gregorian-from-absolute (+ abs i))) 440 (number-sequence 0 (1- n))))) 441 442 (defun org-bbdb-anniversary-description (agenda-date anniv-date) 443 "Return a string used to incorporate into an agenda anniversary entry. 444 The calculation of the anniversary description string is based on 445 the difference between the anniversary date, given as ANNIV-DATE, 446 and the date on which the entry appears in the agenda, given as 447 AGENDA-DATE. This makes it possible to have different entries 448 for the same event depending on if it occurs in the next few days 449 or far away in the future." 450 (let ((delta (- (calendar-absolute-from-gregorian anniv-date) 451 (calendar-absolute-from-gregorian agenda-date)))) 452 (cond 453 ((= delta 0) " -- today\\&") 454 ((= delta 1) " -- tomorrow\\&") 455 ((< delta org-bbdb-general-anniversary-description-after) 456 (format " -- in %d days\\&" delta)) 457 ((pcase-let ((`(,month ,day ,year) anniv-date)) 458 (format " -- %d-%02d-%02d\\&" year month day)))))) 459 460 (defun org-bbdb-anniversaries-future (&optional n) 461 "Return list of anniversaries for today and the next n-1 days (default n=7)." 462 (let ((n (or n 7))) 463 (when (<= n 0) 464 (error "The (optional) argument of `org-bbdb-anniversaries-future' \ 465 must be positive")) 466 (let ( 467 ;; List of relevant dates. 468 (dates (org-bbdb-date-list date n)) 469 ;; Function to annotate text of each element of l with the 470 ;; anniversary date d. 471 (annotate-descriptions 472 (lambda (agenda-date d l) 473 (mapcar (lambda (x) 474 ;; The assumption here is that x is a bbdb link 475 ;; of the form [[bbdb:name][description]]. 476 ;; This function rather arbitrarily modifies 477 ;; the description by adding the date to it in 478 ;; a fixed format. 479 (let ((desc (org-bbdb-anniversary-description 480 agenda-date d))) 481 (string-match "]]" x) 482 (replace-match desc nil nil x))) 483 l)))) 484 ;; Map a function that generates anniversaries for each date 485 ;; over the dates and nconc the results into a single list. When 486 ;; it is no longer necessary to support older versions of Emacs, 487 ;; this can be done with a cl-mapcan; for now, we use the (apply 488 ;; #'nconc ...) method for compatibility. 489 (apply #'nconc 490 (mapcar 491 (lambda (d) 492 (let ((agenda-date date) 493 (date d)) 494 ;; Rebind 'date' so that org-bbdb-anniversaries will 495 ;; be fooled into giving us the list for the given 496 ;; date and then annotate the descriptions for that 497 ;; date. 498 (funcall annotate-descriptions agenda-date d (org-bbdb-anniversaries)))) 499 dates))))) 500 501 (defun org-bbdb-complete-link () 502 "Read a bbdb link with name completion." 503 (org-require-package 'bbdb-com "bbdb") 504 (let ((rec (bbdb-completing-read-record "Name: "))) 505 (concat "bbdb:" 506 (bbdb-record-name (if (listp rec) 507 (car rec) 508 rec))))) 509 510 (defun org-bbdb-anniv-export-ical () 511 "Extract anniversaries from BBDB and convert them to icalendar format." 512 (org-require-package 'bbdb) 513 (require 'diary-lib) 514 (unless (hash-table-p org-bbdb-anniv-hash) 515 (setq org-bbdb-anniv-hash 516 (make-hash-table :test 'equal :size 366))) 517 (when (or org-bbdb-updated-p 518 (= 0 (hash-table-count org-bbdb-anniv-hash))) 519 (org-bbdb-make-anniv-hash)) 520 (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash)) 521 522 (defun org-bbdb-format-vevent (key recs) 523 (let (rec categ) 524 (while (setq rec (pop recs)) 525 (setq categ (or (nth 2 rec) org-bbdb-default-anniversary-format)) 526 (princ (format "BEGIN:VEVENT 527 UID: ANNIV-%4i%02i%02i-%s 528 DTSTART:%4i%02i%02i 529 SUMMARY:%s 530 DESCRIPTION:%s 531 CATEGORIES:%s 532 RRULE:FREQ=YEARLY 533 END:VEVENT\n" 534 (nth 0 rec) (nth 0 key) (nth 1 key) 535 (mapconcat 'identity 536 (org-split-string (nth 1 rec) "[^a-zA-Z0-90]+") 537 "-") 538 (nth 0 rec) (nth 0 key) (nth 1 key) 539 (nth 1 rec) 540 (concat (capitalize categ) " " (nth 1 rec)) 541 categ))))) 542 543 (provide 'ol-bbdb) 544 545 ;; Local variables: 546 ;; generated-autoload-file: "org-loaddefs.el" 547 ;; End: 548 549 ;;; ol-bbdb.el ends here