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: Wed, 27 Oct 2021 13:13:01 +0800 Message-ID: References: <875ytkqt4x.fsf@gnu.org> <871r48qpuv.fsf@gnu.org> <87sfwop8iu.fsf@gnu.org> <87o87cp3fs.fsf@gnu.org> <87ee87wq47.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="13504"; 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 Wed Oct 27 07:13:50 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 1mfbG2-0003J4-7Y for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 27 Oct 2021 07:13:50 +0200 Original-Received: from localhost ([::1]:36236 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfbG1-0004GT-4O for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 27 Oct 2021 01:13:49 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56244) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfbFU-0004GL-JW for help-gnu-emacs@gnu.org; Wed, 27 Oct 2021 01:13:16 -0400 Original-Received: from mail-ua1-x930.google.com ([2607:f8b0:4864:20::930]:45626) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfbFS-0004kl-Ma; Wed, 27 Oct 2021 01:13:16 -0400 Original-Received: by mail-ua1-x930.google.com with SMTP id z22so328725uaq.12; Tue, 26 Oct 2021 22:13:14 -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=HucRIX8vpk5F+vA7cAI8rfJyWd7kDu7Moi/IwbsvTo4=; b=AFBSB2U14HrjgGUodYfYCOsXBzTvyYHV1w921eCNOWL2xOajnInQ605TqtsP+K/ER7 WGtZ4KxtW79oM1wQROzRaeQcReoWGfePEsqKVWWMVcktEyz6V4OrZDcC5tGVbbSW3z/b pUkzRQ8v8kOlq7eRK/pX3/zWXTX8Lqn10EPaG1fXuh6lOOIlJ0oRtlWOwnsEaKFL4z9q w/taxTthc08LKphl8BrZISaUPhmDkNsk2O8tiT31iSL3Zy/HguHoPlkUAvmYHU9OOwa6 QbRYEFJefCLBnwkP2prnvBxp3ctffNFGoLQV+khe+bno2YgfQMa0eOtbplBGLHZJBmLo t2jQ== 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=HucRIX8vpk5F+vA7cAI8rfJyWd7kDu7Moi/IwbsvTo4=; b=kYCv2Y4WErW6BHtt/CdiyduhxoNFjr5hLJ4RwMlJjZpqMRyFC1S2q6JCwJ6OFwRFpM PQubrZU3HsSMbnfJbQMAhFHRykEreCuGkiRsuQ/1VmLdM+i7blRymlrUSIGvBI9Tj0ru BTWXdpfFPSNQ1epD+uPaej/oSOOimXKlrmDDrxd4tFmem1SUuEoXMf75M/ZSEgVPoUpU VslWa4GWZzYnfULCSiw6i4OEvIZKDGh0ZtThTHMqrEZJNkubfBJAvyDGXz5fjTxyg2ow kJ7lYKOe39Rkmywz1fcbxtSCCC6wWQTPOnRH4V0IJfa1VWfKhjnNvuLN3Qda5FTeq1VF KRSQ== X-Gm-Message-State: AOAM531O3Ax+YcnarqNRykwqfl1fbZP71XlnrFU9CYQoTzoSyUkZgmTd CTBVYY3qElmBPN1lAQ2ihEt1jroUysp4N3OhmboMF7MkWhOtfIZn X-Google-Smtp-Source: ABdhPJwhc+GoA87qg71itkT80oW5gbIzxwTHbunMUVxblxRm29wTWcY10bUQYsdWzf9nGVV+yBIgs0qeKSt+gGGH12g= X-Received: by 2002:ab0:6254:: with SMTP id p20mr23877453uao.115.1635311593151; Tue, 26 Oct 2021 22:13:13 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::930; envelope-from=hongyi.zhao@gmail.com; helo=mail-ua1-x930.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:134228 Archived-At: On Wed, Oct 27, 2021 at 12:54 PM Hongyi Zhao wrote: > > On Wed, Oct 27, 2021 at 9:39 AM Hongyi Zhao wrote: > > > > On Wed, Oct 27, 2021 at 3:19 AM Tassilo Horn wrote: > > > > > > Hongyi Zhao writes: > > > > > > >> You mean, the above function changes the value not only in *scratch* > > > >> but all buffers? > > > > > > > > After using the above function, it disables me to check the value of > > > > `company-backends' when I switch from scratch to other buffers. > > > > > > What? > > > > > > > The following message will be triggered when I call `C-h v > > > > company-backends RET': > > > > > > > > Type "q" to restore previous buffer, SPC to scroll help. > > > > > > Yes, but how does that message hinder you from checking the value of > > > `company-backends'? > > > > I can't reproduce this now. > > > > > > I'm using company-tabnine package and the following company-backends > > > > setting: > > > > > > > > (setq company-backends '((company-tabnine :separate company-dabbrev > > > > company-keywords company-files company-ispell company-capf))) > > > > > > > > I want to disable the company-ispell backend when I'm in > > > > emacs-lisp-mode. The following function will do the trick: > > > > > > > > (defun hz/scratch-init () > > > > (with-current-buffer "*scratch*" > > > > (setq-local company-backends > > > > '((company-tabnine :separate company-capf > > > > company-dabbrev company-keywords company-files))) > > > > )) > > > > > > > > (add-hook 'emacs-lisp-mode-hook #'hz/scratch-init) > > > > > > Well, your function will always run when `emacs-lisp-mode' or a mode > > > derived from it, like `lisp-interaction-mode', the major-mode of > > > *scratch*, is activated. But it evaluates that code always in *scratch* > > > which defeats the purpose of disabling company-ispell in every buffer > > > having `emacs-lisp-mode' as major-mode. > > > > > > > But as I've posted here, considering that I've also had a definite > > > > company-backends setting, so I want to remove the company-ispell from > > > > it programmatically. > > > > > > > > I hope this time I've clearly stated my intention. > > > > > > I guess so. In certain modes, you would like to have `company-ispell' > > > removed from the company-tabnine entry of company-backends. Right? > > > > > > In that respect, I think a function like > > > > > > --8<---------------cut here---------------start------------->8--- > > > (defun hz/company-tabnine-remove-company-ispell () > > > (make-local-variable 'company-backends) > > > (setf (alist-get 'company-tabnine company-backends) > > > (remove 'company-ispell > > > (alist-get 'company-tabnine company-backends)))) > > > --8<---------------cut here---------------end--------------->8--- > > > > > > should do the trick. However, I can imagine that you might not be able > > > to use it in major-mode hooks such as `emacs-lisp-mode' due to ordering > > > issues, i.e., I can easily imagine that `company-mode' (and this > > > company-tabnine thingy) is initialized *after* the major-mode hook has > > > run because usually, you activate minor-modes in major-mode hooks. In > > > that case you would try to remove before the company-tabnine entry has > > > been added to company-backends. > > > > > > That's all just guesswork since I don't use company. > > > > I have changed the original function to the following form: > > > > ```emacs-lisp > > (defun hz/company-tabnine-remove-company-ispell () > > (setq-local company-backends > > '((company-tabnine :separate company-capf company-dabbrev > > company-keywords company-files)))) > > ``` > > > > Based on my tries, your above suggest code will set and append the > > following value to the current buffer's company-backends: > > > > (company-tabnine :separate company-capf company-dabbrev > > company-keywords company-files) > > I also tried the following snippet, but still failed: > > (defun hz/company-tabnine-remove-company-ispell () > (make-local-variable 'company-backends) > (cond > ( > (derived-mode-p 'emacs-lisp-mode) > (setf (alist-get 'company-tabnine company-backends) > (remove 'company-ispell (alist-get 'company-tabnine > company-backends)))) > ( > t > (setf (alist-get 'company-tabnine company-backends) > (append (alist-get 'company-tabnine company-backends) > '(company-ispell)))))) > > (add-hook 'emacs-startup-hook #'hz/company-tabnine-remove-company-ispell) The function currently used by me to achieve the goal is as follows: (defun hz/company-tabnine-remove-company-ispell () (when (derived-mode-p 'emacs-lisp-mode) (setq-local company-backends '((company-tabnine :separate company-capf company-dabbrev company-keywords company-files))))) (add-hook 'emacs-startup-hook #'hz/company-tabnine-remove-company-ispell) HZ