branch: externals/substitute
commit 701b965a6fbdfdbcba32439f6d86ba97f99d235d
Author: Protesilaos Stavrou <[email protected]>
Commit: Protesilaos Stavrou <[email protected]>

    Add user option to control if narrowing is respected while substituting
    
    Thanks to zauberen for reporting than the previous implementation
    would break if some targets were outside the narrowed portion of the
    buffer. This was done in issue 11:  
<https://github.com/protesilaos/substitute/issues/11>.
    I am fixing that and also providing the new user option.
---
 README.org    | 11 ++++++++++-
 substitute.el | 28 ++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/README.org b/README.org
index 5569fc5b90..3fa2938825 100644
--- a/README.org
+++ b/README.org
@@ -224,6 +224,14 @@ To always have fixed letter casing, set the user option
 same as always calling the aforementioned commands with a prefix
 argument.
 
+#+vindex: substitute-ignore-narrowing
+Every substitution is set up to ignore narrowing restrictions by
+default. This is controlled by the user option ~substitute-ignore-narrowing~.
+What this means is that a command such as ~substitute-target-in-buffer~
+will make changes across the buffer even if narrowing is in effect.
+Users who prefer to operate only within the narrowed portion of the
+buffer must set ~substitute-ignore-narrowing~ to ~nil~. [ Part of 
{{{development-version}}}. ]
+
 #+vindex: substitute-post-replace-functions
 #+findex: substitute-report-operation
 Every substitution triggers the ~substitute-post-replace-functions~.
@@ -269,7 +277,8 @@ matters.
 + Contributions to code or the manual :: Ed Tavinor, Kostas Andreadis,
   Wang Chunye.
 
-+ Ideas and/or user feedback :: Tomasz Hołubowicz, ersi-dnd, revrari.
++ Ideas and/or user feedback :: Tomasz Hołubowicz, ersi-dnd, revrari,
+  zauberen.
 
 * COPYING
 :PROPERTIES:
diff --git a/substitute.el b/substitute.el
index 4609dcdca4..7235258cb6 100644
--- a/substitute.el
+++ b/substitute.el
@@ -77,6 +77,18 @@ For a reference function, see `substitute-report-operation'."
   :group 'substitute
   :type 'hook)
 
+(defcustom substitute-ignore-narrowing t
+  "When non-nil, ignore any narrowing restrictions.
+In other words, perform the substitution in the scope it would have
+applied to if narrowing was not in effect.  For example, the command
+`substitute-target-in-buffer' will work across the entire buffer.
+
+When nil, respect the narrowing, such that changes only apply to the
+narrowed portion of the buffer."
+  :package-version '(substitute . "0.5.0")
+  :group 'substitute
+  :type 'boolean)
+
 (defface substitute-match
   `((t :inherit ,(if-let* ((face 'lazy-highlight)
                            (_ (facep face)))
@@ -119,7 +131,9 @@ and related."
 
 (defun substitute--remove-highlights ()
   "Remove `substitute-match' overlays."
-  (remove-overlays nil nil 'face 'substitute-match))
+  (save-restriction
+    (widen)
+    (remove-overlays nil nil 'face 'substitute-match)))
 
 (defun substitute--add-highlight (beg end)
   "Add overlay of `substitute-match' between BEG and END positions."
@@ -164,9 +178,14 @@ Pass to it the TARGET and SCOPE arguments."
    target
    scope))
 
+(defun substitute--widen ()
+  "Do `widen' if `substitute-ignore-narrowing' is non-nil."
+  (when substitute-ignore-narrowing
+    (widen)))
+
 (defun substitute--scope-current-and-below (target)
   "Position point to match current TARGET and below."
-  (widen)
+  (substitute--widen)
   (if-let* ((_ (region-active-p))
             (bounds (region-bounds)))
       (goto-char (caar bounds))
@@ -175,7 +194,7 @@ Pass to it the TARGET and SCOPE arguments."
 
 (defun substitute--scope-current-and-above (target)
   "Position point to match current TARGET and above."
-  (widen)
+  (substitute--widen)
   (if-let* ((_ (region-active-p))
             (bounds (region-bounds)))
       (goto-char (cdar bounds))
@@ -198,7 +217,7 @@ Pass to it the TARGET and SCOPE arguments."
 
 (defun substitute--scope-top-of-buffer ()
   "Position point to the top of the buffer."
-  (widen)
+  (substitute--widen)
   (goto-char (point-min)))
 
 (defun substitute--get-bounds (regexp position)
@@ -327,6 +346,7 @@ upcasing based on the target text.  See the documenation of
       (when (listp buffer-undo-list)
         (push (point) buffer-undo-list))
       (save-restriction
+        (substitute--widen)
         (mapcar
          (lambda (target)
            (pcase-let* ((`(,string ,beg ,end) target)

Reply via email to