
;;; Date: Wed, 30 Nov 1988 16:22:58 PST
;;; From: Peter Karp <karp@sumex-aim.stanford.edu>
;;; Subject: Bibliography converter 
;;; Keywords: LaTeX
;;; 
;;; You may find the following tool useful.  I do not read this list
;;; and am new to Latex so if something better already exists I would
;;; be interested to hear about it.
;;; 
;;; 
;;;;
;;;;  Converts a bibliography (.BIB) file in Scribe format to BibTeX format.
;;;;  GNUemacs lisp code.  
;;;;
;;;;  Peter Karp -- karp@sumex-aim.stanford.edu -- Nov 1988
;;;;
;;;;      Please send bugs, suggetions, improvements to the above address.
;;;;
;;;;  To execute, visit the bibiligraphy file to be converted, then:
;;;;        m-X load-file cnvtbib
;;;;        m-X eval-expression (cvbib)
;;;;  
;;;;  BUGS:
;;;;  
;;;;      BibTex will complain if author names are not separated by
;;;;      "and"; this converter does not add necessary "and"s.  BibTex
;;;;      will also complain if names are separated by ", and".  This
;;;;      converter does remove these commas.


(defun  cvbib  ()
  (let  (dpos)

    (beginning-of-buffer)

; First modify all entries so that the delimiters used for the entry
; are "{}".  So we'd convert  @book< ... >  to @book{ ... }

    (while (search-forward "@" nil t)
        (progn
	  (forward-word 1)
	  (setq dpos (point))
	  (goto-matching-delimiter)
	  (backward-delete-char 1)
	  (insert-string "}")
	  (goto-char dpos)
	  (delete-char 1)
	  (insert-string "{")
	  ))

    (beginning-of-buffer)

; Now convert all key fields from, e.g.,  AUTHOR=<PKARP>  to  AUTHOR="PKARP"
; Note that some key fields aren't delimited by anything, which is ok but
; requires a special check in the code below.

    (while  (search-forward "=" nil t)
      (progn
	(while  (search-forward " " (+ 1 (point)) t)
	  nil)
        (if (assoc (point-char) all_delims)
	    (progn
	      (setq dpos (point))
	      (goto-matching-delimiter)
	      (backward-delete-char 1)
	      (insert-string "\"")
	      (goto-char dpos)
	      (delete-char 1)
	      (insert-string "\"")
	      ))
      ))

    (beginning-of-buffer)

;;;;  Change all ", AND" within AUTHOR and EDITOR strings to " AND"
;;;;  because BibTex complains about the former.

    (while (re-search-forward "AUTHOR\\s-*=\\s-*" nil t)
      (setq dpos (point))
      (goto-matching-delimiter)
      (while (re-search-backward ",\\s-*AND" dpos t)
	(delete-char 1))
      )

    (beginning-of-buffer)

    (while (re-search-forward "EDITORS\\s-*=\\s-*" nil t)
      (setq dpos (point))
      (goto-matching-delimiter)
      (while (re-search-backward ",\\s-*AND" dpos t)
	(delete-char 1))
      )

    (beginning-of-buffer)

    )
)

;;;;
;;;;  Goes to the character in the buffer which closes the Scribe delimiter
;;;;  where (point) is when this function is called.  I.e., if the buffer is
;;;;  "@book< foo >bar" and we're called with point at "<", we'll leave
;;;;  point at the "b" of "bar".
;;;;

(setq all_delims '(("<" . ">")
		   ("[" . "]")
		   ("\"" . "\"")
		   ("(" . ")")
		   ("{" . "}")))


(defun  goto-matching-delimiter ()
  (let (delim match-char mdelim redelims (nomatch t))
 	 (setq delim (point-char))
	 (setq mdelim (cdr (assoc delim all_delims)))
	 (if (null mdelim)
	     (error "~s is not a valid delimiter" delim))

 	 (setq redelims (concat "[" mdelim delim "]" ))
	 (forward-char)

         (while nomatch
	   (progn
	     (if (not (re-search-forward redelims nil t))
		 (error "No matching delimiter found"))
	     (setq match-char (buffer-substring (- (point) 1) (point)))

	     (if (and (not (equal "\"" delim))
		      (equal match-char delim))
	       (progn
		 (backward-char)
		 (goto-matching-delimiter))
	       (setq nomatch nil))
	     ))
))

;;;;
;;;;  Return the character at the cursor
;;;;

(defun  point-char  ()
  (buffer-substring  (point)  (+ 1 (point)))
)

;;;;
;;;;  Debugging routine
;;;;

(defun  mark-and-hold (mark-char-string)
  (insert-string mark-char-string)
  (read-string "holding...")
  (backward-delete-char 1)
)
