Hi Andrea, This is a nice idea. Here are a few notes:
1. You should implement it as a minor mode, and enable/disable the hooks there. 2. I would generally recommend using org-element to help with parsing. It will make your code much cleaner and easier to understand. Much of the searching code you currently have is unidiomatic and hard to follow, and org-element will help with that. 3. Avoid using hard-coded to-do keywords, because users may be using custom ones. Instead, use the variables and functions provided by Org that are related to to-do keywords. Use tools like apropos, Helm, or Counsel, or the Org source code, to help discover them.