org-bookmark.el 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ;;; org-bookmark.el - Support for links to bookmark
  2. ;; Copyright (C) 2008-2016 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Tokuya Kameshima <kames AT fa2.so-net.ne.jp>
  5. ;; Version: 1.0
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;;
  8. ;; This file is not part of GNU Emacs.
  9. ;;
  10. ;; 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. ;; This program 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. (require 'org)
  24. (require 'bookmark)
  25. (defgroup org-bookmark nil
  26. "Options concerning the bookmark link."
  27. :tag "Org Startup"
  28. :group 'org-link)
  29. (defcustom org-bookmark-in-dired nil
  30. "Use org-bookmark in dired."
  31. :group 'org-bookmark
  32. :type 'boolean)
  33. (defcustom org-bookmark-when-visiting-a-file nil
  34. "Use org-bookmark in any buffer visiting a file."
  35. :group 'org-bookmark
  36. :type 'boolean)
  37. (defcustom org-bookmark-use-first-bookmark nil
  38. "If several bookmarks links to the buffer, take the first one.
  39. Otherwise prompt the user for the right bookmark to use."
  40. :group 'org-bookmark
  41. :type 'boolean)
  42. (org-add-link-type "bookmark" 'org-bookmark-open)
  43. (add-hook 'org-store-link-functions 'org-bookmark-store-link)
  44. (defun org-bookmark-open (bookmark)
  45. "Visit the bookmark BOOKMARK."
  46. (bookmark-jump bookmark))
  47. (defun org-bookmark-store-link ()
  48. "Store a link to the current line's bookmark in bookmark list."
  49. (let (file bookmark bmks)
  50. (cond ((and org-bookmark-in-dired
  51. (eq major-mode 'dired-mode))
  52. (setq file (abbreviate-file-name (dired-get-filename))))
  53. ((and org-bookmark-when-visiting-a-file
  54. (buffer-file-name (buffer-base-buffer)))
  55. (setq file (abbreviate-file-name
  56. (buffer-file-name (buffer-base-buffer))))))
  57. (if (not file)
  58. (when (eq major-mode 'bookmark-bmenu-mode)
  59. (setq bookmark (bookmark-bmenu-bookmark)))
  60. (when (and (setq bmks
  61. (mapcar (lambda (name)
  62. (if (equal file
  63. (abbreviate-file-name
  64. (bookmark-location name)))
  65. name))
  66. (bookmark-all-names)))
  67. (setq bmks (delete nil bmks)))
  68. (setq bookmark
  69. (if (or (eq 1 (length bmks)) org-bookmark-use-first-bookmark)
  70. (car bmks)
  71. (completing-read "Bookmark: " bmks nil t nil nil (car bmks))))))
  72. (if bookmark
  73. (org-store-link-props :link (concat "bookmark:" bookmark)
  74. :description bookmark))))
  75. (provide 'org-bookmark)
  76. ;;; org-bookmark.el ends here