Problem: Make org-id more human readable while ensuring uniqueness.
Persistent daily counter β highjacking 'ts format.
;; org-id-counter - definition and infrastructure for org-id format %date-%counter
(defvar org-id-counter (make-hash-table :size 1 :test 'equal)
"Hash table to store the counter for each day.")
(defvar org-id-counter-location (locate-user-emacs-file "org-id-counter"))
(setq org-id-method 'ts)
(defun org-id-counter-format ()
"Generate an Org ID using the format %Y/%m/%d-%counter."
(let* ((today (format-time-string "%Y/%m/%d"))
(counter (or (gethash today org-id-counter) 1))
(id (format "%s:%s" today (number-to-string counter))))
(puthash today (1+ counter) org-id-counter)
id))
;; iteration logic
(defun org-id-counter-advice (&rest args)
"Advice function to set `org-id-ts-format` before calling `org-id-new`."
(setq org-id-ts-format (funcall #'org-id-counter-format)))
(advice-add 'org-id-new :before #'org-id-counter-advice)
;; Persistent counter for the day
(defun org-id-counter-save ()
"Save org-id-counter hash table to disk."
(with-temp-file org-id-counter-location
(prin1 org-id-counter (current-buffer))))
(add-hook 'kill-emacs-hook #'org-id-counter-save) ;; Save the hash table on Emacs exit
(defun org-id-counter-load ()
"Load org-id-counter hash table from disk."
(when (file-exists-p org-id-counter-location)
(if (= 0 (nth 7 (file-attributes org-id-counter-location))) ; Check if file is empty
(setq org-id-counter (make-hash-table :test 'equal)) ; Initialize with empty hash table
(with-temp-buffer
(insert-file-contents org-id-counter-location)
(let ((data (read (current-buffer))))
(when (hash-table-p data)
(setq org-id-counter data)))))))
(org-id-counter-load) ;; Load the hash table on Emacs start
;; debug
(defun org-id-counter-show ()
"Display the contents of org-id-counter hash table."
(interactive)
(message "Contents of org-id-counter hash table:")
(maphash (lambda (key value)
(message "%s: %s" key value))
org-id-counter))
(defun org-id-counter-reset ()
"Reset the counter on request.
Caution: make sure you make appropriate provisions to not have conflicting IDs"
;(interactive)
(setq org-id-counter (make-hash-table :test 'equal)))
(provide 'org-id-counter)
Lightly tested - will update on finding bugs and the like.