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: Thu, 28 Oct 2021 13:40:39 +0800

On Thu, Oct 28, 2021 at 12:14 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Thu, Oct 28, 2021 at 2:52 AM Tassilo Horn <tsdh@gnu.org> wrote:
> >
> > Hongyi Zhao <hongyi.zhao@gmail.com> 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



reply via email to

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