# Narrow and move between latex environments

When writing LaTeX presentations with the beamer class in emacs+auctex I would like to have a function (and keybinding) that does the following three things:

1. narrow to the next frame environment if currently nothing is narrowed
2. move to the next frame environment when I'm already narrowed to some frame environment.
3. if there is no subsequent frame environment to narrow to, then widen.

I can do this more or less with several keybindings using the features available in the auctex and expand-region packages. But I would love to have this behaviour bound to a single keybinding.

Is this possible?

Here's a quick & dirty function that meets your requirements:

(defun my-frame-dwim ()
(interactive)
(if (not (buffer-narrowed-p))
(if (search-forward "\\begin{frame}" nil t)
(LaTeX-narrow-to-environment))
(widen)
(if (search-forward "\\begin{frame}" nil t)
(LaTeX-narrow-to-environment))))


To bind that to a key, you could add this to your .emacs:

(eval-after-load "latex"
'(define-key LaTeX-mode-map (kbd "C-c f") 'my-frame-dwim))