comparison lisp/modes/bib-mode.el @ 0:376386a54a3c r19-14

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