From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Roland Winkler Newsgroups: gmane.emacs.bugs Subject: bug#75170: add-to-alist: new function Date: Sat, 28 Dec 2024 23:33:56 -0600 Message-ID: <878qrzm4sb.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19606"; mail-complaints-to="usenet@ciao.gmane.io" To: 75170@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 29 06:35:33 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 1tRlxc-0004wo-Fq for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 29 Dec 2024 06:35:32 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tRlxC-0003fT-2B; Sun, 29 Dec 2024 00:35:06 -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 1tRlxA-0003e5-FM for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 00:35: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 1tRlxA-0003Xr-6O for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 00:35:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=t1mkyvGNBr0B1s1A4AmENN7//+icAfreDWlg1cNx7Js=; b=qdqtQHK2Z9Hply4vNVhjeRXStr3gUFPsHoLLBSB16M3+zRzrH78trIjmMIK/eAmZDqpfBgGlUIVznFRIwdsTXndRyWQwTDUdNEnF9bZ06fXLvEIyJLnDL2RYxk0Q4srq/jeWD4BpCEjGsC49DlMHzo4Oa9w596bMiCgY9nPXkx8ORd7ThCOd8ag+eEkBJF/Xf18VpQ9AuhRLo8bFOrqad2J3/a6VBc1oolW+Zaqv19x0YgsJKMScJjONNRG8/rf8bUL7HkaEEUZgKKjLvCIi9UuxXjaNGeYLba9UCRNrXLe8tXFa+lGY79IRWOAPLj3DoER0ONlr0L7z71OvsOYtGA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tRlx8-0001p0-Vi for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 00:35:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Roland Winkler Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 29 Dec 2024 05:35:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 75170 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.17354504486900 (code B ref -1); Sun, 29 Dec 2024 05:35:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 29 Dec 2024 05:34:08 +0000 Original-Received: from localhost ([127.0.0.1]:53463 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tRlwF-0001nD-Oj for submit@debbugs.gnu.org; Sun, 29 Dec 2024 00:34:08 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:35900) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tRlwD-0001n3-FV for submit@debbugs.gnu.org; Sun, 29 Dec 2024 00:34: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 1tRlwC-0003Zn-LR for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 00:34:04 -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 1tRlwC-0003Hh-7K for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 00:34:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to: references; bh=t1mkyvGNBr0B1s1A4AmENN7//+icAfreDWlg1cNx7Js=; b=pVjH/oGgC9MBDd 3ci7q7ka+4n9bmJi1p4ydW90d4GOU8vDHN2A98IDIzQEsKCm84e3B0lAMB19dqJ93whf+awNJI3Uv RrHUjTxTaj/QqzPZDxcqpvlvu7DCjY4eD0YzNT59cMxvEKiv9/nflsth/0W9n2jPHIcjGu274bfJQ K8qIwrd1bNXkzJ2gYJtGhlcvoPIq22BKiNwKyfgkCIGhvlrHoddqnZ2i1/E+4QmGXulE087Dghjen DkCU5x0A2uvjnc0OT/kGgzMYavHT+rDg2TOLl/CCGITTpZ8a/Z7RvkR86rswmUkcToY3EHvtoPGRp HcLp8lcox2M/1zbAPw0w==; 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:297929 Archived-At: 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. The return value is the new value of ALIST-VAR." (let ((elt (cond ((or (null compare-fn) (eq compare-fn #'equal)) (assoc (car elt-cons) (symbol-value alist-var))) ((eq compare-fn #'eq) (assq (car elt-cons) (symbol-value alist-var))) (t (let ((alist (symbol-value alist-var)) (key (car elt-cons))) (while (and alist (not (funcall compare-fn key (caar alist)))) (setq alist (cdr alist))) (car alist)))))) (if elt (progn (unless no-replace (setcdr elt (cdr elt-cons))) (symbol-value alist-var)) (set alist-var (if append (append (symbol-value alist-var) (list elt-cons)) (cons elt-cons (symbol-value alist-var)))))))