Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-bookmark.el
blob04a473b60bead1dea2fe96585cf673562cbd1840
1 ;;; org-bookmark.el - Support for links to bookmark
2 ;; Copyright (C) 2008-2017 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.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 (require 'org)
27 (require 'bookmark)
29 (defgroup org-bookmark nil
30 "Options concerning the bookmark link."
31 :tag "Org Startup"
32 :group 'org-link)
34 (defcustom org-bookmark-in-dired nil
35 "Use org-bookmark in dired."
36 :group 'org-bookmark
37 :type 'boolean)
39 (defcustom org-bookmark-when-visiting-a-file nil
40 "Use org-bookmark in any buffer visiting a file."
41 :group 'org-bookmark
42 :type 'boolean)
44 (defcustom org-bookmark-use-first-bookmark nil
45 "If several bookmarks links to the buffer, take the first one.
46 Otherwise prompt the user for the right bookmark to use."
47 :group 'org-bookmark
48 :type 'boolean)
50 (org-link-set-parameters "bookmark"
51 :follow #'org-bookmark-open
52 :store #'org-bookmark-store-link)
54 (defun org-bookmark-open (bookmark)
55 "Visit the bookmark BOOKMARK."
56 (bookmark-jump bookmark))
58 (defun org-bookmark-store-link ()
59 "Store a link to the current line's bookmark in bookmark list."
60 (let (file bookmark bmks)
61 (cond ((and org-bookmark-in-dired
62 (eq major-mode 'dired-mode))
63 (setq file (abbreviate-file-name (dired-get-filename))))
64 ((and org-bookmark-when-visiting-a-file
65 (buffer-file-name (buffer-base-buffer)))
66 (setq file (abbreviate-file-name
67 (buffer-file-name (buffer-base-buffer))))))
68 (if (not file)
69 (when (eq major-mode 'bookmark-bmenu-mode)
70 (setq bookmark (bookmark-bmenu-bookmark)))
71 (when (and (setq bmks
72 (mapcar (lambda (name)
73 (if (equal file
74 (abbreviate-file-name
75 (bookmark-location name)))
76 name))
77 (bookmark-all-names)))
78 (setq bmks (delete nil bmks)))
79 (setq bookmark
80 (if (or (eq 1 (length bmks)) org-bookmark-use-first-bookmark)
81 (car bmks)
82 (completing-read "Bookmark: " bmks nil t nil nil (car bmks))))))
83 (if bookmark
84 (org-store-link-props :link (concat "bookmark:" bookmark)
85 :description bookmark))))
87 (provide 'org-bookmark)
89 ;;; org-bookmark.el ends here