在 emacs org 文件中将 CATEGORY 转换为 PROPERTY 的最佳方法

发布于 2024-11-27 12:25:58 字数 675 浏览 1 评论 0原文

在 org 文件中将 CATEGORY 转换为 PROPERTY 的最佳方法是什么?

#+CATEGORY: NETWORKING

我使用

:PROPERTIES:
:CATEGORY: NETWORKING
:END:

以下 elisp 代码来执行此操作。

(let ((buf_name "home.org") (current_line 0) (current_string ""))
  (set-buffer buf_name)
  (goto-char (point-min))
  (while (re-search-forward "#\\+CATEGORY: \\(.*$\\)" nil t)
    (replace-match ":PROPERTIES:\n:CATEGORY: \\1\n:END:")))

有没有更好的方法。

编辑:- 正则表达式

"#\\+CATEGORY: \\(\\w+\\)"

已被修改为,

"#\\+CATEGORY: \\(.*$\\)"

因为我有一个“C++”类别,它不被识别为单词,只有“C++”中的“C”被识别为单词。

What is the best method to convert CATEGORY to PROPERTY in a org file.

#+CATEGORY: NETWORKING

to

:PROPERTIES:
:CATEGORY: NETWORKING
:END:

I have used the following elisp code to do this.

(let ((buf_name "home.org") (current_line 0) (current_string ""))
  (set-buffer buf_name)
  (goto-char (point-min))
  (while (re-search-forward "#\\+CATEGORY: \\(.*$\\)" nil t)
    (replace-match ":PROPERTIES:\n:CATEGORY: \\1\n:END:")))

Is there a better method.

EDIT:- the regular expression

"#\\+CATEGORY: \\(\\w+\\)"

has been modified to

"#\\+CATEGORY: \\(.*$\\)"

since i had a "C++" CATEGORY which is not recognized as a word, only the "C" in "C++" is recognized as a word.

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

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

发布评论

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

评论(2

悲喜皆因你 2024-12-04 12:25:58

你可以使用这个:

(let ((buf_name "home.org")
      (current_line 0)
      (current_string ""))
  (with-current-buffer buf_name
    (replace-regexp "#\\+CATEGORY: \\(\\w+\\)"
                    ":PROPERTIES:\n:CATEGORY: \\1\n:END:" 
                    t
                    (point-min)
                    (point-max))))

但这只是风格上的改变,概念上没有更好的方法。

此外:属性抽屉和类别并不等效:第二个扩展直到文件末尾,第一个仅在当前子树内。
因此,您很可能必须随后手动编辑它。

You can use this:

(let ((buf_name "home.org")
      (current_line 0)
      (current_string ""))
  (with-current-buffer buf_name
    (replace-regexp "#\\+CATEGORY: \\(\\w+\\)"
                    ":PROPERTIES:\n:CATEGORY: \\1\n:END:" 
                    t
                    (point-min)
                    (point-max))))

But it's only a stylistic change, there is no conceptually better way.

Furthermore: property drawers and category are not equivalent: The second expand until the end of the file, the first only inside the current subtree.
So you most probably have to edit it manually afterwards.

迷鸟归林 2024-12-04 12:25:58

试试这个:

(save-excursion
  (let* (file-cat 
     (file-list (org-agenda-files t)))
    (dolist (file file-list)
      (find-file file)
      (goto-char (point-min))
      (re-search-forward "CATEGORY")
      (setq file-cat (substring-no-properties 
              (org-get-category (point))))
      (beginning-of-line)
      (kill-line)
      (goto-char (point-min))
      (outline-next-heading)
      (org-set-property "CATEGORY" file-cat)
      (while (not (condition-case nil (outline-forward-same-level 1)
            (error t)))
    (org-set-property "CATEGORY" file-cat))
      (save-buffer))))

上面的代码获取文件类别,删除已弃用的行,然后将每个顶级标题的 CATEGORY 属性设置为旧文件类别。

编辑:编辑代码以适应乔纳森指出的情况

Try this:

(save-excursion
  (let* (file-cat 
     (file-list (org-agenda-files t)))
    (dolist (file file-list)
      (find-file file)
      (goto-char (point-min))
      (re-search-forward "CATEGORY")
      (setq file-cat (substring-no-properties 
              (org-get-category (point))))
      (beginning-of-line)
      (kill-line)
      (goto-char (point-min))
      (outline-next-heading)
      (org-set-property "CATEGORY" file-cat)
      (while (not (condition-case nil (outline-forward-same-level 1)
            (error t)))
    (org-set-property "CATEGORY" file-cat))
      (save-buffer))))

The above code gets the file category, removes the deprecated line, and then sets the CATEGORY property of each top level headline to the old file category.

Edit: Edited the code to accommodate the case pointed out by Jonathan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文