In Emacs lisp there is add-to-list
to add a single element to a list (if it doesn't exist already).
Instead of one, I want to add multiple elements. Also, I do not want to filter duplicate elements but add them to the list nonetheless.
Currently, I have implemented the following function:
(defun append-to-list (list-var elements)
"Append ELEMENTS to the end of LIST-VAR.
The return value is the new value of LIST-VAR."
(set list-var (append (symbol-value list-var) elements)))
The function does what I want but I was wondering if something like this (or better) already exists in Emacs lisp. I don't want to reinvent the wheel.
Update 1: Stefan points out below that this code does not work with lexical scoping. Is there a way to make it work?
Update 2: Previously I thought that duplicate filtering would be fine but it's not. I do need the duplicates.
symbol-value
andset
means it can't be used with a lexically scoped variable. Unless you really need it, better addelements
at the beginning, sinceelements
will almost always be shorter (and sometimes much shorter) thanlist-var
. – Thenceforward