1 ;;; bib-cite.el --- test
2 ;; bib-cite.el - Display \cite, \ref or \label / Extract refs from BiBTeX file.
4 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2003, 2004, 2005
5 ;; Free Software Foundation
7 ;; Author: Peter S. Galbraith <psg@debian.org>
8 ;; Created: 06 July 1994
9 ;; Version: 3.28 (Feb 23 2005)
10 ;; Keywords: bibtex, cite, auctex, emacs, xemacs
12 ;;; This file is not part of GNU Emacs.
14 ;; This package is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; This package is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
30 ;; bib-cite|Peter Galbraith|GalbraithP@dfo-mpo.gc.ca|
31 ;; Display \cite, \ref or \label / Extract refs from BiBTeX file.|
32 ;; 21-May-1997|3.01|~/misc/bib-cite.el.gz|
34 ;; ----------------------------------------------------------------------------
36 ;; This minor-mode is used in various TeX modes to display or edit references
37 ;; associated with \cite commands, or matching \ref and \label commands.
39 ;; New versions of this package (if they exist) may be found at:
40 ;; http://people.debian.org/~psg/elisp/bib-cite.el
41 ;; and in AUCTeX's CVS archive at
42 ;; http://savannah.gnu.org/cgi-bin/viewcvs/auctex/auctex/
45 ;; Works in unix, DOS and OS/2. Developped under Linux.
48 ;; AUCTeX is a super-charged LaTeX mode for emacs. Get it at:
50 ;; ftp://ftp.gnu.org/pub/gnu/auctex/
52 ;; WWW users may want to check out the AUCTeX page at
53 ;; http://www.gnu.org/software/auctex/
55 ;; bib-cite.el is included in the AUCTeX distribution. Therefore, if
56 ;; you use AUCTeX and didn't obtained bib-cite.el separately, make sure
57 ;; that you are actually using the more recent version.
60 ;; RefTeX is a package with similar functions to bib-cite.
61 ;; http://www.astro.uva.nl/~dominik/Tools/reftex/
62 ;; RefTeX is bundled and preinstalled with Emacs since version 20.2.
63 ;; It was also bundled with XEmacs 19.16--20.x.
65 ;; I suggest that you use RefTeX to help you type-in text as it's functions
66 ;; are better suited to this task than bib-cite, and use bib-cite's features
67 ;; when you proof-read the text.
68 ;; If you wish bib-cite to use RefTeX's reftex-view-crossref command to
69 ;; display and find \label's and \cite bibliography entries, set the variable
70 ;; bib-cite-use-reftex-view-crossref to t.
73 ;; Multifile documents are supported by bib-cite by using etags (TAGS files)
74 ;; which contains a bug for MSDOS (at least for emacs 19.27 it does).
76 ;; http://people.debian.org/~psg/elisp/bib-cite.etags-bug-report
77 ;; to see what patches to make to etags.c to fix it.
81 ;; This package is used in various TeX modes to display or edit references
82 ;; associated with \cite commands, or matching \eqref, \ref and \label
83 ;; commands (so I actually overstep BiBTeX bounds here...).
85 ;; These are the functions:
87 ;; bib-display bib-display-mouse
88 ;; - Display citation, \ref or \label under point
89 ;; bib-find bib-find-mouse
90 ;; - Edit citation, \ref or \label under point
91 ;; bib-find-next - Find next occurrence of a \ref or \eqref
92 ;; bib-make-bibliography - Make BiBTeX file containing only cite keys used.
93 ;; bib-apropos - Search BiBTeX source files for keywords.
94 ;; bib-etags - Refreshes (or builds) the TAGS files for
95 ;; multi-file documents.
96 ;; bib-create-auto-file - Used in bibtex-mode to create cite key
97 ;; completion .el file for AUCTeX.
98 ;; bib-highlight-mouse - Highlight \cite, \ref and \label commands in
99 ;; green when the mouse is over them.
101 ;; About Cite Commands and related functions:
102 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103 ;; Various flavors of \cite commands are allowed (as long as they contain
104 ;; the word `cite') and they may optionally have bracketed [] options.
105 ;; Bibtex Cross-references are displayed, and @string abbreviations are
106 ;; substituted or included.
108 ;; The \cite text is found (by emacs) in the bibtex source files listed in the
109 ;; \bibliography command. The BiBTeX files can be located in a search path
110 ;; defined by an environment variable (typically BIBINPUTS, but you can change
113 ;; All citations used in a buffer can also be listed in a new bibtex buffer by
114 ;; using bib-make-bibliography. This is useful to make a bibtex file for a
115 ;; document from a large bibtex database. In this case, cross-references are
116 ;; included, as well as the @string commands used. The @string abbreviations
117 ;; are not substituted.
119 ;; The bibtex files can also be searched for entries matching a regular
120 ;; expression using bib-apropos.
122 ;; Usage instructions:
123 ;; ~~~~~~~~~~~~~~~~~~
124 ;; bib-display Bound to Mouse-3 when specially highlighted.
125 ;; In Hyperbole, bound to the Assist key.
126 ;; Bound to `\C-c b d'
128 ;; bib-display will show the bibtex entry or the corresponding label or
129 ;; ref commands from anywhere within a document.
130 ;; With cursor on the \cite command itslef
131 ;; -> display all citations of the cite command from the BiBTeX source.
132 ;; With cursor on a particular cite key within the brackets
133 ;; -> display that citation's text from the BiBTeX source file(s).
137 ;; \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
138 ;; ^Cursor -> Display-all-citations ^Cursor -> Display-this-citation
140 ;; With cursor on a \label command
141 ;; -> Display first matching \ref command in the document
142 ;; With cursor on a \ref command
143 ;; -> Display environment associated with the matching \label command.
145 ;; Finding a ref or label within a multi-file document requires a TAGS file,
146 ;; which is automatically generated for you. This enables you to then use
147 ;; any tags related emacs features.
149 ;; bib-find Bound to Mouse-2 when specially highlighted.
150 ;; In Hyperbole, bound to the Action key.
151 ;; Bound to `\C-c b f'
153 ;; bib-find will select the buffer and move point to the BiBTeX source file
154 ;; at the proper citation for a cite command, or move point to anywhere
155 ;; within a document for a label or ref command. The ref chosen is the
156 ;; first occurrance within a document (using a TAGS file). If point is
157 ;; moved within the same buffer, mark is set before the move and a message
158 ;; stating so is given. If point is moved to another file, this is done in
159 ;; a new window using tag functions.
161 ;; The next occurrence of a \ref or \eqref command may be found by invoking
162 ;; bib-find-next, usually bound to `C-c b n'.
164 ;; For multi-file documents, you must be using AUCTeX (so that bib-cite can
165 ;; find the master file) and all \input and \include commands must be first
166 ;; on a line (not preceeded by any non-white text).
168 ;; bib-make-bibliography: Bound to `\C-c b m'
170 ;; Extract citations used in the current document from the \bibliography{}
171 ;; file(s). Put them into a new suitably-named buffer. In a AUCTeX
172 ;; multi-file document, the .aux files are used to find the cite keys (for
173 ;; speed). You will be warned if these are out of date.
175 ;; This buffer is not saved to a file. It is your job to save it to whatever
176 ;; name you wish. Note that AUCTeX has a unique name space for LaTeX and
177 ;; BiBTeX files, so you should *not* name the bib file associated with
178 ;; example.tex as example.bib! Rather, name it something like
181 ;; bib-apropos: Bound to `\C-c b a'
183 ;; Searches the \bibliography{} file(s) for entries containing a keyword and
184 ;; display them in the *help* buffer. You can trim down your search by using
185 ;; bib-apropos in the *Help* buffer after the first invocation. the current
186 ;; buffer is also searched for keyword matches if it is in bibtex-mode.
188 ;; It doesn't display cross-references nor does it substitute or display
189 ;; @string commands used. It could easily be added, but it's faster this
190 ;; way. Drop me a line if this would be a useful addition.
192 ;; If you find yourself entering a cite command and have forgotten which key
193 ;; you want, but have entered a few initial characters as in `\cite{Gal',
194 ;; then invoke bib-apropos. It will take that string (in this case `Gal') as
195 ;; an initial response to the apropos prompt. You are free to edit it, or
196 ;; simply press carriage return.
198 ;; bib-etags: Bound to `\C-c b e'
200 ;; Creates a TAGS file for AUCTeX's multi-file document (or refreshes it).
201 ;; This is used by bib-find when editing multi-file documents. The TAGS file
202 ;; is created automatically, but it isn't refreshed automatically. So if
203 ;; bib-find can't find something, try running bib-etags again.
205 ;; bib-create-auto-file:
207 ;; Use this when editing a BiBTeX buffer to generate the AUCTeX .el file
208 ;; which tell emacs about all its cite keys. I've added this command to
209 ;; bibtex-mode pull-down menu.
211 ;; bib-highlight-mouse: Bound to `\C-c b h'
213 ;; Highlights \cite, \ref and \label commands in green when the mouse is over
214 ;; them. By default, a call to this function is added to LaTeX-mode-hook
215 ;; (via bib-cite-initialize) if you set bib-highlight-mouse-t to true. But
216 ;; you may want to run this command to refresh the highlighting for newly
219 ;; Installation instructions:
220 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~
221 ;; bib-cite is a minor-mode, so you could invoke it in a LaTeX-mode hook.
222 ;; e.g. If you are using AUCTeX (http://www.gnu.org/software/auctex/), you
225 ;; (autoload 'turn-on-bib-cite "bib-cite")
226 ;; (add-hook 'LaTeX-mode-hook 'turn-on-bib-cite)
228 ;; If you are using Emacs' regular LaTeX-mode, use instead:
230 ;; (autoload 'turn-on-bib-cite "bib-cite")
231 ;; (add-hook 'latex-mode-hook 'turn-on-bib-cite)
233 ;; bib-cite can be used with AUCTeX, or stand-alone. If used with AUCTeX on a
234 ;; multi-file document (and AUCTeX's parsing is used), then all \bibliography
235 ;; commands in the document will be found and used.
237 ;; The following variable can be unset (like shown) to tell bib-cite to
238 ;; not give advice messages about which commands to use to find the next
239 ;; occurrence of a search:
241 ;; (setq bib-novice nil)
243 ;; If you wish bib-cite to use RefTeX's reftex-view-crossref command to
244 ;; display and find \label's and \cite bibliography entries, set the variable
245 ;; bib-cite-use-reftex-view-crossref to t:
247 ;; (setq bib-cite-use-reftex-view-crossref t)
249 ;; The following variable determines whether we will attempt to highlight
250 ;; citation, ref and label commands in green when they are under the
251 ;; mouse. When highlighted, the mouse keys work to call bib-display
252 ;; (bound to [mouse-3]) and bib-find (bound to [mouse-2]). If you use a
253 ;; mode other than LaTeX-mode, you'll want to call bib-highlight-mouse with
254 ;; a hook (See how we do this at the end of this file with the add-hook
257 ;; (setq bib-highlight-mouse-t nil)
259 ;; The variable bib-switch-to-buffer-function sets the function used to
260 ;; select buffers (if they differ from the original) in bib-cite commands
261 ;; bib-make-bibliography, bib-display, bib-find
262 ;; You may use `switch-to-buffer' `switch-to-buffer-other-window' or
263 ;; `switch-to-buffer-other-frame'.
265 ;; If you use DOS or OS/2, you may have to set the following variable:
267 ;; (setq bib-dos-or-os2-variable t)
269 ;; if bib-cite.el fails to determine that you are using DOS or OS/2.
270 ;; Try `C-h v bib-dos-or-os2-variable' to see if it needs to be set manually.
272 ;; bib-cite needs to call the etags program with its output file option
273 ;; and also with the append option (usually -a).
274 ;; I figured that DOS and OS/2 would use "etags /o=" instead of the unix
275 ;; variant "etags -o ", but users have reported differently. So while the
276 ;; unix notation is used here, you can reset it if you need to like so:
278 ;; (setq bib-etags-command "etags /r='/.*\\\(eq\|page\|[fvF]\)ref.*/' /o=")
279 ;; (setq bib-etags-append-command
280 ;; "etags /r='/.*\\\(eq\|page\|[fvF]\)ref.*/' /a /o=")
282 ;; For multi-file documents, a TAGS file is generated by etags.
283 ;; By default, its name is TAGS. You can change this like so:
285 ;; (setq bib-etags-filename "TAGSLaTeX")
287 ;; If your environment variable to find BiBTeX files is not BIBINPUTS, then
288 ;; reset it with the following variable (here, assuming it's TEXBIB instead):
290 ;; (setq bib-bibtex-env-variable "TEXBIB")
292 ;; Note that any directory ending in a double slash will cause bib-cite to
293 ;; search recursively through subdirectories for your .bib files. This can
294 ;; be slow, so use this judiciously.
295 ;; e.g. setenv BIBINPUTS .:/home/psg/LaTeX/bibinputs//
296 ;; -> all directories below /home/psg/LaTeX/bibinputs/ will be
299 ;; If your bibtex setup works but Emacs can't see the environment variable
300 ;; correctly (Check `C-h v process-environment'), then customize the
301 ;; variable `bib-cite-inputs' (e.g. `M-x customize-variable bib-cite-imputs')
304 ;; If you do not wish bib-display to substitute @string abbreviations,
305 ;; then set the following variable like so:
307 ;; (setq bib-substitute-string-in-display nil)
309 ;; Warnings are given when @string abbreviations are not defined in your bib
310 ;; files. The exception is for months, usually defined in style files. If you
311 ;; use other definitions in styles file (e.g. journals), then you may add them
312 ;; to the `bib-substitute-string-in-display' list variable.
314 ;; If you find circumstances in which this package fails, please let me know.
316 ;; Things for me to do in later versions:
317 ;; - treat @Strings correctly, not just in isolation.
318 ;; - use `kpsewhich -expand-path='$BIBINPUTS'` instead of BIBINPUTS.
319 ;; - jmv@di.uminho.pt (Jose Manuel Valenca) wants:
320 ;; - prompt for \cite as well as \label and \ref
321 ;; (and use AUCTeX's completion list)
322 ;; - implement string concatenation, with #[ \t\n]*STRING_NAME
323 ;; - Create new command to substitute @string text in any bibtex buffer.
324 ;; ----------------------------------------------------------------------------
326 ;; V3.28 Feb 23 2005 - Ralf Angeli
327 ;; - Some doc fixes in the commentary section.
328 ;; V3.27 Feb 09 2005 - PSG
329 ;; - Patch from Peter Heslin. TeX-master can now have symbol values.
330 ;; V3.26 Aug 06 2004 - Reiner Steib
331 ;; - Changed URL of AUCTeX. Use "AUCTeX", not "auc-tex" (skipped Change log).
332 ;; V3.25 Feb 15 2004 - PSG
333 ;; - Check existence of font-lock-unset-defaults; no longer defined in CVS
334 ;; Emacs. Thanks to Adrian Lanz for reporting the problem.
335 ;; V3.24 Oct 28 2003 - PSG
336 ;; - bib-cite-file-directory-p: new function to replace ff-paths code.
337 ;; V3.23 Oct 09 2003 - PSG
338 ;; - some checkdoc cleanup; not yet complete.
339 ;; V3.22 Sep 17 2003 - PSG
340 ;; - bib-cite-aux-inputs: new defcustom.
341 ;; - minor cleanup for `match-string'.
342 ;; V3.21 Sep 08 2003 - PSG
343 ;; - Ripping out off-topic imenu code.
344 ;; V3.20 Aug 14 2003 - PSG
345 ;; - psg-checkfor-file-list: Allow for relative directoties as entries in
347 ;; - bib-cite-inputs: new defcustom equivalent to BIBINPUTS.
348 ;; - bib-label-help-echo-format: fixed defcustom.
349 ;; - psg-list-env: code cleanup.
350 ;; - trailing whitespace cleanup.
351 ;; V3.19 Apr 06 2003 - PSG
352 ;; Remove code that ran when defcustom not present.
353 ;; Remove hilit19 obsolete code.
354 ;; V3.18 Mar 27 2003 - Bruce Ravel <ravel@phys.washington.edu>
355 ;; Play well with the varioref and fancyref latex styles (vref, fref, Fref).
356 ;; V3.17 May 01 2001 - (RCS V1.38)
357 ;; - XEmacs has imenu after all.
358 ;; V3.16 Dec 20 99 - (RCS V1.37)
359 ;; - Added customize support.
360 ;; V3.15 Dec 20 99 - (RCS V1.36)
361 ;; - Removed stupid debugging code that I had left in.
363 ;; - New variable bib-ref-regexp for \ref regexp to match \label constructs
364 ;; and added \pageref. (RCS V1.34)
365 ;; - Edited bib-etags-command snd bib-etags-append-command to match.
366 ;; V3.13 Dec 20 99 - (RCS V1.32)
367 ;; - License changed to GPL.
368 ;; - Kai Engelhardt <ke@socs.uts.edu.au> bib-master-file takes .ltx extension
369 ;; - imenu--create-LaTeX-index-for-document and bib-document-TeX-files
370 ;; edited to accept .ltx extension.
371 ;; - Michael Steiner <steiner@cs.uni-sb.de> added journals to @string
372 ;; abbrevs and contributed `member-cis' to complaces @strings in a
373 ;; case-insensitive manner.
374 ;; V3.12 Dec 10 98 - Bruce Ravel <bruce.ravel@nist.gov> (RCS V1.30)
375 ;; Fixed bib-label-help.
376 ;; V3.11 Oct 06 98 - PSG (RCS V1.29)
377 ;; Quote \ character fot replace-match;
378 ;; Applies to: @String{JGR = "J. Geophys.\ Res."}
379 ;; V3.10 Sep 21 98 - Matt Hodges <mph1002@cam.ac.uk> (RCS V1.28)
380 ;; Removed instance of expand-file-name due to new behaviour in Emacs-20.3.
381 ;; V3.09 Sep 09 98 - PSG (RCS V1.27)
382 ;; Added support for \eqref; Added bib-find-next.
383 ;; V3.08 Aug 20 98 - PSG (RCS V1.26)
384 ;; Fixed imenu bug (prev-pos (point-max))
385 ;; V3.07 Nov 20 97 - Christoph Wedler <wedler@fmi.uni-passau.de> (RCS V1.24)
386 ;; bib-ext-list variable made permanent-local, otherwise VC registration
387 ;; would use two extents for each reference etc. This was not a visible bug.
388 ;; V3.06 Nov 12 97 - PSG (RCS V1.23)
389 ;; Support use of reftex's reftex-view-crossref command.
390 ;; V3.05 Nov 12 97 - Vladimir Alexiev <vladimir@cs.ualberta.ca> (RCS V1.22)
391 ;; regexp-quote the bibliography keys so a key like galbraith+kelley97 works
392 ;; V3.04 Aug 25 97 - Christoph Wedler <wedler@fmi.uni-passau.de> (RCS V1.20)
393 ;; (bib-highlight-mouse): Would bug out on detached extents,
394 ;; e.g. when killing a whole citation.
395 ;; V3.03 Jul 16 97 - Christoph Wedler <wedler@fmi.uni-passau.de> (RCS V1.18)
396 ;; turn-on-bib-cite back to non-interactive.
397 ;; V3.02 Jul 11 97 - Christoph Wedler <wedler@fmi.uni-passau.de> (RCS V1.17)
398 ;; * auctex/bib-cite.el (turn-on-bib-cite): Make interactive.
399 ;; Argument to `bib-cite-minor-mode' is 1.
400 ;; (bib-label-help-echo-format): New variable.
401 ;; (bib-label-help-echo): New function.
402 ;; (bib-label-help): Addition argument format.
403 ;; (bib-highlight-mouse): Set extent property `help-echo' for XEmacs.
404 ;; V3.01 May 22 97 - Diego Calvanese <calvanes@dis.uniroma1.it> (RCS V1.16)
405 ;; bib-make-bibliography handles commas separated citations in aux files.
406 ;; V3.00 May 16 97 - Peter Galbraith (RCS V1.15)
407 ;; bib-cite is now a minor mode.
408 ;; V2.32 Apr 30 97 - Anders Stenman <stenman@isy.liu.se> (RCS V1.14)
409 ;; - Support for balloon-help.
410 ;; V2.31 Mar 20 97 - Christoph Wedler <wedler@fmi.uni-passau.de> (RCS V1.12)
411 ;; - Better fontification of help buffer as bibtex or latex for XEmacs.
412 ;; V2.30 Feb 10 97 - Peter Galbraith (RCS V1.11)
413 ;; - Better fontification of help buffer as bibtex or latex.
414 ;; V2.29 Jan 29 97 - Peter Galbraith (RCS V1.10)
415 ;; - imenu looks for `\label{stuff}' instead of `\label'
416 ;; V2.28 Jan 22 97 - Peter Galbraith (RCS V1.9)
417 ;; - Bug in bib-create-auto-file.
418 ;; V2.27 Dec 31 96 - Peter Galbraith (RCS V1.8)
419 ;; - allow spaces between cite keys.
420 ;; - Vladimir Alexiev <vladimir@cs.ualberta.ca>
421 ;; Allow () delimiters as well as {}.
422 ;; Better check on bibtex-menu
423 ;; Erase *bibtex-bibliography* buffer.
424 ;; V2.26 Sep 24 96 - Peter Galbraith (RCS V1.7)
426 ;; V2.25 Sep 23 96 - Anders Stenman <stenman@isy.liu.se> (RCS V1.6)
427 ;; XEmacs bib-cite-fontify-help-as-latex bug fix.
428 ;; V2.24 Aug 19 96 - Peter Galbraith (RCS V1.3)
429 ;; XEmacs bug fix, minor defvars - Vladimir Alexiev <vladimir@cs.ualberta.ca>
430 ;; V2.23 Aug 13 96 - Peter Galbraith (RCS V1.2)
431 ;; XEmacs - Add bib-cite entries to bibtex-mode popup menu.
432 ;; V2.22 July 22 96 - Peter Galbraith (RCS V1.1)
433 ;; local-map has `m' for bib-make-bibliography instead of `b'
434 ;; set-buffer-menubar in XEmacs so that menu disappears after use.
435 ;; V2.21 July 12 96 - Peter Galbraith
436 ;; Define `\C-c b' keymap for both plain tex and auctex, in XEmacs and emacs.
437 ;; Separate menu-bar menu in gnu emacs.
438 ;; font-lock support for bib-display'ed citations (bibtex fontification)
439 ;; and for matching \ref{} and \labels (latex fontification).
440 ;; buffer-substring-no-properties in bib-apropos
441 ;; (bug in completing-read with mouse faces)
442 ;; imenu-sort-function made local and nil.
443 ;; imenu--LaTeX-name-and-position fixed for section name containing "\"
444 ;; Various other things... (whitespace within label strings, etc...)
445 ;; V2.20 June 25 96 - Peter Galbraith
446 ;; imenu fixed for emacs-19.31.
449 ;; - @string substitution fixed; bib-edit-citation fixed when buffer exists;
450 ;; Christoph Wedler <wedler@fmi.uni-passau.de>:
451 ;; - Added bib-switch-to-buffer-function
452 ;; - (setq tags-always-exact nil) for xemacs
453 ;; - removed eval-after-load foe xemacs
454 ;; V2.18 May 06 96 - PSG
455 ;; New eval-after-load from Fred Devernay <Frederic.Devernay@sophia.inria.fr>
456 ;; V2.17 May 03 96 - PSG
457 ;; Fixed bug introduced in V2.16, reported by Dennis Dams <wsindd@win.tue.nl>
458 ;; V2.16 May 02 96 - Vladimir Alexiev <vladimir@cs.ualberta.ca>
459 ;; - somewhat compatible with Hyperbole by binding bib-find and bib-display to
460 ;; the Action and Assist keys inside the bib-highlight-mouse-keymap.
461 ;; - makes more liberal provisions for users with a tty.
463 ;; - fix "Buffer read-only" error caused by mouse-face text properties
464 ;; patch by Piet van Oostrum <piet@cs.ruu.nl>
465 ;; - Use tmm non-X menu, patch by Vladimir Alexiev <vladimir@cs.ualberta.ca>
466 ;; - input{file.txt} would not work.
467 ;; bug report: David Kastrup <dak@pool.informatik.rwth-aachen.de>
468 ;; V2.14 Feb 26 96 - PSG - define eval-after-load for xemacs
469 ;; Frederic Devernay's <Frederic.Devernay@sophia.inria.fr> suggestion.
470 ;; V2.13 Feb 08 96 - Peter Galbraith - Fixed recursive use of bib-apropos.
471 ;; V2.12 Jan 19 96 - Peter Galbraith
472 ;; emacs-19.30's [down-mouse-1] is defined (rather than [mouse-1]), so
473 ;; bib-highlight-mouse-keymap now has [down-mouse-1] defined to override it.
474 ;; V2.11 Nov 21 95 - Peter Galbraith
475 ;; - Fixed bib-create-auto-file when bib file loaded before LaTeX file.
476 ;; - Michal Mnuk's better imenu labels menu <Michal.Mnuk@risc.uni-linz.ac.at>
477 ;; - [mouse-1] and [mouse-2] key defs for highlighted regions.
478 ;; - Improve X menus.
479 ;; - Skip over style files in bib-document-TeX-files.
480 ;; - Add menus and mouse highlighting for xemacs
481 ;; Anders Stenman <stenman@isy.liu.se> Dima Barsky <D.Barsky@ee.surrey.ac.uk>
482 ;; - Check bib-use-imenu before calling LaTeX-hook-setq-imenu.
483 ;; From: Kurt Hornik <hornik@ci.tuwien.ac.at>
484 ;; - Remove mouse face properties before inserting new ones.
485 ;; From: Peter Whaite <peta@Whippet.McRCIM.McGill.EDU>
486 ;; V2.10 Aug 17 95 - Peter Galbraith - fatal bugs in bib-make-bibliography.
487 ;; V2.09 Jul 19 95 - Peter Galbraith
488 ;; - Had introduced bug in search-directory-tree. synced with ff-paths.el.
489 ;; V2.08 Jul 13 95 - Peter Galbraith
490 ;; Fred Douglis <douglis@research.att.com> says etags should be required
491 ;; V2.07 Jul 04 95 - Peter Galbraith
492 ;; - Minor changes with filename manipulations (careful with DOS...)
493 ;; - Problem if auc-tex not already loaded -> LaTeX-mode-map
494 ;; V2.06 Jul 03 95 - Peter Galbraith - Added recursion through BIBINPUTS path.
495 ;; V2.05 Jun 22 95 - Peter Galbraith Bug: Hanno Wirth <wirth@jake.igd.fhg.de>
496 ;; bib-get-citations would truncate @String{KEY ="J. {\"u} Res."}
497 ;; V2.04 Jun 19 95 - Peter Galbraith -
498 ;; - use bibtex-mode syntax table in bib buffer, else bib-apropos truncates
499 ;; an article if it contains an unbalanced closing parenthesis.
500 ;; - bib-highlight-mouse would mark a buffer modified
501 ;; V2.03 May 16 95 - Peter Galbraith -
502 ;; auc-tec menu compatible with old "AUC TeX" pull-down name
503 ;; V2.02 May 10 95 - Peter Galbraith -
504 ;; bug report by Bodo Huckestein <bh@thp.Uni-Koeln.DE> (getenv env) under DOS
505 ;; V2.01 Mar 27 95 - Peter Galbraith - No imenu on xemacs; check BIBINPUT also
506 ;; V2.00 Mar 27 95 - Peter Galbraith
507 ;; - bib-find and bib-display replace bib-edit-citation and
508 ;; bib-display-citation
509 ;; - bib-apropos now take initial guess from start of cite argument at point.
510 ;; - Multi-file support for bib-make-bibliography using .aux files.
511 ;; - \label and \ref functionality for bib-find and bib-display:
512 ;; - \label may appear within an \begin\end or to label a (sub-)section.
513 ;; - Cursor on \label, goto first \ref, set next i-search to pattern.
514 ;; - Cursor on \ref, goto \label or display it's environment or section.
515 ;; - Works on hidden code!
516 ;; V1.08 Jan 16 95 - Peter Galbraith
517 ;; bib-apropos can be used within *Help* buffer to trim a search.
518 ;; V1.07 Dec 13 94 - Peter Galbraith
519 ;; - Fixed: multi-line @string commands in non-inserted display.
520 ;; - Fixed: quoted \ character in @string commands.
521 ;; - BiBTeX comments should not affect bib-cite
522 ;; - Fixed bib-apropos (from Christoph Wedler <wedler@fmi.uni-passau.de>)
523 ;; Faster now, and avoids infinite loops.
524 ;; - Added bib-edit-citation to edit a bibtex files about current citation.
525 ;; - Allow space and newlines between citations: \cite{ entry1, entry2}
526 ;; - Added bib-substitute-string-in-display, bib-string-ignored-warning
527 ;; and bib-string-regexp.
528 ;; - bib-display-citation (from Markus Stricker <stricki@vision.ee.ethz.ch>)
529 ;; Could not find entry with trailing spaces
530 ;; V1.06 Nov 20 94 - Peter Galbraith
531 ;; - Fixed bib-apropos for:
532 ;; hilighting without invoking bibtex mode.
533 ;; display message when no matches found.
534 ;; would search only last bib file listed (forgot to `goto-char 1')
535 ;; - Fixed bib-make-bibliography that would only see first citation in a
536 ;; multi-key \cite command (found by Michail Rozman <roz@physik.uni-ulm.de>
537 ;; - bib-make-bibliography didn't see \cite[A-Z]* commands.
538 ;; Found by Richard Stanton <stanton@haas.berkeley.edu>
539 ;; **************************************************
540 ;; - * Completely rewritten code to support crossrefs *
541 ;; **************************************************
542 ;; - autodetection of OS/2 and DOS for bib-dos-or-os2-variable
543 ;; - Created bib-display-citation-mouse
544 ;; - bib-apropos works in bibtex-mode on the current buffer
545 ;; - bibtex entry may have comma on next line (!)
546 ;; @ARTICLE{Kiryati-91
549 ;; V1.05 Nov 02 94 - Peter Galbraith
550 ;; - bug fix by rossmann@TI.Uni-Trier.DE (Jan Rossmann)
551 ;; for (boundp 'TeX-check-path) instead of fboundp. Thanks!
552 ;; - Translate environment variable set by bib-bibtex-env-variable.
553 ;; (suggested by Richard Stanton <stanton@haas.berkeley.edu>)
554 ;; - add bib-dos-or-os2-variable to set environment variable path separator
555 ;; - Add key-defs for any tex-mode and auc-tex menu-bar entries.
556 ;; [in auc-tec TeX-mode-map is common to both TeX and LaTeX at startup
557 ;; (but TeX-mode-map is only copied to LaTeX-mode-map at initilisation)
558 ;; in plain emacs, use tex-mode-map for both TeX and LaTeX.]
559 ;; - Add key def for bibtex-mode to create auc-tex's parsing file.
560 ;; - Fix bugs found by <thompson@loon.econ.wisc.edu>
561 ;; - fix bib-get-citation for options
562 ;; - fix bib-get-citation for commas preceeded citation command
563 ;; - better regexp for citations and their keys.
564 ;; - Added @string support for any entry (not just journal entries).
565 ;; (I had to disallow numbers in @string keys because of years.
567 ;; - added bib-apropos
568 ;; V1.04 Oct 24 94 - Peter Galbraith
569 ;; - Don't require dired-aux, rather define the function we need from it.
570 ;; - Regexp-quote the re-search for keys.
571 ;; - Name the bib-make-bibliography buffer diffently than LaTeX buffer
572 ;; because auc-tex's parsing gets confused if same name base is used.
573 ;; V1.03 Oct 24 94 - Peter Galbraith - require dired-aux for dired-split
574 ;; V1.02 Oct 19 94 - Peter Galbraith
575 ;; - If using auc-tex with parsing activated, use auc-tex's functions
576 ;; to find all \bibliography files in a multi-file document.
577 ;; - Find bib files in pwd, BIBINPUTS environment variable path and
578 ;; TeX-check-path elisp variable path.
579 ;; - Have the parser ignore \bibliography that is on a commented `%' line.
580 ;; (patched by Karl Eichwalder <karl@pertron.central.de>)
581 ;; - Allow for spaces between entry type and key in bib files:
582 ;; (e.g @Article{ key} )
583 ;; (suggested by Nathan E. Doss <doss@ERC.MsState.Edu>)
584 ;; - Allows options in \cite command (e.g. agu++ package \cite[e.g.][]{key})
585 ;; - Includes @String{} abbreviations for `journal' entries
586 ;; V1.01 July 07 94 - Peter Galbraith - \bibliography command may have list of
587 ;; BibTeX files. All must be readable.
588 ;; V1.00 July 06 94 - Peter Galbraith - Created
589 ;; ----------------------------------------------------------------------------
592 (eval-when-compile (require 'cl))
594 (defgroup bib-cite nil
595 "bib-cite, LaTeX minor-mode to display \\cite, \\ref and \\label commands."
598 (defcustom bib-cite-use-reftex-view-crossref nil
599 "*Non-nil means, RefTeX will be used to find cross references.
600 When this variable is non-nil, both `bib-find' and `bib-display' will
601 call a function in RefTeX to find or display the cross reference of a
602 \\ref or \\cite macro at point."
606 (defcustom bib-novice t
607 "*Give advice to novice users about what commands to use next."
611 (defcustom bib-switch-to-buffer-function 'switch-to-buffer
612 "*Function used to select buffers if they differ from the original.
613 You may use `switch-to-buffer' `switch-to-buffer-other-window' or
614 `switch-to-buffer-other-frame'."
616 :type '(choice (function-item switch-to-buffer)
617 (function-item switch-to-buffer-other-window)
618 (function-item switch-to-buffer-other-frame)))
620 (defcustom bib-highlight-mouse-t t
621 "*Call bib-highlight-mouse from `LaTeX-mode-hook' to add green highlight."
625 (defcustom bib-label-help-echo-format "button2 finds %s, button3 displays %s"
626 "*Format string for info if the mouse is over LaTeX commands.
627 If nil, do not display info."
629 :type '(radio (const :tag "Don't display info" nil)
632 (defcustom bib-bibtex-env-variable "BIBINPUTS"
633 "*Environment variable setting the path where BiBTeX input files are found.
634 BiBTeX 0.99b manual says this should be TEXBIB.
635 Another version says it should BSTINPUTS. I don't know anymore!
637 The colon character (:) is the default path separator in unix, but you may
638 use semi-colon (;) for DOS or OS/2 if you set bib-dos-or-os2-variable to t."
642 (defcustom bib-cite-inputs nil
643 "*List of directories to search for .bib files.
644 This is in addition to those listed in the environment variable specified by
645 `bib-bibtex-env-variable'."
647 :type '(repeat (file :format "%v")))
649 (defcustom bib-cite-aux-inputs nil
650 "*List of directories to search for .aux files.
651 MiKTeX has the LaTeX option -aux-directory to store .aux files in an alternate
652 directory. You may set this variable to let bib-cite find these .aux files."
654 :type '(repeat (file :format "%v")))
656 (defcustom bib-dos-or-os2-variable (or (equal 'emx system-type)
657 (equal 'ms-dos system-type))
658 ;; Under OS/2 system-type equals emx
659 ;; Under DOS system-type equals ms-dos
660 "*Whether you use DOS or OS/2 for bib-make-bibliography/bib-display.
662 It tells bib-make-bibliography and bib-display to translate
663 the BIBINPUTS environment variable using the \";\" character as
664 a path separator and to translate DOS' backslash to slash.
666 e.g. Use a path like \"c:\\emtex\\bibinput;c:\\latex\\bibinput\"
668 \(You can change the environment variable which is searched by setting the elisp variable bib-bibtex-env-variable)"
672 (defcustom bib-etags-command "etags -r '/.*\\\\\\(eq\\|page\\|[fvF]\\)ref.*/' -o "
673 "*Variable for the etags command and its output option.
674 In unix, this is usually \"etags -r '/.*\\\(eq\|page\|[fvF]\)ref.*/' -o \"
675 \(we use the -r option to tell etags to list AMS-LaTeX's \\eqref command.)
676 In DOS and OS/2, this *may* be different, e.g. using slashes like \"etags /o=\"
677 If so, set it this variable."
681 (defcustom bib-etags-append-command "etags -r '/.*\\\\\\(eq\\|page\\|[fvF]\\)ref.*/' -a -o "
682 "*Variable for the etags command and its append and output option.
683 In unix, this is usually \"etags -r '/.*\\\(eq\|page\|[fvF]\)ref.*/' -a -o \"
684 In DOS and OS/2, this *may* be \"etags /a /o=\" If so, set it this variable."
688 (defcustom bib-etags-filename "TAGS"
689 "*Variable for the filename generated by etags, by defaults this TAGS.
690 but you may want to change this to something like TAGSLaTeX such that it can
691 coexist with some other tags file in your master file directory."
695 (defcustom bib-ref-regexp "\\\\\\(eq\\|page\\|[fvF]\\)?ref"
696 "*Regular expression for \\ref LaTeX commands that have a matching \\label.
697 without the curly bracket.
699 If you change this variable and you use multi-file documents, make sure you
700 also edit the variables bib-etags-command and bib-etags-append-command."
704 (defcustom bib-substitute-string-in-display t
705 "*Determines if bib-display will substitute @string definitions.
706 If t, then the @string text is substituted.
707 If nil, the text is not substituted but the @string entry is included."
711 (defcustom bib-string-ignored-warning
712 '("jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "sept" "oct" "nov"
713 "dec" "acmcs" "acta" "cacm" "ibmjrd" "ibmjs" "ieeese" "ieeetcad"
714 "ieeetc" "ipl" "jacm" "jcss" "scp" "sicomp" "tcs" "tocs" "tods" "tog"
715 "toms" "toois" "toplas" )
716 "*@string abbreviations for which a warning is not given if not defined.
717 These are usually month abbreviations (or journals) defined in a style file."
719 :type '(repeat (string :tag "Entry")))
721 ;;<<<<<<User-Modifiable variables end here.
723 (defvar bib-ref-regexpc (concat bib-ref-regexp "{")
724 "*Regular expression for \\ref LaTeX commands that have a matching \\label.
725 A opening curly bracket is appended to the regexp.")
727 (defvar bib-cite-is-XEmacs
728 (not (null (save-match-data (string-match "XEmacs\\|Lucid" emacs-version)))))
730 (defvar bib-cite-minor-mode nil)
732 (defvar bib-highlight-mouse-keymap (make-sparse-keymap)
733 "Keymap for mouse bindings in highlighted texts in bicite.")
735 (defvar bib-ext-list nil
736 "Xemacs buffer-local list of bib-cite extents.")
737 (make-variable-buffer-local 'bib-ext-list)
738 (put 'bib-ext-list 'permanent-local t)
740 (defvar bib-cite-minor-mode-menu nil)
743 (defun bib-cite-minor-mode (arg)
744 "Toggle bib-cite mode.
745 When bib-cite mode is enabled, citations, labels and refs are highlighted
746 when the mouse is over them. Clicking on these highlights with [mouse-2]
747 runs bib-find, and [mouse-3] runs bib-display."
749 (set (make-local-variable 'bib-cite-minor-mode)
751 (> (prefix-numeric-value arg) 0)
752 (not bib-cite-minor-mode)))
754 (bib-cite-minor-mode ;Setup the minor-mode
755 ;; Christoph Wedler's <wedler@fmi.uni-passau.de> suggestion for xemacs
756 ;; Added for version 2.19
757 (if (boundp 'tags-always-exact)
759 (make-local-variable 'tags-always-exact)
760 (setq tags-always-exact nil)))
762 (if bib-highlight-mouse-t
764 (bib-cite-setup-highlight-mouse-keymap)
765 (bib-highlight-mouse)
766 (make-local-hook 'after-change-functions)
767 (add-hook 'after-change-functions
768 'bib-cite-setup-mouse-function nil t)))
769 (if bib-cite-is-XEmacs
771 (or (local-variable-p 'current-menubar (current-buffer))
772 (set-buffer-menubar current-menubar))
773 (add-submenu nil bib-cite-minor-mode-menu))))
775 ;;;Undo the minor-mode
780 (delete-extent (car bib-ext-list))
781 (setq bib-ext-list (cdr bib-ext-list))))
783 (remove-hook 'after-change-functions 'bib-cite-setup-mouse-function t)
784 (let ((before-change-functions) (after-change-functions))
785 ;; FIXME This detroys all mouse-faces and local-maps!
786 ;; FIXME Hope no other package is using them in this buffer!
787 (remove-text-properties (point-min) (point-max)
788 '(mouse-face t local-map t)))))
789 (if bib-cite-is-XEmacs
790 (delete-menu-item '("BCite"))))))
792 ;;This must be eval'ed when the LaTeX mode is in use.
793 ;; bib-highlight-mouse-keymap is a local variable so each buffer can have it's
795 (defun bib-cite-setup-highlight-mouse-keymap ()
796 "Set up the bib-cite text in the current buffer to be clickable."
797 (make-local-variable 'bib-highlight-mouse-keymap)
798 (setq bib-highlight-mouse-keymap
799 ;;; First, copy the local keymap so we don't have `disappearing' menus
800 ;;; when the mouse is moved over a \ref, \label or \cite command.
802 ;;; FIXME: Check out (mouse-major-mode-menu) to see how it grabs the local
803 ;;; menus to display. Maybe on `highlighted' commands we could only
804 ;;; display the bib-cite stuff (or a subset of it).
805 (let ((m (copy-keymap (current-local-map))))
808 (set-keymap-name m 'bib-highlight-mouse-keymap)
810 ;;action-key stuff from Vladimir Alexiev <vladimir@cs.ualberta.ca>
811 ((commandp 'action-key)
812 ;; for hyperbole. The Right Way is to define implicit buttons
813 ;; (defib) bib-cite and label-ref instead of overriding
814 ;; action-key and assist key, so that eg smart key help can
815 ;; be obtained, but I'm lazy.
816 (substitute-key-definition 'action-key 'bib-find m global-map)
817 (substitute-key-definition 'assist-key 'bib-display m global-map)
818 (substitute-key-definition 'action-key-depress
819 'bib-find-mouse m global-map)
820 (substitute-key-definition 'assist-key-depress
821 'bib-display-mouse m global-map)
822 (substitute-key-definition 'action-mouse-key nil m global-map)
823 (substitute-key-definition 'assist-mouse-key nil m global-map))
824 (t ; xemacs, not hyperbole
825 (define-key m "\e\r" 'bib-find-mouse) ; bug Fixed in V2.17
826 (define-key m "\e\n" 'bib-display-mouse) ;bug Fixed in V2.17
827 ;;(define-key m [(shift button1)] 'bib-display-mouse)
828 (define-key m [button3] 'bib-display-mouse)
829 (define-key m [button2] 'bib-find-mouse))))
832 ((commandp 'action-key)
833 (substitute-key-definition 'action-key 'bib-find m global-map)
834 (substitute-key-definition 'assist-key 'bib-display m global-map)
835 (substitute-key-definition 'action-mouse-key-emacs19
836 'bib-find-mouse m global-map)
837 (substitute-key-definition 'assist-mouse-key-emacs19
838 'bib-display-mouse m global-map)
839 (substitute-key-definition 'action-key-depress-emacs19
841 (substitute-key-definition 'assist-key-depress-emacs19
843 (t ; emacs 19, not hyperbole
844 (define-key m [down-mouse-3] 'bib-display-mouse)
845 (define-key m [mouse-2] 'bib-find-mouse)))))
849 (defun turn-on-bib-cite ()
850 "Unconditionally turn on Bib Cite mode."
851 (bib-cite-minor-mode 1))
853 (defun bib-cite-setup-mouse-function (beg end old-len)
858 (progn (goto-char beg) (beginning-of-line) (point))
859 (progn (goto-char end) (forward-line 1) (point)))
860 (bib-highlight-mouse)))))
862 (defvar bib-cite-minor-mode-map
863 (let ((map (make-sparse-keymap)))
864 (define-key map "\C-cba" 'bib-apropos)
865 (define-key map "\C-cbb" 'bib-make-bibliography)
866 (define-key map "\C-cbd" 'bib-display)
867 (define-key map "\C-cbe" 'bib-etags)
868 (define-key map "\C-cbf" 'bib-find)
869 (define-key map "\C-cbn" 'bib-find-next)
870 (define-key map "\C-cbh" 'bib-highlight-mouse)
872 "Bib-cite minor-mode keymap.")
875 bib-cite-minor-mode-menu bib-cite-minor-mode-map "Menu keymap for bib-cite."
877 ["Make BibTeX bibliography buffer" bib-make-bibliography t]
878 ["Display citation or matching \\ref or \\label" bib-display t]
879 ["Find BibTeX citation or matching \\ref or \\label" bib-find t]
880 ["Search apropos BibTeX files" bib-apropos t]
881 ["Build TAGS file for multi-file document" bib-etags (bib-master-file)]
882 ["Refresh \\cite, \\ref and \\label mouse highlight"
883 bib-highlight-mouse t]))
885 ;; Install ourselves:
886 (or (assq 'bib-cite-minor-mode minor-mode-alist)
887 (setq minor-mode-alist
888 (cons '(bib-cite-minor-mode " BCite") minor-mode-alist)))
889 (or (assq 'bib-cite-minor-mode minor-mode-map-alist)
890 (setq minor-mode-map-alist
891 (cons (cons 'bib-cite-minor-mode bib-cite-minor-mode-map)
892 minor-mode-map-alist)))
895 ;;; Add a menu entry to bibtex.el (Perhaps I should not do this).
897 ((and (string-match "XEmacs\\|Lucid" emacs-version)
899 (fboundp 'smart-menu))) ;text menus by Bob Weiner
901 ;; xemacs under X with AUCTeX
904 ;; Add to bibtex.el's popup menu
905 (defvar bib-cite-xemacs-bibtex-mode-menu
909 ["Search apropos BibTeX files" bib-apropos t]
910 ["Create AUCTeX auto parsing file" bib-create-auto-file t])
911 "Submenu of bibtex-mode menu, used by bib-cite.")
913 (if (boundp 'bibtex-menu)
918 bib-cite-xemacs-bibtex-mode-menu))
919 ;; Setup to add menu later
920 (defun bib-cite-bibtex-mode-hook ()
921 (if (boundp 'bibtex-menu)
926 bib-cite-xemacs-bibtex-mode-menu))
927 (remove-hook 'bibtex-mode-hook 'bib-cite-bibtex-mode-hook))))
928 (add-hook 'bibtex-mode-hook 'bib-cite-bibtex-mode-hook))
931 ((and (not (string-match "XEmacs\\|Lucid" emacs-version))
932 (string-equal "19" (substring emacs-version 0 2))
934 (fboundp 'tmm-menubar))) ; 19.30 - Will autoload if necessary
936 ;; emacs-19 under X-windows (or non-X with tmm)
939 ;; This *almost* makes me want to switch over to XEmacs...
941 ;; to AUCTeX auto file for a bibtex buffer
946 ((lookup-key bibtex-mode-map [menu-bar move/edit])
948 (lookup-key bibtex-mode-map [menu-bar move/edit])
949 [bib-nil] '("---" . nil) '"--")
951 (lookup-key bibtex-mode-map [menu-bar move/edit])
952 [bib-apropos] '("Search Apropos" . bib-apropos) 'bib-nil)
954 (lookup-key bibtex-mode-map [menu-bar move/edit])
956 '("Create AUCTeX auto parsing file" . bib-create-auto-file)
958 ((lookup-key bibtex-mode-map [menu-bar bibtex-edit])
960 (lookup-key bibtex-mode-map [menu-bar bibtex-edit])
961 [bib-nil] '("---" . nil) '"--")
963 (lookup-key bibtex-mode-map [menu-bar bibtex-edit])
964 [bib-apropos] '("Search Apropos" . bib-apropos) 'bib-nil)
966 (lookup-key bibtex-mode-map [menu-bar bibtex-edit])
968 '("Create AUCTeX auto parsing file" . bib-create-auto-file)
971 ;; Following from bibtex.el
973 bib-cite-bibtex-font-lock-keywords
974 '(("^\\( \\|\t\\)*\\(@[A-Za-z]+\\)[ \t]*[({]\\([][A-Za-z0-9.:;?!`'()/*@_+=|<>-]+\\)?"
975 (2 font-lock-function-name-face)
976 (3 font-lock-reference-face nil t))
977 ;; reference type and reference label
978 ("^[ \t]*\\(OPT[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*\\)[ \t]*="
979 1 font-lock-comment-face)
980 ;; optional field names (treated as comments)
981 ("^[ \t]*\\([^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*\\)[ \t]*="
982 1 font-lock-variable-name-face)
984 "Default expressions to fontify in BibTeX mode."))
986 (defvar bib-cite-bibtex-mode-syntax-table
987 (let ((st (make-syntax-table)))
988 ;; [alarson:19920214.1004CST] make double quote a string quote
989 (modify-syntax-entry ?\" "\"" st)
990 (modify-syntax-entry ?$ "$$ " st)
991 (modify-syntax-entry ?% "< " st)
992 (modify-syntax-entry ?' "w " st)
993 (modify-syntax-entry ?@ "w " st)
994 (modify-syntax-entry ?\\ "\\" st)
995 (modify-syntax-entry ?\f "> " st)
996 (modify-syntax-entry ?\n "> " st)
997 (modify-syntax-entry ?~ " " st)
999 ;; Code from bibtex.el ends
1001 ;; @string starts with a letter and does not contain any of ""#%'(),={}
1002 ;; Here we do not check that the field contains only one string field and
1004 (defvar bib-string-regexp
1005 "^[, \t]*[a-zA-Z]+[ \t]*=[ \t]*\\([a-zA-Z][^#%'(),={}\" \t\n]*\\)"
1006 "Regular expression for field containing a @string.")
1008 (defun bib-display ()
1009 "Display BibTeX citation or matching \\ref or \\label command under point.
1011 If text under cursor is a \\cite command, then display its BibTeX info from
1012 \\bibliography input file.
1013 Example with cursor located over cite command or arguments:
1014 \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
1015 ^Display-all-citations ^Display-this-citation
1017 If text under cursor is a \\ref command, then display environment associated
1018 with its matching \\label command.
1020 If text under cursor is a \\label command, then display the text around
1021 the first matching \\ref command.
1023 The user is prompted for a \\label or \\ref is nothing suitable is found under
1024 the cursor. The first prompt is for a label. If you answer with an empty
1025 string, a second prompt for a ref will be given.
1027 A TAGS file is created and used for multi-file documents under auctex."
1029 (let ((cite)(ref)(label))
1031 (if (not (looking-at "\\\\"))
1032 (search-backward "\\" nil t))
1033 (if (looking-at bib-ref-regexpc)
1035 (if (looking-at "\\\\label{")
1039 ;; reftex doesn't handle label->ref
1040 ((and bib-cite-use-reftex-view-crossref
1042 ;;;FIXME: reftex doesn't want point on \ref or \cite part, but on keyword
1044 (reftex-view-crossref nil))
1046 (bib-display-citation))
1048 (bib-display-label)))))
1051 "Edit BibTeX citation or find matching \\ref or \\label command under point.
1053 For multi-entry cite commands, the cursor should be on the actual cite key
1054 desired (otherwise a random entry will be selected).
1055 e.g.: \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
1056 ^Display-this-citation
1058 If text under cursor is a \\ref command, then point is moved to its matching
1061 If text under cursor is a \\label command, then point is moved to the first
1062 matching \\ref command.
1064 The user is prompted for a \\label or \\ref is nothing suitable is found under
1065 the cursor. The first prompt is for a label. If you answer with an empty
1066 string, a second prompt for a ref will be given.
1068 A TAGS file is created and used for multi-file documents under auctex."
1070 (let ((cite)(ref)(label))
1072 (if (not (looking-at "\\\\"))
1073 (search-backward "\\" nil t))
1074 (if (looking-at bib-ref-regexpc)
1076 (if (looking-at "\\\\label{")
1080 ;; reftex doesn't handle label->ref
1081 ((and bib-cite-use-reftex-view-crossref
1084 (reftex-view-crossref t))
1086 (bib-edit-citation))
1088 (bib-find-label)))))
1090 (defvar bib-cite-search-ring nil
1091 "Bib-cite intenal variable to hold last \\ref or \\eqref find.")
1093 (defun bib-find-next (&optional prev-p)
1094 "Find next occurrence of a \ref or \eqref.
1095 This is made necessary because we now use a regexp to find tags in multi-file
1096 documents, and the Emacs command `find-tag' doesn't allow to interactively
1097 find the next occurrence of a regexp."
1099 (if (bib-master-file) ;Multi-file document
1103 (if bib-cite-search-ring
1104 ;;FIXME: Should first make sure I move off initial \ref{}.
1105 (let ((regexp (concat bib-ref-regexpc bib-cite-search-ring "}")))
1107 (if (not (re-search-backward regexp nil t))
1108 (message "No previous occurrence of reference %s"
1109 bib-cite-search-ring))
1110 (if (not (re-search-forward regexp nil t))
1111 (message "No next occurrence of reference %s"
1112 bib-cite-search-ring))))
1113 (message "Sorry, no previous reference to find. Use bib-find?"))))
1115 (defun bib-display-mouse (EVENT)
1116 "Display BibTeX citation or matching \\ref or \\label under mouse EVENT.
1119 (mouse-set-point EVENT)
1122 (defun bib-find-mouse (EVENT)
1123 "Edit BibTeX citation or find matching \\ref or \\label under mouse EVENT.
1126 (mouse-set-point EVENT)
1129 (defun bib-apropos ()
1130 "Display BibTeX entries containing a keyword from bibliography file.
1131 The files specified in the \\bibliography command are searched unless
1132 the current buffer is in `bibtex-mode' or is the Help buffer. In those
1133 cases, *it* is searched. This allows you to trim down a search further
1134 by using bib-apropos sequentially."
1135 ;;(interactive "sBibTeX apropos: ")
1137 (let* ((keylist (and (boundp 'TeX-auto-update) ;Avoid error in FRAMEPOP
1138 (fboundp 'LaTeX-bibitem-list) ;Use this if using auctex
1139 (LaTeX-bibitem-list)))
1140 (keyword (bib-apropos-keyword-at-point))
1141 (keyword (completing-read "BiBTeX apropos: " keylist nil nil keyword))
1142 (the-text)(key-point)(start-point)
1143 (new-buffer-f (and (not (string-match "^bib" mode-name))
1144 (not (string-equal "*Help*" (buffer-name)))))
1145 (bib-buffer (or (and new-buffer-f (bib-get-bibliography nil))
1148 (set-buffer bib-buffer)
1149 (goto-char (point-min))
1150 (while (and (re-search-forward "^[ \t]*@" nil t)
1151 (re-search-forward keyword nil t))
1152 (setq key-point (point)) ;To make sure this is within entry
1153 (re-search-backward "^[ \t]*@" nil t)
1154 (setq start-point (point))
1156 (if (< (point) key-point) ;And this is that test...
1157 (goto-char key-point) ;Not within entry, skip it.
1159 (cons (concat (buffer-substring start-point (point)) "\n")
1162 (message "Sorry, no matches found.")
1163 (with-output-to-temp-buffer "*Help*"
1164 (mapcar 'princ (nreverse the-text)))
1165 (bib-cite-fontify-help-as-bibtex)
1168 (substitute-command-keys
1169 (concat "Use \\[bib-apropos] again in the *help* buffer"
1170 " to trim the search")))))
1172 (kill-buffer bib-buffer)))))
1174 (defvar bib-document-citekeys-obarray-warnings nil
1175 "Bib-cite internal variable.")
1177 (defun bib-make-bibliography ()
1178 "Extract citations used in the current document from \bibliography{} file(s).
1179 Put them into a buffer named after the current buffer, with extension .bib.
1181 In an AUCTeX multi-file document, parsing must be on and the citation keys
1182 are extracted from the .aux files.
1184 In a plain LaTeX buffer (not multi-file), the cite keys are extracted from
1185 the text itself. Therefore the text need not have been previously processed
1188 This function is useful when you want to share a LaTeX file, and therefore want
1189 to create a bibtex file containing only the references used in the document."
1191 (let* ((the-keys-obarray (or (bib-document-citekeys-obarray)
1192 (bib-buffer-citekeys-obarray)))
1193 ;1st in case of error
1196 (concat (substring (buffer-name) 0
1197 (or (string-match "\\." (buffer-name))
1198 (length (buffer-name))))
1200 (bib-buffer (bib-get-bibliography nil))
1201 (the-warnings (bib-get-citations the-keys-obarray
1205 (kill-buffer bib-buffer)
1206 ;;; (switch-to-buffer new-buffer)
1207 (funcall bib-switch-to-buffer-function new-buffer)
1209 (if (or bib-document-citekeys-obarray-warnings
1213 ((and bib-document-citekeys-obarray-warnings the-warnings)
1214 (with-output-to-temp-buffer "*Help*"
1215 (princ bib-document-citekeys-obarray-warnings the-warnings)))
1216 (bib-document-citekeys-obarray-warnings
1217 (with-output-to-temp-buffer "*Help*"
1218 (princ bib-document-citekeys-obarray-warnings)))
1220 (with-output-to-temp-buffer "*Help*" (princ the-warnings))))
1221 (setq bib-document-citekeys-obarray-warnings nil) ;Reset
1222 (bib-cite-fontify-red)))
1225 (substitute-command-keys
1226 "Use \\[save-buffer] to save this buffer to a file.")))))
1228 (defun bib-cite-fontify-red (&optional limit)
1229 "Fontify *Help* buffer in red-bold up to optional LIMIT."
1230 (if (and window-system ;Not exactly correct for XEmacs
1231 (not (facep 'red-bold)))
1233 (copy-face 'bold 'red-bold)
1234 (set-face-foreground 'red-bold "red")))
1236 (set-buffer "*Help*")
1237 (let ((before-change-functions) (after-change-functions))
1238 (put-text-property (point-min)(or limit (point-max))
1241 (defun bib-cite-fontify-help-xemacs (defaults)
1242 (if (fboundp 'font-lock-set-defaults-1) ; >= XEmcas 19.14
1244 (set-buffer "*Help*")
1245 (setq font-lock-defaults-computed nil
1246 font-lock-keywords nil)
1247 (font-lock-set-defaults-1
1248 (and defaults (font-lock-find-font-lock-defaults defaults)))
1249 (font-lock-fontify-buffer)
1250 (setq font-lock-defaults-computed nil
1251 font-lock-keywords nil)
1252 (font-lock-set-defaults-1))))
1254 (defun bib-cite-fontify-help-as-bibtex ()
1257 ((not (featurep 'font-lock))
1258 nil) ;No font-lock! Stop here.
1259 ;; font-lock under Emacs and XEmacs
1260 ((string-match "XEmacs\\|Lucid" emacs-version)
1262 (bib-cite-fontify-help-xemacs 'bibtex-mode))
1265 (set-buffer "*Help*")
1266 (let ((font-lock-defaults
1267 '(bib-cite-bibtex-font-lock-keywords
1268 nil t ((?$ . "\"")(?\" . ".")))))
1271 (if (fboundp 'font-lock-unset-defaults) (font-lock-unset-defaults))
1272 (font-lock-unfontify-buffer))
1273 (font-lock-fontify-buffer))))))
1275 (defun bib-cite-fontify-help-as-latex ()
1278 ((not (featurep 'font-lock))
1279 nil) ;No font-lock! Stop here.
1280 ;; font-lock under Emacs and XEmacs
1281 ((string-match "XEmacs\\|Lucid" emacs-version)
1282 ;; XEmacs, not necessary to do s.th. special for font-latex, we do *not*
1283 ;; want the buffer-local faces!
1284 (bib-cite-fontify-help-xemacs 'latex-mode))
1287 (set-buffer "*Help*")
1288 ;; Actually, don't want to `permanently' affect *Help* buffer...
1289 ;;(if (featurep 'font-latex)
1290 ;; (font-latex-setup)
1291 ;; Rather I should deal with this in the `let' form:
1292 ;; (make-local-variable 'font-lock-string-face)
1293 ;; (setq font-lock-string-face font-latex-math-face
1294 ;; font-latex-string-face (default-value 'font-lock-string-face))
1295 (let ((font-lock-defaults
1296 (if (featurep 'font-latex)
1297 '((font-latex-keywords font-latex-keywords-1
1298 font-latex-keywords-2)
1299 nil nil ((?\( . ".") (?\) . ".") (?$ . "\"")) nil
1300 (font-lock-comment-start-regexp . "%")
1301 (font-lock-mark-block-function . mark-paragraph))
1302 '(tex-font-lock-keywords nil nil ((?$ . "\""))))))
1305 (if (fboundp 'font-lock-unset-defaults) (font-lock-unset-defaults))
1306 (font-lock-unfontify-buffer))
1307 (font-lock-fontify-buffer))))))
1309 (defvar bib-document-TeX-files-warnings nil
1310 "Bib-cite internal variable.")
1312 (defun bib-etags (&optional masterdir)
1313 "Invoke etags on all tex files of the document in directory MASTERDIR.
1314 Store the TAGS file in the master-directory.
1315 Expect errors if you use this outside of auctex or within a plain
1316 single-file document. Also makes sure that the TAGS buffer is updated.
1317 See variables bib-etags-command and bib-etags-filename"
1320 (let* ((the-file-list (bib-document-TeX-files))
1321 (the-file (car the-file-list))
1322 (dir (or masterdir (bib-master-directory)))
1323 (the-tags-file (expand-file-name bib-etags-filename dir))
1324 (the-tags-buffer (get-file-buffer the-tags-file)))
1325 ;; Create TAGS file with first TeX file (master file)
1326 (shell-command (concat bib-etags-command the-tags-file " " the-file))
1327 (setq the-file-list (cdr the-file-list))
1328 ;; Append to TAGS file for all other TeX files.
1329 (while the-file-list
1330 (setq the-file (car the-file-list))
1332 (concat bib-etags-append-command the-tags-file " " the-file))
1333 (setq the-file-list (cdr the-file-list)))
1334 (if the-tags-buffer ;buffer existed; we must refresh it.
1336 (set-buffer the-tags-buffer)
1337 (revert-buffer t t)))
1339 ;; Check value of tags-file-name against the-tags-file
1340 (or (equal the-tags-file tags-file-name) ;make sure it's current
1341 (visit-tags-table the-tags-file))
1343 ;(set (make-local-variable 'tags-file-name) the-tags-file))
1344 ;; above should not be needed
1347 ;; (visit-tags-table-buffer) seems to get called twice when called by
1348 ;; find-tag on an undefined tag. The second time, it's in the TAGS
1349 ;; buffer and returns an error because TAGS buffer does have
1350 ;; tags-file-name set.
1351 ;; To get around this. I'm setting this variable in the TAGS buffer.
1352 ;; Skip this in XEmacs (Changed by Anders Stenman)
1353 (if (and (not (string-match "XEmacs\\|Lucid" emacs-version))
1354 (get-file-buffer the-tags-file))
1356 (set-buffer (get-file-buffer the-tags-file))
1357 (set (make-local-variable 'tags-file-name) the-tags-file))))
1360 (if bib-document-TeX-files-warnings ;free variable loose in emacs!
1362 (with-output-to-temp-buffer "*Help*"
1363 (princ bib-document-TeX-files-warnings))
1364 (setq bib-document-TeX-files-warnings nil) ;Reset
1365 (bib-cite-fontify-red))))
1367 (defun bib-Is-hidden ()
1368 "Return true is current point is hidden."
1369 (if (not selective-display)
1370 nil ;Not hidden if not using this...
1372 (if (not (re-search-backward "[\n\^M]" nil t))
1374 (if (string-equal (match-string 0) "\n")
1378 (defun bib-highlight-mouse ()
1379 "Make that nice green highlight when the mouse is over LaTeX commands."
1381 ;;;Comment this out. User should be able to use bib-highlight-mouse
1382 ;;;to try it out regardless of bib-highlight-mouse-t.
1383 ;;;Check bib-highlight-mouse-t only in automated cases.
1385 ;;; (if (and bib-highlight-mouse-t
1386 ;;; ;;window-system) ;Do nothing unless under X
1388 ;;; *all of code was here*
1391 (let ((s)(e)(extent)(local-extent-list bib-ext-list)
1392 (inhibit-read-only t)
1393 (modified (buffer-modified-p))) ;put-text-property changing this?
1394 ;; * peta Wed Nov 8 16:27:29 1995 -- better remove the mouse face
1395 ;; properties first.
1396 (setq bib-ext-list nil) ;Reconstructed below...
1397 (if (string-match "XEmacs\\|Lucid" emacs-version)
1398 (while local-extent-list
1399 (setq extent (car local-extent-list))
1400 (if (or (extent-detached-p extent)
1401 (and (<= (point-min)(extent-start-position extent))
1402 (>= (point-max)(extent-end-position extent))))
1403 (delete-extent extent)
1404 (setq bib-ext-list (cons extent bib-ext-list)))
1405 (setq local-extent-list (cdr local-extent-list)))
1406 ;; Remove properties for regular emacs
1407 ;; FIXME This detroys all mouse-faces and local-maps!
1408 ;; FIXME Hope no other package is using them in this buffer!
1409 (let ((before-change-functions) (after-change-functions))
1410 (remove-text-properties (point-min) (point-max)
1411 '(mouse-face t local-map t))))
1412 (goto-char (point-min))
1416 "\\\\\\(" (substring bib-ref-regexp 2)
1417 "\\|label\\|[A-Za-z]*cite[A-Za-z]*\\(\\[.*\\]\\)?\\){[^}]*}")
1419 (setq s (match-beginning 0))
1420 (setq e (match-end 0))
1422 ((string-match "XEmacs\\|Lucid" emacs-version)
1423 (setq extent (make-extent s e))
1424 (setq bib-ext-list (cons extent bib-ext-list))
1425 (set-extent-property extent 'highlight t)
1426 (set-extent-property extent 'start-open t)
1427 (set-extent-property extent 'balloon-help 'bib-label-help)
1428 (set-extent-property extent 'help-echo 'bib-label-help-echo)
1429 (set-extent-property extent 'keymap bib-highlight-mouse-keymap))
1431 (let ((before-change-functions) (after-change-functions)
1432 ;;(this-overlay (make-overlay s e))
1434 ;;; Even using overlays doens't help here. If bib-highlight-mouse-keymap
1435 ;;; does not include the AucTeX menus, then these disappear when we click
1436 ;;; onto a \cite command. Perhaps using bib-cite as a minor mode will fix
1437 ;;; this? For now, bib-cite must be loaded after these menus are built.
1438 ;;; It must therefore be loaded in a mode-hook.
1439 (put-text-property s e 'local-map bib-highlight-mouse-keymap)
1440 (put-text-property s e 'mouse-face 'highlight)
1441 ;;(overlay-put this-overlay 'local-map bib-highlight-mouse-keymap)
1442 ;;(overlay-put this-overlay 'mouse-face 'highlight)
1444 (set-buffer-modified-p modified))))
1446 (defun bib-toggle-highlight ()
1447 "Toggle the enabling of bib-cite entries as clickable things."
1448 ;; FIXME: do something about after-change stuff?
1450 (if (setq bib-highlight-mouse-t (not bib-highlight-mouse-t))
1451 (bib-highlight-mouse)
1452 (let ((modified (buffer-modified-p))
1453 (inhibit-read-only t))
1455 ((string-match "XEmacs\\|Lucid" emacs-version)
1457 (delete-extent (car bib-ext-list))
1458 (setq bib-ext-list (cdr bib-ext-list))))
1460 (let ((before-change-functions) (after-change-functions))
1461 (remove-text-properties (point-min) (point-max)
1462 '(mouse-face local-map)))))
1463 (set-buffer-modified-p modified))))
1465 (defun bib-label-help-echo (object)
1466 (if bib-label-help-echo-format
1467 (bib-label-help object bib-label-help-echo-format)))
1469 ;;; Balloon-help callback. Anders Stenman <stenman@isy.liu.se>
1470 ;;; Patched by Bruce Ravel <bruce.ravel@nist.gov>
1471 (defun bib-label-help (object &optional format)
1472 (or format (setq format "Use mouse button 2 to find the %s.
1473 Use mouse button 3 to display the %s."))
1475 (let* ((string (extent-string object))
1476 (type (cond ((string-match "^\\\\[A-Za-z]*cite[A-Za-z]*" string) "citation")
1478 (concat "^" bib-ref-regexp) string) "\\label{}")
1479 ((string-match "^\\\\label" string) "\\ref{}")
1480 (t "this (unknown) reference"))))
1481 (format format type type))))
1483 ;;----------------------------------------------------------------------------
1484 ;; Routines to display or edit a citation's bibliography
1486 (defun bib-display-citation ()
1487 "Do the displaying of cite info. Return t if found cite key, nil otherwise.
1488 Example with cursor located over cite command or arguments:
1489 \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
1490 ^Display-all-citations ^Display-this-citation"
1492 (let* ((the-keys-obarray (bib-get-citekeys-obarray)) ;1st in case of error
1493 (work-buffer (get-buffer-create "*bibtex-work*"))
1494 (bib-buffer (bib-get-bibliography nil))
1495 (the-warnings (bib-get-citations
1499 bib-substitute-string-in-display))
1503 (set-buffer work-buffer)
1505 (insert the-warnings)
1506 (setq the-warn-point (point))))
1507 (with-output-to-temp-buffer
1509 (set-buffer work-buffer)
1510 (princ (buffer-substring 1 (point-max))))
1511 (bib-cite-fontify-help-as-bibtex)
1513 (bib-cite-fontify-red the-warn-point))
1514 (kill-buffer bib-buffer)
1515 (kill-buffer work-buffer))))
1517 (defun bib-edit-citation ()
1518 "Do the edit of cite info. Return t if found cite key, nil otherwise.
1519 Find and and put edit point in bib file associated with a BibTeX citation
1520 under cursor from \bibliography input file.
1521 In a multi-entry cite command, the cursor should be on the actual cite key
1522 desired (otherwise a random entry will be selected).
1523 e.g.: \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
1524 ^Display-this-citation"
1525 (let ((the-keys-obarray (bib-get-citekeys-obarray)) ;1st in case of error
1526 (bib-buffer (bib-get-bibliography t))
1527 (the-key)(the-file))
1529 (mapatoms ;Do this for each cite-key found...
1531 (setq the-key (symbol-name cite-key)))
1533 (set-buffer bib-buffer)
1534 (goto-char (point-min))
1535 (if (not (re-search-forward
1536 (concat "@[^{(]+[{(][\t ]*" (regexp-quote the-key) "[ ,\n]")
1539 (kill-buffer bib-buffer)
1540 (error "Sorry, could not find bib entry for %s" the-key))
1541 (re-search-backward "%%%Filename: \\([^\n]*\\)" nil t)
1542 (setq the-file (match-string 1))
1543 (kill-buffer bib-buffer)))
1544 ;;; (find-file the-file)
1545 (funcall bib-switch-to-buffer-function (find-file-noselect the-file))
1546 (goto-char (point-min)) ;V2.19 fix
1547 (re-search-forward (concat "@[^{(]+[{(][\t ]*"
1548 (regexp-quote the-key)
1551 ;;--------------------------------------------------------------------------
1552 ;; Function for bib-apropos
1554 (defun bib-apropos-keyword-at-point ()
1555 "Return the keyword under point for initial input to bib-apropos prompt."
1557 (let ((here (point)))
1559 ((and (re-search-backward "[\n{, ]" nil t)
1560 (string-equal "{" (buffer-substring (match-beginning 0)
1562 (if (fboundp 'buffer-substring-no-properties)
1563 (buffer-substring-no-properties (1+ (point)) here)
1564 (buffer-substring (1+ (point)) here)))))))
1566 ;;--------------------------------------------------------------------------
1567 ;; Functions for Displaying or moving to matching \ref or \label command
1569 (defun bib-display-label ()
1570 "Display environment or first ref associated with a label.
1571 The label or ref name is extracted from the text under the cursor, or the
1572 user is prompted is nothing suitable is found. The first prompt is for a
1573 label. If you answer with an empty string, a second prompt for a ref will
1575 (let ((the-regexp (bib-guess-or-prompt-for-label)))
1576 (if (not the-regexp)
1577 (message "No name given")
1578 (bib-display-or-find-label the-regexp t))))
1580 (defun bib-find-label ()
1581 "Move to a label, or the first occurance of a ref.
1582 The label or ref name is extracted from the text under the cursor.
1583 If nothing suitable is found, the user is prompted. The first prompt is for a
1584 label. If you answer with an empty string, a second prompt for a ref will be
1587 If within a single file document:
1588 You can move back with C-xC-x as the mark is set before moving.
1589 You can search for next occurrances of a ref command with C-sC-s.
1591 If within a multi-file document (in auctex only)
1592 You can move back with C-xC-x if within the same buffer. If not, just
1593 select your previous buffer.
1594 You can search for next occurrances of a ref command with tag commands:
1595 C-u M-. Find next alternate definition of last tag specified.
1596 C-u - M-. Go back to previous tag found."
1597 (let ((the-regexp (bib-guess-or-prompt-for-label)))
1598 (if (not the-regexp)
1599 (message "No name given")
1600 (bib-display-or-find-label the-regexp nil))))
1602 ;;--------------------------------------------------------------------------
1603 ;; Functions for Displaying or moving to matching \ref or \label command
1605 (defun bib-display-or-find-label (the-regexp displayf)
1606 ;; work horse for bib-find-label and bib-display-label
1607 (let* ((masterfile (bib-master-file))
1608 (masterdir (and masterfile
1609 (file-name-directory masterfile)))
1610 (new-point)(new-buffer))
1612 ;; Now we are either in a simple file, or with a multi-file document
1614 (masterfile ;Multi-file document
1616 (displayf ;Display only
1617 (set-buffer (bib-etags-find-noselect the-regexp masterdir))
1618 (re-search-forward the-regexp nil t)
1619 ;; ...because tags puts point on beginning of line
1620 (if (string-match "^\\\\\\\\label" the-regexp)
1621 (bib-display-this-environment) ;display the label's environment
1622 (bib-display-this-ref))) ; display the ref's context
1624 (setq new-buffer (bib-etags-find-noselect the-regexp masterdir))
1627 (substitute-command-keys
1628 (concat "Use \\[bib-find-next] to find the next occurrence "
1629 "and C-u \\[bib-find-next] to find previous."))))
1630 (if (equal new-buffer (current-buffer))
1631 (setq new-point (point)))))) ;Moving with the same buffer
1632 (t ;Single-file document
1633 (goto-char (point-min))
1635 ((re-search-forward the-regexp nil t)
1637 (if (string-match "^\\\\label" the-regexp)
1638 (bib-display-this-environment) ;Display the environment
1639 (bib-display-this-ref)) ; display the ref's context
1640 (setq new-point (match-beginning 0)) ;or move there
1641 (if (string-match "{\\(.*\\)}" the-regexp)
1642 (setq bib-cite-search-ring (match-string 1 the-regexp)))
1645 (substitute-command-keys
1646 (concat "Use \\[bib-find-next] to find the next occurrence "
1647 "and C-u \\[bib-find-next] to find previous."))))))
1649 (message "Sorry, cannot find it (%s)" the-regexp))))))
1652 (push-mark (point) t nil) ;We've moving there... push mark
1653 (goto-char new-point))
1654 (if new-buffer ;We've changing buffer
1655 ;;(switch-to-buffer new-buffer)
1656 (funcall bib-switch-to-buffer-function new-buffer)))
1662 (defvar bib-label-prompt-map nil)
1663 (if bib-label-prompt-map
1665 (setq bib-label-prompt-map (copy-keymap minibuffer-local-completion-map))
1666 (define-key bib-label-prompt-map " " 'self-insert-command))
1668 (defun bib-guess-or-prompt-for-label ()
1669 "Guess from context, or prompt the user for a label command."
1671 (if (not (looking-at "\\\\")) ;If not on beginning of a command
1672 (re-search-backward "[\\]"
1673 (save-excursion (beginning-of-line)(point))
1676 ((looking-at bib-ref-regexpc) ;On \ref, looking for matching \label
1677 (let ((b (progn (search-forward "{" nil t)(forward-char -1)(point)))
1678 (e (progn (forward-sexp 1)(point))))
1679 (concat "\\\\label" (regexp-quote (buffer-substring b e)))))
1680 ((looking-at "\\\\label{") ;On \label, looking for matching \ref
1681 (let ((b (progn (search-forward "{" nil t)(forward-char -1)(point)))
1682 (e (progn (forward-sexp 1)(point))))
1683 (concat bib-ref-regexp (regexp-quote (buffer-substring b e)))))
1685 (let* ((minibuffer-local-completion-map bib-label-prompt-map)
1686 (the-alist (create-alist-from-list
1687 (cdr (reverse LaTeX-label-list))))
1688 ;;; LaTeX-label-list example:
1689 ;;; '(("label3" "label4")("label1" "label2") nil)
1690 ;; so let's get rid of that nil part in embedded list.
1692 (if (string-equal "18" (substring emacs-version 0 2))
1693 (completing-read "Label: " the-alist nil nil nil)
1694 (completing-read "Label: " the-alist nil nil nil
1695 'LaTeX-find-label-hist-alist))))
1696 (if (not (equal the-name ""))
1697 (concat "\\\\label{" (regexp-quote the-name) "}")
1698 ;; else try to get a \ref
1699 (if (string-equal "18" (substring emacs-version 0 2))
1700 (setq the-name (completing-read "Ref: " the-alist nil nil nil))
1701 (setq the-name (completing-read "Ref: " the-alist nil nil nil
1702 'LaTeX-find-label-hist-alist)))
1703 (if (not (equal the-name ""))
1704 (concat bib-ref-regexpc (regexp-quote the-name) "}")
1707 (defun bib-display-this-ref ()
1708 "Display a few lines around current point."
1711 (with-output-to-temp-buffer "*BiBTemp*"
1717 (re-search-backward "[\n\^M]" nil t)
1723 (re-search-forward "[\n\^M]" nil t)
1726 (set-buffer "*BiBTemp*")
1727 (while (search-forward "\^M" nil t)
1728 (replace-match "\n" nil t))
1730 (if (looking-at "\n") ;Remove first empty line...
1732 (with-output-to-temp-buffer "*Help*"
1733 (princ (buffer-substring 1 (point-max))))
1734 (bib-cite-fontify-help-as-latex)
1735 (kill-buffer "*BiBTemp*"))
1737 (with-output-to-temp-buffer ; display the ref's context
1740 (buffer-substring (save-excursion (forward-line -2)(point))
1741 (save-excursion (forward-line 3)(point)))))
1742 (bib-cite-fontify-help-as-latex))))
1744 (defun bib-display-this-environment ()
1745 "Display the environment associated with a label, or its section name.
1746 Assumes point is already on the label.
1747 Does not save excursion."
1748 ;; Bugs: The method used here to detect the environment is *not* foolproof.
1749 ;; It will get confused, for example, between two figure environments,
1750 ;; picking out both instead of the section label above them. But since
1751 ;; users typically puts their labels next to the section declaration,
1752 ;; I'm satisfied with this... for now.
1753 ;; I could have used the following AUCTeX functions:
1754 ;; LaTeX-current-environment
1755 ;; Function: Return the name (a string) of the enclosing LaTeX environment.
1756 ;; LaTeX-current-section
1757 ;; Function: Return the level of the section that contain point.
1758 ;; but then this code would only work as part of AUCTeX...
1759 (let ((the-point (point))
1761 (the-environment)(foundf))
1762 (while (and (not foundf)
1763 (goto-char end-point) ;Past end of last search
1764 (re-search-forward "\\(^\\|\^M\\)[ \t]*\\\\end{\\([^}]*\\)}"
1766 (setq end-point (point))
1767 (setq the-environment (match-string 2))
1768 (and (not (string-match "document" the-environment))
1769 (re-search-backward (concat "\\(^\\|\^M\\)[ \t]*\\\\begin{"
1770 (regexp-quote the-environment) "}"))
1771 (<= (point) the-point)
1773 (if foundf ;A good environment
1775 (cond ((bib-Is-hidden) ;Better way is: replace-within-string
1776 (with-output-to-temp-buffer "*BiBTemp*"
1777 (princ (buffer-substring (point) end-point)))
1778 (set-buffer "*BiBTemp*")
1779 (while (search-forward "\^M" nil t)
1780 (replace-match "\n" nil t))
1782 (if (looking-at "\n") ;Remove first empty line...
1784 (with-output-to-temp-buffer "*Help*"
1785 (princ (buffer-substring 1 (point-max))))
1786 (kill-buffer "*BiBTemp*"))
1788 (with-output-to-temp-buffer "*Help*"
1789 (princ (buffer-substring (point) end-point)))))
1790 (bib-cite-fontify-help-as-latex))
1791 ;; Just find the section declaration
1792 (goto-char the-point)
1793 (if (re-search-backward
1794 ;;; "\\(^\\|\^M\\)[ \t]*\\\\\\(sub\\)*section{\\([^}]*\\)}" nil t)
1795 ;;; Michael Steiner <steiner@cs.uni-sb.de> patch
1796 "\\(^\\|\^M\\)[ \t]*\\\\\\(\\(sub\\)*section\\|chapter\\|part\\)\\*?\
1799 (message (match-string 0))
1801 "Sorry, could not find an environment or section declaration")))))
1803 (defvar LaTeX-find-label-hist-alist nil "History list for LaTeX-find-label.")
1804 (defvar LaTeX-label-list nil "Used by AUCTeX to store label names.")
1807 (defun create-alist-from-list (the-list)
1808 "Return a single list from a THE-LIST that may contain either items or lists.
1810 '((\"label3\" \"label4\")(\"label1\" \"label2\") \"label\")
1812 '((\"label3\") (\"label4\") (\"label1\") (\"label2\") (\"label\"))"
1813 (mapcar 'list (bib-cite-mh-list-to-string the-list)))
1816 ;;; Following two functions from mh-utils.el (part of GNU emacs)
1817 ;;; I have changed the names in case these functions change what they do.
1820 (defun bib-cite-mh-list-to-string (l)
1821 "Flattens the list L and make every element of the new list into a string."
1822 (nreverse (bib-cite-mh-list-to-string-1 l)))
1824 (defun bib-cite-mh-list-to-string-1 (l)
1825 (let ((new-list nil))
1827 (cond ((null (car l)))
1829 (setq new-list (cons (symbol-name (car l)) new-list)))
1831 (setq new-list (cons (int-to-string (car l)) new-list)))
1832 ((equal (car l) ""))
1833 ((stringp (car l)) (setq new-list (cons (car l) new-list)))
1835 (setq new-list (nconc (bib-cite-mh-list-to-string-1 (car l))
1837 (t (error "Bad element in mh-list-to-string: %s" (car l))))
1841 ;; -------------------------------------------------------------------------
1842 ;; Routines to extract cite keys from text
1844 ;; ... is truly remarkable, as shown in \citeN{Thomson77,Test56}. Every
1845 ;; \cite[{\it e.g.}]{Thomson77,Test56}
1847 (defun bib-get-citations (keys-obarray bib-buffer new-buffer substitute)
1848 "Put citations of KEYS-OBARRAY from BIB-BUFFER into NEW-BUFFER.
1849 Substitute strings if SUBSTITUTE is t
1850 Return the-warnings as text."
1851 (let ((the-warnings) ;The only variable to remember...
1852 (case-fold-search t)) ;All other results go into new-buffer
1853 ;; bibtex is not case-sensitive for keys.
1856 (set-buffer bib-buffer)
1857 (mapatoms ;Do this for each cite-key found...
1859 (goto-char (point-min))
1860 (if (re-search-forward
1861 (concat "@[^{(]+[{(][\t ]*"
1862 (regexp-quote (symbol-name cite-key))
1864 ;; ^^ ^ comma, space or end-of-line
1866 (setq the-text (concat the-text
1868 (progn (beginning-of-line)(point))
1869 (progn (forward-sexp 2)(point)))
1871 (setq the-warnings (concat the-warnings
1872 "Cannot find entry for: "
1873 (symbol-name cite-key) "\n"))))
1876 (error "Sorry, could not find any of the references"))
1877 ;; Insert the citations in the new buffer
1878 (set-buffer new-buffer)
1882 ;; We are at beginning of new-buffer.
1883 ;; Now handle crossrefs
1884 (let ((crossref-obarray (make-vector 201 0)))
1885 (while (re-search-forward
1886 "[, \t]*crossref[ \t]*=[ \t]*\\(\"\\|\{\\)" nil t)
1887 ;;handle {text} or "text" cases
1888 (if (string-equal "{" (match-string 1))
1889 (re-search-forward "[^\}]+" nil t)
1890 (re-search-forward "[^\"]+" nil t))
1891 (intern (match-string 0) crossref-obarray))
1892 ;; Now find the corresponding keys,
1893 ;; but add them only if not already in `keys-obarray'
1894 (set-buffer bib-buffer)
1897 (mapatoms ;Do this for each crossref key found...
1898 (lambda (crossref-key)
1899 (if (not (intern-soft (symbol-name crossref-key) keys-obarray))
1901 ;; Not in keys-obarray, so not yet displayed.
1902 (goto-char (point-min))
1903 (if (re-search-forward
1904 (concat "@[^{(]+[{(][\t ]*"
1905 (regexp-quote (symbol-name crossref-key))
1911 (progn (beginning-of-line)(point))
1912 (progn (forward-sexp 2)(point)))
1915 (concat the-warnings
1916 "Cannot find crossref entry for: "
1917 (symbol-name crossref-key) "\n"))))))
1919 ;; Insert the citations in the new buffer
1920 (set-buffer new-buffer)
1921 (goto-char (point-max))
1926 ;; Now we have all citations in new-buffer, collect all used @String keys
1927 ;; Ex: journal = JPO,
1928 (let ((strings-obarray (make-vector 201 0)))
1929 (while (re-search-forward bib-string-regexp nil t)
1930 (intern (match-string 1) strings-obarray))
1931 ;; Now find the corresponding @String commands
1932 ;; Collect either the @string commands, or the string to substitute
1933 (set-buffer bib-buffer)
1935 (let ((string-alist)
1937 (mapatoms ;Do this for each string-key found...
1938 (lambda (string-key)
1939 (goto-char (point-min))
1940 ;; search for @string{ key = {text}} or @string{ key = "text"}
1941 (if (re-search-forward
1942 (concat "^[ \t]*@string[{(]"
1943 (regexp-quote (symbol-name string-key))
1944 "[\t ]*=[\t ]*\\(\"\\|\{\\)")
1946 (let ((the-string-start (1- (match-end 1))) ;catch bracket
1947 ;;handle {text} or "text" cases
1950 ((string-equal "\"" (match-string 1))
1951 (re-search-forward "[^\\]\"" nil t)
1957 (if substitute ;Collect substitutions
1962 (cons (symbol-name string-key)
1964 (buffer-substring the-string-start
1965 the-string-end)))));)
1966 ;;Collect the strings command themseves
1970 (progn (forward-char 1)(point))
1971 (re-search-backward "^[ \t]*@string[{(]"
1974 ;; @string entry not found
1975 (if (not (member-cis (symbol-name string-key)
1976 bib-string-ignored-warning))
1978 (concat the-warnings
1979 "Cannot find @String entry for: "
1980 (symbol-name string-key) "\n")))))
1982 ;; Now we have `the-text' of @string commands,
1983 ;; or the `string-alist' to substitute.
1984 (set-buffer new-buffer)
1988 (let* ((the-key (car (car string-alist)))
1989 (the-string (cdr (car string-alist)))
1990 (slashed-string ; "J. of Geo.\" -> "J. of Geo.\\\\"
1991 (dired-replace-in-string
1992 "\\\\" "\\\\" the-string)))
1994 (while (re-search-forward
1995 (concat "\\(^[, \t]*[a-zA-Z]+[ \t]*=[ \t]*\\)"
1996 (regexp-quote the-key)
1999 (replace-match (concat "\\1" slashed-string "\\2") t nil)))
2000 (setq string-alist (cdr string-alist)))
2001 ;; substitute is nil; Simply insert text of @string commands
2004 (insert the-text "\n")))
2008 ;; Return the warnings...
2011 ;;; Following contributed by Michael Steiner <steiner@cs.uni-sb.de> The
2012 ;; @string abbreviation are not case-sensitive, so we replaced the `member'
2013 ;; test above with `member-cis' defined here:
2014 (defun member-cis (ELT LIST)
2015 "Return non-nil if ELT is an element of LIST.
2016 All elements should be strings.
2017 Comparison is case-insensitive."
2018 ;; If list is exhausted,
2020 nil ;; if null then we haven't found the element ...
2021 ;; else split list and ...
2022 (let((listelt (car LIST))(listrest (cdr LIST)))
2023 ;; see if car is equal to ELT
2024 (if (string-equal (downcase ELT) (downcase listelt))
2025 t ;; if so return true
2026 ;; else recurse for rest of list
2027 (member-cis ELT listrest)))))
2029 (defun bib-get-citekeys-obarray ()
2030 "Return obarray of citation key (within curly brackets) under cursor."
2032 ;; First find *only* a key *within a cite command
2033 (let ((the-point (point))
2034 (keys-obarray (make-vector 201 0)))
2035 ;; First try to match a cite command
2036 (if (and (skip-chars-backward "a-zA-Z") ;Stops on \ or {
2037 (looking-at "[a-zA-Z]*cite[a-zA-Z]*"))
2039 ;;skip over any optional arguments to \cite[][]{key} command
2040 (skip-chars-forward "a-zA-Z")
2041 (while (looking-at "\\[")
2043 (re-search-forward "{[ \n]*\\([^,} \n]+\\)" nil t)
2044 (intern (match-string 1) keys-obarray)
2045 (while (and (skip-chars-forward " \n") ;no effect on while
2048 ;;The following re-search skips over leading spaces
2049 (re-search-forward "\\([^,} \n]+\\)" nil t)
2050 (intern (match-string 1) keys-obarray)))
2051 ;; Assume we are on the keyword
2052 (goto-char the-point)
2053 (let ((the-start (re-search-backward "[\n{, ]" nil t))
2054 (the-end (progn (goto-char the-point)
2055 (re-search-forward "[\n}, ]" nil t))))
2056 (if (and the-start the-end)
2057 (intern (buffer-substring (1+ the-start) (1- the-end))
2060 (error "Sorry, can't find a reference here"))))
2063 (defun bib-buffer-citekeys-obarray ()
2064 "Extract citations keys used in the current buffer."
2065 (let ((keys-obarray (make-vector 201 0)))
2067 (goto-char (point-min))
2068 ;; Following must allow for \cite[e.g.][]{key} !!!
2069 ;; regexp for \cite{key1,key2} was "\\\\[a-Z]*cite[a-Z]*{\\([^,}]+\\)"
2070 (while (re-search-forward "\\\\[a-zA-Z]*cite[a-zA-Z]*\\(\\[\\|{\\)"
2073 (while (looking-at "\\[") ; ...so skip all bracketted options
2075 ;; then lookup first key
2076 (if (looking-at "{[ \n]*\\([^,} \n]+\\)")
2078 (intern (match-string 1) keys-obarray)
2079 (goto-char (match-end 1))
2080 (while (and (skip-chars-forward " \n")
2083 (re-search-forward "\\([^,} \n]+\\)" nil t)
2084 (intern (match-string 1) keys-obarray)))))
2087 (error "Sorry, could not find any citation keys in this buffer")))))
2089 ;;---------------------------------------------------------------------------
2090 ;; Multi-file document programming requirements:
2091 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2092 ;; bib-make-bibliography
2093 ;; bib-document-citekeys-obarray needs the master .aux file to extract
2095 ;; Included .aux files (corresponding to \include'd LaTeX files) are
2096 ;; then specified relative to the master-file-directory.
2098 ;; bib-get-bibliography (used by interactive commands to extract bib sources)
2100 ;; bibtex source filenames are returned from (LaTeX-bibliography-list)
2101 ;; unformatted. Since only a single \bibliogragrphy command is allowed
2102 ;; by BiBTeX in a document, it is safe to assume that their path is
2103 ;; relative to the master file's directory (since the path is relative
2104 ;; to where the BiBTeX program is actually ran).
2107 ;; (See TeX-check-files, used in TeX-save-document. All documents related
2108 ;; files are returned by (TeX-style-list) and stored in TeX-active-styles.
2109 ;; Original idea was to search TeX-check-path for files listed in
2110 ;; TeX-active-styles (with possible relative or full paths) that end in .tex.)
2112 (defun bib-master-directory ()
2113 "Return the directory associated with the master file.
2114 If no master file, then return current default."
2115 (let ((masterfile (bib-master-file)))
2117 (file-name-directory (expand-file-name (TeX-master-file)))
2118 default-directory)))
2120 (defun bib-master-file ()
2121 "Return master file full path, or nil if not a multi-file document."
2122 ;; I wish there were a better way to tell about non multi-file documents...
2125 ((not (boundp 'TeX-master))
2126 ;; This buffer doesn't know what a master file is, so return now.
2128 ((and TeX-master ;Set, but not to t
2129 (not (symbolp TeX-master))) ; then we have an actual name
2130 (expand-file-name TeX-master))
2131 ((and (eq TeX-master 't) ;Test if master file itself
2132 (progn ;But also require at least one \include
2134 (goto-char 1) ;Too bad I have to do this search...
2135 ;; Require that user uses \input{file}
2136 ;; rather than \input file
2137 (re-search-forward "^[ \t]*\\\\\\(include\\|input\\){"
2145 ((string-match ".\\(tex\\|ltx\\)$" master)
2147 ((file-readable-p (concat master ".ltx"))
2148 (concat master ".ltx"))
2150 (concat master ".tex")))))
2152 ;; I don't use this one because files are not returned in order...
2153 ;; (defun bib-document-TeX-files ()
2154 ;; ;; Return all tex input files associated with a known multi-file document.
2155 ;; (let ((master-directory (bib-master-directory))
2156 ;; (the-list (cons (file-name-nondirectory (TeX-master-file))
2157 ;; (TeX-style-list)))
2158 ;; ;; TeX-style-list returns "../master" for the main file if TeX-master
2159 ;; ;; was set like that. "../master" would not be found relative
2160 ;; ;; to the master-directory! So let's add it to the list w/o directory.
2164 ;; (setq the-file (expand-file-name (car the-list) master-directory))
2165 ;; (setq the-list (cdr the-list))
2166 ;; (and (not (string-match ".tex$" the-file))
2167 ;; (setq the-file (concat the-file ".tex")))
2168 ;; (and (file-readable-p the-file)
2169 ;; (not (member the-file the-result)) ;listed already?
2170 ;; (setq the-result (cons the-file the-result))))
2173 (defun bib-document-TeX-files ()
2174 "Return all tex input files associated with a *known* multi-file document.
2175 For a multi-file document in auctex only.
2176 No checking is done that this is a real multi-file document.
2177 Sets global variable bib-document-TeX-files-warnings."
2178 (setq bib-document-TeX-files-warnings nil)
2179 (let* ((masterfile (bib-master-file))
2180 (dir (and masterfile (file-name-directory masterfile)))
2181 (tex-buffer (get-buffer-create "*tex-document*"))
2182 (the-list (list masterfile))
2184 (if (not masterfile)
2186 (kill-buffer tex-buffer)
2188 "Sorry, but this is not a multi-file document (Try C-u C-c C-n if using auctex)")))
2190 (set-buffer tex-buffer)
2191 ;; set its directory so relative includes work without expanding
2192 (setq default-directory dir)
2193 (insert-file-contents masterfile)
2194 (goto-char (point-min))
2195 (while (re-search-forward "^[ \t]*\\\\\\(input\\|include\\){\\(.*\\)}"
2197 (let ((the-file (match-string 2)))
2198 (if (string-match ".sty$" the-file) ;Skip over style files!
2200 (if (and (not (file-readable-p (expand-file-name the-file dir)))
2201 (not (string-match ".ltx$" the-file))
2203 (expand-file-name (concat the-file ".ltx") dir)))
2204 (setq the-file (concat the-file ".ltx")))
2205 (if (and (not (file-readable-p (expand-file-name the-file dir)))
2206 (not (string-match ".tex$" the-file)))
2207 (setq the-file (concat the-file ".tex")))
2208 (setq the-file (expand-file-name the-file dir))
2209 (if (not (file-readable-p the-file))
2210 (setq bib-document-TeX-files-warnings
2212 bib-document-TeX-files-warnings
2213 (format "Warning: File not found: %s" the-file)))
2214 (setq the-list (cons (expand-file-name the-file dir) the-list))
2215 (end-of-line)(insert "\n")
2216 (insert-file-contents the-file))))))
2217 (kill-buffer tex-buffer)
2218 (nreverse the-list)))
2220 (defun bib-document-citekeys-obarray ()
2221 "Return cite keys obarray for multi-file document.
2222 Return nil if not a multi-file document.
2223 This is a AUCTeX supported feature only.
2224 Also, see bib-buffer-citekeys-obarray.
2225 Sets global variable bib-document-citekeys-obarray-warnings."
2226 (setq bib-document-citekeys-obarray-warnings nil)
2227 (let ((master-tex (bib-master-file))
2229 (if (not master-tex)
2230 nil ;Not a multifile document. No need...
2231 (setq master-aux (bib-return-aux-file-from-tex master-tex "aux"))
2232 (or (file-readable-p master-aux)
2233 (error "Sorry, cannot read file %s" master-aux))
2234 (and (file-newer-than-file-p master-tex master-aux)
2235 (setq bib-document-citekeys-obarray-warnings
2236 (format "Warning: %s is out of date relative to %s.\n"
2237 master-aux master-tex)))
2238 (let ((work-buffer (get-buffer-create "*bib-cite-work*"))
2239 (keys-obarray (make-vector 201 0)))
2241 (set-buffer work-buffer)
2242 (insert-file-contents master-aux)
2243 ;; Because we will be looking for \input statements, we need to set
2244 ;; the default directory to that of the master file.
2245 (setq default-directory (file-name-directory master-tex))
2246 ;; bib-make-bibliography will need this also to find .bib files
2247 ;; look for \@input{chap1/part1.aux}
2248 (while (re-search-forward "^\\\\@input{\\(.*\\)}$" nil t)
2249 (let* ((auxfile (match-string 1))
2250 (texfile (bib-return-aux-file-from-tex auxfile "tex")))
2251 (if (not (file-readable-p auxfile))
2252 (setq bib-document-citekeys-obarray-warnings
2254 bib-document-citekeys-obarray-warnings
2255 (format "Warning: %s is not found or readable.\n"
2257 (if (file-newer-than-file-p texfile auxfile)
2258 (setq bib-document-citekeys-obarray-warnings
2260 bib-document-citekeys-obarray-warnings
2262 "Warning: %s is out of date relative to %s.\n"
2264 (end-of-line)(insert "\n")
2265 (insert-file-contents auxfile))))
2268 ;;; Patched by calvanes@dis.uniroma1.it (Diego Calvanese)
2269 ;;; ;; look for \citation{gertsenshtein59}
2270 ;;; (while (re-search-forward "^\\\\citation{\\(.*\\)}$" nil t)
2271 ;;; (intern (buffer-substring (match-beginning 1)(match-end 1))
2273 ;; look for \citation{gertsenshtein59,vardi88,...,ullmann90}
2274 ;; comma-separation generated by certain LaTeX styles.
2275 (while (re-search-forward "^\\\\citation{\\(.*\\)}$" nil t)
2276 (let ((string (match-string 1))
2278 (while (string-match "\\([^,\n]+\\)" string start)
2279 (intern (substring string (match-beginning 1) (match-end 1))
2281 (setq start (match-end 0))))))
2282 (kill-buffer work-buffer)
2285 (defun bib-return-aux-file-from-tex (texname ext)
2286 "Given name.name.XXX in TEXNAME return name.name.EXT."
2287 ;; FIXME: Check if in ./, else search
2288 (let* ((filename (if (string-match "\\(.*\\)\\.[^\\.]+" texname)
2289 (concat (match-string 1 texname) "." ext)
2290 (concat texname "." ext)))
2291 (sansdir (file-name-nondirectory filename)))
2292 (if (file-exists-p filename)
2294 ;; Search bib-cite-aux-inputs path
2295 (let ((filename (psg-checkfor-file-list sansdir bib-cite-aux-inputs)))
2296 (if (and filename (file-exists-p filename))
2298 (error "Could not find file %s" sansdir))))))
2300 (defun bib-etags-find-noselect (tag &optional masterdir)
2301 "Returns a buffer with point on TAG.
2302 Buffer is not selected.
2303 Makes sure TAGS file exists, etc."
2305 (let* ((master (or masterdir (bib-master-directory)))
2306 (the-buffer (current-buffer))
2308 (the-tags-file-name (expand-file-name bib-etags-filename master)))
2309 (or (file-exists-p the-tags-file-name) ;make sure TAGS exists
2311 (or (equal the-tags-file-name tags-file-name) ;make sure it's current
2312 (visit-tags-table the-tags-file-name))
2313 ;; find-tag-noselect should set the TAGS file for the new buffer
2314 ;; that's what C-h f visit-tags-table says...
2316 ((string-match "XEmacs\\|Lucid" emacs-version)
2318 (setq new-buffer (current-buffer))
2319 (set-buffer the-buffer))
2321 (setq new-buffer (find-tag-noselect tag nil t))
2322 ; -> Seems to set buffer to TAGS
2323 (set-buffer the-buffer)))
2326 ;; --------------------------------------------------------------------------
2327 ;; The following routines make a temporary bibliography buffer
2328 ;; holding all bibtex files found.
2330 (defun bib-get-bibliography (include-filenames-f)
2331 "Returns a new bibliography buffer holding all bibtex files in the document.
2333 If using AUCTeX, and either TeX-parse-self is set or C-c C-n is used to
2334 parse the document, then the entire multifile document will be searched
2335 for \bibliography commands.
2337 If this fails, the current buffer is searched for the first \bibliography
2340 If include-filenames-f is true, include as a special header the filename
2343 Puts the buffer in text-mode such that forward-sexp works with german \"
2344 accents embeded in bibtex entries."
2345 (let ((bib-list (or (and (fboundp 'LaTeX-bibliography-list)
2346 (boundp 'TeX-auto-update)
2347 (LaTeX-bibliography-list))
2348 ;; LaTeX-bibliography-list (if bound) returns an unformatted list of
2349 ;; bib files used in the document, but only if parsing is turned on
2350 ;; or C-c C-n was used.
2351 (bib-bibliography-list)))
2352 (bib-buffer (get-buffer-create "*bibtex-bibliography*"))
2353 ;; Path is relative to the master directory
2354 (default-directory (bib-master-directory))
2355 (the-name)(the-warnings)(the-file))
2357 ;; such that forward-sexp works with embeeded \" in german,
2358 ;; and unbalanced ()
2359 (set-buffer bib-buffer)
2361 (set-syntax-table text-mode-syntax-table)
2362 ;; (if (boundp 'bibtex-mode-syntax-table)
2363 ;; (set-syntax-table bibtex-mode-syntax-table)
2366 ;;We have a list of bib files
2367 ;;Search for them, include them, list those not readable
2369 (setq the-name (car (car bib-list))) ;Extract the string only
2370 (setq bib-list (cdr bib-list))
2373 (string-match "[^ ]+" the-name) ;remove leading spaces
2374 (string-match "[ ]+$" the-name))) ;remove trailing space
2375 (if (not (string-match "\\.bib$" the-name))
2376 (setq the-name (concat the-name ".bib")))
2378 (or (and (file-readable-p the-name) the-name)
2379 (psg-checkfor-file-list
2380 the-name (psg-list-env bib-bibtex-env-variable))
2381 ;; Check for BIBINPUT env variable as well (by popular demand!)
2382 (psg-checkfor-file-list the-name (psg-list-env "BIBINPUT"))
2383 (and bib-cite-inputs
2384 (psg-checkfor-file-list the-name bib-cite-inputs))
2385 (and (boundp 'TeX-check-path)
2386 (psg-checkfor-file-list the-name TeX-check-path))))
2390 (set-buffer bib-buffer)
2391 (goto-char (point-max))
2392 (if include-filenames-f
2393 (insert "%%%Filename: " the-file "\n"))
2394 (insert-file-contents the-file nil)
2397 (concat the-warnings "Could not read file: " the-name "\n"))))
2400 (with-output-to-temp-buffer "*Help*"
2401 (princ the-warnings))
2402 (kill-buffer bib-buffer)
2404 "Sorry, can't find all bibtex files in \\bibliography command"))
2407 (defun bib-bibliography-list ()
2408 "Return list of bib files listed in first \\bibliography command in buffer.
2409 Similar output to AUCTeX's LaTeX-bibliography-list
2410 The first element may contain trailing whitespace (if there was any in input)
2411 although BiBTeX doesn't allow it!"
2414 (if (not (re-search-forward "^[ \t]*\\\\bibliography{[ \t]*\\([^},]+\\)"
2416 (error "Sorry, can't find \\bibliography command anywhere")
2417 (let ((the-list (list (match-string 1)))
2420 (if (looking-at ",")
2423 (list (buffer-substring
2424 (progn (skip-chars-forward ", ")(point))
2425 (progn (re-search-forward "[,}]" nil t)
2427 (skip-chars-backward ", ")
2430 (mapcar 'list the-list)))))
2432 ;; BibTeX-mode key def to create AUCTeX's parsing file.
2433 (defun bib-create-auto-file ()
2434 "Force the creation of the AUCTeX auto file for a bibtex buffer."
2436 (if (not (require 'latex))
2437 (error "Sorry, This is only useful if you have AUCTeX"))
2438 (let ((TeX-auto-save t)
2440 (TeX-auto-regexp-list BibTeX-auto-regexp-list))
2442 ;; -> calls TeX-auto-store
2443 ;; -> calls TeX-auto-parse
2444 ;; clears LaTeX-auto-bibtem (temporary holding space for bibitems)
2445 ;; searches buffer using regexp in TeX-auto-regexp-list
2446 ;; -> if LaTeX-auto-bibtem (the temporary holding space for bibitems)
2447 ;; holds stuffs like
2448 ;; ("Zimmermann:1991" "Anger_et_al:1993")
2450 ;; (member nil (mapcar 'TeX-auto-entry-clear-p TeX-auto-parser))
2451 ;; then it creates the auto file.
2453 ;; TeX-auto-write may call TeX-master-file which may fail if
2454 ;; TeX-header-end is unset (by LaTeX-common-initialization in latex-mode)
2455 (if (not TeX-header-end)
2456 (setq TeX-header-end LaTeX-header-end))
2460 ;; --------------------------------------------------------------------------
2461 ;; The following routines are also defined in other packages...
2463 (defun psg-checkfor-file-list (filename list)
2464 "Check for presence of FILENAME in directory LIST. Return 1st found path."
2465 ;;USAGE: (psg-checkfor-file-list "gri" (psg-list-env "PATH"))
2466 ;;USAGE: (psg-checkfor-file-list "gri-mode.el" load-path)
2467 ;;USAGE: (psg-checkfor-file-list "gri.cmd" (psg-translate-ff-list "gri.tmp"))
2468 (let ((the-list list)
2471 (if (not (car the-list)) ; it is nil
2472 (setq filespec (expand-file-name filename))
2475 (expand-file-name (file-name-as-directory (car the-list)))
2477 (if (file-exists-p filespec)
2480 (setq the-list (cdr the-list))))
2483 ;; If I have not found a file yet, then check if some directories
2484 ;; ended in // and recurse through them.
2485 (let ((the-list list))
2487 (if (not (string-match "//$" (car the-list))) nil
2489 (search-directory-tree
2490 (substring (car the-list) 0 (match-beginning 0))
2491 (concat "^" filename "$")
2494 (if filespec ;Success!
2495 (setq the-list nil)))
2496 (setq the-list (cdr the-list)))
2500 (defun search-directory-tree (directories extension-regexp recurse first-file)
2501 "Return a list of all reachable files in DIRECTORIES ending with EXTENSION.
2502 DIRECTORIES is a list or a single-directory string
2503 EXTENSION-REGEXP is actually (any) regexp, usually \\\\.bib$
2504 If RECURSE is t, then we will recurse into the directory tree,
2505 nil, we will only search the list given.
2506 If FIRST-FILE is t, stop after first file is found."
2507 (or (listp directories)
2508 (setq directories (list directories)))
2512 (let* ((directory (file-name-as-directory (car directories)))
2513 (content (and directory
2514 (file-readable-p directory)
2515 (file-directory-p directory)
2516 (directory-files directory))))
2517 (setq directories (cdr directories))
2519 (let ((file (expand-file-name (car content) directory)))
2520 (cond ((string-match "[.]+$" (car content))) ;This or parent dir
2521 ((not (file-readable-p file)))
2523 (file-directory-p file))
2525 (cons (file-name-as-directory file) directories)))
2526 ((string-match extension-regexp
2527 (file-name-nondirectory file))
2531 (setq match (cons file match)))))
2532 (setq content (cdr content)))))
2536 ;;; (defun psg-checkfor-file-list (filename list)
2537 ;;; (let ((the-list list)
2540 ;;; (if (not (car the-list)) ; it is nil
2541 ;;; (setq filespec (concat "~/" filename))
2543 ;;; (concat (file-name-as-directory (car the-list)) filename)))
2544 ;;; (if (file-exists-p filespec)
2545 ;;; (setq the-list nil)
2546 ;;; (setq filespec nil)
2547 ;;; (setq the-list (cdr the-list))))
2550 (or (fboundp 'dired-replace-in-string)
2551 ;; This code is part of GNU emacs
2552 (defun dired-replace-in-string (regexp newtext string)
2553 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2554 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2555 (let ((result "") (start 0) mb me)
2556 (while (string-match regexp string start)
2557 (setq mb (match-beginning 0)
2559 result (concat result (substring string start mb) newtext)
2561 (concat result (substring string start)))))
2564 ;; Could use fset here to equal TeX-split-string to dired-split if only
2565 ;; dired-split is defined. That would eliminate a check in psg-list-env.
2566 (and (not (fboundp 'TeX-split-string))
2567 (not (fboundp 'dired-split))
2568 ;; This code is part of AUCTeX
2569 (defun TeX-split-string (char string)
2570 "Returns a list of strings. given REGEXP the STRING is split into
2571 sections which in string was seperated by REGEXP.
2575 (TeX-split-string \"\:\" \"abc:def:ghi\")
2576 -> (\"abc\" \"def\" \"ghi\")
2578 (TeX-split-string \" *\" \"dvips -Plw -p3 -c4 testfile.dvi\")
2580 -> (\"dvips\" \"-Plw\" \"-p3\" \"-c4\" \"testfile.dvi\")
2582 If CHAR is nil, or \"\", an error will occur."
2587 (while (string-match regexp string start)
2588 (let ((match (string-match regexp string start)))
2589 (setq result (cons (substring string start match) result))
2590 (setq start (match-end 0))))
2591 (setq result (cons (substring string start nil) result))
2592 (nreverse result))))
2594 (defun bib-cite-file-directory-p (file)
2595 "Like default `file-directory-p' but allow FILE to end in // for ms-windows."
2597 (if (string-match "\\(.*\\)//$" file)
2598 (file-directory-p (match-string 1 file))
2599 (file-directory-p file))))
2601 (defun psg-list-env (env)
2602 "Return a list of directory elements in ENV variable (w/o leading $)
2603 argument may consist of environment variable plus a trailing directory, e.g.
2604 HOME or HOME/bin (trailing directory not supported in dos or OS/2).
2606 bib-dos-or-os2-variable affects:
2607 path separator used (: or ;)
2608 whether backslashes are converted to slashes"
2609 (if (not (getenv env))
2610 nil ;Because dired-replace-in-string fails
2611 (let* ((value (if bib-dos-or-os2-variable
2612 (dired-replace-in-string "\\\\" "/" (getenv env))
2614 (sep-char (or (and bib-dos-or-os2-variable ";") ":"))
2616 (or (and (fboundp 'TeX-split-string)
2617 (TeX-split-string sep-char value))
2618 (dired-split sep-char value)))))
2619 (loop for x in entries if (bib-cite-file-directory-p x) collect x))))
2622 ;;; bib-cite.el ends here