Re: [PATCH 2/7] emacs: Use text properties instead of overlays for tag coloring

Subject: Re: [PATCH 2/7] emacs: Use text properties instead of overlays for tag coloring

Date: Fri, 13 Jul 2012 14:43:08 -0400

To: Mark Walters

Cc: notmuch@notmuchmail.org

From: Austin Clements


Quoth Mark Walters on Jul 13 at  6:59 pm:
> On Fri, 13 Jul 2012, Austin Clements <amdragon@MIT.EDU> wrote:
> > Previously, tag-based search result highlighting was done by creating
> > an overlay over each search result.  However, overlays have annoying
> > front- and rear-advancement semantics that make it difficult to
> > manipulate text at their boundaries, which the next patch will do.
> > They also have performance problems (creating an overlay is linear in
> > the number of overlays between point and the new overlay, making
> > highlighting a search buffer quadratic in the number of results).
> >
> > Text properties have neither problem.  However, text properties make
> > it more difficult to apply multiple faces since, unlike with overlays,
> > a given character can only have a single 'face text property.  Hence,
> > we introduce a utility function that combines faces into any existing
> > 'face text properties.
> >
> > Using this utility function, it's straightforward to apply all of the
> > appropriate tag faces in notmuch-search-color-line.
> 
> I have some recollection of people talking about text properties as
> opposed to overlays some time ago but saying one problem was you
> couldn't do invisibility with text properties. Is that correct and is it
> a concern? (Otherwise it all looks good)

I believe the problem was that isearch can only automatically expand
invisible text that's marked invisible using overlays.  Regardless,
this shouldn't be an issue for this patch because invisibility and
faces are controlled by different text properties.  It also shouldn't
be a problem for the next patch because, while we do use overlays to
hide authors, that overlay should always be strictly within the search
result (even if it's at the end of the result format, we'll follow it
with a newline) and hence deleting the entire search result region
should also delete the author invisibility overlay.

> Best wishes
> 
> Mark
> > ---
> >  emacs/notmuch-lib.el |   15 +++++++++++++++
> >  emacs/notmuch.el     |   21 +++++++--------------
> >  2 files changed, 22 insertions(+), 14 deletions(-)
> >
> > diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
> > index aa25513..30db58f 100644
> > --- a/emacs/notmuch-lib.el
> > +++ b/emacs/notmuch-lib.el
> > @@ -269,6 +269,21 @@ current buffer, if possible."
> >    (loop for (key value . rest) on plist by #'cddr
> >  	collect (cons (intern (substring (symbol-name key) 1)) value)))
> >  
> > +(defun notmuch-combine-face-text-property (start end face)
> > +  "Combine FACE into the 'face text property between START and END.
> > +
> > +This function combines FACE with any existing faces between START
> > +and END.  Attributes specified by FACE take precedence over
> > +existing attributes.  FACE must be a face name (a symbol or
> > +string), a property list of face attributes, or a list of these."
> > +
> > +  (let ((pos start))
> > +    (while (< pos end)
> > +      (let ((cur (get-text-property pos 'face))
> > +	    (next (next-single-property-change pos 'face nil end)))
> > +	(put-text-property pos next 'face (cons face cur))
> > +	(setq pos next)))))
> > +
> >  ;; Compatibility functions for versions of emacs before emacs 23.
> >  ;;
> >  ;; Both functions here were copied from emacs 23 with the following copyright:
> > diff --git a/emacs/notmuch.el b/emacs/notmuch.el
> > index ef18927..82c148d 100644
> > --- a/emacs/notmuch.el
> > +++ b/emacs/notmuch.el
> > @@ -633,20 +633,13 @@ foreground and blue background."
> >  
> >  (defun notmuch-search-color-line (start end line-tag-list)
> >    "Colorize lines in `notmuch-show' based on tags."
> > -  ;; Create the overlay only if the message has tags which match one
> > -  ;; of those specified in `notmuch-search-line-faces'.
> > -  (let (overlay)
> > -    (mapc (lambda (elem)
> > -	    (let ((tag (car elem))
> > -		  (attributes (cdr elem)))
> > -	      (when (member tag line-tag-list)
> > -		(when (not overlay)
> > -		  (setq overlay (make-overlay start end)))
> > -		;; Merge the specified properties with any already
> > -		;; applied from an earlier match.
> > -		(overlay-put overlay 'face
> > -			     (append (overlay-get overlay 'face) attributes)))))
> > -	  notmuch-search-line-faces)))
> > +  (mapc (lambda (elem)
> > +	  (let ((tag (car elem))
> > +		(attributes (cdr elem)))
> > +	    (when (member tag line-tag-list)
> > +	      (notmuch-combine-face-text-property start end attributes))))
> > +	;; Reverse the list so earlier entries take precedence
> > +	(reverse notmuch-search-line-faces)))
> >  
> >  (defun notmuch-search-author-propertize (authors)
> >    "Split `authors' into matching and non-matching authors and

Thread: