org-bookmark.el 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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