Bug (emacs): lexical binding breaks tag hooks

classic Classic list List threaded Threaded
3 messages Options
Matt Lundin Matt Lundin
Reply | Threaded
Open this post in threaded view
|

Bug (emacs): lexical binding breaks tag hooks

With the upgrade to notmuch 0.32, functions added to
'notmuch-after-tag-hook' or 'notmuch-before-tag-hook' no longer have
access to the 'tag-changes' or 'query' variables advertised in the
docstrings of both hooks. If I try to access either variable in a hook
function, emacs throws an error. I believe this results from the
addition of lexical binding in commit fc4cda07a9af.

Here are steps to reproduce:

1. Create a hook that accesses either the 'query' or 'tag-changes'
   variable:

(defun my-notmuch-tag-change-test ()
  tag-changes)

(add-hook 'notmuch-before-tag-hook #'my-notmuch-tag-change-test)

2. Attempt to change a tag in a message. This results in the following
   backtrace:

--8<---------------cut here---------------start------------->8---
Debugger entered--Lisp error: (void-variable tag-changes)
  my-notmuch-tag-change-test()
  run-hooks(notmuch-before-tag-hook)
  (progn (run-hooks 'notmuch-before-tag-hook) (if (<= (length query) notmuch-tag-argument-limit) (apply 'notmuch-call-notmuch-process "tag" (append tag-changes (list "--" query))) (let ((batch-op (concat (mapconcat #'notmuch-hex-encode tag-changes " ") " -- " query))) (notmuch-call-notmuch-process :stdin-string batch-op "tag" "--batch"))) (run-hooks 'notmuch-after-tag-hook))
  (if tag-changes (progn (run-hooks 'notmuch-before-tag-hook) (if (<= (length query) notmuch-tag-argument-limit) (apply 'notmuch-call-notmuch-process "tag" (append tag-changes (list "--" query))) (let ((batch-op (concat (mapconcat ... tag-changes " ") " -- " query))) (notmuch-call-notmuch-process :stdin-string batch-op "tag" "--batch"))) (run-hooks 'notmuch-after-tag-hook)))
  notmuch-tag("id:[hidden email]" ("+test"))
  notmuch-show-tag(("+test"))
  notmuch-show-add-tag(("+test"))
  funcall-interactively(notmuch-show-add-tag ("+test"))
  call-interactively(notmuch-show-add-tag nil nil)
  command-execute(notmuch-show-add-tag)
--8<---------------cut here---------------end--------------->8---

Best,

Matt
_______________________________________________
notmuch mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
Kyle Meyer Kyle Meyer
Reply | Threaded
Open this post in threaded view
|

[PATCH] emacs: restore tag-changes and query bindings for tag hooks

Matt Lundin writes:

> With the upgrade to notmuch 0.32, functions added to
> 'notmuch-after-tag-hook' or 'notmuch-before-tag-hook' no longer have
> access to the 'tag-changes' or 'query' variables advertised in the
> docstrings of both hooks. If I try to access either variable in a hook
> function, emacs throws an error. I believe this results from the
> addition of lexical binding in commit fc4cda07a9af.

The approach in the patch below is similar to some changes I've seen
Stefan Monnier do for lexical binding conversions.

-- >8 --
Subject: [PATCH] emacs: restore tag-changes and query bindings for tag hooks

notmuch-before-tag-hook and notmuch-after-tag-hook are supposed to
have access to two dynamic variables, tag-changes and query, but these
were lost with the switch to lexical binding in fc4cda07 (emacs: use
lexical-bindings in all libraries, 2021-01-13).

Add a variant of Emacs's dlet (not available until Emacs 28) and use
it in notmuch-tag to expose tag-changes and query to the hooks.
---
 emacs/notmuch-compat.el | 12 ++++++++++++
 emacs/notmuch-tag.el    |  8 ++++++--
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch-compat.el b/emacs/notmuch-compat.el
index ad134dfe..179bf59c 100644
--- a/emacs/notmuch-compat.el
+++ b/emacs/notmuch-compat.el
@@ -41,6 +41,18 @@ (defun notmuch-message--fold-long-headers ()
 (unless (fboundp 'message--fold-long-headers)
   (add-hook 'message-header-hook 'notmuch-message--fold-long-headers))
 
+;; `dlet' isn't available until Emacs 28.1.  Below is a copy, with the
+;; addition of `with-no-warnings'.
+(defmacro notmuch-dlet (binders &rest body)
+  "Like `let*' but using dynamic scoping."
+  (declare (indent 1) (debug let))
+  `(let (_)
+     (with-no-warnings  ; Quiet "lacks a prefix" warning.
+       ,@(mapcar (lambda (binder)
+   `(defvar ,(if (consp binder) (car binder) binder)))
+ binders))
+     (let* ,binders ,@body)))
+
 (provide 'notmuch-compat)
 
 ;;; notmuch-compat.el ends here
diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el
index f348d4ae..ebccb5a0 100644
--- a/emacs/notmuch-tag.el
+++ b/emacs/notmuch-tag.el
@@ -486,7 +486,9 @@ (defun notmuch-tag (query tag-changes)
   (unless query
     (error "Nothing to tag!"))
   (when tag-changes
-    (run-hooks 'notmuch-before-tag-hook)
+    (notmuch-dlet ((tag-changes tag-changes)
+   (query query))
+      (run-hooks 'notmuch-before-tag-hook))
     (if (<= (length query) notmuch-tag-argument-limit)
  (apply 'notmuch-call-notmuch-process "tag"
        (append tag-changes (list "--" query)))
@@ -494,7 +496,9 @@ (defun notmuch-tag (query tag-changes)
       (let ((batch-op (concat (mapconcat #'notmuch-hex-encode tag-changes " ")
       " -- " query)))
  (notmuch-call-notmuch-process :stdin-string batch-op "tag" "--batch")))
-    (run-hooks 'notmuch-after-tag-hook)))
+    (notmuch-dlet ((tag-changes tag-changes)
+   (query query))
+      (run-hooks 'notmuch-after-tag-hook))))
 
 (defun notmuch-tag-change-list (tags &optional reverse)
   "Convert TAGS into a list of tag changes.

base-commit: 63413a5563450bdedee4c077f2f998578e75083a
--
2.31.1
_______________________________________________
notmuch mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
David Bremner-2 David Bremner-2
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] emacs: restore tag-changes and query bindings for tag hooks

Kyle Meyer <[hidden email]> writes:

> +;; `dlet' isn't available until Emacs 28.1.  Below is a copy, with the
> +;; addition of `with-no-warnings'.
> +(defmacro notmuch-dlet (binders &rest body)
> +  "Like `let*' but using dynamic scoping."
> +  (declare (indent 1) (debug let))
> +  `(let (_)
> +     (with-no-warnings  ; Quiet "lacks a prefix" warning.
> +       ,@(mapcar (lambda (binder)
> +   `(defvar ,(if (consp binder) (car binder) binder)))
> + binders))
> +     (let* ,binders ,@body)))
> +
>  (provide 'notmuch-compat)

What do you think Jonas, is this a reasonable approach?

d
_______________________________________________
notmuch mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]