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