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 13:40:39 +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="27019"; 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 07:43:34 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 1mfyCM-0006r1-DZ for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 28 Oct 2021 07:43:34 +0200 Original-Received: from localhost ([::1]:47154 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfyCK-0006xl-2U for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 28 Oct 2021 01:43:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58390) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfy9n-0006wU-Gd for help-gnu-emacs@gnu.org; Thu, 28 Oct 2021 01:40:56 -0400 Original-Received: from mail-ua1-x92f.google.com ([2607:f8b0:4864:20::92f]:33706) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mfy9k-0002Ps-IK; Thu, 28 Oct 2021 01:40:55 -0400 Original-Received: by mail-ua1-x92f.google.com with SMTP id s4so9489499uaq.0; Wed, 27 Oct 2021 22:40:51 -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=TkLbs7FTT/9mq2SBSgvH5WQYuxV6+cwy668iE9rwIUU=; b=g6ndUd2vwL+rGqy46n2D4T2c6VZSNdiRCc2xC2vFcsO/RqWuxhH1AOllxkjevjL/Ep EJzpz68Gg5h8lDR6ztSosd1qQw8rDHW9P5BbkSx47vDojWonjHuePc4ZJBLTbtYNpFT/ 1jLjE5O5hNMLhYEhYcNFpmbeHnLe5AZyogVfW/cOkB+dYvCBRD0taEsZvKEaG52ZNkwQ SAI4buHEPhJ7dRXXtsElVfEK2Fg7xmxm2VmIn3VItO3vLws4TNOWpaY7v7gzHz3UJQiP UVwWb/IpTKDjoDBXr2q1mne31cVVBC03+rsmm+10EWJjEDNSJBtWUhsF7O+GkwEfNx1M pHng== 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=TkLbs7FTT/9mq2SBSgvH5WQYuxV6+cwy668iE9rwIUU=; b=QPZBpZcXwUkR1bNX++L/Esq08w9ymB5b+95tqMnGtPVRbyxwF8NVB/9bLHaroLGi7F ivsjs4Yvn/nUfl8FFMnzHK6MdNCvUOyMdXogwZH9UYQ1egtWUi148VdV2r0qvzb5LgYX 8EzAxRwc1DbzTBAa4nGUFQTDAvLGw3yvYZ7WLM8kzRA3L1E7Dr3uJFxA5kGsEpIryjMx Qhi3xYCyp0J1dHG4xBc5UMsQBOOJ2dy85GAjTcHvCU5hUpRRpjPCPWpYyR/IWLBEm2Pt H/fFfECFuDf7TfDOj0sbk7PNzXXHPO/QlyzJYRMM+m5QjTBgTut1GOrXYptk4wuq2quX 2Rsw== X-Gm-Message-State: AOAM533fcK/KZQjhVgjjPlTpRYbg5UvAuYNI4xDed0RXaSWe8H+aS+0G Spicy0zmnN8MUpcMmVL1Z0iqZ+gFx2ZDp7TzkJuQ6zbiBAs1gR7M X-Google-Smtp-Source: ABdhPJx6lBxXPArj5R5JxUplK8oInCvgFeKCiYIsFGs6GS6cYM0GF8SwcQYEOza8MgJGxfFzRZInfAGDyWwLJbADWqo= X-Received: by 2002:a67:f1ca:: with SMTP id v10mr2237608vsm.55.1635399650771; Wed, 27 Oct 2021 22:40:50 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::92f; envelope-from=hongyi.zhao@gmail.com; helo=mail-ua1-x92f.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:134245 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)) > ))) I also obtained the following code snippet, which is extracted from doom-emacs [1] with slightly adjusting: ```emacs-lisp ;;;;; Some Emacs lisp code extracted from doom-emacs ;;;###autoload (defvar company-backend-alist '((text-mode (company-tabnine :separate company-ispell company-dabbrev company-yasnippet)) (prog-mode company-capf company-yasnippet) (conf-mode company-capf company-dabbrev-code company-yasnippet)) "An alist matching modes to company backends. The backends for any mode is built from this.") (defun doom-enlist (exp) "Return EXP wrapped in a list, or as-is if already a list." (declare (pure t) (side-effect-free t)) (if (proper-list-p exp) exp (list exp))) ;;;###autodef (defun set-company-backend (modes &rest backends) "Prepends BACKENDS (in order) to `company-backends' in MODES. MODES should be one symbol or a list of them, representing major or minor modes. This will overwrite backends for MODES on consecutive uses. If the car of BACKENDS is nil, unset the backends for MODES. Examples: (set-company-backend 'js2-mode 'company-tide 'company-yasnippet) (set-company-backend 'sh-mode '(company-shell :with company-yasnippet)) (set-company-backend '(c-mode c++-mode) '(:separate company-irony-c-headers company-irony)) (set-company-backend 'sh-mode nil) ; unsets backends for sh-mode" (declare (indent defun)) (dolist (mode (doom-enlist modes)) (if (null (car backends)) (setq company-backend-alist (delq (assq mode company-backend-alist) company-backend-alist)) (setf (alist-get mode company-backend-alist) backends)))) ;; ;;; Library (defun company--backends () (let (backends) (let ((mode major-mode) (modes (list major-mode))) (while (setq mode (get mode 'derived-mode-parent)) (push mode modes)) (dolist (mode modes) (dolist (backend (append (cdr (assq mode company-backend-alist)) (default-value 'company-backends))) (push backend backends))) (delete-dups (append (cl-loop for (mode . backends) in company-backend-alist if (or (eq major-mode mode) ; major modes (and (boundp mode) (symbol-value mode))) ; minor modes append backends) (nreverse backends)))))) ``` With the help of the above lisp code, the following configuration is enough for the question discussed here: ```emacs-lisp (use-package company-math) (use-package company-tabnine :hook (LaTeX-mode . (lambda () (setq-local company-backends (company--backends)) (add-to-list 'company-backends '(company-math-symbols-latex company-latex-commands)) )) (text-mode . (lambda () (add-to-list 'company-backends 'company-math-symbols-unicode)))) ``` But I've two problems with the above method: 1. When the `company--backends' command is used separately, as follows: (add-hook 'company-mode-hook (lambda () (setq-local company-backends (company--backends)) )) The `(setq-local company-backends (company--backends))` always put the default backends set in the `company-backend-alist' at the very beginning of the ultimate compand-backends result, which makes it difficult for me to set a higher priority for the company-backends added in other hooks. 2. The `set-company-backend' command can't prepend BACKENDS (in order) to `company-backends' in MODES, say, by the followng one: (set-company-backend 'LaTeX-mode 'company-math-symbols-latex 'company-latex-commands) [1] https://github.com/hlissner/doom-emacs/blob/develop/modules/completion/company/autoload.el > > > 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. > > > 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