Narrow to Environment in AUCTeX

I'd like to be able to use some org-mode-like features inside of AUCTeX. In org-mode, you can narrow the buffer to a node or you can flatout send the node to a separate buffer for editing.

How can this be done in AUCTeX? Specifically, I'd like to be able to perform a function that

• will determine the most specific environment it belongs in
• probably utilize point and mark to capture the environment in its entirety
• do one of two things:
• Narrow the buffer to this region (probably easiest)
• Send this region to another buffer where relevant changes in either buffer are reflected in each, and saving the 'narrowed' buffer saves the original file. (I'm not sure how this works internally in org-mode---see org-tree-to-indirect-buffer and Indirect Buffers in the Emacs documentation.

The ideal option would be total emulation of org-tree-to-indirect-buffer, and would probably involve only a few steps

1. Making an indirect buffer from the current one
2. Rename that indirect buffer to something useful (maybe buffername<envname>?)
3. Narrowing the indirect buffer
-
^C . ^X N N will do this, i.e. mark environment followed by narrow to region. You stay in the same buffer but only the environment is visible. –  Andrew Swann Mar 28 '13 at 18:12
LaTeX-mark-environment may help (I'm working on this too) –  Sean Allred Mar 28 '13 at 18:14
@AndrewSwann Just saw your comment, and yeah - that works. I'm still curious about the indirect buffer thing, but that sill probably turn out to be a simple application of the function I just mentioned. It's always that last bit of research you do after you ask that holds the key... –  Sean Allred Mar 28 '13 at 18:16

Interactively you can do as you request by

C-x 4 c C-c . C-x n n


cloning the buffer, marking the enclosing environment and then narrow to that environment. To build this as a single function, you need to pass appropriate arguments to the functions.

Firstly clone-indirect-buffer gets nil to use the default new name for the new buffer, and t to open it in a new window.

Marking is achieved by LaTeX-mark-environment, which needs no argument.

Finally, narrow-to-region requires the start and end of the region. LaTeX-mark-environment has put point at the start and mark at the end. The corresponding buffer positions are provided by the functions (point) and (mark).

(defun clone-focus-environment ()
"Clone the current buffer to an indirect buffer and narrow to the
environment current the current point."
(interactive "*")
(clone-indirect-buffer nil t)
(LaTeX-mark-environment)
(narrow-to-region (point) (mark))
)

-
This doesn't seem to work recursively; that is, if I have \begin{a}\begin{b}\begin{c}...\end{c}\end{b}\end{a} and narrow to b, I can't further narrow to c for some reason. –  Sean Allred Mar 29 '13 at 15:26
It works for me, even recursively, but you have to be positioned somewhere after the starting backslash of \begin. Also if the \ends are on the same line they will all be contained in the cloned buffer. The behaviour comes from LaTeX-mark-environment which you can verify interactively using C-c .. –  Andrew Swann Mar 29 '13 at 15:37
I suppose something may be wrong with my Emacs installation, then. LaTeX-mark-environment works perfectly as expected, but further narrowing attempts fail. –  Sean Allred Mar 29 '13 at 15:41
For some reason, after cloning, point is being repositioned after the closing brace of the outermost environment. –  Sean Allred Mar 29 '13 at 15:45
If the marking is working as expected, it would surprise me if there something about the emacs installation. I get the recursive effect by moving point forward into the inner environment in the cloned buffer before running the command again. I am running aquamacs 2.4, based on gnu emacs 23.3.50.1. –  Andrew Swann Mar 29 '13 at 15:46