bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#63870: 29.0.90; project.el can't dynamically populate the project li


From: Spencer Baugh
Subject: bug#63870: 29.0.90; project.el can't dynamically populate the project list
Date: Tue, 27 Jun 2023 15:27:30 -0400
User-agent: Gnus/5.13 (Gnus v5.13)

Add project-watch to discover projects with file-notify

Projects can be created outside of Emacs, but users might want to be
able to switch to them with project-switch-project immediately.  This
function supports that.  If a user calls (project-watch "~/src" 1) then
any projects under ~/src will be discovered automatically immediately
after their creation.

I have strived to make this function usable for a wide set of use-cases,
I think it will be a useful addition to project.el.

>From b283eb7eec428b8b0027a28f8c5f547360386b80 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Tue, 27 Jun 2023 15:25:02 -0400
Subject: [PATCH] Add project-watch to discover projects with file-notify

Projects can be created outside of Emacs, but users might want to be
able to switch to them with project-switch-project immediately.  This
API supports that.  If a user calls (project-watch "~/src" 1) then any
projects under ~/src will be discovered automatically immediately
after their creation.

* lisp/progmodes/project.el (project-check-project)
(project--watch-cb-children, project--watch-cb-this)
(project--file-notify-watch, project-watch): Add. (bug#63870)
---
 lisp/progmodes/project.el | 51 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 56c524bcab5..ddb033d50f9 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1884,5 +1884,56 @@ project-switch-project
     (let ((project-current-directory-override dir))
       (call-interactively command))))
 
+(defun project-check-project (dir)
+  "If there's a project at DIR, remember it; otherwise, forget it.
+
+Return the found project, if any."
+  (let ((pr (project--find-in-directory dir)))
+    (if pr (project-remember-project pr)
+      (project-forget-project (file-name-as-directory dir)))
+    pr))
+
+(defun project--watch-cb-children (recursive predicate event)
+  (unless (eq (cl-second event) 'stopped)
+    (dolist (file (cddr event))
+      (condition-case _ (project-watch file recursive predicate)
+        ((file-error file-notify-error))))))
+
+(defun project--watch-cb-this (dir event)
+  (unless (eq (cl-second event) 'stopped)
+    (when (project-check-project dir)
+      (file-notify-rm-watch (cl-first event)))))
+
+(defun project--file-notify-watch (dir callback &optional init)
+  "Like `file-notify-add-watch' but also calls CALLBACK immediately."
+  (let ((watch (file-notify-add-watch dir '(change) callback)))
+    (funcall callback (append (list watch 'started) init))))
+
+;;;###autoload
+(defun project-watch (dir &optional recursive predicate)
+  "Watch DIR until it becomes a project.
+
+We stop watching DIR once it becomes a project.
+
+If RECURSIVE is an integer greater than 0, we'll also run
+`project-watch' on directories which appear inside DIR,
+passing (1- RECURSIVE) as RECURSIVE.  To achieve this, we'll
+continue watching DIR even if it becomes a project.  This can be
+expensive, so it's better to pass small values of RECURSIVE, like
+1 or 2.
+
+If PREDICATE is non-nil, it should be a function which will be
+called with two arguments, the value of RECURSIVE and a
+directory.  Only directories for which PREDICATE returns non-nil
+will be watched for being a project."
+  (setq predicate (or predicate (lambda (_recursive dir) t)))
+  (setq recursive (or recursive 0))
+  (when (and (funcall predicate recursive dir) (file-directory-p dir))
+    (project--file-notify-watch dir (apply-partially #'project--watch-cb-this 
dir)))
+  (when (> recursive 0)
+    (project--file-notify-watch
+     dir (apply-partially #'project--watch-cb-children (1- recursive) 
predicate)
+     (directory-files dir 'full directory-files-no-dot-files-regexp))))
+
 (provide 'project)
 ;;; project.el ends here
-- 
2.39.3


reply via email to

[Prev in Thread] Current Thread [Next in Thread]