(capitalize-title-stop-words): 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; GNU Emacs code to help maintain databases compatible with (troff)
27 ;; refer and lookbib. The file bib-file should be set to your
28 ;; bibliography file. Keys are automagically inserted as you type,
29 ;; and appropriate keys are presented for various kinds of entries.
30
31 ;;; Code:
32
33 (defvar bib-file "~/my-bibliography.bib"
34 "Default name of file used by `addbib'.")
35
36 (defvar unread-bib-file "~/to-be-read.bib"
37 "Default name of file used by `unread-bib' in Bib mode.")
38
39 (defvar bib-mode-map (copy-keymap text-mode-map))
40 (define-key bib-mode-map "\C-M" 'return-key-bib)
41 (define-key bib-mode-map "\C-c\C-u" 'unread-bib)
42 (define-key bib-mode-map "\C-c\C-@" 'mark-bib)
43 (define-key bib-mode-map "\e`" 'abbrev-mode)
44 (defvar bib-mode-abbrev-table nil
45 "Abbrev table used in Bib mode")
46
47 (defun addbib ()
48 "Set up editor to add to troff bibliography file specified
49 by global variable `bib-file'. See description of `bib-mode'."
50 (interactive)
51 (find-file bib-file)
52 (goto-char (point-max))
53 (bib-mode)
54 )
55
56 (defun bib-mode ()
57 "Mode for editing `lookbib' style bibliographies.
58 Hit RETURN to get next % field key.
59 If you want to ignore this field, just hit RETURN again.
60 Use `text-mode' to turn this feature off.
61
62 journal papers: A* T D J V N P K W X
63 articles in books & proceedings: A* T D B E* I C P K W X
64 tech reports: A* T D R I C K W X
65 books: A* T D I C K W X
66
67 Fields:
68
69 A uthor T itle D ate J ournal
70 V olume N umber P age K eywords
71 B in book or proceedings E ditor C ity & state
72 I nstitution, school, or publisher
73 R eport number or 'phd thesis' or 'masters thesis' or 'draft' or
74 'unnumbered' or 'unpublished'
75 W here can be found locally (login name, or ailib, etc.)
76 X comments (not used in indexing)
77
78 \\[unread-bib] appends current entry to a different file (for example,
79 a file of papers to be read in the future), given by the value of the
80 variable `unread-bib-file'.
81 \\[mark-bib] marks current or previous entry.
82 Abbreviations are saved in `bib-mode-abbrev-table'.
83 Hook can be stored in `bib-mode-hook'.
84 Field keys given by variable `bib-assoc'.
85
86 Commands:
87 \\{bib-mode-map}
88 "
89 (interactive)
90 (text-mode)
91 (use-local-map bib-mode-map)
92 (setq mode-name "Bib")
93 (setq major-mode 'bib-mode)
94 (define-abbrev-table 'bib-mode-abbrev-table ())
95 (setq local-abbrev-table bib-mode-abbrev-table)
96 (abbrev-mode 1)
97 (run-hooks 'bib-mode-hook)
98 )
99
100 (defconst bib-assoc '(
101 (" *$" . "%A ")
102 ("%A ." . "%A ")
103 ("%A $" . "%T ")
104 ("%T " . "%D ")
105 ("%D " . "%J ")
106 ("%J ." . "%V ")
107 ("%V " . "%N ")
108 ("%N " . "%P ")
109 ("%P " . "%K ")
110 ("%K " . "%W ")
111 ("%W " . "%X ")
112 ("%X " . "")
113 ("%J $" . "%B ")
114 ("%B ." . "%E ")
115 ("%E ." . "%E ")
116 ("%E $" . "%I ")
117 ("%I " . "%C ")
118 ("%C " . "%P ")
119 ("%B $" . "%R ")
120 ("%R " . "%I ")
121 )
122
123 "Describes bibliographic database format. A line beginning with
124 the car of an entry is followed by one beginning with the cdr.
125 ")
126
127 (defun bib-find-key (slots)
128 (cond
129 ((null slots)
130 (if (bobp)
131 ""
132 (progn (previous-line 1) (bib-find-key bib-assoc))))
133 ((looking-at (car (car slots)))
134 (cdr (car slots)))
135 (t (bib-find-key (cdr slots)))
136 ))
137
138
139 (defvar bib-auto-capitalize t
140 "*True to automatically capitalize appropriate fields in Bib mode.")
141
142 (defconst bib-capitalized-fields "%[AETCBIJR]")
143
144 (defun return-key-bib ()
145 "Magic when user hits return, used by `bib-mode'."
146 (interactive)
147 (if (eolp)
148 (let (empty new-key beg-current end-current)
149 (beginning-of-line)
150 (setq empty (looking-at "%. $"))
151 (if (not empty)
152 (progn
153 (end-of-line)
154 (newline)
155 (forward-line -1)
156 ))
157 (end-of-line)
158 (setq end-current (point))
159 (beginning-of-line)
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))
164 (save-excursion
165 (capitalize-title-region (+ (point) 3) end-current)))
166 (goto-char beg-current)
167 (if empty
168 (kill-line nil)
169 (forward-line 1)
170 )
171 (insert-string new-key))
172 (newline)))
173
174 (defun mark-bib ()
175 "Set mark at beginning of current or previous bib entry, point at end."
176 (interactive)
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)
182 (push-mark (point))
183 (re-search-forward "^ *$" nil 2)
184 (next-line 1)
185 (beginning-of-line nil))
186
187 (defun unread-bib ()
188 "Append current or previous entry to file of unread papers
189 named by variable `unread-bib-file'."
190 (interactive)
191 (mark-bib)
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)))
195
196
197 (defvar capitalize-title-stop-words
198 (concat
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 they're the first word
202 in the title).")
203
204 (defvar capitalize-title-stop-regexp
205 (concat "\\(" capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
206
207 (defun capitalize-title-region (begin end)
208 "Like `capitalize-region', but don't capitalize stop words, except the first."
209 (interactive "r")
210 (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
211 (unwind-protect
212 (save-restriction
213 (set-syntax-table text-mode-syntax-table)
214 (narrow-to-region begin end)
215 (goto-char (point-min))
216 (if (looking-at "[A-Z][a-z]*[A-Z]")
217 (forward-word 1)
218 (capitalize-word 1))
219 (while (re-search-forward "\\<" nil t)
220 (if (looking-at "[A-Z][a-z]*[A-Z]")
221 (forward-word 1)
222 (if (let ((case-fold-search t))
223 (looking-at capitalize-title-stop-regexp))
224 (downcase-word 1)
225 (capitalize-word 1)))
226 ))
227 (set-syntax-table orig-syntax-table))))
228
229
230 (defun capitalize-title (s)
231 "Like `capitalize', but don't capitalize stop words, except the first."
232 (save-excursion
233 (set-buffer (get-buffer-create "$$$Scratch$$$"))
234 (erase-buffer)
235 (insert s)
236 (capitalize-title-region (point-min) (point-max))
237 (buffer-string)))
238
239 (provide 'bib-mode)
240
241 ;;; bib-mode.el ends here