1

When I make an org mode entry AND insert a NOTE to it (C-c C-z) I get a Logbook drawer with my note in it. All is well.

Collapsed: enter image description here

Expanded: enter image description here

My question: can I somehow ADD an indication to the org entry (headline) so it shows me that there is a note under that headline?

So it looks something like this: enter image description here

It can be anything: a red dot or an icon; anything that would warn me about a note that I have stored under that headline.

Currently the only indication is '...' at the end of the headline, BUT that also indicates scheduled date, effort, clocked in time and so on. I want something that indicates that a note is present.

1 Answer 1

4

I think there would be problems if you actually modify the headline, so I'm suggesting an alternative implementation: use a tag to indicate that a note has been added to the headline. Tags are not folded when you fold the headline, so they will be visible all the time as you wish; they don't modify the headline; there is an API to handle them (not as conveniently as I was hoping, but still); and the API makes it easier to avoid inserting duplicate markers. The other nice thing about tags is that you can search for them. That's particularly easy if the file is an agenda file: then org-tags-view can search through the file(s) for tags matches. In some cases, I have been known to add a file to the agenda list temporarily (using C-c [ to add it and C-c ] to remove it afterwards), just so that I can use this mechanism to search for tags. You can also set up a sparse tree based on a combination of tags, properties and TODO state search criteria. The Tags chapter in the manual that I linked to above tells you all about them.

Given that you accept the tag idea for the implementation of what you want to do, the idea is to modify the function that C-c C-z calls, org-add-note. Since there is no hook to modify the action of this function (or the function it calls, org-add-log-setup), the only alternative is to modify the function. But we need a simple and controlled modification here: after the function is done what it is supposed to be doing, we check whether there is a note tag already on the headline and, if not, we add one. The idea is to write a function that does the additional things we need to do:

(defun ndk/org-mark-headline-for-note ()
  (let ((tags (org-get-tags)))
    (unless (seq-contains tags "note")
      (progn 
        (outline-back-to-heading)
        (org-set-tags (cons "note" tags))))))

i.e. get the current set of tags, find out if the tags list contains the tag note and if not, go back to the heading and set a new set of tags, the old one plus the new note one.

Then we modify the function org-add-note by advising it:

(with eval-after-load 'org
   (advice-add 'org-add-note :after #'ndk/org-mark-headline-for-note))

i.e. add an after advice to org-add-note, so that when its original code is done, then the new function is called afterwards to finish up.

All the code can go into your init file. The with-eval-after-load makes sure that the org-add-note function is already defined before we try to advise it - I don't know if that's actually necessary, but I think it's safer this way.


An alternative is to write a function composing the two functions above and then rebind the C-c C-z key to call the composite function:

(defun my/org-add-note ()
   (interactive)
   (org-add-note)                    ; call the original function
   (ndk/org-mark-headline-for-note)) ; then call the function above to add the tag

(with-eval-after-load 'org
   (define-key org-mode-map (kbd "C-c C-z") #'my/org-add-note))

You are basically doing "by hand" what advice-add did above. But I should point out that the advice mechanism can do a lot more than what I described above and it's worth knowing about its capabilities. The section in the Emacs Lisp manual that I linked to above, as well as the sections around it describe the mechanism in much more detail.


Either way, when you now do C-c C-z, you'll get a note added to a headline as before, but in addition, the note tag will also be added, if not there already.

EDIT: As can be seen in the discussion, the OP still cannot get a tag to be added to his headline. All of my tests have worked and I cannot see anything wrong with the code. Can anybody see a way in which this could fail to work?

EDIT 2: The OP was able to get this to work, apparently following an update to Org mode. However, he reported that if he uses #+filetags: foo for file-wide inherited tags, then the foo tag is added willy-nilly, along with the note tag, by the my/org-add-note function. The reason for that is that org-get-tags gets all tags, including inherited ones (and a fortiori, global ones inherited from a #filetags: specification). The cure is simple: org-get-tags takes a couple of (optional) arguments, the second of which specifies whether to use local tags only or inherited as well as local tags - see its doc string with C-h f org-get-tags. The modified ndk/org-mark-headline-for-note function then looks like this:

(defun ndk/org-mark-headline-for-note ()
  (let ((tags (org-get-tags nil t)))     ; use local tags only
    (unless (seq-contains tags "note")
      (progn 
        (outline-back-to-heading)
        (org-set-tags (cons "note" tags))))))

No other modification is needed.

6

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.