summaryrefslogtreecommitdiff
path: root/modules/org-refile-config.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2025-10-12 11:47:26 -0500
committerCraig Jennings <c@cjennings.net>2025-10-12 11:47:26 -0500
commit092304d9e0ccc37cc0ddaa9b136457e56a1cac20 (patch)
treeea81999b8442246c978b364dd90e8c752af50db5 /modules/org-refile-config.el
changing repositories
Diffstat (limited to 'modules/org-refile-config.el')
-rw-r--r--modules/org-refile-config.el78
1 files changed, 78 insertions, 0 deletions
diff --git a/modules/org-refile-config.el b/modules/org-refile-config.el
new file mode 100644
index 00000000..7b50604a
--- /dev/null
+++ b/modules/org-refile-config.el
@@ -0,0 +1,78 @@
+;;; org-refile-config.el --- Org Refile Customizations -*- lexical-binding: t; coding: utf-8; -*-
+;; author: Craig Jennings <c@cjennings.net>
+;;; Commentary:
+;; Configuration and custom functions for org-mode refiling.
+
+;;; Code:
+
+;; ----------------------------- Org Refile Targets ----------------------------
+;; sets refile targets
+;; - adds project files in org-roam to the refile targets
+;; - adds todo.org files in subdirectories of the code and project directories
+
+(defun cj/build-org-refile-targets ()
+ "Build =org-refile-targets=."
+ (interactive)
+ (let ((new-files
+ (list
+ (cons inbox-file '(:maxlevel . 1))
+ (cons reference-file '(:maxlevel . 2))
+ (cons schedule-file '(:maxlevel . 1)))))
+ ;; Extend with org-roam files if available AND org-roam is loaded
+ (when (and (fboundp 'cj/org-roam-list-notes-by-tag)
+ (fboundp 'org-roam-node-list)) ; <-- Add this check
+ (let* ((project-and-topic-files
+ (append (cj/org-roam-list-notes-by-tag "Project")
+ (cj/org-roam-list-notes-by-tag "Topic")))
+ (file-rule '(:maxlevel . 1)))
+ (dolist (file project-and-topic-files)
+ (unless (assoc file new-files)
+ (push (cons file file-rule) new-files)))))
+ ;; Add todo.org files from known directories
+ (dolist (dir (list user-emacs-directory code-dir projects-dir))
+ (let* ((todo-files (directory-files-recursively
+ dir "^[Tt][Oo][Dd][Oo]\\.[Oo][Rr][Gg]$"))
+ (file-rule '(:maxlevel . 1)))
+ (dolist (file todo-files)
+ (unless (assoc file new-files)
+ (push (cons file file-rule) new-files)))))
+ (setq org-refile-targets (nreverse new-files))))
+
+(add-hook 'emacs-startup-hook #'cj/build-org-refile-targets)
+
+(defun cj/org-refile (&optional ARG DEFAULT-BUFFER RFLOC MSG)
+ "Simply rebuilds the refile targets before calling org-refile.
+
+ARG DEFAULT-BUFFER RFLOC and MSG parameters passed to org-refile."
+ (interactive "P")
+ (cj/build-org-refile-targets)
+ (org-refile ARG DEFAULT-BUFFER RFLOC MSG))
+
+;; ----------------------------- Org Refile In File ----------------------------
+;; convenience function for scoping the refile candidates to the current buffer.
+
+(defun cj/org-refile-in-file ()
+ "Refile to a target within the current file and save the buffer."
+ (interactive)
+ (let ((org-refile-targets `(((,(buffer-file-name)) :maxlevel . 6))))
+ (call-interactively 'org-refile)
+ (save-buffer)))
+
+
+;; --------------------------------- Org Refile --------------------------------
+
+(use-package org-refile
+ :ensure nil ;; built-in
+ :defer .5
+ :bind
+ (:map org-mode-map
+ ("C-c C-w" . cj/org-refile)
+ ("C-c w" . cj/org-refile-in-file))
+ :config
+ ;; save all open org buffers after a refile is complete
+ (advice-add 'org-refile :after
+ (lambda (&rest _)
+ (org-save-all-org-buffers))))
+
+(provide 'org-refile-config)
+;;; org-refile-config.el ends here.