5

I would like to get the complete hierarchy numbers in the plain lists of org-mode

Instead of this:

1. something
   1. something
   2. something
      1. something
2. something
   1. something
   2. something
   3. something

I'm looking for something like this:

1. something
1.1. something
1.2. something
1.2.1. something
2. something
2.1. something
2.2. something
2.3. something

And the same thing in headlines. Instead of this:

* chapter
** section A
** section B
*** subsection 
* chapter
** section A
** section B 

this:

* 1. chapter
** 1.1. section A
** 1.2. section B
*** 1.2.1. subsection 
* 2. chapter
** 2.1. section A
** 2.2. section B 

This helps me to see how many children a headline has and where I am.

EDIT: Is it at least possible to get the table of contents with the complete numbering of the headlines (for easier navigating in the org document)?

New EDIT: I mean a new buffer with just these lines (the items are empty):

* 1. chapter
** 1.1. section A
** 1.2. section B
*** 1.2.1. subsection 
* 2. chapter
** 2.1. section A
** 2.2. section B 

Of course every line of the "new buffer" should be linked with the corresponding headline of the org document: So by clicking or typing "RETURN" I can go to the headline directly.

I know that by exporting to txt I can get the TOC but there aren't the links to the headlines. What I'm looking for (meanwhile) is a function that construct the toc with numbers and with links.

  • 1
    What you want doesn't exist (as far as I know). – Dan Apr 26 '17 at 17:56
  • Dan is right (afaik). You can only see the structure with full numbering after exporting to e.g. latex, odt, or text. – NickD Apr 26 '17 at 18:05
  • @rl1: not sure what you mean. I don't think org does a table of contents in the org buffer. Maybe you mean when you export it? – Dan Apr 27 '17 at 15:49
  • @Dan No, I mean before exporting. I have edited the post. – rl1 Apr 27 '17 at 16:42
4

Here is a lightly tested approach to use overlays on the headings. This will work up to 9th level headings (one less zero than is in the counters).

(require 'cl)
(require 'dash)
(defun overlay-numbered-headings ()
  "Put numbered overlays on the headings."
  (interactive)
  (loop for (p lv) in (let ((counters (copy-list '(0 0 0 0 0 0 0 0 0 0)))
                (current-level 1)
                last-level)
            (mapcar (lambda (x)
                  (list (car x)
                    ;; trim trailing zeros
                    (let ((v (nth 1 x)))
                      (while (= 0 (car (last v)))
                        (setq v (butlast v)))
                      v)))
                (org-map-entries
                 (lambda ()
                   (let* ((hl (org-element-context))
                      (level (org-element-property :level hl)))
                     (setq last-level current-level
                       current-level level) 
                     (cond
                      ;; no level change or increase, increment level counter
                      ((or (= last-level current-level)
                       (> current-level last-level))
                       (incf (nth current-level counters)))

                      ;; decrease in level
                      (t
                       (loop for i from (+ 1 current-level) below (length counters)
                         do
                         (setf (nth i counters) 0))
                       (incf (nth current-level counters))))

                     (list (point) (-slice counters 1)))))))
    do
    (let ((ov (make-overlay p (+ 1 p))))
      (overlay-put ov 'display (concat (mapconcat 'number-to-string lv ".") ". "))
      (overlay-put ov 'numbered-heading t))))


(define-minor-mode numbered-org-mode
  "Minor mode to number org headings."
  :init-value nil
  (if numbered-org-mode 
      (overlay-numbered-headings)
    (ov-clear 'numbered-heading)))
  • Just "require"? Actually, I'm not sure wich packages you mean (still an emacs beginner). With cl you mean: elpa.gnu.org/packages/cl-lib.html ? (there are other cl-* packages) dash I've found by using M-x list-packages. Is ov the name abbreviation of some package - I couldn't find it by M-x list-packages. – rl1 Apr 28 '17 at 11:39
  • I have edited the answer with the requires you need. ov is a package in Melpa, but this solution doesn't use any features from it; it uses builtin overlay functions. – John Kitchin Apr 28 '17 at 14:02
  • Thanks. This is exactly what I'm looking for. I can generate the numbers and remove them. BUT It works only at the first time: M-x overlay-numbered-headings gives the correct numbering (let's say 1. 2. 3.) . M-x remove-numbered-overlays remove them as expected. But a second call of M-x overlay-numbered-headings gives a strange numbering (4. 5. 6.): It seems to save the old values (?) – rl1 Apr 28 '17 at 19:55
  • sure enough. That must have to do with some kind of closure I guess. I added a line to make a copy of the counters list and also added a minor mode to make toggling it on and off easier. – John Kitchin Apr 28 '17 at 20:39
  • Great! It's working and it's very easy to use (and for me it is important that it doesn't change the document). I think in this version you have forgotten the require commands (cl and dash). – rl1 Apr 29 '17 at 9:40
1

An alternative implementation of the overlay solution provided in John Kitchin’s answer is to just use the standard functions from org-export. I have wrapped this up in a small library here: https://gitlab.com/andersjohansson/org-outline-numbering

The core is this function (which serves as a direct replacement for Kitchin’s overlay-numbered-headings):

(require 'ox)
(require 'cl-lib)
(require 'ov)

(defun org-outline-numbering-overlay ()
  "Put numbered overlays on the headings."
  (interactive)
  (cl-loop for (p lv) in
           (let* ((info (org-combine-plists
                         (org-export--get-export-attributes)
                         (org-export--get-buffer-attributes)
                         (org-export-get-environment)
                         '(:section-numbers t)))                      
                  (tree (org-element-parse-buffer))
                  numberlist)
             (org-export--prune-tree tree info)
             (setq numberlist
                   (org-export--collect-headline-numbering tree info))
             (cl-loop for hl in numberlist
                      collect (cons
                               (org-element-property :begin (car hl))
                               (list (cdr hl)))))
           do
           (let ((ov (make-overlay p (+ (length lv) p))))
             (overlay-put ov 'display (concat (mapconcat 'number-to-string lv ".") ". "))
             (overlay-put ov 'numbered-heading t)
             (overlay-put ov 'face 'default))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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