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: Thu, 28 Oct 2021 12:36:27 +0800 Message-ID: References: <875ytkqt4x.fsf@gnu.org> <871r48qpuv.fsf@gnu.org> <87sfwop8iu.fsf@gnu.org> <87o87cp3fs.fsf@gnu.org> <87ee87wq47.fsf@gnu.org> <8735omfggk.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="25476"; 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 Thu Oct 28 06:40:46 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 1mfxDZ-0006RK-TQ for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 28 Oct 2021 06:40:46 +0200 Original-Received: from localhost ([::1]:45228 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfxDY-0006fn-UW for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 28 Oct 2021 00:40:44 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49548) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx9h-0001f1-Dc for help-gnu-emacs@gnu.org; Thu, 28 Oct 2021 00:36:46 -0400 Original-Received: from mail-ua1-x92c.google.com ([2607:f8b0:4864:20::92c]:43832) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfx9f-0001Eu-Fu; Thu, 28 Oct 2021 00:36:45 -0400 Original-Received: by mail-ua1-x92c.google.com with SMTP id v3so9099000uam.10; Wed, 27 Oct 2021 21:36:39 -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=pg+wzJs6aYo9LmNVZd/54otw9P5+R9TazkwLwaKZqTE=; b=TBFRLxXEakYbGv0NWpCAplIBDcKzv+YzaygBsp6rVpwtQ+dkP13RNcYzWwtebcEB1Q TRLoGOgdXQBiT1hslTJ1I9rXfXWSvZUmntMi3vU4hzMk8crxgVpN0MSUyI4yALeZrlaG /j7dkP5+qTO/445beE5EimKKnO4vvk8lA2hQkm0C5vdkKFO10HgvBVMoCj17gh/pD1lK sMdxkM1AIHSdOY7kl/rsbTY2PNyoLtndzBzHB6mB5TR7sGTbO5urmaEbA7E5ZXuGEeb1 YycIVZ1h8jrPAuVDZK25TwyCoD7I0T291HeDrMGIDL40xWH6saWU2O+ind/+wxFN/oPA 3tGQ== 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=pg+wzJs6aYo9LmNVZd/54otw9P5+R9TazkwLwaKZqTE=; b=hxs4MChKryxowQszNXdj+PMe6Vl9XdcAvadpxOoQtsxgiPhoaPPBj43NbV9u5X32qP ASRqDU1C6l6t+x7tm7FNH/zb/zqGIngTul6rxcjdzYePDXNq+WJkTEUaebfQWkmXlSI8 MkNPVnTb/T1RshaSgbZsrhIV54lSAphYyvwUD+1jlPklDBiTJwu2qeVURFjbEIM4qyR6 qOcEqhjFplB6aMSqV0oNkAmNiZPkrSprdf6liHwMQxjWU8g05o/O3H+LtVZbFkhWt/2W 7qH9MmtPGnq7Z61TVzPWjoneIzuCvoX5od5oQeAVsquRIWPIz30I+JBt9WJoquGdpMhc h3nA== X-Gm-Message-State: AOAM530NtqkST+SGKB10fcTvzEqNjahbzD5+vWWFBBzHy8rmIH2czi3C lBABPzmh8Q29VgnS6hTb7fBJ0scx7c2HNiNaRO1GfozRK+fHIkCb X-Google-Smtp-Source: ABdhPJxfHSic8WQepBan0gcTygc7L3pkbLQ1Y2B7AvhEnRQGus6SmBRhZUSReF7dXkJ4SOFqS7gTyHNNN6uZrVNdMXU= X-Received: by 2002:a05:6102:3a4c:: with SMTP id c12mr2187483vsu.10.1635395798472; Wed, 27 Oct 2021 21:36:38 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::92c; envelope-from=hongyi.zhao@gmail.com; helo=mail-ua1-x92c.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:134244 Archived-At: On Thu, Oct 28, 2021 at 12:14 PM Hongyi Zhao wrote: > > On Thu, Oct 28, 2021 at 2:52 AM Tassilo Horn wrote: > > > > Hongyi Zhao writes: > > > > >> > 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 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 first cond clause looks good but I don't see why you need the > > t-clause? Isn't the thing that company-ispell is part of the default > > value of the company-tabnine entry of company-backends? If that's the > > case, you want to make company-backends buffer-local and remove it in > > emacs-lisp-mode and derived modes but there is no need to add it again > > otherwise because the default value won't be altered. So that'd be: > > > > --8<---------------cut here---------------start------------->8--- > > (defun hz/company-tabnine-remove-company-ispell () > > (when (derived-mode-p 'emacs-lisp-mode) > > (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--- > > A very strange result based on the above code is that it will remove > company-ispell in all modes, say, LaTeX mode. But the following code > works as expected: > > (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)) > ))) > > > > But most importantly, `emacs-startup-hook' is not the one you want. > > That runs once after emacs has loaded the init file. I'm even not sure > > what buffer is current then. I guess the most logical hook is > > `company-mode-hook'. > > > > (add-hook 'company-mode-hook > > #'hz/company-tabnine-remove-company-ispell) > > This works. Thank you for pointing this out. The following one, which is based on the idea here [1], also works: (add-hook 'after-change-major-mode-hook #'hz/company-tabnine-remove-company-ispell) [1] https://github.com/hlissner/doom-emacs/blob/0869d28483b5d81b818b110af351fd5c4dc04dd9/modules/completion/company/config.el#L66 > > Or maybe there is also some `company-tabnine-*-hook' that runs after > > that package has done its setup in a buffer? > > Unfortunately, it doesn't supply these type hooks. > > HZ -- Assoc. Prof. Hongyi Zhao Theory and Simulation of Materials Hebei Vocational University of Technology and Engineering No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province