Add some Org lint checkers for bookmark links

Code taken from `org-lint-invalid-id-link` and suitably modified.
This commit is contained in:
Daniel Borchmann 2025-02-25 18:26:08 +01:00
parent 3dbc9604d4
commit a522f48dc5
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625
2 changed files with 45 additions and 3 deletions

22
init.el
View File

@ -574,7 +574,9 @@
db/org-clock-goto-first-open-checkbox db/org-clock-goto-first-open-checkbox
org-password-manager-get-password-by-id org-password-manager-get-password-by-id
db/org-bookmark-open db/org-bookmark-open
db/org-bookmark-store-link)) db/org-bookmark-store-link
db/org-lint-invalid-bookmark-link
db/org-lint-possible-bookmark-link))
;; This is to make the byte-compiler happy about setting some variables later on ;; This is to make the byte-compiler happy about setting some variables later on
;; that are defined in those packages. ;; that are defined in those packages.
@ -816,15 +818,29 @@
:init (setq org-cycle-include-plain-lists 'integrate)) :init (setq org-cycle-include-plain-lists 'integrate))
(use-package org-lint (use-package org-lint
:autoload (org-lint-checker-name) :autoload (org-lint-checker-name
org-lint-add-checker)
:config (progn :config (progn
;; Yes, this is ugly, but I would like to get rid of checking for ;; Yes, this is ugly, but I would like to get rid of checking for
;; obsolete percentage encoding in URLs without loosing the other ;; obsolete percentage encoding in URLs without loosing the other
;; checkers. ;; checkers.
(setq org-lint--checkers (cl-remove-if #'(lambda (c) (setq org-lint--checkers (cl-remove-if #'(lambda (c)
(eq (org-lint-checker-name c) (eq (org-lint-checker-name c)
'percent-encoding-link-escape)) 'percent-encoding-link-escape))
org-lint--checkers)))) org-lint--checkers))
(org-lint-add-checker 'link-to-bookmark
"Report links to non-existing bookmarks"
#'db/org-lint-invalid-bookmark-link
:categories '(link)
:trust 'low)
(org-lint-add-checker 'link-replacable-by-bookmark
"Report links that can be replaced by bookmarks"
#'db/org-lint-possible-bookmark-link
:categories '(link)
:trust 'low)))
;; Drag-and-Drop images into org-mode buffer ;; Drag-and-Drop images into org-mode buffer
(use-package org-download (use-package org-download

View File

@ -2242,6 +2242,32 @@ PARAMS may contain the following values:
(org-link-store-props :link (concat "bookmark:" bookmark) (org-link-store-props :link (concat "bookmark:" bookmark)
:description bookmark)))) :description bookmark))))
(defun db/org-lint-invalid-bookmark-link (ast)
"Org lint checker to verify bookmark links in AST point to known bookmarks."
(bookmark-maybe-load-default-file)
(org-element-map ast 'link
(lambda (link)
(let ((bmk (org-element-property :path link)))
(and (equal (org-element-property :type link) "bookmark")
(not (assoc-string bmk bookmark-alist))
(list (org-element-begin link)
(format "Unknown bookmark link \"%s\"" bmk)))))))
(defun db/org-lint-possible-bookmark-link (ast)
"Org lint checker to point out links in AST that could be replaced by bookmarks."
(bookmark-maybe-load-default-file)
(org-element-map ast 'link
(lambda (link)
(when-let* ((link-path (org-element-property :raw-link link))
(known-bookmark (-find #'(lambda (bmk)
(string= (bookmark-get-filename bmk)
link-path))
bookmark-alist)))
(list (org-element-begin link)
(format "Link to \"%s\" can be replaced by bookmark \"%s\""
link-path
(car known-bookmark)))))))
;;; End ;;; End