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: Tue, 26 Oct 2021 16:12:54 +0800

On Tue, Oct 26, 2021 at 3:09 PM Tassilo Horn <tsdh@gnu.org> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> >> This would add it again as the last element:
> >>
> >> --8<---------------cut here---------------start------------->8---
> >> (let ((place (assoc 'company-tabnine th/test)))
> >>   (setf place (append place '(company-ispell))))
> >> --8<---------------cut here---------------end--------------->8---
> >
> > Thanks. But the push doesn't work:
> >
> > (let ((place (assoc 'company-tabnine th/test)))
> >   (setf place (push place '(company-ispell))))
>
> Have a look at the docs.  `push' wants the place as second element, and
> then you don't need the `setf' anyway.
>
> --8<---------------cut here---------------start------------->8---
> (let ((place (assoc 'company-tabnine th/test)))
>   (push 'company-ispell place))
> --8<---------------cut here---------------end--------------->8---
>
> But note that `push' will add it to the front of the list.
>
> Hm, I just saw that none of my code actually works, i.e., it does not
> alter the value of `th/test'.
>
> --8<---------------cut here---------------start------------->8---
> (setq th/test '((company-tabnine :separate company-capf company-dabbrev 
> company-keywords company-files company-ispell)))
>
> (let ((place (assoc 'company-tabnine th/test)))
>   (setf place (remove 'company-ispell place)))
>
> (let ((place (assoc 'company-tabnine th/test)))
>   (setf place (append place '(company-ispell))))
>
> (let ((place (assoc 'company-tabnine th/test)))
>   (push 'company-ispell place))
> --8<---------------cut here---------------end--------------->8---
>
> Aren't alist entries setf-able places?  It seems so, at least:
>
> (let ((val (assoc 'company-tabnine th/test)))
>   (setf (assoc 'company-tabnine th/test)
>         (remove 'company-ispell val)))
>
> signals a void-function error.
>
> Ah, now I got it.  You must use `alist-get' instead of `assoc' and not
> bind the place to a variable but use it explicitly as the first argument
> of `setf' which makes sense given that `setf' is a macro determining the
> place from its first argument.  So these do actually work:
>
> --8<---------------cut here---------------start------------->8---
> (setf (alist-get 'company-tabnine th/test)
>       (remove 'company-ispell (alist-get 'company-tabnine th/test)))
>
> (setf (alist-get 'company-tabnine th/test)
>       (append (alist-get 'company-tabnine th/test)
>               '(company-ispell)))
>
> (push 'company-ispell (alist-get 'company-tabnine th/test))
> --8<---------------cut here---------------end--------------->8---

I tried with the following function:

```emacs-lisp
(use-package company-tabnine
  :init
  (setq company-backends '((company-tabnine :separate company-dabbrev
company-keywords company-files company-ispell company-capf)))

  :hook
  (emacs-lisp-mode . hz/scratch-init)

  :config
  (defun hz/scratch-init ()
    (with-current-buffer "*scratch*"
      (make-local-variable 'company-backends)
       (setf (alist-get 'company-tabnine company-backends)
               (remove 'company-ispell (alist-get 'company-tabnine
company-backends)))
     ))
```
The above method will set the correct company-backends when I'm on
scratch buffer. But I also find that when I switch to other buffers,
says, AUCTeX based LaTeX-mode, the company-backends settings still
remains the same, and can't be restored to the default configuration,
i.e., the following:

(setq company-backends '((company-tabnine :separate company-dabbrev
company-keywords company-files company-ispell company-capf)))

HZ



reply via email to

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