/[emacs]/emacs/lisp/textmodes/bibtex.el
ViewVC logotype

Diff of /emacs/lisp/textmodes/bibtex.el

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.88 by jpw, Mon Nov 1 08:12:31 2004 UTC revision 1.89 by monnier, Fri Nov 26 23:24:42 2004 UTC
# Line 34  Line 34 
34  ;;  Major mode for editing and validating BibTeX files.  ;;  Major mode for editing and validating BibTeX files.
35    
36  ;;  Usage:  ;;  Usage:
37  ;;  See documentation for function bibtex-mode (or type "\M-x describe-mode"  ;;  See documentation for function bibtex-mode or type "\M-x describe-mode"
38  ;;  when you are in BibTeX mode).  ;;  when you are in BibTeX mode.
39    
40  ;;  Todo:  ;;  Todo:
41  ;;  Distribute texinfo file.  ;;  Distribute texinfo file.
# Line 63  Line 63 
63    :type 'hook)    :type 'hook)
64    
65  (defcustom bibtex-field-delimiters 'braces  (defcustom bibtex-field-delimiters 'braces
66    "*Type of field delimiters.  Allowed values are `braces' or `double-quotes'."    "Type of field delimiters.  Allowed values are `braces' or `double-quotes'."
67    :group 'bibtex    :group 'bibtex
68    :type '(choice (const braces)    :type '(choice (const braces)
69                   (const double-quotes)))                   (const double-quotes)))
70    
71  (defcustom bibtex-entry-delimiters 'braces  (defcustom bibtex-entry-delimiters 'braces
72    "*Type of entry delimiters.  Allowed values are `braces' or `parentheses'."    "Type of entry delimiters.  Allowed values are `braces' or `parentheses'."
73    :group 'bibtex    :group 'bibtex
74    :type '(choice (const braces)    :type '(choice (const braces)
75                   (const parentheses)))                   (const parentheses)))
76    
77  (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")  (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
78    "*List of entries that get an OPTcrossref field."    "List of BibTeX entries that get an OPTcrossref field."
79    :group 'bibtex    :group 'bibtex
80    :type '(repeat string))    :type '(repeat string))
81    
82  (defcustom bibtex-include-OPTkey t  (defcustom bibtex-include-OPTkey t
83    "*If non-nil, all entries will have an OPTkey field.    "If non-nil, all entries will have an OPTkey field.
84  If this is a string, it will be used as the initial field text.  If this is a string, it will be used as the initial field text.
85  If this is a function, it will be called to generate the initial field text."  If this is a function, it will be called to generate the initial field text."
86    :group 'bibtex    :group 'bibtex
# Line 91  If this is a function, it will be called Line 91  If this is a function, it will be called
91    
92  (defcustom bibtex-user-optional-fields  (defcustom bibtex-user-optional-fields
93    '(("annote" "Personal annotation (ignored)"))    '(("annote" "Personal annotation (ignored)"))
94    "*List of optional fields the user wants to have always present.    "List of optional fields the user wants to have always present.
95  Entries should be of the same form as the OPTIONAL and  Entries should be of the same form as the OPTIONAL and
96  CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (see documentation  CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (which see)."
 of this variable for details)."  
97    :group 'bibtex    :group 'bibtex
98    :type '(repeat (group (string :tag "Field")    :type '(repeat (group (string :tag "Field")
99                          (string :tag "Comment")                          (string :tag "Comment")
# Line 106  of this variable for details)." Line 105  of this variable for details)."
105    
106  (defcustom bibtex-entry-format  (defcustom bibtex-entry-format
107    '(opts-or-alts required-fields numerical-fields)    '(opts-or-alts required-fields numerical-fields)
108    "*Type of formatting performed by `bibtex-clean-entry'.    "Type of formatting performed by `bibtex-clean-entry'.
109  It may be t, nil, or a list of symbols out of the following:  It may be t, nil, or a list of symbols out of the following:
110  opts-or-alts        Delete empty optional and alternative fields and  opts-or-alts        Delete empty optional and alternative fields and
111                        remove OPT and ALT prefixes from used fields.                        remove OPT and ALT prefixes from used fields.
# Line 118  inherit-booktitle   If entry contains a Line 117  inherit-booktitle   If entry contains a
117                        field is empty, it is set to the contents of the                        field is empty, it is set to the contents of the
118                        title field of the crossreferenced entry.                        title field of the crossreferenced entry.
119                        Caution: this will work only if buffer is                        Caution: this will work only if buffer is
120                         correctly sorted.                        correctly sorted.
121  realign             Realign entries, so that field texts and perhaps equal  realign             Realign entries, so that field texts and perhaps equal
122                        signs (depending on the value of                        signs (depending on the value of
123                        `bibtex-align-at-equal-sign') begin in the same column.                        `bibtex-align-at-equal-sign') begin in the same column.
# Line 145  The value nil means do no formatting at Line 144  The value nil means do no formatting at
144                        (const unify-case))))                        (const unify-case))))
145    
146  (defcustom bibtex-clean-entry-hook nil  (defcustom bibtex-clean-entry-hook nil
147    "*List of functions to call when entry has been cleaned.    "List of functions to call when entry has been cleaned.
148  Functions are called with point inside the cleaned entry, and the buffer  Functions are called with point inside the cleaned entry, and the buffer
149  narrowed to just the entry."  narrowed to just the entry."
150    :group 'bibtex    :group 'bibtex
151    :type 'hook)    :type 'hook)
152    
153  (defcustom bibtex-maintain-sorted-entries nil  (defcustom bibtex-maintain-sorted-entries nil
154    "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order.    "*If non-nil, BibTeX mode maintains all entries in sorted order.
155  Allowed non-nil values are:  Allowed non-nil values are:
156  plain        All entries are sorted alphabetically.  plain        All entries are sorted alphabetically.
157  crossref     All entries are sorted alphabetically unless an entry has a  crossref     All entries are sorted alphabetically unless an entry has a
# Line 168  See also `bibtex-sort-ignore-string-entr Line 167  See also `bibtex-sort-ignore-string-entr
167                   (const crossref)                   (const crossref)
168                   (const entry-class)))                   (const entry-class)))
169    
170  (defvar bibtex-sort-entry-class  (defcustom bibtex-sort-entry-class
171    '(("String")    '(("String")
172      (catch-all)      (catch-all)
173      ("Book" "Proceedings"))      ("Book" "Proceedings"))
# Line 176  See also `bibtex-sort-ignore-string-entr Line 175  See also `bibtex-sort-ignore-string-entr
175  If value of `bibtex-maintain-sorted-entries' is `entry-class'  If value of `bibtex-maintain-sorted-entries' is `entry-class'
176  entries are ordered according to the classes they belong to.  Each  entries are ordered according to the classes they belong to.  Each
177  class contains a list of entry names.  An entry `catch-all' applies  class contains a list of entry names.  An entry `catch-all' applies
178  to all entries not explicitely mentioned.")  to all entries not explicitely mentioned."
179      :group 'BibTeX
180      :type '(repeat (choice :tag "Class"
181                             (const :tag "catch-all" (catch-all))
182                             (repeat :tag "Entry name" string))))
183    
184  (defcustom bibtex-sort-ignore-string-entries t  (defcustom bibtex-sort-ignore-string-entries t
185    "*If non-nil, BibTeX @String entries are not sort-significant.    "*If non-nil, BibTeX @String entries are not sort-significant.
# Line 196  That means they are ignored when determi Line 199  That means they are ignored when determi
199    :type 'integer)    :type 'integer)
200    
201  (defcustom bibtex-parse-keys-timeout 60  (defcustom bibtex-parse-keys-timeout 60
202    "*Specify interval for parsing BibTeX buffers.    "*Time interval in seconds for parsing BibTeX buffers during idle time.
203  All BibTeX buffers in Emacs are parsed if Emacs has been idle  Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
 `bibtex-parse-keys-timeout' seconds.  Only buffers which were modified  
 after last parsing and which are maintained in sorted order are parsed."  
204    :group 'bibtex    :group 'bibtex
205    :type 'integer)    :type 'integer)
206    
# Line 210  If parsing fails, try to set this variab Line 211  If parsing fails, try to set this variab
211    :type 'boolean)    :type 'boolean)
212    
213  (defvar bibtex-entry-field-alist  (defvar bibtex-entry-field-alist
214    '(    '(("Article"
     ("Article"  
215       ((("author" "Author1 [and Author2 ...] [and others]")       ((("author" "Author1 [and Author2 ...] [and others]")
216         ("title" "Title of the article (BibTeX converts it to lowercase)")         ("title" "Title of the article (BibTeX converts it to lowercase)")
217         ("journal" "Name of the journal (use string, remove braces)")         ("journal" "Name of the journal (use string, remove braces)")
# Line 231  If parsing fails, try to set this variab Line 231  If parsing fails, try to set this variab
231         ("month" "Month of the publication as a string (remove braces)")         ("month" "Month of the publication as a string (remove braces)")
232         ("note" "Remarks to be put at the end of the \\bibitem"))))         ("note" "Remarks to be put at the end of the \\bibitem"))))
233      ("Book"      ("Book"
234       ((("author" "Author1 [and Author2 ...] [and others]" "" t)       ((("author" "Author1 [and Author2 ...] [and others]" nil t)
235         ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)         ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
236         ("title" "Title of the book")         ("title" "Title of the book")
237         ("publisher" "Publishing company")         ("publisher" "Publishing company")
238         ("year" "Year of publication"))         ("year" "Year of publication"))
# Line 243  If parsing fails, try to set this variab Line 243  If parsing fails, try to set this variab
243         ("edition" "Edition of the book as a capitalized English word")         ("edition" "Edition of the book as a capitalized English word")
244         ("month" "Month of the publication as a string (remove braces)")         ("month" "Month of the publication as a string (remove braces)")
245         ("note" "Remarks to be put at the end of the \\bibitem")))         ("note" "Remarks to be put at the end of the \\bibitem")))
246       ((("author" "Author1 [and Author2 ...] [and others]" "" t)       ((("author" "Author1 [and Author2 ...] [and others]" nil t)
247         ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)         ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
248         ("title" "Title of the book"))         ("title" "Title of the book"))
249        (("publisher" "Publishing company")        (("publisher" "Publishing company")
250         ("year" "Year of publication")         ("year" "Year of publication")
# Line 264  If parsing fails, try to set this variab Line 264  If parsing fails, try to set this variab
264         ("year" "Year of publication")         ("year" "Year of publication")
265         ("note" "Remarks to be put at the end of the \\bibitem"))))         ("note" "Remarks to be put at the end of the \\bibitem"))))
266      ("InBook"      ("InBook"
267       ((("author" "Author1 [and Author2 ...] [and others]" "" t)       ((("author" "Author1 [and Author2 ...] [and others]" nil t)
268         ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)         ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
269         ("title" "Title of the book")         ("title" "Title of the book")
270         ("chapter" "Chapter in the book")         ("chapter" "Chapter in the book")
271         ("publisher" "Publishing company")         ("publisher" "Publishing company")
# Line 279  If parsing fails, try to set this variab Line 279  If parsing fails, try to set this variab
279         ("month" "Month of the publication as a string (remove braces)")         ("month" "Month of the publication as a string (remove braces)")
280         ("pages" "Pages in the book")         ("pages" "Pages in the book")
281         ("note" "Remarks to be put at the end of the \\bibitem")))         ("note" "Remarks to be put at the end of the \\bibitem")))
282       ((("author" "Author1 [and Author2 ...] [and others]" "" t)       ((("author" "Author1 [and Author2 ...] [and others]" nil t)
283         ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)         ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
284         ("title" "Title of the book")         ("title" "Title of the book")
285         ("chapter" "Chapter in the book"))         ("chapter" "Chapter in the book"))
286        (("pages" "Pages in the book")        (("pages" "Pages in the book")
# Line 419  If parsing fails, try to set this variab Line 419  If parsing fails, try to set this variab
419         ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")         ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
420         ("note" "Remarks to be put at the end of the \\bibitem"))         ("note" "Remarks to be put at the end of the \\bibitem"))
421        (("month" "Month of the publication as a string (remove braces)")        (("month" "Month of the publication as a string (remove braces)")
422         ("year" "Year of publication"))))         ("year" "Year of publication")))))
     )  
423    
424    "Defines entry types and their associated fields.    "List of BibTeX entry types and their associated fields.
425  List of  List elements are triples
426  \(ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL))  \(ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL)).
427  triples.  ENTRY-NAME is the name of a BibTeX entry.  The remaining pairs contain
428  If the third element is nil, the first pair is always used.  the required and optional fields of the BibTeX entry.
429  If not, the second pair is used in the case of presence of a crossref  The second pair is used if a crossref field is present
430  field and the third in the case of absence.  and the first pair is used if a crossref field is absent.
431    If the second pair is nil, the first pair is always used.
432  REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.  REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.
433  Each element of these lists is a list of the form  Each element of these lists is a list of the form
434  \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).  \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).
435  COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.  COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.
436  FIELD-NAME is the name of the field, COMMENT-STRING the comment to  FIELD-NAME is the name of the field, COMMENT-STRING is the comment that
437  appear in the echo area, INIT is either the initial content of the  appears in the echo area, INIT is either the initial content of the
438  field or a function, which is called to determine the initial content  field or a function, which is called to determine the initial content
439  of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the  of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
440  field is an alternative.  ALTERNATIVE-FLAG may be t only in the  field is an alternative.  ALTERNATIVE-FLAG may be t only in the
441  REQUIRED or CROSSREF-REQUIRED lists.")  REQUIRED or CROSSREF-REQUIRED lists.")
442    
443  (defvar bibtex-comment-start "@Comment"  (defcustom bibtex-comment-start "@Comment"
444    "String starting a BibTeX comment.")    "String starting a BibTeX comment."
445      :group 'bibtex
446      :type 'string)
447    
448  (defcustom bibtex-add-entry-hook nil  (defcustom bibtex-add-entry-hook nil
449    "List of functions to call when entry has been inserted."    "List of functions to call when BibTeX entry has been inserted."
450    :group 'bibtex    :group 'bibtex
451    :type 'hook)    :type 'hook)
452    
# Line 498  Each element is a pair of strings (ABBRE Line 500  Each element is a pair of strings (ABBRE
500    
501  (defcustom bibtex-string-files nil  (defcustom bibtex-string-files nil
502    "*List of BibTeX files containing string definitions.    "*List of BibTeX files containing string definitions.
503  List elements can be absolute file names or file names relative to the  List elements can be absolute file names or file names relative
504  directories specified in `bibtex-string-file-path'."  to the directories specified in `bibtex-string-file-path'."
505    :group 'bibtex    :group 'bibtex
506    :type '(repeat file))    :type '(repeat file))
507    
# Line 507  directories specified in `bibtex-string- Line 509  directories specified in `bibtex-string-
509    "*Colon separated list of paths to search for `bibtex-string-files'.")    "*Colon separated list of paths to search for `bibtex-string-files'.")
510    
511  (defcustom bibtex-files nil  (defcustom bibtex-files nil
512    "*List of BibTeX files checked for duplicate keys.    "List of BibTeX files that are searched for entry keys.
513  List elements can be absolute file names or file names relative to the  List elements can be absolute file names or file names relative to the
514  directories specified in `bibtex-file-path'.  If an element is a directory,  directories specified in `bibtex-file-path'.  If an element is a directory,
515  check all BibTeX files in this directory.  If an element is the symbol  check all BibTeX files in this directory.  If an element is the symbol
# Line 524  check all BibTeX files in this directory Line 526  check all BibTeX files in this directory
526    :type 'boolean)    :type 'boolean)
527    
528  (defcustom bibtex-autokey-prefix-string ""  (defcustom bibtex-autokey-prefix-string ""
529    "*String to use as a prefix for all generated keys.    "String prefix for automatically generated reference keys.
530  See `bibtex-generate-autokey' for details."  See `bibtex-generate-autokey' for details."
531    :group 'bibtex-autokey    :group 'bibtex-autokey
532    :type 'string)    :type 'string)
# Line 539  See `bibtex-generate-autokey' for detail Line 541  See `bibtex-generate-autokey' for detail
541                   integer))                   integer))
542    
543  (defcustom bibtex-autokey-names-stretch 0  (defcustom bibtex-autokey-names-stretch 0
544    "*Number of names that can additionally be used.    "Number of names that can additionally be used for reference keys.
545  These names are used only, if all names are used then.  These names are used only, if all names are used then.
546  See `bibtex-generate-autokey' for details."  See `bibtex-generate-autokey' for details."
547    :group 'bibtex-autokey    :group 'bibtex-autokey
548    :type 'integer)    :type 'integer)
549    
550  (defcustom bibtex-autokey-additional-names ""  (defcustom bibtex-autokey-additional-names ""
551    "*String to prepend to the generated key if not all names could be used.    "String to append to the generated key if not all names could be used.
552  See `bibtex-generate-autokey' for details."  See `bibtex-generate-autokey' for details."
553    :group 'bibtex-autokey    :group 'bibtex-autokey
554    :type 'string)    :type 'string)
# Line 581  removing any character accents.") Line 583  removing any character accents.")
583  (defcustom bibtex-autokey-name-change-strings  (defcustom bibtex-autokey-name-change-strings
584    bibtex-autokey-transcriptions    bibtex-autokey-transcriptions
585    "Alist of (OLD-REGEXP . NEW-STRING) pairs.    "Alist of (OLD-REGEXP . NEW-STRING) pairs.
586  Any part of name matching a OLD-REGEXP is replaced by NEW-STRING.  Any part of a name matching OLD-REGEXP is replaced by NEW-STRING.
587  Case is significant in OLD-REGEXP.  All regexps are tried in the  Case is significant in OLD-REGEXP.  All regexps are tried in the
588  order in which they appear in the list.  order in which they appear in the list.
589  See `bibtex-generate-autokey' for details."  See `bibtex-generate-autokey' for details."
# Line 637  See `bibtex-generate-autokey' for detail Line 639  See `bibtex-generate-autokey' for detail
639    :type '(choice (const :tag "All" infty)    :type '(choice (const :tag "All" infty)
640                   integer))                   integer))
641    
642  (defcustom bibtex-autokey-title-terminators  (defcustom bibtex-autokey-title-terminators "[.!?:;]\\|--"
643    '("\\." "!"  "\\?" ":" ";" "--")    "Regexp defining the termination of the main part of the title.
644    "*Regexp list defining the termination of the main part of the title.  Case of the regexps is ignored.  See `bibtex-generate-autokey' for details."
 Case of the regexps is ignored.  
 See `bibtex-generate-autokey' for details."  
645    :group 'bibtex-autokey    :group 'bibtex-autokey
646    :type '(repeat regexp))    :type 'regexp)
647    
648  (defcustom bibtex-autokey-titlewords-stretch 2  (defcustom bibtex-autokey-titlewords-stretch 2
649    "*Number of words that can additionally be used from the title.    "*Number of words that can additionally be used from the title.
# Line 724  See `bibtex-generate-autokey' for detail Line 724  See `bibtex-generate-autokey' for detail
724    :type 'boolean)    :type 'boolean)
725    
726  (defcustom bibtex-autokey-before-presentation-function nil  (defcustom bibtex-autokey-before-presentation-function nil
727    "*Function to call before the generated key is presented.    "If non-nil, function to call before generated key is presented.
728  If non-nil this should be a function which is called before the generated key  The function must take one argument (the automatically generated key),
729  is presented.  The function must take one argument (the automatically  and must return a string (the key to use)."
 generated key), and must return a string (the key to use)."  
730    :group 'bibtex-autokey    :group 'bibtex-autokey
731    :type '(choice (const nil) function))    :type '(choice (const nil) function))
732    
# Line 781  If non-nil, the column for the equal sig Line 780  If non-nil, the column for the equal sig
780  (defcustom bibtex-summary-function 'bibtex-summary  (defcustom bibtex-summary-function 'bibtex-summary
781    "Function to call for generating a one-line summary of a BibTeX entry.    "Function to call for generating a one-line summary of a BibTeX entry.
782  It takes one argument, the key of the entry.  It takes one argument, the key of the entry.
783  Used by `bibtex-complete-key-cleanup' and `bibtex-copy-summary-as-kill'."  Used by `bibtex-complete-crossref-cleanup' and `bibtex-copy-summary-as-kill'."
784    :group 'bibtex    :group 'bibtex
785    :type '(choice (const :tag "Default" bibtex-summary)    :type '(choice (const :tag "Default" bibtex-summary)
786                   (function :tag "Personalized function")))                   (function :tag "Personalized function")))
# Line 790  Used by `bibtex-complete-key-cleanup' an Line 789  Used by `bibtex-complete-key-cleanup' an
789    '((("url" . ".*:.*"))    '((("url" . ".*:.*"))
790      ;; Example of a complex setup.      ;; Example of a complex setup.
791      (("journal" . "\\<\\(PR[ABCDEL]?\\|RMP\\)\\>")      (("journal" . "\\<\\(PR[ABCDEL]?\\|RMP\\)\\>")
792       "http://publish.aps.org/abstract/"       "http://link.aps.org/abstract/"
793       ("journal" ".*" downcase)       ("journal" ".*" downcase)
794       "/v"       "/v"
795       ("volume" ".*" 0)       ("volume" ".*" 0)
# Line 862  Case is always ignored.  Always remove t Line 861  Case is always ignored.  Always remove t
861      (define-key km "\C-c\C-c" 'bibtex-clean-entry)      (define-key km "\C-c\C-c" 'bibtex-clean-entry)
862      (define-key km "\C-c\C-q" 'bibtex-fill-entry)      (define-key km "\C-c\C-q" 'bibtex-fill-entry)
863      (define-key km "\C-c\C-s" 'bibtex-find-entry)      (define-key km "\C-c\C-s" 'bibtex-find-entry)
864        (define-key km "\C-c\C-x" 'bibtex-find-crossref)
865      (define-key km "\C-c\C-t" 'bibtex-copy-summary-as-kill)      (define-key km "\C-c\C-t" 'bibtex-copy-summary-as-kill)
866      (define-key km "\C-c?" 'bibtex-print-help-message)      (define-key km "\C-c?" 'bibtex-print-help-message)
867      (define-key km "\C-c\C-p" 'bibtex-pop-previous)      (define-key km "\C-c\C-p" 'bibtex-pop-previous)
# Line 914  Case is always ignored.  Always remove t Line 914  Case is always ignored.  Always remove t
914       ["Next Field" bibtex-next-field t]       ["Next Field" bibtex-next-field t]
915       ["Beginning of Entry" bibtex-beginning-of-entry t]       ["Beginning of Entry" bibtex-beginning-of-entry t]
916       ["End of Entry" bibtex-end-of-entry t])       ["End of Entry" bibtex-end-of-entry t])
917      ("Moving in BibTeX Buffer"      ("Moving in BibTeX Buffers"
918       ["Find Entry" bibtex-find-entry t]       ["Find Entry" bibtex-find-entry t]
919       ["Find Crossref Entry" bibtex-find-crossref t])       ["Find Crossref Entry" bibtex-find-crossref t])
     ("Moving between BibTeX Buffers"  
      ["Find Entry Globally" bibtex-find-entry-globally t])  
920      "--"      "--"
921      ("Operating on Current Field"      ("Operating on Current Field"
922       ["Fill Field" fill-paragraph t]       ["Fill Field" fill-paragraph t]
# Line 950  Case is always ignored.  Always remove t Line 948  Case is always ignored.  Always remove t
948       ["Paste Previously Killed Entry" bibtex-yank-pop t]       ["Paste Previously Killed Entry" bibtex-yank-pop t]
949       "--"       "--"
950       ["Copy Summary to Kill Ring" bibtex-copy-summary-as-kill t]       ["Copy Summary to Kill Ring" bibtex-copy-summary-as-kill t]
951         ["Browse URL" bibtex-url t]
952       "--"       "--"
953       ["Ispell Entry" bibtex-ispell-entry t]       ["Ispell Entry" bibtex-ispell-entry t]
954       ["Ispell Entry Abstract" bibtex-ispell-abstract t]       ["Ispell Entry Abstract" bibtex-ispell-abstract t]
955         "--"
956       ["Narrow to Entry" bibtex-narrow-to-entry t]       ["Narrow to Entry" bibtex-narrow-to-entry t]
957         ["Make Entry Visible" bibtex-reposition-window t]
958         ["Mark Entry" bibtex-mark-entry t]
959       "--"       "--"
960       ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex       ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
961        (fboundp 'reftex-view-crossref-from-bibtex)])        (fboundp 'reftex-view-crossref-from-bibtex)])
# Line 983  Case is always ignored.  Always remove t Line 985  Case is always ignored.  Always remove t
985          ["Technical Manual" bibtex-Manual t]          ["Technical Manual" bibtex-Manual t]
986          ["Unpublished" bibtex-Unpublished t]          ["Unpublished" bibtex-Unpublished t]
987          ["Miscellaneous" bibtex-Misc t]          ["Miscellaneous" bibtex-Misc t]
988            "--"
989          ["String" bibtex-String t]          ["String" bibtex-String t]
990          ["Preamble" bibtex-Preamble t]))          ["Preamble" bibtex-Preamble t]))
991    
# Line 1085  The CDRs of the elements are t for heade Line 1088  The CDRs of the elements are t for heade
1088  (defconst bibtex-key-in-head 2  (defconst bibtex-key-in-head 2
1089    "Regexp subexpression number of the key part in `bibtex-entry-head'.")    "Regexp subexpression number of the key part in `bibtex-entry-head'.")
1090    
1091    (defconst bibtex-string-maybe-empty-head
1092      (concat "^[ \t]*\\(@String\\)[ \t]*[({]\\("
1093              bibtex-reference-key
1094              "\\)?")
1095      "Regexp matching the header line of a BibTeX String entry.")
1096    
1097  (defconst bibtex-entry-postfix "[ \t\n]*,?[ \t\n]*[})]"  (defconst bibtex-entry-postfix "[ \t\n]*,?[ \t\n]*[})]"
1098    "Regexp matching the postfix of a BibTeX entry.")    "Regexp matching the postfix of a BibTeX entry.")
1099    
1100  (defvar bibtex-known-entry-type-re  (defvar bibtex-known-entry-type-re
1101    (regexp-opt (mapcar 'car bibtex-entry-field-alist))    (regexp-opt (mapcar 'car bibtex-entry-field-alist))
1102    "Regexp matching the name of a BibTeX entry type.")    "Regexp matching the name of a BibTeX entry.")
1103    
1104  (defvar bibtex-valid-entry-re  (defvar bibtex-valid-entry-re
1105    (concat "@[ \t]*\\(" bibtex-known-entry-type-re "\\)")    (concat "@[ \t]*\\(" bibtex-known-entry-type-re "\\)")
# Line 1166  ARG is ignored." Line 1175  ARG is ignored."
1175   '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil))   '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil))
1176    
1177    
1178    (defun bibtex-remove-delimiters-string (str)
1179      "Remove delimiters of string STR."
1180      (if (and (memq (aref str 0) '(?\{ ?\"))
1181               (memq (aref str (1- (length str))) '(?\} ?\")))
1182          (substring str 1 -1)
1183        str))
1184    
1185  (defun bibtex-parse-association (parse-lhs parse-rhs)  (defun bibtex-parse-association (parse-lhs parse-rhs)
1186    "Parse a string of the format <left-hand-side = right-hand-side>.    "Parse a string of the format <left-hand-side = right-hand-side>.
1187  The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding  The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
# Line 1226  BibTeX field as necessary." Line 1242  BibTeX field as necessary."
1242    "Syntax-table to parse matched quotes.")    "Syntax-table to parse matched quotes.")
1243    
1244  (defun bibtex-parse-field-string ()  (defun bibtex-parse-field-string ()
1245    "Parse a field string enclosed by braces or quotes.    "Parse a BibTeX field string enclosed by braces or quotes.
1246  If a syntactically correct string is found, a pair containing the start and  If a syntactically correct string is found, a pair containing the start and
1247  end position of the field string is returned, nil otherwise."  end position of the field string is returned, nil otherwise."
1248    (let ((end-point    (let ((end-point
# Line 1273  the name and text parts of the field is Line 1289  the name and text parts of the field is
1289                                'bibtex-parse-field-text)))                                'bibtex-parse-field-text)))
1290    
1291  (defun bibtex-search-forward-field (name &optional bound)  (defun bibtex-search-forward-field (name &optional bound)
1292    "Search forward to find a field of name NAME.    "Search forward to find a BibTeX field of name NAME.
1293  If a syntactically correct field is found, a pair containing the boundaries of  If a syntactically correct field is found, a pair containing the boundaries of
1294  the name and text parts of the field is returned.  The search is limited by  the name and text parts of the field is returned.  The search is limited by
1295  optional arg BOUND.  If BOUND is t the search is limited by the end of the  optional arg BOUND.  If BOUND is t the search is limited by the end of the
# Line 1300  current entry.  Do not move point." Line 1316  current entry.  Do not move point."
1316          boundaries))))          boundaries))))
1317    
1318  (defun bibtex-search-backward-field (name &optional bound)  (defun bibtex-search-backward-field (name &optional bound)
1319    "Search backward to find a field of name NAME.    "Search backward to find a BibTeX field of name NAME.
1320  If a syntactically correct field is found, a pair containing the boundaries of  If a syntactically correct field is found, a pair containing the boundaries of
1321  the name and text parts of the field is returned.  The search is limited by  the name and text parts of the field is returned.  The search is limited by
1322  optional arg BOUND.  If BOUND is t the search is limited by the beginning of the  optional arg BOUND.  If BOUND is t the search is limited by the beginning of the
# Line 1352  If optional arg REMOVE-DELIM is non-nil Line 1368  If optional arg REMOVE-DELIM is non-nil
1368  if present."  if present."
1369    (let ((content (buffer-substring-no-properties (cadr bounds)    (let ((content (buffer-substring-no-properties (cadr bounds)
1370                                                   (cddr bounds))))                                                   (cddr bounds))))
1371      (if (and remove-delim      (if remove-delim
1372               (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content))          (bibtex-remove-delimiters-string content)
         (substring content (match-beginning 1) (match-end 1))  
1373        content)))        content)))
1374    
1375  (defun bibtex-text-in-field (field &optional follow-crossref)  (defun bibtex-text-in-field (field &optional follow-crossref)
1376    "Get content of field FIELD of current BibTeX entry.  Return nil if not found.    "Get content of field FIELD of current BibTeX entry.
1377    Return nil if not found.
1378  If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."  If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1379    (save-excursion    (save-excursion
1380      (save-restriction      (save-restriction
# Line 1458  If optional arg REMOVE-DELIM is non-nil Line 1474  If optional arg REMOVE-DELIM is non-nil
1474  delimiters if present."  delimiters if present."
1475    (let ((content (buffer-substring-no-properties (nth 0 (cdr bounds))    (let ((content (buffer-substring-no-properties (nth 0 (cdr bounds))
1476                                                   (nth 1 (cdr bounds)))))                                                   (nth 1 (cdr bounds)))))
1477      (if (and remove-delim      (if remove-delim
1478               (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content))          (bibtex-remove-delimiters-string content)
         (substring content (match-beginning 1) (match-end 1))  
1479        content)))        content)))
1480    
1481  (defsubst bibtex-start-of-text-in-string (bounds)  (defsubst bibtex-start-of-text-in-string (bounds)
# Line 1552  If `bibtex-sort-ignore-string-entries' i Line 1567  If `bibtex-sort-ignore-string-entries' i
1567    "Echo a message about progress of current buffer.    "Echo a message about progress of current buffer.
1568  If FLAG is a string, the message is initialized (in this case a  If FLAG is a string, the message is initialized (in this case a
1569  value for INTERVAL may be given as well (if not this is set to 5)).  value for INTERVAL may be given as well (if not this is set to 5)).
1570  If FLAG is done, the message is deinitialized.  If FLAG is `done', the message is deinitialized.
1571  If FLAG is nil, a message is echoed if point was incremented at least  If FLAG is nil, a message is echoed if point was incremented at least
1572  `bibtex-progress-interval' percent since last message was echoed."  `bibtex-progress-interval' percent since last message was echoed."
1573    (cond ((stringp flag)    (cond ((stringp flag)
1574           (setq bibtex-progress-lastmes flag)           (setq bibtex-progress-lastmes flag
1575           (setq bibtex-progress-interval (or interval 5)                 bibtex-progress-interval (or interval 5)
1576                 bibtex-progress-lastperc 0))                 bibtex-progress-lastperc 0))
1577          ((equal flag 'done)          ((eq flag 'done)
1578           (message  "%s (done)" bibtex-progress-lastmes)           (message  "%s (done)" bibtex-progress-lastmes)
1579           (setq bibtex-progress-lastmes nil))           (setq bibtex-progress-lastmes nil))
1580          (t          (t
# Line 1574  If FLAG is nil, a message is echoed if p Line 1589  If FLAG is nil, a message is echoed if p
1589    
1590  (defun bibtex-field-left-delimiter ()  (defun bibtex-field-left-delimiter ()
1591    "Return a string dependent on `bibtex-field-delimiters'."    "Return a string dependent on `bibtex-field-delimiters'."
1592    (if (equal bibtex-field-delimiters 'braces)    (if (eq bibtex-field-delimiters 'braces)
1593        "{"        "{"
1594      "\""))      "\""))
1595    
1596  (defun bibtex-field-right-delimiter ()  (defun bibtex-field-right-delimiter ()
1597    "Return a string dependent on `bibtex-field-delimiters'."    "Return a string dependent on `bibtex-field-delimiters'."
1598    (if (equal bibtex-field-delimiters 'braces)    (if (eq bibtex-field-delimiters 'braces)
1599        "}"        "}"
1600      "\""))      "\""))
1601    
1602  (defun bibtex-entry-left-delimiter ()  (defun bibtex-entry-left-delimiter ()
1603    "Return a string dependent on `bibtex-entry-delimiters'."    "Return a string dependent on `bibtex-entry-delimiters'."
1604    (if (equal bibtex-entry-delimiters 'braces)    (if (eq bibtex-entry-delimiters 'braces)
1605        "{"        "{"
1606      "("))      "("))
1607    
1608  (defun bibtex-entry-right-delimiter ()  (defun bibtex-entry-right-delimiter ()
1609    "Return a string dependent on `bibtex-entry-delimiters'."    "Return a string dependent on `bibtex-entry-delimiters'."
1610    (if (equal bibtex-entry-delimiters 'braces)    (if (eq bibtex-entry-delimiters 'braces)
1611        "}"        "}"
1612      ")"))      ")"))
1613    
1614  (defun bibtex-search-entry (empty-head &optional bound noerror backward)  (defun bibtex-search-entry (empty-head &optional bound noerror backward)
1615    "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t).    "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t).
1616  BOUND and NOERROR are exactly as in `re-search-forward'.  If BACKWARD  BOUND and NOERROR are exactly as in `re-search-forward'.  If BACKWARD
1617  is non-nil, search is done in reverse direction.  Point is moved past the  is non-nil, search in reverse direction.  Move point past the closing
1618  closing delimiter (at the beginning of entry if BACKWARD is non-nil).  delimiter (at the beginning of entry if BACKWARD is non-nil).
1619  Return a cons pair with buffer positions of beginning and end of entry.  Return a cons pair with buffer positions of beginning and end of entry.
1620  After call to this function MATCH-BEGINNING and MATCH-END functions  After call to this function MATCH-BEGINNING and MATCH-END functions
1621  are defined, but only for the head part of the entry  are defined, but only for the head part of the entry
# Line 1620  are defined, but only for the head part Line 1635  are defined, but only for the head part
1635              (cond ((not noerror)              (cond ((not noerror)
1636                     ;; yell                     ;; yell
1637                     (error "Backward search of BibTeX entry failed"))                     (error "Backward search of BibTeX entry failed"))
1638                    ((equal noerror t)                    ((eq noerror t)
1639                     ;; don't move                     ;; don't move
1640                     (goto-char pnt)))                     (goto-char pnt)))
1641              nil))              nil))
1642        (let ((limit (or bound (point-max)))        (let (found)
1643              found)          (unless bound (setq bound (point-max)))
1644          (while (and (not found)          (while (and (not found)
1645                      (re-search-forward entry-head-re bound noerror))                      (re-search-forward entry-head-re bound noerror))
1646            (save-match-data            (save-match-data
# Line 1639  are defined, but only for the head part Line 1654  are defined, but only for the head part
1654                    (infix-start (point))                    (infix-start (point))
1655                    finished bounds)                    finished bounds)
1656                (while (not finished)                (while (not finished)
1657                  (skip-chars-forward " \t\n" limit)                  (skip-chars-forward " \t\n" bound)
1658                  (if (and (setq bounds (bibtex-parse-field bibtex-field-name))                  (if (and (setq bounds (bibtex-parse-field bibtex-field-name))
1659                           (<= (bibtex-end-of-field bounds) limit))                           (<= (bibtex-end-of-field bounds) bound))
1660                      (setq infix-start (bibtex-end-of-field bounds))                      (setq infix-start (bibtex-end-of-field bounds))
1661                    (setq finished t))                    (setq finished t))
1662                  (goto-char infix-start))                  (goto-char infix-start))
# Line 1649  are defined, but only for the head part Line 1664  are defined, but only for the head part
1664                ;; that BOUND is respected.                ;; that BOUND is respected.
1665                (when (and (looking-at bibtex-entry-postfix)                (when (and (looking-at bibtex-entry-postfix)
1666                           (eq (char-before (match-end 0)) entry-closer)                           (eq (char-before (match-end 0)) entry-closer)
1667                           (<= (match-end 0) limit))                           (<= (match-end 0) bound))
1668                  (goto-char (match-end 0))                  (goto-char (match-end 0))
1669                  (setq found t)))))                  (setq found t)))))
1670          (if found          (if found
# Line 1657  are defined, but only for the head part Line 1672  are defined, but only for the head part
1672            (cond ((not noerror)            (cond ((not noerror)
1673                   ;; yell                   ;; yell
1674                   (error "Search of BibTeX entry failed"))                   (error "Search of BibTeX entry failed"))
1675                  ((equal noerror t)                  ((eq noerror t)
1676                   ;; don't move                   ;; don't move
1677                   (goto-char pnt)))                   (goto-char pnt)))
1678            nil)))))            nil)))))
# Line 1727  are defined, but only for the head part Line 1742  are defined, but only for the head part
1742    
1743  (defun bibtex-enclosing-field (&optional noerr)  (defun bibtex-enclosing-field (&optional noerr)
1744    "Search for BibTeX field enclosing point.    "Search for BibTeX field enclosing point.
1745  Use `match-beginning' and `match-end' to parse the field.  If NOERR is non-nil,  Unless NOERR is non-nil, signal an error if no enclosing field is found.
1746  no error is signalled.  In this case, bounds are returned on success,  On success return bounds, nil otherwise.  Do not move point."
 nil otherwise.  Does not move point."  
1747    (let ((bounds (bibtex-search-backward-field bibtex-field-name t)))    (let ((bounds (bibtex-search-backward-field bibtex-field-name t)))
1748      (if (and bounds      (if (and bounds
1749               (<= (bibtex-start-of-field bounds) (point))               (<= (bibtex-start-of-field bounds) (point))
# Line 1751  Beginning (but not end) of entry is give Line 1765  Beginning (but not end) of entry is give
1765        (goto-char old-point)        (goto-char old-point)
1766        (error "Can't find end of enclosing BibTeX entry"))))        (error "Can't find end of enclosing BibTeX entry"))))
1767    
1768  (defun bibtex-insert-current-kill (n)  (defun bibtex-insert-kill (n)
1769      "Reinsert the Nth stretch of killed BibTeX text."
1770    (if (not bibtex-last-kill-command)    (if (not bibtex-last-kill-command)
1771        (error "BibTeX kill ring is empty")        (error "BibTeX kill ring is empty")
1772      (let* ((kr (if (equal bibtex-last-kill-command 'field)      (let* ((kr (if (eq bibtex-last-kill-command 'field)
1773                     'bibtex-field-kill-ring                     'bibtex-field-kill-ring
1774                   'bibtex-entry-kill-ring))                   'bibtex-entry-kill-ring))
1775             (kryp (if (equal bibtex-last-kill-command 'field)             (kryp (if (eq bibtex-last-kill-command 'field)
1776                       'bibtex-field-kill-ring-yank-pointer                       'bibtex-field-kill-ring-yank-pointer
1777                     'bibtex-entry-kill-ring-yank-pointer))                     'bibtex-entry-kill-ring-yank-pointer))
1778             (ARGth-kill-element (nthcdr (mod (- n (length (eval kryp)))             (current (car (set kryp (nthcdr (mod (- n (length (eval kryp)))
1779                                              (length (eval kr)))                                                  (length (eval kr)))
1780                                         (eval kr)))                                             (eval kr))))))
1781             (current (car (set kryp ARGth-kill-element))))        (if (eq bibtex-last-kill-command 'field)
1782        (cond            (let (bibtex-help-message)
1783         ((equal bibtex-last-kill-command 'field)              (bibtex-find-text)
1784          (let (bibtex-help-message)              (if (looking-at "[}\"]")
1785            (bibtex-find-text nil t)                  (forward-char))
1786            (if (looking-at "[}\"]")              (set-mark (point))
1787                (forward-char)))              (message "Mark set")
1788          (set-mark (point))              (bibtex-make-field current t))
         (message "Mark set")  
         (bibtex-make-field (list (elt current 1) nil (elt current 2)) t))  
        ((equal bibtex-last-kill-command 'entry)  
1789          (unless (eobp) (bibtex-beginning-of-entry))          (unless (eobp) (bibtex-beginning-of-entry))
1790          (set-mark (point))          (set-mark (point))
1791          (message "Mark set")          (message "Mark set")
1792          (insert (elt current 1)))          (insert current)))))
        (t  
         (error "Unknown tag field: %s.  Please submit a bug report"  
                bibtex-last-kill-command))))))  
1793    
1794  (defun bibtex-format-entry ()  (defun bibtex-format-entry ()
1795    "Helper function for `bibtex-clean-entry'.    "Helper function for `bibtex-clean-entry'.
# Line 1789  Formats current entry according to varia Line 1798  Formats current entry according to varia
1798      (save-restriction      (save-restriction
1799        (bibtex-narrow-to-entry)        (bibtex-narrow-to-entry)
1800        (let ((case-fold-search t)        (let ((case-fold-search t)
1801              (format (if (equal bibtex-entry-format t)              (format (if (eq bibtex-entry-format t)
1802                          '(realign opts-or-alts required-fields                          '(realign opts-or-alts required-fields
1803                                    numerical-fields                                    numerical-fields
1804                                    last-comma page-dashes delimiters                                    last-comma page-dashes delimiters
# Line 1828  Formats current entry according to varia Line 1837  Formats current entry according to varia
1837            (setq crossref-key (and field            (setq crossref-key (and field
1838                                    (not (string-match bibtex-empty-field-re                                    (not (string-match bibtex-empty-field-re
1839                                                       (cdr field)))                                                       (cdr field)))
1840                                    (cdr field))                                    (bibtex-remove-delimiters-string (cdr field)))
1841                  req-field-list (if crossref-key                  req-field-list (if crossref-key
1842                                     (nth 0 (nth 2 entry-list)) ; crossref part                                     (nth 0 (nth 2 entry-list)) ; crossref part
1843                                   (nth 0 (nth 1 entry-list)))) ; required part                                   (nth 0 (nth 1 entry-list)))) ; required part
# Line 1843  Formats current entry according to varia Line 1852  Formats current entry according to varia
1852                    (cond ((not non-empty-alternative)                    (cond ((not non-empty-alternative)
1853                           (setq non-empty-alternative t))                           (setq non-empty-alternative t))
1854                          ((memq 'required-fields format)                          ((memq 'required-fields format)
1855                           (error "More than one non-empty alternative.")))))))                           (error "More than one non-empty alternative")))))))
1856    
1857          (if (and alternatives-there          (if (and alternatives-there
1858                   (not non-empty-alternative)                   (not non-empty-alternative)
# Line 1854  Formats current entry according to varia Line 1863  Formats current entry according to varia
1863          (goto-char (point-min))          (goto-char (point-min))
1864          (while (setq bounds (bibtex-search-forward-field bibtex-field-name))          (while (setq bounds (bibtex-search-forward-field bibtex-field-name))
1865            (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))            (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))
1866                   (end-field (copy-marker (bibtex-end-of-field bounds)))                   (end-field (copy-marker (bibtex-end-of-field bounds) t))
1867                   (beg-name  (copy-marker (bibtex-start-of-name-in-field bounds)))                   (beg-name  (copy-marker (bibtex-start-of-name-in-field bounds)))
1868                   (end-name  (copy-marker (bibtex-end-of-name-in-field bounds)))                   (end-name  (copy-marker (bibtex-end-of-name-in-field bounds)))
1869                   (beg-text  (copy-marker (bibtex-start-of-text-in-field bounds)))                   (beg-text  (copy-marker (bibtex-start-of-text-in-field bounds)))
1870                   (end-text  (copy-marker (bibtex-end-of-text-in-field bounds)))                   (end-text  (copy-marker (bibtex-end-of-text-in-field bounds) t))
1871                   (opt-alt   (string-match "OPT\\|ALT"                   (opt-alt   (string-match "OPT\\|ALT"
1872                                            (buffer-substring-no-properties beg-name (+ beg-name 3))))                                            (buffer-substring-no-properties
1873                                               beg-name (+ beg-name 3))))
1874                   (field-name (buffer-substring-no-properties                   (field-name (buffer-substring-no-properties
1875                                (if opt-alt (+ beg-name 3) beg-name) end-name))                                (if opt-alt (+ beg-name 3) beg-name) end-name))
1876                   (empty-field (string-match bibtex-empty-field-re                   (empty-field (string-match bibtex-empty-field-re
1877                                              (buffer-substring-no-properties beg-field end-field)))                                              (buffer-substring-no-properties
1878                                                 beg-field end-field)))
1879                   deleted)                   deleted)
1880    
1881              ;; We have more elegant high-level functions for several              ;; We have more elegant high-level functions for several
# Line 1926  Formats current entry according to varia Line 1937  Formats current entry according to varia
1937                         empty-field                         empty-field
1938                         (bibtex-string= field-name "booktitle")                         (bibtex-string= field-name "booktitle")
1939                         crossref-key)                         crossref-key)
1940                    (let ((title (save-restriction                    (let ((title (save-excursion
1941                                   (widen)                                   (save-restriction
1942                                   (if (bibtex-find-entry crossref-key)                                     (widen)
1943                                       (bibtex-text-in-field "title")))))                                     (if (bibtex-find-entry crossref-key t)
1944                                           (bibtex-text-in-field "title"))))))
1945                      (when title                      (when title
1946                        (setq empty-field nil)                        (setq empty-field nil)
1947                        (goto-char (1+ beg-text))                        (goto-char (1+ beg-text))
# Line 2055  and `bibtex-autokey-names-stretch'." Line 2067  and `bibtex-autokey-names-stretch'."
2067    (let ((names (bibtex-autokey-get-field "author\\|editor"    (let ((names (bibtex-autokey-get-field "author\\|editor"
2068                                           bibtex-autokey-name-change-strings)))                                           bibtex-autokey-name-change-strings)))
2069      ;; Some entries do not have a name field.      ;; Some entries do not have a name field.
2070      (unless (string= "" names)      (if (string= "" names)
2071            names
2072        (let* ((case-fold-search t)        (let* ((case-fold-search t)
2073               (name-list (mapcar 'bibtex-autokey-demangle-name               (name-list (mapcar 'bibtex-autokey-demangle-name
2074                                  (split-string names "[ \t\n]+and[ \t\n]+")))                                  (split-string names "[ \t\n]+and[ \t\n]+")))
# Line 2096  and `bibtex-autokey-names-stretch'." Line 2109  and `bibtex-autokey-names-stretch'."
2109                        ;; --> take the last token                        ;; --> take the last token
2110                        (match-string 1 fullname))                        (match-string 1 fullname))
2111                       (t (error "Name `%s' is incorrectly formed" fullname)))))                       (t (error "Name `%s' is incorrectly formed" fullname)))))
2112      (bibtex-autokey-abbrev      (funcall bibtex-autokey-name-case-convert
2113       (funcall bibtex-autokey-name-case-convert name)               (bibtex-autokey-abbrev name bibtex-autokey-name-length))))
      bibtex-autokey-name-length)))  
