bir-split.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ;;; bir-split.el --- Split functions for BIR -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 c1-g
  3. ;; Author: c1-g <char1iegordon@protonmail.com>
  4. ;; Keywords:
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'org-fc)
  19. (defun bir-split (arg)
  20. (interactive "P")
  21. (let* ((file-level-p (org-with-point-at (point-min) (= (org-outline-level) 0)))
  22. (article-title (if file-level-p
  23. (cadar (org-collect-keywords '("TITLE")))
  24. (save-excursion (while (org-up-heading-safe))
  25. (nth 4 (org-heading-components)))))
  26. (article-prop (org-entry-get nil bir-article-property t))
  27. (article-id (cond (article-prop (progn (string-match org-link-bracket-re article-prop)
  28. (substring-no-properties (match-string 1 article-prop) 3)))
  29. (file-level-p (org-with-point-at (point-min) (org-id-get-create)))
  30. (t (save-excursion (while (org-up-heading-safe))
  31. (org-id-get-create)))))
  32. (article (org-link-make-string (concat "id:" article-id) article-title))
  33. (refs (org-roam-node-refs (org-roam-node-from-id article-id)))
  34. (split-dir (file-name-as-directory (expand-file-name article-id bir-directory)))
  35. (count (how-many org-outline-regexp-bol (point-min) (point-max)))
  36. ids
  37. split-files
  38. template)
  39. (make-directory split-dir t)
  40. (setq ids (org-map-entries #'org-id-get-create))
  41. (save-buffer)
  42. (org-show-all)
  43. (start-process "bir-split" nil (or (executable-find "gcsplit")
  44. (executable-find "csplit"))
  45. "-s" "-z" "-f" (shell-quote-argument split-dir)
  46. "-b" (format "%%0%dd.org"
  47. (string-width (number-to-string count)))
  48. (shell-quote-argument (buffer-file-name))
  49. "/^\\*\\+ /" "{*}")
  50. (setq split-files (directory-files split-dir t directory-files-no-dot-files-regexp))
  51. (when split-files
  52. (org-map-entries (lambda ()
  53. (org-set-property "CUSTOM_ID" (org-id-get))
  54. (org-delete-property "ID")))
  55. (save-buffer)
  56. (org-roam-db-update-file))
  57. (dolist (file split-files)
  58. (with-temp-buffer
  59. (org-mode)
  60. (insert-file-contents file)
  61. (goto-char (point-min))
  62. (setq template (org-roam-format-template
  63. (string-trim (org-capture-fill-template org-roam-extract-new-file-path))
  64. (lambda (key default-val)
  65. (let ((fn (intern key))
  66. (node-fn (intern (concat "org-roam-node-" key)))
  67. (ksym (intern (concat ":" key))))
  68. (cond
  69. ((fboundp fn)
  70. (funcall fn node))
  71. ((fboundp node-fn)
  72. (funcall node-fn node))
  73. (t (let ((r (read-from-minibuffer (format "%s: " key) default-val)))
  74. (plist-put template-info ksym r)
  75. r)))))))
  76. (write-region (point-min) (point-max) file nil t)
  77. (org-set-property bir-article-property article)
  78. (when refs (org-set-property "ROAM_REFS" (concat "cite:&" (car refs))))
  79. (org-fc-type-topic-init)
  80. (when (> (org-outline-level) 0)
  81. (org-schedule '(4))
  82. (let ((title (nth 4 (org-heading-components)))
  83. (tags (nth 5 (org-heading-components))))
  84. (beginning-of-line)
  85. (kill-line 1)
  86. (org-roam-end-of-meta-data 'full)
  87. (insert "#+TITLE: " title "\n")
  88. (when tags (insert "#+FILETAGS: " tags "\n"))))
  89. (save-buffer)
  90. (org-id-add-location (org-id-get) (buffer-file-name))
  91. (org-roam-db-update-file)))
  92. ids))
  93. (provide 'bir-split)
  94. ;;; bir-split.el ends here