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