"Fossies" - the Fresh Open Source Software Archive

Member "auctex-12.3/bib-cite.el" (18 Oct 2020, 101491 Bytes) of package /linux/misc/auctex-12.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lisp source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "bib-cite.el": 12.2_vs_12.3.

    1 ;;; bib-cite.el --- test
    2 ;; bib-cite.el - Display \cite, \ref or \label / Extract refs from BiBTeX file.
    3 
    4 ;; Copyright (C) 1994-1999, 2001, 2003-2005, 2014
    5 ;; Free Software Foundation, Inc.
    6 
    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
   11 
   12 ;;; This file is not part of GNU Emacs.
   13 
   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)
   17 ;; any later version.
   18 
   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.
   23 
   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.
   28 
   29 ;; LCD Archive Entry:
   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|
   33 
   34 ;; ----------------------------------------------------------------------------
   35 ;;; Commentary:
   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.
   38 
   39 ;; New versions of this package (if they exist) may be found at:
   40 ;;   https://people.debian.org/~psg/elisp/bib-cite.el
   41 ;; and in AUCTeX's Git archive at
   42 ;;   https://git.savannah.gnu.org/cgit/auctex.git
   43 
   44 ;; Operating Systems:
   45 ;;  Works in unix, DOS and OS/2.  Developped under Linux.
   46 
   47 ;; AUCTeX users:
   48 ;;  AUCTeX is a super-charged LaTeX mode for emacs. Get it at:
   49 ;;
   50 ;;    https://ftp.gnu.org/pub/gnu/auctex/
   51 ;;
   52 ;;  WWW users may want to check out the AUCTeX page at
   53 ;;    https://www.gnu.org/software/auctex/
   54 ;;
   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.
   58 
   59 ;; RefTeX users:
   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.
   64 ;;
   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.
   71 
   72 ;; MS-DOS users:
   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).
   75 ;;  Get the file
   76 ;;    https://people.debian.org/~psg/elisp/bib-cite.etags-bug-report
   77 ;;  to see what patches to make to etags.c to fix it.
   78 
   79 ;; Description:
   80 ;; ~~~~~~~~~~~
   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...).
   84 ;;
   85 ;; These are the functions:
   86 ;;
   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.
  100 
  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.
  107 ;;
  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
  111 ;;  this).
  112 ;;
  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.
  118 ;;
  119 ;;  The bibtex files can also be searched for entries matching a regular
  120 ;;  expression using bib-apropos.
  121 
  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'
  127 ;;
  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).
  134 ;;
  135 ;;       Example:
  136 ;;
  137 ;;       \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
  138 ;;         ^Cursor -> Display-all-citations    ^Cursor -> Display-this-citation
  139 ;;
  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.
  144 ;;
  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.
  148 ;;
  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'
  152 ;;
  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.
  160 ;;
  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'.
  163 ;;
  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).
  167 ;;
  168 ;;  bib-make-bibliography:     Bound to `\C-c b m'
  169 ;;
  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.
  174 ;;
  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
  179 ;;   example-bib.bib.
  180 ;;
  181 ;;  bib-apropos:               Bound to `\C-c b a'
  182 ;;
  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.
  187 ;;
  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.
  191 ;;
  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.
  197 ;;
  198 ;;  bib-etags:                 Bound to `\C-c b e'
  199 ;;
  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.
  204 ;;
  205 ;;  bib-create-auto-file:
  206 ;;
  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.
  210 ;;
  211 ;;  bib-highlight-mouse:       Bound to `\C-c b h'
  212 ;;
  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
  217 ;;   edited text.
  218 
  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 (https://www.gnu.org/software/auctex/), you
  223 ;;  could use:
  224 ;;
  225 ;;   (autoload 'turn-on-bib-cite "bib-cite")
  226 ;;   (add-hook 'LaTeX-mode-hook 'turn-on-bib-cite)
  227 ;;
  228 ;;  If you are using Emacs' regular LaTeX-mode, use instead:
  229 ;;
  230 ;;   (autoload 'turn-on-bib-cite "bib-cite")
  231 ;;   (add-hook 'latex-mode-hook 'turn-on-bib-cite)
  232 ;;
  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.
  236 ;;  ---
  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:
  240 ;;
  241 ;;    (setq bib-novice nil)
  242 ;;  ---
  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:
  246 ;;
  247 ;;    (setq bib-cite-use-reftex-view-crossref t)
  248 ;;  ---
  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
  255 ;;  command).
  256 ;;
  257 ;;    (setq bib-highlight-mouse-t nil)
  258 ;;  ---
  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'.
  264 ;;  ---
  265 ;;  If you use DOS or OS/2, you may have to set the following variable:
  266 ;;
  267 ;;    (setq bib-dos-or-os2-variable t)
  268 ;;
  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.
  271 ;;  ---
  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:
  277 ;;
  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=")
  281 ;;  ---
  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:
  284 ;;
  285 ;;    (setq bib-etags-filename "TAGSLaTeX")
  286 ;;  ---
  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):
  289 ;;
  290 ;;    (setq bib-bibtex-env-variable "TEXBIB")
  291 ;;
  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
  297 ;;           searched.
  298 ;;
  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')
  302 ;;
  303 ;;  ---
  304 ;;  If you do not wish bib-display to substitute @string abbreviations,
  305 ;;  then set the following variable like so:
  306 ;;
  307 ;;    (setq bib-substitute-string-in-display nil)
  308 ;;  ---
  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.
  313 
  314 ;; If you find circumstances in which this package fails, please let me know.
  315 
  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 ;; ----------------------------------------------------------------------------
  325 ;;; Change log:
  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
  346 ;;    BIBINPUTS.
  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.
  362 ;; V3.14 Dec 20 99 -
  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)
  425 ;;  imenu bug fix.
  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.
  447 ;; V2.19 May 13 96
  448 ;;  PSG:
  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.
  462 ;; V2.15 Apr 09 96 -
  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
  547 ;;         , YEAR          = {1991    }
  548 ;;         ...
  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.
  566 ;;        Is that ok?)
  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 ;; ----------------------------------------------------------------------------
  590 ;;; Code:
  591 
  592 (eval-when-compile
  593   (require 'cl-lib))
  594 
  595 ;; Silence the compiler:
  596 (declare-function dired-replace-in-string "ext:dired"
  597           (regexp newtext string))
  598 (declare-function dired-split "ext:dired-aux"
  599           (pat str &optional limit))
  600 (declare-function reftex-view-crossref "ext:reftex-dcr"
  601           (&optional arg auto-how fail-quietly))
  602 
  603 (defgroup bib-cite nil
  604   "bib-cite, LaTeX minor-mode to display \\cite, \\ref and \\label commands."
  605   :group 'tex)
  606 
  607 (defcustom bib-cite-use-reftex-view-crossref nil
  608   "*Non-nil means, RefTeX will be used to find cross references.
  609 When this variable is non-nil, both `bib-find' and `bib-display' will
  610 call a function in RefTeX to find or display the cross reference of a
  611 \\ref or \\cite macro at point."
  612   :group 'bib-cite
  613   :type 'boolean)
  614 
  615 (defcustom bib-novice t
  616   "*Give advice to novice users about what commands to use next."
  617   :group 'bib-cite
  618   :type 'boolean)
  619 
  620 (defcustom bib-switch-to-buffer-function 'switch-to-buffer
  621   "*Function used to select buffers if they differ from the original.
  622 You may use `switch-to-buffer' `switch-to-buffer-other-window' or
  623 `switch-to-buffer-other-frame'."
  624   :group 'bib-cite
  625   :type '(choice (function-item switch-to-buffer)
  626          (function-item switch-to-buffer-other-window)
  627          (function-item switch-to-buffer-other-frame)))
  628 
  629 (defcustom bib-highlight-mouse-t t
  630   "*Call bib-highlight-mouse from `LaTeX-mode-hook' to add green highlight."
  631   :group 'bib-cite
  632   :type 'boolean)
  633 
  634 (defcustom bib-label-help-echo-format "button2 finds %s, button3 displays %s"
  635   "*Format string for info if the mouse is over LaTeX commands.
  636 If nil, do not display info."
  637   :group 'bib-cite
  638   :type '(radio (const :tag "Don't display info" nil)
  639          string))
  640 
  641 (defcustom bib-bibtex-env-variable "BIBINPUTS"
  642   "*Environment variable setting the path where BiBTeX input files are found.
  643 BiBTeX 0.99b manual says this should be TEXBIB.
  644 Another version says it should BSTINPUTS.  I don't know anymore!
  645 
  646 The colon character (:) is the default path separator in unix, but you may
  647 use semi-colon (;) for DOS or OS/2 if you set bib-dos-or-os2-variable to t."
  648   :group 'bib-cite
  649   :type 'string)
  650 
  651 (defcustom bib-cite-inputs nil
  652   "*List of directories to search for .bib files.
  653 This is in addition to those listed in the environment variable specified by
  654 `bib-bibtex-env-variable'."
  655   :group 'bib-cite
  656   :type '(repeat (file :format "%v")))
  657 
  658 (defcustom bib-cite-aux-inputs nil
  659   "*List of directories to search for .aux files.
  660 MiKTeX has the LaTeX option -aux-directory to store .aux files in an alternate
  661 directory.  You may set this variable to let bib-cite find these .aux files."
  662   :group 'bib-cite
  663   :type '(repeat (file :format "%v")))
  664 
  665 (defcustom bib-dos-or-os2-variable (or (equal 'emx system-type)
  666                        (equal 'ms-dos system-type))
  667   ;; Under OS/2 system-type equals emx
  668   ;; Under DOS  system-type equals ms-dos
  669   "*Whether you use DOS or OS/2 for bib-make-bibliography/bib-display.
  670 
  671 It tells bib-make-bibliography and bib-display to translate
  672 the BIBINPUTS environment variable using the \";\" character as
  673 a path separator and to translate DOS' backslash to slash.
  674 
  675 e.g. Use a path like \"c:\\emtex\\bibinput;c:\\latex\\bibinput\"
  676 
  677 \(You can change the environment variable which is searched by setting the  elisp variable bib-bibtex-env-variable)"
  678   :group 'bib-cite
  679   :type 'boolean)
  680 
  681 (defcustom bib-etags-command "etags -r '/.*\\\\\\(eq\\|page\\|[fvF]\\)ref.*/' -o "
  682   "*Variable for the etags command and its output option.
  683 In unix, this is usually \"etags -r '/.*\\\(eq\|page\|[fvF]\)ref.*/' -o \"
  684 \(we use the -r option to tell etags to list AMS-LaTeX's \\eqref command.)
  685 In DOS and OS/2, this *may* be different, e.g. using slashes like \"etags /o=\"
  686 If so, set it this variable."
  687   :group 'bib-cite
  688   :type 'string)
  689 
  690 (defcustom bib-etags-append-command "etags -r '/.*\\\\\\(eq\\|page\\|[fvF]\\)ref.*/' -a -o "
  691   "*Variable for the etags command and its append and output option.
  692 In unix, this is usually \"etags -r '/.*\\\(eq\|page\|[fvF]\)ref.*/' -a -o \"
  693 In DOS and OS/2, this *may* be \"etags /a /o=\"  If so, set it this variable."
  694   :group 'bib-cite
  695   :type 'string)
  696 
  697 (defcustom bib-etags-filename "TAGS"
  698    "*Variable for the filename generated by etags, by defaults this TAGS.
  699 but you may want to change this to something like TAGSLaTeX such that it can
  700 coexist with some other tags file in your master file directory."
  701   :group 'bib-cite
  702   :type 'string)
  703 
  704 (defcustom bib-ref-regexp "\\\\\\(eq\\|page\\|[fvF]\\)?ref"
  705   "*Regular expression for \\ref LaTeX commands that have a matching \\label.
  706 without the curly bracket.
  707 
  708 If you change this variable and you use multi-file documents, make sure you
  709 also edit the variables bib-etags-command and bib-etags-append-command."
  710   :group 'bib-cite
  711   :type 'regexp)
  712 
  713 (defcustom bib-substitute-string-in-display t
  714   "*Determines if bib-display will substitute @string definitions.
  715 If t, then the @string text is substituted.
  716 If nil, the text is not substituted but the @string entry is included."
  717   :group 'bib-cite
  718   :type 'boolean)
  719 
  720 (defcustom bib-string-ignored-warning
  721   '("jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "sept" "oct" "nov"
  722     "dec" "acmcs" "acta" "cacm" "ibmjrd" "ibmjs" "ieeese" "ieeetcad"
  723     "ieeetc" "ipl" "jacm" "jcss" "scp" "sicomp" "tcs" "tocs" "tods" "tog"
  724     "toms" "toois" "toplas" )
  725   "*@string abbreviations for which a warning is not given if not defined.
  726 These are usually month abbreviations (or journals) defined in a style file."
  727   :group 'bib-cite
  728   :type '(repeat (string :tag "Entry")))
  729 
  730 ;;<<<<<<User-Modifiable variables end here.
  731 
  732 (defvar bib-ref-regexpc (concat bib-ref-regexp "{")
  733   "*Regular expression for \\ref LaTeX commands that have a matching \\label.
  734 A opening curly bracket is appended to the regexp.")
  735 
  736 (defvar bib-cite-minor-mode nil)
  737 
  738 (defvar bib-highlight-mouse-keymap (make-sparse-keymap)
  739   "Keymap for mouse bindings in highlighted texts in bicite.")
  740 
  741 (defvar bib-ext-list nil
  742   "Xemacs buffer-local list of bib-cite extents.")
  743 (make-variable-buffer-local 'bib-ext-list)
  744 (put 'bib-ext-list 'permanent-local t)
  745 
  746 (defvar bib-cite-minor-mode-menu nil)
  747 
  748 ;;;###autoload
  749 (defun bib-cite-minor-mode (arg)
  750   "Toggle bib-cite mode.
  751 When bib-cite mode is enabled, citations, labels and refs are highlighted
  752 when the mouse is over them.  Clicking on these highlights with [mouse-2]
  753 runs bib-find, and [mouse-3] runs bib-display."
  754   (interactive "P")
  755   (set (make-local-variable 'bib-cite-minor-mode)
  756        (if arg
  757        (> (prefix-numeric-value arg) 0)
  758      (not bib-cite-minor-mode)))
  759   (cond
  760    (bib-cite-minor-mode                 ;Setup the minor-mode
  761     ;; Christoph Wedler's <wedler@fmi.uni-passau.de> suggestion for xemacs
  762     ;; Added for version 2.19
  763     (if (boundp 'tags-always-exact)
  764     (progn
  765       (set (make-local-variable 'tags-always-exact) nil)))
  766     ;; mouse overlay
  767     (if bib-highlight-mouse-t
  768     (progn
  769       (bib-cite-setup-highlight-mouse-keymap)
  770       (bib-highlight-mouse)
  771       (add-hook 'after-change-functions
  772             'bib-cite-setup-mouse-function nil t))))
  773    (t
  774    ;;;Undo the minor-mode
  775     ;; mouse overlay
  776     (remove-hook 'after-change-functions 'bib-cite-setup-mouse-function t)
  777     (let ((before-change-functions) (after-change-functions))
  778       ;; FIXME This detroys all mouse-faces and local-maps!
  779       ;; FIXME Hope no other package is using them in this buffer!
  780       (remove-text-properties (point-min) (point-max)
  781                   '(mouse-face t local-map t))))))
  782 
  783 ;;This must be eval'ed when the LaTeX mode is in use.
  784 ;; bib-highlight-mouse-keymap is a local variable so each buffer can have it's
  785 ;; own.
  786 (defun bib-cite-setup-highlight-mouse-keymap ()
  787   "Set up the bib-cite text in the current buffer to be clickable."
  788   (set (make-local-variable 'bib-highlight-mouse-keymap)
  789    ;;; First, copy the local keymap so we don't have `disappearing' menus
  790    ;;; when the mouse is moved over a \ref, \label or \cite command.
  791 
  792    ;;; FIXME: Check out (mouse-major-mode-menu) to see how it grabs the local
  793    ;;;        menus to display.  Maybe on `highlighted' commands we could only
  794    ;;;        display the bib-cite stuff (or a subset of it).
  795     (let ((m (copy-keymap (current-local-map))))
  796       ;; emacs 19
  797       (cond
  798        ((commandp 'action-key)
  799         (substitute-key-definition 'action-key 'bib-find m global-map)
  800         (substitute-key-definition 'assist-key 'bib-display m global-map)
  801         (substitute-key-definition 'action-mouse-key-emacs19
  802                        'bib-find-mouse m global-map)
  803         (substitute-key-definition 'assist-mouse-key-emacs19
  804                        'bib-display-mouse m global-map)
  805         (substitute-key-definition 'action-key-depress-emacs19
  806                        nil m global-map)
  807         (substitute-key-definition 'assist-key-depress-emacs19
  808                        nil m global-map))
  809        (t                               ; emacs 19, not hyperbole
  810         (define-key m [down-mouse-3] 'bib-display-mouse)
  811         (define-key m [mouse-2] 'bib-find-mouse)))
  812       m)))
  813 
  814 ;;;###autoload
  815 (defun turn-on-bib-cite ()
  816   "Unconditionally turn on Bib Cite mode."
  817   (bib-cite-minor-mode 1))
  818 
  819 (defun bib-cite-setup-mouse-function (beg end _old-len)
  820   (save-excursion
  821     (save-match-data
  822       (save-restriction
  823     (narrow-to-region
  824      (progn (goto-char beg) (beginning-of-line) (point))
  825      (progn (goto-char end) (forward-line 1) (point)))
  826     (bib-highlight-mouse)))))
  827 
  828 (defvar bib-cite-minor-mode-map
  829   (let ((map (make-sparse-keymap)))
  830     (define-key map "\C-cba" 'bib-apropos)
  831     (define-key map "\C-cbb" 'bib-make-bibliography)
  832     (define-key map "\C-cbd" 'bib-display)
  833     (define-key map "\C-cbe" 'bib-etags)
  834     (define-key map "\C-cbf" 'bib-find)
  835     (define-key map "\C-cbn" 'bib-find-next)
  836     (define-key map "\C-cbh" 'bib-highlight-mouse)
  837     map)
  838   "Bib-cite minor-mode keymap.")
  839 
  840 (easy-menu-define
  841  bib-cite-minor-mode-menu bib-cite-minor-mode-map "Menu keymap for bib-cite."
  842  '("BCite"
  843    ["Make BibTeX bibliography buffer" bib-make-bibliography t]
  844    ["Display citation or matching \\ref or \\label" bib-display t]
  845    ["Find BibTeX citation or matching \\ref or \\label" bib-find t]
  846    ["Search apropos BibTeX files" bib-apropos t]
  847    ["Build TAGS file for multi-file document" bib-etags (bib-master-file)]
  848    ["Refresh \\cite, \\ref and \\label mouse highlight"
  849     bib-highlight-mouse t]))
  850 
  851 ;; Install ourselves:
  852 (or (assq 'bib-cite-minor-mode minor-mode-alist)
  853     (setq minor-mode-alist
  854       (cons '(bib-cite-minor-mode " BCite") minor-mode-alist)))
  855 (or (assq 'bib-cite-minor-mode minor-mode-map-alist)
  856     (setq minor-mode-map-alist
  857       (cons (cons 'bib-cite-minor-mode bib-cite-minor-mode-map)
  858         minor-mode-map-alist)))
  859 
  860 
  861 ;; Following from bibtex.el
  862 (defvar
  863   bib-cite-bibtex-font-lock-keywords
  864   '(("^\\( \\|\t\\)*\\(@[A-Za-z]+\\)[ \t]*[({]\\([][A-Za-z0-9.:;?!`'()/*@_+=|<>-]+\\)?"
  865      (2 font-lock-function-name-face)
  866      (3 font-lock-reference-face nil t))
  867     ;; reference type and reference label
  868     ("^[ \t]*\\(OPT[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*\\)[ \t]*="
  869      1 font-lock-comment-face)
  870     ;; optional field names (treated as comments)
  871     ("^[ \t]*\\([^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*\\)[ \t]*="
  872      1 font-lock-variable-name-face)
  873     ;; field names
  874     "Default expressions to fontify in BibTeX mode."))
  875 
  876 (defvar bib-cite-bibtex-mode-syntax-table
  877   (let ((st (make-syntax-table)))
  878     ;; [alarson:19920214.1004CST] make double quote a string quote
  879     (modify-syntax-entry ?\" "\"" st)
  880     (modify-syntax-entry ?$ "$$  " st)
  881     (modify-syntax-entry ?% "<   " st)
  882     (modify-syntax-entry ?'  "w   " st)
  883     (modify-syntax-entry ?@  "w   " st)
  884     (modify-syntax-entry ?\\ "\\" st)
  885     (modify-syntax-entry ?\f ">   " st)
  886     (modify-syntax-entry ?\n ">   " st)
  887     (modify-syntax-entry ?~ " " st)
  888     st))
  889 ;; Code from bibtex.el ends
  890 
  891 ;; @string starts with a letter and does not contain any of ""#%'(),={}
  892 ;; Here we do not check that the field contains only one string field and
  893 ;; nothing else.
  894 (defvar bib-string-regexp
  895       "^[, \t]*[a-zA-Z]+[ \t]*=[ \t]*\\([a-zA-Z][^#%'(),={}\" \t\n]*\\)"
  896       "Regular expression for field containing a @string.")
  897 
  898 (defun bib-cite--kind ()
  899   (save-excursion
  900     (if (not (looking-at "\\\\"))
  901         (search-backward "\\" nil t))
  902     (if (looking-at bib-ref-regexpc)
  903         'ref
  904       (if (looking-at "\\\\label{")
  905           'label
  906         'cite))))
  907 
  908 (defun bib-display ()
  909   "Display BibTeX citation or matching \\ref or \\label command under point.
  910 
  911 If text under cursor is a \\cite command, then display its BibTeX info from
  912 \\bibliography input file.
  913    Example with cursor located over cite command or arguments:
  914      \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
  915     ^Display-all-citations          ^Display-this-citation
  916 
  917 If text under cursor is a \\ref command, then display environment associated
  918 with its matching \\label command.
  919 
  920 If text under cursor is a \\label command, then display the text around
  921 the first matching \\ref command.
  922 
  923 The user is prompted for a \\label or \\ref is nothing suitable is found under
  924 the cursor.  The first prompt is for a label.  If you answer with an empty
  925 string, a second prompt for a ref will be given.
  926 
  927 A TAGS file is created and used for multi-file documents under auctex."
  928   (interactive)
  929   (let ((kind (bib-cite--kind)))
  930     (cond
  931      ;; reftex doesn't handle label->ref
  932      ((and bib-cite-use-reftex-view-crossref
  933        (memq kind '(ref cite)))
  934       ;;;FIXME: reftex doesn't want point on \ref or \cite part, but on keyword
  935       (require 'reftex)
  936       (reftex-view-crossref nil))
  937      ((eq kind 'cite)
  938       (bib-display-citation))
  939      (t
  940       (bib-display-label)))))
  941 
  942 (defun bib-find ()
  943   "Edit BibTeX citation or find matching \\ref or \\label command under point.
  944 
  945 For multi-entry cite commands, the cursor should be on the actual cite key
  946 desired (otherwise a random entry will be selected).
  947 e.g.: \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
  948             ^Display-this-citation
  949 
  950 If text under cursor is a \\ref command, then point is moved to its matching
  951 \\label command.
  952 
  953 If text under cursor is a \\label command, then point is moved to the first
  954 matching \\ref command.
  955 
  956 The user is prompted for a \\label or \\ref is nothing suitable is found under
  957 the cursor.  The first prompt is for a label.  If you answer with an empty
  958 string, a second prompt for a ref will be given.
  959 
  960 A TAGS file is created and used for multi-file documents under auctex."
  961   (interactive)
  962   (let ((kind (bib-cite--kind)))
  963     (cond
  964      ;; reftex doesn't handle label->ref
  965      ((and bib-cite-use-reftex-view-crossref
  966        (memq kind '(ref cite)))
  967       (require 'reftex)
  968       (reftex-view-crossref t))
  969      ((eq kind 'cite)
  970       (bib-edit-citation))
  971      (t
  972       (bib-find-label)))))
  973 
  974 (defvar bib-cite-search-ring nil
  975   "Bib-cite intenal variable to hold last \\ref or \\eqref find.")
  976 
  977 (defun bib-find-next (&optional prev-p)
  978   "Find next occurrence of a \ref or \eqref.
  979 This is made necessary because we now use a regexp to find tags in multi-file
  980 documents, and the Emacs command `find-tag' doesn't allow to interactively
  981 find the next occurrence of a regexp."
  982   (interactive "P")
  983   (if (bib-master-file)                 ;Multi-file document
  984       ;; FIXME: This check for `xref-find-definitions' should be
  985       ;; removed once AUCTeX requires Emacs >= 25.1
  986       (let ((func (if (fboundp 'xref-find-definitions)
  987               'xref-find-definitions
  988             'find-tag)))
  989     (if prev-p
  990         (funcall func t '- t)
  991       (funcall func t t t)))
  992     (if bib-cite-search-ring
  993     ;;FIXME: Should first make sure I move off initial \ref{}.
  994     (let ((regexp (concat bib-ref-regexpc bib-cite-search-ring "}")))
  995       (if prev-p
  996           (if (not (re-search-backward regexp nil t))
  997           (message "No previous occurrence of reference %s"
  998                bib-cite-search-ring))
  999         (if (not (re-search-forward regexp nil t))
 1000         (message "No next occurrence of reference %s"
 1001              bib-cite-search-ring))))
 1002       (message "Sorry, no previous reference to find.  Use bib-find?"))))
 1003 
 1004 (defun bib-display-mouse (EVENT)
 1005   "Display BibTeX citation or matching \\ref or \\label under mouse EVENT.
 1006 See bib-display."
 1007   (interactive "e")
 1008   (mouse-set-point EVENT)
 1009   (bib-display))
 1010 
 1011 (defun bib-find-mouse (EVENT)
 1012   "Edit BibTeX citation or find matching \\ref or \\label under mouse EVENT.
 1013 See bib-find."
 1014   (interactive "e")
 1015   (mouse-set-point EVENT)
 1016   (bib-find))
 1017 
 1018 (defun bib-apropos ()
 1019   "Display BibTeX entries containing a keyword from bibliography file.
 1020 The files specified in the \\bibliography command are searched unless
 1021 the current buffer is in `bibtex-mode' or is the Help buffer.  In those
 1022 cases, *it* is searched.  This allows you to trim down a search further
 1023 by using bib-apropos sequentially."
 1024   ;;(interactive "sBibTeX apropos: ")
 1025   (interactive)
 1026   (let* ((keylist (and (boundp 'TeX-auto-update) ;Avoid error in FRAMEPOP
 1027                (fboundp 'LaTeX-bibitem-list) ;Use this if using auctex
 1028                (LaTeX-bibitem-list)))
 1029      (keyword (bib-apropos-keyword-at-point))
 1030      (keyword (completing-read "BiBTeX apropos: " keylist nil nil keyword))
 1031      (the-text)(key-point)(start-point)
 1032      (new-buffer-f (and (not (string-match "^bib" mode-name))
 1033                 (not (string-equal "*Help*" (buffer-name)))))
 1034      (bib-buffer (or (and new-buffer-f (bib-get-bibliography nil))
 1035              (current-buffer))))
 1036     (with-current-buffer bib-buffer
 1037       (goto-char (point-min))
 1038       (while (and (re-search-forward "^[ \t]*@" nil t)
 1039           (re-search-forward keyword nil t))
 1040     (setq key-point (point))        ;To make sure this is within entry
 1041     (re-search-backward "^[ \t]*@" nil t)
 1042     (setq start-point (point))
 1043     (forward-list 1)
 1044     (if (< (point) key-point)       ;And this is that test...
 1045         (goto-char key-point)       ;Not within entry, skip it.
 1046       (setq the-text
 1047         (cons (concat (buffer-substring start-point (point)) "\n")
 1048               the-text))))
 1049       (if (not the-text)
 1050       (message "Sorry, no matches found.")
 1051     (with-output-to-temp-buffer "*Help*"
 1052       (mapc #'princ (nreverse the-text)))
 1053     (bib-cite-fontify-help-as-bibtex)
 1054     (if bib-novice
 1055         (message
 1056          (substitute-command-keys
 1057           (concat "Use \\[bib-apropos] again in the *help* buffer"
 1058               " to trim the search")))))
 1059       (if new-buffer-f
 1060       (kill-buffer bib-buffer)))))
 1061 
 1062 (defvar bib-document-citekeys-obarray-warnings nil
 1063   "Bib-cite internal variable.")
 1064 
 1065 (defun bib-make-bibliography ()
 1066   "Extract citations used in the current document from \bibliography{} file(s).
 1067 Put them into a buffer named after the current buffer, with extension .bib.
 1068 
 1069 In an AUCTeX multi-file document, parsing must be on and the citation keys
 1070 are extracted from the .aux files.
 1071 
 1072 In a plain LaTeX buffer (not multi-file), the cite keys are extracted from
 1073 the text itself.  Therefore the text need not have been previously processed
 1074 by LaTeX.
 1075 
 1076 This function is useful when you want to share a LaTeX file, and therefore want
 1077 to create a bibtex file containing only the references used in the document."
 1078   (interactive)
 1079   (let* ((the-keys-obarray (or (bib-document-citekeys-obarray)
 1080                    (bib-buffer-citekeys-obarray)))
 1081                     ;1st in case of error
 1082      (new-buffer
 1083       (create-file-buffer
 1084        (concat (substring (buffer-name) 0
 1085                   (or (string-match "\\." (buffer-name))
 1086                   (length (buffer-name))))
 1087            "-bib.bib")))
 1088      (bib-buffer (bib-get-bibliography nil))
 1089      (the-warnings (bib-get-citations the-keys-obarray
 1090                       bib-buffer
 1091                       new-buffer
 1092                       nil)))
 1093     (kill-buffer bib-buffer)
 1094 ;;; (switch-to-buffer new-buffer)
 1095     (funcall bib-switch-to-buffer-function new-buffer)
 1096     (bibtex-mode)
 1097     (if (or bib-document-citekeys-obarray-warnings
 1098         the-warnings)
 1099     (progn
 1100       (cond
 1101        ((and bib-document-citekeys-obarray-warnings the-warnings)
 1102         (with-output-to-temp-buffer "*Help*"
 1103           (princ bib-document-citekeys-obarray-warnings the-warnings)))
 1104        (bib-document-citekeys-obarray-warnings
 1105         (with-output-to-temp-buffer "*Help*"
 1106           (princ bib-document-citekeys-obarray-warnings)))
 1107        (the-warnings
 1108         (with-output-to-temp-buffer "*Help*" (princ the-warnings))))
 1109       (setq bib-document-citekeys-obarray-warnings nil) ;Reset
 1110       (bib-cite-fontify-red)))
 1111     (if bib-novice
 1112     (message
 1113      (substitute-command-keys
 1114       "Use \\[save-buffer] to save this buffer to a file.")))))
 1115 
 1116 (defun bib-cite-fontify-red (&optional limit)
 1117   "Fontify *Help* buffer in red-bold up to optional LIMIT."
 1118   (if (and window-system                ;Not exactly correct for XEmacs
 1119        (not (facep 'red-bold)))
 1120       (progn
 1121     (copy-face 'bold 'red-bold)
 1122     (set-face-foreground 'red-bold "red")))
 1123   (with-current-buffer "*Help*"
 1124     (let ((before-change-functions) (after-change-functions))
 1125       (put-text-property (point-min)(or limit (point-max))
 1126              'face 'red-bold))))
 1127 
 1128 (defun bib-cite--fontify-help ()
 1129   ;; FIXME: This looks ugly and incorrect.
 1130   (if font-lock-mode
 1131       (font-lock-mode -1)
 1132     (if (fboundp 'font-lock-unset-defaults) (font-lock-unset-defaults))
 1133     (font-lock-unfontify-buffer))
 1134   (if (fboundp 'font-lock-ensure)
 1135       (font-lock-ensure)
 1136     (with-no-warnings (font-lock-fontify-buffer))))
 1137 
 1138 (defun bib-cite-fontify-help-as-bibtex ()
 1139   (cond
 1140    ((not (featurep 'font-lock))
 1141     nil)                                ;No font-lock! Stop here.
 1142    (t
 1143     ;; Emacs
 1144     (with-current-buffer "*Help*"
 1145       (let ((font-lock-defaults
 1146              '(bib-cite-bibtex-font-lock-keywords
 1147                nil t ((?$ . "\"")(?\" . ".")))))
 1148         (bib-cite--fontify-help))))))
 1149 
 1150 (defun bib-cite-fontify-help-as-latex ()
 1151   (cond
 1152    ((not (featurep 'font-lock))
 1153     nil)                                ;No font-lock! Stop here.
 1154    (t
 1155     ;; Emacs
 1156     (with-current-buffer "*Help*"
 1157       ;; Actually, don't want to `permanently' affect *Help* buffer...
 1158       ;;(if (featurep 'font-latex)
 1159       ;; (font-latex-setup)
 1160       ;; Rather I should deal with this in the `let' form:
 1161       ;; (make-local-variable 'font-lock-string-face)
 1162       ;; (setq font-lock-string-face font-latex-math-face
 1163       ;;       font-latex-string-face (default-value 'font-lock-string-face))
 1164       (let ((font-lock-defaults
 1165          (if (featurep 'font-latex)
 1166          '((font-latex-keywords font-latex-keywords-1
 1167                     font-latex-keywords-2)
 1168            nil nil ((?\( . ".") (?\) . ".") (?$ . "\"")) nil
 1169            (font-lock-comment-start-regexp . "%")
 1170            (font-lock-mark-block-function . mark-paragraph))
 1171            '(tex-font-lock-keywords nil nil ((?$ . "\""))))))
 1172     (bib-cite--fontify-help))))))
 1173 
 1174 (defvar bib-document-TeX-files-warnings nil
 1175   "Bib-cite internal variable.")
 1176 
 1177 (defun bib-etags (&optional masterdir)
 1178   "Invoke etags on all tex files of the document in directory MASTERDIR.
 1179 Store the TAGS file in the master-directory.
 1180 Expect errors if you use this outside of auctex or within a plain
 1181 single-file document.  Also makes sure that the TAGS buffer is updated.
 1182 See variables bib-etags-command and bib-etags-filename"
 1183   (interactive)
 1184   (require 'etags)
 1185   (let* ((the-file-list (bib-document-TeX-files))
 1186      (the-file (car the-file-list))
 1187      (dir (or masterdir (bib-master-directory)))
 1188      (the-tags-file (expand-file-name bib-etags-filename dir))
 1189      (the-tags-buffer (get-file-buffer the-tags-file)))
 1190     ;; Create TAGS file with first TeX file (master file)
 1191     (shell-command (concat bib-etags-command the-tags-file " " the-file))
 1192     (setq the-file-list (cdr the-file-list))
 1193     ;; Append to TAGS file for all other TeX files.
 1194     (while the-file-list
 1195       (setq the-file (car the-file-list))
 1196       (shell-command
 1197        (concat bib-etags-append-command the-tags-file " " the-file))
 1198       (setq the-file-list (cdr the-file-list)))
 1199     (if the-tags-buffer                 ;buffer existed; we must refresh it.
 1200     (with-current-buffer the-tags-buffer
 1201       (revert-buffer t t)))
 1202 
 1203     ;; Check value of tags-file-name against the-tags-file
 1204     (or (equal the-tags-file  tags-file-name) ;make sure it's current
 1205     (visit-tags-table the-tags-file))
 1206 
 1207     ;(set (make-local-variable 'tags-file-name) the-tags-file))
 1208     ;; above should not be needed
 1209 
 1210     ;; Weird Bug:
 1211     ;;  (visit-tags-table-buffer) seems to get called twice when called by
 1212     ;;  find-tag on an undefined tag. The second time, it's in the TAGS
 1213     ;;  buffer and returns an error because TAGS buffer does have
 1214     ;;  tags-file-name set.
 1215     ;;  To get around this.  I'm setting this variable in the TAGS buffer.
 1216     ;; (Changed by Anders Stenman)
 1217     (if (get-file-buffer the-tags-file)
 1218     (with-current-buffer (get-file-buffer the-tags-file)
 1219       (set (make-local-variable 'tags-file-name) the-tags-file))))
 1220 
 1221 
 1222   (if bib-document-TeX-files-warnings   ;free variable loose in emacs!
 1223       (progn
 1224     (with-output-to-temp-buffer "*Help*"
 1225       (princ bib-document-TeX-files-warnings))
 1226     (setq bib-document-TeX-files-warnings nil) ;Reset
 1227     (bib-cite-fontify-red))))
 1228 
 1229 (defun bib-Is-hidden ()
 1230   "Return true is current point is hidden."
 1231   (if (not selective-display)
 1232       nil                               ;Not hidden if not using this...
 1233     (save-excursion
 1234       (if (not (re-search-backward "[\n\^M]" nil t))
 1235       nil                           ;Play safe
 1236     (if (string-equal (match-string 0) "\n")
 1237         nil
 1238       t)))))
 1239 
 1240 (defun bib-highlight-mouse ()
 1241   "Make that nice green highlight when the mouse is over LaTeX commands."
 1242   (interactive)
 1243   ;;Comment this out.  User should be able to use bib-highlight-mouse
 1244   ;;to try it out regardless of bib-highlight-mouse-t.
 1245   ;;Check bib-highlight-mouse-t only in automated cases.
 1246   ;;
 1247   ;;  (if (and bib-highlight-mouse-t
 1248   ;;           ;;window-system)        ;Do nothing unless under X
 1249   ;;           )
 1250   ;; *all of code was here*
 1251   ;;      )
 1252   (save-excursion
 1253     (let ((s)(e)(extent)(local-extent-list bib-ext-list)
 1254       (inhibit-read-only t)
 1255       (modified (buffer-modified-p))) ;put-text-property changing this?
 1256       ;; * peta Wed Nov  8 16:27:29 1995 -- better remove the mouse face
 1257       ;;   properties first.
 1258       (setq bib-ext-list nil)       ;Reconstructed below...
 1259       ;; Remove properties for regular emacs
 1260       ;; FIXME This detroys all mouse-faces and local-maps!
 1261       ;; FIXME Hope no other package is using them in this buffer!
 1262       (let ((before-change-functions) (after-change-functions))
 1263     (remove-text-properties (point-min) (point-max)
 1264                 '(mouse-face t local-map t)))
 1265       (goto-char (point-min))
 1266       (while
 1267       (re-search-forward
 1268        (concat
 1269         "\\\\\\(" (substring bib-ref-regexp 2)
 1270         "\\|label\\|[A-Za-z]*cite[A-Za-z]*\\(\\[[^]]*\\]\\)*\\){[^}]*}")
 1271        nil t)
 1272     (setq s (match-beginning 0))
 1273     (setq e (match-end 0))
 1274     (let ((before-change-functions) (after-change-functions)
 1275           ;;(this-overlay (make-overlay s e))
 1276           )
 1277 ;;;  Even using overlays doesn't help here.  If bib-highlight-mouse-keymap
 1278 ;;;  does not include the AucTeX menus, then these disappear when we click
 1279 ;;;  onto a \cite command.  Perhaps using bib-cite as a minor mode will fix
 1280 ;;;  this?  For now, bib-cite must be loaded after these menus are built.
 1281 ;;;  It must therefore be loaded in a mode-hook.
 1282       (put-text-property s e 'local-map bib-highlight-mouse-keymap)
 1283       (put-text-property s e 'mouse-face 'highlight)
 1284       ;;(overlay-put this-overlay 'local-map bib-highlight-mouse-keymap)
 1285       ;;(overlay-put this-overlay 'mouse-face 'highlight)
 1286       ))
 1287       (set-buffer-modified-p modified))))
 1288 
 1289 (defun bib-toggle-highlight ()
 1290   "Toggle the enabling of bib-cite entries as clickable things."
 1291 ;; FIXME: do something about after-change stuff?
 1292   (interactive)
 1293   (if (setq bib-highlight-mouse-t (not bib-highlight-mouse-t))
 1294       (bib-highlight-mouse)
 1295     (let ((modified (buffer-modified-p))
 1296       (inhibit-read-only t))
 1297       (let ((before-change-functions) (after-change-functions))
 1298     (remove-text-properties (point-min) (point-max)
 1299                 '(mouse-face local-map)))
 1300       (set-buffer-modified-p modified))))
 1301 
 1302 (defun bib-label-help-echo (object)
 1303   (if bib-label-help-echo-format
 1304       (bib-label-help object bib-label-help-echo-format)))
 1305 
 1306 ;; Balloon-help callback. Anders Stenman <stenman@isy.liu.se>
 1307 ;;             Patched by Bruce Ravel <bruce.ravel@nist.gov>
 1308 (defun bib-label-help (object &optional format)
 1309   (or format (setq format "Use mouse button 2 to find the %s.
 1310 Use mouse button 3 to display the %s."))
 1311   (save-match-data
 1312     (let* ((string (extent-string object))
 1313        (type (cond ((string-match "^\\\\[A-Za-z]*cite[A-Za-z]*" string) "citation")
 1314                ((string-match
 1315              (concat "^" bib-ref-regexp) string) "\\label{}")
 1316                ((string-match "^\\\\label" string) "\\ref{}")
 1317                (t "this (unknown) reference"))))
 1318       (format format type type))))
 1319 
 1320 ;;----------------------------------------------------------------------------
 1321 ;; Routines to display or edit a citation's bibliography
 1322 
 1323 (defun bib-display-citation ()
 1324   "Do the displaying of cite info.  Return t if found cite key, nil otherwise.
 1325 Example with cursor located over cite command or arguments:
 1326 \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
 1327    ^Display-all-citations          ^Display-this-citation"
 1328   (save-excursion
 1329     (let* ((the-keys-obarray (bib-get-citekeys-obarray)) ;1st in case of error
 1330        (work-buffer (get-buffer-create "*bibtex-work*"))
 1331        (bib-buffer (bib-get-bibliography nil))
 1332        (the-warnings (bib-get-citations
 1333               the-keys-obarray
 1334               bib-buffer
 1335               work-buffer
 1336               bib-substitute-string-in-display))
 1337        (the-warn-point))
 1338       (if the-warnings
 1339       (progn
 1340         (set-buffer work-buffer)
 1341         (goto-char 1)
 1342         (insert the-warnings)
 1343         (setq the-warn-point (point))))
 1344       (with-output-to-temp-buffer
 1345       "*Help*"
 1346     (set-buffer work-buffer)
 1347     (princ (buffer-substring 1 (point-max))))
 1348       (bib-cite-fontify-help-as-bibtex)
 1349       (if the-warn-point
 1350       (bib-cite-fontify-red the-warn-point))
 1351       (kill-buffer bib-buffer)
 1352       (kill-buffer work-buffer))))
 1353 
 1354 (defun bib-edit-citation ()
 1355   "Do the edit of cite info.  Return t if found cite key, nil otherwise.
 1356 Find and and put edit point in bib file associated with a BibTeX citation
 1357 under cursor from \bibliography input file.
 1358 In a multi-entry cite command, the cursor should be on the actual cite key
 1359 desired (otherwise a random entry will be selected).
 1360 e.g.: \cite{Wadhams81,Bourke.et.al87,SchneiderBudeus94}
 1361             ^Display-this-citation"
 1362   (let ((the-keys-obarray (bib-get-citekeys-obarray)) ;1st in case of error
 1363     (bib-buffer (bib-get-bibliography t))
 1364     (the-key)(the-file))
 1365     (save-excursion
 1366       (mapatoms                     ;Do this for each cite-key found...
 1367        (lambda (cite-key)
 1368      (setq the-key (symbol-name cite-key)))
 1369        the-keys-obarray)
 1370       (set-buffer bib-buffer)
 1371       (goto-char (point-min))
 1372       (if (not (re-search-forward
 1373         (concat "@[^{(]+[{(][\t ]*" (regexp-quote the-key) "[ ,\n]")
 1374         nil t))
 1375       (progn
 1376         (kill-buffer bib-buffer)
 1377         (error "Sorry, could not find bib entry for %s" the-key))
 1378     (re-search-backward "%%%Filename: \\([^\n]*\\)" nil t)
 1379     (setq the-file (match-string 1))
 1380     (kill-buffer bib-buffer)))
 1381 ;;; (find-file the-file)
 1382     (funcall bib-switch-to-buffer-function (find-file-noselect the-file))
 1383     (goto-char (point-min))             ;V2.19 fix
 1384     (re-search-forward (concat "@[^{(]+[{(][\t ]*"
 1385                    (regexp-quote the-key)
 1386                    "[ ,\n]") nil t)))
 1387 
 1388 ;;--------------------------------------------------------------------------
 1389 ;; Function for bib-apropos
 1390 
 1391 (defun bib-apropos-keyword-at-point ()
 1392   "Return the keyword under point for initial input to bib-apropos prompt."
 1393   (save-excursion
 1394     (let ((here (point)))
 1395       (cond
 1396        ((and (re-search-backward "[\n{, ]" nil t)
 1397          (string-equal "{" (buffer-substring (match-beginning 0)
 1398                          (match-end 0))))
 1399     (if (fboundp 'buffer-substring-no-properties)
 1400         (buffer-substring-no-properties (1+ (point)) here)
 1401     (buffer-substring (1+ (point)) here)))))))
 1402 
 1403 ;;--------------------------------------------------------------------------
 1404 ;; Functions for Displaying or moving to matching \ref or \label command
 1405 
 1406 (defun bib-display-label ()
 1407 "Display environment or first ref associated with a label.
 1408 The label or ref name is extracted from the text under the cursor, or the
 1409 user is prompted is nothing suitable is found.  The first prompt is for a
 1410 label.  If you answer with an empty string, a second prompt for a ref will
 1411 be given."
 1412   (let ((the-regexp (bib-guess-or-prompt-for-label)))
 1413     (if (not the-regexp)
 1414     (message "No name given")
 1415       (bib-display-or-find-label the-regexp t))))
 1416 
 1417 (defun bib-find-label ()
 1418   "Move to a label, or the first occurance of a ref.
 1419 The label or ref name is extracted from the text under the cursor.
 1420 If nothing suitable is found, the user is prompted. The first prompt is for a
 1421 label. If you answer with an empty string, a second prompt for a ref will be
 1422 given.
 1423 
 1424 If within a single file document:
 1425   You can move back with C-xC-x as the mark is set before moving.
 1426   You can search for next occurrances of a ref command with C-sC-s.
 1427 
 1428 If within a multi-file document (in auctex only)
 1429   You can move back with C-xC-x if within the same buffer.  If not, just
 1430   select your previous buffer.
 1431   You can search for next occurrances of a ref command with tag commands:
 1432      C-u M-.     Find next alternate definition of last tag specified.
 1433      C-u - M-.   Go back to previous tag found."
 1434   (let ((the-regexp (bib-guess-or-prompt-for-label)))
 1435     (if (not the-regexp)
 1436     (message "No name given")
 1437       (bib-display-or-find-label the-regexp nil))))
 1438 
 1439 ;;--------------------------------------------------------------------------
 1440 ;; Functions for Displaying or moving to matching \ref or \label command
 1441 
 1442 (defun bib-display-or-find-label (the-regexp displayf)
 1443 ;; work horse for bib-find-label and bib-display-label
 1444   (let* ((masterfile (bib-master-file))
 1445      (masterdir (and masterfile
 1446              (file-name-directory masterfile)))
 1447      (new-point)(new-buffer))
 1448     (save-excursion
 1449       ;; Now we are either in a simple file, or with a multi-file document
 1450       (cond
 1451        (masterfile                   ;Multi-file document
 1452     (cond
 1453      (displayf                  ;Display only
 1454       (set-buffer (bib-etags-find-noselect the-regexp masterdir))
 1455       (re-search-forward the-regexp nil t)
 1456       ;; ...because tags puts point on beginning of line
 1457       (if (string-match "^\\\\\\\\label" the-regexp)
 1458           (bib-display-this-environment) ;display the label's environment
 1459         (bib-display-this-ref)))    ;     display the ref's context
 1460      (t                         ;Move to it
 1461       (setq new-buffer (bib-etags-find-noselect the-regexp masterdir))
 1462       (if bib-novice
 1463           (message
 1464            (substitute-command-keys
 1465         (concat "Use \\[bib-find-next] to find the next occurrence "
 1466             "and C-u \\[bib-find-next] to find previous."))))
 1467       (if (equal new-buffer (current-buffer))
 1468           (setq new-point (point)))))) ;Moving with the same buffer
 1469        (t                           ;Single-file document
 1470     (goto-char (point-min))
 1471     (cond
 1472      ((re-search-forward the-regexp nil t)
 1473       (if displayf
 1474           (if (string-match "^\\\\label" the-regexp)
 1475           (bib-display-this-environment) ;Display the environment
 1476         (bib-display-this-ref)) ;         display the ref's context
 1477         (setq new-point (match-beginning 0))  ;or move there
 1478         (if (string-match "{\\(.*\\)}" the-regexp)
 1479         (setq bib-cite-search-ring (match-string 1 the-regexp)))
 1480         (if bib-novice
 1481         (message
 1482          (substitute-command-keys
 1483           (concat "Use \\[bib-find-next] to find the next occurrence "
 1484               "and C-u \\[bib-find-next] to find previous."))))))
 1485      (t
 1486       (message "Sorry, cannot find it (%s)" the-regexp))))))
 1487     (if new-point
 1488     (progn
 1489       (push-mark (point) t nil)   ;We've moving there... push mark
 1490       (goto-char new-point))
 1491       (if new-buffer                    ;We've changing buffer
 1492       ;;(switch-to-buffer new-buffer)
 1493       (funcall bib-switch-to-buffer-function new-buffer)))
 1494     (if (bib-Is-hidden)
 1495     (save-excursion
 1496       (beginning-of-line)
 1497       (show-entry)))))
 1498 
 1499 (defvar bib-label-prompt-map
 1500   (let ((map (make-sparse-keymap)))
 1501     (set-keymap-parent map minibuffer-local-completion-map)
 1502     (define-key map " " 'self-insert-command)
 1503     map))
 1504 
 1505 (defun bib-guess-or-prompt-for-label ()
 1506   "Guess from context, or prompt the user for a label command."
 1507   (save-excursion
 1508     (if (not (looking-at "\\\\"))        ;If not on beginning of a command
 1509          (re-search-backward "[\\]"
 1510                  (save-excursion (beginning-of-line)(point))
 1511                  t))
 1512     (cond
 1513      ((looking-at bib-ref-regexpc)   ;On \ref, looking for matching \label
 1514       (let ((b (progn (search-forward "{" nil t)(forward-char -1)(point)))
 1515         (e (progn (forward-sexp 1)(point))))
 1516     (concat "\\\\label" (regexp-quote (buffer-substring b e)))))
 1517      ((looking-at "\\\\label{")         ;On \label, looking for matching \ref
 1518       (let ((b (progn (search-forward "{" nil t)(forward-char -1)(point)))
 1519         (e (progn (forward-sexp 1)(point))))
 1520     (concat  bib-ref-regexp (regexp-quote (buffer-substring b e)))))
 1521      (t                                 ;Prompt the user
 1522       (let* ((minibuffer-local-completion-map bib-label-prompt-map)
 1523          (the-alist (create-alist-from-list
 1524              (cdr (reverse LaTeX-label-list))))
 1525     ;;; LaTeX-label-list example:
 1526     ;;;  '(("label3" "label4")("label1" "label2") nil)
 1527     ;; so let's get rid of that nil part in embedded list.
 1528          (the-name
 1529           (completing-read "Label: " the-alist nil nil nil
 1530                    'LaTeX-find-label-hist-alist)))
 1531     (if (not (equal the-name ""))
 1532         (concat "\\\\label{" (regexp-quote the-name) "}")
 1533       ;; else try to get a \ref
 1534       (setq the-name (completing-read "Ref: " the-alist nil nil nil
 1535                       'LaTeX-find-label-hist-alist))
 1536       (if (not (equal the-name ""))
 1537           (concat bib-ref-regexpc (regexp-quote the-name) "}")
 1538         nil)))))))
 1539 
 1540 (defun bib-display-this-ref ()
 1541   "Display a few lines around current point."
 1542   (cond
 1543    ((bib-Is-hidden)
 1544     (with-output-to-temp-buffer "*BiBTemp*"
 1545       (princ
 1546        (buffer-substring
 1547     (save-excursion
 1548       (let ((i 3))
 1549         (while (and (> i 0)
 1550             (re-search-backward "[\n\^M]" nil t)
 1551             (setq i (1- i)))))
 1552       (point))
 1553     (save-excursion
 1554       (let ((i 3))
 1555         (while (and (> i 0)
 1556             (re-search-forward "[\n\^M]" nil t)
 1557             (setq i (1- i)))))
 1558       (point)))))
 1559     (set-buffer "*BiBTemp*")
 1560     (while (search-forward "\^M" nil t)
 1561       (replace-match "\n" nil t))
 1562     (goto-char 1)
 1563     (if (looking-at "\n")  ;Remove first empty line...
 1564     (delete-char 1))
 1565     (with-output-to-temp-buffer "*Help*"
 1566       (princ (buffer-substring (point-min) (point-max))))
 1567     (bib-cite-fontify-help-as-latex)
 1568     (kill-buffer "*BiBTemp*"))
 1569    (t
 1570     (with-output-to-temp-buffer ;     display the ref's context
 1571     "*Help*"
 1572       (princ
 1573        (buffer-substring (save-excursion (forward-line -2)(point))
 1574              (save-excursion (forward-line 3)(point)))))
 1575     (bib-cite-fontify-help-as-latex))))
 1576 
 1577 (defun bib-display-this-environment ()
 1578   "Display the environment associated with a label, or its section name.
 1579 Assumes point is already on the label.
 1580 Does not save excursion."
 1581 ;; Bugs:  The method used here to detect the environment is *not* foolproof.
 1582 ;;        It will get confused, for example, between two figure environments,
 1583 ;;        picking out both instead of the section label above them.  But since
 1584 ;;        users typically puts their labels next to the section declaration,
 1585 ;;        I'm satisfied with this... for now.
 1586 ;; I could have used the following AUCTeX functions:
 1587 ;;  LaTeX-current-environment
 1588 ;;    Function: Return the name (a string) of the enclosing LaTeX environment.
 1589 ;;  LaTeX-current-section
 1590 ;;    Function: Return the level of the section that contain point.
 1591 ;; but then this code would only work as part of AUCTeX...
 1592   (let ((the-point (point))
 1593     (end-point (point))
 1594     (the-environment)(foundf))
 1595     (while (and (not foundf)
 1596         (goto-char end-point) ;Past end of last search
 1597         (re-search-forward "\\(^\\|\^M\\)[ \t]*\\\\end{\\([^}]*\\)}"
 1598                    nil t))
 1599       (setq end-point (point))
 1600       (setq the-environment (match-string 2))
 1601       (and (not (string-match "document" the-environment))
 1602        (re-search-backward (concat "\\(^\\|\^M\\)[ \t]*\\\\begin{"
 1603                        (regexp-quote the-environment) "}"))
 1604        (<= (point) the-point)
 1605        (setq foundf t)))
 1606     (if foundf                          ;A good environment
 1607     (progn
 1608       (cond ((bib-Is-hidden)        ;Better way is: replace-within-string
 1609          (with-output-to-temp-buffer "*BiBTemp*"
 1610            (princ (buffer-substring (point) end-point)))
 1611          (set-buffer "*BiBTemp*")
 1612          (while (search-forward "\^M" nil t)
 1613            (replace-match "\n" nil t))
 1614          (goto-char 1)
 1615          (if (looking-at "\n")  ;Remove first empty line...
 1616              (delete-char 1))
 1617          (with-output-to-temp-buffer "*Help*"
 1618            (princ (buffer-substring 1 (point-max))))
 1619          (kill-buffer "*BiBTemp*"))
 1620         (t
 1621          (with-output-to-temp-buffer "*Help*"
 1622            (princ (buffer-substring (point) end-point)))))
 1623       (bib-cite-fontify-help-as-latex))
 1624       ;; Just find the section declaration
 1625       (goto-char the-point)
 1626       (if (re-search-backward
 1627 ;;;        "\\(^\\|\^M\\)[ \t]*\\\\\\(sub\\)*section{\\([^}]*\\)}" nil t)
 1628 ;;;  Michael Steiner <steiner@cs.uni-sb.de> patch
 1629        "\\(^\\|\^M\\)[ \t]*\\\\\\(\\(sub\\)*section\\|chapter\\|part\\)\\*?\
 1630 {\\([^}]*\\)}"
 1631        nil t)
 1632       (message (match-string 0))
 1633     (error
 1634      "Sorry, could not find an environment or section declaration")))))
 1635 
 1636 (defvar LaTeX-find-label-hist-alist nil "History list for LaTeX-find-label.")
 1637 (defvar LaTeX-label-list nil "Used by AUCTeX to store label names.")
 1638 
 1639 
 1640 (defun create-alist-from-list (the-list)
 1641   "Return a single list from a THE-LIST that may contain either items or lists.
 1642 e.g. turns
 1643 '((\"label3\" \"label4\")(\"label1\" \"label2\") \"label\")
 1644 into
 1645 '((\"label3\") (\"label4\") (\"label1\") (\"label2\") (\"label\"))"
 1646   (mapcar 'list (bib-cite-mh-list-to-string the-list)))
 1647 
 1648 ;;
 1649 ;; Following two functions from mh-utils.el (part of GNU emacs)
 1650 ;; I have changed the names in case these functions change what they do.
 1651 ;;
 1652 
 1653 (defun bib-cite-mh-list-to-string (l)
 1654   "Flattens the list L and make every element of the new list into a string."
 1655   (nreverse (bib-cite-mh-list-to-string-1 l)))
 1656 
 1657 (defun bib-cite-mh-list-to-string-1 (l)
 1658   (let ((new-list nil))
 1659     (while l
 1660       (cond ((null (car l)))
 1661         ((symbolp (car l))
 1662          (setq new-list (cons (symbol-name (car l)) new-list)))
 1663         ((numberp (car l))
 1664          (setq new-list (cons (int-to-string (car l)) new-list)))
 1665         ((equal (car l) ""))
 1666         ((stringp (car l)) (setq new-list (cons (car l) new-list)))
 1667         ((listp (car l))
 1668          (setq new-list (nconc (bib-cite-mh-list-to-string-1 (car l))
 1669                    new-list)))
 1670         (t (error "Bad element in mh-list-to-string: %s" (car l))))
 1671       (setq l (cdr l)))
 1672     new-list))
 1673 
 1674 ;; -------------------------------------------------------------------------
 1675 ;; Routines to extract cite keys from text
 1676 
 1677 ;;    ... is truly remarkable, as shown in \citeN{Thomson77,Test56}. Every
 1678 ;; \cite[{\it e.g.}]{Thomson77,Test56}
 1679 
 1680 (defun bib-get-citations (keys-obarray bib-buffer new-buffer substitute)
 1681   "Put citations of KEYS-OBARRAY from BIB-BUFFER into NEW-BUFFER.
 1682 Substitute strings if SUBSTITUTE is t
 1683 Return the-warnings as text."
 1684   (let ((the-warnings)                  ;The only variable to remember...
 1685     (case-fold-search t))           ;All other results go into new-buffer
 1686     ;; bibtex is not case-sensitive for keys.
 1687     (save-excursion
 1688       (let ((the-text))
 1689     (set-buffer bib-buffer)
 1690     (mapatoms                         ;Do this for each cite-key found...
 1691      (lambda (cite-key)
 1692        (goto-char (point-min))
 1693        (if (re-search-forward
 1694         (concat "@[^{(]+[{(][\t ]*"
 1695             (regexp-quote (symbol-name cite-key))
 1696             "\\([, ]\\\|$\\)")
 1697         ;;           ^^     ^  comma, space or end-of-line
 1698         nil t)
 1699            (setq the-text (concat the-text
 1700                       (buffer-substring
 1701                        (progn (beginning-of-line)(point))
 1702                        (progn (forward-sexp 2)(point)))
 1703                       "\n\n"))
 1704          (setq the-warnings (concat the-warnings
 1705                     "Cannot find entry for: "
 1706                     (symbol-name cite-key) "\n"))))
 1707      keys-obarray)
 1708     (if (not the-text)
 1709         (error "Sorry, could not find any of the references"))
 1710     ;; Insert the citations in the new buffer
 1711     (set-buffer new-buffer)
 1712     (insert the-text)
 1713     (goto-char 1))
 1714 
 1715       ;; We are at beginning of new-buffer.
 1716       ;; Now handle crossrefs
 1717       (let ((crossref-obarray (make-vector 201 0)))
 1718     (while (re-search-forward
 1719         "[, \t]*crossref[ \t]*=[ \t]*\\(\"\\|\{\\)" nil t)
 1720       ;;handle {text} or "text" cases
 1721       (if (string-equal "{" (match-string 1))
 1722           (re-search-forward "[^\}]+" nil t)
 1723         (re-search-forward "[^\"]+" nil t))
 1724       (intern (match-string 0) crossref-obarray))
 1725     ;; Now find the corresponding keys,
 1726     ;; but add them only if not already in `keys-obarray'
 1727     (set-buffer bib-buffer)
 1728     (goto-char 1)
 1729     (let ((the-text))
 1730       (mapatoms                     ;Do this for each crossref key found...
 1731        (lambda (crossref-key)
 1732          (if (not (intern-soft (symbol-name crossref-key) keys-obarray))
 1733          (progn
 1734            ;; Not in keys-obarray, so not yet displayed.
 1735            (goto-char (point-min))
 1736            (if (re-search-forward
 1737             (concat "@[^{(]+[{(][\t ]*"
 1738                 (regexp-quote (symbol-name crossref-key))
 1739                 "\\(,\\|$\\)")
 1740             nil t)
 1741                (setq the-text
 1742                  (concat the-text
 1743                      (buffer-substring
 1744                       (progn (beginning-of-line)(point))
 1745                       (progn (forward-sexp 2)(point)))
 1746                      "\n\n"))
 1747              (setq the-warnings
 1748                (concat the-warnings
 1749                    "Cannot find crossref entry for: "
 1750                    (symbol-name crossref-key) "\n"))))))
 1751        crossref-obarray)
 1752       ;; Insert the citations in the new buffer
 1753       (set-buffer new-buffer)
 1754       (goto-char (point-max))
 1755       (if the-text
 1756           (insert the-text)))
 1757     (goto-char 1))
 1758 
 1759       ;; Now we have all citations in new-buffer, collect all used @String keys
 1760       ;; Ex:  journal =      JPO,
 1761       (let ((strings-obarray (make-vector 201 0)))
 1762     (while (re-search-forward bib-string-regexp nil t)
 1763       (intern (match-string 1) strings-obarray))
 1764     ;; Now find the corresponding @String commands
 1765     ;; Collect either the @string commands, or the string to substitute
 1766     (set-buffer bib-buffer)
 1767     (goto-char 1)
 1768     (let ((string-alist)
 1769           (the-text))
 1770       (mapatoms                     ;Do this for each string-key found...
 1771        (lambda (string-key)
 1772          (goto-char (point-min))
 1773          ;; search for @string{ key = {text}} or @string{ key = "text"}
 1774          (if (re-search-forward
 1775           (concat "^[ \t]*@string[{(]"
 1776               (regexp-quote (symbol-name string-key))
 1777               "[\t ]*=[\t ]*\\(\"\\|\{\\)")
 1778           nil t)
 1779          (let ((the-string-start (1- (match-end 1))) ;catch bracket
 1780                ;;handle {text} or "text" cases
 1781                (the-string-end
 1782             (cond
 1783              ((string-equal "\"" (match-string 1))
 1784               (re-search-forward "[^\\]\"" nil t)
 1785               (point))
 1786              (t
 1787               (forward-char -1)
 1788               (forward-list 1)
 1789               (point)))))
 1790            (if substitute      ;Collect substitutions
 1791                (setq string-alist
 1792                  (append
 1793                   string-alist
 1794                   (list
 1795                    (cons (symbol-name string-key)
 1796                     ;(regexp-quote
 1797                      (buffer-substring the-string-start
 1798                                the-string-end)))));)
 1799              ;;Collect the strings command themseves
 1800              (setq the-text
 1801                (concat the-text
 1802                    (buffer-substring
 1803                     (progn (forward-char 1)(point))
 1804                     (re-search-backward "^[ \t]*@string[{(]"
 1805                             nil t))
 1806                    "\n"))))
 1807            ;; @string entry not found
 1808            (if (not (member-cis (symbol-name string-key)
 1809                     bib-string-ignored-warning))
 1810            (setq the-warnings
 1811              (concat the-warnings
 1812                  "Cannot find @String entry for: "
 1813                  (symbol-name string-key) "\n")))))
 1814        strings-obarray)
 1815       ;; Now we have `the-text' of @string commands,
 1816       ;; or the `string-alist' to substitute.
 1817       (set-buffer new-buffer)
 1818       (if substitute
 1819           (while string-alist
 1820         (goto-char 1)
 1821         (let* ((the-key (car (car string-alist)))
 1822                (the-string (cdr (car string-alist)))
 1823                (slashed-string  ; "J. of Geo.\" -> "J. of Geo.\\\\"
 1824             (dired-replace-in-string
 1825              "\\\\" "\\\\" the-string)))
 1826 
 1827           (while (re-search-forward
 1828               (concat "\\(^[, \t]*[a-zA-Z]+[ \t]*=[ \t]*\\)"
 1829                   (regexp-quote the-key)
 1830                   "\\([, \t\n]\\)")
 1831               nil t)
 1832             (replace-match (concat "\\1" slashed-string "\\2") t nil)))
 1833         (setq string-alist (cdr string-alist)))
 1834         ;; substitute is nil; Simply insert text of @string commands
 1835         (goto-char 1)
 1836         (if the-text
 1837         (insert the-text "\n")))
 1838       (goto-char 1))))
 1839 
 1840     ;; We are done!
 1841     ;; Return the warnings...
 1842     the-warnings))
 1843 
 1844 ;; Following contributed by Michael Steiner <steiner@cs.uni-sb.de> The
 1845 ;;  @string abbreviation are not case-sensitive, so we replaced the `member'
 1846 ;;  test above with `member-cis' defined here:
 1847 (defun member-cis (ELT LIST)
 1848   "Return non-nil if ELT is an element of LIST.
 1849 All elements should be strings.
 1850 Comparison is case-insensitive."
 1851   ;; If list is exhausted,
 1852   (if (null LIST)
 1853       nil ;; if null then we haven't found the element ...
 1854     ;; else split list and ...
 1855     (let((listelt (car LIST))(listrest (cdr LIST)))
 1856       ;; see if car is equal to ELT
 1857       (if (string-equal (downcase ELT) (downcase listelt))
 1858       t ;; if so return true
 1859     ;; else recurse for rest of list
 1860     (member-cis ELT listrest)))))
 1861 
 1862 (defun bib-get-citekeys-obarray ()
 1863   "Return obarray of citation key (within curly brackets) under cursor."
 1864   (save-excursion
 1865     ;; First find *only* a key *within a cite command
 1866     (let ((the-point (point))
 1867       (keys-obarray (make-vector 201 0)))
 1868       ;; First try to match a cite command
 1869       (if (and (skip-chars-backward "a-zA-Z") ;Stops on \ or {
 1870            (looking-at "[a-zA-Z]*cite[a-zA-Z]*"))
 1871       (progn
 1872         ;;skip over any optional arguments to \cite[][]{key} command
 1873         (skip-chars-forward "a-zA-Z")
 1874         (while (looking-at "\\[")
 1875           (forward-list 1))
 1876         (re-search-forward "{[ \n]*\\([^,} \n]+\\)" nil t)
 1877         (intern (match-string 1) keys-obarray)
 1878         (while (and (skip-chars-forward " \n") ;no effect on while
 1879             (looking-at ","))
 1880           (forward-char 1)
 1881           ;;The following re-search skips over leading spaces
 1882           (re-search-forward "\\([^,} \n]+\\)" nil t)
 1883           (intern (match-string 1) keys-obarray)))
 1884     ;; Assume we are on the keyword
 1885     (goto-char the-point)
 1886     (let ((the-start (re-search-backward "[\n{, ]" nil t))
 1887           (the-end (progn (goto-char the-point)
 1888                   (re-search-forward "[\n}, ]" nil t))))
 1889       (if (and the-start the-end)
 1890           (intern (buffer-substring (1+ the-start) (1- the-end))
 1891               keys-obarray)
 1892         ;; Neither...
 1893         (error "Sorry, can't find a reference here"))))
 1894       keys-obarray)))
 1895 
 1896 (defun bib-buffer-citekeys-obarray ()
 1897   "Extract citations keys used in the current buffer."
 1898   (let ((keys-obarray (make-vector 201 0)))
 1899     (save-excursion
 1900       (goto-char (point-min))
 1901       ;; Following must allow for \cite[e.g.][]{key} !!!
 1902       ;; regexp for \cite{key1,key2} was "\\\\[a-Z]*cite[a-Z]*{\\([^,}]+\\)"
 1903       (while (re-search-forward "\\\\[a-zA-Z]*cite[a-zA-Z]*\\(\\[\\|{\\)"
 1904                 nil t)
 1905     (backward-char 1)
 1906     (while (looking-at "\\[")       ; ...so skip all bracketted options
 1907       (forward-sexp 1))
 1908     ;; then lookup first key
 1909     (if (looking-at "{[ \n]*\\([^,} \n]+\\)")
 1910         (progn
 1911           (intern (match-string 1) keys-obarray)
 1912           (goto-char (match-end 1))
 1913           (while (and (skip-chars-forward " \n")
 1914               (looking-at ","))
 1915         (forward-char 1)
 1916         (re-search-forward "\\([^,} \n]+\\)" nil t)
 1917         (intern (match-string 1) keys-obarray)))))
 1918       (if keys-obarray
 1919       keys-obarray
 1920     (error "Sorry, could not find any citation keys in this buffer")))))
 1921 
 1922 ;;---------------------------------------------------------------------------
 1923 ;; Multi-file document programming requirements:
 1924 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1925 ;; bib-make-bibliography
 1926 ;;    bib-document-citekeys-obarray needs the master .aux file to extract
 1927 ;;   citation keys.
 1928 ;;    Included .aux files (corresponding to \include'd LaTeX files) are
 1929 ;;   then specified relative to the master-file-directory.
 1930 ;;
 1931 ;; bib-get-bibliography (used by interactive commands to extract bib sources)
 1932 ;;
 1933 ;;    bibtex source filenames are returned from (LaTeX-bibliography-list)
 1934 ;;   unformatted.  Since only a single \bibliogragrphy command is allowed
 1935 ;;   by BiBTeX in a document, it is safe to assume that their path is
 1936 ;;   relative to the master file's directory (since the path is relative
 1937 ;;   to where the BiBTeX program is actually ran).
 1938 ;;
 1939 
 1940 ;; (See TeX-check-files, used in TeX-save-document.  All documents related
 1941 ;;  files are returned by (TeX-style-list) and stored in TeX-active-styles.
 1942 ;;  Original idea was to search TeX-check-path for files listed in
 1943 ;;  TeX-active-styles (with possible relative or full paths) that end in .tex.)
 1944 
 1945 (defun bib-master-directory ()
 1946   "Return the directory associated with the master file.
 1947 If no master file, then return current default."
 1948   (let ((masterfile (bib-master-file)))
 1949     (if masterfile
 1950     (file-name-directory (expand-file-name (TeX-master-file)))
 1951       default-directory)))
 1952 
 1953 (defun bib-master-file ()
 1954   "Return master file full path, or nil if not a multi-file document."
 1955 ;; I wish there were a better way to tell about non multi-file documents...
 1956   (let ((master
 1957      (cond
 1958       ((not (boundp 'TeX-master))
 1959        ;; This buffer doesn't know what a master file is, so return now.
 1960        nil)
 1961       ((and TeX-master              ;Set, but not to t
 1962         (not (symbolp TeX-master))) ; then we have an actual name
 1963        (expand-file-name TeX-master))
 1964       ((and (eq TeX-master 't)      ;Test if master file itself
 1965         (progn                  ;But also require at least one \include
 1966           (save-excursion
 1967             (goto-char 1)       ;Too bad I have to do this search...
 1968             ;; Require that user uses \input{file}
 1969             ;; rather than            \input file
 1970             (re-search-forward "^[ \t]*\\\\\\(include\\|input\\){"
 1971                        nil t))))
 1972        (buffer-file-name))
 1973       (t
 1974        nil))))
 1975     (cond
 1976      ((not master)
 1977       nil)
 1978      ((string-match ".\\(tex\\|ltx\\)$" master)
 1979       master)
 1980      ((file-readable-p (concat master ".ltx"))
 1981       (concat master ".ltx"))
 1982      (t
 1983       (concat master ".tex")))))
 1984 
 1985 ;; I don't use this one because files are not returned in order...
 1986 ;; (defun bib-document-TeX-files ()
 1987 ;; ;; Return all tex input files associated with a known multi-file document.
 1988 ;;   (let ((master-directory (bib-master-directory))
 1989 ;;         (the-list (cons (file-name-nondirectory (TeX-master-file))
 1990 ;;                         (TeX-style-list)))
 1991 ;;      ;; TeX-style-list returns "../master" for the main file if TeX-master
 1992 ;;      ;; was set like that.  "../master" would not be found relative
 1993 ;;      ;; to the master-directory!  So let's add it to the list w/o directory.
 1994 ;;         (the-result)
 1995 ;;         (the-file))
 1996 ;;     (while the-list
 1997 ;;       (setq the-file (expand-file-name (car the-list) master-directory))
 1998 ;;       (setq the-list (cdr the-list))
 1999 ;;       (and (not (string-match ".tex$" the-file))
 2000 ;;            (setq the-file (concat the-file ".tex")))
 2001 ;;       (and (file-readable-p the-file)
 2002 ;;            (not (member the-file the-result)) ;listed already?
 2003 ;;            (setq the-result (cons the-file the-result))))
 2004 ;;     the-result))
 2005 
 2006 (defun bib-document-TeX-files ()
 2007   "Return all tex input files associated with a *known* multi-file document.
 2008 For a multi-file document in auctex only.
 2009 No checking is done that this is a real multi-file document.
 2010 Sets global variable bib-document-TeX-files-warnings."
 2011   (setq bib-document-TeX-files-warnings nil)
 2012   (let* ((masterfile (bib-master-file))
 2013      (dir (and masterfile (file-name-directory masterfile)))
 2014      (tex-buffer (get-buffer-create "*tex-document*"))
 2015      (the-list (list masterfile)))
 2016     (if (not masterfile)
 2017     (progn
 2018       (kill-buffer tex-buffer)
 2019       (error
 2020        "Sorry, but this is not a multi-file document (Try C-u C-c C-n if using auctex)")))
 2021     (with-current-buffer tex-buffer
 2022       ;; set its directory so relative includes work without expanding
 2023       (setq default-directory dir)
 2024       (insert-file-contents masterfile)
 2025       (goto-char (point-min))
 2026       (while (re-search-forward "^[ \t]*\\\\\\(input\\|include\\){\\(.*\\)}"
 2027                 nil t)
 2028     (let ((the-file (match-string 2)))
 2029       (if (string-match ".sty$" the-file) ;Skip over style files!
 2030           nil
 2031         (if (and (not (file-readable-p (expand-file-name the-file dir)))
 2032              (not (string-match ".ltx$" the-file))
 2033              (file-readable-p
 2034               (expand-file-name (concat the-file ".ltx") dir)))
 2035         (setq the-file (concat the-file ".ltx")))
 2036         (if (and (not (file-readable-p (expand-file-name the-file dir)))
 2037              (not (string-match ".tex$" the-file)))
 2038         (setq the-file (concat the-file ".tex")))
 2039         (setq the-file (expand-file-name the-file dir))
 2040         (if (not (file-readable-p the-file))
 2041         (setq bib-document-TeX-files-warnings
 2042               (concat
 2043                bib-document-TeX-files-warnings
 2044                (format "Warning: File not found: %s" the-file)))
 2045           (setq the-list (cons (expand-file-name the-file dir) the-list))
 2046           (end-of-line)(insert "\n")
 2047           (insert-file-contents the-file))))))
 2048     (kill-buffer tex-buffer)
 2049     (nreverse the-list)))
 2050 
 2051 (defun bib-document-citekeys-obarray ()
 2052   "Return cite keys obarray for multi-file document.
 2053 Return nil if not a multi-file document.
 2054 This is a AUCTeX supported feature only.
 2055 Also, see bib-buffer-citekeys-obarray.
 2056 Sets global variable bib-document-citekeys-obarray-warnings."
 2057   (setq bib-document-citekeys-obarray-warnings nil)
 2058   (let ((master-tex (bib-master-file))
 2059     (master-aux))
 2060     (if (not master-tex)
 2061     nil                             ;Not a multifile document.  No need...
 2062       (setq master-aux (bib-return-aux-file-from-tex master-tex "aux"))
 2063       (or (file-readable-p master-aux)
 2064       (error "Sorry, cannot read file %s" master-aux))
 2065       (and (file-newer-than-file-p master-tex master-aux)
 2066        (setq bib-document-citekeys-obarray-warnings
 2067          (format "Warning: %s is out of date relative to %s.\n"
 2068              master-aux master-tex)))
 2069       (let ((work-buffer (get-buffer-create "*bib-cite-work*"))
 2070         (keys-obarray (make-vector 201 0)))
 2071     (with-current-buffer work-buffer
 2072       (insert-file-contents master-aux)
 2073       ;; Because we will be looking for \input statements, we need to set
 2074       ;; the default directory to that of the master file.
 2075       (setq default-directory (file-name-directory master-tex))
 2076       ;; bib-make-bibliography will need this also to find .bib files
 2077       ;; look for \@input{chap1/part1.aux}
 2078       (while (re-search-forward "^\\\\@input{\\(.*\\)}$" nil t)
 2079         (let* ((auxfile (match-string 1))
 2080            (texfile (bib-return-aux-file-from-tex auxfile "tex")))
 2081           (if (not (file-readable-p auxfile))
 2082           (setq bib-document-citekeys-obarray-warnings
 2083             (concat
 2084              bib-document-citekeys-obarray-warnings
 2085              (format "Warning: %s is not found or readable.\n"
 2086                  auxfile)))
 2087         (if (file-newer-than-file-p texfile auxfile)
 2088             (setq bib-document-citekeys-obarray-warnings
 2089               (concat
 2090                bib-document-citekeys-obarray-warnings
 2091                (format
 2092                 "Warning: %s is out of date relative to %s.\n"
 2093                 auxfile texfile))))
 2094         (end-of-line)(insert "\n")
 2095         (insert-file-contents auxfile))))
 2096       (goto-char 1)
 2097 
 2098 ;;;  Patched by calvanes@dis.uniroma1.it (Diego Calvanese)
 2099 ;;;      ;; look for \citation{gertsenshtein59}
 2100 ;;;       (while (re-search-forward "^\\\\citation{\\(.*\\)}$" nil t)
 2101 ;;;         (intern (buffer-substring (match-beginning 1)(match-end 1))
 2102 ;;;                 keys-obarray))
 2103       ;; look for \citation{gertsenshtein59,vardi88,...,ullmann90}
 2104       ;; comma-separation generated by certain LaTeX styles.
 2105       (while (re-search-forward "^\\\\citation{\\(.*\\)}$" nil t)
 2106         (let ((string (match-string 1))
 2107           (start 0))
 2108           (while (string-match "\\([^,\n]+\\)" string start)
 2109         (intern (substring string (match-beginning 1) (match-end 1))
 2110             keys-obarray)
 2111         (setq start (match-end 0))))))
 2112     (kill-buffer work-buffer)
 2113     keys-obarray))))
 2114 
 2115 (defun bib-return-aux-file-from-tex (texname ext)
 2116   "Given name.name.XXX in TEXNAME return name.name.EXT."
 2117 ;; FIXME: Check if in ./, else search
 2118   (let* ((filename (if (string-match "\\(.*\\)\\.[^\\.]+" texname)
 2119                (concat (match-string 1 texname) "." ext)
 2120              (concat texname "." ext)))
 2121      (sansdir (file-name-nondirectory filename)))
 2122     (if (file-exists-p filename)
 2123     filename
 2124       ;; Search bib-cite-aux-inputs path
 2125       (let ((filename (psg-checkfor-file-list sansdir bib-cite-aux-inputs)))
 2126     (if (and filename (file-exists-p filename))
 2127         filename
 2128       (error "Could not find file %s" sansdir))))))
 2129 
 2130 (defun bib-etags-find-noselect (tag &optional masterdir)
 2131   "Returns a buffer with point on TAG.
 2132 Buffer is not selected.
 2133 Makes sure TAGS file exists, etc."
 2134   (require 'etags)
 2135   (let* ((master (or masterdir (bib-master-directory)))
 2136      (the-buffer (current-buffer))
 2137      (new-buffer)
 2138      (the-tags-file-name (expand-file-name bib-etags-filename master)))
 2139     (or (file-exists-p the-tags-file-name) ;make sure TAGS exists
 2140     (bib-etags master))
 2141     (or (equal the-tags-file-name tags-file-name) ;make sure it's current
 2142     (visit-tags-table the-tags-file-name))
 2143     ;; find-tag-noselect should set the TAGS file for the new buffer
 2144     ;; that's what C-h f visit-tags-table says...
 2145     (setq new-buffer (find-tag-noselect tag nil t))
 2146                     ; -> Seems to set buffer to TAGS
 2147     (set-buffer the-buffer)
 2148     new-buffer))
 2149 
 2150 ;; --------------------------------------------------------------------------
 2151 ;; The following routines make a temporary bibliography buffer
 2152 ;; holding all bibtex files found.
 2153 
 2154 (defun bib-get-bibliography (include-filenames-f)
 2155   "Returns a new bibliography buffer holding all bibtex files in the document.
 2156 
 2157 If using AUCTeX, and either TeX-parse-self is set or C-c C-n is used to
 2158 parse the document, then the entire multifile document will be searched
 2159 for \bibliography commands.
 2160 
 2161 If this fails, the current buffer is searched for the first \bibliography
 2162 command.
 2163 
 2164 If include-filenames-f is true, include as a special header the filename
 2165 of each bib file.
 2166 
 2167 Puts the buffer in text-mode such that forward-sexp works with german \"
 2168 accents embeded in bibtex entries."
 2169   (let ((bib-list (or (and (fboundp 'LaTeX-bibliography-list)
 2170                (boundp 'TeX-auto-update)
 2171                (LaTeX-bibliography-list))
 2172 ;; LaTeX-bibliography-list (if bound) returns an unformatted list of
 2173 ;; bib files used in the document, but only if parsing is turned on
 2174 ;; or C-c C-n was used.
 2175               (bib-bibliography-list)))
 2176     (bib-buffer (get-buffer-create "*bibtex-bibliography*"))
 2177     ;; Path is relative to the master directory
 2178     (default-directory (bib-master-directory))
 2179     (the-name)(the-warnings)(the-file))
 2180     (with-current-buffer bib-buffer
 2181       ;; such that forward-sexp works with embeeded \" in german,
 2182       ;; and unbalanced ()
 2183       (erase-buffer)
 2184       (set-syntax-table text-mode-syntax-table)
 2185 ;;      (if (boundp 'bibtex-mode-syntax-table)
 2186 ;;          (set-syntax-table bibtex-mode-syntax-table)
 2187 ;;        (text-mode))
 2188       )
 2189     ;;We have a list of bib files
 2190     ;;Search for them, include them, list those not readable
 2191     (while bib-list
 2192       (setq the-name (car (car bib-list))) ;Extract the string only
 2193       (setq bib-list (cdr bib-list))
 2194       (setq the-name
 2195         (substring the-name
 2196                (string-match "[^ ]+" the-name) ;remove leading spaces
 2197                (string-match "[ ]+$" the-name))) ;remove trailing space
 2198       (if (not (string-match "\\.bib$" the-name))
 2199       (setq the-name (concat the-name ".bib")))
 2200       (setq the-file
 2201         (or (and (file-readable-p the-name) the-name)
 2202         (psg-checkfor-file-list
 2203          the-name (psg-list-env bib-bibtex-env-variable))
 2204         ;; Check for BIBINPUT env variable as well (by popular demand!)
 2205         (psg-checkfor-file-list the-name (psg-list-env "BIBINPUT"))
 2206         (and bib-cite-inputs
 2207              (psg-checkfor-file-list the-name bib-cite-inputs))
 2208         (and (boundp 'TeX-check-path)
 2209              (psg-checkfor-file-list the-name TeX-check-path))))
 2210       (if the-file
 2211       (with-current-buffer bib-buffer
 2212         (goto-char (point-max))
 2213         (if include-filenames-f
 2214         (insert "%%%Filename: " the-file "\n"))
 2215         (insert-file-contents the-file nil)
 2216         (goto-char 1))
 2217     (setq the-warnings
 2218           (concat the-warnings "Could not read file: " the-name "\n"))))
 2219     (if the-warnings
 2220     (progn
 2221       (with-output-to-temp-buffer "*Help*"
 2222         (princ the-warnings))
 2223       (kill-buffer bib-buffer)
 2224       (error
 2225        "Sorry, can't find all bibtex files in \\bibliography command"))
 2226       bib-buffer)))
 2227 
 2228 (defun bib-bibliography-list ()
 2229   "Return list of bib files listed in first \\bibliography command in buffer.
 2230 Similar output to AUCTeX's LaTeX-bibliography-list
 2231 The first element may contain trailing whitespace (if there was any in input)
 2232 although BiBTeX doesn't allow it!"
 2233   (save-excursion
 2234     (goto-char 1)
 2235     (if (not (re-search-forward "^[ \t]*\\\\bibliography{[ \t]*\\([^},]+\\)"
 2236                 nil t))
 2237     (error "Sorry, can't find \\bibliography command anywhere")
 2238       (let ((the-list (list (match-string 1)))
 2239         (doNext t))
 2240     (while doNext
 2241       (if (looking-at ",")
 2242           (setq the-list
 2243             (append the-list
 2244                 (list (buffer-substring
 2245                    (progn (skip-chars-forward ", ")(point))
 2246                    (progn (re-search-forward "[,}]" nil t)
 2247                       (backward-char 1)
 2248                       (skip-chars-backward ", ")
 2249                       (point))))))
 2250         (setq doNext nil)))
 2251     (mapcar 'list the-list)))))
 2252 
 2253 (defvar TeX-auto-save)
 2254 (defvar TeX-auto-update)
 2255 (defvar TeX-auto-regexp-list)
 2256 
 2257 ;; BibTeX-mode key def to create AUCTeX's parsing file.
 2258 (defun bib-create-auto-file ()
 2259   "Force the creation of the AUCTeX auto file for a bibtex buffer."
 2260   (interactive)
 2261   (if (not (require 'latex))
 2262       (error "Sorry, This is only useful if you have AUCTeX"))
 2263   (let ((TeX-auto-save t)
 2264         (TeX-auto-update t)
 2265         (TeX-auto-regexp-list BibTeX-auto-regexp-list))
 2266     ;; TeX-auto-write
 2267     ;; -> calls TeX-auto-store
 2268     ;;    -> calls TeX-auto-parse
 2269     ;;       clears LaTeX-auto-bibtem (temporary holding space for bibitems)
 2270     ;;       searches buffer using regexp in TeX-auto-regexp-list
 2271     ;;    -> if LaTeX-auto-bibtem (the temporary holding space for bibitems)
 2272     ;;       holds stuffs like
 2273     ;;         ("Zimmermann:1991" "Anger_et_al:1993")
 2274     ;;       as determined by
 2275     ;;         (member nil (mapcar 'TeX-auto-entry-clear-p TeX-auto-parser))
 2276     ;;       then it creates the auto file.
 2277 
 2278     ;; TeX-auto-write may call TeX-master-file which may fail if
 2279     ;; TeX-header-end is unset (by LaTeX-common-initialization in latex-mode)
 2280     (if (not TeX-header-end)
 2281     (setq TeX-header-end LaTeX-header-end))
 2282 
 2283     (TeX-auto-write)))
 2284 
 2285 ;; --------------------------------------------------------------------------
 2286 ;; The following routines are also defined in other packages...
 2287 
 2288 (defun psg-checkfor-file-list (filename list)
 2289   "Check for presence of FILENAME in directory LIST.  Return 1st found path."
 2290   ;;USAGE: (psg-checkfor-file-list "gri" (psg-list-env "PATH"))
 2291   ;;USAGE: (psg-checkfor-file-list "gri-mode.el" load-path)
 2292   ;;USAGE: (psg-checkfor-file-list "gri.cmd" (psg-translate-ff-list "gri.tmp"))
 2293   (let ((the-list list)
 2294     (filespec))
 2295     (while the-list
 2296       (if (not (car the-list))          ; it is nil
 2297       (setq filespec (expand-file-name filename))
 2298     (setq filespec
 2299           (concat
 2300            (expand-file-name (file-name-as-directory (car the-list)))
 2301            filename)))
 2302       (if (file-exists-p filespec)
 2303         (setq the-list nil)
 2304     (setq filespec nil)
 2305     (setq the-list (cdr the-list))))
 2306     (if filespec
 2307     filespec
 2308       ;; If I have not found a file yet, then check if some directories
 2309       ;; ended in // and recurse through them.
 2310       (let ((the-list list))
 2311     (while the-list
 2312       (if (not (string-match "//$" (car the-list))) nil
 2313         (setq filespec (car
 2314                 (search-directory-tree
 2315                  (substring (car the-list) 0 (match-beginning 0))
 2316                  (concat "^" filename "$")
 2317                  t
 2318                  t)))
 2319         (if filespec                ;Success!
 2320         (setq the-list nil)))
 2321       (setq the-list (cdr the-list)))
 2322     filespec))))
 2323 
 2324 
 2325 (defun search-directory-tree (directories extension-regexp recurse first-file)
 2326   "Return a list of all reachable files in DIRECTORIES ending with EXTENSION.
 2327 DIRECTORIES is a list or a single-directory string
 2328 EXTENSION-REGEXP is actually (any) regexp, usually \\\\.bib$
 2329 If RECURSE is t, then we will recurse into the directory tree,
 2330           nil, we will only search the list given.
 2331 If FIRST-FILE is t, stop after first file is found."
 2332   (or (listp directories)
 2333       (setq directories (list directories)))
 2334 
 2335   (let (match)
 2336     (while directories
 2337       (let* ((directory (file-name-as-directory  (car directories)))
 2338          (content (and directory
 2339                (file-readable-p directory)
 2340                (file-directory-p directory)
 2341                (directory-files directory))))
 2342     (setq directories (cdr directories))
 2343     (while content
 2344       (let ((file (expand-file-name (car content) directory)))
 2345         (cond ((string-match "[.]+$" (car content))) ;This or parent dir
 2346           ((not (file-readable-p file)))
 2347           ((and recurse
 2348             (file-directory-p file))
 2349            (setq directories
 2350              (cons (file-name-as-directory file) directories)))
 2351           ((string-match extension-regexp
 2352                  (file-name-nondirectory file))
 2353            (and first-file
 2354             (setq content nil
 2355                   directories nil))
 2356            (setq match (cons file match)))))
 2357       (setq content (cdr content)))))
 2358 
 2359     match))
 2360 
 2361 ;; (defun psg-checkfor-file-list (filename list)
 2362 ;;   (let ((the-list list)
 2363 ;;         (filespec))
 2364 ;;     (while the-list
 2365 ;;       (if (not (car the-list))          ; it is nil
 2366 ;;           (setq filespec (concat "~/" filename))
 2367 ;;         (setq filespec
 2368 ;;               (concat (file-name-as-directory (car the-list)) filename)))
 2369 ;;       (if (file-exists-p filespec)
 2370 ;;             (setq the-list nil)
 2371 ;;         (setq filespec nil)
 2372 ;;         (setq the-list (cdr the-list))))
 2373 ;;     filespec))
 2374 
 2375 (or (fboundp 'dired-replace-in-string)
 2376     ;; This code is part of GNU emacs
 2377     (defun dired-replace-in-string (regexp newtext string)
 2378       ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
 2379       ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
 2380       (let ((result "") (start 0) mb me)
 2381     (while (string-match regexp string start)
 2382       (setq mb (match-beginning 0)
 2383         me (match-end 0)
 2384         result (concat result (substring string start mb) newtext)
 2385         start me))
 2386     (concat result (substring string start)))))
 2387 
 2388 
 2389 ;; Could use fset here to equal TeX-split-string to dired-split if only
 2390 ;; dired-split is defined.  That would eliminate a check in psg-list-env.
 2391 (and (not (fboundp 'TeX-split-string))
 2392      (not (fboundp 'dired-split))
 2393      ;; This code is part of AUCTeX
 2394      (defun TeX-split-string (char string)
 2395        "Returns a list of strings. given REGEXP the STRING is split into
 2396 sections which in string was seperated by REGEXP.
 2397 
 2398 Examples:
 2399 
 2400       (TeX-split-string \"\:\" \"abc:def:ghi\")
 2401       -> (\"abc\" \"def\" \"ghi\")
 2402 
 2403       (TeX-split-string \" *\" \"dvips -Plw -p3 -c4 testfile.dvi\")
 2404 
 2405       -> (\"dvips\" \"-Plw\" \"-p3\" \"-c4\" \"testfile.dvi\")
 2406 
 2407 If CHAR is nil, or \"\", an error will occur."
 2408 
 2409        (let ((regexp char)
 2410          (start 0)
 2411          (result '()))
 2412      (while (string-match regexp string start)
 2413        (let ((match (string-match regexp string start)))
 2414          (setq result (cons (substring string start match) result))
 2415          (setq start (match-end 0))))
 2416      (setq result (cons (substring string start nil) result))
 2417      (nreverse result))))
 2418 
 2419 (defun bib-cite-file-directory-p (file)
 2420   "Like default `file-directory-p' but allow FILE to end in // for ms-windows."
 2421   (save-match-data
 2422     (if (string-match "\\(.*\\)//$" file)
 2423     (file-directory-p (match-string 1 file))
 2424       (file-directory-p file))))
 2425 
 2426 (defun psg-list-env (env)
 2427   "Return a list of directory elements in ENV variable (w/o leading $)
 2428 argument may consist of environment variable plus a trailing directory, e.g.
 2429 HOME or HOME/bin (trailing directory not supported in dos or OS/2).
 2430 
 2431 bib-dos-or-os2-variable affects:
 2432   path separator used (: or ;)
 2433   whether backslashes are converted to slashes"
 2434   (if (not (getenv env))
 2435       nil                               ;Because dired-replace-in-string fails
 2436     (let* ((value (if bib-dos-or-os2-variable
 2437               (dired-replace-in-string "\\\\" "/" (getenv env))
 2438             (getenv env)))
 2439        (sep-char (or (and bib-dos-or-os2-variable ";") ":"))
 2440        (entries (and value
 2441              (or (and (fboundp 'TeX-split-string)
 2442                   (TeX-split-string sep-char value))
 2443                  (dired-split sep-char value)))))
 2444       (cl-loop for x in entries if (bib-cite-file-directory-p x) collect x))))
 2445 
 2446 (provide 'bib-cite)
 2447 ;;; bib-cite.el ends here