org-caldav icon indicating copy to clipboard operation
org-caldav copied to clipboard

Propose to create only necessary UID

Open linktohack opened this issue 4 years ago • 1 comments

Hello,

I have this function overwritten for a several months and haven't encountered syncing problems from so I would like to have your idea if I should make a PR. Basically we create only UID for either:

  • Headline with SCHEDULE or DEADLINE
  • Timestamp with active or active-range

I also have a an advice for org-save-all-org-buffers (s in org-agenda) to create these UIDs interactively.

    (el-patch-defun org-caldav-create-uid (file &optional bell)
      "Set ID property on headlines missing it in FILE.
When optional argument BELL is non-nil, inform the user with
a message if the file was modified. This func is the same as
org-icalendar-create-uid except that it ignores entries that
match org-caldav-skip-conditions."
      (let (modified-flag)
	(el-patch-swap
	  (org-map-entries
	   (lambda ()
	     (let ((entry (org-element-at-point)))
	       (unless (org-element-property :ID entry)
		 (unless (apply 'org-agenda-skip-entry-if org-caldav-skip-conditions)
		   (org-id-get-create)
		   (setq modified-flag t)
		   (forward-line)))))
	   nil nil 'comment)
	  (let ((timestamps
		 (thread-last
		     (org-element-map (org-element-parse-buffer) '(headline timestamp) 'identity)
		   (seq-map #'(lambda (el)
				(let ((ts (pcase el
					    (`(headline ,(map :scheduled :deadline) . ,_)
					     (or scheduled deadline))
					    (`(timestamp . ,_)
					     el))))
				  (pcase ts
				    (`(timestamp ,(map :type :begin) . ,_)
				     (and (or (eq type 'active) (eq type 'active-range)) begin))))))
		   (seq-filter #'identity)
		   (seq-reverse))))
	    (save-excursion
	      (mapc #'(lambda (pos)
			(goto-char pos)
			(org-back-to-heading t)
			(unless (org-element-property :ID (org-element-at-point))
			  (unless (apply 'org-agenda-skip-entry-if org-caldav-skip-conditions)
			    (org-id-get-create)
			    (setq id-pos pos)
			    (setq modified-flag t)
			    (forward-line))))
		    timestamps))))
	(when (and bell modified-flag)
	  (message "ID properties created in file \"%s\"" file)
	  (sit-for 2)))))

  (defun my/org-agenda-create-uid (&rest r)
    (thread-last
	(org-agenda-files)
      (seq-map (lambda (it)
		 (with-current-buffer (get-file-buffer it)
		   (org-set-tags-command '(4))
		   (org-caldav-create-uid it))))))
  (advice-add 'org-save-all-org-buffers :before #'my/org-agenda-create-uid)

linktohack avatar Feb 15 '21 09:02 linktohack

Haven't tried your patched function but would like to see this implemented.

jtoloe avatar Jan 09 '24 08:01 jtoloe