The question title says it, pretty much.
How can I link together RefTeX's TOC functionality with outline-mode
such that I can fold sections/subsections/etc.?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityINITIAL DRAFT (February 19, 2014): First rough draft.
EDIT (February 20, 2014): Consolidated both functions into just one. Added a variable beg-flag-regexp
and revised the logic of the function in relation thereto. The function now examines the beginning regexp to determine the matching ending regexp; and, it will work with the format of \begin{anything}
and \end{anything}
-- provided (of course) that anything is the same for both begin and end. The function now takes into consideration the possibility that there may be several occurrences on the same line matching the beg-flag-regexp
-- e.g., \begin{minipage}\begin{singlespace*}
-- i.e., selection is based upon proximity to the cursor position.
The following is a demonstration of how code folding works in Emacs. Code folding requires that there be a defined regexp for the beginning and ending of the folded region.
(defvar beg-flag-regexp (concat
"\\(\\\\begin\{\\)\\("
"[^}]*"
"\\)\\(\}\\)" )
"Regexp matching the beginning of the folded region.")
(defun toggle-block ()
"When FLAG is non-nil, hide the region. Otherwise make it visible. For this
function to work, the cursor must be on the same line as the beginning regexp."
(interactive)
(require 'outline)
(cond
((or
;; sweet-spot
(looking-at beg-flag-regexp)
;; point could be between backslash and before the letter n
(let ((line-begin (save-excursion (beginning-of-line 1) (point))))
(save-excursion
(re-search-backward "\\\\" line-begin t)
(looking-at beg-flag-regexp)))
;; point could be to the right of \begin
(let ((line-begin (save-excursion (beginning-of-line 1) (point))))
(save-excursion
(re-search-backward "\\\\begin" line-begin t)
(looking-at beg-flag-regexp)))
;; point could be to the left of \begin
(let ((line-end (save-excursion (end-of-line 1) (point))))
(save-excursion
(re-search-forward "\\\\begin" line-end t)
(backward-char 6)
(looking-at beg-flag-regexp))))
(let* (
(flag (not (get-char-property (match-end 0) 'invisible)))
(beg (match-end 0))
end
(base-flag-match (regexp-quote
(buffer-substring-no-properties (match-beginning 2) (match-end 2))))
(end-flag-match (concat "\\\\end\{" base-flag-match "\}"))
(go-fish (concat "\\begin\{" base-flag-match "\}")) )
(save-excursion
(if (re-search-forward end-flag-match nil t)
(progn
(setq end (point))
(outline-flag-region beg end flag)
(cond
(flag
(overlay-put (make-overlay beg end) 'display "\u25be"))
(t
(mapc 'delete-overlay (overlays-in beg end)))))
(user-error "Error locating an ending match for: %s." go-fish)))
(if (> (point) beg)
(goto-char beg)) ))
(t
(message "Sorry, you are not on a line containing the beginning regexp."))))
\subsection
s under\section
s, as I would have expected. (Am I confusingoutline-mode
withorg-mode
?) – Sean Allred Feb 19 '14 at 0:51org-mode
is something likeoutline-mode
on steroids:-)
How do you expect\subsection
s to be folded? – giordano Feb 19 '14 at 1:05\section
sS
and\subsection
so
(continuing the pattern down the hierarchy):SooSooooSoSooo
could be folded intoS...SooooSoSooo
orS...S...SoS...
, etc. To establish the pattern:CSSooSooSSoo
->CSS...S...SS...
->C...
. (where each...
is anorg
-like continuation line) – Sean Allred Feb 21 '14 at 2:37