From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh Newsgroups: gmane.emacs.bugs Subject: bug#63870: 29.0.90; project.el can't dynamically populate the project list Date: Tue, 27 Jun 2023 15:27:30 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29065"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: 63870@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jun 27 21:28:26 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qEEMU-0007Lv-BR for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 27 Jun 2023 21:28:26 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEEM8-0008Bd-Kv; Tue, 27 Jun 2023 15:28:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEEM6-0008BF-CU for bug-gnu-emacs@gnu.org; Tue, 27 Jun 2023 15:28:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qEEM6-0006yg-37 for bug-gnu-emacs@gnu.org; Tue, 27 Jun 2023 15:28:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qEEM5-0001AH-QS for bug-gnu-emacs@gnu.org; Tue, 27 Jun 2023 15:28:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Spencer Baugh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 27 Jun 2023 19:28:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63870 X-GNU-PR-Package: emacs Original-Received: via spool by 63870-submit@debbugs.gnu.org id=B63870.16878940594450 (code B ref 63870); Tue, 27 Jun 2023 19:28:01 +0000 Original-Received: (at 63870) by debbugs.gnu.org; 27 Jun 2023 19:27:39 +0000 Original-Received: from localhost ([127.0.0.1]:49551 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qEELi-00019i-Sh for submit@debbugs.gnu.org; Tue, 27 Jun 2023 15:27:39 -0400 Original-Received: from mxout5.mail.janestreet.com ([64.215.233.18]:38145) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qEELg-00019S-Eo for 63870@debbugs.gnu.org; Tue, 27 Jun 2023 15:27:37 -0400 In-Reply-To: (Spencer Baugh's message of "Sat, 03 Jun 2023 07:55:10 -0400") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:264174 Archived-At: --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Add-project-watch-to-discover-projects-with-file-not.patch >From b283eb7eec428b8b0027a28f8c5f547360386b80 Mon Sep 17 00:00:00 2001 From: Spencer Baugh 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 --=-=-=--