GNU bug report logs - #57179
29.0.50: 'wrong-number-of-arguments' for function of two arguments called from 'window-scroll-functions'

Previous Next

Package: emacs;

Reported by: Andrea Greselin <greselin.andrea <at> gmail.com>

Date: Sat, 13 Aug 2022 16:19:01 UTC

Severity: normal

Found in version 29.0.50

Done: Alan Mackenzie <acm <at> muc.de>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 57179 in the body.
You can then email your comments to 57179 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sat, 13 Aug 2022 16:19:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Andrea Greselin <greselin.andrea <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sat, 13 Aug 2022 16:19:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Andrea Greselin <greselin.andrea <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.0.50: 'wrong-number-of-arguments' for function of two arguments
 called from 'window-scroll-functions'
Date: Sat, 13 Aug 2022 18:17:54 +0200
[Message part 1 (text/plain, inline)]
Hi all,
I have this function (the part at issue here should be the first
line):

    (defun my-recenter-after-jump (window _window-start-after)
      "Recenter the point after a non-scroll command brings it out of view.
    This function is meant to be called from the hook
    ‘window-scroll-functions’."
      (with-selected-window window
        (unless (or (> 0.001 (float-time (time-subtract (current-time)
buffer-display-time))) ; Don’t run this function after a change of buffer.
Non-nil here means the selected window displayed the current buffer less
than a thousandth of a second ago, so it was most likely a change of buffer
that triggered ‘window-scroll-functions’, not a scroll. See also
https://emacs.stackexchange.com/questions/63558/how-to-distinguish-a-scroll-from-a-change-of-buffer-in-a-function-called-from-w
                    ;; Don’t recenter for these
                    ;;   - major modes
                    (memq major-mode '(help-mode ;
           ‘my-recenter-after-jump’ breaks scrolling with SPC/<backspace>
in Help mode and Info mode because they don’t obey
‘scroll-preserve-screen-position’ and ‘scroll-error-top-bottom’.
                                       Info-mode))
                    ;;   - minor modes
                    isearch-mode ;
           For Isearch, change the value of ‘scroll-conservatively’ and
‘hscroll-step’ in ‘isearch-update-post-hook’ and ‘isearch-mode-end-hook’.
Doing so allows recentering the point even after a purely horizontal scroll.
                    (bound-and-true-p view-mode) ;
           ‘my-recenter-after-jump’ breaks scrolling with SPC/<backspace>
in View mode because View mode doesn’t obey
‘scroll-preserve-screen-position’ and ‘scroll-error-top-bottom’.
                    (bound-and-true-p follow-mode)
                    ;;   - commands
                    (and (symbolp last-command) (get last-command
'scroll-command)) ; Don’t check the value of the ‘scroll-command’ property
for anonymous commands (it can’t be t anyway).
                    (memq last-command '(recenter-top-bottom
                                         query-replace query-replace-regexp
;         Query replacement is taken care of by the ‘recentering-on-jump’
advice to ‘perform-replace’, which allows recentering the point after a
purely horizontal scroll.
                                         previous-line next-line ;
           Don’t let these commands trigger ‘my-recenter-after-jump’. It
happens when Emacs has a moment of lag during which the point slips beyond
the scroll margin.
                                         previous-logical-line
next-logical-line)))
          (let* ((bottom-line-before (line-number-at-pos (window-end)))
                 (bottom-line-after (line-number-at-pos (window-end nil
'update))) ; NB: ‘window-start’ doesn’t take the ‘update’ argument.
                 (vertical-displacement (- bottom-line-after
bottom-line-before)))
            (unless (> 2 (abs vertical-displacement)) ; The purpose of
having the first ‘unless’ separate from this one is to call
‘line-number-at-pos’ only when it’s needed, because it can be slow. See (2
links)
https://emacs.stackexchange.com/questions/51648/how-to-detect-the-number-of-lines-scrolled-from-scroll-up-down/51664#51664,
https://emacs.stackexchange.com/questions/3821/a-faster-method-to-obtain-line-number-at-pos-in-large-buffers
              (recenter)
              (when truncate-lines
                (my-horizontal-recenter))))))) ; REVIEW: Isn’t there a
built-in function for recentering the point horizontally?

which I call from `window-scroll-functions` like this

    (add-hook 'window-scroll-functions #'my-recenter-after-jump)


It's worked until I upgraded from v. 28.1 (from Fedora’s repos) to
29.0.50 (commit 6de88b6b02). Now I get

    Error in window-scroll-functions (my-recenter-after-jump):
(wrong-number-of-arguments (lambda (window _window-start-after) "Recenter
the point after a non-scroll command brings it out of view.
    This function is meant to be called from the hook
    ‘window-scroll-functions’." (let ((save-selected-window--state
(internal--before-with-selected-window window))) (save-current-buffer
(unwind-protect (progn (select-window (car save-selected-window--state)
'norecord) (if (or (> 0.001 (float-time (time-subtract (current-time)
buffer-display-time))) (memq major-mode '(help-mode Info-mode))
isearch-mode (and (boundp 'view-mode) view-mode) (and (boundp 'follow-mode)
follow-mode) (and (symbolp last-command) (get last-command
'scroll-command)) (memq last-command '(recenter-top-bottom query-replace
query-replace-regexp previous-line next-line previous-logical-line
next-logical-line))) nil (let* ((bottom-line-before (line-number-at-pos
(window-end))) (bottom-line-after (line-number-at-pos (window-end nil
'update))) (vertical-displacement (- bottom-line-after
bottom-line-before))) (if (> 2 (abs vertical-displacement)) nil (recenter)
(if truncate-lines (progn (my-horizontal-recenter)))))))
(internal--after-with-selected-window save-selected-window--state))))) 0)

