When an outline tree of headers

* top 1
** something 
** something 
** something 
*** something 
**** something
***** something
****** something

is exported, say, to html, org-mode produces a numerical outline as deep as the options are set

1. top
1.1. something
1.2. something 
1.3. something
1.3.1. something something something something 

Now, is there any way to actually capture that outline numeration for use in the original buffer? I'm thinking I would like to make a sort of tags or custom_id system based on these numbers.

* top :1:
** something :1.1:
** something :1.2:
** something :1.3:
*** something :1.3.1:
**** something :
***** something :
****** something :

It would also be nice if org-mode simply used these numbers in the original buffer instead of stars.


Well, it would be a bit silly to have to export it to an html file and then read the result back in just to parse out the headline numbers. What you can do instead is call the functions that parse the buffer and use that information to add the tags.

I don't know the org source code very well, but it's usually pretty easy to get started with these things. I first double checked the manual to see how you actually start an html export (I don't export my org mode files, so I couldn't remember). As usual, the manual gives both the key sequence and the function name, so I started by looking at the help for this function: C-h f org-html-export-to-html. This help buffer includes a link to the source code, so I followed it. This function calls org-export-to-file, which is longer, so I just skimmed it. After setting some variables and checking some conditions, it calls org-export-as. Several steps later I found org-export-collect-tree-properties. This appears to be the function you want; it can collect the headline numbers for you; if the second argument is '(:headline-numbering) it'll return exactly that.

Of course it doesn't operate on the buffer directly, it looks like you have to first parse the buffer with org-element-parse-buffer first; this examines the current buffer and parses it to build an AST.

Once you've called org-export-collect-tree-properties you'll have an alist mapping headlines to headline-numbers and can go to each headline and add/remove tags as needed.

That's about as far as I can go at so late an hour, but it should be enough to get you started.

As for using numbered headlines from the get-go, the problem then becomes one of renumbering everything whenever you move nodes around. Using unordered symbols such as asterisks eliminates that headache.

  • Great answer! We need more detailed answers like these. – Kaushal Modi Nov 6 '15 at 13:46
  • Yes, thanks. I tried something the org-mode list suggested, i.e., to indeed capture the output and carve it. In any event, I think org-mode could really use this sort of outlining capability. "Stars-only" for headers in the original buffer is too restrictive. I'm a real fan of things being broken down into fine-grained outline formats in order to reduce the opaque "blobbiness" of things, text/prose or even code. In a way I'm looking for a sort of generalized "ctags/etags". – 147pm Nov 6 '15 at 15:49

Your Answer

