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