[comp.emacs] even better BibTeX mode

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

A few days ago I posted a BibTeX mode for GNU Emacs.  In the meantime I
added an X menu interface (one reason being that my secretary couldn't
figure out the difference between C-c m, C-c M, C-c C-m, etc.; the other
reason was that I couldn't either!).  I also fixed a few bugs.  The result
is enclosed below.

There is one bug left, I'd like someone to help me fix.  I haven't managed
to bind mouse button control-right to x-bibtex-help in *only* the bibtex
buffer.  I tried to make mouse-map buffer-local but that doesn't seem to
work.


						Marc Shapiro

INRIA; B.P. 105; 78153 Le Chesnay Cedex; France; tel.: +33 (1) 39-63-53-25
e-mail: shapiro@inria.inria.fr; or: ...!mcvax!inria!shapiro

---- (cut here for bibtex-mode.el) ------------------------------

;;; 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)
;;; Marc Shapiro 19-oct-1987
;;;  add X window menu option
;;;

(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.

Set bibtex-mode-hook to be bibtex-x-environment to get X menus for the above.
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\eOS" 'kill-current-line)
  (define-key bibtex-mode-map "\C-c\C-k" '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)
  (define-key bibtex-mode-map "\C-c?" 'describe-mode)

					; 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."
  (beginning-of-line)
  (let ((beg (point)))
    (end-of-line)
    (if (search-backward """" beg t)
	t
      (search-forward """" (point-max) 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)
  )

;;; X window menus for bibtex mode

(defun x-bibtex-help (arg)
  "Mouse commands for BibTeX mode"

  (let ((selection
	 (x-popup-menu
	  arg
	  '("BibTeX commands"
	    ("Document types"
	     ("article in Conference Proceedings" . bibtex-InProceedings)
	     ("article in journal" . bibtex-Article)
	     ("Book" . bibtex-Book)
	     ("Booklet" . bibtex-Booklet)
	     ("Master's Thesis" . bibtex-MastersThesis)
	     ("Phd. Thesis" . bibtex-PhdThesis)
	     ("Technical Report" . bibtex-TechReport)
	     ("technical Manual" . bibtex-Manual)
	     ("Conference Proceedings" . bibtex-Proceedings)
	     ("in a Book" . bibtex-InBook)
	     ("in a Collection" . bibtex-InCollection)
	     ("miscellaneous" . bibtex-Misc)
	     ("unpublished" . bibtex-Unpublished)
	     )
	    ("editing"
	     ("next field" . next-position)
	     ("to end of field" . find-it)
	     ("remove OPT" . bibtex-remove-opt)
	     ("remove quotes" . bibtex-remove-double-quotes)
	     ("remove this line" . kill-current-line))
	    ("others"
	     ("describe BibTeX mode" . describe-mode)
	     ("string" . bibtex-string))))))
    (and selection (call-interactively selection))))

(defun bibtex-x-environment ()
  "Set up X menus for BibTeX mode.  Call it as bibtex-mode-hook, or interactively"
  (interactive)
  (require 'x-mouse)

  (make-variable-buffer-local 'mouse-map)
  (define-key mouse-map x-button-c-right 'x-bibtex-help)
  )

----- (the end) --------------------------