在org-mode中,如何折叠/隐藏脚注?

6
在Emacs org-mode中,有没有一种方法可以让内联脚注定义呈折叠状态显示?
这样,例如像这样的一行:
This effect is due to the strength of weak ties[fn:: Newman, Mark, Albert-László Barabási, and Duncan J. Watts. 2006. The Structure and Dynamics of Networks. Princeton, NJ: Princeton University Press].

可能只是像这样出现:
This effect is due to the strength of weak ties[✭].

我还需要一个命令,在必要时显示脚注。因此,也许需要两个命令:org-hide-footnotesorg-show-footnotes


我们能否将潜在的结束正则表达式].缩小到更确定的范围?例如,在[fn ::后面第一次出现的].,这意味着您不能在脚注内有第二对方括号。或者,也许我们可以说它将是第一个带有硬回车的行末尾的].?可能会有多于一行吗?例如,段落或更多,段落之间有空行?潜在的解决方案考虑到此脚注将位于属性抽屉中,该抽屉将单独折叠。 - lawlist
脚注可能不在行末。但是脚注内绝不会有方括号。因此,第一次出现的]表示脚注的结束。换句话说,我们采用您的第一个建议。 - incandescentman
2个回答

3

目前我不认为这是可能的。此外,在其内部使用 TAB 作为扩展键也可能会导致更多的按键过载。

另一方面,你是否有任何特定的原因不使用脚注部分来放置脚注呢?

C-c C-x f 将创建/交互您拥有的任何脚注。

(org-footnote-action &optional SPECIAL)

针对脚注执行正确的操作。

在脚注引用处时,跳转到定义处。

在定义处时,如果存在,则跳转到引用,否则提供创建引用的选项。

既不在定义处也不在引用处时,以交互方式创建新的脚注。

使用前缀 arg SPECIAL,在菜单中提供其他命令。

其他命令包括:

      s   Sort the footnote definitions by reference sequence.  During editing,
          Org makes no effort to sort footnote definitions into a particular
          sequence.  If you want them sorted, use this command, which will
          also move entries according to org-footnote-section.  Automatic
          sorting after each insertion/deletion can be configured using the
          variable org-footnote-auto-adjust.
      r   Renumber the simple fn:N footnotes.  Automatic renumbering
          after each insertion/deletion can be configured using the variable
          org-footnote-auto-adjust.
      S   Short for first r, then s action.
      n   Normalize the footnotes by collecting all definitions (including
          inline definitions) into a special section, and then numbering them
          in sequence.  The references will then also be numbers.  This is
          meant to be the final step before finishing a document (e.g., sending
          off an email).  The exporters do this automatically, and so could
          something like message-send-hook.
      d   Delete the footnote at point, and all definitions of and references
          to it.

2

初始版(2014年2月6日):第一份工作草案。

编辑(2014年2月18日):修订了功能lawlist-toggle-block-visibility,以便它包含恰当的if / then / else语句 - 即,如果该行包含所需的开始区域正则表达式,则块可见性将被切换,否则会出现一个消息说抱歉......。增加了与代码折叠相关的线程引用。将错误消息修改为引用点而不是行。

下面的答案源代码也存储在Github上:https://github.com/lawlist/lawlist-org-block-toggle/blob/master/lawlist-org-block-toggle.el

关于一个相关问题(即完全隐藏包括单词:PROPERTIES:在内的属性抽屉),请参阅以下线程:在org-mode中完全隐藏: PROPERTIES:抽屉

关于半相关问题(即创建可代码折叠的自定义块),还请参见:https://tex.stackexchange.com/a/161196/26911

此解决方案已在Emacs Trunk的相当新版本(于2014年1月19日构建)中进行了测试,其中包含org-mode版本8.2.5c。由于:PROPERTIES:抽屉通过:END:折叠与脚注和html块不同,因此该解决方案认为脚注和/或html代码块不会出现在该属性抽屉中。脚注可以出现在文本段落的任何位置,但不能在脚注内再有另一对方括号 - 因为此代码寻找第一个结束方括号才能标记折叠区域的结束。此代码认为#+BEGIN_HTML#+END_HTML都将与左边距对齐。启动视图仍然以相同方式工作 - 例如,变量org-startup-foldedorg-hide-block-startup

