(defun dot () (point))

; Convert -man format troff docs to -me format.  At present, this pkg is
; specialized to work for InterViews man-page-format docs, so it probably does
; not yet work for all general man pages.  If we ever get around to it,
; generalizing this function would be nice.
;
(defun man2me-and-save ()
    (load "~gfisher/emacs/lib/misc.el")
    (man2me)
    (save-buffer)
)

(defun man2me ()
"Convert -man  format troff docs to -me format.  At present, this function is
specialized to work for InterViews man-page-format docs, so it probably does
not yet work for all general man pages.  If we ever get around to it,
generalizing this function would be nice."
    (interactive)
    (beginning-of-buffer)
;    (insert-string
;        (concat
;            ".if !\\n[indoc] \\{\\\n"
;            ".    sz 10\n"
;            ".    ds ~ /usersgfisher/nroff\n"
;            ".    so \\*~/stdhdr.me\n"
;	    ".\\}\n"))
    (replace-man-TP)
    (beginning-of-buffer)
    (replace-man-SH)
    (beginning-of-buffer)
    (insert-man-links)
    (beginning-of-buffer)
    (replace-regexp "^\\.PP" ".sp")
    (beginning-of-buffer)
    (replace-string ".(i\n.ns\n.)i" ".br")  ; -- HACK; this gets rid of extra
                     ; stuff that was put in for one-line .TPs, with .ns's
                     ; between them; inelligant, but it works.
    (beginning-of-buffer)
    (replace-regexp "^\\.B \"\\(.*\\)\"" "\\\\fB\\1\\\\fP")
    (beginning-of-buffer)
    (replace-regexp "^\\.B \\(.*\\)" "\\\\fB\\1\\\\fP")
    (beginning-of-buffer)
    (replace-regexp "^\\.I \\(.*\\)" "\\\\fI\\1\\\\fP")
    (beginning-of-buffer)
    (replace-regexp "^\\.TH \\([^ ]*\\) .*"
		    ".bp\n.he 'InterViews Reference Manual'\\1'Page %'")
)

(defun replace-man-SH ()
    (interactive)
    (while (search-forward-regexp "^\\.SH \\(.*\\)" nil t)
        (setq m (match-string 1))
        (beginning-of-line)
	(kill-line 1)
	(insert-string (concat ".uh \"" m "\"\n.(i\n"))
	(if (search-forward-regexp "^\\.SH" nil t)
	  (progn
	    (beginning-of-line)
	    (insert-string ".)i\n")
	  )
	  (progn
	    (end-of-buffer)
	    (insert-string ".)i\n")
	  )
        )
    )
)

(defun replace-man-TP ()
    (interactive)
    (while (search-forward-regexp "^\\.TP" nil t)
        (beginning-of-line)
	(kill-line 1)
        (next-line 1)
	(insert-string ".(i\n")
	(if (search-forward-regexp "^\\.TP\\|^\\.SH" nil t)
	  (progn
	    (beginning-of-line)
	    (insert-string ".)i\n")
	  )
	  (progn
	    (search-forward-regexp "^\\." nil t)
	    (beginning-of-line)
	    (insert-string ".)i\n")
	  )
        )
    )
)

(defun insert-man-links ()
"Replace all (2I) and (3I) man-style refs to -me-style html refs."
    (interactive)
    (while (search-forward-regexp "([23]I)" nil t)
        (backward-kill-word 1)
	(backward-delete-char 1)
	(delete-whitespace)
	(if (not (eolp))
	    (open-line 1)
	)
	(backward-word 1)
	(delete-whitespace)
	(if (not (bolp))
	    (newline 1)
	)
	(insert-string ".(Ah \"")
	(setq d (dot))
	(end-of-line)
	(setq w (buffer-substring d (dot)))
	(insert-string
	    (concat ".html\"\n" w "\n.)Ah"))
    )
)


(defun man2me-good-design ()
"Convert a -man formant troff doc to -me format."
    (interactive)
    (convert-man-to-me-title)
    (convert-man-to-me-sections)
    (convert-man-to-me-bold)
    (convert-man-to-me-italics)
)