unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Spencer Baugh <sbaugh@janestreet.com>
To: 63870@debbugs.gnu.org
Subject: bug#63870: 29.0.90; project.el can't dynamically populate the project list
Date: Tue, 27 Jun 2023 15:27:30 -0400	[thread overview]
Message-ID: <ierbkh01za5.fsf@janestreet.com> (raw)
In-Reply-To: <ierjzwkahgh.fsf@janestreet.com> (Spencer Baugh's message of "Sat, 03 Jun 2023 07:55:10 -0400")

[-- Attachment #1: Type: text/plain, Size: 488 bytes --]


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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-project-watch-to-discover-projects-with-file-not.patch --]
[-- Type: text/x-patch, Size: 3362 bytes --]

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


  parent reply	other threads:[~2023-06-27 19:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-03 11:55 bug#63870: 29.0.90; project.el can't dynamically populate the project list Spencer Baugh
2023-06-15 19:30 ` Spencer Baugh
2023-06-16  5:45   ` Eli Zaretskii
2023-06-17  2:55 ` Dmitry Gutov
2023-06-27 19:29   ` Spencer Baugh
2023-06-27 19:27 ` Spencer Baugh [this message]
2023-06-28 11:24   ` Eli Zaretskii
2023-06-28 12:05     ` Spencer Baugh
2023-06-28 12:18       ` Eli Zaretskii
2023-06-28 12:37         ` Spencer Baugh
2023-06-28 12:56           ` Eli Zaretskii
2023-07-18  2:21             ` Dmitry Gutov
2023-07-18 16:28               ` Spencer Baugh
2023-07-18 17:41                 ` Juri Linkov
2023-07-27  1:59                   ` Dmitry Gutov
2023-07-27  1:57                 ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ierbkh01za5.fsf@janestreet.com \
    --to=sbaugh@janestreet.com \
    --cc=63870@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).