all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Phil Sainty <psainty@orcon.net.nz>,
	Lennart Borgman <lennart.borgman@gmail.com>
Cc: 17401@debbugs.gnu.org, contact@zaneashby.co.nz
Subject: bug#17401: 24.4.50; Narrow to {region,page,defun} in an indirect clone buffer in the other window
Date: Sat, 10 May 2014 21:12:44 -0700 (PDT)	[thread overview]
Message-ID: <758cb6d1-f13d-412c-8669-ae5eb23dea6b@default> (raw)
In-Reply-To: <536E2451.3090808@orcon.net.nz>

[-- Attachment #1: Type: text/plain, Size: 2220 bytes --]

> I was initially against the idea of naming the indirect buffer
> according to the narrowed region, for the simple reason that the
> name stops making sense as soon as you widen the buffer.
> 
> However, I expect that people are far less likely to widen these
> buffers (as opposed to simply killing them); and this naming
> approach would certainly be useful in the buffer list, so I think
> it's a good idea after all.
> 
> I've also noticed there are some org-mode specific narrowing
> commands as well, which should probably be supported. I did
> consider whether we could support all existing narrowing commands
> automagically with some trickery, but I concluded that the only
> approach I could think of was too convoluted and brittle for its
> own good, so I think simply adding a new function alongside each
> existing one is still the most practical way to go.
> 
> Do we want both a `narrow-to-*-indirect' and an `...-other-window'
> variant for each case? I think the latter works very nicely with
> the C-x 4 n prefix (as per the initial patch), but I can see the
> utility in providing both functions.

I agree about killing vs widening indirect buffers.  And I
don't find a need for same-window versions of the commands,
for my own use.

FWIW, attached is what I've been using lately.

The default buffer naming is similar to what Lennart suggested,
but (for the non-defun command) the region text (with whitespace
collapsed) is used, not the first line.  And the buffer-name
length is limited.  And the text following the original name is
separated by ` | ' instead of being enclosed in <...>.

Two options control naming: one limits the length, the other
is a name prefix (e.g., to easily distinguish indirect buffers).

A prefix arg prompts you for the new buffer name.

For defun narrowing, a negative prefix arg prompts you for the
text that follows ` | ' (in place of the defun object name).

To me, being able to provide names different from the default
is important.

--

(FWIW, I also use library `wide-n.el', which lets you widen
to any number of narrowings of the same buffer - e.g., cycle.
http://www.emacswiki.org/emacs-de/MultipleNarrowings)

[-- Attachment #2: narrow-other.el --]
[-- Type: application/octet-stream, Size: 3744 bytes --]

(defcustom narrowed-buf-name-max 60
  "Max length of cloned indirect buffer name, for narrowing commands."
  :type '(restricted-sexp :tag "Max length of buffer name"
          :match-alternatives ((lambda (x) (and (integerp x)  (> x 0))))
          :value ignore)
  :group 'editing)

(defcustom narrowed-buf-name-prefix ""
  "Name prefix for indirect buffer cloned by `narrow-*-indirect*' commands.
Using a non-empty prefix lets you easily distinguish the indirect
buffers from the original."
  :type 'string :group 'editing)

(defun narrow-to-defun-indirect-other-window (&optional full-name text)
  "`narrow-to-defun' in a cloned indirect buffer in the other window.
The name of the indirect buffer depends on the use of a prefix arg:

* No prefix arg: the current buffer name, but with ` | NAME'
  appended, where NAME is the name of the object defined by the defun.

* Prefix arg < 0 : like no prefix arg, but you are prompted for NAME.

* Prefix arg >= 0: you are prompted for the full buffer name.

However, the buffer name is in any case truncated at
`narrowed-buf-name-max' chars.

Non-interactively:
* FULL-NAME is the full buffer name, and TEXT is ignored.
* TEXT is used for NAME, if FULL-NAME is nil.

See `clone-indirect-buffer'."
  (interactive
   (list (and current-prefix-arg
              (natnump (prefix-numeric-value current-prefix-arg))
              (read-string "Buffer name: "))
         (and current-prefix-arg
              (< (prefix-numeric-value current-prefix-arg) 0)
              (read-string "Buffer name suffix: "))))
  (require 'which-func)
  (let ((here  (point)))
    (mark-defun)
    (narrow-to-region-indirect-other-window
     (region-beginning) (region-end) here full-name
     (and (not full-name)  (or text  (which-function))))))
     
(defun narrow-to-region-indirect-other-window (start end here
                                               &optional full-name text msgp)
  "`narrow-to-region' in a cloned indirect buffer in the other window.
The indirect buffer is named the same as the current buffer, except:

 * It is prefixed by the value of option `narrowed-buf-name-prefix'.
 * It is suffixed by ` | TEXT', where TEXT is the region text.

However, the buffer name is in any case truncated at
`narrowed-buf-name-max' chars.

Non-interactively:
START and END are the region beginning and end.
HERE is where to place the cursor, relative to START.
TEXT is prefixed by ` | ' and appended to the original
 buffer name, which is appended to `narrowed-buf-name-prefix' to name
 the new buffer.
If FULL-NAME is a string then it is used as the complete indirect
buffer name.  (TEXT is then ignored.)

See `clone-indirect-buffer'."
  (interactive
   (list (region-beginning) (region-end) (point)
         (and current-prefix-arg  (read-string "Buffer name: "))
         nil 'MSGP))
  (if (and (= start end)  msgp)
      (message "Region is empty")
    (deactivate-mark)
    (let* ((buf  (or full-name
                     text
                     (replace-regexp-in-string
                      "\\(\s \\)+" "\1"
                      (replace-regexp-in-string
                       "\\`\s+\\|\s+\\'" ""
                       (buffer-substring-no-properties start end)))))
           (buf  (or full-name
                     (concat narrowed-buf-name-prefix (buffer-name) " | " buf)))
           (buf  (or full-name
                     (substring buf 0 (min (length buf) narrowed-buf-name-max))))
           (buf  (clone-indirect-buffer buf nil)))
      (with-current-buffer buf (narrow-to-region start end) (goto-char here))
      (pop-to-buffer buf))))

(define-key ctl-x-4-map "nn" 'narrow-to-region-indirect-other-window)
(define-key ctl-x-4-map "nd" 'narrow-to-defun-indirect-other-window)


  parent reply	other threads:[~2014-05-11  4:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-04 10:38 bug#17401: 24.4.50; Narrow to {region,page,defun} in an indirect clone buffer in the other window Phil Sainty
2014-05-04 13:43 ` Drew Adams
2014-05-08  0:05 ` Lennart Borgman
2014-05-10 13:06   ` Phil Sainty
2014-05-10 20:57     ` Josh
2014-05-11  4:12     ` Drew Adams [this message]
2020-09-18 13:57 ` Lars Ingebrigtsen
2020-09-18 16:02   ` Drew Adams
2020-09-18 21:22   ` Zane Ashby
2020-09-19 14:14     ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=758cb6d1-f13d-412c-8669-ae5eb23dea6b@default \
    --to=drew.adams@oracle.com \
    --cc=17401@debbugs.gnu.org \
    --cc=contact@zaneashby.co.nz \
    --cc=lennart.borgman@gmail.com \
    --cc=psainty@orcon.net.nz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.