Function: cl-pushnew

cl-pushnew is a macro defined in cl-lib.el.gz.

Signature

(cl-pushnew X PLACE [KEYWORD VALUE]...)

Documentation

Add X to the list stored in PLACE unless X is already in the list.

PLACE is a generalized variable that stores a list.

Like (push X PLACE), except that PLACE is unmodified if X is eql to an element already in the list stored in PLACE.


Keywords supported: :test :test-not :key

Aliases

pushnew (obsolete since 27.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-lib.el.gz
(defmacro cl-pushnew (x place &rest keys)
  "Add X to the list stored in PLACE unless X is already in the list.
PLACE is a generalized variable that stores a list.

Like (push X PLACE), except that PLACE is unmodified if X is `eql'
to an element already in the list stored in PLACE.

\nKeywords supported:  :test :test-not :key
\n(fn X PLACE [KEYWORD VALUE]...)"
  (declare (debug
            (form place &rest
                  &or [[&or ":test" ":test-not" ":key"] form]
                  [keywordp form])))
  (if (symbolp place)
      (if (null keys)
          (macroexp-let2 nil var x
            `(if (memql ,var ,place)
                 ;; This symbol may later on expand to actual code which then
                 ;; trigger warnings like "value unused" since cl-pushnew's
                 ;; return value is rarely used.  It should not matter that
                 ;; other warnings may be silenced, since `place' is used
                 ;; earlier and should have triggered them already.
                 (with-no-warnings ,place)
               (setq ,place (cons ,var ,place))))
	`(setq ,place (cl-adjoin ,x ,place ,@keys)))
    `(cl-callf2 cl-adjoin ,x ,place ,@keys)))