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

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

cl-delete-if vs list of overlays


From: Omar Polo
Subject: cl-delete-if vs list of overlays
Date: Wed, 09 Dec 2020 10:53:26 +0100
User-agent: mu4e 1.4.13; emacs 27.1

Hello,

I was playing with overlays and the fringe, when I found something I
couldn't really understand.  Hence this help request :)

Let me say beforehand that on a second thought what I was doing isn't
probably the best way to manage the overlays, that I don't really need
to maintain a list, but I'm still asking because I'm curious.

I'm adding and removing markers on the fringe, and saving the respective
overlays in a list.  When I have to remove a marker, I use cl-delete-if
to remove the correct overlay from the list (while also doing the
side-effect of `delete-overlay').  The thing is, the removed overlays
are still present in the list.

The following is a proof-of-code to better explain what I mean:


(require 'cl-lib)

(defvar foo--overlays nil
  "List of active overlays.")

(defun foo--delete-overlay-at (point)
  "t if an overlay was deleted."
  (let (delp)
    (cl-delete-if (lambda (overlay)
                    (when (= point (overlay-start overlay))
                      (delete-overlay overlay)
                      (setq delp t)))
                  foo--overlays)
    delp))

(defun foo-toggle-this-line ()
  "Add/remove a marker on the current line."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (unless (foo--delete-overlay-at (point))
      (let ((overlay (make-overlay (point) (point))))
        (overlay-put overlay 'before-string
                     (propertize "A"
                                 'display '(left-fringe
                                            right-triangle)))
        (push overlay foo--overlays)))))

(provide 'foo)
;;; foo.el ends here


Try to add a marker on a line with (foo-toggle-this-line) and then
remove it: you'll find that `foo--overlays' is not nil.

(You'll actually get an error if you play with it too much, because for
deleted overlays `overlay-start' returns nil and that makes = raise an
error)

Why is cl-delete-if not removing items from the list?  (setq delp t)
evaluates to t, so it should delete.  cl-delete-if a destructive
function.  This is what I'm not getting.

Thanks,

Omar Polo



reply via email to

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