aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2021-11-24 19:38:41 +0100
committerLars Ingebrigtsen2021-11-24 19:38:41 +0100
commitfde9363a57d0d38d592122fe5ca01aaafd0afa52 (patch)
tree0f80c2aa5effa3bea4248d6c5a741e23dc75b93b
parent34f2878ce25a74c1283266b67575a56554684be5 (diff)
downloademacs-fde9363a57d0d38d592122fe5ca01aaafd0afa52.tar.gz
emacs-fde9363a57d0d38d592122fe5ca01aaafd0afa52.zip
Add new function 'add-display-text-property'
* doc/lispref/display.texi (Display Property): Document it. * lisp/emacs-lisp/subr-x.el (add-display-text-property): New function.
-rw-r--r--doc/lispref/display.texi25
-rw-r--r--etc/NEWS7
-rw-r--r--lisp/emacs-lisp/subr-x.el45
-rw-r--r--test/lisp/emacs-lisp/subr-x-tests.el18
4 files changed, 95 insertions, 0 deletions
diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi
index fdebba939be..7204581e407 100644
--- a/doc/lispref/display.texi
+++ b/doc/lispref/display.texi
@@ -4904,6 +4904,31 @@ with @code{get-char-property}, for instance (@pxref{Examining
4904Properties}). 4904Properties}).
4905@end defun 4905@end defun
4906 4906
4907@defun add-display-text-property start end prop value &optional append object
4908Add @code{display} property @var{prop} of @var{value} to the text from
4909@var{start} to @var{end}.
4910
4911If any text in the region has a non-@code{nil} @code{display}
4912property, those properties are retained. For instance:
4913
4914@lisp
4915(add-display-text-property 4 8 'height 2.0)
4916(add-display-text-property 2 12 'raise 0.5)
4917@end lisp
4918
4919After doing this, the region from 2 to 4 will have the @code{raise}
4920@code{display} property, the region from 4 to 8 will have both the
4921@code{raise} and @code{height} @code{display} properties, and finally
4922the region from 8 to 12 will only have the @code{raise} @code{display}
4923property.
4924
4925If @var{append} is non-@code{nil}, append to the list of display
4926properties; otherwise prepend.
4927
4928If @var{object} is non-@code{nil}, it should be a string or a buffer.
4929If @code{nil}, this defaults to the current buffer.
4930@end defun
4931
4907@cindex display property, unsafe evaluation 4932@cindex display property, unsafe evaluation
4908@cindex security, and display specifications 4933@cindex security, and display specifications
4909 Some of the display specifications allow inclusion of Lisp forms, 4934 Some of the display specifications allow inclusion of Lisp forms,
diff --git a/etc/NEWS b/etc/NEWS
index 24b8cb27961..8b7c2f78508 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -676,10 +676,17 @@ Use 'exif-parse-file' and 'exif-field' instead.
676 676
677* Lisp Changes in Emacs 29.1 677* Lisp Changes in Emacs 29.1
678 678
679+++
679** New function 'get-display-property'. 680** New function 'get-display-property'.
680This is like 'get-text-property', but works on the 'display' text 681This is like 'get-text-property', but works on the 'display' text
681property. 682property.
682 683
684+++
685** New function 'add-text-display-property'.
686This is like 'put-text-property', but works on the 'display' text
687property.
688
689+++
683** New 'min-width' 'display' property. 690** New 'min-width' 'display' property.
684This allows setting a minimum display width for a region of text. 691This allows setting a minimum display width for a region of text.
685 692
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 95254b946e5..3ec880f8b8f 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -469,6 +469,51 @@ This takes into account combining characters and grapheme clusters."
469 (setq start (1+ start)))) 469 (setq start (1+ start))))
470 (nreverse result))) 470 (nreverse result)))
471 471
472;;;###autoload
473(defun add-display-text-property (start end prop value
474 &optional append object)
475 "Add display property PROP with VALUE to the text from START to END.
476If any text in the region has a non-nil `display' property, those
477properties are retained.
478
479If APPEND is non-nil, append to the list of display properties;
480otherwise prepend.
481
482If OBJECT is non-nil, it should be a string or a buffer. If nil,
483this defaults to the current buffer."
484 (let ((sub-start start)
485 (sub-end 0)
486 disp)
487 (while (< sub-end end)
488 (setq sub-end (next-single-property-change sub-start 'display object
489 (if (stringp object)
490 (min (length object) end)
491 (min end (point-max)))))
492 (if (not (setq disp (get-text-property sub-start 'display object)))
493 ;; No old properties in this range.
494 (put-text-property sub-start sub-end 'display (list prop value))
495 ;; We have old properties.
496 (let ((vector nil))
497 ;; Make disp into a list.
498 (setq disp
499 (cond
500 ((vectorp disp)
501 (setq vector t)
502 (seq-into disp 'list))
503 ((not (consp (car disp)))
504 (list disp))
505 (t
506 disp)))
507 (setq disp
508 (if append
509 (append disp (list (list prop value)))
510 (append (list (list prop value)) disp)))
511 (when vector
512 (setq disp (seq-into disp 'vector)))
513 ;; Finally update the range.
514 (put-text-property sub-start sub-end 'display disp)))
515 (setq sub-start sub-end))))
516
472(provide 'subr-x) 517(provide 'subr-x)
473 518
474;;; subr-x.el ends here 519;;; subr-x.el ends here
diff --git a/test/lisp/emacs-lisp/subr-x-tests.el b/test/lisp/emacs-lisp/subr-x-tests.el
index f9cfea888c7..69d59e84f6d 100644
--- a/test/lisp/emacs-lisp/subr-x-tests.el
+++ b/test/lisp/emacs-lisp/subr-x-tests.el
@@ -676,5 +676,23 @@
676 (buffer-string)) 676 (buffer-string))
677 "foo\n"))) 677 "foo\n")))
678 678
679(ert-deftest test-add-display-text-property ()
680 (with-temp-buffer
681 (insert "Foo bar zot gazonk")
682 (add-display-text-property 4 8 'height 2.0)
683 (add-display-text-property 2 12 'raise 0.5)
684 (should (equal (get-text-property 2 'display) '(raise 0.5)))
685 (should (equal (get-text-property 5 'display)
686 '((raise 0.5) (height 2.0))))
687 (should (equal (get-text-property 9 'display) '(raise 0.5))))
688 (with-temp-buffer
689 (insert "Foo bar zot gazonk")
690 (put-text-property 4 8 'display [(height 2.0)])
691 (add-display-text-property 2 12 'raise 0.5)
692 (should (equal (get-text-property 2 'display) '(raise 0.5)))
693 (should (equal (get-text-property 5 'display)
694 [(raise 0.5) (height 2.0)]))
695 (should (equal (get-text-property 9 'display) '(raise 0.5)))))
696
679(provide 'subr-x-tests) 697(provide 'subr-x-tests)
680;;; subr-x-tests.el ends here 698;;; subr-x-tests.el ends here