2114    
2115  (defun bibtex-autokey-get-year ()  (defun bibtex-autokey-get-year ()
2116    "Return year field contents as a string obeying `bibtex-autokey-year-length'."    "Return year field contents as a string obeying `bibtex-autokey-year-length'."
# Line 2114  Return the result as a string" Line 2126  Return the result as a string"
2126           (bibtex-autokey-get-field "title"           (bibtex-autokey-get-field "title"
2127                                     bibtex-autokey-titleword-change-strings)))                                     bibtex-autokey-titleword-change-strings)))
2128      ;; ignore everything past a terminator      ;; ignore everything past a terminator
2129      (dolist (terminator bibtex-autokey-title-terminators)      (if (string-match bibtex-autokey-title-terminators titlestring)
2130        (if (string-match terminator titlestring)          (setq titlestring (substring titlestring 0 (match-beginning 0))))
           (setq titlestring (substring titlestring 0 (match-beginning 0)))))  
2131      ;; gather words from titlestring into a list.  Ignore      ;; gather words from titlestring into a list.  Ignore
2132      ;; specific words and use only a specific amount of words.      ;; specific words and use only a specific amount of words.
2133      (let ((counter 0)      (let ((counter 0)
# Line 2135  Return the result as a string" Line 2146  Return the result as a string"
2146                                                           "\\)\\'") word)))                                                           "\\)\\'") word)))
2147                      (setq lst (cdr lst)))                      (setq lst (cdr lst)))
2148                    lst)                    lst)
2149            (setq word (funcall bibtex-autokey-titleword-case-convert word)            (setq counter (1+ counter))
                 counter (1+ counter))  
