org-bookmark.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ;;; org-bookmark.el - Support for links to bookmark
  2. ;; Copyright (C) 2008-2018 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-link-set-parameters "bookmark"
  43. :follow #'org-bookmark-open
  44. :store #'org-bookmark-store-link)
  45. (defun org-bookmark-open (bookmark)
  46. "Visit the bookmark BOOKMARK."
  47. (bookmark-jump bookmark))
  48. (defun org-bookmark-store-link ()
  49. "Store a link to the current line's bookmark in bookmark list."
  50. (let (file bookmark bmks)
  51. (cond ((and org-bookmark-in-dired
  52. (eq major-mode 'dired-mode))
  53. (setq file (abbreviate-file-name (dired-get-filename))))
  54. ((and org-bookmark-when-visiting-a-file
  55. (buffer-file-name (buffer-base-buffer)))
  56. (setq file (abbreviate-file-name
  57. (buffer-file-name (buffer-base-buffer))))))
  58. (if (not file)
  59. (when (eq major-mode 'bookmark-bmenu-mode)
  60. (setq bookmark (bookmark-bmenu-bookmark)))
  61. (when (and (setq bmks
  62. (mapcar (lambda (name)
  63. (if (equal file
  64. (abbreviate-file-name
  65. (bookmark-location name)))
  66. name))
  67. (bookmark-all-names)))
  68. (setq bmks (delete nil bmks)))
  69. (setq bookmark
  70. (if (or (eq 1 (length bmks)) org-bookmark-use-first-bookmark)
  71. (car bmks)
  72. (completing-read "Bookmark: " bmks nil t nil nil (car bmks))))))
  73. (if bookmark
  74. (org-store-link-props :link (concat "bookmark:" bookmark)
  75. :description bookmark))))
  76. (provide 'org-bookmark)
  77. ;;; org-bookmark.el ends here