13

Q: Is there a way to prepend zeros to org-mode numbered lists?

Text alignment in numbered lists starts to look unpleasant when numbers get into the double digits:

* A numbered list

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4
  5. Item 5
  6. Item 6
  7. Item 7
  8. Item 8
  9. Item 9
  10. Item 10   # ah, my eyes!

The org-mode manual on plain lists states that:

Items belonging to the same list must have the same indentation on the first line. In particular, if an ordered list reaches number ‘10.’, then the 2–digit numbers must be written left-aligned with the other numbers in the list. An item ends before the next line that is less or equally indented than its bullet/number.

So it looks like org-mode needs the digits to left-align. As such, I'm wondering if it's possible to prepend zeros onto the single-digit items so that the text aligns nicely:

* A zero-padded numbered list

  01. Item 1
  02. Item 2
  03. Item 3
  04. Item 4
  05. Item 5
  06. Item 6
  07. Item 7
  08. Item 8
  09. Item 9
  10. Item 10   # much better!

Out of curiosity, I tried hand-entering the zeros in a list and hitting C-c C-c, which simply strips them out again.

So: is there any way to convince org-mode to prepend zeros in numbered lists?

(Note: question inspired by this S.O. thread.)

10

The following diff on org-list.el does the trick!

diff --git a/lisp/org-list.el b/lisp/org-list.el
index b1d47c9..509e79f 100644
--- a/lisp/org-list.el
+++ b/lisp/org-list.el
@@ -1641,7 +1641,8 @@ as returned by `org-list-prevs-alist'."
      ;; Num bullet: increment it.
      ((string-match "[0-9]+" bullet)
       (replace-match
-       (number-to-string (1+ (string-to-number (match-string 0 bullet))))
+       (format (format "%%0%dd" (length (match-string 0 bullet)))
+          (1+ (string-to-number (match-string 0 bullet))))
        nil nil bullet))
      ;; Alpha bullet: increment it.
      ((string-match "[A-Za-z]" bullet)
@@ -1715,7 +1716,7 @@ This function modifies STRUCT."
          (replace-match "a" nil nil bullet))
         ;; First bullet is num: use "1".
         ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
-         (replace-match "1" nil nil bullet))
+         (replace-match (format (format "%%0%dd" (1+ (log (length struct) 10))) 1) nil nil bullet))
         ;; Not an ordered list: keep bullet.
         (t bullet)))))))))
     (mapc fix-bul (mapcar 'car struct))))

The first chunk patches function org-list-inc-bullet-maybe to keep the padding used on the first item. The second chunk patches function org-list-struct-fix-bul to calculate the padding based on the number of elements in the list.

  • 2
    Nice! It would be great if you could submit that (preferably with an option to toggle it) to the org-mode developers. – Saint Aardvark the Carpeted Dec 12 '14 at 14:59

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.