UP | HOME

emacs-删除org-roam空的daily文件

[2024-04-07 周日 10:12]

[2024-10-14 周一 16:53]:手动删除org-roam空的daily文件

(require 'find-lisp)

(defun count-lines-in-file (file)
  "返回文件 FILE 的行数。"
  (with-temp-buffer
    (insert-file-contents file)
    (count-lines (point-min) (point-max))))

(defun count-non-empty-lines (file)
  "计算文件 FILE 中的非空行数。"
  (with-temp-buffer
    (insert-file-contents file)
    (goto-char (point-min))
    (let ((count 0))
      (while (not (eobp))
        (when (not (looking-at-p "^[[:space:]]*$"))
          (setq count (1+ count)))
        (forward-line 1))
      count)))

(defun delete-file-if-lines-sm (file threshold)
  "如果文件 FILE 的行数小于等于 THRESHOLD,则删除该文件。"
  (let ((line-count (count-non-empty-lines file)))
    (when (<= line-count threshold)
      (delete-file file)
      (message "删除文件: %s (行数: %d)" file line-count))))

(defun my/delete-empty-node ()
  "获取 DIRECTORY 目录下所有 .org 文件的路径,并逐一计算每个文件的行数。
   如果行数小于等于 THRESHOLD,则删除该文件。"
  (interactive)
  (let ((org-files (find-lisp-find-files "~/.emacs.d/org/journals/daily" "\\.org$")))
    (dolist (file org-files)
      (delete-file-if-lines-sm file 4))))

(my/delete-empty-node)

▲ 编辑于 [2024-10-14 周一 16:53] | © Published by Emacs 31.0.50 (Org mode 9.7.16) on [2024-11-21 周四 00:22] | RSS