2150            (if (or (not (numberp bibtex-autokey-titlewords))            (if (or (not (numberp bibtex-autokey-titlewords))
2151                    (< counter bibtex-autokey-titlewords))                    (< counter bibtex-autokey-titlewords))
2152                (push word titlewords)                (push word titlewords)
# Line 2161  and `bibtex-autokey-titleword-length'." Line 2171  and `bibtex-autokey-titleword-length'."
2171        (setq alist (cdr alist)))        (setq alist (cdr alist)))
2172      (if alist      (if alist
2173          (cdar alist)          (cdar alist)
2174        (bibtex-autokey-abbrev titleword        (funcall bibtex-autokey-titleword-case-convert
2175                               bibtex-autokey-titleword-length))))                 (bibtex-autokey-abbrev titleword bibtex-autokey-titleword-length)))))
2176    
2177  (defun bibtex-generate-autokey ()  (defun bibtex-generate-autokey ()
2178    "Generate automatically a key from the author/editor and the title field.    "Generate automatically a key for a BibTeX entry.
2179  This will only work for entries where each field begins on a separate line.  Use the author/editor, the year and the title field.
2180  The generation algorithm works as follows:  The algorithm works as follows.
2181   1. Use the value of `bibtex-autokey-prefix-string' as a prefix.  
2182   2. If there is a non-empty author (preferred) or editor field,  The name part:
2183      use it as the name part of the key.   1. Use the author or editor field to generate the name part of the key.
2184   3. Change any substring found in   2. Change the content of the name field according to
2185      `bibtex-autokey-name-change-strings' to the corresponding new      `bibtex-autokey-name-change-strings' (see there for further detail).
2186      one (see documentation of this variable for further detail).   3. Use the first `bibtex-autokey-names' names in the name field.  If there
2187   4. For every of at least first `bibtex-autokey-names' names in      are up to `bibtex-autokey-names' + `bibtex-autokey-names-stretch' names,
2188      the name field, determine the last name.  If there are maximal      use all names.
2189      `bibtex-autokey-names' + `bibtex-autokey-names-stretch'   4. Use only the last names to form the name part.  From these last names,
2190      names, all names are used.      take at least `bibtex-autokey-name-length' characters (truncate only
2191   5. From every last name, take at least `bibtex-autokey-name-length'      after a consonant or at a word end).
2192      characters (abort only after a consonant or at a word end).   5. Convert all last names using the function
  6. Convert all last names according to the conversion function  
2193      `bibtex-autokey-name-case-convert'.      `bibtex-autokey-name-case-convert'.
2194   7. Build the name part of the key by concatenating all   6. Build the name part of the key by concatenating all abbreviated last
2195      abbreviated last names with the string      names with the string `bibtex-autokey-name-separator' between any two.
2196      `bibtex-autokey-name-separator' between any two.  If there are      If there are more names in the name field than names used in the name
2197      more names than are used in the name part, prepend the string      part, append the string `bibtex-autokey-additional-names'.
2198      contained in `bibtex-autokey-additional-names'.  
2199   8. Build the year part of the key by truncating the contents of  The year part:
2200      the year field to the rightmost `bibtex-autokey-year-length'   1. Build the year part of the key by truncating the content of the year
2201      digits (useful values are 2 and 4).  If the year field (or any      field to the rightmost `bibtex-autokey-year-length' digits (useful
2202      other field required to generate the key) is absent, but the entry      values are 2 and 4).
2203      has a valid crossref field and the variable   2. If the year field (or any other field required to generate the key)
2204        is absent, but the entry has a valid crossref field and
2205      `bibtex-autokey-use-crossref' is non-nil, use the field of the      `bibtex-autokey-use-crossref' is non-nil, use the field of the
2206      crossreferenced entry instead.      crossreferenced entry instead.
2207   9. For the title part of the key change the contents of the  
2208      title field of the entry according to  The title part
2209      `bibtex-autokey-titleword-change-strings' to the   1. Change the content of the title field according to
2210      corresponding new one (see documentation of this variable for      `bibtex-autokey-titleword-change-strings' (see there for further detail).
2211      further detail).   2. Truncate the title before the first match of
2212  10. Abbreviate the result to the string up to (but not including)      `bibtex-autokey-title-terminators' and delete those words which appear
2213      the first occurrence of a regexp matched by the items of      in `bibtex-autokey-titleword-ignore'.  Build the title part using the
2214      `bibtex-autokey-title-terminators' and delete those words which      first `bibtex-autokey-titlewords' words from this truncated title.
2215      appear in `bibtex-autokey-titleword-ignore'.      If the truncated title ends after up to `bibtex-autokey-titlewords' +
2216      Build the title part of the key by using at least the first      `bibtex-autokey-titlewords-stretch' words, use all words from the
2217      `bibtex-autokey-titlewords' words from this      truncated title.
2218      abbreviated title.  If the abbreviated title ends after   3. For every title word that appears in `bibtex-autokey-titleword-abbrevs'
2219      maximal `bibtex-autokey-titlewords' +      use the corresponding abbreviation (see documentation of this variable
2220      `bibtex-autokey-titlewords-stretch' words, all      for further detail).
2221      words from the abbreviated title are used.   4. From every title word not generated by an abbreviation, take at least
2222  11. Convert all used titlewords according to the conversion function      `bibtex-autokey-titleword-length' characters (truncate only after
2223        a consonant or at a word end).
2224     5. Convert all title words using the function
2225      `bibtex-autokey-titleword-case-convert'.      `bibtex-autokey-titleword-case-convert'.
2226  12. For every used title word that appears in   6. Build the title part by concatenating all abbreviated title words with
2227      `bibtex-autokey-titleword-abbrevs' use the corresponding      the string `bibtex-autokey-titleword-separator' between any two.
2228      abbreviation (see documentation of this variable for further  
2229      detail).  Concatenate the key:
2230  13. From every title word not generated by an abbreviation, take   1. Concatenate `bibtex-autokey-prefix-string', the name part, the year
2231      at least `bibtex-autokey-titleword-length' characters (abort      part and the title part.  If the name part and the year part are both
2232      only after a consonant or at a word end).      non-empty insert `bibtex-autokey-name-year-separator' between the two.
2233  14. Build the title part of the key by concatenating all      If the title part and the year (or name) part are non-empty, insert
2234      abbreviated title words with the string      `bibtex-autokey-year-title-separator' between the two.
2235      `bibtex-autokey-titleword-separator' between any two.   2. If `bibtex-autokey-before-presentation-function' is non-nil, it must be
2236  15. At least, to get the key, concatenate      a function taking one argument.  Call this function with the generated
2237      `bibtex-autokey-prefix-string', the name part, the year part      key as the argument.  Use the return value of this function (a string)
2238      and the title part with `bibtex-autokey-name-year-separator'      as the key.
2239      between the name part and the year part if both are non-empty   3. If `bibtex-autokey-edit-before-use' is non-nil, present the key in the
2240      and `bibtex-autokey-year-title-separator' between the year      minibuffer to the user for editing.  Insert the key given by the user."
     part and the title part if both are non-empty.  If the year  
     part is empty, but not the other two parts,  
     `bibtex-autokey-year-title-separator' is used as well.  
 16. If the value of `bibtex-autokey-before-presentation-function'  
     is non-nil, it must be a function taking one argument.  This  
     function is then called with the generated key as the  
     argument.  The return value of this function (a string) is  
     used as the key.  
 17. If the value of `bibtex-autokey-edit-before-use' is non-nil,  
     the key is then presented in the minibuffer to the user,  
     where it can be edited.  The key given by the user is then  
     used."  
2241    (let* ((names (bibtex-autokey-get-names))    (let* ((names (bibtex-autokey-get-names))
2242           (year (bibtex-autokey-get-year))           (year (bibtex-autokey-get-year))
2243           (title (bibtex-autokey-get-title))           (title (bibtex-autokey-get-title))
# Line 2257  The generation algorithm works as follow Line 2257  The generation algorithm works as follow
2257        autokey)))        autokey)))
2258    
2259    
2260  (defun bibtex-read-key (prompt &optional key)  (defun bibtex-global-key-alist ()
2261    "Read BibTeX key from minibuffer using PROMPT and default KEY."    "Return global key alist based on `bibtex-files'."
2262    (completing-read prompt bibtex-reference-keys    (if bibtex-files
2263                     nil nil key 'bibtex-key-history))        (apply 'append
2264                 (mapcar (lambda (buf)
2265                           (with-current-buffer buf bibtex-reference-keys))
2266                         (bibtex-files-expand t)))
2267        bibtex-reference-keys))
2268    
2269    (defun bibtex-read-key (prompt &optional key global)
2270      "Read BibTeX key from minibuffer using PROMPT and default KEY.
2271    If optional arg GLOBAL is non-nil, completion is based on the keys in
2272    `bibtex-reference-keys' of `bibtex-files',"
2273      (let (completion-ignore-case)
2274        (completing-read prompt (if global (bibtex-global-key-alist)
2275                                  bibtex-reference-keys)
2276                         nil nil key 'bibtex-key-history)))
2277    
2278    (defun bibtex-read-string-key (&optional key)
2279      "Read BibTeX string key from minibuffer using default KEY."
2280      (let ((completion-ignore-case t))
2281        (completing-read "String key: " bibtex-strings
2282                         nil nil key 'bibtex-key-history)))
2283    
2284  (defun bibtex-parse-keys (&optional abortable verbose)  (defun bibtex-parse-keys (&optional abortable verbose)
2285    "Set `bibtex-reference-keys' to the keys used in the whole buffer.    "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2286  Find both entry keys and crossref entries.  If ABORTABLE is non-nil abort on  Find both entry keys and crossref entries.  If ABORTABLE is non-nil abort
2287  user input.  If VERBOSE is non-nil gives messages about progress.  Return alist  on user input.  If VERBOSE is non-nil give messages about progress.
2288  of keys if parsing was completed, `aborted' otherwise."  Return alist of keys if parsing was completed, `aborted' otherwise."
2289    (let (ref-keys crossref-keys)    (let (ref-keys crossref-keys)
2290      (save-excursion      (save-excursion
2291        (save-match-data        (save-match-data
# Line 2325  of keys if parsing was completed, `abort Line 2344  of keys if parsing was completed, `abort
2344    
2345  (defun bibtex-parse-strings (&optional add abortable)  (defun bibtex-parse-strings (&optional add abortable)
2346    "Set `bibtex-strings' to the string definitions in the whole buffer.    "Set `bibtex-strings' to the string definitions in the whole buffer.
 The buffer might possibly be restricted.  
2347  If ADD is non-nil add the new strings to `bibtex-strings' instead of  If ADD is non-nil add the new strings to `bibtex-strings' instead of
2348  simply resetting it.  If ADD is an alist of strings, also add ADD to  simply resetting it.  If ADD is an alist of strings, also add ADD to
2349  `bibtex-strings'.  If ABORTABLE is non-nil abort on user input.  `bibtex-strings'.  If ABORTABLE is non-nil abort on user input.
# Line 2395  Use `bibtex-predefined-strings' and BibT Line 2413  Use `bibtex-predefined-strings' and BibT
2413  (defun bibtex-parse-buffers-stealthily ()  (defun bibtex-parse-buffers-stealthily ()
2414    "Parse buffer in the background during idle time.    "Parse buffer in the background during idle time.
2415  Called by `run-with-idle-timer'.  Whenever Emacs has been idle  Called by `run-with-idle-timer'.  Whenever Emacs has been idle
2416  for `bibtex-parse-keys-timeout' seconds, all BibTeX buffers (starting  for `bibtex-parse-keys-timeout' seconds, parse all BibTeX buffers
2417  with the current) are parsed."  which have been modified after last parsing.
2418    Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
2419    (save-excursion    (save-excursion
2420      (let ((buffers (buffer-list))      (let ((buffers (buffer-list))
2421            (strings-init (bibtex-string-files-init)))            (strings-init (bibtex-string-files-init)))
# Line 2417  with the current) are parsed." Line 2436  with the current) are parsed."
2436                    (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick)))))                    (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick)))))
2437          (setq buffers (cdr buffers))))))          (setq buffers (cdr buffers))))))
2438    
2439  (defun bibtex-files-expand (&optional current)  (defun bibtex-files-expand (&optional current force)
2440    "Return an expanded list of BibTeX buffers based on `bibtex-files'.    "Return an expanded list of BibTeX buffers based on `bibtex-files'.
2441  Initialize in these buffers `bibtex-reference-keys' if not yet set.  Initialize in these buffers `bibtex-reference-keys' if not yet set.
2442  List includes current buffer if CURRENT is non-nil."  List of BibTeX buffers includes current buffer if CURRENT is non-nil.
2443    If FORCE is non-nil, (re)initialize `bibtex-reference-keys' even if
2444    already set."
2445    (let ((file-path (split-string (or bibtex-file-path default-directory) ":+"))    (let ((file-path (split-string (or bibtex-file-path default-directory) ":+"))
2446          file-list dir-list buffer-list)          file-list dir-list buffer-list)
2447      (dolist (file bibtex-files)      (dolist (file bibtex-files)
# Line 2454  List includes current buffer if CURRENT Line 2475  List includes current buffer if CURRENT
2475        (when (file-readable-p file)        (when (file-readable-p file)
2476          (push (find-file-noselect file) buffer-list)          (push (find-file-noselect file) buffer-list)
2477          (with-current-buffer (car buffer-list)          (with-current-buffer (car buffer-list)
2478            (unless (listp bibtex-reference-keys)            (if (or force (not (listp bibtex-reference-keys)))
2479              (bibtex-parse-keys)))))              (bibtex-parse-keys)))))
2480      (cond ((and current (not (memq (current-buffer) buffer-list)))      (cond ((and current (not (memq (current-buffer) buffer-list)))
2481             (push (current-buffer) buffer-list))             (push (current-buffer) buffer-list)
2482               (if force (bibtex-parse-keys)))
2483            ((and (not current) (memq (current-buffer) buffer-list))            ((and (not current) (memq (current-buffer) buffer-list))
2484             (setq buffer-list (delq (current-buffer) buffer-list))))             (setq buffer-list (delq (current-buffer) buffer-list))))
2485      buffer-list))      buffer-list))
2486    
2487  (defun bibtex-complete-internal (completions)  (defun bibtex-complete-internal (completions)
2488    "Complete word fragment before point to longest prefix of COMPLETIONS.    "Complete word fragment before point to longest prefix of COMPLETIONS.
2489  COMPLETIONS should be a list of strings.  If point is not after the part  COMPLETIONS is an alist of strings.  If point is not after the part
2490  of a word, all strings are listed.  Return completion."  of a word, all strings are listed.  Return completion."
2491    (let* ((case-fold-search t)    (let* ((case-fold-search t)
2492           (beg (save-excursion           (beg (save-excursion
# Line 2491  of a word, all strings are listed.  Retu Line 2513  of a word, all strings are listed.  Retu
2513             ;; return value is handled by choose-completion-string-functions             ;; return value is handled by choose-completion-string-functions
2514             nil))))             nil))))
2515    
2516  (defun bibtex-complete-string-cleanup (str strings-alist)  (defun bibtex-complete-string-cleanup (str compl)
2517    "Cleanup after inserting string STR.    "Cleanup after inserting string STR.
2518  Remove enclosing field delimiters for string STR.  Display message with  Remove enclosing field delimiters for STR.  Display message with
2519  expansion of STR using expansion list STRINGS-ALIST."  expansion of STR using expansion list COMPL."
2520    (let ((pair (if (stringp str)    (save-excursion
2521                    (assoc-string str strings-alist t))))      (bibtex-inside-field)
2522      (when pair      (let ((bounds (bibtex-enclosing-field))
2523        (if (cdr pair)            (abbr (cdr (if (stringp str)
2524            (message "Abbreviation for `%s'" (cdr pair)))                           (assoc-string str compl t)))))
2525        (save-excursion        (if abbr (message "Abbreviation for `%s'" abbr))
2526          (bibtex-inside-field)        (goto-char (bibtex-start-of-text-in-field bounds))
2527          (let ((bounds (bibtex-enclosing-field)))        (let ((boundaries (bibtex-parse-field-string)))
2528            (goto-char (bibtex-start-of-text-in-field bounds))          (if (and boundaries
2529            (let ((boundaries (bibtex-parse-field-string)))                   (equal (cdr boundaries)
2530              (if (and boundaries                          (bibtex-end-of-text-in-field bounds)))
2531                       (equal (cdr boundaries)              (bibtex-remove-delimiters))))))
                             (bibtex-end-of-text-in-field bounds)))  
                 (bibtex-remove-delimiters))))))))  
2532    
2533  (defun bibtex-complete-key-cleanup (key)  (defun bibtex-complete-crossref-cleanup (key)
2534    "Display summary message on entry KEY after completion of a crossref key.    "Display summary message on entry KEY after completion of a crossref key.
2535  Use `bibtex-summary-function' to generate summary."  Use `bibtex-summary-function' to generate summary."
2536    (save-excursion    (save-excursion
2537      ;; Don't do anything if we completed the key of an entry.      (if (and (stringp key)
2538      (let ((pnt (bibtex-beginning-of-entry)))               (bibtex-find-entry key t))
2539        (if (and (stringp key)          (message "Ref: %s" (funcall bibtex-summary-function key)))))
                (bibtex-find-entry key)  
                (/= pnt (point)))  
           (message "Ref: %s" (funcall bibtex-summary-function key))))))  
