unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* cl-delete-if vs list of overlays
@ 2020-12-09  9:53 Omar Polo
  2020-12-09 10:17 ` Thien-Thi Nguyen
  0 siblings, 1 reply; 5+ messages in thread
From: Omar Polo @ 2020-12-09  9:53 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: cl-delete-if vs list of overlays
  2020-12-09  9:53 cl-delete-if vs list of overlays Omar Polo
@ 2020-12-09 10:17 ` Thien-Thi Nguyen
  2020-12-09 11:05   ` Omar Polo
  0 siblings, 1 reply; 5+ messages in thread
From: Thien-Thi Nguyen @ 2020-12-09 10:17 UTC (permalink / raw)
  To: Omar Polo; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 893 bytes --]


() Omar Polo <op@omarpolo.com>
() Wed, 09 Dec 2020 10:53:26 +0100

   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.

On my system, ‘C-h f cl-delete-if RET’ sez:

 This is a destructive function; it reuses the storage of SEQ
 whenever possible.

So, your question becomes: When is it NOT possible to reuse the
storage of SEQ in the process of deleting an item?

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (query)               ; (2020) Software Libero
   (pcase (context query)               ;       = Dissenso Etico
     (`(technical ,ml) (correctp ml))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 219 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: cl-delete-if vs list of overlays
  2020-12-09 10:17 ` 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
  0 siblings, 2 replies; 5+ messages in thread
From: Omar Polo @ 2020-12-09 11:05 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: help-gnu-emacs


Thien-Thi Nguyen <ttn@gnuvola.org> writes:

> () Omar Polo <op@omarpolo.com>
> () Wed, 09 Dec 2020 10:53:26 +0100
>
>    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.
>
> On my system, ‘C-h f cl-delete-if RET’ sez:
>
>  This is a destructive function; it reuses the storage of SEQ
>  whenever possible.
>
> So, your question becomes: When is it NOT possible to reuse the
> storage of SEQ in the process of deleting an item?

Oh my, I feel so stupid now...

I was under the (wrong) assumption that a destructive function will
always modify the list, when it's not the case.  I needed to setq the
result of cl-delete-if to the list itself to cover all the cases.

Thanks!



^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: cl-delete-if vs list of overlays
  2020-12-09 11:05   ` Omar Polo
@ 2020-12-09 16:14     ` Drew Adams
  2020-12-09 18:16     ` Thien-Thi Nguyen
  1 sibling, 0 replies; 5+ messages in thread
From: Drew Adams @ 2020-12-09 16:14 UTC (permalink / raw)
  To: Omar Polo, Thien-Thi Nguyen; +Cc: help-gnu-emacs

> I was under the (wrong) assumption that a destructive function will
> always modify the list, when it's not the case.  I needed to setq the
> result of cl-delete-if to the list itself to cover all the cases.

Yes.  When you pass a list-valued variable to a "destructive"
function you pretty much always want to set the variable to
the result.

`C-h i' Elisp manual `i destructive list operations'

And `g Sets and Lists' tells us, about using `delq':

  Don’t assume that a variable which formerly held the
  argument LIST now has fewer elements, or that it still
  holds the original list!  Instead, save the result of
  ‘delq’ and use that.  Most often we store the result
  back into the variable that held the original list:






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: cl-delete-if vs list of overlays
  2020-12-09 11:05   ` Omar Polo
  2020-12-09 16:14     ` Drew Adams
@ 2020-12-09 18:16     ` Thien-Thi Nguyen
  1 sibling, 0 replies; 5+ messages in thread
From: Thien-Thi Nguyen @ 2020-12-09 18:16 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 953 bytes --]


() Omar Polo <op@omarpolo.com>
() Wed, 09 Dec 2020 12:05:17 +0100

   Oh my, I feel so stupid now...

No worries.

   I was under the (wrong) assumption that a destructive
   function will always modify the list, when it's not the case.
   I needed to setq the result of cl-delete-if to the list
   itself to cover all the cases.

But then you wouldn't have had the opportunity to read the
docstring (again?).  :-D

   Thanks!

Emacs is self-documenting but unfortunately not self-explanatory
(yet).  Thanks go to the Emacs hackers who write docstrings.

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (query)               ; (2020) Software Libero
   (pcase (context query)               ;       = Dissenso Etico
     (`(technical ,ml) (correctp ml))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 219 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-12-09 18:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-09  9:53 cl-delete-if vs list of overlays Omar Polo
2020-12-09 10:17 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).