org-bbdb.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>,
  4. ;; Thomas Baumann <thomas dot baumann at ch dot tum dot de>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file implements links to BBDB database entries from within Org-mode.
  24. ;; Org-mode loads this module by default - if this is not what you want,
  25. ;; configure the variable `org-modules'.
  26. ;; It also implements an interface (based on Ivar Rummelhoff's
  27. ;; bbdb-anniv.el) for those org-mode users, who do not use the diary
  28. ;; but who do want to include the anniversaries stored in the BBDB
  29. ;; into the org-agenda. If you already include the `diary' into the
  30. ;; agenda, you might want to prefer to include the anniversaries in
  31. ;; the diary using bbdb-anniv.el.
  32. ;;
  33. ;; Put the following in /somewhere/at/home/diary.org and make sure
  34. ;; that this file is in `org-agenda-files`
  35. ;;
  36. ;; %%(org-bbdb-anniversaries)
  37. ;;
  38. ;; For example my diary.org looks like:
  39. ;; * Anniversaries
  40. ;; #+CATEGORY: Anniv
  41. ;; %%(org-bbdb-anniversaries)
  42. ;;
  43. ;;
  44. ;; To add an anniversary to a BBDB record, press `C-o' in the record.
  45. ;; You will be prompted for the field name, in this case it must be
  46. ;; "anniversary". If this is the first time you are using this field,
  47. ;; you need to confirm that it should be created.
  48. ;;
  49. ;; The format of an anniversary field stored in BBDB is the following
  50. ;; (items in {} are optional):
  51. ;;
  52. ;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}
  53. ;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}...
  54. ;;
  55. ;; CLASS-OR-FORMAT-STRING is one of two things:
  56. ;;
  57. ;; - an identifier for a class of anniversaries (eg. birthday or
  58. ;; wedding) from `org-bbdb-anniversary-format-alist' which then
  59. ;; defines the format string for this class
  60. ;; - the (format) string displayed in the diary.
  61. ;;
  62. ;; You can enter multiple anniversaries for a single BBDB record by
  63. ;; separating them with a newline character. At the BBDB prompt for
  64. ;; the field value, type `C-q C-j' to enter a newline between two
  65. ;; anniversaries.
  66. ;;
  67. ;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the
  68. ;; value of `org-bbdb-default-anniversary-format' ("birthday" by
  69. ;; default).
  70. ;;
  71. ;; The substitutions in the format string are (in order):
  72. ;; - the name of the record containing this anniversary
  73. ;; - the number of years
  74. ;; - an ordinal suffix (st, nd, rd, th) for the year
  75. ;;
  76. ;; See the documentation of `org-bbdb-anniversary-format-alist' for
  77. ;; further options.
  78. ;;
  79. ;; Example
  80. ;;
  81. ;; 1973-06-22
  82. ;; 20??-??-?? wedding
  83. ;; 1998-03-12 %s created bbdb-anniv.el %d years ago
  84. ;;
  85. ;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB
  86. ;; link from which the entry at point originates.
  87. ;;
  88. ;;; Code:
  89. (require 'org)
  90. (eval-when-compile
  91. (require 'cl))
  92. ;; Declare external functions and variables
  93. (declare-function bbdb "ext:bbdb-com" (string elidep))
  94. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  95. (declare-function bbdb-current-record "ext:bbdb-com"
  96. (&optional planning-on-modifying))
  97. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  98. (declare-function bbdb-completing-read-record "ext:bbdb-com"
  99. (prompt &optional omit-records))
  100. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  101. (declare-function bbdb-record-name "ext:bbdb" (record))
  102. (declare-function bbdb-records "ext:bbdb"
  103. (&optional dont-check-disk already-in-db-buffer))
  104. (declare-function bbdb-split "ext:bbdb" (string separators))
  105. (declare-function bbdb-string-trim "ext:bbdb" (string))
  106. (declare-function bbdb-record-get-field "ext:bbdb"
  107. (fn file &optional arglist fileonly))
  108. ;; These two functions below are part of BBDB3:
  109. (declare-function bbdb-search-name "ext:bbdb" (regexp &optional layout))
  110. (declare-function bbdb-search-organization "ext:bbdb" (regexp &optional layout))
  111. (declare-function calendar-leap-year-p "calendar" (year))
  112. (declare-function diary-ordinal-suffix "diary-lib" (n))
  113. (defvar date) ;; dynamically scoped from Org
  114. (defvar name) ;; dynamically scoped from Org
  115. ;; Support for version 2.35
  116. (defvar org-bbdb-old (fboundp 'bbdb-record-get-field-internal))
  117. ;; Customization
  118. (defgroup org-bbdb-anniversaries nil
  119. "Customizations for including anniversaries from BBDB into Agenda."
  120. :group 'org-bbdb)
  121. (defcustom org-bbdb-default-anniversary-format "birthday"
  122. "Default anniversary class."
  123. :type 'string
  124. :group 'org-bbdb-anniversaries
  125. :require 'bbdb)
  126. (defcustom org-bbdb-anniversary-format-alist
  127. '(("birthday" lambda
  128. (name years suffix)
  129. (concat "Birthday: [[bbdb:" name "][" name " ("
  130. (format "%s" years) ; handles numbers as well as strings
  131. suffix ")]]"))
  132. ("wedding" lambda
  133. (name years suffix)
  134. (concat "[[bbdb:" name "][" name "'s "
  135. (format "%s" years)
  136. suffix " wedding anniversary]]")))
  137. "How different types of anniversaries should be formatted.
  138. An alist of elements (STRING . FORMAT) where STRING is the name of an
  139. anniversary class and format is either:
  140. 1) A format string with the following substitutions (in order):
  141. * the name of the record containing this anniversary
  142. * the number of years
  143. * an ordinal suffix (st, nd, rd, th) for the year
  144. 2) A function to be called with three arguments: NAME YEARS SUFFIX
  145. (string int string) returning a string for the diary or nil.
  146. 3) An Emacs Lisp form that should evaluate to a string (or nil) in the
  147. scope of variables NAME, YEARS and SUFFIX (among others)."
  148. :type 'sexp
  149. :group 'org-bbdb-anniversaries
  150. :require 'bbdb)
  151. (defcustom org-bbdb-anniversary-field 'anniversary
  152. "The BBDB field which contains anniversaries.
  153. The anniversaries are stored in the following format
  154. YYYY-MM-DD Class-or-Format-String
  155. where class is one of the customized classes for anniversaries;
  156. birthday and wedding are predefined. Format-String can take three
  157. substitutions 1) the name of the record containing this
  158. anniversary, 2) the number of years, and 3) an ordinal suffix for
  159. the year.
  160. Multiple anniversaries can be separated by \\n."
  161. :type 'symbol
  162. :group 'org-bbdb-anniversaries
  163. :require 'bbdb)
  164. (defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
  165. "How to retrieve `month date year' from the anniversary field.
  166. Customize if you have already filled your BBDB with dates
  167. different from YYYY-MM-DD. The function must return a list (month
  168. date year)."
  169. :type 'function
  170. :group 'org-bbdb-anniversaries
  171. :require 'bbdb)
  172. ;; Install the link type
  173. (org-add-link-type "bbdb" 'org-bbdb-open 'org-bbdb-export)
  174. (add-hook 'org-store-link-functions 'org-bbdb-store-link)
  175. ;; Implementation
  176. (defun org-bbdb-store-link ()
  177. "Store a link to a BBDB database entry."
  178. (when (eq major-mode 'bbdb-mode)
  179. ;; This is BBDB, we make this link!
  180. (let* ((rec (bbdb-current-record))
  181. (name (bbdb-record-name rec))
  182. (company (if org-bbdb-old
  183. (bbdb-record-getprop rec 'company)
  184. (car (bbdb-record-get-field rec 'organization))))
  185. (link (org-make-link "bbdb:" name)))
  186. (org-store-link-props :type "bbdb" :name name :company company
  187. :link link :description name)
  188. link)))
  189. (defun org-bbdb-export (path desc format)
  190. "Create the export version of a BBDB link specified by PATH or DESC.
  191. If exporting to either HTML or LaTeX FORMAT the link will be
  192. italicized, in all other cases it is left unchanged."
  193. (when (string= desc (format "bbdb:%s" path))
  194. (setq desc path))
  195. (cond
  196. ((eq format 'html) (format "<i>%s</i>" desc))
  197. ((eq format 'latex) (format "\\textit{%s}" desc))
  198. (t desc)))
  199. (defun org-bbdb-open (name)
  200. "Follow a BBDB link to NAME."
  201. (require 'bbdb)
  202. (let ((inhibit-redisplay (not debug-on-error))
  203. (bbdb-electric-p nil))
  204. (if org-bbdb-old
  205. (org-bbdb-open-old)
  206. (org-bbdb-open-new))))
  207. (defun org-bbdb-open-old ()
  208. (catch 'exit
  209. ;; Exact match on name
  210. (bbdb-name (concat "\\`" name "\\'") nil)
  211. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  212. ;; Exact match on name
  213. (bbdb-company (concat "\\`" name "\\'") nil)
  214. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  215. ;; Partial match on name
  216. (bbdb-name name nil)
  217. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  218. ;; Partial match on company
  219. (bbdb-company name nil)
  220. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  221. ;; General match including network address and notes
  222. (bbdb name nil)
  223. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  224. (delete-window (get-buffer-window "*BBDB*"))
  225. (error "No matching BBDB record"))))
  226. (defun org-bbdb-open-new ()
  227. (catch 'exit
  228. ;; Exact match on name
  229. (bbdb-search-name (concat "\\`" name "\\'") nil)
  230. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  231. ;; Exact match on name
  232. (bbdb-search-organization (concat "\\`" name "\\'") nil)
  233. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  234. ;; Partial match on name
  235. (bbdb-search-name name nil)
  236. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  237. ;; Partial match on company
  238. (bbdb-search-organization name nil)
  239. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  240. ;; General match including network address and notes
  241. (bbdb name nil)
  242. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  243. (delete-window (get-buffer-window "*BBDB*"))
  244. (error "No matching BBDB record"))))
  245. (defun org-bbdb-anniv-extract-date (time-str)
  246. "Convert YYYY-MM-DD to (month date year).
  247. Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted
  248. it will be considered unknown."
  249. (multiple-value-bind (a b c) (values-list (bbdb-split time-str "-"))
  250. (if (eq c nil)
  251. (list (string-to-number a)
  252. (string-to-number b)
  253. nil)
  254. (list (string-to-number b)
  255. (string-to-number c)
  256. (string-to-number a)))))
  257. (defun org-bbdb-anniv-split (str)
  258. "Split multiple entries in the BBDB anniversary field.
  259. Argument STR is the anniversary field in BBDB."
  260. (let ((pos (string-match "[ \t]" str)))
  261. (if pos (list (substring str 0 pos)
  262. (bbdb-string-trim (substring str pos)))
  263. (list str nil))))
  264. (defvar org-bbdb-anniv-hash nil
  265. "A hash holding anniversaries extracted from BBDB.
  266. The hash table is created on first use.")
  267. (defvar org-bbdb-updated-p t
  268. "This is non-nil if BBDB has been updated since we last built the hash.")
  269. (defun org-bbdb-make-anniv-hash ()
  270. "Create a hash with anniversaries extracted from BBDB, for fast access.
  271. The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
  272. (let (split tmp annivs)
  273. (clrhash org-bbdb-anniv-hash)
  274. (dolist (rec (bbdb-records))
  275. (when (setq annivs (bbdb-record-getprop
  276. rec org-bbdb-anniversary-field))
  277. (setq annivs (bbdb-split annivs "\n"))
  278. (while annivs
  279. (setq split (org-bbdb-anniv-split (pop annivs)))
  280. (multiple-value-bind (m d y)
  281. (values-list (funcall org-bbdb-extract-date-fun (car split)))
  282. (setq tmp (gethash (list m d) org-bbdb-anniv-hash))
  283. (puthash (list m d) (cons (list y
  284. (bbdb-record-name rec)
  285. (cadr split))
  286. tmp)
  287. org-bbdb-anniv-hash))))))
  288. (setq org-bbdb-updated-p nil))
  289. (defun org-bbdb-updated (rec)
  290. "Record the fact that BBDB has been updated.
  291. This is used by Org to re-create the anniversary hash table."
  292. (setq org-bbdb-updated-p t))
  293. (add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
  294. ;;;###autoload
  295. (defun org-bbdb-anniversaries()
  296. "Extract anniversaries from BBDB for display in the agenda."
  297. (require 'bbdb)
  298. (require 'diary-lib)
  299. (unless (hash-table-p org-bbdb-anniv-hash)
  300. (setq org-bbdb-anniv-hash
  301. (make-hash-table :test 'equal :size 366)))
  302. (when (or org-bbdb-updated-p
  303. (= 0 (hash-table-count org-bbdb-anniv-hash)))
  304. (org-bbdb-make-anniv-hash))
  305. (let* ((m (car date)) ; month
  306. (d (nth 1 date)) ; day
  307. (y (nth 2 date)) ; year
  308. (annivs (gethash (list m d) org-bbdb-anniv-hash))
  309. (text ())
  310. rec recs)
  311. ;; we don't want to miss people born on Feb. 29th
  312. (when (and (= m 3) (= d 1)
  313. (not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
  314. (not (calendar-leap-year-p y)))
  315. (setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
  316. (while (setq rec (pop recs))
  317. (push rec annivs)))
  318. (when annivs
  319. (while (setq rec (pop annivs))
  320. (when rec
  321. (let* ((class (or (nth 2 rec)
  322. org-bbdb-default-anniversary-format))
  323. (form (or (cdr (assoc-string
  324. class org-bbdb-anniversary-format-alist t))
  325. class)) ; (as format string)
  326. (name (nth 1 rec))
  327. (years (if (eq (car rec) nil)
  328. "unknown"
  329. (- y (car rec))))
  330. (suffix (if (eq (car rec) nil)
  331. ""
  332. (diary-ordinal-suffix years)))
  333. (tmp (cond
  334. ((functionp form)
  335. (funcall form name years suffix))
  336. ((listp form) (eval form))
  337. (t (format form name years suffix)))))
  338. (org-add-props tmp nil 'org-bbdb-name name)
  339. (if text
  340. (setq text (append text (list tmp)))
  341. (setq text (list tmp)))))
  342. ))
  343. text))
  344. (defun org-bbdb-complete-link ()
  345. "Read a bbdb link with name completion."
  346. (require 'bbdb-com)
  347. (concat "bbdb:"
  348. (bbdb-record-name (car (bbdb-completing-read-record "Name: ")))))
  349. (defun org-bbdb-anniv-export-ical ()
  350. "Extract anniversaries from BBDB and convert them to icalendar format."
  351. (require 'bbdb)
  352. (require 'diary-lib)
  353. (unless (hash-table-p org-bbdb-anniv-hash)
  354. (setq org-bbdb-anniv-hash
  355. (make-hash-table :test 'equal :size 366)))
  356. (when (or org-bbdb-updated-p
  357. (= 0 (hash-table-count org-bbdb-anniv-hash)))
  358. (org-bbdb-make-anniv-hash))
  359. (maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash))
  360. (defun org-bbdb-format-vevent (key recs)
  361. (let (rec categ)
  362. (while (setq rec (pop recs))
  363. (setq categ (or (nth 2 rec) org-bbdb-default-anniversary-format))
  364. (princ (format "BEGIN:VEVENT
  365. UID: ANNIV-%4i%02i%02i-%s
  366. DTSTART:%4i%02i%02i
  367. SUMMARY:%s
  368. DESCRIPTION:%s
  369. CATEGORIES:%s
  370. RRULE:FREQ=YEARLY
  371. END:VEVENT\n"
  372. (nth 0 rec) (nth 0 key) (nth 1 key)
  373. (mapconcat 'identity
  374. (org-split-string (nth 1 rec) "[^a-zA-Z0-90]+")
  375. "-")
  376. (nth 0 rec) (nth 0 key) (nth 1 key)
  377. (nth 1 rec)
  378. (concat (capitalize categ) " " (nth 1 rec))
  379. categ)))))
  380. (provide 'org-bbdb)
  381. ;;; org-bbdb.el ends here