BSD 4_4 development
[unix-history] / usr / contrib / lib / emacs / lisp / bibtex.el
CommitLineData
b7040acb
C
1;;; Simple BibTeX mode for GNU Emacs
2;;; Bengt Martensson 87-06-28
3;;; changes by Marc Shapiro shapiro@inria.inria.fr 15-oct-1986
4;;; (align long lines nicely; C-c C-o checks for the "OPT" string;
5;;; TAB goes to the end of the string; use lower case; use
6;;; run-hooks)
7;;; Marc Shapiro 19-oct-1987
8;;; add X window menu option; bug fixes. TAB, LFD, C-c " and C-c C-o now
9;;; behave consistently; deletion never occurs blindly.
10;;; Marc Shapiro 3-nov-87
11;;; addition for France: DEAthesis
12;;; Skip Montanaro <steinmetz!sprite!montanaro> 7-dec-87, Shapiro 10-dec-87
13;;; before inserting an entry, make sure we are outside of a bib entry
14;;; Marc Shapiro 14-dec-87
15;;; Cosmetic fixes. Fixed small bug in bibtex-move-outside-of-entry.
16
17;;; NOTE by Marc Shapiro, 14-dec-87:
18;;; (bibtex-x-environment) binds an X menu for bibtex mode to x-button-c-right.
19;;; Trouble is, in Emacs 18.44 you can't have a mode-specific mouse binding,
20;;; so it will remain active in all windows. Yuck!
21
22;;; Bengt Martensson 88-05-06:
23;;; Added Sun menu support. Locally bound to right mouse button in
24;;; bibtex-mode. Emacs 18.49 allows local mouse bindings!!
25;;; Commented out vtxxx-keys and DEAthesis. Changed documentation slightly.
26
27(defvar bibtex-mode-syntax-table nil "")
28(defvar bibtex-mode-abbrev-table nil "")
29(define-abbrev-table 'bibtex-mode-abbrev-table ())
30(defvar bibtex-mode-map (make-sparse-keymap) "")
31
32(defun bibtex-mode ()
33 "Major mode for editing bibtex files. Commands:
34\\{bibtex-mode-map}
35
36A command such as \\[bibtex-Book] will outline the fields for a BibTeX
37book entry.
38
39The optional fields are preceded by ""OPT"", thus ignored by BibTeX.
40Use \\[bibtex-remove-opt] to remove ""OPT"" on the current line.
41
42Use \\[bibtex-find-it] to position the dot at the end of the string on the same line.
43Use \\[bibtex-next-position] to move to the next position to fill in. Use \\[kill-current-line]
44to kill the whole line.
45
46M-x bibtex-x-environment binds a mode-specific X menu to control+right
47mouse button.
48M-x bibtex-sun-environment binds a mode-specific Sun menu to right
49mouse button.
50
51Fields:
52 address
53 Publisher's address
54 annote
55 Long annotation used for annotated bibliographies (begins sentence)
56 author
57 Name(s) of author(s), in BibTeX name format
58 booktitle
59 Book title when the thing being referenced isn't the whole book.
60 For book entries, the title field should be used instead.
61 chapter
62 Chapter number
63 edition
64 Edition of a book (e.g., ""second"")
65 editor
66 Name(s) of editor(s), in BibTeX name format.
67 If there is also an author field, then the editor field should be
68 for the book or collection that the work appears in
69 howpublished
70 How something strange has been published (begins sentence)
71 institution
72 Sponsoring institution
73 journal
74 Journal name (macros are provided for many)
75 key
76 Alphabetizing and labeling key (needed when no author or editor)
77 month
78 Month (macros are provided)
79 note
80 To help the reader find a reference (begins sentence)
81 number
82 Number of a journal or technical report
83 organization
84 Organization (sponsoring a conference)
85 pages
86 Page number or numbers (use `--' to separate a range)
87 publisher
88 Publisher name
89 school
90 School name (for theses)
91 series
92 The name of a series or set of books.
93 An individual book will will also have it's own title
94 title
95 The title of the thing being referenced
96 type
97 Type of a Techreport (e.g., ""Research Note"") to be used instead of
98 the default ""Technical Report""
99 volume
100 Volume of a journal or multivolume work
101 year
102 Year---should contain only numerals
103---------------------------------------------------------
104Entry to this mode calls the value of bibtex-mode-hook
105if that value is non-nil."
106 (interactive)
107 (kill-all-local-variables)
108 (if (not bibtex-mode-syntax-table)
109 (setq bibtex-mode-syntax-table (copy-syntax-table)))
110 (set-syntax-table bibtex-mode-syntax-table)
111 (modify-syntax-entry ?\$ "$$ ")
112 (modify-syntax-entry ?\% "< ")
113 (modify-syntax-entry ?\f "> ")
114 (modify-syntax-entry ?\n "> ")
115 (modify-syntax-entry ?' "w ")
116 (modify-syntax-entry ?@ "w ")
117 (use-local-map bibtex-mode-map)
118 (setq major-mode 'bibtex-mode)
119
120
121 (setq mode-name "BibTeX")
122 (set-syntax-table bibtex-mode-syntax-table)
123 (setq local-abbrev-table bibtex-mode-abbrev-table)
124 (make-local-variable 'paragraph-start)
125 (setq paragraph-start "^[ \f\n\t]*$")
126
127 (define-key bibtex-mode-map "\t" 'bibtex-find-it)
128 (define-key bibtex-mode-map "\n" 'bibtex-next-position)
129 ;;(define-key bibtex-mode-map "\e[25~" 'bibtex-next-position)
130 (define-key bibtex-mode-map "\C-c\"" 'bibtex-remove-double-quotes)
131 ;;(define-key bibtex-mode-map "\C-c\eOS" 'kill-current-line)
132 (define-key bibtex-mode-map "\C-c\C-k" 'kill-current-line)
133 (define-key bibtex-mode-map "\C-c\C-a" 'bibtex-Article)
134 (define-key bibtex-mode-map "\C-c\C-b" 'bibtex-Book)
135 ;;(define-key bibtex-mode-map "\C-c\C-d" 'bibtex-DEAthesis)
136 (define-key bibtex-mode-map "\C-c\C-c" 'bibtex-InProceedings)
137 (define-key bibtex-mode-map "\C-c\C-i" 'bibtex-InBook)
138 (define-key bibtex-mode-map "\C-ci" 'bibtex-InCollection)
139 (define-key bibtex-mode-map "\C-cI" 'bibtex-InProceedings)
140 (define-key bibtex-mode-map "\C-c\C-m" 'bibtex-Manual)
141 (define-key bibtex-mode-map "\C-cm" 'bibtex-MastersThesis)
142 (define-key bibtex-mode-map "\C-cM" 'bibtex-Misc)
143 (define-key bibtex-mode-map "\C-c\C-o" 'bibtex-remove-opt)
144 (define-key bibtex-mode-map "\C-c\C-p" 'bibtex-PhdThesis)
145 (define-key bibtex-mode-map "\C-cp" 'bibtex-Proceedings)
146 (define-key bibtex-mode-map "\C-c\C-t" 'bibtex-TechReport)
147 (define-key bibtex-mode-map "\C-c\C-s" 'bibtex-string)
148 (define-key bibtex-mode-map "\C-c\C-u" 'bibtex-Unpublished)
149 (define-key bibtex-mode-map "\C-c?" 'describe-mode)
150
151 ; nice alignements
152 (auto-fill-mode 1)
153 (setq left-margin 17)
154
155 (run-hooks 'bibtex-mode-hook))
156
157(defun bibtex-move-outside-of-entry ()
158 "Make sure we are outside of a bib entry"
159 (if (or
160 (= (point) (point-max))
161 (= (point) (point-min))
162 (looking-at "[ \n]*@")
163 )
164 t
165 (progn
166 (backward-paragraph)
167 (forward-paragraph)))
168 (re-search-forward "[ \t\n]*" (point-max) t))
169
170(defun bibtex-entry (entry-type required optional)
171 (bibtex-move-outside-of-entry)
172 (insert (concat "@" entry-type "{,\n\n}\n\n"))
173 (previous-line 3)
174 (insert (mapconcat 'bibtex-make-entry required ",\n"))
175 (if required (insert ",\n"))
176 (insert (mapconcat 'bibtex-make-opt-entry optional ",\n"))
177 (up-list -1)
178 (forward-char 1))
179
180(defun bibtex-make-entry (str)
181 (interactive "s")
182 (concat " " str " = \t"""""))
183
184(defun bibtex-make-opt-entry (str)
185 (interactive "s")
186 (concat " OPT" str " = \t"""""))
187
188(defun bibtex-Article ()
189 (interactive)
190 (bibtex-entry "Article" '("author" "title" "journal" "year")
191 '("volume" "number" "pages" "month" "note")))
192
193(defun bibtex-Book ()
194 (interactive)
195 (bibtex-entry "Book" '("author" "title" "publisher" "year")
196 '("editor" "volume" "series" "address"
197 "edition" "month" "note")))
198
199(defun bibtex-Booklet ()
200 (interactive)
201 (bibtex-entry "Booklet" '("title")
202 '("author" "howpublished" "address" "month" "year" "note")))
203
204;;; France: Dipl\^{o}me d'Etudes Approfondies (similar to Master's)
205;(defun bibtex-DEAthesis ()
206; (interactive)
207; (bibtex-entry "DEAthesis" '("author" "title" "school" "year")
208; '("address" "month" "note")))
209
210(defun bibtex-InBook ()
211 (interactive)
212 (bibtex-entry "InBook" '("author" "title" "chapter" "publisher" "year")
213 '("editor" "pages" "volume" "series" "address"
214 "edition" "month" "note")))
215
216(defun bibtex-InCollection ()
217 (interactive)
218 (bibtex-entry "InCollection" '("author" "title" "booktitle"
219 "publisher" "year")
220 '("editor" "chapter" "pages" "address" "month" "note")))
221
222
223(defun bibtex-InProceedings ()
224 (interactive)
225 (bibtex-entry "InProceedings" '("author" "title" "booktitle" "year")
226 '("editor" "pages" "organization" "publisher"
227 "address" "month" "note")))
228
229(defun bibtex-Manual ()
230 (interactive)
231 (bibtex-entry "Manual" '("title")
232 '("author" "organization" "address" "edition" "year"
233 "month" "note")))
234
235(defun bibtex-MastersThesis ()
236 (interactive)
237 (bibtex-entry "MastersThesis" '("author" "title" "school" "year")
238 '("address" "month" "note")))
239
240(defun bibtex-Misc ()
241 (interactive)
242 (bibtex-entry "Misc" '()
243 '("author" "title" "howpublished" "year" "month" "note")))
244
245(defun bibtex-PhdThesis ()
246 (interactive)
247 (bibtex-entry "PhDThesis" '("author" "title" "school" "year")
248 '("address" "month" "note")))
249
250(defun bibtex-Proceedings ()
251 (interactive)
252 (bibtex-entry "Proceedings" '("title" "year")
253 '("editor" "publisher" "organization"
254 "address" "month" "note")))
255(defun bibtex-TechReport ()
256 (interactive)
257 (bibtex-entry "TechReport" '("author" "title" "institution" "year")
258 '("type" "number" "address" "month" "note")))
259
260
261(defun bibtex-Unpublished ()
262 (interactive)
263 (bibtex-entry "Unpublished" '("author" "title" "note")
264 '("year" "month")))
265
266(defun bibtex-string ()
267 (interactive)
268 (bibtex-move-outside-of-entry)
269 (insert "@string{ = """"}\n")
270 (previous-line 1)
271 (forward-char 8))
272
273(defun bibtex-next-position ()
274 "Finds next position to write in."
275 (interactive)
276 (forward-line 1)
277 (bibtex-find-it))
278
279(defun bibtex-find-it ()
280 (interactive)
281 "Find position on current line (if possible) to add entry text."
282 (beginning-of-line)
283 (let ((beg (point)))
284 (end-of-line)
285 (search-backward "," beg t)
286 (backward-char 1)
287 (if (looking-at """")
288 t
289 (forward-char 1))
290 ))
291
292(defun bibtex-remove-opt ()
293 "Removes the 'OPT' starting optional arguments."
294 (interactive)
295 (beginning-of-line)
296 (forward-char 2)
297 (if (looking-at "OPT")
298 (delete-char 3))
299 (bibtex-find-it))
300
301(defun kill-current-line ()
302 "Kills the current line."
303 (interactive)
304 (beginning-of-line)
305 (kill-line 1))
306
307(defun bibtex-remove-double-quotes ()
308 "Removes """" around string."
309 (interactive)
310 (bibtex-find-it)
311 (let
312 ((here (point))
313 (eol (progn (end-of-line) (point))))
314 (beginning-of-line)
315 (if (search-forward """" eol t)
316 (progn
317 (delete-char -1)
318 (if (search-forward """" eol t)
319 (delete-char -1)
320 ))
321 (goto-char here))
322 )
323 )
324
325
326;;; X window menus for bibtex mode
327
328(defun bibtex-x-help (arg)
329 "Mouse commands for BibTeX mode"
330
331 (let ((selection
332 (x-popup-menu
333 arg
334 '("BibTeX commands"
335 ("Document types"
336 ("article in Conference Proceedings" . bibtex-InProceedings)
337 ("article in journal" . bibtex-Article)
338 ("Book" . bibtex-Book)
339 ("Booklet" . bibtex-Booklet)
340 ("Master's Thesis" . bibtex-MastersThesis)
341 ;;("DEA Thesis" . bibtex-DEAthesis)
342 ("PhD. Thesis" . bibtex-PhdThesis)
343 ("Technical Report" . bibtex-TechReport)
344 ("technical Manual" . bibtex-Manual)
345 ("Conference Proceedings" . bibtex-Proceedings)
346 ("in a Book" . bibtex-InBook)
347 ("in a Collection" . bibtex-InCollection)
348 ("miscellaneous" . bibtex-Misc)
349 ("unpublished" . bibtex-Unpublished)
350 )
351 ("others"
352 ("next field" . bibtex-next-position)
353 ("to end of field" . bibtex-find-it)
354 ("remove OPT" . bibtex-remove-opt)
355 ("remove quotes" . bibtex-remove-double-quotes)
356 ("remove this line" . kill-current-line)
357 ("describe BibTeX mode" . describe-mode)
358 ("string" . bibtex-string))))))
359 (and selection (call-interactively selection))))
360
361(defun bibtex-x-environment ()
362 "Set up X menus for BibTeX mode. Call it as bibtex-mode-hook, or interactively"
363 (interactive)
364 (require 'x-mouse)
365 (define-key mouse-map x-button-c-right 'bibtex-x-help)
366 )
367
368;; Please don't send anything to bug-gnu-emacs about these Sunwindows functions
369;; since we aren't interested. See etc/SUN-SUPPORT for the reasons why
370;; we consider this nothing but a distraction from our work.
371
372(defmenu bibtex-sun-entry-menu
373 ("Article In Conf. Proc."
374 (lambda () (eval-in-window *menu-window* (bibtex-InProceedings))))
375 ("Article In Journal"
376 (lambda () (eval-in-window *menu-window* (bibtex-Article))))
377 ("Book"
378 (lambda () (eval-in-window *menu-window* (bibtex-Book))))
379 ("Booklet"
380 (lambda () (eval-in-window *menu-window* (bibtex-Booklet))))
381 ("Master's Thesis"
382 (lambda () (eval-in-window *menu-window* (bibtex-MastersThesis))))
383 ;;("DEA Thesis" bibtex-DEAthesis)
384 ("PhD. Thesis"
385 (lambda () (eval-in-window *menu-window* (bibtex-PhdThesis))))
386 ("Technical Report"
387 (lambda () (eval-in-window *menu-window* (bibtex-TechReport))))
388 ("Technical Manual"
389 (lambda () (eval-in-window *menu-window* (bibtex-Manual))))
390 ("Conference Proceedings"
391 (lambda () (eval-in-window *menu-window* (bibtex-Proceedings))))
392 ("In A Book"
393 (lambda () (eval-in-window *menu-window* (bibtex-InBook))))
394 ("In A Collection"
395 (lambda () (eval-in-window *menu-window* (bibtex-InCollection))))
396 ("Miscellaneous"
397 (lambda () (eval-in-window *menu-window* (bibtex-Misc))))
398 ("Unpublished"
399 (lambda () (eval-in-window *menu-window* (bibtex-Unpublished)))))
400
401(defmenu bibtex-sun-menu
402 ("BibTeX menu")
403 ("add entry" . bibtex-sun-entry-menu)
404 ("add string"
405 (lambda () (eval-in-window *menu-window* (bibtex-string))))
406 ;("next field" bibtex-next-position)
407 ;("to end of field" bibtex-find-it)
408; ("remove OPT"
409; (lambda () (eval-in-window *menu-window* (bibtex-remove-opt))))
410; ("remove quotes"
411; (lambda () (eval-in-window *menu-window* (bibtex-remove-double-quotes))))
412; ("remove this line"
413; (lambda () (eval-in-window *menu-window* (kill-current-line))))
414 ("describe BibTeX mode"
415 (lambda () (eval-in-window *menu-window* (describe-mode))))
416 ("Main Emacs menu" . emacs-menu))
417
418(defun bibtex-sun-menu-eval (window x y)
419 "Pop-up menu of BibTeX commands."
420 (sun-menu-evaluate window (1+ x) (1- y) 'bibtex-sun-menu))
421
422(defun bibtex-sun-environment ()
423 "Set up sun menus for BibTeX mode. Call it as bibtex-mode-hook, or interactively"
424 (interactive)
425 (local-set-mouse '(text right) 'bibtex-sun-menu-eval))
426