Subject: bug#56682: feature/improved-locked-narrowing 9dee6df39c: Reworked locked narrowing.

From: Gregory Heytings <[email-masked]>
Date: Sat, 18 Feb 2023 23:12:27 +0000

>>> Yes: that's what the "record_unwind_protect (save_restriction_restore, 
>>> save_restriction_save ());" does.
>> 
>> So save_restriction_restore will reinstate the buffer in the 
>> narrowing_locks list?
>
> Yes:
>
> Lisp_Object
> save_restriction_save (void)
> {
>  Lisp_Object restr = save_restriction_save_1 ();
>  Lisp_Object locks = narrowing_locks_save ();
>  return Fcons (restr, locks);
> }
>
> void
> save_restriction_restore (Lisp_Object data)
> {
>  narrowing_locks_restore (XCDR (data));
>  save_restriction_restore_1 (XCAR (data));
> }
>

Eli, do you have further comments or objections, or can I install this? 
(Patch attached again for your convenience.)

BTW, I also would like to rename the narrowing_locks_* functions to make 
their names coherent with your change of the name of the macro from 
'with-narrowing' to 'with-restriction'.  I guess that's okay?
From c482389fd59edcbc738a4d48ca6b65d14af60b25 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Tue, 14 Feb 2023 22:41:14 +0000
Subject: [PATCH] Remove narrowing locks before calling Fwiden

* src/editfns.c (narrowing_locks_remove_in_current_buffer) : New
function.

* src/lisp.h: Make it externally visible.

* src/xdisp.c (display_count_lines_logically):
* src/lread.c (readevalloop):
* src/indent.c (line_number_display_width):
* src/fileio.c (write_region):
* src/callproc.c (Fcall_process_region):
* src/buffer.c (Ferase_buffer): Use it.
---
 src/buffer.c   | 1 +
 src/callproc.c | 1 +
 src/editfns.c  | 7 +++++++
 src/fileio.c   | 1 +
 src/indent.c   | 1 +
 src/lisp.h     | 1 +
 src/lread.c    | 1 +
 src/xdisp.c    | 1 +
 8 files changed, 14 insertions(+)

diff --git a/src/buffer.c b/src/buffer.c
index df1f5206668..a9d004d8727 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -2386,6 +2386,7 @@ DEFUN ("erase-buffer", Ferase_buffer, Serase_buffer, 0, 0, "*",
 so the buffer is truly empty after this.  */)
   (void)
 {
+  narrowing_locks_remove_in_current_buffer ();
   Fwiden ();
 
   del_range (BEG, Z);
diff --git a/src/callproc.c b/src/callproc.c
index 5e1e1a8cc0a..c144f6e66d9 100644
--- a/src/callproc.c
+++ b/src/callproc.c
@@ -1113,6 +1113,7 @@ t (mix it with ordinary output), or a file name string.
         {
           /* No need to save restrictions since we delete everything
              anyway.  */
+          narrowing_locks_remove_in_current_buffer ();
           Fwiden ();
           del_range (BEG, Z);
         }
diff --git a/src/editfns.c b/src/editfns.c
index f83c5c7259b..32f654af8c3 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -2814,6 +2814,13 @@ narrowing_locks_restore (Lisp_Object buf_and_saved_locks)
     narrowing_locks_add (buf, saved_locks);
 }
 
+/* Remove all narrowing locks in the current buffer.  */
+void
+narrowing_locks_remove_in_current_buffer (void)
+{
+  narrowing_locks_remove (Fcurrent_buffer ());
+}
+
 static void
 unwind_narrow_to_region_locked (Lisp_Object tag)
 {
diff --git a/src/fileio.c b/src/fileio.c
index f00c389a520..cf5a4ecf80a 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -5269,6 +5269,7 @@ write_region (Lisp_Object start, Lisp_Object end, Lisp_Object filename,
     }
 
   record_unwind_protect (save_restriction_restore, save_restriction_save ());
+  narrowing_locks_remove_in_current_buffer ();
 
   /* Special kludge to simplify auto-saving.  */
   if (NILP (start))
diff --git a/src/indent.c b/src/indent.c
index 6de18d749ca..5f6dc47f034 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -2065,6 +2065,7 @@ line_number_display_width (struct window *w, int *width, int *pixel_width)
 	{
 	  record_unwind_protect (save_restriction_restore,
 				 save_restriction_save ());
+	  narrowing_locks_remove_in_current_buffer ();
 	  Fwiden ();
 	  saved_restriction = true;
 	}
diff --git a/src/lisp.h b/src/lisp.h
index 1276285e2f2..637ede7051e 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -4689,6 +4689,7 @@ XMODULE_FUNCTION (Lisp_Object o)
 					    ptrdiff_t, bool);
 extern void narrow_to_region_locked (Lisp_Object, Lisp_Object, Lisp_Object);
 extern void reset_outermost_narrowings (void);
+extern void narrowing_locks_remove_in_current_buffer (void);
 extern void init_editfns (void);
 extern void syms_of_editfns (void);
 
diff --git a/src/lread.c b/src/lread.c
index d0dc85f51c8..28ca483b819 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -2255,6 +2255,7 @@ readevalloop (Lisp_Object readcharfun,
 	  record_unwind_protect_excursion ();
 	  /* Save ZV in it.  */
 	  record_unwind_protect (save_restriction_restore, save_restriction_save ());
+	  narrowing_locks_remove_in_current_buffer ();
 	  /* Those get unbound after we read one expression.  */
 
 	  /* Set point and ZV around stuff to be read.  */
diff --git a/src/xdisp.c b/src/xdisp.c
index 5c5ecaa2bcb..6f2b4e6cd41 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -24111,6 +24111,7 @@ display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
   ptrdiff_t val;
   specpdl_ref pdl_count = SPECPDL_INDEX ();
   record_unwind_protect (save_restriction_restore, save_restriction_save ());
+  narrowing_locks_remove_in_current_buffer ();
   Fwiden ();
   val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
   unbind_to (pdl_count, Qnil);
-- 
2.39.0