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

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

Re: What happens with overlays when their buffer is killed?


From: Marcin Borkowski
Subject: Re: What happens with overlays when their buffer is killed?
Date: Mon, 26 Mar 2018 18:44:04 +0200
User-agent: mu4e 1.1.0; emacs 27.0.50

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 via email to

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