Assign IDs to every entry in Org-mode

前端 未结 2 698
情深已故
情深已故 2020-12-25 09:04

Org-mode has a bundled extension called org-id, that implements global unique IDs for org-mode files. Every entry (a headline with its body) can have an ID property in its <

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-25 09:12

    I searched through the source and couldn't find anything obvious to do this already. I know it's used by Org-mobile, but I've not much experience of that.

    By way of a starter, the following snippet will loop through all outline headings in the current file (Org-mode headings are basically standard Emacs outline headings), and add an ID if there's not already one there:

       (require 'org-id)
       (save-excursion
          (goto-char (point-max))
          (while (outline-previous-heading)
            (org-id-get-create)))
    

    (If you're wondering why this loop goes backwards over the buffer rather than forwards, it's because the while loop will always call the outline navigation function at least once and if your Org-mode file starts with a heading this would be skipped if going forwards.)

    This could be reasonably easily used within a loop over all the files known to the Org-mode agenda by looping over the entries returned by the function (org-agenda-files), or added to a save hook for Org-mode files.

    If someone more nimble-fingered than I doesn't get there first, if you can identify when you'd like the IDs added I could probably expand the above to be a complete solution.

提交回复
热议问题