; Do a little bit of grooming on an auto-gen'd html toc.  In particular, get
; rid of the right justifying ^A chars that will print period-filled leaders
; up to a page number.  Also get rid of the '' pairs, which were evidently
; inserted when curfile was empty in the modified version of $0 in stdhdr.me,
; q.v.
;
; Also, to force leading indentation, change "\ " to " ".  To avoid ugly
; anchor underlining, the  's must then be moved in front of the .(Ah 
; immeidately above.
;
; And a new addition for summer '01 -- put in the leveling.
;
; Summer '02 update -- I think the toc leveling needs to go later in the 
; merge-html-toc function, since that's where we lof and lot indices.
; Alternatively, we could do the lof/lot insertions here, but whatever.
;

(defun dot () (point))

(defun groom-html-toc ()
"Do a little bit of grooming on an auto-gen'd html toc.  In particular, get
rid of the right justifying ^A chars that will print period-filled leaders
up to a page number.  Assumed we're in file buffer toc.me, which is supplied
as a batch arg, or navigated to if we run this interactively."
    (interactive)

    ; The hope in the comment below that the find-file was causing a problem
    ; was stupid.  The problem, as noted below, is the t default value of
    ; case-fold search, which is not what we want.
    (setq case-fold-search nil)

    ;(find-file "toc.me") ; Hmm, having this here is useless; but worse, it
			  ; caused some really squirely errors when the
			  ; re-search below was changed from [1-9] to [A-Z1-9],
			  ; which change was done to make the new appendix
			  ; formatting look correct in the html toc.  Anyway,
			  ; we've fixed the problem, but without fully figuring
			  ; out why it was happening.  Chalk it up!
    (groom-html-toc-1)
    (write-file "toc-html.me")
)

(defun groom-html-toc-1 ()
"Work doer for groom-html-toc, after toc.me file is opened and before
toc-html.me file is saved.  Separating this function allows it to be run
interactively on an already open toc.me buffer.  Testing, testing, testing."
    (interactive)
    (while (search-forward "" nil t)

	; Backing up 5 chars takes care of the "\ \ " that's put in front of
	; every  char in the $0 macro in stdhdr.me, q.v. 
        (backward-char 5)

	; Killing the entire region up to eol takes care of everything
	; ncluding the line number itself.
	(setq d (dot))
	(end-of-line)
	(kill-region d (dot))
    )
    (beginning-of-buffer)
    (replace-string "''#" "#")
    (beginning-of-buffer)
    (replace-string "\\ " " ")
    (beginning-of-buffer)
    (while (search-forward-regexp "^ " nil t)
        (beginning-of-line)
	(setq d (dot))

	;
	; Monster-fucking global var problem rediscovered here.  The default
	; value of case-fold-search is t by default, which caused the search
	; pattern [A-Z] to fail miserably in this context.  Where's my
        ; precondition?!?
	;
        (search-forward-regexp "[A-Z1-9]")
	; Where's my precond, Chapter 2.  Version 21 of emacs finally started
	; enforcing its concat precond that disallows int args.
        (message (concat "At pos " (int-to-string (dot))))
	(backward-char 1)
	(kill-region d (dot))
	(search-backward ".(Ah")
	(yank)
	(newline)
	(forward-line 2)
    )
)