Subject: Re: bug#17401: 24.4.50; Narrow to {region,page,defun} in an indirect clone buffer in the other window

From: Lars Ingebrigtsen <[email-masked]>
Date: Fri, 18 Sep 2020 15:57:33 +0200

Phil Sainty <[email-masked]> writes:

> That post provides a function which creates a indirect clone of
> the current buffer, and narrows to the marked region in that, so
> that you have both the original un-narrowed buffer and the narrowed
> indirect clone available simultaneously.
>
> This is incredibly useful, and it seems like an obvious contender
> for standard functionality in Emacs.
>
> I'm attaching a patch which uses that approach to implement the
> following functions and bindings:
>
> C-x 4 n n: narrow-to-region-indirect-other-window
> C-x 4 n p: narrow-to-page-indirect-other-window
> C-x 4 n d: narrow-to-defun-indirect-other-window

Sounds like a good idea to me, and seems to be a logical extension.
Anybody got an opinion?

However:

> It's still essentially Zane's code, so I've confirmed that he's
> happy for me to send this, and am CCing this to him.

We'd need copyright assignment papers for something like this.  Zane,
would you be willing to assign copyright for this code to the FSF?

diff --git a/lisp/bindings.el b/lisp/bindings.el
index a1751a253c..e2b4ceb08e 100644
--- a/lisp/bindings.el
+++ b/lisp/bindings.el
@@ -1418,6 +1418,9 @@ esc-map
 
 (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
+(define-key ctl-x-4-map "nn" 'narrow-to-region-indirect-other-window)
+(define-key ctl-x-4-map "np" 'narrow-to-page-indirect-other-window)
+(define-key ctl-x-4-map "nd" 'narrow-to-defun-indirect-other-window)
 
 ;; Signal handlers
 (define-key special-event-map [sigusr1] 'ignore)
diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index 8c18557c79..94edd8fdd6 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -647,6 +647,16 @@ narrow-to-defun
       (re-search-backward "^\n" (- (point) 1) t)
       (narrow-to-region beg end))))
 
+(defun narrow-to-defun-indirect-other-window (&optional arg)
+  "`narrow-to-defun' in a cloned indirect buffer in the other window.
+
+See `clone-indirect-buffer'."
+  (interactive)
+  (let ((buf (clone-indirect-buffer nil nil)))
+    (with-current-buffer buf
+      (narrow-to-defun arg))
+    (pop-to-buffer buf)))
+
 (defcustom insert-pair-alist
   '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
   "Alist of paired characters inserted by `insert-pair'.
diff --git a/lisp/simple.el b/lisp/simple.el
index 7dc695848b..8e360a0076 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -8947,6 +8947,16 @@ clone-indirect-buffer-other-window
   (let ((pop-up-windows t))
     (clone-indirect-buffer newname display-flag norecord)))
 
+(defun narrow-to-region-indirect-other-window (start end)
+  "`narrow-to-region' in a cloned indirect buffer in the other window.
+
+See `clone-indirect-buffer'."
+  (interactive "r")
+  (deactivate-mark)
+  (let ((buf (clone-indirect-buffer nil nil)))
+    (with-current-buffer buf
+      (narrow-to-region start end))
+    (pop-to-buffer buf)))
 
 ;;; Handling of Backspace and Delete keys.
 
diff --git a/lisp/textmodes/page.el b/lisp/textmodes/page.el
index 029ba96691..e729c020a7 100644
--- a/lisp/textmodes/page.el
+++ b/lisp/textmodes/page.el
@@ -143,6 +143,16 @@ page--count-lines-page
               (count-lines (point) opoint)
               (count-lines opoint end))))))
 
+(defun narrow-to-page-indirect-other-window (&optional arg)
+  "`narrow-to-page' in a cloned indirect buffer in the other window.
+
+See `clone-indirect-buffer'."
+  (interactive "P")
+  (let ((buf (clone-indirect-buffer nil nil)))
+    (with-current-buffer buf
+      (narrow-to-page arg))
+    (pop-to-buffer buf)))
+
 (defun count-lines-page ()
   "Report number of lines on current page, and how many are before or after point."
   (interactive)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no