diff -u /tmp/denote-refs.el.1 /tmp/denote-refs.el --- /tmp/denote-refs.el.1 2022-12-19 19:38:55.000000000 +0100 +++ /tmp/denote-refs.el 2022-12-19 17:57:48.712068577 +0100 @@ -5,8 +5,8 @@ ;; Author: Akib Azmain Turja ;; Created: 2022-12-18 ;; Version: 0.1 -;; Package-Requires: ((emacs "28.1")) -;; Keywords: hypermedia outlines files +;; Package-Requires: ((emacs "28.1") (denote "1.2.0")) +;; Keywords: hypermedia, outlines, files ;; URL: https://codeberg.org/akib/emacs-denote-refs ;; This file is not part of GNU Emacs. @@ -36,12 +36,11 @@ (require 'denote) (require 'subr-x) -(defgroup denote-refs nil +(defgroup denote-refs '() "Show links and backlinks in Denote notes." :group 'denote - :group 'hypermedia - :group 'outlines - :group 'files + ;; You don't need to add multiple groups, we only need it to appear + ;; once in the customize hierarchy :link '(url-link "https://codeberg.org/akib/emacs-denote-refs") :prefix "denote-refs-") @@ -62,7 +61,8 @@ Available sections are `links' and `backlinks', which shows the list of linked file and the list of backlinks respectively." - :type '(set (const :tag "Links" links) + :type '(set :greedy t + (const :tag "Links" links) (const :tag "Backlinks" backlinks))) (defvar denote-refs--links 'not-ready @@ -83,65 +83,67 @@ (defun denote-refs--render (section) "Render SECTION." (let ((refs (pcase section - ('links denote-refs--links) - ('backlinks denote-refs--backlinks)))) - (pcase major-mode - ;; There's no comment syntax in `text-mode', so just follow - ;; `org-mode'. - ((or 'org-mode 'text-mode) - ;; Insert references count. - (insert (if (eq refs 'not-ready) - (format "# ... %s\n" (if (eq section 'links) - "links" - "backlinks")) - (format "# %i %s%s\n" (length refs) - (if (eq section 'links) - "link" - "backlink") - (pcase (length refs) - (0 "") - (1 ":") - (_ "s:"))))) - ;; Insert reference list. - (when (listp refs) - (dolist (ref refs) - (insert "# ") - (insert-button (car ref) - 'help-echo (cdr ref) - 'face 'denote-faces-link - 'action (lambda (_) - (funcall denote-link-button-action - (cdr ref)))) - (insert ?\n)))) - ('markdown-mode - ;; Insert references count. - (insert (if (eq refs 'not-ready) - (format "\n" (if (eq section 'links) - "links" - "backlinks")) - (format "") - (1 ":") - (_ "s:"))))) - ;; Insert reference list. - (when (listp refs) - (while refs - (let ((ref (pop refs))) - (insert " ") - (insert-button - (car ref) - 'help-echo (cdr ref) - 'face 'denote-faces-link - 'action (lambda (_) - (funcall denote-link-button-action - (cdr ref)))) - (unless refs - (insert " -->")) - (insert ?\n)))))))) + ('links denote-refs--links) + ('backlinks denote-refs--backlinks)))) + (cond + ;; There's no comment syntax in `text-mode', so just follow + ;; `org-mode'. + ((derived-mode-p 'org-mode 'text-mode) + ;; Insert references count. + (insert (if (eq refs 'not-ready) + (format "# ... %s\n" (if (eq section 'links) + "links" + "backlinks")) + (format "# %i %s%s\n" (length refs) + (if (eq section 'links) + "link" + "backlink") + (pcase (length refs) + (0 "") + (1 ":") + (_ "s:"))))) + ;; Insert reference list. + (when (listp refs) + (dolist (ref refs) + (insert "# ") + (insert-button (car ref) + 'help-echo (cdr ref) + 'face 'denote-faces-link + 'action (lambda (_) + (funcall denote-link-button-action + (cdr ref)))) + ;; Could (newline) work here? + (insert ?\n)))) + ((derived-mode-p 'markdown-mode) + ;; Insert references count. + (insert (if (eq refs 'not-ready) + (format "\n" (if (eq section 'links) + "links" + "backlinks")) + (format "") + (1 ":") + (_ "s:"))))) + ;; Insert reference list. + (when (listp refs) + (while refs + (let ((ref (pop refs))) + (insert " ") + (insert-button + (car ref) + 'help-echo (cdr ref) + 'face 'denote-faces-link + ;; This way we avoid creating a closure that ignores an + ;; argument. + 'action #'denote-link-button-action + 'button-data (cdr ref)) + (unless refs + (insert " -->")) + (insert ?\n)))))))) (defun denote-refs--goto-end-of-front-matter () "Go to the end of front matter of the note." @@ -159,15 +161,14 @@ (save-excursion (denote-refs--goto-end-of-front-matter) (when (get-text-property (point) 'denote-refs--sections) - (let ((end (or (next-single-property-change - (point) 'denote-refs--sections) - (point-max)))) - (when (< end (point-max)) - (setq end (1+ end))) - (let ((mod-flag (buffer-modified-p)) - (inhibit-read-only t)) - (delete-region (point) end) - (restore-buffer-modified-p mod-flag))))))) + (let ((end (or (next-single-property-change + (point) 'denote-refs--sections) + (point-max))) + (inhibit-read-only t)) + (when (< end (point-max)) + (setq end (1+ end))) + (with-silent-modifications + (delete-region (point) end))))))) (defun denote-refs--show () "Show references." @@ -177,19 +178,18 @@ (denote-refs--remove) (save-excursion (denote-refs--goto-end-of-front-matter) - (let ((begin (point)) - (mod-flag (buffer-modified-p)) - (inhibit-read-only t)) - (dolist (section denote-refs-sections) - (pcase-exhaustive section - ('links - (denote-refs--render 'links)) - ('backlinks - (denote-refs--render 'backlinks)))) - (put-text-property begin (point) 'read-only t) - (put-text-property begin (point) 'denote-refs--sections t) - (insert ?\n) - (restore-buffer-modified-p mod-flag))))) + (with-silent-modifications + (let ((begin (point)) + (inhibit-read-only t)) + (dolist (section denote-refs-sections) + (pcase-exhaustive section + ('links + (denote-refs--render 'links)) + ('backlinks + (denote-refs--render 'backlinks)))) + (put-text-property begin (point) 'read-only t) + (put-text-property begin (point) 'denote-refs--sections t) + (insert ?\n)))))) (defun denote-refs--make-path-relative (path) "Return a cons of relative and absolute version of PATH. @@ -203,22 +203,22 @@ (pcase-exhaustive section ('links (setq denote-refs--links - (when (and (buffer-file-name) - (file-exists-p (buffer-file-name))) - (mapcar #'denote-refs--make-path-relative - (denote-link--expand-identifiers - (denote--link-in-context-regexp - (denote-filetype-heuristics - (buffer-file-name)))))))) + (and (buffer-file-name) + (file-exists-p (buffer-file-name)) + (mapcar #'denote-refs--make-path-relative + (denote-link--expand-identifiers + (denote--link-in-context-regexp + (denote-filetype-heuristics + (buffer-file-name)))))))) ('backlinks (setq denote-refs--backlinks - (when (and (buffer-file-name) - (file-exists-p (buffer-file-name))) - (mapcar #'denote-refs--make-path-relative - (delete (buffer-file-name) - (denote--retrieve-files-in-xrefs - (denote-retrieve-filename-identifier - (buffer-file-name))))))))))) + (and (buffer-file-name) + (file-exists-p (buffer-file-name)) + (mapcar #'denote-refs--make-path-relative + (delete (buffer-file-name) + (denote--retrieve-files-in-xrefs + (denote-retrieve-filename-identifier + (buffer-file-name))))))))))) (defun denote-refs-update () "Update Denote references shown." Diff finished. Mon Dec 19 17:57:57 2022