5

I'm planning to move StackMode to a literate program once it matures, but until then I'm using the conventional ^L section markers.

My files look like this:

;;; Code:

^L
;;; Section 1 Title

(defun code () things)

^L
;;; Section 2 Title

(defun bleh () bergh)

I'd like to be able to add a hook that creates a table of contents at some marker, say ^;;; Contents:$:

;;; Contents:

;;;  1. Section 1 Title
;;;  2. Section 2 Title

;;; Code:

^L
;;; Section 1 Title

(defun code () things)

^L
;;; Section 2 Title

(defun bleh () bergh)

For extra fun, subsections could be repeated linefeed characters:

^L^L
;;; Subsection
  • Off topic, is this repo the latest version of this package? I was going to do something similar, but if you're very far along maybe I won't need to. – Malabarba Oct 29 '14 at 18:44
  • @Malabarba It is, but I just pushed this branch up (experiment). I expect it will get much farther along than the Org-mode version has been, especially since I now have a well-defined interested audience :). If you have any resources / ideas, don't hesitate to get in touch :) My email is available in the git repo; please use the one listed on experiment if you want to get in touch that way. (Just putting information out there; use it or leave it. :)) – Sean Allred Oct 29 '14 at 22:28
5

This function will find all section headers and collect them in a list, then print the list in reverse order:

(defun my-table-of-contents ()
  (interactive)
  (goto-char (point-min))
  (let ((results nil))
    (while (re-search-forward "^^L\n;;; \\(.+\\)" nil t)
      (setq results (cons (match-string 1) results)))

    ;; print table of contents
    (goto-char (point-min))
    (insert ";;; Contents:\n\n")
    (let ((n 1))
      (dolist (heading (reverse results))
        (insert (format ";;; %d. %s\n" n heading))
        (setq n (1+ n))))))

The ^L in the regular expression string should be input as an actual control character (C-q C-l).

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.