如何折叠 emacs org-mode 日志文件中超过 3 个月的所有条目?

发布于 2024-11-17 07:09:55 字数 303 浏览 4 评论 0原文

我有一个包含这样的日期条目的大型日志文件:

[2011-06-23 Thu]

some text

[2011-06-22 Wed]

some more text

[... 12MB of text later ...]

[2000-01-01 Sat]

first entry

我希望能够配置组织模式,以便只有最近 3 个月可见,而较旧的条目会折叠起来,并且仅在需要时才展开。有没有一种方法可以自动设置,以便每个新的一天都会使 3 个月 + 1 天的旧条目下次折叠?

现在看来,我无法使用,因为加载文档需要一分多钟的时间。

I have a large journal file with date entries like this:

[2011-06-23 Thu]

some text

[2011-06-22 Wed]

some more text

[... 12MB of text later ...]

[2000-01-01 Sat]

first entry

I would like to be able to configure org-mode so that only the last 3 months are visible, whereas older entries are collapsed, and are only expanded when I need to. Is there a way of setting this up automatically so that every new day makes the 3 month + 1 day old entry be collapsed next time?

As it is now, it's impossible for me to use, because it takes more than a minute to load the document.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不再见 2024-11-24 07:09:56

对于 12MB 的文本,这肯定是大量的日期输入标题,所以我想缓冲区缩小可能仍然是一个很好的方法。

我是组织模式新手,所以我可能缺少一种更简单的方法来实现此目的,但以下内容会自动将缓冲区缩小到当前日期前三个月以来所做的条目。

(defvar my-org-journal "/path/to/file.org")

(add-hook 'org-mode-hook 'my-org-mode-hook)
(defun my-org-mode-hook ()
  (when (equal (expand-file-name (buffer-file-name))
               (expand-file-name my-org-journal))
    (my-org-narrow-to-month-of-entries)))