All the best,
Andrea
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sat, 13 Aug 2022 17:31:02 GMT) Full text and rfc822 format available.

Message #8 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Andrea Greselin <greselin.andrea <at> gmail.com>
Cc: 57179 <at> debbugs.gnu.org
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Sat, 13 Aug 2022 20:30:05 +0300
> From: Andrea Greselin <greselin.andrea <at> gmail.com>
> Date: Sat, 13 Aug 2022 18:17:54 +0200
> 
> It's worked until I upgraded from v. 28.1 (from Fedora’s repos) to
> 29.0.50 (commit 6de88b6b02). Now I get
> 
>     Error in window-scroll-functions (my-recenter-after-jump):
> (wrong-number-of-arguments (lambda (window _window-start-after) "Recenter
> the point after a non-scroll command brings it out of view.
>     This function is meant to be called from the hook
>     ‘window-scroll-functions’." (let ((save-selected-window--state
> (internal--before-with-selected-window window))) (save-current-buffer
> (unwind-protect (progn (select-window (car save-selected-window--state)

The backtrace seems to show that a function that is called is
different from the one you show as your hook function.  So I think you
should look closer into how you set up window-scroll-functions.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sat, 13 Aug 2022 20:51:01 GMT) Full text and rfc822 format available.

Message #11 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Andrea Greselin <greselin.andrea <at> gmail.com>, 57179 <at> debbugs.gnu.org
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Sat, 13 Aug 2022 22:50:47 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

> The backtrace seems to show that a function that is called is
> different from the one you show as your hook function.  So I think you
> should look closer into how you set up window-scroll-functions.

I too have seen this problem just yesterday.  There must be something
going on, it is unlikely that this is a coincidence.

In the backtrace I saw that the function had been called by name with
zero arguments.  But on top of the backtrace the symbol-function had
been shown (a closure).  I also wondered about that.

I verified that the function was only accessible via
`window-scroll-functions'.  My code did not use the function in any
other place.

Some entries below in the backtrace was `redisplay' - so... somehow
`redisplay' is sometimes calling functions in `window-scroll-functions'
with zero arguments.  Maybe the buffer local binding of
`window-scroll-functions'.

Please tell whether this information suffices and you already have some
idea or if I should try to create a recipe.  I guess this must be
related to a recent change from within the last few days.

Michael.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sat, 13 Aug 2022 23:09:02 GMT) Full text and rfc822 format available.

Message #14 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Andrea Greselin <greselin.andrea <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 57179 <at> debbugs.gnu.org
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Sun, 14 Aug 2022 01:07:32 +0200
[Message part 1 (text/plain, inline)]
On Sat, 13 Aug 2022 at 19:30, Eli Zaretskii <eliz <at> gnu.org> wrote:

>
> > The backtrace seems to show that a function that is called is
> > different from the one you show as your hook function.  So I think you
> > should look closer into how you set up window-scroll-functions.
>

‘my-recenter-after-jump’ is the only thing I add to
‘window-scroll-functions’.

Andrea
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sun, 14 Aug 2022 00:20:01 GMT) Full text and rfc822 format available.

Message #17 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Alan Mackenzie <acm <at> muc.de>, Andrea Greselin <greselin.andrea <at> gmail.com>,
 57179 <at> debbugs.gnu.org
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Sun, 14 Aug 2022 02:19:31 +0200
[Message part 1 (text/plain, inline)]
Michael Heerdegen <michael_heerdegen <at> web.de> writes:

> Please tell whether this information suffices and you already have some
> idea or if I should try to create a recipe.  I guess this must be
> related to a recent change from within the last few days.

Reverting a part of

48215c41d1 New debugging facility: backtraces from errors in Lisp called
from redisplay (Alan Mackenzie <acm <at> muc.de> 2022-08-11)

like this:

[0001-Test-for-57179.patch (text/x-diff, inline)]
From ffb7c3d558b599bcc84f1bf4fb4388569892d927 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen <at> web.de>
Date: Sun, 14 Aug 2022 02:14:58 +0200
Subject: [PATCH] Test for #57179

---
 src/xdisp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/xdisp.c b/src/xdisp.c
index 719b131baa..47eaddabce 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -18221,8 +18221,8 @@ run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
     {
       specpdl_ref count = SPECPDL_INDEX ();
       specbind (Qinhibit_quit, Qt);
-      safe_run_hooks_2
-	(Qwindow_scroll_functions, window, make_fixnum (CHARPOS (startp)));
+      run_hook_with_args_2 (Qwindow_scroll_functions, window,
+			    make_fixnum (CHARPOS (startp)));
       unbind_to (count, Qnil);
       SET_TEXT_POS_FROM_MARKER (startp, w->start);
       /* In case the hook functions switch buffers.  */
--
2.30.2

[Message part 3 (text/plain, inline)]
seems to fix my incarnation of this problem.  Can it be that?  CC'ing
Alan (the author).

TIA,

Michael.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sun, 14 Aug 2022 07:19:02 GMT) Full text and rfc822 format available.

Message #20 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Michael Heerdegen <michael_heerdegen <at> web.de>
Cc: acm <at> muc.de, greselin.andrea <at> gmail.com, 57179 <at> debbugs.gnu.org
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Sun, 14 Aug 2022 10:18:06 +0300
> From: Michael Heerdegen <michael_heerdegen <at> web.de>
> Cc: Andrea Greselin <greselin.andrea <at> gmail.com>,  57179 <at> debbugs.gnu.org,
>  Alan Mackenzie <acm <at> muc.de>
> Date: Sun, 14 Aug 2022 02:19:31 +0200
> 
> Michael Heerdegen <michael_heerdegen <at> web.de> writes:
> 
> > Please tell whether this information suffices and you already have some
> > idea or if I should try to create a recipe.  I guess this must be
> > related to a recent change from within the last few days.
> 
> Reverting a part of
> 
> 48215c41d1 New debugging facility: backtraces from errors in Lisp called
> from redisplay (Alan Mackenzie <acm <at> muc.de> 2022-08-11)

Indeed, the new function safe_run_hooks_2 has a bug.  In a build with
enable-checking I get an assertion violation in safe_run_hook_funcall,
because nargs is not 2 but 4.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sun, 14 Aug 2022 13:23:02 GMT) Full text and rfc822 format available.

Message #23 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Michael Heerdegen <michael_heerdegen <at> web.de>
Cc: acm <at> muc.de, Eli Zaretskii <eliz <at> gnu.org>, 57179 <at> debbugs.gnu.org,
 Andrea Greselin <greselin.andrea <at> gmail.com>
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Sun, 14 Aug 2022 13:22:36 +0000
Hello, Michael and Andrea.

On Sun, Aug 14, 2022 at 02:19:31 +0200, Michael Heerdegen wrote:
> Michael Heerdegen <michael_heerdegen <at> web.de> writes:

> > Please tell whether this information suffices and you already have some
> > idea or if I should try to create a recipe.  I guess this must be
> > related to a recent change from within the last few days.

> Reverting a part of

> 48215c41d1 New debugging facility: backtraces from errors in Lisp called
> from redisplay (Alan Mackenzie <acm <at> muc.de> 2022-08-11)

> like this:


> From ffb7c3d558b599bcc84f1bf4fb4388569892d927 Mon Sep 17 00:00:00 2001
> From: Michael Heerdegen <michael_heerdegen <at> web.de>
> Date: Sun, 14 Aug 2022 02:14:58 +0200
> Subject: [PATCH] Test for #57179
> 
> ---
>  src/xdisp.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/xdisp.c b/src/xdisp.c
> index 719b131baa..47eaddabce 100644
> --- a/src/xdisp.c
> +++ b/src/xdisp.c
> @@ -18221,8 +18221,8 @@ run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
>      {
>        specpdl_ref count = SPECPDL_INDEX ();
>        specbind (Qinhibit_quit, Qt);
> -      safe_run_hooks_2
> -	(Qwindow_scroll_functions, window, make_fixnum (CHARPOS (startp)));
> +      run_hook_with_args_2 (Qwindow_scroll_functions, window,
> +			    make_fixnum (CHARPOS (startp)));
>        unbind_to (count, Qnil);
>        SET_TEXT_POS_FROM_MARKER (startp, w->start);
>        /* In case the hook functions switch buffers.  */
> --
> 2.30.2
> 


> seems to fix my incarnation of this problem.  Can it be that?  CC'ing
> Alan (the author).

Yes, this was indeed the problem.  Can I ask you please, instead of
applying your patch (above) to try out the following patch, which works
for me.

Thanks!



diff --git a/src/keyboard.c b/src/keyboard.c
index 8a2b7d58c4..1d7125a0a3 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -1832,8 +1832,16 @@ adjust_point_for_property (ptrdiff_t last_pt, bool modified)
 static Lisp_Object
 safe_run_hooks_1 (ptrdiff_t nargs, Lisp_Object *args)
 {
-  eassert (nargs == 2);
-  return call0 (args[1]);
+  eassert (nargs >= 2 && nargs <= 4);
+  switch (nargs)
+    {
+    case 2:
+      return call0 (args[1]);
+    case 3:
+      return call1 (args[1], args[2]);
+    default:
+      return call2 (args[1], args[2], args[3]);
+    }
 }
 
 /* Subroutine for safe_run_hooks: handle an error by clearing out the function
@@ -1878,11 +1886,27 @@ safe_run_hooks_error (Lisp_Object error, ptrdiff_t nargs, Lisp_Object *args)
 static Lisp_Object
 safe_run_hook_funcall (ptrdiff_t nargs, Lisp_Object *args)
 {
-  eassert (nargs == 2);
+  eassert (nargs >= 2 && nargs <= 4);
   /* Yes, run_hook_with_args works with args in the other order.  */
-  internal_condition_case_n (safe_run_hooks_1,
-			     2, ((Lisp_Object []) {args[1], args[0]}),
-			     Qt, safe_run_hooks_error);
+  switch (nargs)
+    {
+    case 2:
+      internal_condition_case_n (safe_run_hooks_1,
+				 2, ((Lisp_Object []) {args[1], args[0]}),
+				 Qt, safe_run_hooks_error);
+      break;
+    case 3:
+      internal_condition_case_n (safe_run_hooks_1,
+				 3, ((Lisp_Object []) {args[1], args[0], args[2]}),
+				 Qt, safe_run_hooks_error);
+      break;
+    default:
+      internal_condition_case_n (safe_run_hooks_1,
+				 4, ((Lisp_Object [])
+				     {args[1], args[0], args[2], args[3]}),
+				 Qt, safe_run_hooks_error);
+      break;
+    }
   return Qnil;
 }
 


> TIA,

> Michael.

-- 
Alan Mackenzie (Nuremberg, Germany).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Sun, 14 Aug 2022 23:02:01 GMT) Full text and rfc822 format available.

Message #26 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Alan Mackenzie <acm <at> muc.de>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 57179 <at> debbugs.gnu.org,
 Andrea Greselin <greselin.andrea <at> gmail.com>
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Mon, 15 Aug 2022 01:00:55 +0200
Alan Mackenzie <acm <at> muc.de> writes:

> Hello, Michael and Andrea.

> [patch...]

Thanks.  Yes, seems my case is fixed, and I didn't see any new problems
so far.

Michael.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57179; Package emacs. (Mon, 15 Aug 2022 09:44:01 GMT) Full text and rfc822 format available.

Message #29 received at 57179 <at> debbugs.gnu.org (full text, mbox):

From: Andrea Greselin <greselin.andrea <at> gmail.com>
To: Alan Mackenzie <acm <at> muc.de>
Cc: Michael Heerdegen <michael_heerdegen <at> web.de>, Eli Zaretskii <eliz <at> gnu.org>,
 57179 <at> debbugs.gnu.org
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Mon, 15 Aug 2022 11:42:47 +0200
[Message part 1 (text/plain, inline)]
On Sun, 14 Aug 2022 at 15:22, Alan Mackenzie <acm <at> muc.de> wrote:

> > Can I ask you please, instead of applying your patch (above) to try
> > out the following patch, which works for me.
>

It fixes my bug too.

Andrea
[Message part 2 (text/html, inline)]

Reply sent to Alan Mackenzie <acm <at> muc.de>:
You have taken responsibility. (Mon, 15 Aug 2022 12:25:01 GMT) Full text and rfc822 format available.

Notification sent to Andrea Greselin <greselin.andrea <at> gmail.com>:
bug acknowledged by developer. (Mon, 15 Aug 2022 12:25:02 GMT) Full text and rfc822 format available.

Message #34 received at 57179-done <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Michael Heerdegen <michael_heerdegen <at> web.de>,
 Andrea Greselin <greselin.andrea <at> gmail.com>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 57179-done <at> debbugs.gnu.org
Subject: Re: bug#57179: 29.0.50: 'wrong-number-of-arguments' for function of
 two arguments called from 'window-scroll-functions'
Date: Mon, 15 Aug 2022 12:24:31 +0000
Hello, Michael and Andrea.

Thanks for testing my patch.  I have just committed the patch to master,
and I am closing the bug with this post.

On Sat, Aug 13, 2022 at 22:50:47 +0200, Michael Heerdegen wrote:
> Eli Zaretskii <eliz <at> gnu.org> writes:

> > The backtrace seems to show that a function that is called is
> > different from the one you show as your hook function.  So I think you
> > should look closer into how you set up window-scroll-functions.

> I too have seen this problem just yesterday.  There must be something
> going on, it is unlikely that this is a coincidence.

> In the backtrace I saw that the function had been called by name with
> zero arguments.  But on top of the backtrace the symbol-function had
> been shown (a closure).  I also wondered about that.

> I verified that the function was only accessible via
> `window-scroll-functions'.  My code did not use the function in any
> other place.

> Some entries below in the backtrace was `redisplay' - so... somehow
> `redisplay' is sometimes calling functions in `window-scroll-functions'
> with zero arguments.  Maybe the buffer local binding of
> `window-scroll-functions'.

> Please tell whether this information suffices and you already have some
> idea or if I should try to create a recipe.  I guess this must be
> related to a recent change from within the last few days.

> Michael.

-- 
Alan Mackenzie (Nuremberg, Germany).




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Tue, 13 Sep 2022 11:24:08 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 219 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.