all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Marcin Borkowski <mbork@mbork.pl>
To: help-gnu-emacs@gnu.org
Subject: Re: What happens with overlays when their buffer is killed?
Date: Mon, 26 Mar 2018 18:44:04 +0200	[thread overview]
Message-ID: <87zi2uzt1a.fsf@mbork.pl> (raw)
In-Reply-To: <83k1tyg9mr.fsf@gnu.org>


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



  reply	other threads:[~2018-03-26 16:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2018-03-26 17:55 ` Stefan Monnier

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=87zi2uzt1a.fsf@mbork.pl \
    --to=mbork@mbork.pl \
    --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.