修改 org-cycle-internal-local 可以启用 org-mode 中所有形式的折叠标签循环。我们只是修复了函数的分页以使其更易读,并添加了以下条件: ((eq org-cycle-subtree-status 'subtree) (org-show-subtree) (message "ALL") (setq org-cycle-subtree-status 'all))。交互式函数 org-cycle 仍然用于在各种折叠 / 展开视图之间进行标签循环。函数 lawlist-block-org-cycle-internal-local 是一个非交互式的支持函数,由 org-cycle 使用。解决方案中这两个 defalias 部分是使一切正常需要的。用户可以直接调用交互式函数来在标题或子标题的开头切换可见性,而不是使用标签循环:M-x org-cycle RET 要直接切换脚注或 HTML 块的可见性,我们使用交互式函数 lawlist-toggle-block-visibility。将光标放在包含脚注开始或 HTML 块开始的行上,然后键入:M-x lawlist-toggle-block-visibility RET
(require 'org)

(defalias 'org-cycle-hide-drawers 'lawlist-block-org-cycle-hide-drawers)

(defun lawlist-block-org-cycle-hide-drawers (state)
  "Re-hide all drawers, footnotes or html blocks after a visibility state change."
  (when
    (and
      (derived-mode-p 'org-mode)
      (not (memq state '(overview folded contents))))
    (save-excursion
      (let* (
          (globalp (memq state '(contents all)))
          (beg (if globalp (point-min) (point)))
          (end
            (cond
              (globalp
                (point-max))
              ((eq state 'children)
                (save-excursion (outline-next-heading) (point)))
              (t (org-end-of-subtree t)) )))
        (goto-char beg)
        (while
          (re-search-forward
            ".*\\[fn\\|^\\#\\+BEGIN_HTML.*$\\|^[ \t]*:PROPERTIES:[ \t]*$" end t)
          (lawlist-org-flag t))))))

(defalias 'org-cycle-internal-local 'lawlist-block-org-cycle-internal-local)

(defun lawlist-block-org-cycle-internal-local ()
  "Do the local cycling action."
  (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
    (save-excursion
      (if (org-at-item-p)
        (progn
          (beginning-of-line)
          (setq struct (org-list-struct))
          (setq eoh (point-at-eol))
          (setq eos (org-list-get-item-end-before-blank (point) struct))
          (setq has-children (org-list-has-child-p (point) struct)))
        (org-back-to-heading)
        (setq eoh (save-excursion (outline-end-of-heading) (point)))
        (setq eos (save-excursion (1- (org-end-of-subtree t t))))
        (setq has-children
          (or
            (save-excursion
              (let ((level (funcall outline-level)))
                (outline-next-heading)
                (and
                  (org-at-heading-p t)
                  (> (funcall outline-level) level))))
            (save-excursion
              (org-list-search-forward (org-item-beginning-re) eos t)))))
      (beginning-of-line 2)
      (if (featurep 'xemacs)
        (while
            (and
              (not (eobp))
              (get-char-property (1- (point)) 'invisible))
          (beginning-of-line 2))
        (while
            (and
              (not (eobp))
              (get-char-property (1- (point)) 'invisible))
          (goto-char (next-single-char-property-change (point) 'invisible))
          (and
            (eolp)
            (beginning-of-line 2))))
      (setq eol (point)))
    (cond
      ((= eos eoh)
        (unless (org-before-first-heading-p)
          (run-hook-with-args 'org-pre-cycle-hook 'empty))
        (org-unlogged-message "EMPTY ENTRY")
        (setq org-cycle-subtree-status nil)
        (save-excursion
          (goto-char eos)
          (outline-next-heading)
          (if (outline-invisible-p)
            (org-flag-heading nil))))
      ((and
          (or
            (>= eol eos)
            (not (string-match "\\S-" (buffer-substring eol eos))))
          (or
            has-children
            (not (setq children-skipped
              org-cycle-skip-children-state-if-no-children))))
        (unless (org-before-first-heading-p)
          (run-hook-with-args 'org-pre-cycle-hook 'children))
        (if (org-at-item-p)
          ;; then
          (org-list-set-item-visibility (point-at-bol) struct 'children)
          ;; else
          (org-show-entry)
          (org-with-limited-levels (show-children))
          (when (eq org-cycle-include-plain-lists 'integrate)
            (save-excursion
              (org-back-to-heading)
              (while (org-list-search-forward (org-item-beginning-re) eos t)
                (beginning-of-line 1)
                (let* (
                    (struct (org-list-struct))
                    (prevs (org-list-prevs-alist struct))
                    (end (org-list-get-bottom-point struct)))
                  (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
                    (org-list-get-all-items (point) struct prevs))
                  (goto-char (if (< end eos) end eos)))))))
        (org-unlogged-message "CHILDREN")
        (save-excursion
          (goto-char eos)
          (outline-next-heading)
          (if (outline-invisible-p)
            (org-flag-heading nil)))
        (setq org-cycle-subtree-status 'children)
        (unless (org-before-first-heading-p)
          (run-hook-with-args 'org-cycle-hook 'children)))
      ((or
          children-skipped
          (and
            (eq last-command this-command)
            (eq org-cycle-subtree-status 'children)))
        (unless (org-before-first-heading-p)
          (run-hook-with-args 'org-pre-cycle-hook 'subtree))
        (outline-flag-region eoh eos nil)
        (org-unlogged-message
        (if children-skipped
          "SUBTREE (NO CHILDREN)"
          "SUBTREE"))
        (setq org-cycle-subtree-status 'subtree)
        (unless (org-before-first-heading-p)
          (run-hook-with-args 'org-cycle-hook 'subtree)))
      ((eq org-cycle-subtree-status 'subtree)
        (org-show-subtree)
        (message "ALL")
        (setq org-cycle-subtree-status 'all))
      (t
        (run-hook-with-args 'org-pre-cycle-hook 'folded)
        (outline-flag-region eoh eos t)
        (org-unlogged-message "FOLDED")
        (setq org-cycle-subtree-status 'folded)
        (unless (org-before-first-heading-p)
        (run-hook-with-args 'org-cycle-hook 'folded))))))

(defun lawlist-org-flag (flag)
  "When FLAG is non-nil, hide any of the following:  html code block;
footnote; or, the properties drawer.  Otherwise make it visible."
  (save-excursion
    (beginning-of-line 1)
    (cond
      ((looking-at ".*\\[fn")
        (let* (
          (begin (match-end 0))
          end-footnote)
          (if (re-search-forward "\\]"
                (save-excursion (outline-next-heading) (point)) t)
            (progn
              (setq end-footnote (point))
              (outline-flag-region begin end-footnote flag))
            (user-error "Error beginning at point %s." begin))))
      ((looking-at "^\\#\\+BEGIN_HTML.*$\\|^[ \t]*:PROPERTIES:[ \t]*$")
        (let* ((begin (match-end 0)))
          (if (re-search-forward "^\\#\\+END_HTML.*$\\|^[ \t]*:END:"
                (save-excursion (outline-next-heading) (point)) t)
            (outline-flag-region begin (point-at-eol) flag)
            (user-error "Error beginning at point %s." begin)))))))

(defun lawlist-toggle-block-visibility ()
"For this function to work, the cursor must be on the same line as the regexp."
(interactive)
  (if
      (save-excursion
        (beginning-of-line 1)
          (looking-at
            ".*\\[fn\\|^\\#\\+BEGIN_HTML.*$\\|^[ \t]*:PROPERTIES:[ \t]*$"))
    (lawlist-org-flag (not (get-char-property (match-end 0) 'invisible)))
    (message "Sorry, you are not on a line containing the beginning regexp.")))

这个对我来说似乎不再起作用了(而且github链接已经失效)。特别是如果我在一行上有多个脚注,它就会完全失败。你最近更新过它吗?如果没有,那么我应该再次提出请求以便其他人能看到吗? - avv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接