(defun my-org-narrow-to-month-of-entries ()
  "Narrow buffer to entries within the last three months."
  (interactive)
  (let ((minimum-date
         (with-temp-buffer
           (progn
             (org-insert-time-stamp (current-time) nil t)
             (org-timestamp-change -3 'month)
             (buffer-substring-no-properties
              (point-min) (point-max))))))
    (save-excursion
      (while (search-forward-regexp org-tsr-regexp-both nil t)
        (let ((end (point)))
          (backward-sexp)
          (let ((datestamp (buffer-substring-no-properties (point) end)))
            (if (string< datestamp minimum-date)
                (narrow-to-region (point-min) (point))
              (forward-sexp))))))))

当然,Cxnw再次扩大缓冲区以查看所有条目。

如果您想基于局部变量而不是文件名来应用此方法,则可以使用此方法:

(defvar my-org-narrow-to-month-of-entries nil)

(add-hook 'org-mode-hook 'my-org-mode-hook)
(defun my-org-mode-hook ()
  (add-hook 'hack-local-variables-hook
            (lambda () (when my-org-narrow-to-month-of-entries
                         (my-org-narrow-to-month-of-entries)))
            nil t))

在文件末尾添加以下内容:

;;; Local Variables:
;;; my-org-narrow-to-month-of-entries: t
;;; End:

参考文献:

编辑:

说实话,我不确定这是否会对这么长的加载时间产生任何影响。类似大小的示例组织文件在我的计算机上加载不需要那么长时间,但我不知道这是由于更好的硬件,还是我的简单虚拟文件更容易处理。

如果上述方法没有改善情况,我们可以考虑在 org-mode 初始化之前进行缩小,看看这是否有任何区别?

以下可能不是执行此操作的最佳方法,但出于性能原因值得尝试,以防万一它有所作为。

这将取代 org-mode-hook,并使用 my-org-journal 变量作为文件名,就像上面的第一个示例一样。

(defadvice insert-file-contents (after my-insert-file-contents)
  (when (equal (expand-file-name (buffer-file-name))
               (expand-file-name my-org-journal))
    (require 'org)
    (my-org-narrow-to-month-of-entries)))
(ad-activate 'insert-file-contents)

With 12MB of text, that's got to be a huge number of date entry headings, so I would imagine buffer narrowing might still be a good way to go.

I'm an org-mode novice, so I may be missing an easier way to achieve this, but the following will automatically narrow the buffer to the entries made since three months prior to the current date.

(defvar my-org-journal "/path/to/file.org")

(add-hook 'org-mode-hook 'my-org-mode-hook)
(defun my-org-mode-hook ()
  (when (equal (expand-file-name (buffer-file-name))
               (expand-file-name my-org-journal))
    (my-org-narrow-to-month-of-entries)))

(defun my-org-narrow-to-month-of-entries ()
  "Narrow buffer to entries within the last three months."
  (interactive)
  (let ((minimum-date
         (with-temp-buffer
           (progn
             (org-insert-time-stamp (current-time) nil t)
             (org-timestamp-change -3 'month)
             (buffer-substring-no-properties
              (point-min) (point-max))))))
    (save-excursion
      (while (search-forward-regexp org-tsr-regexp-both nil t)
        (let ((end (point)))
          (backward-sexp)
          (let ((datestamp (buffer-substring-no-properties (point) end)))
            (if (string< datestamp minimum-date)
                (narrow-to-region (point-min) (point))
              (forward-sexp))))))))

And, of course, C-xnw to widen the buffer again to see all the entries.

If you wanted to apply this based on a Local Variable rather than by file name, you could use this approach instead:

(defvar my-org-narrow-to-month-of-entries nil)

(add-hook 'org-mode-hook 'my-org-mode-hook)
(defun my-org-mode-hook ()
  (add-hook 'hack-local-variables-hook
            (lambda () (when my-org-narrow-to-month-of-entries
                         (my-org-narrow-to-month-of-entries)))
            nil t))

with the following at the end of your file:

;;; Local Variables:
;;; my-org-narrow-to-month-of-entries: t
;;; End:

References:

edit:

I'm not sure if that will do anything about that long load time, to be honest. A sample org file of comparable size doesn't take remotely that long to load on my machine, but I don't know whether that's due to better hardware, or my simple dummy file being easier to process.

If the above doesn't improve things, we could look at making the narrowing happen before org-mode is initialised, and see if that makes any difference?

The following may not be the best way of doing this, but it should be worth trying out for performance reasons, just in case it makes a difference.

This would be in place of the org-mode-hook, and using the my-org-journal variable for the filename, as with the first example above.

(defadvice insert-file-contents (after my-insert-file-contents)
  (when (equal (expand-file-name (buffer-file-name))
               (expand-file-name my-org-journal))
    (require 'org)
    (my-org-narrow-to-month-of-entries)))
(ad-activate 'insert-file-contents)
冰之心 2024-11-24 07:09:56

使用 C-[ 创建议程视图,然后使用 Cu 90 Cc a a 查看每日/每周议程。您可以自定义它并将其绑定到某些内容,请参阅 http://www.gnu.org/software/emacs/manual/html_node/org/Custom-agenda-views.html#Custom-agenda-views

[编辑] 实际上,这并没有回答有关折叠旧条目的问题,但确实允许您仅查看相关条目。

Create an agenda view with C-[ then look at the daily/weekly agenda with C-u 90 C-c a a. You can customise this and bind it to something, see http://www.gnu.org/software/emacs/manual/html_node/org/Custom-agenda-views.html#Custom-agenda-views .

[EDIT] Actually, this doesn't answer the question about collapsing older entries, but does allow you to view only the relevant entries.

爱格式化 2024-11-24 07:09:56

我没有完全自动化的方法来完成你所要求的事情。我确信具有 elisp/org-mode 能力的人可以将其自动化。

以下是一些可能有所帮助的组织模式功能:

  • 您可以使用“存档”标签(通过 Cc Cx a)标记标题,这可以防止它们在循环可见性时扩展。请参阅有关存档的组织模式手册。我认为这可以实现您想要的可见性。
  • 正如@Juancho 评论的那样,您可以搜索与表达式匹配的时间戳。这会生成一个匹配列表,您可以在其中轻松编写键盘宏以将每个匹配标记为存档。这是关于org-mode 中的高级搜索的好文档。

在我的系统上尝试此操作时,我发现的一个烦恼是搜索结果最终显示的是所有标题级别的平面列表。我组织条目的方式,最好将搜索限制在顶级标题(这就是我在日记中用于日常条目的内容)。我确信您可以通过设置某些属性来解决它,或者组织模式可以过滤特定级别的搜索。

I don't have a completely automated way to do what you're asking. I'm sure someone with some elisp/org-mode chops could automate it.

Here are some org-mode features that might help:

  • You can label headings with the Archive tag (via C-c C-x a) and that prevents them from expanding when you cycle visibility. See the org-mode manual on Archiving. I think that achieves what you want visibility-wise.
  • As @Juancho commented, you can search for Timestamps that match an expression. This generates a list of matches where you can easily write a keyboard macro to tag each as an Archive. Here's a good document on Advanced searches in org-mode.

One annoyance that I found when trying this on my system is that the search result ended up with a flat list of all heading levels. The way I organize entries, it would be better to limit the search to top-level headings (that's what I use for daily entries in my journal). I'm sure you could work around it by setting some property, or perhaps org-mode can filter searches at a particular level.

疯了 2024-11-24 07:09:56

您有三个选择:

  1. 将所有旧条目的 VISIBILITY 属性设置为 folded。你
    可以使用抄送a< a 将议程限制为当前文件
    然后标记条目并设置上述属性 en
    群众。
  2. 使用 Cc Cx A 将较旧的日记条目移至“存档同级”。
  3. 使用 Cc Cx Cs 将旧条目存档到存档文件中。

You have three options:

  1. set the VISIBILITY property to folded for all older entries. You
    can use C-c a < a to get the agenda restricted to the current file
    followed by marking the entries and setting the above property en
    masse.
  2. Move the older journal entries to an "Archive sibling" with C-c C-x A.
  3. Archive the older entries to an archive file with C-c C-x C-s.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文