From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#75170: add-to-alist: new function Date: Sun, 29 Dec 2024 09:50:04 +0200 Message-ID: <86cyhbq66r.fsf@gnu.org> References: <878qrzm4sb.fsf@gnu.org> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15028"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 75170@debbugs.gnu.org To: Roland Winkler Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 29 08:51:22 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tRo54-0003nt-5e for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 29 Dec 2024 08:51:22 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tRo4n-0006FY-84; Sun, 29 Dec 2024 02:51:05 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tRo4m-0006FL-1R for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 02:51:04 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tRo4l-0006x7-NZ for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 02:51:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=References:In-Reply-To:From:Date:To:Subject; bh=ZZQGGx/Y0r1PegjkG/cNejJ5wVf22mUdBRIYM0gaRBk=; b=VcTj9Xjr2d9A7JFfUFrsghmitRcK1vJsBnN84pyGEXke0f1x4gYhrZ+KboQdHvA+YoXd5bsd4W4wzrsgx8cYqgBi9ECFCDq5JF6f0PDdmJcgTwxnNr95wA530sOoRtDKWH+EPRtsuk0CEeF0+SJjNch9alxl/QEZul7I0oSTKK6Lqd/Pyin14VXMAW6dkwkmPs6ktCJEh7gQjgE2UJn2XoB+TZk9UARZMOGNbgQOyJeKOe4HPfp+/kkgLbi8XjntpLfFnMEs2JOXM25e70C1Fmq6pV8JL/fjvjowOVop44YStXK9IFLyawbtsKMZU+nEOBxPxR/wzxXorLMzgHOh5g==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tRo4k-0000UL-G4 for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 02:51:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 29 Dec 2024 07:51:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 75170 X-GNU-PR-Package: emacs Original-Received: via spool by 75170-submit@debbugs.gnu.org id=B75170.17354586151804 (code B ref 75170); Sun, 29 Dec 2024 07:51:02 +0000 Original-Received: (at 75170) by debbugs.gnu.org; 29 Dec 2024 07:50:15 +0000 Original-Received: from localhost ([127.0.0.1]:53637 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tRo3y-0000T2-GA for submit@debbugs.gnu.org; Sun, 29 Dec 2024 02:50:14 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:55452) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tRo3w-0000Q2-0C for 75170@debbugs.gnu.org; Sun, 29 Dec 2024 02:50:12 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tRo3q-0006ih-IA for 75170@debbugs.gnu.org; Sun, 29 Dec 2024 02:50:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=ZZQGGx/Y0r1PegjkG/cNejJ5wVf22mUdBRIYM0gaRBk=; b=CGrE+eaApweK msVoGffc7x9GG+lCMAuR3KyJMtMl1WsLA0rrPeyUcZi4IFOOTwDgZOy2bjPxiJYGUIwPkKu49UhaH X9a6VWxg1kWCj3/jC7EISDsbVlWinss4HsrXaU6kZtcRaylRZhDYdHAtvCJzwxHB6UEUjtUiKlJX4 jFK0IJI7KOIFEjVE76RwHfXGUORCyekdKhvrq3ESyykkre65zJPV0xMuUprl37JRY0DSgcdm4YD/p O19f8ZqLzK4DNHEn3TmNntEwUSp51HjqpbBeXblJJILRwICfWssbznwwOMF9nH0xxg7J85sub1pnB 3pUJDoHUwLtCwnTCgTAXzw==; In-Reply-To: <878qrzm4sb.fsf@gnu.org> (message from Roland Winkler on Sat, 28 Dec 2024 23:33:56 -0600) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:297936 Archived-At: > From: Roland Winkler > 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?