0

I want to automatically remove 'tag1', from a heading if I either assign a scheduled or due date or 'tag2' to it.

Just to clarify, there is:

* Some heading :tag1:tag3:

If I add 'tag2', it becomes:

* Some heading :tag2:tag3:

If I add a scheduled date, it becomes:

* Some heading :tag3:
  SCHEDULED: <2022-09-21 Wed>

Can this be done? How?

3
  • It depends on how you add tags: if you do it manually, then you will have to delete it manually. If you use org-set-tags-command (bound to C-c C-q in the Org mode keymap), then you could advise org-set-tags to check the tags: if both tags1 and tags2 are present, then delete tags1 before the "real" org-set-tags is called. Similarly, you can advise org-schedule, org-deadline and/or org-time-stamp with an after advice: if they've inserted scheduling information, then call org-get-tags, check for tags1 , delete if if present and call org-set-tags with the new tag set.
    – NickD
    Sep 21 at 18:36
  • @NickD Thanks. I use org-set-tags-command to manage tags. I guess, I will need to write a function to remove 'tag1' if present and then add that function to the advice. (advice-add :before 'org-set-tags-command #'remove-tag1) (advice-add :after 'org-schedule #'remove-tag1) But no matter how hard I try, I am not able to get the 'remove-tag1' function. Could you please provide some pointers?
    – deshmukh
    Sep 22 at 7:50
  • I provided a (partial) answer on how to remove a tag under certain conditions. HTH.
    – NickD
    Sep 22 at 14:01

2 Answers 2

0

Partial answer: how to remove a tag

  (defun ndk/remove-tag ()
      (org-set-tags (delete "tag1" (org-get-tags))))

This needs to be applied while at the appropriate headline.

1
  • Sorry - I got rid of the extraneous let.
    – NickD
    Sep 22 at 18:27
0

You can use the org-after-tags-change-hook hook to remove TAG1 conditionally.

(defun remove-tag-if (&optional remove-tag1)
  (let ((tags (org-get-tags nil 'local)))
    (when (or (and remove-tag1 (member "tag1" tags))
              (and (member "tag1" tags) (member "tag2" tags)))
      (org-set-tags (delete "tag1" tags)))))

(add-hook 'org-after-tags-change-hook 'remove-tag-if)

And to also remove it after org-schedule, use:

(advice-add 'org-schedule :after (lambda (_) (remove-tag-if 'tag1)))
1
  • 1
    Instead of (member (and "tag1" "tag2") tags)), I think you mean (and (member "tag1" tags) (member "tag2" tags)) - right?
    – NickD
    Sep 23 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.