[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/compat a28ce55d8e 1/4: compat-29: Add with-narrowing
From: |
ELPA Syncer |
Subject: |
[elpa] externals/compat a28ce55d8e 1/4: compat-29: Add with-narrowing |
Date: |
Wed, 18 Jan 2023 02:57:31 -0500 (EST) |
branch: externals/compat
commit a28ce55d8efd3c25c0c0daff93008e626d4bf678
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
compat-29: Add with-narrowing
---
NEWS.org | 1 +
compat-29.el | 17 +++++++++++++++++
compat-tests.el | 10 ++++++++++
compat.texi | 11 +++++++++++
4 files changed, 39 insertions(+)
diff --git a/NEWS.org b/NEWS.org
index a8ba33d778..985fb71100 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -12,6 +12,7 @@
- compat-29: Add ~plistp~.
- compat-29: Add ~list-of-strings-p~.
- compat-29: Add ~delete-line~.
+- compat-29: Add ~with-narrowing~.
* Release of "Compat" Version 29.1.2.0
diff --git a/compat-29.el b/compat-29.el
index 9373c0ce62..4010a9ee0f 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -187,6 +187,23 @@ This function does not move point. Also see
`line-end-position'."
"Delete the current line."
(delete-region (pos-bol) (pos-bol 2)))
+(compat-defmacro with-narrowing (start end &rest rest)
+ "Execute BODY with restrictions set to START and END.
+
+The current restrictions, if any, are restored upon return.
+
+With the optional :locked TAG argument, inside BODY,
+`narrow-to-region' and `widen' can be used only within the START
+and END limits, unless the restrictions are unlocked by calling
+`narrowing-unlock' with TAG. See `narrowing-lock' for a more
+detailed description.
+
+\(fn START END [:locked TAG] BODY)"
+ `(save-restriction
+ (narrow-to-region ,start ,end)
+ ;; Locking is ignored
+ ,@(if (eq (car rest) :locked) (cddr rest) rest)))
+
(compat-defmacro with-memoization (place &rest code) ;;
<compat-tests:with-memoization>
"Return the value of CODE and stash it in PLACE.
If PLACE's value is non-nil, then don't bother evaluating CODE
diff --git a/compat-tests.el b/compat-tests.el
index 1b61570af8..9b787db94b 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -211,6 +211,16 @@
(should-equal 'h (get-text-property 2 'help-echo))
(should-equal 'h (get-text-property 6 'help-echo))))
+(ert-deftest with-narrowing ()
+ (with-temp-buffer
+ (insert "abc")
+ (with-narrowing 2 3 :locked 'foo
+ (should-equal "b" (buffer-string)))
+ (should-equal "abc" (buffer-string))
+ (with-narrowing 2 3
+ (should-equal "b" (buffer-string)))
+ (should-equal "abc" (buffer-string))))
+
(ert-deftest with-memoization ()
(let ((x (cons nil nil)) y computed)
(with-memoization (car x)
diff --git a/compat.texi b/compat.texi
index bb86925125..61a9fc7aeb 100644
--- a/compat.texi
+++ b/compat.texi
@@ -2034,6 +2034,17 @@ evaluated and then stashed in @var{place}. If
@var{place}'s value is
non-@code{nil}, return that value instead of evaluating @var{code}.
@end defmac
+@c based on lisp/subr.el
+@defmac with-narrowing start end [:locked tag] &rest body
+Execute @var{body} with restrictions set to @var{start} and @var{end}.
+The current restrictions, if any, are restored upon return. With the
+optional :locked @var{tag} argument, inside @var{tag},
+@code{narrow-to-region} and @code{widen} can be used only within the
+@var{start} and @var{end} limits, unless the restrictions are unlocked
+by calling @code{narrowing-unlock} with @var{tag}. See
+@code{narrowing-lock} for a more detailed description.
+@end defmac
+
@c copied from lispref/positions.texi
@defun pos-bol &optional count
Like @code{line-beginning-position}, but ignores fields (and is more