(event-basic-type): Doc fix.
[bpt/emacs.git] / lisp / textmodes / bib-mode.el
1 ;;; bib-mode.el --- bib-mode, major mode for editing bib files.
2
3 ;; Copyright (C) 1989 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: bib
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; GNU Emacs code to help maintain databases compatible with (troff)
28 ;; refer and lookbib. The file bib-file should be set to your
29 ;; bibliography file. Keys are automagically inserted as you type,
30 ;; and appropriate keys are presented for various kinds of entries.
31
32 ;;; Code:
33 (defgroup bib nil
34 "Major mode for editing bib files."
35 :prefix "bib-"
36 :group 'wp)
37
38 (defcustom bib-file "~/my-bibliography.bib"
39 "Default name of file used by `addbib'."
40 :type 'file
41 :group 'bib)
42
43 (defcustom unread-bib-file "~/to-be-read.bib"
44 "Default name of file used by `unread-bib' in Bib mode."
45 :type 'file
46 :group 'bib)
47
48 (defvar bib-mode-map (copy-keymap text-mode-map))
49 (define-key bib-mode-map "\C-M" 'return-key-bib)
50 (define-key bib-mode-map "\C-c\C-u" 'unread-bib)
51 (define-key bib-mode-map "\C-c\C-@" 'mark-bib)
52 (define-key bib-mode-map "\e`" 'abbrev-mode)
53 (defvar bib-mode-abbrev-table nil
54 "Abbrev table used in Bib mode")
55
56 (defun addbib ()
57 "Set up editor to add to troff bibliography file specified
58 by global variable `bib-file'. See description of `bib-mode'."
59 (interactive)
60 (find-file bib-file)
61 (goto-char (point-max))
62 (bib-mode)
63 )
64
65 (defun bib-mode ()
66 "Mode for editing `lookbib' style bibliographies.
67 Hit RETURN to get next % field key.
68 If you want to ignore this field, just hit RETURN again.
69 Use `text-mode' to turn this feature off.
70
71 journal papers: A* T D J V N P K W X
72 articles in books & proceedings: A* T D B E* I C P K W X
73 tech reports: A* T D R I C K W X
74 books: A* T D I C K W X
75
76 Fields:
77
78 A uthor T itle D ate J ournal
79 V olume N umber P age K eywords
80 B in book or proceedings E ditor C ity & state
81 I nstitution, school, or publisher
82 R eport number or 'phd thesis' or 'masters thesis' or 'draft' or
83 'unnumbered' or 'unpublished'
84 W here can be found locally (login name, or ailib, etc.)
85 X comments (not used in indexing)
86
87 \\[unread-bib] appends current entry to a different file (for example,
88 a file of papers to be read in the future), given by the value of the
89 variable `unread-bib-file'.
90 \\[mark-bib] marks current or previous entry.
91 Abbreviations are saved in `bib-mode-abbrev-table'.
92 Hook can be stored in `bib-mode-hook'.
93 Field keys given by variable `bib-assoc'.
94
95 Commands:
96 \\{bib-mode-map}
97 "
98 (interactive)
99 (text-mode)
100 (use-local-map bib-mode-map)
101 (setq mode-name "Bib")
102 (setq major-mode 'bib-mode)
103 (define-abbrev-table 'bib-mode-abbrev-table ())
104 (setq local-abbrev-table bib-mode-abbrev-table)
105 (abbrev-mode 1)
106 (run-hooks 'bib-mode-hook)
107 )
108
109 (defconst bib-assoc '(
110 (" *$" . "%A ")
111 ("%A ." . "%A ")
112 ("%A $" . "%T ")
113 ("%T " . "%D ")
114 ("%D " . "%J ")
115 ("%J ." . "%V ")
116 ("%V " . "%N ")
117 ("%N " . "%P ")
118 ("%P " . "%K ")
119 ("%K " . "%W ")
120 ("%W " . "%X ")
121 ("%X " . "")
122 ("%J $" . "%B ")
123 ("%B ." . "%E ")
124 ("%E ." . "%E ")
125 ("%E $" . "%I ")
126 ("%I " . "%C ")
127 ("%C " . "%P ")
128 ("%B $" . "%R ")
129 ("%R " . "%I ")
130 )
131
132 "Describes bibliographic database format. A line beginning with
133 the car of an entry is followed by one beginning with the cdr.
134 ")
135
136 (defun bib-find-key (slots)
137 (cond
138 ((null slots)
139 (if (bobp)
140 ""
141 (progn (previous-line 1) (bib-find-key bib-assoc))))
142 ((looking-at (car (car slots)))
143 (cdr (car slots)))
144 (t (bib-find-key (cdr slots)))
145 ))
146
147
148 (defcustom bib-auto-capitalize t
149 "*True to automatically capitalize appropriate fields in Bib mode."
150 :type 'boolean
151 :group 'bib)
152
153 (defconst bib-capitalized-fields "%[AETCBIJR]")
154
155 (defun return-key-bib ()
156 "Magic when user hits return, used by `bib-mode'."
157 (interactive)
158 (if (eolp)
159 (let (empty new-key beg-current end-current)
160 (beginning-of-line)
161 (setq empty (looking-at "%. $"))
162 (if (not empty)
163 (progn
164 (end-of-line)
165 (newline)
166 (forward-line -1)
167 ))
168 (end-of-line)
169 (setq end-current (point))
170 (beginning-of-line)
171 (setq beg-current (point))
172 (setq new-key (bib-find-key bib-assoc))
173 (if (and (not empty) bib-auto-capitalize
174 (looking-at bib-capitalized-fields))
175 (save-excursion
176 (bib-capitalize-title-region (+ (point) 3) end-current)))
177 (goto-char beg-current)
178 (if empty
179 (kill-line nil)
180 (forward-line 1)
181 )
182 (insert-string new-key))
183 (newline)))
184
185 (defun mark-bib ()
186 "Set mark at beginning of current or previous bib entry, point at end."
187 (interactive)
188 (beginning-of-line nil)
189 (if (looking-at "^ *$") (re-search-backward "[^ \n]" nil 2))
190 (re-search-backward "^ *$" nil 2)
191 (re-search-forward "^%")
192 (beginning-of-line nil)
193 (push-mark (point))
194 (re-search-forward "^ *$" nil 2)
195 (next-line 1)
196 (beginning-of-line nil))
197
198 (defun unread-bib ()
199 "Append current or previous entry to file of unread papers
200 named by variable `unread-bib-file'."
201 (interactive)
202 (mark-bib)
203 (if (get-file-buffer unread-bib-file)
204 (append-to-buffer (get-file-buffer unread-bib-file) (mark) (point))
205 (append-to-file (mark) (point) unread-bib-file)))
206
207
208 (defvar bib-capitalize-title-stop-words
209 (concat
210 "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
211 "by\\|with\\|that\\|its")
212 "Words not to be capitalized in a title (unless the first word).")
213
214 (defvar bib-capitalize-title-stop-regexp
215 (concat "\\(" bib-capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
216
217 (defun bib-capitalize-title-region (begin end)
218 "Like `capitalize-region', but don't capitalize stop words, except the first."
219 (interactive "r")
220 (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
221 (unwind-protect
222 (save-restriction
223 (set-syntax-table text-mode-syntax-table)
224 (narrow-to-region begin end)
225 (goto-char (point-min))
226 (if (looking-at "[A-Z][a-z]*[A-Z]")
227 (forward-word 1)
228 (capitalize-word 1))
229 (while (re-search-forward "\\<" nil t)
230 (if (looking-at "[A-Z][a-z]*[A-Z]")
231 (forward-word 1)
232 (if (let ((case-fold-search t))
233 (looking-at bib-capitalize-title-stop-regexp))
234 (downcase-word 1)
235 (capitalize-word 1)))
236 ))
237 (set-syntax-table orig-syntax-table))))
238
239
240 (defun bib-capitalize-title (s)
241 "Like `capitalize', but don't capitalize stop words, except the first."
242 (save-excursion
243 (set-buffer (get-buffer-create "$$$Scratch$$$"))
244 (erase-buffer)
245 (insert s)
246 (bib-capitalize-title-region (point-min) (point-max))
247 (buffer-string)))
248
249 (provide 'bib-mode)
250
251 ;;; bib-mode.el ends here