forked from emacs-tw/awesome-emacs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen-toc.el
33 lines (28 loc) · 1.08 KB
/
gen-toc.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
;;; gen-toc.el --- -*- lexical-binding: t; -*-
;; Generate Table Of Contents for Awesome-Emacs.
;; Copyleft (C) 2014 kuanyui
;; Author: kuanyui <[email protected]>
;; License: WTFPL 1.0
;; Usage: M-x eval-buffer, then switch to awesome-emacs's README.org, M-x awesome-emacs-gen-toc
(defun awesome-emacs-gen-toc ()
(interactive)
(let* ((fin "")
pos)
(save-excursion
(goto-char (point-min))
(replace-regexp "^#\\+BEGIN_QUOTE\n\\*Table of Contents\\*\n\\(?:.\\|\n\\)*+?#\\+END_QUOTE" "")
(setq pos (point)))
(goto-char (point-min))
(while (re-search-forward "^\\(\\*+\\) \\(.+\\)" nil :no-error)
(let* ((depth (length (match-string 1)))
(name (match-string 2)))
(setq fin (concat fin (format "%s- [[#%s][%s]]\n"
(make-string (* 2 (1- depth)) 32)
(replace-regexp-in-string
" " "-"
(replace-regexp-in-string
"[^A-z0-9- ]" "" (downcase name)))
name
)))))
(goto-char pos)
(insert (format "#+BEGIN_QUOTE\n*Table of Contents*\n%s#+END_QUOTE" fin))))