updated copyright year and owner
[org-bookmarks.git] / org-bookmarks.el
blob4e1d40af615a42fb04a9865edbfc70f9b0b48d8e
1 ;;; org-bookmarks.el --- Manage bookmarks in Org mode -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 stardiviner <numbchild@gmail.com>
6 ;; Authors: stardiviner <numbchild@gmail.com>
7 ;; Package-Requires: ((emacs "26.1"))
8 ;; Version: 0.1
9 ;; Keywords: outline matching hypermedia org
10 ;; URL: https://repo.or.cz/org-bookmarks.git
12 ;; org-bookmarks is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; org-bookmarks is distributed in the hope that it will be useful, but WITHOUT
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 ;; License for more details. https://www.gnu.org/licenses/gpl-3.0.txt
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Usage
30 ;;; Code:
32 (require 'org) ; for `org-tags-column'
33 (require 'org-element)
35 (eval-when-compile (require 'org-capture))
36 (eval-when-compile (require 'nerd-icons nil t))
37 (declare-function 'nerd-icons-mdicon "nerd-icons" (icon-name &rest args))
39 (defgroup org-bookmarks nil
40 "The defcustom group of `org-bookmarks'."
41 :prefix "org-boomarks-"
42 :group 'org)
44 ;; It would be better to default to the demo file in the repository.
45 ;; That avoids creating a directory on the user's system that may not exist yet.
46 ;; It also allows the user to try the command out without customizing anything first.
47 (defcustom org-bookmarks-file
48 (expand-file-name "bookmarks.org" (file-name-directory (or load-file-name (buffer-file-name))))
49 "The Org bookmarks filename."
50 :type 'string
51 :safe #'stringp
52 :group 'org-bookmarks)
54 (defcustom org-bookmarks-tag "bookmark"
55 "The tag to mark Org headline as bookmark entry."
56 :type 'string
57 :safe #'stringp
58 :group 'org-bookmarks)
60 (defcustom org-bookmarks-browse-function #'browse-url
61 "Function called by `org-bookmarks' with selected URL as its sole argument."
62 :type 'function
63 :group 'org-bookmarks)
65 (defcustom org-bookmarks-add-org-capture-template nil
66 "Add `org-capture' template for `org-bookmarks'."
67 :type 'boolean
68 :safe #'booleanp
69 :group 'org-bookmarks)
72 (defun org-bookmarks--candidate (headline)
73 "Return candidate string from Org HEADLINE."
74 (when-let* ((tags (org-element-property :tags headline))
75 ( (member org-bookmarks-tag tags))
76 (url (alist-get "URL" (org-entry-properties headline 'standard) nil nil #'equal))
77 (info (concat "\n" (propertize url 'face 'link) "\n")) ; multi-line candidate with "\n"
78 (headline-title (org-element-property :raw-value headline)))
79 ;; The URL and ANNOTATION properties will be used for candidate display and browsing.
80 (let* ((tags-searchable (delete org-bookmarks-tag tags))
81 ;; TODO: The length counting method not correct on Chinese.
82 (middle-line-length (when-let* ((length (- (- org-tags-column)
83 (length (string-join tags-searchable ":"))
84 (length headline-title) 2))
85 ((wholenump length)))
86 length)))
87 (propertize
88 (concat headline-title
89 (format " %s [%s]"
90 (make-string (or middle-line-length 0) ?―)
91 (if (= (length tags-searchable) 1)
92 (car tags-searchable)
93 (string-join tags-searchable ":"))))
94 'url url 'annotation info))))
96 (defun org-bookmarks--candidates (file)
97 "Return a list of candidates from FILE."
98 ;; It's better to use a temp buffer than touch the user's buffer.
99 ;; It also cleans up after itself.
100 (with-temp-buffer
101 (insert-file-contents file) ; don't need to actually open file.
102 (delay-mode-hooks ; This will prevent user hooks from running during parsing.
103 (org-mode)
104 (goto-char (point-min))
105 (let ((candidates nil))
106 (org-element-map (org-element-parse-buffer 'headline) 'headline
107 (lambda (headline)
108 (when-let ((candidate (org-bookmarks--candidate headline)))
109 (push candidate candidates))))
110 (nreverse candidates)))))
112 (defun org-bookmarks--annotator (candidate)
113 "Annotate bookmark completion CANDIDATE."
114 (concat (propertize " " 'display '(space :align-to center))
115 (get-text-property 0 'annotation candidate)))
117 (defun org-bookmarks--return-candidates (&optional file)
118 "Return `org-bookmarks' candidates which parsed from FILE."
119 (if-let ((file (or file org-bookmarks-file)))
120 (org-bookmarks--candidates file)
121 (user-error "File does not exist: %S" file)))
123 (defvar org-bookmarks--candidates-cache nil
124 "A cache variable of org-bookmarks--candidates.")
126 ;;;###autoload
127 (defun org-bookmarks (&optional file)
128 "Open bookmark read from FILE or `org-bookmarks-file'."
129 (interactive)
130 (unless org-bookmarks--candidates-cache
131 (setq org-bookmarks--candidates-cache (org-bookmarks--return-candidates)))
132 (if-let* ((file (or file org-bookmarks-file))
133 ( (file-exists-p file)))
134 (if-let* ((candidates org-bookmarks--candidates-cache)
135 (minibuffer-allow-text-properties t)
136 (completion-extra-properties
137 ;; Using the "bookmark" category caused the annotations to not show.
138 ;; I think that may have be do to vertico-mode, but
139 ;; it's probably worth using a unique category so users can exercise finer-grained customization.
140 (list :category 'org-bookmark
141 :annotation-function #'org-bookmarks--annotator))
142 (choice (completing-read "org-bookmarks: " candidates nil 'require-match))
143 (url (get-text-property 0 'url choice)))
144 (funcall org-bookmarks-browse-function url)
145 (user-error "No bookmarks found in %S" file))
146 (user-error "File does not exist: %S" file)))
148 ;;; TEST:
149 ;; (org-bookmarks "bookmarks.org")
150 ;; (org-bookmarks (expand-file-name org-bookmarks-file))
152 ;;; Add `org-capture' template for adding new bookmark to `org-bookmarks-file'
153 ;;;###autoload
154 (when org-bookmarks-add-org-capture-template
155 (unless (assoc "b" org-capture-templates)
156 (add-to-list
157 'org-capture-templates
158 `("b" ,(format "%s\tAdd a new bookmark to %s"
159 (when (fboundp 'nerd-icons-mdicon)
160 (nerd-icons-mdicon "nf-md-bookmark_plus_outline" :face 'nerd-icons-blue))
161 org-bookmarks-file)
162 entry (file ,(expand-file-name org-bookmarks-file))
163 "* %^{bookmark title}
164 :PROPERTIES:
165 :URL: %^C
166 :DATE: %t
167 :END:"
168 :empty-lines 1
169 :jump-to-captured t)
170 :append)))
174 (provide 'org-bookmarks)
176 ;;; org-bookmarks.el ends here