r/emacs•
Titibandit

How to remove an element for a list efficiently?

emacs-fu

Hey, I want to configure the ivy-rich-display-transformers-list variable from the ivy-rich package. More precisely, I just want to remove the (ivy-rich-switch-buffer-project (:width 0.18 :face success)) part of it, that shows to which project a buffer belongs when switching buffers. Redefining the entire variable without this bit clutters my config a lot since this variable has a lot of content. Is there a few to just remove that bit from the original value of the variable? I tried to do it using my limited elisp, but I couldn't do it even in a semi-elegant way. Any tips there? Cheers!

Posted Jun 16, 2021, 12:00 PM UTC

66
_pkal•

The simplest thing to do is usually

(setq list (delq (assoc key list) list))
3
github-alphapapa•

Nowadays the simplest way would probably be to use map-delete. The map library's API is easy to use.

2
TitibanditOP•

Indeed, it's pretty straightforward using map-delete. Thanks for that! I got it working by writing:

(require 'map)
(map-delete (plist-get
             (plist-get ivy-rich-display-transformers-list 'ivy-switch-buffer)
             :columns) 'ivy-rich-switch-buffer-project))
2
Lazy-Snail•

First you need to know how to accede to the data.

ivy-rich-display-transformers-list is a plist of plist.

(plist-get
 (plist-get ivy-rich-display-transformers-list 'ivy-switch-buffer)
 :columns)
;; =>
;; ((ivy-switch-buffer-transformer
;;   (:width 0.35))
;;  (ivy-rich-switch-buffer-size
;;   (:width 7))
;;  (ivy-rich-switch-buffer-indicators
;;   (:width 4
;;           :face error
;;           :align right))
;;  (ivy-rich-switch-buffer-major-mode
;;   (:width 12 :face warning))
;;  (ivy-rich-switch-buffer-project
;;   (:width 0.18 :face success))
;;  (ivy-rich-switch-buffer-path
;;   (:width (lambda (x)
;;             (ivy-rich-switch-buffer-shorten-path
;;              x
;;              (ivy-rich-minibuffer-width 0.3))))))

plist-get can be used with generalized setters so we could write

(setf (plist-get
       (plist-get ivy-rich-display-transformers-list 'ivy-switch-buffer)
       :columns)
      (assq-delete-all
       'ivy-rich-switch-buffer-project
       (plist-get
        (plist-get ivy-rich-display-transformers-list 'ivy-switch-buffer)
        :columns)))

But that not elegant as using the variant cl-callf2 of the cl-callf macro:

(cl-callf2 assq-delete-all
    'ivy-rich-switch-buffer-project
    (plist-get
     (plist-get ivy-rich-display-transformers-list 'ivy-switch-buffer)
     :columns))

More about that :

You have to set a variable or PLACE with the return value of a delete function in emacs-lisp because you can't rely on delete functions to always do it for you.

cl-callf call a function on PLACE and set the PLACE with the result (PLACE is the valid target of generalized setters).

So you can commonly use cl-callf or cl-callf2 in theses circumstances to write concise code. Using macro-expansion utils to help you to get it right.

Aside from that, if your only concern with 'ivy-rich-switch-buffer-project is the performance of ivy-rich ; you can activate the cache :

(setq ivy-rich-project-root-cache-mode 1)

And remember to refresh it if you paths change.

1
TitibanditOP•

Thanks for the detailed answer! I was actually missing the concept of functions that operate on "PLACE". It's way easier doing this using these functions!

1
Lazy-Snail•

Yes that is practical to operate on eg, nested list.

Just remember to check what are the valid places Setting-Generalized-Variables to know when you can use it.

1