问题标题基本说明了一切。
我如何将 RefTeX 的 TOC 功能链接在一起outline-mode
以便可以折叠章节/小节/等等?
答案1
初稿(2014 年 2 月 19 日):第一稿。
编辑(2014 年 2 月 20 日):将两个函数合并为一个。添加了一个变量并修改了与此相关的函数逻辑。该函数现在检查开头的正则表达式以确定匹配的结尾正则表达式;并且,它将使用和beg-flag-regexp
的格式——当然前提是\begin{anything}
\end{anything}
任何事物两者相同开始和结尾. 该函数现在考虑到在同一行上可能存在多个匹配的情况beg-flag-regexp
——例如\begin{minipage}\begin{singlespace*}
——即选择是基于与光标位置的接近程度。
以下是 Emacs 中代码折叠的工作原理的演示。代码折叠需要为开始 和 结尾折叠区域。
(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."))))