Authentication-Results: mail-b.sr.ht; dkim=none Received: from outbound.soverin.net (outbound.soverin.net [185.233.34.20]) by mail-b.sr.ht (Postfix) with ESMTPS id 2600611EE06 for <~protesilaos/denote@lists.sr.ht>; Wed, 26 Oct 2022 20:01:55 +0000 (UTC) Received: from smtp.soverin.net (c04smtp-lb01.int.sover.in [10.10.4.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by outbound.soverin.net (Postfix) with ESMTPS id 4MyKTG17S0z6r; Wed, 26 Oct 2022 20:01:54 +0000 (UTC) Received: from smtp.soverin.net (smtp.soverin.net [10.10.4.100]) by soverin.net (Postfix) with ESMTPSA id 4MyKTF5Sz5zNR; Wed, 26 Oct 2022 20:01:53 +0000 (UTC) X-Soverin-Authenticated: true From: Noboru Ota To: ~protesilaos/denote@lists.sr.ht Cc: Protesilaos Stavrou Subject: Re: [PATCH] Display context of identifier in backlinks buffer with xref In-Reply-To: <87o7tzl1zv.fsf@protesilaos.com> Date: Wed, 26 Oct 2022 22:01:51 +0200 Message-ID: <86ilk6uzf4.fsf@nobiot.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Made patches to make the current version compatible with 27.2. I have tested both cases where user option 'denote-backlilnks-show-context' is on and off for both 27.2 and 28.1. I have noted this in the commit text: TODO: It is likely that these two functions can be made into one to be comptabile for both Emacs versions. Further work is requried to refactor them. Thanks. nobiot --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Fix-denote-retrieve-xrefs-for-Emacs-version-27.2.patch Content-Description: Patch 1 From 6721b7a437794ff4d5f4237432d6f799986c1c7e Mon Sep 17 00:00:00 2001 From: Noboru Ota Date: Wed, 26 Oct 2022 21:28:01 +0200 Subject: [PATCH 1/2] Fix denote--retrieve-xrefs for Emacs version 27.2 'denote--retrieve-xrefs' uses 'xref--alistify', which has changed its signature between 27.2 and 28.1. This patch makes the former compatible with both Emacs versions. --- denote.el | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/denote.el b/denote.el index 89d82cd..663c015 100644 --- a/denote.el +++ b/denote.el @@ -1146,10 +1146,17 @@ Run `denote-desluggify' on title if the extraction is sucessful." (defun denote--retrieve-xrefs (identifier) "Return xrefs of IDENTIFIER in variable `denote-directory'. The xrefs are returned as an alist." - (xref--alistify - (xref-matches-in-files identifier (denote-directory-text-only-files)) - (lambda (x) - (xref-location-group (xref-item-location x))))) + (cond ((>= emacs-major-version 28) + (xref--alistify + (xref-matches-in-files identifier (denote-directory-text-only-files)) + (lambda (x) + (xref-location-group (xref-item-location x))))) + (t ;; This must only be 27.2 as Denote's prerequisite is 27.2 + (xref--alistify + (xref-matches-in-files identifier (denote-directory-text-only-files)) + (lambda (x) + (xref-location-group (xref-item-location x))) + nil)))) (defun denote--retrieve-files-in-xrefs (xrefs-alist) "Return sorted file names sans directory from XREFS. -- 2.34.1 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-Add-denote-xref-insert-xrefs-27-for-Emacs-version-27.patch Content-Description: Patch 2 From 8fcead5c27ec28800d9590bcfbe949e368771567 Mon Sep 17 00:00:00 2001 From: Noboru Ota Date: Wed, 26 Oct 2022 21:57:56 +0200 Subject: [PATCH 2/2] Add denote-xref--insert-xrefs-27 for Emacs version 27.2 The implementation detail of 'xref--insert-xrefs' has changed between Emacs 27.2 and 28, which made them incompatible with each other. As 'denote-xref--insert-xrefs' is based on that function, a new function has been created. TODO: It is likely that these two functions can be made into one to be comptabile for both Emacs versions. Further work is requried to refactor them. --- denote.el | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/denote.el b/denote.el index 663c015..3768435 100644 --- a/denote.el +++ b/denote.el @@ -356,7 +356,7 @@ command." :link '(info-link "(denote) The denote-templates option") :group 'denote) -(defcustom denote-backlilnks-show-context nil +(defcustom denote-backlinks-show-context nil "When non-nil, the backlinks buffer shows context of identifiers. The context is the one line an identifier is found. This option also enables the backlink buffer to show multiple occurrences of an @@ -2644,8 +2644,11 @@ Use optional TITLE for a prettier heading." (l (length heading))) (insert (format "%s\n%s\n\n" heading (make-string l ?-)))) ;;; We could have a user option to use the current backlink buffer - (if denote-backlilnks-show-context - (denote-xref--insert-xrefs xrefs-alist) + (if denote-backlinks-show-context + (cond ((>= emacs-major-version 28) + (denote-xref--insert-xrefs xrefs-alist)) + (t ;; This can only be Emacs 27.2 as Denote's prequiresite is 27.2 + (denote-xref--insert-xrefs-27 xrefs-alist))) (mapc (lambda (x) (insert (denote-get-file-name-relative-to-denote-directory (car x))) (make-button (line-beginning-position) (line-end-position) :type 'denote-link-backlink-button) @@ -2659,6 +2662,41 @@ Use optional TITLE for a prettier heading." (denote-link--prepare-backlinks id xrefs-alist title))))) (denote-link--display-buffer buf))) +(defun denote-xref--insert-xrefs-27 (xref-alist) + "Insert XREF-ALIST in the current-buffer. +XREF-ALIST is of the form ((GROUP . (XREF ...)) ...), where +GROUP is a string for decoration purposes and XREF is an +`xref-item' object." + (require 'compile) ; For the compilation faces. + (cl-loop for ((group . xrefs) . more1) on xref-alist + for max-line-width = + (cl-loop for xref in xrefs + maximize (let ((line (xref-location-line + (oref xref location)))) + (length (and line (format "%d" line))))) + for line-format = (and max-line-width + (format "%%%dd: " max-line-width)) + do + (xref--insert-propertized '(face xref-file-header xref-group t) + (denote-get-file-name-relative-to-denote-directory group) "\n") + (cl-loop for (xref . more2) on xrefs do + (with-slots (summary location) xref + (let* ((line (xref-location-line location)) + (prefix + (if line + (propertize (format line-format line) + 'face 'xref-line-number) + " "))) + (xref--insert-propertized + (list 'xref-item xref + 'mouse-face 'highlight + 'keymap xref--button-map + 'help-echo + (concat "mouse-2: display in another window, " + "RET or mouse-1: follow reference")) + prefix summary))) + (insert "\n\n")))) + (defun denote-xref--insert-xrefs (xref-alist) "Insert XREF-ALIST in the current buffer. XREF-ALIST is of the form ((GROUP . (XREF ...)) ...), where GROUP -- 2.34.1 --=-=-=--