comparison lisp/textmodes/bib-mode.el @ 187:cf77dffd7bba

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