auctex / style / natbib.el

;;; natbib.el --- Style hook for the NatBib package
;;; AUTHOR: Carsten Dominik <>
;;;         building on older code from Berwin Turlach

;;; Code:

(TeX-add-style-hook "natbib"
  (lambda ()
    ;; The number in the cdr of the following list indicates how many
    ;; optional note arguments we consider useful.  Prompting for those
    ;; arguments will still depend upon `TeX-arg-cite-note-p'.
    (let  ((citecmds 
	    '(("cite" . 0)
	      ("citet" . 1) ("citet*" . 1) ("citealt" . 1) ("citealt*" . 1)
	      ("citep" . 2) ("citep*" . 2) ("citealp" . 2) ("citealp*" . 2)
	      ("citeauthor" . 0) ("citeauthor*" . 0) ("citefullauthor" . 0)
	      ("citeyear" . 0) ("citeyearpar" . 0)
	      ("shortcites" . 0))))

      ;; Add these symbols
	(lambda (cmd)
	   ((= (cdr cmd) 0)
	    ;; No optional arguments
	    (list (car cmd) 'TeX-arg-cite))
	   ((= (cdr cmd) 1)
	    ;; Just one optional argument, the post note
	     (car cmd)
	     '(TeX-arg-conditional TeX-arg-cite-note-p (["Post-note"]) nil)
	   ((= (cdr cmd) 2)
	    ;; Pre and post notes
	     (car cmd)
	     '(TeX-arg-conditional TeX-arg-cite-note-p (natbib-note-args) nil)

      ;; Add the other symbols
       '("citetext" "Text")
       '("bibpunct" ["Post note separator"] 
		 "Opening bracket"
		 "Closing bracket"
		 "Punctuation between multiple citations"
		 "style [n]umeric [s]uperscript [a]uthor-year"
		 "Punctuation between author and year"
		 "Punctuation between years for common authors")
       '("citestyle" "Style")

      ;; Make an entry in TeX-complete-list
	(concat "\\\\\\(" 
		(mapconcat (lambda (x) (regexp-quote (car x)))
			   citecmds "\\|")
	4 'LaTeX-bibitem-list "}")))

    ;; Add fontification for references
    (when (and (featurep 'font-latex)
	       (eq TeX-install-font-lock 'font-latex-setup))
      (setq font-latex-match-reference-keywords-local
	    (append font-latex-match-reference-keywords-local
		    '("cite" "citet" "citet*" "citealt" "citealt*"
		      "citep" "citep*" "citealp" "citealp*" 
		      "citeauthor" "citeauthor*" "citefullauthor"
		      "citeyear" "citeyearpar""shortcites")))

    ;; Tell RefTeX
    (if (fboundp 'reftex-set-cite-format)
	(reftex-set-cite-format 'natbib)))))

(defun natbib-note-args (optional &optional prompt definition)
  "Prompt for two note arguments a natbib citation command."
  (if TeX-arg-cite-note-p
      (let* ((pre (read-string 
		   (TeX-argument-prompt optional optional "Pre-note")))
	     (post (read-string
		    (TeX-argument-prompt optional optional "Post-note"))))
	(if (not (string= pre "")) (insert "[" pre "]"))
	(if (not (string= post ""))
	    (insert "[" post "]")
	  ;; Make sure that we have an empty post note if pre is not empty
	  (if (string= pre "") (insert "[]"))))))

(defvar LaTeX-natbib-package-options '("numbers" "super" "authoryear"
				       "round" "square" "angle" "curly"
				       "comma" "colon" "nobibstyle" 
				       "bibstyle" "openbib" "sectionbib"
				       "sort" "sort&compress"
				       "longnamesfirst" "nonamebreak")
  "Package options for the natbib package.")

;; natbib.el ends here