summaryrefslogtreecommitdiff
path: root/modules/org-refile-config.el
blob: f36af000381e48e216c60c6ecb1120b03495bef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
;;; org-refile-config.el --- Org Refile Customizations -*- lexical-binding: t; -*-

;;; 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)
	;; Start with the inbox and the schedule files.
	(setq new-files `((,inbox-file . (:maxlevel . 1)) (,schedule-file . (:maxlevel . 1))))

	;; Extend new-files with the project and topic files.
	(let ((project-and-topic-files (append (cj/org-roam-list-notes-by-tag "Project")
										   (cj/org-roam-list-notes-by-tag "Topic"))))
	  (let ((file-rule `(:level . 1)))
		(dolist (file project-and-topic-files)
		  (unless (assoc file new-files)
			(push `(,file . ,file-rule) new-files)))))

	;; Extend new-files with todo.org files in the specified 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]$")))
		(let ((file-rule `(:level . 1)))
		  (dolist (file todo-files)
			(unless (assoc file new-files)
			  (push `(,file . ,file-rule) new-files))))))

	;; Set org-refile-targets.
	(setq org-refile-targets (nreverse new-files))))

;; -------------------------------- Org-Refile -------------------------------
;; used in place of org-refile to ensure the refile targets are rebuilt.

(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.