From 31e448d83857986c7751a06bc6384ac874249707 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Fri, 21 Feb 2025 17:39:14 +0100 Subject: [PATCH] Add Org links to bookmarks This is to remove redundant references in both bookmarks and links in Org files. The code is based on the original implementation of `ol-bookmark.el` by Tokuya Kameshima, as found in [org-contrib][1]. [1]: https://github.com/emacsmirror/org-contrib/blob/bd39cca48b1c4a8a1cdfb1cdd6be2ce700acdd97/lisp/ol-bookmark.el --- site-lisp/db-org.el | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index b992a8d..437720d 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -20,6 +20,8 @@ (require 'ox-icalendar) (require 'org-ql) (require 'holidays) +(require 'dired) +(require 'bookmark) (autoload 'which-function "which-func") (autoload 'org-element-property "org-element") @@ -2220,6 +2222,51 @@ PARAMS may contain the following values: (org-dynamic-block-define "db/org-backlinks" #'db/org-insert-backlink-block) + +;;; Adding links to bookmarks + +;; The code in this section is based version 1.0 of `ol-bookmark.el' by Tokuya Kameshima; +;; see https://github.com/emacsmirror/org-contrib/blob/bd39cca48b1c4a8a1cdfb1cdd6be2ce700acdd97/lisp/ol-bookmark.el. + +(org-link-set-parameters "bookmark" + :follow #'org-bookmark-open + :store #'org-bookmark-store-link) + +(defun org-bookmark-open (bookmark _) + "Visit the bookmark BOOKMARK." + (bookmark-jump bookmark)) + +(defun org-bookmark-store-link () + "Store a link to the bookmark at point. + +When in Dired, try to find bookmark that points to the file at +point. When in a buffer associated with a file, try to find a +bookmark that points to this file." + (let (file bookmark bmks) + (cond ((eq major-mode 'dired-mode) + (setq file (abbreviate-file-name (dired-get-filename)))) + ((buffer-file-name (buffer-base-buffer)) + (setq file (abbreviate-file-name + (buffer-file-name (buffer-base-buffer)))))) + (if (not file) + (when (eq major-mode 'bookmark-bmenu-mode) + (setq bookmark (bookmark-bmenu-bookmark))) + (when (setq bmks + (->> (bookmark-all-names) + (-map (lambda (name) + (if (file-equal-p file + (abbreviate-file-name + (bookmark-location name))) + name))) + (delete nil))) + (setq bookmark + (if (eq 1 (length bmks)) + (car bmks) + (completing-read "Bookmark: " bmks nil t nil nil (car bmks)))))) + (if bookmark + (org-link-store-props :link (concat "bookmark:" bookmark) + :description bookmark)))) + ;;; End