1 ;;; bib-mode.el --- major mode for editing bib files
3 ;; Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 Free Software Foundation, Inc.
7 ;; (according to authors.el)
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; GNU Emacs code to help maintain databases compatible with (troff)
29 ;; refer and lookbib. The file bib-file should be set to your
30 ;; bibliography file. Keys are automagically inserted as you type,
31 ;; and appropriate keys are presented for various kinds of entries.
36 "Major mode for editing bib files."
41 (defcustom bib-file
"~/my-bibliography.bib"
42 "Default name of file used by `addbib'."
46 (defcustom unread-bib-file
"~/to-be-read.bib"
47 "Default name of file used by `unread-bib' in Bib mode."
51 (defvar bib-mode-map
(copy-keymap text-mode-map
))
52 (define-key bib-mode-map
"\C-M" 'return-key-bib
)
53 (define-key bib-mode-map
"\C-c\C-u" 'unread-bib
)
54 (define-key bib-mode-map
"\C-c\C-@" 'mark-bib
)
55 (define-key bib-mode-map
"\e`" 'abbrev-mode
)
58 "Set up editor to add to troff bibliography file specified
59 by global variable `bib-file'. See description of `bib-mode'."
62 (goto-char (point-max))
66 (define-derived-mode bib-mode text-mode
"Bib"
67 "Mode for editing `lookbib' style bibliographies.
68 Hit RETURN to get next % field key.
69 If you want to ignore this field, just hit RETURN again.
70 Use `text-mode' to turn this feature off.
72 journal papers: A* T D J V N P K W X
73 articles in books & proceedings: A* T D B E* I C P K W X
74 tech reports: A* T D R I C K W X
75 books: A* T D I C K W X
79 A uthor T itle D ate J ournal
80 V olume N umber P age K eywords
81 B in book or proceedings E ditor C ity & state
82 I nstitution, school, or publisher
83 R eport number or 'phd thesis' or 'masters thesis' or 'draft' or
84 'unnumbered' or 'unpublished'
85 W here can be found locally (login name, or ailib, etc.)
86 X comments (not used in indexing)
88 \\[unread-bib] appends current entry to a different file (for example,
89 a file of papers to be read in the future), given by the value of the
90 variable `unread-bib-file'.
91 \\[mark-bib] marks current or previous entry.
92 Abbreviations are saved in `bib-mode-abbrev-table'.
93 Hook can be stored in `bib-mode-hook'.
94 Field keys given by variable `bib-assoc'.
121 "Describes bibliographic database format.
122 A line beginning with the car of an entry is followed by one beginning
125 (defun bib-find-key (slots)
130 (progn (forward-line -
1) (bib-find-key bib-assoc
))))
131 ((looking-at (car (car slots
)))
133 (t (bib-find-key (cdr slots
)))
137 (defcustom bib-auto-capitalize t
138 "*True to automatically capitalize appropriate fields in Bib mode."
142 (defconst bib-capitalized-fields
"%[AETCBIJR]")
144 (defun return-key-bib ()
145 "Magic when user hits return, used by `bib-mode'."
148 (let (empty new-key beg-current end-current
)
150 (setq empty
(looking-at "%. $"))
158 (setq end-current
(point))
160 (setq beg-current
(point))
161 (setq new-key
(bib-find-key bib-assoc
))
162 (if (and (not empty
) bib-auto-capitalize
163 (looking-at bib-capitalized-fields
))
165 (bib-capitalize-title-region (+ (point) 3) end-current
)))
166 (goto-char beg-current
)
175 "Set mark at beginning of current or previous bib entry, point at end."
177 (beginning-of-line nil
)
178 (if (looking-at "^ *$") (re-search-backward "[^ \n]" nil
2))
179 (re-search-backward "^ *$" nil
2)
180 (re-search-forward "^%")
181 (beginning-of-line nil
)
183 (re-search-forward "^ *$" nil
2)
185 (beginning-of-line nil
))
188 "Append current or previous entry to file of unread papers
189 named by variable `unread-bib-file'."
192 (if (get-file-buffer unread-bib-file
)
193 (append-to-buffer (get-file-buffer unread-bib-file
) (mark) (point))
194 (append-to-file (mark) (point) unread-bib-file
)))
197 (defvar bib-capitalize-title-stop-words
199 "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
200 "by\\|with\\|that\\|its")
201 "Words not to be capitalized in a title (unless the first word).")
203 (defvar bib-capitalize-title-stop-regexp
204 (concat "\\(" bib-capitalize-title-stop-words
"\\)\\(\\b\\|'\\)"))
206 (defun bib-capitalize-title-region (begin end
)
207 "Like `capitalize-region', but don't capitalize stop words, except the first."
209 (let ((case-fold-search nil
) (orig-syntax-table (syntax-table)))
212 (set-syntax-table text-mode-syntax-table
)
213 (narrow-to-region begin end
)
214 (goto-char (point-min))
215 (if (looking-at "[A-Z][a-z]*[A-Z]")
218 (while (re-search-forward "\\<" nil t
)
219 (if (looking-at "[A-Z][a-z]*[A-Z]")
221 (if (let ((case-fold-search t
))
222 (looking-at bib-capitalize-title-stop-regexp
))
224 (capitalize-word 1)))
226 (set-syntax-table orig-syntax-table
))))
229 (defun bib-capitalize-title (s)
230 "Like `capitalize', but don't capitalize stop words, except the first."
231 (with-current-buffer (get-buffer-create "$$$Scratch$$$")
234 (bib-capitalize-title-region (point-min) (point-max))
239 ;; arch-tag: e3a97958-3c2c-487f-9557-fafc3c98452d
240 ;;; bib-mode.el ends here