GNU bug report logs -
#57502
29.0.50; Issue with `or' clause of buffer-match-p
Previous Next
Reported by: Augusto Stoffel <arstoffel <at> gmail.com>
Date: Wed, 31 Aug 2022 12:03:02 UTC
Severity: normal
Found in version 29.0.50
Done: Philip Kaludercic <philipk <at> posteo.net>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
[Message part 1 (text/plain, inline)]
Philip Kaludercic <philipk <at> posteo.net> writes:
> Augusto Stoffel <arstoffel <at> gmail.com> writes:
>
>> This buffer-match-p condition does the expected job:
>>
>> (buffer-match-p '(or "\\*" (derived-mode . special-mode))
>> (current-buffer))
>>
>> But this presumably equivalent one gives a “(wrong-type-argument listp
>> special-mode)” error:
>>
>> (buffer-match-p '(or (and "\\*")
>> (derived-mode . special-mode))
>> (current-buffer))
>
> It seems to me that the issue is related to the `and' being wrapped by
> an `or', specifically because of a typo in the handling of `and':
>
> diff --git a/lisp/subr.el b/lisp/subr.el
> index 2ffc594997..0350c16ccf 100644
> --- a/lisp/subr.el
> +++ b/lisp/subr.el
> @@ -7014,8 +7014,8 @@ buffer-match-p
> (funcall match (cdr condition)))
> ((eq (car-safe condition) 'and)
> (catch 'fail
> - (dolist (c (cdr conditions))
> - (unless (funcall match c)
> + (dolist (c (cdr condition))
> + (unless (funcall match (list c))
> (throw 'fail nil)))
> t)))
> (throw 'match t)))))))
>
>
> As you have pointed out to me privately, it might make sense to rewrite
> the case distinction using pcase, to avoid simple mistakes like these.
That might look something like this:
[Message part 2 (text/plain, inline)]
diff --git a/lisp/subr.el b/lisp/subr.el
index 2ffc594997..db1dc25044 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6992,32 +6992,32 @@ buffer-match-p
(lambda (conditions)
(catch 'match
(dolist (condition conditions)
- (when (cond
- ((eq condition t))
- ((stringp condition)
- (string-match-p condition (buffer-name buffer)))
- ((functionp condition)
- (if (eq 1 (cdr (func-arity condition)))
- (funcall condition buffer)
- (funcall condition buffer arg)))
- ((eq (car-safe condition) 'major-mode)
- (eq
- (buffer-local-value 'major-mode buffer)
- (cdr condition)))
- ((eq (car-safe condition) 'derived-mode)
- (provided-mode-derived-p
- (buffer-local-value 'major-mode buffer)
- (cdr condition)))
- ((eq (car-safe condition) 'not)
- (not (funcall match (cdr condition))))
- ((eq (car-safe condition) 'or)
- (funcall match (cdr condition)))
- ((eq (car-safe condition) 'and)
- (catch 'fail
- (dolist (c (cdr conditions))
- (unless (funcall match c)
- (throw 'fail nil)))
- t)))
+ (when (pcase condition
+ ('t t)
+ ((pred stringp)
+ (string-match-p condition (buffer-name buffer)))
+ ((pred functionp)
+ (if (eq 1 (cdr (func-arity condition)))
+ (funcall condition buffer)
+ (funcall condition buffer arg)))
+ (`(major-mode . ,mode)
+ (eq
+ (buffer-local-value 'major-mode buffer)
+ mode))
+ (`(derived-mode . ,mode)
+ (provided-mode-derived-p
+ (buffer-local-value 'major-mode buffer)
+ mode))
+ (`(not . cond)
+ (not (funcall match cond)))
+ (`(or . ,args)
+ (funcall match args))
+ (`(and . ,args)
+ (catch 'fail
+ (dolist (c args)
+ (unless (funcall match (list c))
+ (throw 'fail nil)))
+ t)))
(throw 'match t)))))))
(funcall match (list condition))))
This bug report was last modified 2 years and 320 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.