all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Omar Polo <op@omarpolo.com>
To: help-gnu-emacs@gnu.org
Subject: cl-delete-if vs list of overlays
Date: Wed, 09 Dec 2020 10:53:26 +0100	[thread overview]
Message-ID: <87zh2nfgq1.fsf@omarpolo.com> (raw)

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	other threads:[~2020-12-09  9:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-09  9:53 Omar Polo [this message]
2020-12-09 10:17 ` cl-delete-if vs list of overlays Thien-Thi Nguyen
2020-12-09 11:05   ` Omar Polo
2020-12-09 16:14     ` Drew Adams
2020-12-09 18:16     ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zh2nfgq1.fsf@omarpolo.com \
    --to=op@omarpolo.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.