From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Hongyi Zhao Newsgroups: gmane.emacs.help Subject: Re: Add/remove an element into/from a cons. Date: Tue, 26 Oct 2021 16:12:54 +0800 Message-ID: References: <875ytkqt4x.fsf@gnu.org> <871r48qpuv.fsf@gnu.org> <87sfwop8iu.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24834"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs , Stefan Monnier To: Tassilo Horn Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Oct 26 10:16:30 2021 Return-path: Envelope-to: geh-help-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 1mfHdF-0006FA-Le for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 26 Oct 2021 10:16:29 +0200 Original-Received: from localhost ([::1]:51408 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfHd2-00061e-Kb for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 26 Oct 2021 04:16:19 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44658) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfHa1-0003BF-OE for help-gnu-emacs@gnu.org; Tue, 26 Oct 2021 04:13:09 -0400 Original-Received: from mail-ua1-x936.google.com ([2607:f8b0:4864:20::936]:34625) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfHZz-0007C5-7O; Tue, 26 Oct 2021 04:13:09 -0400 Original-Received: by mail-ua1-x936.google.com with SMTP id h4so27537287uaw.1; Tue, 26 Oct 2021 01:13:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cC+fn7W350P6WC4ofdVF9VXAqlci0nqmiPwiRVarAOs=; b=OSwpxFK5kBR1uA29F1jjOqfOukDoEJufU2YuoJ32HPL0xTMIMDJzVEaU72j4MFquM4 74RJzbZTUYBdd6s61+ZMSoNScNMHgsTyM8TcjhyHYT7tfSahcUrAlPPTDWcVaVygC0Hl zc5GhnKXuUyeW74r6EJ7TYVFtjDpfrIaNig8tyoQ4UxLrjqYiW3vpC/uSIIfjqooIhsd 9+nRcjmAkfi+8ApgylN4JE9kjg8UhhTGIIuQa8VSbd66JKWHArlDquMH3sDlhLHZbTry 8f3LzgM+vWo+7B1gImzSxbE45Lva3FDpKASsTQw1kwVM2R2YdBwYZg49YHYwQh39bRHy ZgnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cC+fn7W350P6WC4ofdVF9VXAqlci0nqmiPwiRVarAOs=; b=MHx8uFUa1kv/hRrGpLd3in4YnKQL/2s0Tkrh4cF47VgCbuN9IRaIsp1pzBL0962GJs FFqQk/JX9OAWdjJdUS2J9DEnSawdyyZVA2pWEbyQAfC4+q1FgN2xtx4z7H4yM1DJ8Lye aHjH9xh3oomXYQxVmnCg0OCzZdB/MPgIHsosEd5SIpjhHlZ3GpUfOrGqgx6iglP4Open L4sdKO4wkFJmePHGwJnxcTDlE0ZcTIZ1fbS517vmsfceJbSdRvNjVtwAvOeF7rPHT+D6 pq+gQTMUA1+NlnPTNy9uGj6jUJiALmzYavk3fqBnH6zlqW38zZ876e6/tbY8ZdqXndGw UrYQ== X-Gm-Message-State: AOAM5325EKgCuzz9MdeUQLncU+JlG/n64elX1K6tFIGiRXF5Qh89qfwX 9AmmgPIHI2bFsjoBWk4I/r+8e30MiMw/Jd8uFwdl/uWNJpMDwH/h X-Google-Smtp-Source: ABdhPJwLeWdSaRfOcYNJEw4CdjVtRu+lV5VGjg8IO9vThMRjXsMRC6ZJwtksjNZGdxXWUQoNEJFFfto1if3eEXM/g6g= X-Received: by 2002:a67:a60c:: with SMTP id p12mr21709837vse.25.1635235985552; Tue, 26 Oct 2021 01:13:05 -0700 (PDT) In-Reply-To: <87sfwop8iu.fsf@gnu.org> Received-SPF: pass client-ip=2607:f8b0:4864:20::936; envelope-from=hongyi.zhao@gmail.com; helo=mail-ua1-x936.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:134189 Archived-At: On Tue, Oct 26, 2021 at 3:09 PM Tassilo Horn wrote: > > Hongyi Zhao writes: > > >> This would add it again as the last element: > >> > >> --8<---------------cut here---------------start------------->8--- > >> (let ((place (assoc 'company-tabnine th/test))) > >> (setf place (append place '(company-ispell)))) > >> --8<---------------cut here---------------end--------------->8--- > > > > Thanks. But the push doesn't work: > > > > (let ((place (assoc 'company-tabnine th/test))) > > (setf place (push place '(company-ispell)))) > > Have a look at the docs. `push' wants the place as second element, and > then you don't need the `setf' anyway. > > --8<---------------cut here---------------start------------->8--- > (let ((place (assoc 'company-tabnine th/test))) > (push 'company-ispell place)) > --8<---------------cut here---------------end--------------->8--- > > But note that `push' will add it to the front of the list. > > Hm, I just saw that none of my code actually works, i.e., it does not > alter the value of `th/test'. > > --8<---------------cut here---------------start------------->8--- > (setq th/test '((company-tabnine :separate company-capf company-dabbrev company-keywords company-files company-ispell))) > > (let ((place (assoc 'company-tabnine th/test))) > (setf place (remove 'company-ispell place))) > > (let ((place (assoc 'company-tabnine th/test))) > (setf place (append place '(company-ispell)))) > > (let ((place (assoc 'company-tabnine th/test))) > (push 'company-ispell place)) > --8<---------------cut here---------------end--------------->8--- > > Aren't alist entries setf-able places? It seems so, at least: > > (let ((val (assoc 'company-tabnine th/test))) > (setf (assoc 'company-tabnine th/test) > (remove 'company-ispell val))) > > signals a void-function error. > > Ah, now I got it. You must use `alist-get' instead of `assoc' and not > bind the place to a variable but use it explicitly as the first argument > of `setf' which makes sense given that `setf' is a macro determining the > place from its first argument. So these do actually work: > > --8<---------------cut here---------------start------------->8--- > (setf (alist-get 'company-tabnine th/test) > (remove 'company-ispell (alist-get 'company-tabnine th/test))) > > (setf (alist-get 'company-tabnine th/test) > (append (alist-get 'company-tabnine th/test) > '(company-ispell))) > > (push 'company-ispell (alist-get 'company-tabnine th/test)) > --8<---------------cut here---------------end--------------->8--- I tried with the following function: ```emacs-lisp (use-package company-tabnine :init (setq company-backends '((company-tabnine :separate company-dabbrev company-keywords company-files company-ispell company-capf))) :hook (emacs-lisp-mode . hz/scratch-init) :config (defun hz/scratch-init () (with-current-buffer "*scratch*" (make-local-variable 'company-backends) (setf (alist-get 'company-tabnine company-backends) (remove 'company-ispell (alist-get 'company-tabnine company-backends))) )) ``` The above method will set the correct company-backends when I'm on scratch buffer. But I also find that when I switch to other buffers, says, AUCTeX based LaTeX-mode, the company-backends settings still remains the same, and can't be restored to the default configuration, i.e., the following: (setq company-backends '((company-tabnine :separate company-dabbrev company-keywords company-files company-ispell company-capf))) HZ