(defun dot () (point))

(defun merge-html-toc ()
"Merge the auto-gen'd toc.html into the locale of the <! -- toc --> comment, if
it is present.  If the toc comment was found, save the buffer, otherwise do
nothing.  To allow postscript and html tocs to be in different locations,
e.g., in a book before and after the preface, resp., there may be separate
troff vs html toc lcations, designatied toc_html and toc_ps, resp.  In this
case, the toc goes where the <! -- toc_html --> comment appears."
    (interactive)
    (if (search-forward "\n<!-- toc_html -->\n" nil t)
      (progn
	(merge-html-toc-1)
      )
      (progn
	(beginning-of-buffer)
	(if (search-forward "\n<!-- toc -->\n" nil t)
	  (progn
	    (merge-html-toc-1)
	  )
	)
      )
    )
)

(defun merge-html-toc-1 ()
"Work doer for merge-html-toc."
    (interactive)
    (previous-line 1)
    (beginning-of-line)
    (kill-line 1)
    (insert-string "<br>\n<br>\n<br>\n<blockquote>\n")
    (insert-string "</blockquote>\n")
    (previous-line 1)
    (insert "??????")
    (backward-char 6)
    (insert-file "toc.html")
    (search-forward "??????")
    (backward-delete-char 6)

    ; Put in links to the list of figures and tables files if they exist.
    (add-lof-lot-links)

    ; Add toc leveling to index.html and toc-?.html.
    (add-html-toc-leveling)

    (save-buffer)
)

(defun add-lof-lot-links ()
"Add list-of-figure and/or list-of-table links at the end of the current
buffer." 
    (if (lof-non-empty "lof-raw.me")
      (progn
	(insert
          (concat
	    "<br>\n<br>\n<br>\n"
	    "<center>\n"
	    "<a href=lof.html>\n"
	    "<strong>List of Figures</strong>\n"
	    "</a>\n"
	  )
        )
	(if (lof-non-empty "lot-raw.me")
	    (insert
	      (concat
		"&nbsp;&nbsp;|&nbsp;&nbsp;\n"
	        "<a href=lot.html>\n"
		"<strong>List of Tables\n</strong>"
	        "</a>\n"
	      )
	    )
	)
	(insert "</center>\n")
      )
    )
)

(defun lof-non-empty (file)
"Determine of if a list-of-figures or list-of-tables file is non-empty.  This
means there's some stuff after the pre-defined header that ends in ``.spt''."
    (interactive)
    (let ((rtn nil))
        (setq buf (buffer-name))
	(find-file file)
	(end-of-buffer)
	(setq rtn
	      (not (string= (buffer-substring (1- (dot)) (- (dot) 5)) ".spt")))
        (switch-to-buffer buf)
	rtn
    )
)

(defun merge-whole-html-toc()
"DEPRECATED.  Like merge-html-toc, q.v., but merges toc-whole.html,
which is generated with relative doc refs, suitable for inclusion in
the whole (i.e., single file) version of an html doc."
    (interactive)
    (if (search-forward "\n<!-- toc -->\n" nil t)
	(progn
	    (previous-line 1)
	    (beginning-of-line)
	    (kill-line 1)
	    (insert-string "<br>\n<br\n<br>\n<blockquote>\n")
	    (insert-string "</blockquote>\n")
	    (previous-line 1)
	    (insert-file "toc-html-whole.html")
	    (save-buffer)
	 )
    )
)


