;
; Functions useful for parsing.
;

(defun split (string &optional regexp sep)
  "Splits STRING into a list of strings by choosing breakpoints 
according to REGEXP.  If REGEXP does not occur the entire STRING
is contained in the first element of the returned list.  REGEXP is
optional and defaults to [ \t]+ (whitespace).  Optional third arg
SEPARATORS causes the text that matches grouped expressions in REGEXP
at each breakpoint to be included in the returned list.  WARNING: This
function can loop endlessly if REGEXP matches the null string."
  (if regexp nil (setq regexp "[ \\t]+"))
  (let ((buf (generate-new-buffer "  *tmp")))
    (unwind-protect 
	(save-excursion
	  (let* ((split-list (list 'split-list))
		 (split-tail (last split-list)))
	    (set-buffer buf)
	    (insert string)
	    (goto-char (point-min))
	    (let ((p (point)))
	      (while (re-search-forward regexp nil t)
		(setcdr split-tail
			(list (buffer-substring p (match-beginning 0))))
		(setq split-tail (cdr split-tail))
		(setq mtext (matched-strings))
		(if (and sep mtext)
		    (progn
		      (setcdr split-tail (list mtext))
		      (setq split-tail (cdr split-tail))))
		(setq p (point))))
	    (setcdr split-tail (list (buffer-substring (point) (point-max))))
	    (setq split-tail (cdr split-tail))
	    (cdr split-list)))
      (kill-buffer buf))))
