I have an idea where I have something like a FOCUS tag that I can apply to tasks that need special attention.

However, I want to make it so that only 3 tasks can actually be tagged with FOCUS at a time. If I try to tag a fourth with FOCUS, I should just get an error. So in order to apply a FOCUS tag, the count of currently FOCUS-tagged tasks should be <3, otherwise I'll have to untag one.

Any idea on how to get started on doing this?


1 Answer 1


The following concept draft idea will comb through the org-agenda-files and gather all the tags that fall within ambit of regexp group 5 to org-complex-heading-regexp, and then search through that list to find out how many matches of a particular tag. The original poster may wish to perform a similar test when assigning a particular tag.

USAGE: (stop-or-go "NAME_OF_TAG")

(require 'cl) ;; for loop
(require 'org)
(require 'org-agenda)

(defun how-many-tags (tag)
"Tag must be a string without any colons."
  (let ((tag-list
          (loop for file in (org-agenda-files nil 'ifmode)
            (let (result)
              (org-check-agenda-file file)
              (with-current-buffer (find-file-noselect file)
                  (goto-char (point-max))
                  (while (re-search-backward org-complex-heading-regexp nil t)
                    (push (match-string-no-properties 5) result)))
      (delq nil
          (lambda (x) (when (and x (string-match (concat ":" tag ":") x)) x))

(defun stop-or-go (tag)
"The default hard-coded count is 3.  Tag must be a string without any colons."
  (let ((count (how-many-tags tag)))
    (if (>= count 3)
      (message "STOP!:  %s" count)
      (message "GO!:  %S" count))))
  • The concept code works like a charm, and is very likely on the right path. The trouble I'm having now is finding a way to hook this into org's tag system.
    – wiuah
    Nov 12, 2016 at 1:02
  • When I use C-c C-q for org-set-tags-command, it tags the task and then runs the org-after-tags-change-hook. If I use org-toggle-tag or org-set-tags-at in stop-and-go to check and remove FOCUS, and then hook that, it will cause an infinite loop (since they call org-set-tags internally, which runs the hook). Are there any tag modification commands which don't?
    – wiuah
    Nov 12, 2016 at 1:02
  • The function org-set-tags is somewhat lengthy and would require a couple of cups of coffee to digest. I'll have a look at it over the weekend and give it some thought.
    – lawlist
    Nov 12, 2016 at 5:07
  • It looks like a potential location to hack org-set-tags-command is right after the comment that reads: ;; Insert new tags at the correct column. I've never tried to add more than one tag, but it appears as though multiple tags are expressly contemplated. Therefore, each tag may need to be tested using something akin to the concept code above -- e.g., transform the string of tags at that point from ":tag1:tag2:tag3:" to a list '("tag1" "tag2" "tag3") and each element of the list would be tested using the concept code above. If any element fails the test, then throw an error/stop!
    – lawlist
    Nov 13, 2016 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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