all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* What happens with overlays when their buffer is killed?
@ 2018-03-26  7:25 Marcin Borkowski
  2018-03-26 15:05 ` Eli Zaretskii
  2018-03-26 17:55 ` Stefan Monnier
  0 siblings, 2 replies; 4+ messages in thread
From: Marcin Borkowski @ 2018-03-26  7:25 UTC (permalink / raw
  To: Help Gnu Emacs mailing list

Hi list,

assume that I create a few overlays in some buffer, and then I kill that
buffer.  What happens with the overlays?  If they were not assigned to
a variable, do they get garbage collected?

TIA,

--
Marcin Borkowski
http://mbork.pl



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

* Re: What happens with overlays when their buffer is killed?
  2018-03-26  7:25 What happens with overlays when their buffer is killed? Marcin Borkowski
@ 2018-03-26 15:05 ` Eli Zaretskii
  2018-03-26 16:44   ` Marcin Borkowski
  2018-03-26 17:55 ` Stefan Monnier
  1 sibling, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2018-03-26 15:05 UTC (permalink / raw
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Date: Mon, 26 Mar 2018 09:25:27 +0200
> 
> assume that I create a few overlays in some buffer, and then I kill that
> buffer.  What happens with the overlays?  If they were not assigned to
> a variable, do they get garbage collected?

They will be GC'ed on the next opportunity when Emacs decides to GC.



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

* Re: What happens with overlays when their buffer is killed?
  2018-03-26 15:05 ` Eli Zaretskii
@ 2018-03-26 16:44   ` Marcin Borkowski
  0 siblings, 0 replies; 4+ messages in thread
From: Marcin Borkowski @ 2018-03-26 16:44 UTC (permalink / raw
  To: help-gnu-emacs


On 2018-03-26, at 17:05, Eli Zaretskii <eliz@gnu.org> wrote:

>> From: Marcin Borkowski <mbork@mbork.pl>
>> Date: Mon, 26 Mar 2018 09:25:27 +0200
>>
>> assume that I create a few overlays in some buffer, and then I kill that
>> buffer.  What happens with the overlays?  If they were not assigned to
>> a variable, do they get garbage collected?
>
> They will be GC'ed on the next opportunity when Emacs decides to GC.

Thanks, that's great!

Here is my code displaying the size of the attachment in message-mode
and highlighting the "important" parts of the mml tag.  (I will probably
write about it on my blog some day.)

--8<---------------cut here---------------start------------->8---
(require 'cl)

(defconst iec-prefixes ["" "Ki" "Mi" "Gi"])

(defun human-readable-size (size)
  "Return SIZE as human-readable string, using IEC prefixes."
  (let* ((order (1- (max 1 (ceiling (log (max size 1) 1024)))))
	 (prefix (elt iec-prefixes (min order (length iec-prefixes))))
	 (size-in-unit (/ size (expt 1024.0 order)))
	 (precision (max 3 (+ 2 (floor (log (max size-in-unit 1) 10)))))
	 (size-str (format (format "%%.%dg%%sB" precision) size-in-unit prefix)))
    size-str))

(defun mml-add-size-to-message-part (name &rest plist)
  "Add filesize to the inserted part if applicable"
  (if (and (eq name 'part)
	   (plist-member plist 'filename))
      (plist-put plist 'size
		 (number-to-string
		  (file-attribute-size
		   (file-attributes
		    (plist-get plist filename))))))
  (cons name plist))

(advice-add 'mml-insert-empty-tag
	    :filter-args
	    'mml-add-size-to-message-part)

(defun embolden-with-overlay (start end)
  "Embolden the text between START and END, using an overlay."
  (let ((tmp-overlay (make-overlay start end)))
    (overlay-put tmp-overlay 'face 'bold)))

(defun make-mml-tag-before-point-more-legible (name &rest plist)
  "Apply boldface to relevant parts of mml tag before point."
  (save-excursion
    (let ((opoint (point)))
      (when (search-backward (format "<#%s" name) nil nil)
	(goto-char (match-end 0))
	(while (looking-at " [a-z]+=")
	  (goto-char (match-end 0))
	  (if (eq (char-after) ?\")
	      (embolden-with-overlay
	       (progn
		 (forward-char)
		 (point))
	       (progn (while (and (search-forward "\"" opoint t)
				  (save-excursion
				    (backward-char)
				    (cl-oddp (skip-chars-backward "\\\\")))))
		      (point)))
	    (embolden-with-overlay
	     (point)
	     (progn
	       (re-search-forward "[ >]" opoint t)
	       (backward-char)
	       (point)))))))))

(advice-add 'mml-insert-tag :after 'make-mml-tag-before-point-more-legible)
--8<---------------cut here---------------end--------------->8---

Comments welcome.

--
Marcin Borkowski
http://mbork.pl



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

* Re: What happens with overlays when their buffer is killed?
  2018-03-26  7:25 What happens with overlays when their buffer is killed? Marcin Borkowski
  2018-03-26 15:05 ` Eli Zaretskii
@ 2018-03-26 17:55 ` Stefan Monnier
  1 sibling, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2018-03-26 17:55 UTC (permalink / raw
  To: help-gnu-emacs

> assume that I create a few overlays in some buffer, and then I kill that
> buffer.  What happens with the overlays?

When a buffer is killed, all the markers (and overlays) it holds are
"deleted" (aka moved so they point to any buffer any more).  This is
equivalent to calling `delete-overlay` for overlays (and set-marker
with a nil value for markers).

> If they were not assigned to a variable, do they get
> garbage collected?

Yes.


        Stefan




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

end of thread, other threads:[~2018-03-26 17:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-26  7:25 What happens with overlays when their buffer is killed? Marcin Borkowski
2018-03-26 15:05 ` Eli Zaretskii
2018-03-26 16:44   ` Marcin Borkowski
2018-03-26 17:55 ` Stefan Monnier

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.