emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/denote c12d4fdb21 02/11: Add 'denote--get-all-used-ids'


From: ELPA Syncer
Subject: [elpa] externals/denote c12d4fdb21 02/11: Add 'denote--get-all-used-ids' and 'denote--find-first-unused-id'
Date: Sun, 8 Oct 2023 03:57:49 -0400 (EDT)

branch: externals/denote
commit c12d4fdb21058dc33543c545895d56e50ee4823d
Author: Jean-Philippe Gagné Guay <jeanphilippe150@gmail.com>
Commit: Jean-Philippe Gagné Guay <jeanphilippe150@gmail.com>

    Add 'denote--get-all-used-ids' and 'denote--find-first-unused-id'
---
 denote.el | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/denote.el b/denote.el
index 1d64b128f3..d86ba50b10 100644
--- a/denote.el
+++ b/denote.el
@@ -1662,6 +1662,31 @@ where the former does not read dates without a time 
component."
      (string-prefix-p identifier (file-name-nondirectory file)))
    (append (denote-directory-files) (denote--buffer-file-names))))
 
+(defun denote--get-all-used-ids ()
+  "Return a hash-table of all used identifiers.
+It checks files in variable `denote-directory' and active buffer files."
+  (let* ((ids (make-hash-table :test 'equal))
+         (file-names (mapcar
+                      (lambda (file) (file-name-nondirectory file))
+                      (denote-directory-files)))
+         (names (append file-names (denote--buffer-file-names))))
+    (dolist (name names)
+      (let ((id (when (string-match (concat "\\`" denote--id-regexp) name)
+                  (match-string-no-properties 0 name))))
+        (puthash id t ids)))
+    ids))
+
+(defun denote--find-first-unused-id (id used-ids)
+  "Return the first unused id starting at ID from USED-IDS.
+USED-IDS is a hash-table of all used IDs. If ID is already used,
+increment it 1 second at a time until an available id is found."
+  (let ((time (date-to-time id)))
+    (while (gethash
+            (format-time-string denote--id-format time)
+            used-ids)
+      (setq time (time-add time 1)))
+    (format-time-string denote--id-format time)))
+
 (defun denote--increment-identifier (identifier)
   "Increment IDENTIFIER.
 Preserve the date component and append to it the current time."



reply via email to

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