Function: buffer-match-p

buffer-match-p is a byte-compiled function defined in subr.el.gz.

Signature

(buffer-match-p CONDITION BUFFER-OR-NAME &optional ARG)

Documentation

Return non-nil if BUFFER-OR-NAME matches CONDITION.

CONDITION is either:
- the symbol t, to always match,
- the symbol nil, which never matches,
- a regular expression, to match a buffer name,
- a predicate function that takes BUFFER-OR-NAME and ARG as
  arguments, and returns non-nil if the buffer matches,
- a cons-cell, where the car describes how to interpret the cdr.
  The car can be one of the following:
  * derived-mode: the buffer matches if the buffer's major mode
    is derived from the major mode in the cons-cell's cdr.
  * major-mode: the buffer matches if the buffer's major mode
    is eq to the cons-cell's cdr. Prefer using derived-mode
    instead when both can work.
  * not: the cadr is interpreted as a negation of a condition.
  * and: the cdr is a list of recursive conditions, that all have
    to be met.
  * or: the cdr is a list of recursive condition, of which at
    least one has to be met.

View in manual

Probably introduced at or before Emacs version 29.1.

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun buffer-match-p (condition buffer-or-name &optional arg)
  "Return non-nil if BUFFER-OR-NAME matches CONDITION.
CONDITION is either:
- the symbol t, to always match,
- the symbol nil, which never matches,
- a regular expression, to match a buffer name,
- a predicate function that takes BUFFER-OR-NAME and ARG as
  arguments, and returns non-nil if the buffer matches,
- a cons-cell, where the car describes how to interpret the cdr.
  The car can be one of the following:
  * `derived-mode': the buffer matches if the buffer's major mode
    is derived from the major mode in the cons-cell's cdr.
  * `major-mode': the buffer matches if the buffer's major mode
    is eq to the cons-cell's cdr.  Prefer using `derived-mode'
    instead when both can work.
  * `not': the cadr is interpreted as a negation of a condition.
  * `and': the cdr is a list of recursive conditions, that all have
    to be met.
  * `or': the cdr is a list of recursive condition, of which at
    least one has to be met."
  (letrec
      ((buffer (get-buffer buffer-or-name))
       (match
        (lambda (conditions)
          (catch 'match
            (dolist (condition conditions)
              (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-or-name)
                         (funcall condition buffer-or-name 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))))