(defun add-html-toc-leveling ()

"Add the level selections below the Contents title line in index.html and all
of the toc files.  The approach taken is to groom each of the levels of toc
gen'd by the $0 troff macro, q.v.  This grooming consists of the following:

  * have only as many toc level links as there are toc levels
  * go into the index to find where the toc-html command is and put everthing
    above it into each toc
  * put a link line of the following for under the ``Contents''line:

        level: 1 2 3 4 full

    where, as noted previously, there as only as many digits as there are
    toc-levels.

Before I discovered that toc.me === toc-raw.me, I wrote the following now
obsolete comment here:

  The approach is to generate the different levels of toc file from the full
  one, rather than hacking the troff $0 macro somemore.  The biggest problem
  with the latter appraoch is that at the time the $0 troff macro is invoked,
  the toc is still rough, which would mean we'd have to groom 5 toc files
  rather than just 1.  Also, when I started thinking about how I was going to
  check the max toc depth that had been reached in troff, and potentially pass
  that info on to here, I got a little ill.  So, we'll do it this way and see
  how we like it.  The bottom line is that emacs hacking is far eaiser on the
  brain than troff hacking."

  (interactive)
  (let ((front-matter ""))
    (beginning-of-buffer)

    (setq depth (get-max-toc-depth))

    (if (> depth 1)
      (progn

        ; Find the "Contents" line.
	(search-forward "Contents")
	(end-of-line)
	(newline 1)

        ; Put in the table stuff, so highlighting is easy and centering is
	; automatic.
	(insert "<br>\n<table>\n<tr>\n")

	; Add the "level: " label.
	(insert "<td><font size=-1>Level:&nbsp;</font></td>")

	; Save everything from the beginning of the buffer to here for
	; insertion into each toc-x.html file.
	(setq front-matter (buffer-substring 1 (dot)))

	; For each depth > 1, put in a numbered link to that level.
	(setq i 1)
	(while (< i depth)
	    (insert-level-link (int-to-string i))
	    (setq i (1+ i))
	)

	; Put in the highlighted, non-linked "full" label.
	(insert
	    "<td bgcolor=blue><font color=white>&nbsp;full&nbsp;</font></td>\n"
	)

        ; Finish things off by closing table.
	(insert "</tr>\n</table>\n")

      )
    )

    ; Add similar leveling in the toc-x.html files.
    (add-html-toc-level-in-subfiles depth front-matter)

  )
)

(defun get-max-toc-depth ()
"Return the value of the single integer value stored in the file ./toc-depth.
Return 0 if the file does not exist."
  (interactive)
  (let ((rtn 0))
    (if (not (file-exists-p "toc-depth"))
	0
	(progn
	    (setq b (buffer-name))
	    (find-file "toc-depth")
	    (beginning-of-buffer)
	    (setq d (dot))
	    (forward-word 1)
	    (setq rtn (string-to-int (buffer-substring d (dot))))
	    (switch-to-buffer b)
	)
    )
    rtn
  )
)

(defun add-html-toc-level-in-subfiles (depth front-matter)
"Open each toc-x.html file and stick in the front matter, contents links and
lists of figures and tables."

  (let ((i 1) (b (buffer-name)))
    (while (< i depth)
        (find-file (concat "toc-" (int-to-string i) ".html"))

	; Insert the same front matter is in the main index.html; this
	; replaces the extant front matter, up to the </p> following the
	; Contents line.
	(beginning-of-buffer)
	(search-forward "Contents")
	(forward-line 3)
	(beginning-of-line)
	(kill-region 1 (dot))
	(insert front-matter)
	(newline 1)

	; Add the toc links for each level, others linked, this level high-
	; lighted and not linked.
	(add-html-toc-level-in-1-subfile i depth)

	; Add the full link; it's linked unconditionally since the current
	; buffer is always something other than the full index.a
        (insert-level-link "full")

        ; Close the table, centered paragraph, and font+2, which were started
	; in the inserted front matter.
	(insert "</tr>\n</table>\n</p>\n</font>\n")

        ; Add lof and lot links at the end, just before the </body>
	(end-of-buffer)
	(previous-line 2)
	(add-lof-lot-links)

	(save-buffer)
	(setq i (1+ i))
    )
    (switch-to-buffer b)
  )

)

(defun add-html-toc-level-in-1-subfile (level depth)
"In the toc-?.html file of the given level, stick in the contents links.  Each
level except the given on is a link.  The given one is a highlighted label, not
linked."

  (let ((i 1))
    (while (< i depth)
        (if (= i level)
	    (insert-level-highlighted (int-to-string i))
	    (insert-level-link (int-to-string i))
	)
	(setq i (1+ i))
    )
  )

)

(defun insert-level-link (level)
    (interactive)
    (insert (concat
	"<td>"
        "<font size=-1>&nbsp;"
	"<a href=\""
	    (if (string= level "full") "index" (concat "toc-" level))
	".html\">"
	level
	"</a>&nbsp;"
	"</font>"
	"</td>\n"
    ))
)

(defun insert-level-highlighted (level)
    (interactive)
    (insert (concat
	"<td bgcolor=blue><font color=white>"
        "<font size=-1>&nbsp;"
	level
	"&nbsp;"
	"</font>"
	"</td>\n"
    ))
)