2540    
2541  (defun bibtex-copy-summary-as-kill (key)  (defun bibtex-copy-summary-as-kill ()
2542    "Push summery of BibTeX entry KEY to kill ring.    "Push summery of current BibTeX entry to kill ring.
2543  Use `bibtex-summary-function' to generate summary."  Use `bibtex-summary-function' to generate summary."
2544    (interactive    (interactive)
2545     (list (bibtex-read-key    (let ((key (save-excursion
2546            "Key: " (save-excursion                 (bibtex-beginning-of-entry)
2547                      (bibtex-beginning-of-entry)                 (if (looking-at bibtex-entry-maybe-empty-head)
2548                      (when (re-search-forward bibtex-entry-head nil t)                     (bibtex-key-in-head)
2549                        (bibtex-key-in-head))))))                   (error "No key found")))))
2550    (kill-new (message "%s" (funcall bibtex-summary-function key))))      (kill-new (message "%s" (funcall bibtex-summary-function key)))))
2551    
2552  (defun bibtex-summary (key)  (defun bibtex-summary (key)
2553    "Return summary of BibTeX entry KEY.    "Return summary of BibTeX entry KEY.
2554  Used as default value of `bibtex-summary-function'."  Used as default value of `bibtex-summary-function'."
2555    ;; It would be neat to customize this function.  How?    ;; It would be neat to customize this function.  How?
2556    (save-excursion    (save-excursion
2557      (if (bibtex-find-entry key)      (if (bibtex-find-entry key t)
2558          (let* ((bibtex-autokey-name-case-convert 'identity)          (let* ((bibtex-autokey-name-case-convert 'identity)
2559                 (bibtex-autokey-name-length 'infty)                 (bibtex-autokey-name-length 'infty)
2560                 (bibtex-autokey-names 1)                 (bibtex-autokey-names 1)
# Line 2563  Used as default value of `bibtex-summary Line 2580  Used as default value of `bibtex-summary
2580                       `((" " . ,names) (" " . ,year) (": " . ,title)                       `((" " . ,names) (" " . ,year) (": " . ,title)
2581                         (", " . ,journal) (" " . ,volume) (":" . ,pages))                         (", " . ,journal) (" " . ,volume) (":" . ,pages))
2582                       ""))                       ""))
2583        (error "Key `%s' not found." key))))        (error "Key `%s' not found" key))))
2584    
2585  (defun bibtex-pop (arg direction)  (defun bibtex-pop (arg direction)
2586    "Fill current field from the ARG'th same field's text in DIRECTION.    "Fill current field from the ARGth same field's text in DIRECTION.
2587  Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."  Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
2588    (let (bibtex-help-message)    (let (bibtex-help-message)
2589      (bibtex-find-text nil))      (bibtex-find-text))
2590    (save-excursion    (save-excursion
2591      ;; parse current field      ;; parse current field
2592      (bibtex-inside-field)      (bibtex-inside-field)
# Line 2613  Generic function used by `bibtex-pop-pre Line 2630  Generic function used by `bibtex-pop-pre
2630            (delete-region start-old-text stop-old-text)            (delete-region start-old-text stop-old-text)
2631            (insert new-text)))))            (insert new-text)))))
2632    (let (bibtex-help-message)    (let (bibtex-help-message)
2633      (bibtex-find-text nil))      (bibtex-find-text))
2634    (setq this-command 'bibtex-pop))    (setq this-command 'bibtex-pop))
2635    
2636  (defun bibtex-beginning-of-field ()  (defun bibtex-beginning-of-field ()
# Line 2626  begins at the beginning of a line.  We u Line 2643  begins at the beginning of a line.  We u
2643        (re-search-backward field-reg nil t))))        (re-search-backward field-reg nil t))))
2644    
2645  (defun bibtex-font-lock-url (bound)  (defun bibtex-font-lock-url (bound)
2646    "Font-lock for URLs."    "Font-lock for URLs.  BOUND limits the search."
2647    (let ((case-fold-search t)    (let ((case-fold-search t)
2648          (pnt (point))          (pnt (point))
2649          field bounds start end found)          field bounds start end found)
# Line 2657  begins at the beginning of a line.  We u Line 2674  begins at the beginning of a line.  We u
2674      found))      found))
2675    
2676  (defun bibtex-font-lock-crossref (bound)  (defun bibtex-font-lock-crossref (bound)
2677    "Font-lock for crossref fields."    "Font-lock for crossref fields.  BOUND limits the search."
2678    (let ((case-fold-search t)    (let ((case-fold-search t)
2679          (pnt (point))          (pnt (point))
2680          (crossref-reg (concat "^[ \t]*crossref[ \t]*=[ \t\n]*"          (crossref-reg (concat "^[ \t]*crossref[ \t]*=[ \t\n]*"
# Line 2690  begins at the beginning of a line.  We u Line 2707  begins at the beginning of a line.  We u
2707    'help-echo (purecopy "mouse-2, RET: follow crossref"))    'help-echo (purecopy "mouse-2, RET: follow crossref"))
2708    
2709  (defun bibtex-button (beg end type &rest args)  (defun bibtex-button (beg end type &rest args)
2710      "Make a BibTeX button from BEG to END of type TYPE in the current buffer."
2711    (make-text-button beg end 'type type 'bibtex-args args))    (make-text-button beg end 'type type 'bibtex-args args))
2712    
2713    
# Line 2701  begins at the beginning of a line.  We u Line 2719  begins at the beginning of a line.  We u
2719    
2720  General information on working with BibTeX mode:  General information on working with BibTeX mode:
2721    
2722  You should use commands such as \\[bibtex-Book] to get a template for a  Use commands such as \\[bibtex-Book] to get a template for a specific entry.
2723  specific entry.  You should then fill in all desired fields using  Then fill in all desired fields using \\[bibtex-next-field] to jump from field
2724  \\[bibtex-next-field] to jump from field to field.  After having filled  to field.  After having filled in all desired fields in the entry, clean the
2725  in all desired fields in the entry, you should clean the new entry  new entry with the command \\[bibtex-clean-entry].
 with the command \\[bibtex-clean-entry].  
2726    
2727  Some features of BibTeX mode are available only by setting the variable  Some features of BibTeX mode are available only by setting the variable
2728  `bibtex-maintain-sorted-entries' to non-nil.  However, then BibTeX mode will  `bibtex-maintain-sorted-entries' to non-nil.  However, then BibTeX mode will
# Line 2722  Special information: Line 2739  Special information:
2739    
2740  A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.  A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
2741    
2742  The optional fields start with the string OPT, and are thus ignored by BibTeX.  The names of optional fields start with the string OPT, and are thus ignored
2743  Alternatives from which only one is required start with the string ALT.  by BibTeX.  The names of alternative fields from which only one is required
2744  The OPT or ALT string may be removed from a field with \\[bibtex-remove-OPT-or-ALT].  start with the string ALT.  The OPT or ALT string may be removed from
2745    the name of a field with \\[bibtex-remove-OPT-or-ALT].
2746  \\[bibtex-make-field] inserts a new field after the current one.  \\[bibtex-make-field] inserts a new field after the current one.
2747  \\[bibtex-kill-field] kills the current field entirely.  \\[bibtex-kill-field] kills the current field entirely.
2748  \\[bibtex-yank] yanks the last recently killed field after the current field.  \\[bibtex-yank] yanks the last recently killed field after the current field.
2749  \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.  \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
2750   \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.  \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
2751    \\[bibtex-find-text] moves point to the end of the current field.
2752    \\[bibtex-complete] completes word fragment before point according to context.
2753    
2754  The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT  The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2755  from all non-empty optional or alternative fields, checks that no required  from the names of all non-empty optional or alternative fields, checks that
2756  fields are empty, and does some formatting dependent on the value of  no required fields are empty, and does some formatting dependent on the value
2757  `bibtex-entry-format'.  of `bibtex-entry-format'.  Furthermore, it can automatically generate a key
2758    for the BibTeX entry, see `bibtex-generate-autokey'.
2759  Note: some functions in BibTeX mode depend on entries being in a special  Note: some functions in BibTeX mode depend on entries being in a special
2760  format (all fields beginning on separate lines), so it is usually a bad  format (all fields beginning on separate lines), so it is usually a bad
2761  idea to remove `realign' from `bibtex-entry-format'.  idea to remove `realign' from `bibtex-entry-format'.
2762    
2763  Use \\[bibtex-find-text] to position the cursor at the end of the current field.  BibTeX mode supports Imenu and hideshow minor mode (`hs-minor-mode').
 Use \\[bibtex-next-field] to move to end of the next field.  
2764    
2765  The following may be of interest as well:  ----------------------------------------------------------
2766    Entry to BibTeX mode calls the value of `bibtex-mode-hook'
2767    Functions:  if that value is non-nil.
     `bibtex-entry'  
     `bibtex-kill-entry'  
     `bibtex-yank-pop'  
     `bibtex-pop-previous'  
     `bibtex-pop-next'  
     `bibtex-complete'  
     `bibtex-print-help-message'  
     `bibtex-generate-autokey'  
     `bibtex-beginning-of-entry'  
     `bibtex-end-of-entry'  
     `bibtex-reposition-window'  
     `bibtex-mark-entry'  
     `bibtex-ispell-abstract'  
     `bibtex-ispell-entry'  
     `bibtex-narrow-to-entry'  
     `bibtex-sort-buffer'  
     `bibtex-validate'  
     `bibtex-count'  
     `bibtex-fill-entry'  
     `bibtex-reformat'  
     `bibtex-convert-alien'  
   
   Variables:  
     `bibtex-field-delimiters'  
     `bibtex-include-OPTcrossref'  
     `bibtex-include-OPTkey'  
     `bibtex-user-optional-fields'  
     `bibtex-entry-format'  
     `bibtex-sort-ignore-string-entries'  
     `bibtex-maintain-sorted-entries'  
     `bibtex-entry-field-alist'  
     `bibtex-predefined-strings'  
     `bibtex-string-files'  
   
 ---------------------------------------------------------  
 Entry to BibTeX mode calls the value of `bibtex-mode-hook' if that value is  
 non-nil.  
2768    
2769  \\{bibtex-mode-map}"  \\{bibtex-mode-map}"
2770    (interactive)    (interactive)
# Line 2824  non-nil. Line 2807  non-nil.
2807                (set-mark (bibtex-end-of-entry))                (set-mark (bibtex-end-of-entry))
2808                (bibtex-beginning-of-entry)))))                (bibtex-beginning-of-entry)))))
2809    (setq imenu-generic-expression    (setq imenu-generic-expression
2810          (list (list nil bibtex-entry-head bibtex-key-in-head)))          (list (list nil bibtex-entry-head bibtex-key-in-head))
2811            imenu-case-fold-search t)
2812    (make-local-variable 'choose-completion-string-functions)    (make-local-variable 'choose-completion-string-functions)
   (setq imenu-case-fold-search t)  
2813    ;; XEmacs needs easy-menu-add, Emacs does not care    ;; XEmacs needs easy-menu-add, Emacs does not care
2814    (easy-menu-add bibtex-edit-menu)    (easy-menu-add bibtex-edit-menu)
2815    (easy-menu-add bibtex-entry-menu)    (easy-menu-add bibtex-entry-menu)
# Line 2836  non-nil. Line 2819  non-nil.
2819    "Return list of allowed fields for entry ENTRY-TYPE.    "Return list of allowed fields for entry ENTRY-TYPE.
2820  More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),  More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),
2821  where REQUIRED and OPTIONAL are lists of the required and optional field  where REQUIRED and OPTIONAL are lists of the required and optional field
2822  names for ENTRY-TYPE according to `bibtex-entry-field-alist'."  names for ENTRY-TYPE according to `bibtex-entry-field-alist',
2823    `bibtex-include-OPTkey', `bibtex-include-OPTcrossref',
2824    and `bibtex-user-optional-fields'."
2825    (let ((e (assoc-string entry-type bibtex-entry-field-alist t))    (let ((e (assoc-string entry-type bibtex-entry-field-alist t))
2826          required optional)          required optional)
2827      (unless e      (unless e
# Line 2862  names for ENTRY-TYPE according to `bibte Line 2847  names for ENTRY-TYPE according to `bibte
2847    
2848  (defun bibtex-entry (entry-type)  (defun bibtex-entry (entry-type)
2849    "Insert a new BibTeX entry of type ENTRY-TYPE.    "Insert a new BibTeX entry of type ENTRY-TYPE.
2850  After insertion it calls the functions in `bibtex-add-entry-hook'."  After insertion call the value of `bibtex-add-entry-hook' if that value
2851    (interactive (let* ((completion-ignore-case t)  is non-nil."
2852                        (e-t (completing-read    (interactive
2853                              "Entry Type: "     (let ((completion-ignore-case t))
2854                              bibtex-entry-field-alist       (list (completing-read "Entry Type: " bibtex-entry-field-alist
2855                              nil t nil 'bibtex-entry-type-history)))                              nil t nil 'bibtex-entry-type-history))))
                  (list e-t)))  
2856    (let ((key (if bibtex-maintain-sorted-entries    (let ((key (if bibtex-maintain-sorted-entries
2857                   (bibtex-read-key (format "%s key: " entry-type))))                   (bibtex-read-key (format "%s key: " entry-type))))
2858          (field-list (bibtex-field-list entry-type)))          (field-list (bibtex-field-list entry-type)))
# Line 2893  After insertion it calls the functions i Line 2877  After insertion it calls the functions i
2877  (defun bibtex-entry-update ()  (defun bibtex-entry-update ()
2878    "Update an existing BibTeX entry.    "Update an existing BibTeX entry.
2879  In the BibTeX entry at point, make new fields for those items that may occur  In the BibTeX entry at point, make new fields for those items that may occur
2880  according to `bibtex-entry-field-alist', but are not yet present."  according to `bibtex-field-list', but are not yet present."
2881    (interactive)    (interactive)
2882    (save-excursion    (save-excursion
2883      (bibtex-beginning-of-entry)      (bibtex-beginning-of-entry)
# Line 2928  Move point to the end of the last field. Line 2912  Move point to the end of the last field.
2912      alist))      alist))
2913    
2914  (defun bibtex-autofill-entry ()  (defun bibtex-autofill-entry ()
2915    "Try to fill fields based on surrounding entries."    "Try to fill fields of current BibTeX entry based on neighboring entries.
2916    The current entry must have a key.  Determine the neighboring entry
2917    \(previouse or next\) whose key is more similar to the key of the current
2918    entry.  For all empty fields of the current entry insert the corresponding
2919    field contents of the neighboring entry.  Finally try to update the text
2920    based on the difference between the keys of the neighboring and the current
2921    entry (for example, the year parts of the keys)."
2922    (interactive)    (interactive)
2923    (undo-boundary)       ;So you can easily undo it, if it didn't work right.    (undo-boundary)       ;So you can easily undo it, if it didn't work right.
2924    (bibtex-beginning-of-entry)    (bibtex-beginning-of-entry)
# Line 2945  Move point to the end of the last field. Line 2935  Move point to the end of the last field.
2935        (save-excursion        (save-excursion
2936          (goto-char (1- (match-beginning 0)))          (goto-char (1- (match-beginning 0)))
2937          (bibtex-beginning-of-entry)          (bibtex-beginning-of-entry)
2938          (when (and          (if (and (looking-at bibtex-entry-head)
2939                 (looking-at bibtex-entry-head)                   (bibtex-string= type (bibtex-type-in-head))
2940                 (bibtex-string= type (bibtex-type-in-head))                   ;; In case we found ourselves :-(
2941                 ;; In case we found ourselves :-(                   (not (equal key (setq tmp (bibtex-key-in-head)))))
2942                 (not (equal key (setq tmp (bibtex-key-in-head)))))            (setq other-key tmp
2943            (setq other-key tmp)                  other (point))))
           (setq other (point))))  
2944        (save-excursion        (save-excursion
2945          (bibtex-end-of-entry)          (bibtex-end-of-entry)
2946          (bibtex-skip-to-valid-entry)          (bibtex-skip-to-valid-entry)
2947          (when (and          (if (and (looking-at bibtex-entry-head)
2948                 (looking-at bibtex-entry-head)                   (bibtex-string= type (bibtex-type-in-head))
2949                 (bibtex-string= type (bibtex-type-in-head))                   ;; In case we found ourselves :-(
2950                 ;; In case we found ourselves :-(                   (not (equal key (setq tmp (bibtex-key-in-head))))
2951                 (not (equal key (setq tmp (bibtex-key-in-head))))                   (or (not other-key)
2952                 (or (not other-key)                       ;; Check which is the best match.
2953                     ;; Check which is the best match.                       (< (length (try-completion "" (list key other-key)))
2954                     (< (length (try-completion "" (list key other-key)))                          (length (try-completion "" (list key tmp))))))
2955                        (length (try-completion "" (list key tmp))))))              (setq other-key tmp
2956            (setq other-key tmp)                    other (point))))
           (setq other (point))))  
2957        ;; Then fill the new entry's fields with the chosen other entry.        ;; Then fill the new entry's fields with the chosen other entry.
2958        (when other        (when other
2959          (setq other (save-excursion (goto-char other) (bibtex-parse-entry)))          (setq other (save-excursion (goto-char other) (bibtex-parse-entry)))
# Line 3007  Move point to the end of the last field. Line 2995  Move point to the end of the last field.
2995            (message (nth 1 comment))            (message (nth 1 comment))
2996          (message "No comment available")))))          (message "No comment available")))))
2997    
2998  (defun bibtex-make-field (field &optional called-by-yank interactive)  (defun bibtex-make-field (field &optional move interactive)
2999    "Make a field named FIELD in current BibTeX entry.    "Make a field named FIELD in current BibTeX entry.
3000  FIELD is either a string or a list of the form  FIELD is either a string or a list of the form
3001  \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in  \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
3002  `bibtex-entry-field-alist'.  `bibtex-entry-field-alist'.
3003  If CALLED-BY-YANK is non-nil, don't insert delimiters.  If MOVE is non-nil, move point past the present field before making
3004  In that case, or when called interactively, also don't do (WHAT?)."  the new field.  If INTERACTIVE is non-nil, move point to the end of
3005    the new field.  Otherwise move point past the new field.
3006    MOVE and INTERACTIVE are t when called interactively."
3007    (interactive    (interactive
3008     (list (let ((completion-ignore-case t)     (list (let ((completion-ignore-case t)
3009                 (field-list (bibtex-field-list                 (field-list (bibtex-field-list
# Line 3023  In that case, or when called interactive Line 3013  In that case, or when called interactive
3013             (completing-read "BibTeX field name: "             (completing-read "BibTeX field name: "
3014                              (append (car field-list) (cdr field-list))                              (append (car field-list) (cdr field-list))
3015                              nil nil nil bibtex-field-history))                              nil nil nil bibtex-field-history))
3016           t))           t t))
3017    (unless (consp field)    (unless (consp field)
3018      (setq field (list field)))      (setq field (list field)))
3019    (if (or interactive called-by-yank)    (if move
3020        (let (bibtex-help-message)        (let (bibtex-help-message)
3021          (bibtex-find-text nil t t)          (bibtex-find-text)
3022          (if (looking-at "[}\"]")          (if (looking-at "[}\"]")
3023              (forward-char))))              (forward-char))))
3024    (insert ",\n")    (insert ",\n")
# Line 3042  In that case, or when called interactive Line 3032  In that case, or when called interactive
3032    (unless bibtex-align-at-equal-sign    (unless bibtex-align-at-equal-sign
3033      (indent-to-column (+ bibtex-entry-offset      (indent-to-column (+ bibtex-entry-offset
3034                           bibtex-text-indentation)))                           bibtex-text-indentation)))
   (unless called-by-yank (insert (bibtex-field-left-delimiter)))  
3035    (let ((init (nth 2 field)))    (let ((init (nth 2 field)))
3036      (cond ((stringp init)      (insert (cond ((stringp init) init)
3037             (insert init))                    ((fboundp init) (funcall init))
3038            ((fboundp init)                    (t (concat (bibtex-field-left-delimiter)
3039             (insert (funcall init)))))                               (bibtex-field-right-delimiter))))))
   (unless called-by-yank (insert (bibtex-field-right-delimiter)))  
3040    (when interactive    (when interactive
3041      (forward-char -1)      (forward-char -1)
3042      (bibtex-print-help-message)))      (bibtex-print-help-message)))
# Line 3083  Return the new location of point." Line 3071  Return the new location of point."
3071             (goto-char (bibtex-end-of-string bounds)))             (goto-char (bibtex-end-of-string bounds)))
3072            ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")            ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")
3073             (goto-char (match-end 0))             (goto-char (match-end 0))
3074             (cond ((looking-at "(")             (if (looking-at "[({]")
3075                    (unless (re-search-forward ")[ \t]*\n\n" nil 'move)                 (forward-sexp 1)
3076                      (setq err t)))               (setq err t)))
                  ((looking-at "{")  
                   (unless (re-search-forward "}[ \t]*\n\n" nil 'move)  
                     (setq err t)))  
                  (t  
                   (setq err t)))  
            (unless err  
              (goto-char (match-beginning 0))  
              (forward-char)))  
3077            (t            (t
3078             (if (interactive-p)             (if (interactive-p)
3079                 (message "Not on a known BibTeX entry."))                 (message "Not on a known BibTeX entry."))
# Line 3164  If mark is active it counts entries in r Line 3144  If mark is active it counts entries in r
3144                        (bibtex-end-of-entry))))                        (bibtex-end-of-entry))))
3145    
3146  (defun bibtex-entry-index ()  (defun bibtex-entry-index ()
3147    "Return the index of the BibTeX entry at point.  Move point.    "Return index of BibTeX entry head at or past position of point.
3148  The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting  The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting
3149  the entries of the BibTeX buffer.  Return nil if no entry found."  the entries of the BibTeX buffer.  CROSSREF-KEY is nil unless the value
3150    of `bibtex-maintain-sorted-entries' is `crossref'.  Move point to the end
3151    of the head of the entry found.  Return nil if no entry found."
3152    (let ((case-fold-search t))    (let ((case-fold-search t))
3153      (if (re-search-forward bibtex-entry-maybe-empty-head nil t)      (if (re-search-forward bibtex-entry-maybe-empty-head nil t)
3154          (let ((key (bibtex-key-in-head))          (let ((key (bibtex-key-in-head))
3155                ;; all entry names should be downcase (for ease of comparison)                ;; all entry names should be downcase (for ease of comparison)
3156                (entry-name (downcase (bibtex-type-in-head))))                (entry-name (downcase (bibtex-type-in-head))))
3157            ;; Don't search CROSSREF-KEY if we don't need it.            ;; Don't search CROSSREF-KEY if we don't need it.
3158            (if (equal bibtex-maintain-sorted-entries 'crossref)            (if (eq bibtex-maintain-sorted-entries 'crossref)
3159                (save-excursion                (let ((bounds (bibtex-search-forward-field
3160                  (bibtex-beginning-of-entry)                               "\\(OPT\\)?crossref" t)))
3161                  (let ((bounds (bibtex-search-forward-field                  (list key
3162                                 "\\(OPT\\)?crossref" t)))                        (if bounds (bibtex-text-in-field-bounds bounds t))
3163                    (list key                        entry-name))
                         (if bounds (bibtex-text-in-field-bounds bounds t))  
                         entry-name)))  
3164              (list key nil entry-name))))))              (list key nil entry-name))))))
3165    
3166  (defun bibtex-lessp (index1 index2)  (defun bibtex-lessp (index1 index2)
# Line 3190  The predicate depends on the variable `b Line 3170  The predicate depends on the variable `b
3170  If its value is nil use plain sorting."  If its value is nil use plain sorting."
3171    (cond ((not index1) (not index2)) ; indices can be nil    (cond ((not index1) (not index2)) ; indices can be nil
3172          ((not index2) nil)          ((not index2) nil)
3173          ((equal bibtex-maintain-sorted-entries 'crossref)          ((eq bibtex-maintain-sorted-entries 'crossref)
3174           (if (nth 1 index1)           (if (nth 1 index1)
3175               (if (nth 1 index2)               (if (nth 1 index2)
3176                   (or (string-lessp (nth 1 index1) (nth 1 index2))                   (or (string-lessp (nth 1 index1) (nth 1 index2))
# Line 3200  If its value is nil use plain sorting." Line 3180  If its value is nil use plain sorting."
3180             (if (nth 1 index2)             (if (nth 1 index2)
3181                 (string-lessp (nth 0 index1) (nth 1 index2))                 (string-lessp (nth 0 index1) (nth 1 index2))
3182               (string-lessp (nth 0 index1) (nth 0 index2)))))               (string-lessp (nth 0 index1) (nth 0 index2)))))
3183          ((equal bibtex-maintain-sorted-entries 'entry-class)          ((eq bibtex-maintain-sorted-entries 'entry-class)
3184           (let ((n1 (cdr (or (assoc (nth 2 index1) bibtex-sort-entry-class-alist)           (let ((n1 (cdr (or (assoc (nth 2 index1) bibtex-sort-entry-class-alist)
3185                              (assoc 'catch-all bibtex-sort-entry-class-alist)                              (assoc 'catch-all bibtex-sort-entry-class-alist)
3186                              '(nil . 1000))))  ; if there is nothing else                              '(nil . 1000))))  ; if there is nothing else
# Line 3210  If its value is nil use plain sorting." Line 3190  If its value is nil use plain sorting."
3190             (or (< n1 n2)             (or (< n1 n2)
3191                 (and (= n1 n2)                 (and (= n1 n2)
3192                      (string-lessp (car index1) (car index2))))))                      (string-lessp (car index1) (car index2))))))
3193          (t ; (equal bibtex-maintain-sorted-entries 'plain)          (t ; (eq bibtex-maintain-sorted-entries 'plain)
3194           (string-lessp (car index1) (car index2)))))           (string-lessp (car index1) (car index2)))))
3195    
3196  (defun bibtex-sort-buffer ()  (defun bibtex-sort-buffer ()
# Line 3232  will be ignored." Line 3212  will be ignored."
3212                 nil                         ; ENDKEY function                 nil                         ; ENDKEY function
3213                 'bibtex-lessp)))            ; PREDICATE                 'bibtex-lessp)))            ; PREDICATE
3214    
 (defun bibtex-find-entry-globally (key)  
   "Move point to the beginning of BibTeX entry named KEY in `bibtex-files'."  
   (interactive  
    (list (let (key-alist)  
            (dolist (buffer (bibtex-files-expand t))  
              (with-current-buffer buffer  
                (setq key-alist (append bibtex-reference-keys key-alist))))  
            (completing-read "Find key: " key-alist  
                             nil nil nil 'bibtex-key-history))))  
   (let ((buffer-list (bibtex-files-expand t))  
         buffer found)  
     (while (and (not found)  
                 (setq buffer (pop buffer-list)))  
       (with-current-buffer buffer  
         (if (cdr (assoc-string key bibtex-reference-keys))  
             (setq found t))))  
     (if found  
         (progn  
           (let ((same-window-buffer-names  
                  (cons (buffer-name buffer) same-window-buffer-names)))  
             (pop-to-buffer buffer))  
           (bibtex-find-entry key))  
       (message "Key `%s' not found" key))))  
   
3215  (defun bibtex-find-crossref (crossref-key &optional pnt split)  (defun bibtex-find-crossref (crossref-key &optional pnt split)
3216    "Move point to the beginning of BibTeX entry CROSSREF-KEY.    "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3217  Return position of entry if CROSSREF-KEY is found and nil otherwise.  If `bibtex-files' is non-nil, search all these files.
3218  If position of current entry is after CROSSREF-KEY an error is signaled.  Otherwise the search is limited to the current buffer.
3219  Optional arg PNT is the position of the referencing entry.  Return position of entry if CROSSREF-KEY is found or nil otherwise.
3220  If optional arg SPLIT is non-nil, split window so that both the referencing  If CROSSREF-KEY is in the same buffer like current entry but before it
3221  and the crossrefed entry are displayed.  an error is signaled.  Optional arg PNT is the position of the referencing
3222    entry. It defaults to position of point.  If optional arg SPLIT is non-nil,
3223    split window so that both the referencing and the crossrefed entry are
3224    displayed.
3225  If called interactively, CROSSREF-KEY defaults to crossref key of current  If called interactively, CROSSREF-KEY defaults to crossref key of current
3226  entry and SPLIT is t."  entry and SPLIT is t."
3227    (interactive    (interactive
# Line 3272  entry and SPLIT is t." Line 3231  entry and SPLIT is t."
3231              (let ((bounds (bibtex-search-forward-field "crossref" t)))              (let ((bounds (bibtex-search-forward-field "crossref" t)))
3232                (if bounds                (if bounds
3233                    (bibtex-text-in-field-bounds bounds t))))))                    (bibtex-text-in-field-bounds bounds t))))))
3234       (list (bibtex-read-key "Find crossref key: " crossref-key) (point) t)))       (list (bibtex-read-key "Find crossref key: " crossref-key t)
3235    (let ((pos (save-excursion (bibtex-find-entry crossref-key))))             (point) t)))
3236      (unless pnt (setq pnt (point)))    (let (buffer pos eqb)
3237        (save-excursion
3238          (setq pos (bibtex-find-entry crossref-key t)
3239                buffer (current-buffer)))
3240        (setq eqb (eq buffer (current-buffer)))
3241      (cond ((not pos)      (cond ((not pos)
3242             (message "Crossref key `%s' not found" crossref-key))             (if split (message "Crossref key `%s' not found" crossref-key)))
3243            (split            (split ; called (quasi) interactively
3244               (unless pnt (setq pnt (point)))
3245             (goto-char pnt)             (goto-char pnt)
3246             (select-window (split-window))             (if eqb (select-window (split-window))
3247                 (pop-to-buffer buffer))
3248             (goto-char pos)             (goto-char pos)
3249             (beginning-of-line)             (beginning-of-line)
3250             (set-window-start (selected-window) (point))             (set-window-start (selected-window) (point))
3251             (if (> pnt pos)             (if (and eqb (> pnt pos))
3252                 (error "The referencing entry must preceed the crossrefed entry!")))                 (error "The referencing entry must preceed the crossrefed entry!")))
3253            ((> pnt pos)            ;; `bibtex-find-crossref' is called noninteractively during
3254                 (error "The referencing entry must preceed the crossrefed entry!"))            ;; clean-up of an entry.  Then it is not possible to check
3255            (t (goto-char pos)))            ;; whether the current entry and the crossrefed entry have
3256              ;; the correct sorting order.
3257              (eqb (goto-char pos))
3258              (t (set-buffer buffer) (goto-char pos)))
3259      pos))      pos))
3260    
3261  (defun bibtex-find-entry (key &optional start)  (defun bibtex-find-entry (key &optional global start display)
3262    "Move point to the beginning of BibTeX entry named KEY.    "Move point to the beginning of BibTeX entry named KEY.
3263  Return position of entry if KEY is found or nil if not found.  Return position of entry if KEY is found or nil if not found.
3264    With prefix arg GLOBAL non-nil, search KEY in `bibtex-files'.
3265    Otherwise the search is limited to the current buffer.
3266  Optional arg START is buffer position where the search starts.  Optional arg START is buffer position where the search starts.
3267  If it is nil, start search at beginning of buffer.  If it is nil, start search at beginning of buffer.
3268  With prefix arg, the value of START is position of point."  If DISPLAY is non-nil, display the buffer containing KEY.
3269    (interactive (list (bibtex-read-key "Find key: ")  Otherwise, use `set-buffer'.  DISPLAY is t when called interactively."
3270                       (if current-prefix-arg (point))))    (interactive (list (bibtex-read-key "Find key: " nil current-prefix-arg)
3271    (let* (case-fold-search                       current-prefix-arg nil t))
3272           (pnt (save-excursion    (if (and global bibtex-files)
3273                  (goto-char (or start (point-min)))        (let ((buffer-list (bibtex-files-expand t))
3274                  (if (re-search-forward (concat "^[ \t]*\\("              buffer found)
3275                                                 bibtex-entry-type          (while (and (not found)
3276                                                 "\\)[ \t]*[({][ \t\n]*\\("                      (setq buffer (pop buffer-list)))
3277                                                 (regexp-quote key)            (with-current-buffer buffer
3278                                                 "\\)[ \t\n]*[,=]")              (if (cdr (assoc-string key bibtex-reference-keys))
3279                                         nil t)                  ;; `bibtex-find-entry' moves point if key found
3280                      (match-beginning 0)))))                  (setq found (bibtex-find-entry key)))))
3281      (cond (pnt          (cond ((and found display)
3282             (goto-char pnt))                 (let ((same-window-buffer-names
3283            ((interactive-p)                        (cons (buffer-name buffer) same-window-buffer-names)))
3284             (message "Key `%s' not found" key)))))                   (pop-to-buffer buffer)))
3285                  (found (set-buffer buffer))
3286                  (t (message "Key `%s' not found" key)))
3287            found)
3288    
3289        (let* (case-fold-search
3290               (pnt (save-excursion
3291                      (goto-char (or start (point-min)))
3292                      (if (re-search-forward (concat "^[ \t]*\\("
3293                                                     bibtex-entry-type
3294                                                     "\\)[ \t]*[({][ \t\n]*\\("
3295                                                     (regexp-quote key)
3296                                                     "\\)[ \t\n]*[,=]")
3297                                             nil t)
3298                          (match-beginning 0)))))
3299          (cond (pnt
3300                 (goto-char pnt))
3301                ((interactive-p)
3302                 (message "Key `%s' not found" key))))))
3303    
3304  (defun bibtex-prepare-new-entry (index)  (defun bibtex-prepare-new-entry (index)
3305    "Prepare a new BibTeX entry with index INDEX.    "Prepare a new BibTeX entry with index INDEX.
# Line 3437  Return t if test was successful, nil oth Line 3425  Return t if test was successful, nil oth
3425    
3426            ;; Check for duplicate keys in `bibtex-files'.            ;; Check for duplicate keys in `bibtex-files'.
3427            (bibtex-parse-keys)            (bibtex-parse-keys)
3428            (dolist (buffer (bibtex-files-expand))            ;; We don't want to be fooled by outdated `bibtex-reference-keys'.
3429              (dolist (key (with-current-buffer buffer            (dolist (buffer (bibtex-files-expand nil t))
3430                             ;; We don't want to be fooled by outdated              (dolist (key (with-current-buffer buffer bibtex-reference-keys))
                            ;; bibtex-reference-keys.  
                            (bibtex-parse-keys) bibtex-reference-keys))  
3431                (when (and (cdr key)                (when (and (cdr key)
3432                           (cdr (assoc-string (car key) bibtex-reference-keys)))                           (cdr (assoc-string (car key) bibtex-reference-keys)))
3433                  (bibtex-find-entry (car key))                  (bibtex-find-entry (car key))
# Line 3540  Return t if test was successful, nil oth Line 3526  Return t if test was successful, nil oth
3526    
3527  (defun bibtex-validate-globally (&optional strings)  (defun bibtex-validate-globally (&optional strings)
3528    "Check for duplicate keys in `bibtex-files'.    "Check for duplicate keys in `bibtex-files'.
3529  With prefix arg STRINGS, check for duplicate strings, too.  With optional prefix arg STRINGS, check for duplicate strings, too.
3530  Return t if test was successful, nil otherwise."  Return t if test was successful, nil otherwise."
3531    (interactive "P")    (interactive "P")
3532    (let ((buffer-list (bibtex-files-expand t))    (let ((buffer-list (bibtex-files-expand t))
# Line 3595  Return t if test was successful, nil oth Line 3581  Return t if test was successful, nil oth
3581        (message "No duplicate keys.")        (message "No duplicate keys.")
3582        t)))        t)))
3583    
3584  (defun bibtex-next-field (arg)  (defun bibtex-next-field (begin)
3585    "Find end of text of next BibTeX field; with ARG, to its beginning."    "Move point to end of text of next BibTeX field.
3586    With prefix BEGIN non-nil, move point to its beginning."
3587    (interactive "P")    (interactive "P")
3588    (bibtex-inside-field)    (bibtex-inside-field)
3589    (let ((start (point)))    (let ((start (point)))
# Line 3608  Return t if test was successful, nil oth Line 3595  Return t if test was successful, nil oth
3595         (goto-char start)         (goto-char start)
3596         (end-of-line)         (end-of-line)
3597         (forward-char))))         (forward-char))))
3598    (bibtex-find-text arg t))    (bibtex-find-text begin))
3599    
3600  (defun bibtex-find-text (arg &optional as-if-interactive no-error)  (defun bibtex-find-text (&optional begin noerror)
3601    "Go to end of text of current field; with ARG, go to beginning."    "Move point to end of text of current BibTeX field.
3602    (interactive "P\np")  With optional prefix BEGIN non-nil, move point to its beginning.
3603    (bibtex-inside-field)  Unless NOERROR is non-nil, an error is signaled if point is not
3604    (let ((bounds (bibtex-enclosing-field as-if-interactive)))  on a BibTeX field."
3605      (if bounds    (interactive "P")
3606          (progn (if arg    (let* ((pnt (point))
3607                     (progn (goto-char (bibtex-start-of-text-in-field bounds))           (_ (bibtex-inside-field))
3608                            (if (looking-at "[{\"]")           (bounds (bibtex-enclosing-field t)))
3609                                (forward-char)))      (beginning-of-line)
3610                   (goto-char (bibtex-end-of-text-in-field bounds))      (cond (bounds
3611                   (if (or (= (preceding-char) ?})             (if begin
3612                           (= (preceding-char) ?\"))                 (progn (goto-char (bibtex-start-of-text-in-field bounds))
3613                       (forward-char -1)))                        (if (looking-at "[{\"]")
3614                 (if bibtex-help-message                            (forward-char)))
3615                     (bibtex-print-help-message)))               (goto-char (bibtex-end-of-text-in-field bounds))
3616        (beginning-of-line)               (if (or (= (preceding-char) ?})
3617        (cond ((setq bounds (bibtex-parse-string))                       (= (preceding-char) ?\"))
3618               (goto-char (if arg                   (forward-char -1)))
3619                              (bibtex-start-of-text-in-string bounds)             (if bibtex-help-message
3620                            (bibtex-end-of-text-in-string bounds))))                 (bibtex-print-help-message)))
3621              ((looking-at bibtex-entry-maybe-empty-head)            ((setq bounds (bibtex-parse-string))
3622               (goto-char (if arg             (goto-char (if begin
3623                              (match-beginning bibtex-key-in-head)                            (1+ (bibtex-start-of-text-in-string bounds))
3624                            (match-end 0))))                          (1- (bibtex-end-of-text-in-string bounds)))))
3625              (t            ((looking-at bibtex-entry-maybe-empty-head)
3626               (unless no-error             (goto-char (if begin
3627                 (error "Not on BibTeX field")))))))                            (match-beginning bibtex-key-in-head)
3628                            (match-end 0))))
3629              (t
3630               (goto-char pnt)
3631               (unless noerror (error "Not on BibTeX field"))))))
3632    
3633  (defun bibtex-remove-OPT-or-ALT ()  (defun bibtex-remove-OPT-or-ALT ()
3634    "Remove the string starting optional/alternative fields.    "Remove the string starting optional/alternative fields.
# Line 3690  but do not actually kill it." Line 3681  but do not actually kill it."
3681             (beg (bibtex-start-of-field bounds)))             (beg (bibtex-start-of-field bounds)))
3682        (goto-char end)        (goto-char end)
3683        (skip-chars-forward " \t\n,")        (skip-chars-forward " \t\n,")
3684        (push (list 'field (bibtex-name-in-field bounds)        (push (list (bibtex-name-in-field bounds) nil
3685                    (bibtex-text-in-field-bounds bounds))                    (bibtex-text-in-field-bounds bounds))
3686              bibtex-field-kill-ring)              bibtex-field-kill-ring)
3687        (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)        (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
# Line 3703  but do not actually kill it." Line 3694  but do not actually kill it."
3694    (setq bibtex-last-kill-command 'field))    (setq bibtex-last-kill-command 'field))
3695    
3696  (defun bibtex-copy-field-as-kill ()  (defun bibtex-copy-field-as-kill ()
3697    "Copy the field at point to the kill ring."    "Copy the BibTeX field at point to the kill ring."
3698    (interactive)    (interactive)
3699    (bibtex-kill-field t))    (bibtex-kill-field t))
3700    
3701  (defun bibtex-kill-entry (&optional copy-only)  (defun bibtex-kill-entry (&optional copy-only)
3702    "Kill the entire enclosing BibTeX entry.    "Kill the entire enclosing BibTeX entry.
3703  With prefix arg COPY-ONLY the current entry to  With prefix arg COPY-ONLY, copy the current entry to `bibtex-entry-kill-ring',
3704  `bibtex-entry-kill-ring', but do not actually kill it."  but do not actually kill it."
3705    (interactive "P")    (interactive "P")
3706    (save-excursion    (save-excursion
3707      (let* ((case-fold-search t)      (let* ((case-fold-search t)
# Line 3720  With prefix arg COPY-ONLY the current en Line 3711  With prefix arg COPY-ONLY the current en
3711                              bibtex-entry-maybe-empty-head nil 'move)                              bibtex-entry-maybe-empty-head nil 'move)
3712                             (goto-char (match-beginning 0)))                             (goto-char (match-beginning 0)))
3713                         (point))))                         (point))))
3714        (push (list 'entry (buffer-substring-no-properties beg end))        (push (buffer-substring-no-properties beg end)
3715              bibtex-entry-kill-ring)              bibtex-entry-kill-ring)
3716        (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)        (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
3717            (setcdr (nthcdr (1- bibtex-entry-kill-ring-max)            (setcdr (nthcdr (1- bibtex-entry-kill-ring-max)
# Line 3742  More precisely, reinsert the field or en Line 3733  More precisely, reinsert the field or en
3733  With argument N, reinsert the Nth most recently killed BibTeX item.  With argument N, reinsert the Nth most recently killed BibTeX item.
3734  See also the command \\[bibtex-yank-pop]]."  See also the command \\[bibtex-yank-pop]]."
3735    (interactive "*p")    (interactive "*p")
3736    (bibtex-insert-current-kill (1- n))    (bibtex-insert-kill (1- n))
3737    (setq this-command 'bibtex-yank))    (setq this-command 'bibtex-yank))
3738    
3739  (defun bibtex-yank-pop (n)  (defun bibtex-yank-pop (n)
3740    "Replace just-yanked killed BibTeX item with a different.    "Replace just-yanked killed BibTeX item with a different item.
3741  This command is allowed only immediately after a `bibtex-yank' or a  This command is allowed only immediately after a `bibtex-yank' or a
3742  `bibtex-yank-pop'.  `bibtex-yank-pop'.  At such a time, the region contains a reinserted
3743  At such a time, the region contains a reinserted previously killed  previously killed BibTeX item.  `bibtex-yank-pop' deletes that item
3744  BibTeX item.  `bibtex-yank-pop' deletes that item and inserts in its  and inserts in its place a different killed BibTeX item.
 place a different killed BibTeX item.  
3745    
3746  With no argument, the previous kill is inserted.  With no argument, the previous kill is inserted.
3747  With argument N, insert the Nth previous kill.  With argument N, insert the Nth previous kill.
# Line 3765  comes the newest one." Line 3755  comes the newest one."
3755    (setq this-command 'bibtex-yank)    (setq this-command 'bibtex-yank)
3756    (let ((inhibit-read-only t))    (let ((inhibit-read-only t))
3757      (delete-region (point) (mark t))      (delete-region (point) (mark t))
3758      (bibtex-insert-current-kill n)))      (bibtex-insert-kill n)))
3759    
3760  (defun bibtex-empty-field ()  (defun bibtex-empty-field ()
3761    "Delete the text part of the current field, replace with empty text."    "Delete the text part of the current field, replace with empty text."
# Line 3807  At end of the cleaning process, the func Line 3797  At end of the cleaning process, the func
3797    ;; is called by bibtex-reformat    ;; is called by bibtex-reformat
3798    (interactive "P")    (interactive "P")
3799    (let ((case-fold-search t)    (let ((case-fold-search t)
3800          entry-type key)          (start (bibtex-beginning-of-entry))
3801      (bibtex-beginning-of-entry)          (_ (looking-at bibtex-entry-maybe-empty-head))
3802      (save-excursion          (entry-type (bibtex-type-in-head))
3803        (when (re-search-forward bibtex-entry-maybe-empty-head nil t)          (key (bibtex-key-in-head)))
         (setq entry-type (bibtex-type-in-head))  
         (setq key (bibtex-key-in-head))))  
3804      ;; formatting      ;; formatting
3805      (cond ((bibtex-string= entry-type "preamble")      (cond ((bibtex-string= entry-type "preamble")
3806             ;; (bibtex-format-preamble)             ;; (bibtex-format-preamble)
3807             (error "No clean up of @Preamble entries"))             (error "No clean up of @Preamble entries"))
3808            ((bibtex-string= entry-type "string"))            ((bibtex-string= entry-type "string")
3809               (setq entry-type 'string))
3810            ;; (bibtex-format-string)            ;; (bibtex-format-string)
3811            (t (bibtex-format-entry)))            (t (bibtex-format-entry)))
3812      ;; set key      ;; set key
# Line 3825  At end of the cleaning process, the func Line 3814  At end of the cleaning process, the func
3814        (setq key (bibtex-generate-autokey))        (setq key (bibtex-generate-autokey))
3815        ;; Sometimes bibtex-generate-autokey returns an empty string        ;; Sometimes bibtex-generate-autokey returns an empty string
3816        (if (or bibtex-autokey-edit-before-use (string= "" key))        (if (or bibtex-autokey-edit-before-use (string= "" key))
3817            (setq key (bibtex-read-key "Key to use: " key)))            (setq key (if (eq entry-type 'string)
3818        (re-search-forward bibtex-entry-maybe-empty-head)                          (bibtex-read-string-key key)
3819        (if (match-beginning bibtex-key-in-head)                        (bibtex-read-key "Key to use: " key))))
3820            (delete-region (match-beginning bibtex-key-in-head)        (save-excursion
3821                           (match-end bibtex-key-in-head)))          (re-search-forward (if (eq entry-type 'string)
3822        (insert key))                                 bibtex-string-maybe-empty-head
3823                                 bibtex-entry-maybe-empty-head))
3824            (if (match-beginning bibtex-key-in-head)
3825                (delete-region (match-beginning bibtex-key-in-head)
3826                               (match-end bibtex-key-in-head)))
3827            (insert key)))
3828    
3829      (unless called-by-reformat      (unless called-by-reformat
3830        (let* ((start (bibtex-beginning-of-entry))        (let* ((end (save-excursion
3831               (end (progn (bibtex-end-of-entry)                      (bibtex-end-of-entry)
3832                           (if (re-search-forward                      (if (re-search-forward
3833                                bibtex-entry-maybe-empty-head nil 'move)                           bibtex-entry-maybe-empty-head nil 'move)
3834                               (goto-char (match-beginning 0)))                          (goto-char (match-beginning 0)))
3835                           (point)))                      (point)))
3836               (entry (buffer-substring start end))               (entry (buffer-substring start end))
3837               ;; include the crossref key in index               ;; include the crossref key in index
3838               (index (let ((bibtex-maintain-sorted-entries 'crossref))               (index (let ((bibtex-maintain-sorted-entries 'crossref))
3839                        (goto-char start)                        (bibtex-entry-index))) ; moves point to end of head
                       (bibtex-entry-index)))  
3840               error)               error)
3841          ;; sorting          ;; sorting
3842          (if (and bibtex-maintain-sorted-entries          (if (and bibtex-maintain-sorted-entries
3843                   (not (and bibtex-sort-ignore-string-entries                   (not (and bibtex-sort-ignore-string-entries
3844                             (bibtex-string= entry-type "string"))))                             (eq entry-type 'string))))
3845              (progn              (progn
3846                (delete-region start end)                (delete-region start end)
3847                (setq error (not (bibtex-prepare-new-entry index)))                (setq error (not (bibtex-prepare-new-entry index))
3848                (insert entry)                      start (point)) ; update start
3849                (forward-char -1)                (save-excursion (insert entry)))
               (bibtex-beginning-of-entry) ; moves backward  
               (re-search-forward bibtex-entry-head))  
3850            (bibtex-find-entry key)            (bibtex-find-entry key)
3851            (setq error (or (/= (point) start)            (setq error (or (/= (point) start)
3852                            (bibtex-find-entry key end))))                            (bibtex-find-entry key nil end))))
3853          (if error          (if error
3854              (error "New inserted entry yields duplicate key"))              (error "New inserted entry yields duplicate key"))
3855          (dolist (buffer (bibtex-files-expand))          (dolist (buffer (bibtex-files-expand))
# Line 3867  At end of the cleaning process, the func Line 3858  At end of the cleaning process, the func
3858                  (error "Duplicate key in %s" (buffer-file-name)))))                  (error "Duplicate key in %s" (buffer-file-name)))))
3859    
3860          ;; Only update the list of keys if it has been built already.          ;; Only update the list of keys if it has been built already.
3861          (cond ((bibtex-string= entry-type "string")          (cond ((eq entry-type 'string)
3862                 (if (and (listp bibtex-strings)                 (if (and (listp bibtex-strings)
3863                          (not (assoc key bibtex-strings)))                          (not (assoc key bibtex-strings)))
3864                     (push (list key) bibtex-strings)))                     (push (cons key (bibtex-text-in-string
3865                                        (save-excursion (bibtex-parse-string)) t))
3866                               bibtex-strings)))
3867                ;; We have a normal entry.                ;; We have a normal entry.
3868                ((listp bibtex-reference-keys)                ((listp bibtex-reference-keys)
3869                 (cond ((not (assoc key bibtex-reference-keys))                 (cond ((not (assoc key bibtex-reference-keys))
# Line 4080  signaled if point is outside key or BibT Line 4073  signaled if point is outside key or BibT
4073                 (<= pnt (bibtex-end-of-text-in-field bounds)))                 (<= pnt (bibtex-end-of-text-in-field bounds)))
4074            (setq name (bibtex-name-in-field bounds t)            (setq name (bibtex-name-in-field bounds t)
4075                  compl (cond ((bibtex-string= name "crossref")                  compl (cond ((bibtex-string= name "crossref")
4076                               'key)                               ;; point is in crossref field
4077                                 'crossref-key)
4078                              ((bibtex-string= name "month")                              ((bibtex-string= name "month")
4079                                 ;; point is in month field
4080                               bibtex-predefined-month-strings)                               bibtex-predefined-month-strings)
4081                                ;; point is in other field
4082                              (t (if (listp bibtex-strings)                              (t (if (listp bibtex-strings)
4083                                     bibtex-strings                                     bibtex-strings
4084                                   ;; so that bibtex-complete-string-cleanup                                   ;; so that bibtex-complete-string-cleanup
# Line 4090  signaled if point is outside key or BibT Line 4086  signaled if point is outside key or BibT
4086                                   (bibtex-parse-strings                                   (bibtex-parse-strings
4087                                    (bibtex-string-files-init))))))                                    (bibtex-string-files-init))))))
4088          (bibtex-beginning-of-entry)          (bibtex-beginning-of-entry)
4089          (if (and (re-search-forward bibtex-entry-maybe-empty-head nil t)          (cond ((and (looking-at bibtex-string-maybe-empty-head)
4090                   ;; point is inside a key                      ;; point is inside a string key
4091                   (or (and (match-beginning bibtex-key-in-head)                      (or (and (match-beginning bibtex-key-in-head)
4092                            (>= pnt (match-beginning bibtex-key-in-head))                               (>= pnt (match-beginning bibtex-key-in-head))
4093                            (<= pnt (match-end bibtex-key-in-head)))                               (<= pnt (match-end bibtex-key-in-head)))
4094                       ;; or point is on empty key                          ;; or point is on empty string key
4095                       (and (not (match-beginning bibtex-key-in-head))                          (and (not (match-beginning bibtex-key-in-head))
4096                            (= pnt (match-end 0)))))                               (= pnt (match-end 0)))))
4097              (setq compl 'key))))                 (setq compl 'string))
4098                  ((and (looking-at bibtex-entry-maybe-empty-head)
4099      (cond ((equal compl 'key)                      ;; point is inside a key
4100             ;; key completion                      (or (and (match-beginning bibtex-key-in-head)
4101             (setq choose-completion-string-functions                               (>= pnt (match-beginning bibtex-key-in-head))
4102                   (lambda (choice buffer mini-p base-size)                               (<= pnt (match-end bibtex-key-in-head)))
4103                     (let ((choose-completion-string-functions nil))                          ;; or point is on empty key
4104                       (choose-completion-string choice buffer base-size))                          (and (not (match-beginning bibtex-key-in-head))
4105                     (bibtex-complete-key-cleanup choice)                               (= pnt (match-end 0)))))
4106                     ;; return t (required by choose-completion-string-functions)                 (setq compl 'key)))))
4107                     t))  
4108             (bibtex-complete-key-cleanup (bibtex-complete-internal      (cond ((eq compl 'key)
4109                                           bibtex-reference-keys)))             ;; key completion: no cleanup needed
4110               (let (completion-ignore-case)
4111                 (bibtex-complete-internal (bibtex-global-key-alist))))
4112    
4113              ((eq compl 'crossref-key)
4114               ;; crossref key completion
4115               (let (completion-ignore-case)
4116                 (setq choose-completion-string-functions
4117                       (lambda (choice buffer mini-p base-size)
4118                         (let ((choose-completion-string-functions nil))
4119                           (choose-completion-string choice buffer base-size))
4120                         (bibtex-complete-crossref-cleanup choice)
4121                         ;; return t (needed by choose-completion-string-functions)
4122                         t))
4123                 (bibtex-complete-crossref-cleanup (bibtex-complete-internal
4124                                                    (bibtex-global-key-alist)))))
4125    
4126              ((eq compl 'string)
4127               ;; string key completion: no cleanup needed
4128               (let ((completion-ignore-case t))
4129                 (bibtex-complete-internal bibtex-strings)))
4130    
4131            (compl            (compl
4132             ;; string completion             ;; string completion
4133             (setq choose-completion-string-functions             (let ((completion-ignore-case t))
4134                   `(lambda (choice buffer mini-p base-size)               (setq choose-completion-string-functions
4135                      (let ((choose-completion-string-functions nil))                     `(lambda (choice buffer mini-p base-size)
4136                        (choose-completion-string choice buffer base-size))                        (let ((choose-completion-string-functions nil))
4137                      (bibtex-complete-string-cleanup choice ',compl)                          (choose-completion-string choice buffer base-size))
4138                      ;; return t (required by choose-completion-string-functions)                        (bibtex-complete-string-cleanup choice ',compl)
4139                      t))                        ;; return t (needed by choose-completion-string-functions)
4140             (bibtex-complete-string-cleanup (bibtex-complete-internal compl)                        t))
4141                                             compl))               (bibtex-complete-string-cleanup (bibtex-complete-internal compl)
4142                                                 compl)))
4143    
4144            (t (error "Point outside key or BibTeX field")))))            (t (error "Point outside key or BibTeX field")))))
4145    
# Line 4193  signaled if point is outside key or BibT Line 4210  signaled if point is outside key or BibT
4210    
4211  (defun bibtex-String (&optional key)  (defun bibtex-String (&optional key)
4212    "Insert a new BibTeX @String entry with key KEY."    "Insert a new BibTeX @String entry with key KEY."
4213    (interactive (list (completing-read "String key: " bibtex-strings    (interactive (list (bibtex-read-string-key)))
                                       nil nil nil 'bibtex-key-history)))  
4214    (let ((bibtex-maintain-sorted-entries    (let ((bibtex-maintain-sorted-entries
4215           (unless bibtex-sort-ignore-string-entries           (unless bibtex-sort-ignore-string-entries
4216             bibtex-maintain-sorted-entries))             bibtex-maintain-sorted-entries))
# Line 4242  The URL is generated using the schemes d Line 4258  The URL is generated using the schemes d
4258            ;; Always ignore case,            ;; Always ignore case,
4259            (case-fold-search t)            (case-fold-search t)
4260            (lst bibtex-generate-url-list)            (lst bibtex-generate-url-list)
           (delim-regexp "\\`[{\"]\\(.*\\)[}\"]\\'")  
4261            field url scheme)            field url scheme)
4262        (while (setq scheme (pop lst))        (while (setq scheme (pop lst))
4263          (when (and (setq field (cdr (assoc-string (caar scheme)          (when (and (setq field (cdr (assoc-string (caar scheme)
4264                                                    fields-alist t)))                                                    fields-alist t)))
4265                     ;; Always remove field delimiters                     ;; Always remove field delimiters
4266                     (progn (if (string-match delim-regexp field)                     (progn (setq field (bibtex-remove-delimiters-string field))
                               (setq field (match-string 1 field)))  
4267                            (string-match (cdar scheme) field)))                            (string-match (cdar scheme) field)))
4268            (setq lst nil)            (setq lst nil)
4269            (if (null (cdr scheme))            (if (null (cdr scheme))
# Line 4259  The URL is generated using the schemes d Line 4273  The URL is generated using the schemes d
4273                     (setq url (concat url step)))                     (setq url (concat url step)))
4274                    ((setq field (cdr (assoc-string (car step) fields-alist t)))                    ((setq field (cdr (assoc-string (car step) fields-alist t)))
4275                     ;; Always remove field delimiters                     ;; Always remove field delimiters
4276                     (if (string-match delim-regexp field)                     (setq field (bibtex-remove-delimiters-string field))
                        (setq field (match-string 1 field)))  
4277                     (if (string-match (nth 1 step) field)                     (if (string-match (nth 1 step) field)
4278                         (setq field (cond                         (setq field (cond
4279                                      ((functionp (nth 2 step))                                      ((functionp (nth 2 step))
# Line 4268  The URL is generated using the schemes d Line 4281  The URL is generated using the schemes d
4281                                      ((numberp (nth 2 step))                                      ((numberp (nth 2 step))
4282                                       (match-string (nth 2 step) field))                                       (match-string (nth 2 step) field))
4283                                      (t                                      (t
4284                                       (replace-match (nth 2 step) nil nil field))))                                       (replace-match (nth 2 step) t nil field))))
4285                       ;; If the scheme is set up correctly,                       ;; If the scheme is set up correctly,
4286                       ;; we should never reach this point                       ;; we should never reach this point
4287                       (error "Match failed: %s" field))                       (error "Match failed: %s" field))

Legend:
Removed from v.1.88  
changed lines
  Added in v.1.89

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26