org-roam个人整理函数分享

将daily笔记以逆序datetree方式refile到另一个窗口

Table of Contents

  1. 使用场景
  2. 函数内容
  3. 运行效果

使用场景

整理org-roam的daily笔记时,希望能够将涉及某些node的标题移动到该node并按逆序时间树排序

函数内容

(use-package org-reverse-datetree)
(setq-default org-reverse-datetree-level-formats
              '("%Y"                    ; year
                "%Y-%m %B"
                "%Y-%m-%d %A"           ; date
                ))
(defun org-get-next-window-file ()
  (if (delq (selected-window) (window-list-1 (next-window nil 'nomini 'visible)))
      (let ((buffer2 (window-buffer (next-window nil 'nomini 'visible))))
        (with-current-buffer buffer2
          (when (eq major-mode 'org-mode)
            (buffer-file-name buffer2))))))
(defun org-get-current-window-title ()
  (with-current-buffer (window-buffer)
        (pcase (org-collect-keywords '("TITLE"))
          (`(("TITLE" . ,val))
           (setq title (car val))))))
(defun org-refile-to-other-window (arg)
  (interactive "P")
  (if (org-get-next-window-file)
      (org-reverse-datetree-refile-to-file (org-get-next-window-file) (date-to-time (org-get-current-window-title)))
        ))

运行效果

在左右并排的两个window间,会根据本buffer的title,将指定的headline refile到另一个窗口,并按reverse-datetree排列

4 个赞

我用的是 Hierarchical archiving for Org-mode · GitHub

结构性的归档,不管是 TODO 还是 daily log 我都用他来,还蛮好用的。相较你这个可能少了时序,不过归档是最新的放在最上方。

1 个赞

这个方式的需求来自于整理org-roam的笔记,特别是org-roam-buffer里的daily笔记,所以泛用性确实不如org-archive相关函数

自动删除org-roam空的daily文件

(defun vulpea-buffer-p ()
  "Return non-nil if the currently visited buffer is a note."
  (and buffer-file-name
       (string-prefix-p
        (expand-file-name (file-name-as-directory YOUR-ORG-ROAM-DIR))
        (file-name-directory buffer-file-name))))

(defun my/delete-empty-daily ()
  "delete daily node without content"
  ;;限制在daily文件夹
  (if (and (vulpea-buffer-p) (string-match "daily" (file-name-directory buffer-file-name)))
      ;;查找heading
      (unless (org-map-entries
           (lambda ()
             (org-entry-get nil "ITEM"))
           nil nil)
          (delete-file (buffer-file-name))
          (kill-this-buffer))))

(add-hook 'after-save-hook #'my/delete-empty-daily)
1 个赞

Table of Contents

  1. 调用matplotlib画图,并存储到Windows的临时文件夹中
    1. 根据已有表格绘图
    2. 根据代码块标题绘图
    3. 运行结果

调用matplotlib画图,并存储到Windows的临时文件夹中

根据已有表格绘图

在表格上方标注名称,如:

#+NAME: myTimeTable
|1|2|
|-+-|
|3|4|

在python代码行的header里传入表格数据(在我用的org 9.6.14中数据会自动去除标题行和分割行,只剩下面的数据行):

#+begin_src python :results file link :var data=myTimeTable
import os,matplotlib, numpy
……

savedir = os.getenv('TEMP')+'\\myTimeTable.png'
plt.savefig(savedir)
return savedir

根据代码块标题绘图

会把代码块上方的name动态传入到代码中

#+name: 代码块name
#+begin_src python :var filename=(org-element-property :name (org-element-context)) :results file link
import os,matplotlib, numpy
……
savedir = os.getenv('TEMP')+'\\'+filename+'.png'
plt.savefig(savedir)
return savedir

运行结果

运行后会在下方生成

#+RESULTS:
[[file:..../XXX.png]]

的图片,可以配合inline 图片相关函数使用

;; (setq org-startup-with-inline-images t)
;;; Only display inline images under current subtree.
(defun org-display-subtree-inline-images (&optional state)
  "Toggle the display of inline images under current subtree.
INCLUDE-LINKED is passed to `org-display-inline-images'."
  (interactive)
  (save-excursion
    (save-restriction
      (org-narrow-to-subtree)
      (let* ((beg (point-min))
             (end (point-max))
             (image-overlays (cl-intersection
                              org-inline-image-overlays
                              (overlays-in beg end)))
             (display-inline-images-local
              (lambda ()
                (org-display-inline-images t t beg end)
                (setq image-overlays (cl-intersection
                                      org-inline-image-overlays
                                      (overlays-in beg end)))
                (if (and (org-called-interactively-p) image-overlays)
                    (message "%d images displayed inline"
                             (length image-overlays)))))
             (hide-inline-images-local
              (lambda ()
                (org-remove-inline-images)
                (message "Inline image display turned off"))))
        (if state
            (pcase state
              ('subtree
               (funcall display-inline-images-local))
              ('folded
               (funcall hide-inline-images-local)))
          (if image-overlays
              (funcall display-inline-images-local)
            (funcall hide-inline-images-local)))))))

(define-key org-mode-map (kbd "C-c C-x C-v") 'org-display-subtree-inline-images)

;;; auto display inline images on Org TAB cycle expand headlines.
(add-hook 'org-cycle-hook #'org-display-subtree-inline-images)
(add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images)

参考:分享几个关于Org-link的函数 - #6,来自 stardiviner

1 个赞

在编辑一些较大的org文件时,往往会在保存的时候卡一秒多。经过排查,问题出在org-roam的默认设置上。

根据Org-Roam Manual里的说法:

6.3 When to cache

6.3 何时进行缓存

By default, Org-roam is eager in caching: each time an Org-roam file is modified and saved, it updates the database for the corresponding file. This keeps the database up-to-date, causing the least surprise when using the interactive commands.

默认情况下,Org-roam 在缓存方面表现积极:每次修改并保存 Org-roam 文件时,它都会更新相应文件的数据库。这使得数据库始终保持最新状态,在使用交互式命令时带来最小的意外。

However, depending on how large your Org files are, database updating can be a slow operation. You can disable the automatic updating of the database by setting org-roam-db-update-on-save to nil.

然而,根据您的 Org 文件大小,数据库更新可能会是一个缓慢的操作。您可以通过将 org-roam-db-update-on-save 设置为 nil 来禁用数据库的自动更新。

Variable: org-roam-db-update-on-save

变量:org-roam-db-update-on-save ¶

If t, update the Org-roam database upon saving the file. Disable this if your files are large and updating the database is slow.

如果启用,则在保存文件时更新 Org-roam 数据库。如果文件较大且更新数据库速度较慢,请禁用此功能。

每次保存org-roam文件时,都会执行一次数据库更新。这对于小体量文件来说很快,但稍微大一点的就容易引发卡顿。

我们可以如下设置:

;;关闭自动更新db
(setq org-roam-db-update-on-save nil)
;;空闲10秒后执行更新db
(run-with-idle-timer 10 t 'org-roam-db-sync)
1 个赞

现在有了一个名为 org-node 的包来加速 org-roam 的保存和读取。

1 个赞