[comp.emacs] Better BibTeX mode for GNU Emacs

shapiro@inria.UUCP (Marc Shapiro) (10/15/87)

Here is a slightly better version of the bibtex mode posted some time ago.
The differences are summarized in the header comment.

-----------
;;; Simple BibTeX mode for GNU Emacs
;;; Bengt Martensson 87-06-28
;;; changes by Marc Shapiro shapiro@inria.inria.fr 15-oct-1986
;;;  (align long lines nicely; C-c C-o checks for the "OPT" string;
;;;   TAB goes to the end of the string; use lower case; use
;;;   run-hooks)
;;;

(defvar bibtex-mode-syntax-table nil "")
(defvar bibtex-mode-abbrev-table nil "")
(define-abbrev-table 'bibtex-mode-abbrev-table ())
(defvar bibtex-mode-map (make-sparse-keymap) "")

(defun bibtex-mode ()
  "Major mode for editing bibtex files.  A keysequence such as \\[bibtex-book]
will outline the fields for a BibTeX book entry.  The optional fields are 
preceded by ""OPT"", thus ignored by BibTeX.  Use \\[bibtex-remove-opt]
to remove ""OPT"" on the current line.  Use \\[find-it] to position the dot at the 
end of the string on the same line.  Use \\[next-position] to move to the beginning 
of next position to fill in.  Use \\[kill-current-line] to kill the whole line.

Blank lines separate paragraphs.
Commands:
\\{bibtex-mode-map}

Fields:
    address
           Publisher's address
    annote
           Long annotation used for annotated bibliographies (begins sentence)
    author
           Name(s) of author(s), in BibTeX name format
    booktitle
           Book title when the thing being referenced isn't the whole book.
           For book entries, the title field should be used instead.
    chapter
           Chapter number
    edition
           Edition of a book (e.g., ""second"")
    editor
           Name(s) of editor(s), in BibTeX name format.
           If there is also an author field, then the editor field should be
           for the book or collection that the work appears in
    howpublished
            How something strange has been published (begins sentence)
    institution
           Sponsoring institution
    journal
           Journal name (macros are provided for many)
    key
           Alphabetizing and labeling key (needed when no author or editor)
    month
           Month (macros are provided)
    note
           To help the reader find a reference (begins sentence)
    number
           Number of a journal or technical report
    organization
           Organization (sponsoring a conference)
    pages
           Page number or numbers (use `--' to separate a range)
    publisher
           Publisher name
    school
           School name (for theses)
    series
           The name of a series or set of books.
           An individual book will will also have it's own title
    title
           The title of the thing being referenced
    type
           Type of a Techreport (e.g., ""Research Note"") to be used instead of
           the default ""Technical Report""
    volume
           Volume of a journal or multivolume work
    year
           Year---should contain only numerals
---------------------------------------------------------
Entry to this mode calls the value of bibtex-mode-hook
if that value is non-nil."
  (interactive)
  (kill-all-local-variables)
  (if (not bibtex-mode-syntax-table)
      (setq bibtex-mode-syntax-table (copy-syntax-table)))
  (set-syntax-table bibtex-mode-syntax-table)
  (modify-syntax-entry ?\$ "$$  ")
  (modify-syntax-entry ?\% "<   ")
  (modify-syntax-entry ?\f ">   ")
  (modify-syntax-entry ?\n ">   ")
  (modify-syntax-entry ?'  "w   ")
  (modify-syntax-entry ?@  "w   ")
  (use-local-map bibtex-mode-map)
  (setq major-mode 'bibtex-mode)


  (setq mode-name "BibTeX")
  (set-syntax-table bibtex-mode-syntax-table)
  (setq local-abbrev-table bibtex-mode-abbrev-table)
  (make-local-variable 'paragraph-start)
  (setq paragraph-start "^[ \f\n\t]*$")

  (define-key bibtex-mode-map "\t" 'find-it)
  (define-key bibtex-mode-map "\n" 'next-position)
  (define-key bibtex-mode-map "\e[25~" 'next-position)
  (define-key bibtex-mode-map "\C-c""" 'bibtex-remove-double-quotes)
  (define-key bibtex-mode-map "\C-c\C-k" 'kill-current-line)
  (define-key bibtex-mode-map "\C-c\eOS" 'kill-current-line)
  (define-key bibtex-mode-map "\C-c\C-a" 'bibtex-Article)
  (define-key bibtex-mode-map "\C-c\C-b" 'bibtex-Book)
  (define-key bibtex-mode-map "\C-cb" 'bibtex-Booklet)
  (define-key bibtex-mode-map "\C-c\C-c" 'bibtex-InProceedings)
  (define-key bibtex-mode-map "\C-c\C-i" 'bibtex-InBook)
  (define-key bibtex-mode-map "\C-ci" 'bibtex-InCollection)
  (define-key bibtex-mode-map "\C-cI" 'bibtex-InProceedings)
  (define-key bibtex-mode-map "\C-c\C-m" 'bibtex-Manual)
  (define-key bibtex-mode-map "\C-cm" 'bibtex-MastersThesis)
  (define-key bibtex-mode-map "\C-cM" 'bibtex-Misc)
  (define-key bibtex-mode-map "\C-c\C-o" 'bibtex-remove-opt)
  (define-key bibtex-mode-map "\C-c\C-p" 'bibtex-PhdThesis)
  (define-key bibtex-mode-map "\C-cp" 'bibtex-Proceedings)
  (define-key bibtex-mode-map "\C-c\C-t" 'bibtex-TechReport)
  (define-key bibtex-mode-map "\C-c\C-s" 'bibtex-string)
  (define-key bibtex-mode-map "\C-c\C-u" 'bibtex-Unpublished)

  ; nice alignements
  (auto-fill-mode 1)
  (setq left-margin 17)

  (run-hooks 'bibtex-mode-hook))

(defun bibtex-entry (entry-type required optional)
  (insert (concat "@" entry-type "{,\n\n}\n\n"))
  (previous-line 3)
  (insert (mapconcat 'make-entry required ",\n"))
  (if required (insert ",\n"))
  (insert (mapconcat 'make-opt-entry optional ",\n"))
  (up-list -1)
  (forward-char 1))

(defun make-entry (str)
  (interactive "s")
  (concat "  " str " = \t"""""))

(defun make-opt-entry (str)
  (interactive "s")
  (concat "  OPT" str " = \t"""""))
  
(defun bibtex-article ()
  (interactive)
  (bibtex-entry "Article" '("author" "title" "journal" "year")
		'("volume" "number" "pages" "month" "note")))

(defun bibtex-Book ()
  (interactive)
  (bibtex-entry "Book" '("author" "title" "publisher" "year")
		'("editor" "volume" "series" "address"
			   "edition" "month" "note")))

(defun bibtex-Booklet ()
  (interactive)
  (bibtex-entry "Booklet" '("title")
		'("author" "howpublished" "address" "month" "year" "note")))

(defun bibtex-InBook ()
  (interactive)
  (bibtex-entry "InBook" '("author" "title" "chapter" "publisher" "year")
		'("editor" "pages" "volume" "series" "address"
			   "edition" "month" "note")))

(defun bibtex-InCollection ()
  (interactive)
  (bibtex-entry "InCollection" '("author" "title" "booktitle"
					  "publisher" "year")
		'("editor" "chapter" "pages" "address" "month" "note")))


(defun bibtex-InProceedings ()
  (interactive)
  (bibtex-entry "InProceedings" '("author" "title" "booktitle" "year")
		'("editor" "pages" "organization" "publisher"
			   "address" "month" "note")))

(defun bibtex-Manual ()
  (interactive)
  (bibtex-entry "Manual" '("title")
		'("author" "organization" "address" "edition" "year"
			   "month" "note")))

(defun bibtex-MastersThesis ()
  (interactive)
  (bibtex-entry "MastersThesis" '("author" "title" "school" "year")
		'("address" "month" "note")))

(defun bibtex-Misc ()
  (interactive)
  (bibtex-entry "Misc" '()
		'("author" "title" "howpublished" "year" "month" "note")))

(defun bibtex-PhdThesis ()
  (interactive)
  (bibtex-entry "PhdThesis" '("author" "title" "school" "year")
		'("address" "month" "note")))

(defun bibtex-Proceedings ()
  (interactive)
  (bibtex-entry "Proceedings" '("title" "year")
		'("editor" "publisher" "organization"
			   "address" "month" "note")))
(defun bibtex-TechReport ()
  (interactive)
  (bibtex-entry "TechReport" '("author" "title" "institution" "year")
		'("type" "number" "address" "month" "note")))


(defun bibtex-Unpublished ()
  (interactive)
  (bibtex-entry "Unpublished" '("author" "title" "note")
		'("year" "month")))

(defun bibtex-string ()
  (interactive)
  (insert "@string{ = """"}\n")
  (previous-line 1)
  (forward-char 8))

(defun next-position ()
  "Finds next posion to write in."
  (interactive)
  (beginning-of-next-line)
  (find-it))

(defun find-it ()
  (interactive)
  "Find position on current line (if possible) to add entry text."
  (end-of-line)
  (search-backward """" (point-min) t))

(defun bibtex-remove-opt ()
  "Removes the 'OPT' starting optional arguments."
  (interactive)
  (beginning-of-line)
  (forward-char 2)
  (if (looking-at "OPT")
      (delete-char 3))
  (find-it))

(defun kill-current-line ()
  "Kills the current line."
  (interactive)
  (beginning-of-line)
  (kill-line 1))

(defun bibtex-remove-double-quotes ()
  "Removes next occurence of """"."
  (interactive)
  (find-it)
  (delete-char -1)
  (delete-char 1))

(defun beginning-of-next-line ()
  (end-of-line)
  (forward-char)
  )