unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Roland Winkler <winkler@gnu.org>
Cc: 75170@debbugs.gnu.org
Subject: bug#75170: add-to-alist: new function
Date: Sun, 29 Dec 2024 09:50:04 +0200	[thread overview]
Message-ID: <86cyhbq66r.fsf@gnu.org> (raw)
In-Reply-To: <878qrzm4sb.fsf@gnu.org> (message from Roland Winkler on Sat, 28 Dec 2024 23:33:56 -0600)

> From: Roland Winkler <winkler@gnu.org>
> Date: Sat, 28 Dec 2024 23:33:56 -0600
> 
> On Mon, 12 Feb 2001, Stephen Gildea wrote:
> > Here's a handy function I'd like to see added to Emacs 21: add-to-alist.
> > It is like add-to-list, but it looks only at the cars of the element to
> > be added and the existing list elements when considering a match.  An
> > additional optional argument to add-to-alist says what to do if the car
> > matches but the cdr does not.
> > 
> > I use this function in my .emacs to update values in alists such as
> > default-frame-alist and auto-mode-alist; I'm sure it has other uses.
> > My goal in proposing this function is to allow .emacs files to be
> > shorter and easier to write.  The functions add-to-list and add-hook
> > were important steps in that direction; here is another such step.
> > 
> > 
> > (defun add-to-alist (alist-var elt-cons &optional no-replace)
> >   "Add to the value of ALIST-VAR an element ELT-CONS if it isn't there yet.
> > If an element with the same car as the car of ELT-CONS is already present,
> > replace it with ELT-CONS unless NO-REPLACE is non-nil; if a matching
> > element is not already present, add ELT-CONS to the front of the alist.
> > The test for presence of the car of ELT-CONS is done with `equal'."
> >   (let ((existing-element (assoc (car elt-cons) (symbol-value alist-var))))
> >     (if existing-element
> >         (or no-replace
> >             (rplacd existing-element (cdr elt-cons)))
> >       (set alist-var (cons elt-cons (symbol-value alist-var))))))
> > 
> > 
> > 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)
> 
> While I thought about a function add-to-alist I found the above thread
> from 24 years ago.  Stephen's message describes nicely when such a
> function can be useful.  I suggest to add such a function to subr.el.
> 
> The code below follows the conventions of add-to-list.
> 
> (defun add-to-alist (alist-var elt-cons &optional no-replace append compare-fn)
>   "Add ELT-CONS to the value of ALIST-VAR if it isn't there yet.
> If an element with the same car as the car of ELT-CONS is already present
> in ALIST-VAR, replace it with ELT-CONS unless NO-REPLACE is non-nil.
> If a matching element is not yet present, add ELT-CONS at the beginning
> of ALIST-VAR.  If APPEND is non-nil, add ELT-CONS at the end of ALIST-VAR.
> The test for presence of ELT-CONS is done with `equal', or with COMPARE-FN
> if that's non-nil.
> ALIST-VAR should not refer to a lexical variable.

Thanks.

What is the advantage of adding this function, given that add-to-list
can be used with alists, and given that alist-get can nowadays be used
as a generalize variable?





  parent reply	other threads:[~2024-12-29  7:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-29  5:33 bug#75170: add-to-alist: new function Roland Winkler
2024-12-29  6:34 ` Roland Winkler
2024-12-29  7:54   ` Juri Linkov
2024-12-29 14:54     ` Roland Winkler
2024-12-29  7:33 ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-12-29  7:50 ` Eli Zaretskii [this message]
2024-12-29 14:50   ` Roland Winkler
2025-01-05 15:21     ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86cyhbq66r.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=75170@debbugs.gnu.org \
    --cc=winkler@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).