aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2020-12-11 14:40:20 +0100
committerLars Ingebrigtsen2020-12-11 14:40:20 +0100
commitaa7e5ce651b1872180e8da94ac80fbc25e33eec0 (patch)
treed7a50705c11b9257683743857ea479b2d0739d1c
parent9d598ef93cbebe59f1d3a91f4fda35d3e00f36a9 (diff)
downloademacs-aa7e5ce651b1872180e8da94ac80fbc25e33eec0.tar.gz
emacs-aa7e5ce651b1872180e8da94ac80fbc25e33eec0.zip
Add new function `object-intervals'
* doc/lispref/text.texi (Examining Properties): Document it. * src/fns.c (Fobject_intervals): New defun. (collect_interval): New function.
-rw-r--r--doc/lispref/text.texi16
-rw-r--r--etc/NEWS6
-rw-r--r--src/fns.c35
-rw-r--r--test/src/fns-tests.el16
4 files changed, 73 insertions, 0 deletions
diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi
index c6ca4eed2e1..b712768a213 100644
--- a/doc/lispref/text.texi
+++ b/doc/lispref/text.texi
@@ -2931,6 +2931,22 @@ used instead. Here is an example:
2931@end example 2931@end example
2932@end defvar 2932@end defvar
2933 2933
2934@defun object-intervals OBJECT
2935This function returns a copy of the intervals (i.e., text properties)
2936in @var{object} as a list of intervals. @var{object} must be a string
2937or a buffer. Altering the structure of this list does not change the
2938intervals in the object.
2939
2940@example
2941(object-intervals (propertize "foo" 'face 'bold))
2942 @result{} ((0 3 (face bold)))
2943@end example
2944
2945Each element in the returned list represents one interval. Each
2946interval has three parts: The first is the start, the second is the
2947end, and the third part is the text property itself.
2948@end defun
2949
2934@node Changing Properties 2950@node Changing Properties
2935@subsection Changing Text Properties 2951@subsection Changing Text Properties
2936@cindex changing text properties 2952@cindex changing text properties
diff --git a/etc/NEWS b/etc/NEWS
index befcf08cec3..1640e277987 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1389,6 +1389,12 @@ that makes it a valid button.
1389 1389
1390** Miscellaneous 1390** Miscellaneous
1391 1391
1392+++
1393*** New function 'object-intervals'.
1394This function returns a copy of the list of intervals (i.e., text
1395properties) in the object in question (which must either be a string
1396or a buffer).
1397
1392--- 1398---
1393*** 'hexl-mode' scrolling commands now heed 'next-screen-context-lines'. 1399*** 'hexl-mode' scrolling commands now heed 'next-screen-context-lines'.
1394Previously, 'hexl-scroll-down' and 'hexl-scroll-up' would scroll 1400Previously, 'hexl-scroll-down' and 'hexl-scroll-up' would scroll
diff --git a/src/fns.c b/src/fns.c
index e9b6a96f344..a0c4a1fbf1a 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5573,6 +5573,40 @@ Case is always significant and text properties are ignored. */)
5573 5573
5574 return make_int (string_byte_to_char (haystack, res - SSDATA (haystack))); 5574 return make_int (string_byte_to_char (haystack, res - SSDATA (haystack)));
5575} 5575}
5576
5577static void
5578collect_interval (INTERVAL interval, Lisp_Object collector)
5579{
5580 nconc2 (collector,
5581 list1(list3 (make_fixnum (interval->position),
5582 make_fixnum (interval->position + LENGTH (interval)),
5583 interval->plist)));
5584}
5585
5586DEFUN ("object-intervals", Fobject_intervals, Sobject_intervals, 1, 1, 0,
5587 doc: /* Return a copy of the text properties of OBJECT.
5588OBJECT must be a buffer or a string.
5589
5590Altering this copy does not change the layout of the text properties
5591in OBJECT. */)
5592 (register Lisp_Object object)
5593{
5594 Lisp_Object collector = Fcons (Qnil, Qnil);
5595 INTERVAL intervals;
5596
5597 if (STRINGP (object))
5598 intervals = string_intervals (object);
5599 else if (BUFFERP (object))
5600 intervals = buffer_intervals (XBUFFER (object));
5601 else
5602 wrong_type_argument (Qbuffer_or_string_p, object);
5603
5604 if (! intervals)
5605 return Qnil;
5606
5607 traverse_intervals (intervals, 0, collect_interval, collector);
5608 return CDR (collector);
5609}
5576 5610
5577 5611
5578void 5612void
@@ -5614,6 +5648,7 @@ syms_of_fns (void)
5614 defsubr (&Smaphash); 5648 defsubr (&Smaphash);
5615 defsubr (&Sdefine_hash_table_test); 5649 defsubr (&Sdefine_hash_table_test);
5616 defsubr (&Sstring_search); 5650 defsubr (&Sstring_search);
5651 defsubr (&Sobject_intervals);
5617 5652
5618 /* Crypto and hashing stuff. */ 5653 /* Crypto and hashing stuff. */
5619 DEFSYM (Qiv_auto, "iv-auto"); 5654 DEFSYM (Qiv_auto, "iv-auto");
diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el
index 86b8d655d26..14c0437d5f0 100644
--- a/test/src/fns-tests.el
+++ b/test/src/fns-tests.el
@@ -983,3 +983,19 @@
983 (should (equal (string-search (string-to-multibyte "o\303\270") "foo\303\270") 983 (should (equal (string-search (string-to-multibyte "o\303\270") "foo\303\270")
984 2)) 984 2))
985 (should (equal (string-search "\303\270" "foo\303\270") 3))) 985 (should (equal (string-search "\303\270" "foo\303\270") 3)))
986
987(ert-deftest object-intervals ()
988 (should (equal (object-intervals (propertize "foo" 'bar 'zot))
989 ((0 3 (bar zot)))))
990 (should (equal (object-intervals (concat (propertize "foo" 'bar 'zot)
991 (propertize "foo" 'gazonk "gazonk")))
992 ((0 3 (bar zot)) (3 6 (gazonk "gazonk")))))
993 (should (equal
994 (with-temp-buffer
995 (insert "foobar")
996 (put-text-property 1 3 'foo 1)
997 (put-text-property 3 6 'bar 2)
998 (put-text-property 2 5 'zot 3)
999 (object-intervals (current-buffer)))
1000 ((0 1 (foo 1)) (1 2 (zot 3 foo 1)) (2 4 (zot 3 bar 2))
1001 (4 5 (bar 2)) (5 6 nil)))))