Automatically update org-agenda-files
I heavily rely on org mode for my daily schedule, project management, personal learning, paper/book reading, and all forms of writing. One of the crucial aspects of my workflow is using org-agenda to gather my schedules, to-dos, and appointments from all the org files in my working folder. This function has significantly improved my efficiency.
However, org-agenda does not automatically update the file list for aggregation. For instance, if you set org-agenda-file to monitor a folder such as $HOME/Org/Work, org-agenda fails to add a new org file under $HOME/Org/Work to its file list unless you manually update org-agenda-file by evaluating the command
(setq org-agenda-file '(xxx xxx xxx))
To solve the problem, I first try to get help by using filenotify mode. The plan is to utilize the file-notify-add-watch function of filenotify to monitor files or folders for org-agenda’s aggregation. Whenever there is any change, it will automatically re-evaluate org-agenda-file. After doing some research, I wrote a script similar to this.
(require 'filenotify) (defun my-notify-callback(event) (setq org-agenda-files (append '("path_to_folder") ))) (file-notify-add-watch "path_to_folder" '(change attribute-change) 'my-notify-callback)
I believe this is the best solution for my problem. However, it appears that it is not working as expected, and I am unsure why. Despite my attempts at debugging and searching for solutions on Google, I have had no luck so far.
Thus, I opted for another method. I added a hook function that evaluates org-agenda-file to the org-agenda-mode-hook. The code is straightforward:
(defun my-set-agenda-files() (setq org-agenda-files (append '("path_to_folder") ))) (add-hook 'org-agenda-mode-hook 'my-set-agenda-files)
The Org-agenda-mode-hook is activated before org-agenda is executed, meaning that Emacs will evaluate org-agenda-files every time org-agenda is invoked or restarted. While this solution may not be perfect, especially for those with a large number of org files in the watched folder, it works for my needs. Additionally, since I do not have a large number of org files in my working folders, the potential slow down issue is not a concern for me.
Note: org-agenda-files does not monitor all the org files within a specific folder. I am not sure where the problem lies, but it seems like org-agenda only gathers the org files directly within the folder specified and does not look up files recursively. To resolve this issue, you can specify the folder as follows:
(setq org-agenda-files (append '("path_to_a_file") (directory-files-recursively "path_to_folder" "\\`[^.].*\\.org\\'") )))
The function directory=files-recursively is the solution. :-)