;;; sdocutil.el
;;;
;;; Utilities for building Sdoc databases.
;;;
;;; Copyright © 1995,1996 Torbjörn Axelsson
;;;
;;; This file is NOT part of the GNU Emacs.
;;;
;;; Verbatim copies of this file may be freely redistributed together with
;;; its documentation file.
;;;
;;; Modified versions of this file may be redistributed provided that this
;;; notice remains unchanged, the file contains prominent notice of author
;;; and time of modifications, and redistribution of the file is not
;;; further restricted in any way.
;;;
;;; This file is distributed `as is', without warranties of any kind. 
;;;
;;; To contact the author, send email to torax@lysator.liu.se.
;;;
;;; History:
;;; v1.01 1996-05-19: Fixed a few stupid bugs/inconveniences.
;;; v1.00 1995-02-04: sdocutil-autodoc for Amiga AutoDoc files.
;;;                   sdocutil-map to map over sdocutil- functions.
;;;
;;; Future:
;;; Pri Project
;;; top Scanning c header files for references.

(require 'sdoc)
(provide 'sdocutil)

;;; Traverse multiple files
(defun sdocutil-map (buffer function &optional add-directly)
  "BUFFER holds one filename per line, which are fed as arguments to FUNCTION.
If optional ADD-DIRECTLY is non-nil the files will be added immediately,
otherwise this function will return a list of buffers returned from FUNCTION.
WARNING! Will load the files one by one, then killing the buffers!

Example usage:
Create a buffer foo with the filenames of the Amiga
AutoDocs you want to add.
Then execute this line (in *scratch*):
(sdocutil-map (\"foo\" 'sdocutil-autodoc t))

After quite a while (this is slooow) it will finish, having added all
entries into your Sdoc database file. Don't forget to save the database
when you are done!"
  (interactive "bFilenames buffer (current): \naSdocutil function: \nP")
  (save-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (let (buflist)
      (while (< (point) (point-max))
	(beginning-of-line)
	(let ((bol (point)))
	  (end-of-line)
	  (let ((fbuf (find-file-noselect (buffer-substring bol (point)))))
	    (setq buflist (cons (apply function (list fbuf add-directly)) buflist))
	    (kill-buffer fbuf)))
	(forward-line))
      buflist)))

;;; Amiga AutoDocs

(defun sdocutil-autodoc (&optional buffer add-directly)
  "*Parse BUFFER (default current) and extract Amiga AutoDoc information for Sdoc. With prefix add directly to the database.
Optional arg ADD-DIRECTLY adds the information directly to the Sdoc
database, else the function will create a work buffer and return it.
The basename of the entry is take as matchid, with matchtype being
\"autodoc\", except for --background-- that will have id as the first
part of the Autodoc entry and type background."
  (interactive "bAutoDoc buffer: \nP")
  (save-excursion
    (if buffer
	(set-buffer buffer)
      (setq buffer (current-buffer)))
    (if (not buffer-file-name)
	(error "Cannot add a buffer not visiting a file!")
      (let ((work-buffer (generate-new-buffer " sdocutil")))
	(set-buffer work-buffer)
	(delete-region (point-min) (point-max))
	(sdoc-databasep work-buffer)
	(set-buffer buffer)
	(goto-char (point-min))
	(while (re-search-forward "^\^L.*/.*" nil t)
	  (beginning-of-line)
	  (forward-char)
	  (let ((beginning (point)))
	    (re-search-forward "\^L" nil 1)
	    (backward-char)
	    (let ((end (point)))
	      (goto-char beginning)
	      (re-search-forward ".*/\\([^ \n]*\\)" nil t)
	      (let ((id (buffer-substring (match-beginning 1) (match-end 1)))
		    (j sdoc-database))
		(sdoc-add-region-as-match id "autodoc" beginning end nil work-buffer))
	      (goto-char end))))
	(if (not add-directly)
	    work-buffer
	  (if (sdoc-databasep work-buffer)
	      (sdoc-merge-buffer-to-database work-buffer))
	  (kill-buffer work-buffer))))))


