diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2023-01-18 08:10:53 +0100 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2023-01-18 08:37:33 +0100 |
| commit | a28ce55d8efd3c25c0c0daff93008e626d4bf678 (patch) | |
| tree | 4aeda8fde2583505d347636f936c714a710122a3 | |
| parent | 12e2d82a5a469aabadb1082678f7a33539779d8b (diff) | |
compat-29: Add with-narrowing
| -rw-r--r-- | NEWS.org | 1 | ||||
| -rw-r--r-- | compat-29.el | 17 | ||||
| -rw-r--r-- | compat-tests.el | 10 | ||||
| -rw-r--r-- | compat.texi | 11 |
4 files changed, 39 insertions, 0 deletions
@@ -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 9373c0c..4010a9e 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 1b61570..9b787db 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 bb86925..61a9fc7 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 |
