org-bbdb.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ;;; org-bbdb.el --- Support for links to BBDB entries from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.00pre-1
  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, or (at your option)
  13. ;; 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; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file implements links to BBDB database entries from within Org-mode.
  26. ;; Org-mode loads this module by default - if this is not what you want,
  27. ;; configure the variable `org-modules'.
  28. ;;; Code:
  29. (require 'org)
  30. ;; Declare external functions and variables
  31. (declare-function bbdb "ext:bbdb-com" (string elidep))
  32. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  33. (declare-function bbdb-current-record "ext:bbdb-com"
  34. (&optional planning-on-modifying))
  35. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  36. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  37. (declare-function bbdb-record-name "ext:bbdb" (record))
  38. ;; Install the link type
  39. (org-add-link-type "bbdb" 'org-bbdb-open 'org-bbdb-export)
  40. (add-hook 'org-store-link-functions 'org-bbdb-store-link)
  41. ;; Implementation
  42. (defun org-bbdb-store-link ()
  43. "Store a link to a BBDB database entry."
  44. (when (eq major-mode 'bbdb-mode)
  45. ;; This is BBDB, we make this link!
  46. (let* ((name (bbdb-record-name (bbdb-current-record)))
  47. (company (bbdb-record-getprop (bbdb-current-record) 'company))
  48. (link (org-make-link "bbdb:" name)))
  49. (org-store-link-props :type "bbdb" :name name :company company
  50. :link link :description name)
  51. link)))
  52. (defun org-bbdb-export (path desc format)
  53. "Create the export version of a BBDB link specified by PATH or DESC.
  54. If exporting to either HTML or LaTeX FORMAT the link will be
  55. italicised, in all other cases it is left unchanged."
  56. "Create the exprt verison of a bbdb link."
  57. (cond
  58. ((eq format 'html) (format "<i>%s</i>" (or desc path)))
  59. ((eq format 'latex) (format "\\textit{%s}" (or desc path)))
  60. (t (or desc path))))
  61. (defun org-bbdb-open (name)
  62. "Follow a BBDB link to NAME."
  63. (require 'bbdb)
  64. (let ((inhibit-redisplay (not debug-on-error))
  65. (bbdb-electric-p nil))
  66. (catch 'exit
  67. ;; Exact match on name
  68. (bbdb-name (concat "\\`" name "\\'") nil)
  69. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  70. ;; Exact match on name
  71. (bbdb-company (concat "\\`" name "\\'") nil)
  72. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  73. ;; Partial match on name
  74. (bbdb-name name nil)
  75. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  76. ;; Partial match on company
  77. (bbdb-company name nil)
  78. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  79. ;; General match including network address and notes
  80. (bbdb name nil)
  81. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  82. (delete-window (get-buffer-window "*BBDB*"))
  83. (error "No matching BBDB record")))))
  84. (provide 'org-bbdb)
  85. ;;; org-bbdb.el ends here