ol-bookmark.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ;;; ol-bookmark.el - Links to bookmarks
  2. ;; Copyright (C) 2008-2021 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. (require 'ol)
  26. (defgroup org-bookmark nil
  27. "Options concerning the bookmark link."
  28. :tag "Org Startup"
  29. :group 'org-link)
  30. (defcustom org-bookmark-in-dired nil
  31. "Use org-bookmark in dired."
  32. :group 'org-bookmark
  33. :type 'boolean)
  34. (defcustom org-bookmark-when-visiting-a-file nil
  35. "Use org-bookmark in any buffer visiting a file."
  36. :group 'org-bookmark
  37. :type 'boolean)
  38. (defcustom org-bookmark-use-first-bookmark nil
  39. "If several bookmarks links to the buffer, take the first one.
  40. Otherwise prompt the user for the right bookmark to use."
  41. :group 'org-bookmark
  42. :type 'boolean)
  43. (org-link-set-parameters "bookmark"
  44. :follow #'org-bookmark-open
  45. :store #'org-bookmark-store-link)
  46. (defun org-bookmark-open (bookmark _)
  47. "Visit the bookmark BOOKMARK."
  48. (bookmark-jump bookmark))
  49. (defun org-bookmark-store-link ()
  50. "Store a link to the current line's bookmark in bookmark list."
  51. (let (file bookmark bmks)
  52. (cond ((and org-bookmark-in-dired
  53. (eq major-mode 'dired-mode))
  54. (setq file (abbreviate-file-name (dired-get-filename))))
  55. ((and org-bookmark-when-visiting-a-file
  56. (buffer-file-name (buffer-base-buffer)))
  57. (setq file (abbreviate-file-name
  58. (buffer-file-name (buffer-base-buffer))))))
  59. (if (not file)
  60. (when (eq major-mode 'bookmark-bmenu-mode)
  61. (setq bookmark (bookmark-bmenu-bookmark)))
  62. (when (and (setq bmks
  63. (mapcar (lambda (name)
  64. (if (equal file
  65. (abbreviate-file-name
  66. (bookmark-location name)))
  67. name))
  68. (bookmark-all-names)))
  69. (setq bmks (delete nil bmks)))
  70. (setq bookmark
  71. (if (or (eq 1 (length bmks)) org-bookmark-use-first-bookmark)
  72. (car bmks)
  73. (completing-read "Bookmark: " bmks nil t nil nil (car bmks))))))
  74. (if bookmark
  75. (org-store-link-props :link (concat "bookmark:" bookmark)
  76. :description bookmark))))
  77. (provide 'ol-bookmark)
  78. ;;; ol-bookmark.el ends here