; Transitivly close on .(Children ... .)Children structures, generating .file,
; .prev, .next, .up, and .top hyperlinks in all affected .me files.  Note that
; this is a bit different than other forms of html-related emacs processing,
; since it directly edits .me files, rather than .html or some fully volatile
; generated file.  What this means is that after we gen these hyper links, we
; may start to edit a file that was just modified.  If so, emacs will be nice
; and warn us, which we count on dearly in this case.
; 
; The primary rationnale for why it's OK to do things this way is that this
; hyper link gen will presumably be relatively infrequent, once we establish
; the overall document structure, and so what we're doing is kind of like make
; depend, only even less frequent, or at least that's the hoped for idea.
; Under any circustances, we do count on Emacs to save our bacon with the "file
; has changed on disk" messages.  The other way to avoid any file hassle at all
; is to run this in non-batch mode from right inside the edit buffer of the
; topmost .me file.
;
; 11jul01 desired update: Would like only to save buffer if new link commands
; are different than the old, so that the file dates dont change unnecessarily.
; This shouldn't be too hard to do, I just don't feel like dicking with it
; right now.
;
(defun dot () (point))

(defun gen-html-hyper-links ()
"Starting from the top of the current buffer, transitively close on .(Children
... .)Children structures, generating .file, .prev, .next, .up, and .top
hyperlinks in all affected .me files."
    (interactive)
    (let ((save-version-control version-control))
	(setq version-control 'never)
	(gen-html-hyper-links1 (substring (buffer-name) 0 -3))
	(setq version-control save-version-control)
    )
)

(defun gen-html-hyper-links1 (top)
"Work doer for gen-html-hyper-links, q.v."
    (interactive)
    ;(message (concat "File: " (buffer-file-name)))
    (if (search-forward-regexp "^.(Children" nil t)
	(progn
	    (setq bn (buffer-name))
	    (message (concat "File: " bn))
	    (setq up (remove-buffer-extension-and-number bn))
				; Loose .me extension and any <X> buffer num
	    (setq prev nil)
	    (while (search-forward-regexp "^.so \\(.*\\)\.me" nil t)
	        (setq file (match-string 1))
		(setq d (dot))
		(if (search-forward-regexp "^.so \\(.*\\)\.me" nil t)
		    (progn
		        (setq next (match-string 1))
			(goto-char d)
		    )
		    (setq next nil)
		)
		(record-html-hyper-links-in-file file prev next up top)
		(setq prev file)
	    )
	)
    )
)

(defun remove-buffer-extension-and-number (bn)
"Remove the file extension and any angle-bracketed number from a buffer name."
    (interactive)
    (let ((b (buffer-name)))
        (switch-to-buffer "XXX")
	(beginning-of-buffer)
	(setq d (dot))
	(end-of-buffer)
	(kill-region d (dot))
	(insert-string bn)
	(beginning-of-buffer)
	(search-forward ".")
	(backward-char 1)
	(kill-line 1)
	(setq d (dot))
	(beginning-of-buffer)
	(setq rtn (buffer-substring (dot) d))
	(switch-to-buffer b)
    )
    rtn
)

(defun record-html-hyper-links-in-file (file prev next up top)
"Put .file, .prev, .next, .up, .top commands in given file, per given args."
    (interactive)
    (save-excursion
        (setq vc-follow-symlinks t)
        (find-file (concat file ".me"))
	(remove-old-html-hyper-links)
	(insert-string
	    (concat
	        ".file " file "\n"
		".prev " prev "\n"
		".next " next "\n"
		".up " up "\n"
		".top " top "\n"
            )
	)
	(save-buffer)
	(gen-html-hyper-links1 top)
    )
)

(defun remove-old-html-hyper-links ()
"Remove any extant .file, .prev, .next, .up, or .top commands in the current
file buffer."
    (interactive)
    (beginning-of-buffer)
    (replace-regexp "^\\.file.*\n" "")
    (beginning-of-buffer)
    (replace-regexp "^\\.prev.*\n" "")
    (beginning-of-buffer)
    (replace-regexp "^\\.next.*\n" "")
    (beginning-of-buffer)
    (replace-regexp "^\\.up.*\n" "")
    (beginning-of-buffer)
    (replace-regexp "^\\.top.*\n" "")
    (beginning-of-buffer)
)