[Top][All Lists]

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

Text property searching

From: Lars Ingebrigtsen
Subject: Text property searching
Date: Mon, 16 Apr 2018 00:56:00 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

I've suggested a few times before that it would be nice to have search
functions that are... nicer... than the ones we have now
(`text-property-any' and `next-single-property-change'), and the
maintainer(s) at the time said "sure".  I think.

But I never implemented that wonderful function, because I could never
decide what it would look like.

But last night I think I got it: It should be just like search-forward,
only not.  (Hm.  I'm feeling a slight sense of deja vu while typing
this -- have I had this revelation before but forgotten about it?)


Let's say you have a region in the buffer that has the text property
`shr-url' with the value "http://fsf.org/";, then:

(text-property-search-forward 'shr-url "http://fsf.org/"; t)

would place point at the end of that region, and `match-beginning' and
`match-end' would point to the start and end.

The `t' there is the predicate: `t' means "equal", `nil' means "not
equal", and then you can write your own predicates for other uses.

So, to collect all urls from text properties, you'd write:

(while (text-property-search-forward 'shr-url nil nil)
  (push (get-text-property (match-beginning 0) 'shr-url) urls))

and that's it.  Or to collect all images:

(while (text-property-search-forward 'display 'image
                                     (lambda (elem val)
                                       (and (consp elem)
                                            (eq (car elem) val))))
  (push (plist-get (cdr (get-text-property (match-beginning 0) 'display)) :data)

Does this look OK to everybody?

(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no

reply via email to

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