all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Akib Azmain Turja <akib@disroot.org>
Cc: Emacs Developer List <emacs-devel@gnu.org>
Subject: Re: [NonGNU ELPA] New package: Denote-Refs
Date: Mon, 19 Dec 2022 16:58:14 +0000	[thread overview]
Message-ID: <87ili75o0p.fsf@posteo.net> (raw)
In-Reply-To: <87a63jl5i2.fsf@disroot.org> (Akib Azmain Turja's message of "Mon, 19 Dec 2022 22:31:33 +0600")

[-- Attachment #1: Type: text/plain, Size: 982 bytes --]

Akib Azmain Turja <akib@disroot.org> writes:

> Denote-Refs shows links and backlinks inline in a Denote note buffer,
> just below the front matter of the note.
>
> Here is the patch:
>
> From f4145634df9eb45df7d526871c55326bfafb3785 Mon Sep 17 00:00:00 2001
> From: Akib Azmain Turja <akib@disroot.org>
> Date: Mon, 19 Dec 2022 22:22:46 +0600
> Subject: [PATCH] * elpa-packages (denote-refs): New package.
>
> ---
>  elpa-packages | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/elpa-packages b/elpa-packages
> index 9dde563..1d26a5a 100644
> --- a/elpa-packages
> +++ b/elpa-packages
> @@ -108,6 +108,8 @@
>    :ignored-files ("LICENSE" "test" "Cask" "Makefile")
>    :news "CHANGELOG.md")
>  
> + (denote-refs           :url "https://codeberg.org/akib/emacs-denote-refs")
> +
>   (devhelp               :url "https://codeberg.org/akib/emacs-devhelp")
>  
>   (diff-ansi		:url "https://codeberg.org/ideasman42/emacs-diff-ansi"
> -- 
> 2.37.1

Here are a few comments:


[-- Attachment #2: Type: text/plain, Size: 9370 bytes --]

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 <akib@disroot.org>
 ;; 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 "<!-- ... %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)
-         (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 "<!-- ... %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)
+	(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

  reply	other threads:[~2022-12-19 16:58 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-19 16:31 [NonGNU ELPA] New package: Denote-Refs Akib Azmain Turja
2022-12-19 16:58 ` Philip Kaludercic [this message]
2022-12-19 18:00   ` Akib Azmain Turja
2022-12-19 19:19     ` Philip Kaludercic
2022-12-19 20:20       ` [External] : " Drew Adams
2022-12-19 21:08         ` Philip Kaludercic
2022-12-19 21:51           ` Drew Adams
2022-12-20 16:48             ` Philip Kaludercic
2022-12-20 17:39               ` Drew Adams
2022-12-21  0:05                 ` Philip Kaludercic
2022-12-21  5:22                   ` Drew Adams
2022-12-21  9:07                     ` Philip Kaludercic
2022-12-21 15:50                       ` Drew Adams
2022-12-20 15:09       ` Akib Azmain Turja
2022-12-20 16:55         ` Philip Kaludercic
2022-12-20 20:17           ` Akib Azmain Turja
2022-12-20 22:02             ` Stefan Monnier
2022-12-21 16:03               ` Akib Azmain Turja

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ili75o0p.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=akib@disroot.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.