Subject: bug#75170: add-to-alist: new function

From: Roland Winkler <[email-masked]>
Date: Sun, 19 Jan 2025 15:17:56 -0600

On Sun, Jan 19 2025, Juri Linkov wrote:
> Isn't NO-REPLACE equivalent to this:
>
>   (cl-pushnew "statement2" (alist-get 'sentence (alist-get 'c treesit-thing-settings)))
>
> that results in
>
>   ((c (sexp (not "[](),[{}]"))
>       (sentence "statement2" "statement")
>       (text "\\(?:comment\\|raw_string_literal\\)")))

I am not sure how to read the above example.  But it seems to me, it is
different from what NO-REPLACE does.  (Anyway, it is a piece of code
that I would not want to suggest to users to put into their init file.)

With NO-REPLACE non-nil, add-to-list will add a new association for KEY,
if KEY is not yet a key in the alist.  But if the alist already contains
an association for KEY, this association will not be modified.

Stephen's original proposal illustrated how this can be useful in a
user's init file in the context of auto-mode-alist:

On Mon, 12 Feb 2001, Stephen Gildea wrote:
> The no-replace argument is useful for setting auto-mode-alist when you
> don't know whether Emacs supports a particular programming language.
> For example, the following suppresses using text-mode for m4 files in
> Emacs 19 but doesn't override using m4-mode in Emacs 20.
> 
> (setq default-major-mode 'text-mode)
> (add-to-alist 'auto-mode-alist '("\\.m4\\'" . fundamental-mode) t)