changeset 659: |
cad61259ba57 |
parent 658: |
804b5ee20a46 |
child 660: |
da507f0274b3 |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Fri, 20 Sep 2024 19:59:36 -0400 |
files: |
emacs/lib/ulang.el |
description: |
org-generate-custom-ids |
1.1--- a/emacs/lib/ulang.el Thu Sep 19 23:23:02 2024 -0400
1.2+++ b/emacs/lib/ulang.el Fri Sep 20 19:59:36 2024 -0400
1.3@@ -94,26 +94,44 @@
1.4 ("so" . "https://stackoverflow.com/%s")))
1.5
1.6 ;;; IDs
1.7-(defun org-custom-id-get (&optional pom create prefix)
1.8- "Get the CUSTOM_ID property of the entry at point-or-marker POM.
1.9- If POM is nil, refer to the entry at point. If the entry does
1.10- not have an CUSTOM_ID, the function returns nil. However, when
1.11- CREATE is non nil, create a CUSTOM_ID if none is present
1.12- already. PREFIX will be passed through to `org-id-new'. In any
1.13- case, the CUSTOM_ID of the entry is returned."
1.14- (interactive)
1.15-(org-with-point-at pom
1.16- (let ((id (org-entry-get nil "CUSTOM_ID"))
1.17- ;; use CUSTOM_ID for links
1.18- (org-id-link-to-org-use-id 'create-if-interactive-and-no-custom-id))
1.19- (cond
1.20- ((and id (stringp id) (string-match "\\S-" id))
1.21- id)
1.22- (create
1.23- (setq id (org-id-new prefix))
1.24- (org-entry-put pom "CUSTOM_ID" id)
1.25- (org-id-add-location id (buffer-file-name (buffer-base-buffer)))
1.26- id)))))
1.27+
1.28+(defun org-title-to-filename (title)
1.29+ "Convert TITLE to a reasonable filename."
1.30+ ;; Based on the slug logic in org-roam, but org-roam also uses a
1.31+ ;; timestamp.
1.32+ (setq title (downcase title))
1.33+ (setq title (s-replace-regexp "[^a-zA-Z0-9]+" "-" title))
1.34+ (setq title (s-replace-regexp "-+" "-" title))
1.35+ (setq title (s-replace-regexp "^-" "" title))
1.36+ (setq title (s-replace-regexp "-$" "" title))
1.37+ title)
1.38+
1.39+(defun org-get-custom-id-list ()
1.40+ (flatten
1.41+ (org-map-entries
1.42+ (lambda ()
1.43+ (org-entry-get nil "CUSTOM_ID")))))
1.44+
1.45+(defun org-generate-custom-id (&optional id-list)
1.46+ (let* ((custom-id (org-entry-get nil "CUSTOM_ID"))
1.47+ (heading (org-heading-components))
1.48+ (level (nth 0 heading))
1.49+ (todo (nth 2 heading))
1.50+ (headline (nth 4 heading))
1.51+ (slug (org-title-to-filename headline))
1.52+ (duplicate-id (when id-list (member slug id-list))))
1.53+ (when (not duplicate-id)
1.54+ (message "Adding CUSTOM_ID %s to %s" slug headline)
1.55+ (org-entry-put nil "CUSTOM_ID" slug))))
1.56+
1.57+(defun org-generate-custom-ids ()
1.58+ "Generate CUSTOM_ID for any headings that are missing one"
1.59+ (save-excursion
1.60+ (org-with-wide-buffer
1.61+ (let ((existing-ids (org-get-custom-id-list)))
1.62+ (org-map-entries
1.63+ (lambda ()
1.64+ (org-generate-custom-id existing-ids)))))))
1.65
1.66 ;;;###autoload
1.67 (defun org-id-add-to-headlines-in-file ()
1.68@@ -122,12 +140,6 @@
1.69 (interactive)
1.70 (org-map-entries (lambda () (org-id-get (point) 'create))))
1.71
1.72-(defun org-custom-id-add-to-headlines-in-file ()
1.73- "Add CUSTOM_ID properties to all headlines in the
1.74- current file which do not already have one."
1.75- (interactive)
1.76- (org-map-entries (lambda () (org-custom-id-get (point) 'create))))
1.77-
1.78 (defun org-id-add-to-headlines-in-files (&optional files)
1.79 (interactive)
1.80 (with-temp-buffer