4

I'm using make-frame-on-display for collaborative editing with a colleague. Everything seems to work OK, but to avoid confusion we tend not to edit the exact part of the same file at the same time.

As a part of my workflow, I like to narrow-to-region, but if I do that during collaborative editing, then the buffer is narrowed not only on my frame/display, but also on that of my colleague. This means I cannot narrow because otherwise she cannot edit the part she is interested in.

Is there a way to make narrow to region be frame specific?

Daniel
  • 3,563
  • 16
  • 41
  • 2
    Would using [`(emacs) Indirect Buffers`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Indirect-Buffers.html) help? – Basil Feb 21 '18 at 14:24

1 Answers1

4

Try library narrow-indirect.el. See Narrow Indirect for a description.

Indirect buffers let you have any number of different views of the same buffer/file. Each view is a different buffer, whose content is some region of the original buffer (possibly the whole buffer).

An indirect buffer always has the same text and text properties as its base buffer, but otherwise it is pretty independent.

In particular, you can kill an indirect buffer without affecting its base buffer. You will likely want to kill indirect narrowed buffers rather than widening them.

I use this key binding, to let C-x 4 n n capture the region as an indirect buffer and visit that buffer in another window:

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

The three commands defined in narrow-indirect.el create and visit indirect buffers in another window. But if you want similar commands that do so in a separate frame, just bind pop-up-frames to non-nil in your own command. This definition uses the same arguments and interactive spec as ni-narrow-to-region-indirect-other-window and then just calls it with those arguments.

(defun my-narrow-to-region-indirect-other-frame (start end here
                                                 &optional full-name text msgp)
  "`narrow-to-region' in a cloned indirect buffer in another frame.
See `ni-narrow-to-region-indirect-other-window'."
  (interactive
    (list (region-beginning) (region-end) (point)
          (and current-prefix-arg  (read-string "Buffer name: ")) nil 'MSGP))
  (let ((pop-up-frames  t))
    (ni-narrow-to-region-indirect-other-window start end here full-name text msgp)))

This is what C-h f ni-narrow-to-region-indirect-other-window tells you:

ni-narrow-to-region-indirect-other-window is an interactive Lisp function in narrow-indirect.el.

It is bound to C-x 4 n n.

(ni-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 ni-buf-name-prefix.
  • It is suffixed by | TEXT, where TEXT is the region text, filtered by collapsing whitespace and (for Emacs 24.4+) removing invisible text. (Actually, option ni-buf-name-separator prefixes TEXT. " | " is the default value of this option.)

However, the buffer name is in any case truncated at ni-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 ni-buf-name-separator and appended to the original buffer name, which is appended to ni-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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Thanks for the answer. So in my use case I would `make-frame-on-display` and visit the file we want to collaborate on, just as I'm doing now, but then use narrow-indirect instead of narrow to not mess up my colleagues buffer. Alternatively, should I not just always work on a full indirect buffer and use normal narrowing? Rephrasing. is there a difference between 1. indirect-buffer 2. narrow and 1. buffer 2. indirect-narrow? – Daniel Feb 21 '18 at 15:47
  • The `narrow-indirect.el` commands do exactly that: (1) `clone-indirect-buffer`, (2) visit the new indirect buffer, (3) narrow. They are convenience commands. But they do that in a way that makes it easy to distinguish indirect buffers from non-indirect buffers and to identify each narrowing. (The narrowing part is optional.) Please see [Narrow Indirect](https://www.emacswiki.org/emacs/NarrowIndirect). – Drew Feb 21 '18 at 16:25