help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Add/remove an element into/from a cons.


From: Hongyi Zhao
Subject: Re: Add/remove an element into/from a cons.
Date: Wed, 27 Oct 2021 13:13:01 +0800

On Wed, Oct 27, 2021 at 12:54 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Wed, Oct 27, 2021 at 9:39 AM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> >
> > On Wed, Oct 27, 2021 at 3:19 AM Tassilo Horn <tsdh@gnu.org> wrote:
> > >
> > > Hongyi Zhao <hongyi.zhao@gmail.com> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]