Subject: Re: bug#41741: [PATCH] Save project list as lisp data

From: Simen Heggestøyl <[email-masked]>
Date: Mon, 08 Jun 2020 21:00:36 +0200

Dmitry Gutov <[email-masked]> writes:

> On 07.06.2020 22:55, Simen Heggestøyl wrote:
>
>> It is transient now, yes.
>
> And later, it won't be?

Yes, later too (sorry, I think I misunderstood your original message).

>> Just as an example I can imagine an optional feature in the future where
>> you could name a project interactively the first time it's seen
>> (i.e. when it's added to the project list for the first time).
>
> And then lose all these annotations when a project root directory is
> moved/renamed/etc?
>
> But OK, maybe it won't happen too often to really worry about.

Yes. I think it should only be used to store metadata that isn't a
hassle to lose.

Another example from the top of my head: recording project visit
counts. That could be used for an option to show the most popular
projects first in the completion list, for instance.

>>> AKA:
>>>
>>>       (goto-char (point-min))
>>>       (read (current-buffer))
>>>
>>> Though the practical difference will be tiny.
>> I don't mind changing it, but could you explain the difference to
>> me? My
>> understanding isn't deep enough to see anything but one line versus
>> two. :)
>
> Just an extra string allocation, I guess.

Sure, I've changed it.

Updated patch attached!

-- Simen

>From 6a907d09b9a5b341f5c01f93fd031cb45229f320 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <[email-masked]>
Date: Fri, 5 Jun 2020 19:32:30 +0200
Subject: [PATCH] Save project list as lisp data

Save the project list file as lisp data instead of line separated
strings to make it more extendable in the future.

* lisp/progmodes/project.el (project--read-project-list)
(project--write-project-list, project--add-to-project-list-front)
(project--remove-from-project-list): Adjust to `project--list' now
being an alist.
---
 lisp/progmodes/project.el | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 4d57fb25fd..0cca518d26 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -763,13 +763,8 @@ project--read-project-list
           (when (file-exists-p filename)
             (with-temp-buffer
               (insert-file-contents filename)
-              (let ((dirs (split-string (buffer-string) "\n" t))
-                    (project-list '()))
-                (dolist (dir dirs)
-                  (cl-pushnew (file-name-as-directory dir)
-                              project-list
-                              :test #'equal))
-                (reverse project-list)))))))
+              (goto-char (point-min))
+              (read (current-buffer)))))))
 
 (defun project--ensure-read-project-list ()
   "Initialize `project--list' if it hasn't already been."
@@ -780,7 +775,8 @@ project--write-project-list
   "Persist `project--list' to the project list file."
   (let ((filename project-list-file))
     (with-temp-buffer
-      (insert (string-join project--list "\n"))
+      (insert ";;; -*- lisp-data -*-\n")
+      (pp project--list (current-buffer))
       (write-region nil nil filename nil 'silent))))
 
 (defun project--add-to-project-list-front (pr)
@@ -788,9 +784,9 @@ project--add-to-project-list-front
 Save the result to disk if the project list was changed."
   (project--ensure-read-project-list)
   (let ((dir (project-root pr)))
-    (unless (equal (car project--list) dir)
-      (setq project--list (delete dir project--list))
-      (push dir project--list)
+    (unless (equal (caar project--list) dir)
+      (setq project--list (assoc-delete-all dir project--list))
+      (push (list dir) project--list)
       (project--write-project-list))))
 
 (defun project--remove-from-project-list (pr-dir)
@@ -798,8 +794,8 @@ project--remove-from-project-list
 If the directory was in the list before the removal, save the
 result to disk."
   (project--ensure-read-project-list)
-  (when (member pr-dir project--list)
-    (setq project--list (delete pr-dir project--list))
+  (when (assoc pr-dir project--list)
+    (setq project--list (assoc-delete-all pr-dir project--list))
     (message "Project `%s' not found; removed from list" pr-dir)
     (project--write-project-list)))
 
-- 
2.26.2