2015-12-20 21:37:36 +00:00
|
|
|
;;; org-bbdb.el --- Support for links to BBDB entries -*- lexical-binding: t; -*-
|
2008-03-14 17:22:03 +00:00
|
|
|
|
2016-01-09 22:12:03 +00:00
|
|
|
;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
|
2008-03-14 17:22:03 +00:00
|
|
|
|
2012-04-01 22:53:28 +00:00
|
|
|
;; Authors: Carsten Dominik <carsten at orgmode dot org>
|
|
|
|
;; Thomas Baumann <thomas dot baumann at ch dot tum dot de>
|
2008-03-14 17:22:03 +00:00
|
|
|
;; Keywords: outlines, hypermedia, calendar, wp
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;;
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;;
|
2008-05-06 12:45:52 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2008-03-14 17:22:03 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 12:45:52 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2008-03-14 17:22:03 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2008-05-06 12:45:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2008-03-14 17:22:03 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;
|
|
|
|
;;; Commentary:
|
|
|
|
|
2008-03-21 16:10:23 +00:00
|
|
|
;; This file implements links to BBDB database entries from within Org-mode.
|
2016-08-23 20:13:56 +00:00
|
|
|
;; Org mode loads this module by default - if this is not what you want,
|
2008-03-14 17:22:03 +00:00
|
|
|
;; configure the variable `org-modules'.
|
|
|
|
|
2008-04-19 05:21:33 +00:00
|
|
|
;; It also implements an interface (based on Ivar Rummelhoff's
|
2016-08-23 20:13:56 +00:00
|
|
|
;; bbdb-anniv.el) for those Org users, who do not use the diary
|
2008-04-19 05:21:33 +00:00
|
|
|
;; but who do want to include the anniversaries stored in the BBDB
|
|
|
|
;; into the org-agenda. If you already include the `diary' into the
|
|
|
|
;; agenda, you might want to prefer to include the anniversaries in
|
|
|
|
;; the diary using bbdb-anniv.el.
|
2008-04-24 08:29:47 +00:00
|
|
|
;;
|
2008-04-13 19:04:21 +00:00
|
|
|
;; Put the following in /somewhere/at/home/diary.org and make sure
|
2015-05-19 21:59:15 +00:00
|
|
|
;; that this file is in `org-agenda-files'.
|
2008-04-13 19:04:21 +00:00
|
|
|
;;
|
|
|
|
;; %%(org-bbdb-anniversaries)
|
|
|
|
;;
|
|
|
|
;; For example my diary.org looks like:
|
|
|
|
;; * Anniversaries
|
|
|
|
;; #+CATEGORY: Anniv
|
|
|
|
;; %%(org-bbdb-anniversaries)
|
|
|
|
;;
|
|
|
|
;;
|
2009-02-27 07:27:44 +00:00
|
|
|
;; To add an anniversary to a BBDB record, press `C-o' in the record.
|
|
|
|
;; You will be prompted for the field name, in this case it must be
|
|
|
|
;; "anniversary". If this is the first time you are using this field,
|
|
|
|
;; you need to confirm that it should be created.
|
2008-04-13 19:04:21 +00:00
|
|
|
;;
|
2009-02-27 07:27:44 +00:00
|
|
|
;; The format of an anniversary field stored in BBDB is the following
|
|
|
|
;; (items in {} are optional):
|
|
|
|
;;
|
|
|
|
;; YYYY-MM-DD{ CLASS-OR-FORMAT-STRING}
|
|
|
|
;; {\nYYYY-MM-DD CLASS-OR-FORMAT-STRING}...
|
2008-04-13 19:04:21 +00:00
|
|
|
;;
|
|
|
|
;; CLASS-OR-FORMAT-STRING is one of two things:
|
|
|
|
;;
|
2009-02-27 07:27:44 +00:00
|
|
|
;; - an identifier for a class of anniversaries (eg. birthday or
|
|
|
|
;; wedding) from `org-bbdb-anniversary-format-alist' which then
|
2009-11-12 12:39:29 +00:00
|
|
|
;; defines the format string for this class
|
2009-02-27 07:27:44 +00:00
|
|
|
;; - the (format) string displayed in the diary.
|
|
|
|
;;
|
|
|
|
;; You can enter multiple anniversaries for a single BBDB record by
|
|
|
|
;; separating them with a newline character. At the BBDB prompt for
|
|
|
|
;; the field value, type `C-q C-j' to enter a newline between two
|
|
|
|
;; anniversaries.
|
2008-04-13 19:04:21 +00:00
|
|
|
;;
|
2009-02-27 07:27:44 +00:00
|
|
|
;; If you omit the CLASS-OR-FORMAT-STRING entirely, it defaults to the
|
|
|
|
;; value of `org-bbdb-default-anniversary-format' ("birthday" by
|
|
|
|
;; default).
|
2008-04-13 19:04:21 +00:00
|
|
|
;;
|
|
|
|
;; The substitutions in the format string are (in order):
|
2009-02-27 07:27:44 +00:00
|
|
|
;; - the name of the record containing this anniversary
|
|
|
|
;; - the number of years
|
|
|
|
;; - an ordinal suffix (st, nd, rd, th) for the year
|
2008-04-13 19:04:21 +00:00
|
|
|
;;
|
|
|
|
;; See the documentation of `org-bbdb-anniversary-format-alist' for
|
|
|
|
;; further options.
|
|
|
|
;;
|
|
|
|
;; Example
|
|
|
|
;;
|
|
|
|
;; 1973-06-22
|
|
|
|
;; 20??-??-?? wedding
|
|
|
|
;; 1998-03-12 %s created bbdb-anniv.el %d years ago
|
2008-06-12 13:09:03 +00:00
|
|
|
;;
|
|
|
|
;; From Org's agenda, you can use `C-c C-o' to jump to the BBDB
|
|
|
|
;; link from which the entry at point originates.
|
|
|
|
;;
|
2008-03-21 16:10:23 +00:00
|
|
|
;;; Code:
|
|
|
|
|
2008-03-14 17:22:03 +00:00
|
|
|
(require 'org)
|
2015-12-20 21:37:36 +00:00
|
|
|
(require 'cl-lib)
|
2008-03-14 17:22:03 +00:00
|
|
|
|
|
|
|
;; Declare external functions and variables
|
2008-03-21 16:10:23 +00:00
|
|
|
|
2008-03-14 17:22:03 +00:00
|
|
|
(declare-function bbdb "ext:bbdb-com" (string elidep))
|
|
|
|
(declare-function bbdb-company "ext:bbdb-com" (string elidep))
|
2008-03-21 16:10:23 +00:00
|
|
|
(declare-function bbdb-current-record "ext:bbdb-com"
|
2008-03-14 17:22:03 +00:00
|
|
|
(&optional planning-on-modifying))
|
|
|
|
(declare-function bbdb-name "ext:bbdb-com" (string elidep))
|
2009-05-10 07:15:13 +00:00
|
|
|
(declare-function bbdb-completing-read-record "ext:bbdb-com"
|
|
|
|
(prompt &optional omit-records))
|
2015-12-20 21:37:36 +00:00
|
|
|
(declare-function bbdb-record-field "ext:bbdb" (recond field))
|
2008-03-14 17:22:03 +00:00
|
|
|
(declare-function bbdb-record-getprop "ext:bbdb" (record property))
|
|
|
|
(declare-function bbdb-record-name "ext:bbdb" (record))
|
2008-04-13 19:04:21 +00:00
|
|
|
(declare-function bbdb-records "ext:bbdb"
|
2012-08-11 17:10:44 +00:00
|
|
|
(&optional dont-check-disk already-in-db-buffer))
|
2008-04-13 19:04:21 +00:00
|
|
|
(declare-function bbdb-split "ext:bbdb" (string separators))
|
|
|
|
(declare-function bbdb-string-trim "ext:bbdb" (string))
|
2012-03-04 19:35:04 +00:00
|
|
|
(declare-function bbdb-record-get-field "ext:bbdb" (record field))
|
|
|
|
(declare-function bbdb-search-name "ext:bbdb-com" (regexp &optional layout))
|
|
|
|
(declare-function bbdb-search-organization "ext:bbdb-com" (regexp &optional layout))
|
2009-05-10 07:15:13 +00:00
|
|
|
|
2013-04-03 14:45:57 +00:00
|
|
|
;; `bbdb-record-note' was part of BBDB v3.x
|
2012-04-17 09:17:17 +00:00
|
|
|
(declare-function bbdb-record-note "ext:bbdb" (record label))
|
2013-04-03 11:54:17 +00:00
|
|
|
;; `bbdb-record-xfield' replaces it in recent BBDB v3.x+
|
|
|
|
(declare-function bbdb-record-xfield "ext:bbdb" (record label))
|
2012-04-17 09:17:17 +00:00
|
|
|
|
2008-04-13 19:04:21 +00:00
|
|
|
(declare-function calendar-leap-year-p "calendar" (year))
|
|
|
|
(declare-function diary-ordinal-suffix "diary-lib" (n))
|
|
|
|
|
2016-06-23 13:20:32 +00:00
|
|
|
(with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
|
2008-04-13 19:04:21 +00:00
|
|
|
|
2008-04-19 05:21:33 +00:00
|
|
|
;; Customization
|
|
|
|
|
2008-04-13 19:04:21 +00:00
|
|
|
(defgroup org-bbdb-anniversaries nil
|
|
|
|
"Customizations for including anniversaries from BBDB into Agenda."
|
|
|
|
:group 'org-bbdb)
|
|
|
|
|
|
|
|
(defcustom org-bbdb-default-anniversary-format "birthday"
|
|
|
|
"Default anniversary class."
|
|
|
|
:type 'string
|
|
|
|
:group 'org-bbdb-anniversaries
|
|
|
|
:require 'bbdb)
|
|
|
|
|
|
|
|
(defcustom org-bbdb-anniversary-format-alist
|
2012-04-29 15:00:16 +00:00
|
|
|
'(("birthday" .
|
|
|
|
(lambda (name years suffix)
|
|
|
|
(concat "Birthday: [[bbdb:" name "][" name " ("
|
|
|
|
(format "%s" years) ; handles numbers as well as strings
|
|
|
|
suffix ")]]")))
|
|
|
|
("wedding" .
|
|
|
|
(lambda (name years suffix)
|
|
|
|
(concat "[[bbdb:" name "][" name "'s "
|
|
|
|
(format "%s" years)
|
|
|
|
suffix " wedding anniversary]]"))))
|
2008-04-13 19:04:21 +00:00
|
|
|
"How different types of anniversaries should be formatted.
|
|
|
|
An alist of elements (STRING . FORMAT) where STRING is the name of an
|
|
|
|
anniversary class and format is either:
|
|
|
|
1) A format string with the following substitutions (in order):
|
2012-04-29 15:00:16 +00:00
|
|
|
- the name of the record containing this anniversary
|
|
|
|
- the number of years
|
|
|
|
- an ordinal suffix (st, nd, rd, th) for the year
|
2008-04-13 19:04:21 +00:00
|
|
|
|
|
|
|
2) A function to be called with three arguments: NAME YEARS SUFFIX
|
|
|
|
(string int string) returning a string for the diary or nil.
|
|
|
|
|
|
|
|
3) An Emacs Lisp form that should evaluate to a string (or nil) in the
|
|
|
|
scope of variables NAME, YEARS and SUFFIX (among others)."
|
2012-04-29 15:00:16 +00:00
|
|
|
:type '(alist :key-type (string :tag "Class")
|
|
|
|
:value-type (function :tag "Function"))
|
2008-04-13 19:04:21 +00:00
|
|
|
:group 'org-bbdb-anniversaries
|
|
|
|
:require 'bbdb)
|
|
|
|
|
|
|
|
(defcustom org-bbdb-anniversary-field 'anniversary
|
|
|
|
"The BBDB field which contains anniversaries.
|
|
|
|
The anniversaries are stored in the following format
|
|
|
|
|
|
|
|
YYYY-MM-DD Class-or-Format-String
|
|
|
|
|
|
|
|
where class is one of the customized classes for anniversaries;
|
|
|
|
birthday and wedding are predefined. Format-String can take three
|
|
|
|
substitutions 1) the name of the record containing this
|
|
|
|
anniversary, 2) the number of years, and 3) an ordinal suffix for
|
|
|
|
the year.
|
|
|
|
|
2008-05-09 04:08:45 +00:00
|
|
|
Multiple anniversaries can be separated by \\n."
|
2008-04-13 19:04:21 +00:00
|
|
|
:type 'symbol
|
|
|
|
:group 'org-bbdb-anniversaries
|
|
|
|
:require 'bbdb)
|
|
|
|
|
|
|
|
(defcustom org-bbdb-extract-date-fun 'org-bbdb-anniv-extract-date
|
|
|
|
"How to retrieve `month date year' from the anniversary field.
|
|
|
|
|
2008-05-09 04:08:45 +00:00
|
|
|
Customize if you have already filled your BBDB with dates
|
2008-04-13 19:04:21 +00:00
|
|
|
different from YYYY-MM-DD. The function must return a list (month
|
2008-05-09 04:08:45 +00:00
|
|
|
date year)."
|
2008-04-13 19:04:21 +00:00
|
|
|
:type 'function
|
|
|
|
:group 'org-bbdb-anniversaries
|
|
|
|
:require 'bbdb)
|
|
|
|
|
2008-03-14 17:22:03 +00:00
|
|
|
;; Install the link type
|
2016-07-05 14:25:39 +00:00
|
|
|
(org-link-set-parameters "bbdb"
|
|
|
|
:follow #'org-bbdb-open
|
|
|
|
:export #'org-bbdb-export
|
|
|
|
:complete #'org-bbdb-complete-link
|
|
|
|
:store #'org-bbdb-store-link)
|
2008-03-14 17:22:03 +00:00
|
|
|
|
|
|
|
;; Implementation
|
|
|
|
(defun org-bbdb-store-link ()
|
2008-03-21 16:10:23 +00:00
|
|
|
"Store a link to a BBDB database entry."
|
2008-03-14 17:22:03 +00:00
|
|
|
(when (eq major-mode 'bbdb-mode)
|
|
|
|
;; This is BBDB, we make this link!
|
2011-12-31 16:04:27 +00:00
|
|
|
(let* ((rec (bbdb-current-record))
|
|
|
|
(name (bbdb-record-name rec))
|
2012-03-02 16:28:17 +00:00
|
|
|
(company (if (fboundp 'bbdb-record-getprop)
|
2011-12-31 16:04:27 +00:00
|
|
|
(bbdb-record-getprop rec 'company)
|
2015-12-20 21:37:36 +00:00
|
|
|
(car (bbdb-record-field rec 'organization))))
|
2012-08-03 17:03:21 +00:00
|
|
|
(link (concat "bbdb:" name)))
|
2008-03-14 17:22:03 +00:00
|
|
|
(org-store-link-props :type "bbdb" :name name :company company
|
2008-03-15 20:34:06 +00:00
|
|
|
:link link :description name)
|
|
|
|
link)))
|
2008-03-14 17:22:03 +00:00
|
|
|
|
|
|
|
(defun org-bbdb-export (path desc format)
|
2008-03-21 16:10:23 +00:00
|
|
|
"Create the export version of a BBDB link specified by PATH or DESC.
|
|
|
|
If exporting to either HTML or LaTeX FORMAT the link will be
|
2010-07-15 20:26:51 +00:00
|
|
|
italicized, in all other cases it is left unchanged."
|
2011-02-15 04:37:17 +00:00
|
|
|
(when (string= desc (format "bbdb:%s" path))
|
|
|
|
(setq desc path))
|
2008-03-14 17:22:03 +00:00
|
|
|
(cond
|
2011-02-15 04:37:17 +00:00
|
|
|
((eq format 'html) (format "<i>%s</i>" desc))
|
|
|
|
((eq format 'latex) (format "\\textit{%s}" desc))
|
2012-06-03 12:22:56 +00:00
|
|
|
((eq format 'odt)
|
|
|
|
(format "<text:span text:style-name=\"Emphasis\">%s</text:span>" desc))
|
2011-02-15 04:37:17 +00:00
|
|
|
(t desc)))
|
2008-03-14 17:22:03 +00:00
|
|
|
|
|
|
|
(defun org-bbdb-open (name)
|
|
|
|
"Follow a BBDB link to NAME."
|
2012-03-02 16:28:17 +00:00
|
|
|
(require 'bbdb-com)
|
2015-12-20 21:37:36 +00:00
|
|
|
(let ((inhibit-redisplay (not debug-on-error)))
|
2012-03-02 16:28:17 +00:00
|
|
|
(if (fboundp 'bbdb-name)
|
2015-12-20 21:37:36 +00:00
|
|
|
(org-bbdb-open-old name)
|
2012-03-04 19:37:46 +00:00
|
|
|
(org-bbdb-open-new name))))
|
2011-12-31 16:04:27 +00:00
|
|
|
|
2012-03-04 19:37:46 +00:00
|
|
|
(defun org-bbdb-open-old (name)
|
2011-12-31 16:04:27 +00:00
|
|
|
(catch 'exit
|
|
|
|
;; Exact match on name
|
|
|
|
(bbdb-name (concat "\\`" name "\\'") nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; Exact match on name
|
|
|
|
(bbdb-company (concat "\\`" name "\\'") nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; Partial match on name
|
|
|
|
(bbdb-name name nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; Partial match on company
|
|
|
|
(bbdb-company name nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; General match including network address and notes
|
|
|
|
(bbdb name nil)
|
|
|
|
(when (= 0 (buffer-size (get-buffer "*BBDB*")))
|
|
|
|
(delete-window (get-buffer-window "*BBDB*"))
|
|
|
|
(error "No matching BBDB record"))))
|
|
|
|
|
2012-03-04 19:37:46 +00:00
|
|
|
(defun org-bbdb-open-new (name)
|
2011-12-31 16:04:27 +00:00
|
|
|
(catch 'exit
|
|
|
|
;; Exact match on name
|
|
|
|
(bbdb-search-name (concat "\\`" name "\\'") nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; Exact match on name
|
|
|
|
(bbdb-search-organization (concat "\\`" name "\\'") nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; Partial match on name
|
|
|
|
(bbdb-search-name name nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; Partial match on company
|
|
|
|
(bbdb-search-organization name nil)
|
|
|
|
(if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
|
|
|
|
;; General match including network address and notes
|
|
|
|
(bbdb name nil)
|
|
|
|
(when (= 0 (buffer-size (get-buffer "*BBDB*")))
|
|
|
|
(delete-window (get-buffer-window "*BBDB*"))
|
|
|
|
(error "No matching BBDB record"))))
|
2008-03-14 17:22:03 +00:00
|
|
|
|
2008-04-13 19:04:21 +00:00
|
|
|
(defun org-bbdb-anniv-extract-date (time-str)
|
|
|
|
"Convert YYYY-MM-DD to (month date year).
|
2011-02-07 16:43:15 +00:00
|
|
|
Argument TIME-STR is the value retrieved from BBDB. If YYYY- is omitted
|
|
|
|
it will be considered unknown."
|
2016-07-25 14:34:48 +00:00
|
|
|
(pcase (org-split-string time-str "-")
|
|
|
|
(`(,a ,b nil) (list (string-to-number a) (string-to-number b) nil))
|
|
|
|
(`(,a ,b ,c) (list (string-to-number b)
|
|
|
|
(string-to-number c)
|
|
|
|
(string-to-number a)))))
|
2008-04-13 19:04:21 +00:00
|
|
|
|
|
|
|
(defun org-bbdb-anniv-split (str)
|
2008-05-09 04:08:45 +00:00
|
|
|
"Split multiple entries in the BBDB anniversary field.
|
2008-04-13 19:04:21 +00:00
|
|
|
Argument STR is the anniversary field in BBDB."
|
|
|
|
(let ((pos (string-match "[ \t]" str)))
|
|
|
|
(if pos (list (substring str 0 pos)
|
|
|
|
(bbdb-string-trim (substring str pos)))
|
|
|
|
(list str nil))))
|
|
|
|
|
2008-05-06 10:46:51 +00:00
|
|
|
(defvar org-bbdb-anniv-hash nil
|
|
|
|
"A hash holding anniversaries extracted from BBDB.
|
|
|
|
The hash table is created on first use.")
|
2008-04-13 19:04:21 +00:00
|
|
|
|
2008-05-06 10:46:51 +00:00
|
|
|
(defvar org-bbdb-updated-p t
|
|
|
|
"This is non-nil if BBDB has been updated since we last built the hash.")
|
|
|
|
|
|
|
|
(defun org-bbdb-make-anniv-hash ()
|
|
|
|
"Create a hash with anniversaries extracted from BBDB, for fast access.
|
|
|
|
The anniversaries are assumed to be stored `org-bbdb-anniversary-field'."
|
2012-04-07 12:21:57 +00:00
|
|
|
(let ((old-bbdb (fboundp 'bbdb-record-getprop))
|
2013-04-03 11:54:17 +00:00
|
|
|
(record-func (if (fboundp 'bbdb-record-xfield)
|
|
|
|
'bbdb-record-xfield
|
|
|
|
'bbdb-record-note))
|
2012-04-07 12:21:57 +00:00
|
|
|
split tmp annivs)
|
2008-05-06 10:46:51 +00:00
|
|
|
(clrhash org-bbdb-anniv-hash)
|
2008-04-13 19:04:21 +00:00
|
|
|
(dolist (rec (bbdb-records))
|
2012-04-07 12:21:57 +00:00
|
|
|
(when (setq annivs (if old-bbdb
|
|
|
|
(bbdb-record-getprop
|
|
|
|
rec org-bbdb-anniversary-field)
|
2013-04-03 11:54:17 +00:00
|
|
|
(funcall record-func
|
|
|
|
rec org-bbdb-anniversary-field)))
|
2012-04-07 12:21:57 +00:00
|
|
|
(setq annivs (if old-bbdb
|
|
|
|
(bbdb-split annivs "\n")
|
|
|
|
;; parameter order is reversed in new bbdb
|
|
|
|
(bbdb-split "\n" annivs)))
|
2008-04-13 19:04:21 +00:00
|
|
|
(while annivs
|
|
|
|
(setq split (org-bbdb-anniv-split (pop annivs)))
|
2016-07-25 14:34:48 +00:00
|
|
|
(pcase-let ((`(,m ,d ,y) (funcall org-bbdb-extract-date-fun
|
|
|
|
(car split))))
|
|
|
|
(setq tmp (gethash (list m d) org-bbdb-anniv-hash))
|
2008-12-04 14:33:43 +00:00
|
|
|
(puthash (list m d) (cons (list y
|
|
|
|
(bbdb-record-name rec)
|
2008-05-06 10:46:51 +00:00
|
|
|
(cadr split))
|
|
|
|
tmp)
|
|
|
|
org-bbdb-anniv-hash))))))
|
|
|
|
(setq org-bbdb-updated-p nil))
|
2008-04-24 08:29:47 +00:00
|
|
|
|
2015-12-20 21:37:36 +00:00
|
|
|
(defun org-bbdb-updated (_rec)
|
2008-05-06 10:46:51 +00:00
|
|
|
"Record the fact that BBDB has been updated.
|
|
|
|
This is used by Org to re-create the anniversary hash table."
|
|
|
|
(setq org-bbdb-updated-p t))
|
|
|
|
|
|
|
|
(add-hook 'bbdb-after-change-hook 'org-bbdb-updated)
|
|
|
|
|
|
|
|
;;;###autoload
|
2012-10-02 07:57:10 +00:00
|
|
|
(defun org-bbdb-anniversaries ()
|
2008-05-06 10:46:51 +00:00
|
|
|
"Extract anniversaries from BBDB for display in the agenda."
|
2008-09-26 16:38:27 +00:00
|
|
|
(require 'bbdb)
|
2008-05-06 10:46:51 +00:00
|
|
|
(require 'diary-lib)
|
|
|
|
(unless (hash-table-p org-bbdb-anniv-hash)
|
|
|
|
(setq org-bbdb-anniv-hash
|
|
|
|
(make-hash-table :test 'equal :size 366)))
|
|
|
|
|
|
|
|
(when (or org-bbdb-updated-p
|
|
|
|
(= 0 (hash-table-count org-bbdb-anniv-hash)))
|
|
|
|
(org-bbdb-make-anniv-hash))
|
|
|
|
|
|
|
|
(let* ((m (car date)) ; month
|
|
|
|
(d (nth 1 date)) ; day
|
|
|
|
(y (nth 2 date)) ; year
|
|
|
|
(annivs (gethash (list m d) org-bbdb-anniv-hash))
|
|
|
|
(text ())
|
2009-01-26 08:42:33 +00:00
|
|
|
rec recs)
|
2008-12-04 14:33:43 +00:00
|
|
|
|
2008-05-06 10:46:51 +00:00
|
|
|
;; we don't want to miss people born on Feb. 29th
|
2008-10-06 18:45:02 +00:00
|
|
|
(when (and (= m 3) (= d 1)
|
|
|
|
(not (null (gethash (list 2 29) org-bbdb-anniv-hash)))
|
|
|
|
(not (calendar-leap-year-p y)))
|
|
|
|
(setq recs (gethash (list 2 29) org-bbdb-anniv-hash))
|
|
|
|
(while (setq rec (pop recs))
|
|
|
|
(push rec annivs)))
|
2008-05-06 10:46:51 +00:00
|
|
|
|
|
|
|
(when annivs
|
|
|
|
(while (setq rec (pop annivs))
|
2008-12-04 14:33:43 +00:00
|
|
|
(when rec
|
2008-05-06 10:46:51 +00:00
|
|
|
(let* ((class (or (nth 2 rec)
|
|
|
|
org-bbdb-default-anniversary-format))
|
2010-08-12 09:03:07 +00:00
|
|
|
(form (or (cdr (assoc-string
|
|
|
|
class org-bbdb-anniversary-format-alist t))
|
2008-05-06 10:46:51 +00:00
|
|
|
class)) ; (as format string)
|
|
|
|
(name (nth 1 rec))
|
2011-02-07 16:43:15 +00:00
|
|
|
(years (if (eq (car rec) nil)
|
|
|
|
"unknown"
|
|
|
|
(- y (car rec))))
|
|
|
|
(suffix (if (eq (car rec) nil)
|
|
|
|
""
|
|
|
|
(diary-ordinal-suffix years)))
|
2008-05-06 10:46:51 +00:00
|
|
|
(tmp (cond
|
|
|
|
((functionp form)
|
|
|
|
(funcall form name years suffix))
|
|
|
|
((listp form) (eval form))
|
|
|
|
(t (format form name years suffix)))))
|
2008-06-12 13:09:03 +00:00
|
|
|
(org-add-props tmp nil 'org-bbdb-name name)
|
2008-05-06 10:46:51 +00:00
|
|
|
(if text
|
|
|
|
(setq text (append text (list tmp)))
|
|
|
|
(setq text (list tmp)))))
|
|
|
|
))
|
2010-10-24 20:01:47 +00:00
|
|
|
text))
|
2008-04-24 08:29:47 +00:00
|
|
|
|
2015-10-13 13:00:16 +00:00
|
|
|
;;; Return list of anniversaries for today and the next n-1 (default: n=7) days.
|
|
|
|
;;; This is meant to be used in an org file instead of org-bbdb-anniversaries:
|
|
|
|
;;;
|
|
|
|
;;; %%(org-bbdb-anniversaries-future)
|
|
|
|
;;;
|
|
|
|
;;; or
|
|
|
|
;;;
|
|
|
|
;;; %%(org-bbdb-anniversaries-future 3)
|
|
|
|
;;;
|
|
|
|
;;; to override the 7-day default.
|
|
|
|
|
2015-12-20 21:37:36 +00:00
|
|
|
(defun org-bbdb-date-list (d n)
|
|
|
|
"Return a list of dates in (m d y) format from the given date D to n-1 days hence."
|
|
|
|
(let ((abs (calendar-absolute-from-gregorian d)))
|
|
|
|
(mapcar (lambda (i) (calendar-gregorian-from-absolute (+ abs i)))
|
|
|
|
(number-sequence 0 (1- n)))))
|
2015-10-13 13:00:16 +00:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun org-bbdb-anniversaries-future (&optional n)
|
|
|
|
"Return list of anniversaries for today and the next n-1 days (default n=7)."
|
|
|
|
(let ((n (or n 7)))
|
|
|
|
(when (<= n 0)
|
2016-07-25 14:34:48 +00:00
|
|
|
(error "The (optional) argument of `org-bbdb-anniversaries-future' \
|
|
|
|
must be positive"))
|
2015-10-13 13:00:16 +00:00
|
|
|
(let (
|
|
|
|
;; List of relevant dates.
|
|
|
|
(dates (org-bbdb-date-list date n))
|
2016-07-25 14:34:48 +00:00
|
|
|
;; Function to annotate text of each element of l with the
|
|
|
|
;; anniversary date d.
|
2015-10-13 13:00:16 +00:00
|
|
|
(annotate-descriptions
|
|
|
|
(lambda (d l)
|
|
|
|
(mapcar (lambda (x)
|
2016-07-25 14:34:48 +00:00
|
|
|
;; The assumption here is that x is a bbdb link
|
|
|
|
;; of the form [[bbdb:name][description]].
|
|
|
|
;; This function rather arbitrarily modifies
|
|
|
|
;; the description by adding the date to it in
|
|
|
|
;; a fixed format.
|
2015-10-13 13:00:16 +00:00
|
|
|
(string-match "]]" x)
|
2016-07-25 14:34:48 +00:00
|
|
|
(replace-match (format " -- %d-%02d-%02d\\&"
|
|
|
|
(nth 2 d)
|
|
|
|
(nth 0 d)
|
|
|
|
(nth 1 d))
|
2015-10-13 13:00:16 +00:00
|
|
|
nil nil x))
|
|
|
|
l))))
|
2016-07-25 14:34:48 +00:00
|
|
|
;; Map a function that generates anniversaries for each date
|
|
|
|
;; over the dates and nconc the results into a single list. When
|
|
|
|
;; it is no longer necessary to support older versions of Emacs,
|
|
|
|
;; this can be done with a cl-mapcan; for now, we use the (apply
|
|
|
|
;; #'nconc ...) method for compatibility.
|
2015-10-13 13:00:16 +00:00
|
|
|
(apply #'nconc
|
2015-10-21 01:10:52 +00:00
|
|
|
(mapcar
|
|
|
|
(lambda (d)
|
|
|
|
(let ((date d))
|
2016-07-25 14:34:48 +00:00
|
|
|
;; Rebind 'date' so that org-bbdb-anniversaries will
|
|
|
|
;; be fooled into giving us the list for the given
|
|
|
|
;; date and then annotate the descriptions for that
|
|
|
|
;; date.
|
2015-10-21 01:10:52 +00:00
|
|
|
(funcall annotate-descriptions d (org-bbdb-anniversaries))))
|
|
|
|
dates)))))
|
2015-10-13 13:00:16 +00:00
|
|
|
|
2009-05-10 06:51:18 +00:00
|
|
|
(defun org-bbdb-complete-link ()
|
|
|
|
"Read a bbdb link with name completion."
|
2009-05-10 07:15:13 +00:00
|
|
|
(require 'bbdb-com)
|
2014-04-29 00:59:13 +00:00
|
|
|
(let ((rec (bbdb-completing-read-record "Name: ")))
|
|
|
|
(concat "bbdb:"
|
|
|
|
(bbdb-record-name (if (listp rec)
|
|
|
|
(car rec)
|
|
|
|
rec)))))
|
2009-05-10 06:51:18 +00:00
|
|
|
|
2009-06-11 15:42:28 +00:00
|
|
|
(defun org-bbdb-anniv-export-ical ()
|
2009-06-11 20:18:47 +00:00
|
|
|
"Extract anniversaries from BBDB and convert them to icalendar format."
|
|
|
|
(require 'bbdb)
|
|
|
|
(require 'diary-lib)
|
|
|
|
(unless (hash-table-p org-bbdb-anniv-hash)
|
|
|
|
(setq org-bbdb-anniv-hash
|
2009-06-11 15:42:28 +00:00
|
|
|
(make-hash-table :test 'equal :size 366)))
|
2009-06-11 20:18:47 +00:00
|
|
|
(when (or org-bbdb-updated-p
|
|
|
|
(= 0 (hash-table-count org-bbdb-anniv-hash)))
|
|
|
|
(org-bbdb-make-anniv-hash))
|
|
|
|
(maphash 'org-bbdb-format-vevent org-bbdb-anniv-hash))
|
|
|
|
|
|
|
|
(defun org-bbdb-format-vevent (key recs)
|
|
|
|
(let (rec categ)
|
|
|
|
(while (setq rec (pop recs))
|
2009-06-22 19:59:21 +00:00
|
|
|
(setq categ (or (nth 2 rec) org-bbdb-default-anniversary-format))
|
2009-06-11 20:18:47 +00:00
|
|
|
(princ (format "BEGIN:VEVENT
|
2009-06-11 15:42:28 +00:00
|
|
|
UID: ANNIV-%4i%02i%02i-%s
|
|
|
|
DTSTART:%4i%02i%02i
|
2009-06-22 19:59:21 +00:00
|
|
|
SUMMARY:%s
|
|
|
|
DESCRIPTION:%s
|
|
|
|
CATEGORIES:%s
|
|
|
|
RRULE:FREQ=YEARLY
|
|
|
|
END:VEVENT\n"
|
|
|
|
(nth 0 rec) (nth 0 key) (nth 1 key)
|
2009-06-11 20:18:47 +00:00
|
|
|
(mapconcat 'identity
|
|
|
|
(org-split-string (nth 1 rec) "[^a-zA-Z0-90]+")
|
|
|
|
"-")
|
2009-06-22 19:59:21 +00:00
|
|
|
(nth 0 rec) (nth 0 key) (nth 1 key)
|
2009-08-03 15:30:30 +00:00
|
|
|
(nth 1 rec)
|
2009-06-22 19:59:21 +00:00
|
|
|
(concat (capitalize categ) " " (nth 1 rec))
|
2009-06-23 16:20:30 +00:00
|
|
|
categ)))))
|
2009-06-11 15:42:28 +00:00
|
|
|
|
2008-03-14 17:22:03 +00:00
|
|
|
(provide 'org-bbdb)
|
|
|
|
|
2012-10-02 06:50:46 +00:00
|
|
|
;; Local variables:
|
|
|
|
;; generated-autoload-file: "org-loaddefs.el"
|
|
|
|
;; End:
|
|
|
|
|
2008-03-14 17:22:03 +00:00
|
|
|
;;; org-bbdb.el ends here
|