Skip to content

Commit

Permalink
Refine zetteldeft-generate-id, for rapid unique ID generation
Browse files Browse the repository at this point in the history
Adds a loop to check if ID is unavailable, incrementing ID until it is unique
Avoids error message and the need to wait for time to change in orer to
create a unique ID.
  • Loading branch information
localauthor committed Nov 20, 2021
1 parent f4f227a commit 9897983
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions zetteldeft.el
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ that function is used and TITLE and FILENAME are passed to it."
(if-let ((f zetteldeft-custom-id-function))
(funcall f title filename)
(format-time-string zetteldeft-id-format))))
(if (zetteldeft--id-available-p id)
id
(error "Generated ID %s is not unique." id))))
(while (zetteldeft--id-unavailable-p id)
(setq id (number-to-string (1+ (string-to-number id)))))
id))

(defun zetteldeft--id-available-p (str)
"Return t only if provided string STR is unique among Zetteldeft filenames."
(defun zetteldeft--id-unavailable-p (str)
"Return t if provided string STR occurs among Zetteldeft filenames."
(let ((deft-filter-only-filenames t))
(deft-filter str t))
(eq 0 (length deft-current-files)))
(not (eq 0 (length deft-current-files))))

(defcustom zetteldeft-custom-id-function nil
"User-defined function to generate an ID.
Expand Down

0 comments on commit 9897983

Please sign in to comment.