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