comparison lisp/textmodes/bibtex.el @ 89978:566253900690

Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-40 Merge from emacs--cvs-trunk--0 Patches applied: * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-535 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-536 sync-tree with gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-537 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-538 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-539 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-540 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-541 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-542 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-545 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-546 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-547 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-548 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-549 Use symbol-matching for generic-mode keywords * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-550 Update from CVS * miles@gnu.org--gnu-2004/gnus--devo--0--patch-2 Add {arch}/=cvs-sync-make-log * miles@gnu.org--gnu-2004/gnus--rel--5.8--base-0 Import from Gnus CVS branch V5-8 * miles@gnu.org--gnu-2004/gnus--rel--5.8--patch-1 {arch}/=tagging-method: Add CVS and autoconf grot to junk regexp * miles@gnu.org--gnu-2004/gnus--rel--5.8--patch-2 Use explicit tags for autoconf input files * miles@gnu.org--gnu-2004/gnus--rel--5.8--patch-3 Remove RCS keywords * miles@gnu.org--gnu-2004/gnus--rel--5.8--patch-4 Fix copied explicit id-tags * miles@gnu.org--gnu-2004/gnus--rel--5.8--patch-5 Add {arch}/=cvs-sync-make-log * miles@gnu.org--gnu-2004/gnus--rel--5.8--patch-6 configure.in: Use ifelse instead of m4_if for arch-tag: comment * miles@gnu.org--gnu-2004/gnus--rel--5.10--base-0 tag of miles@gnu.org--gnu-2004/gnus--rel--5.8--base-0 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-1 Gnus 5.10, from CVS branch v5-10 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-2 Merge from gnus--rel--5.8 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-3 Use explicit tags for autoconf input files * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-4 sync-tree with gnus--rel--5.8 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-5 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-6 Merge from gnus--rel--5.8 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-7 Remove RCS keywords * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-8 Merge from gnus--rel--5.8 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-9 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-10 Add {arch}/=cvs-sync-make-log * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-11 Merge from gnus--rel--5.8 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-12 Update from CVS: make.bat: Fix line endings around arch-tag. * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-13 - miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-17 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-21 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-22 Update from CVS: lisp/nndb.el (require): Remove tcp and duplicate cl. * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-23 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-24 lisp/nnimap.el (nnimap-open-connection): Remove extraneous end-paren * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-25 - miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-26 Update from CVS
author Miles Bader <miles@gnu.org>
date Wed, 15 Sep 2004 09:00:10 +0000
parents 4c90ffeb71c5 3d9707888790
children e24e2e78deda
comparison
equal deleted inserted replaced
89977:549fdd6f6856 89978:566253900690
1 ;;; bibtex.el --- BibTeX mode for GNU Emacs 1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
2 2
3 ;; Copyright (C) 1992,94,95,96,97,98,1999,2003,2004 3 ;; Copyright (C) 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2004
4 ;; Free Software Foundation, Inc. 4 ;; Free Software Foundation, Inc.
5 5
6 ;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de> 6 ;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de>
7 ;; Bengt Martensson <bengt@mathematik.uni-Bremen.de> 7 ;; Bengt Martensson <bengt@mathematik.uni-Bremen.de>
8 ;; Mark Shapiro <shapiro@corto.inria.fr> 8 ;; Mark Shapiro <shapiro@corto.inria.fr>
59 "List of functions to call on entry to BibTeX mode." 59 "List of functions to call on entry to BibTeX mode."
60 :group 'bibtex 60 :group 'bibtex
61 :type 'hook) 61 :type 'hook)
62 62
63 (defcustom bibtex-field-delimiters 'braces 63 (defcustom bibtex-field-delimiters 'braces
64 "*Type of field delimiters. Allowed values are `braces' or `double-quotes'." 64 "*Type of field delimiters. Allowed values are `braces' or `double-quotes'."
65 :group 'bibtex 65 :group 'bibtex
66 :type '(choice (const braces) 66 :type '(choice (const braces)
67 (const double-quotes))) 67 (const double-quotes)))
68 68
69 (defcustom bibtex-entry-delimiters 'braces 69 (defcustom bibtex-entry-delimiters 'braces
70 "*Type of entry delimiters. Allowed values are `braces' or `parentheses'." 70 "*Type of entry delimiters. Allowed values are `braces' or `parentheses'."
71 :group 'bibtex 71 :group 'bibtex
72 :type '(choice (const braces) 72 :type '(choice (const braces)
73 (const parentheses))) 73 (const parentheses)))
74 74
75 (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection") 75 (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
152 (defcustom bibtex-maintain-sorted-entries nil 152 (defcustom bibtex-maintain-sorted-entries nil
153 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order. 153 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order.
154 Allowed non-nil values are: 154 Allowed non-nil values are:
155 plain All entries are sorted alphabetically. 155 plain All entries are sorted alphabetically.
156 crossref All entries are sorted alphabetically unless an entry has a 156 crossref All entries are sorted alphabetically unless an entry has a
157 crossref field. These crossrefed entries are placed in 157 crossref field. These crossrefed entries are placed in
158 alphabetical order immediately preceding the main entry. 158 alphabetical order immediately preceding the main entry.
159 entry-class The entries are divided into classes according to their 159 entry-class The entries are divided into classes according to their
160 entry name, see `bibtex-sort-entry-class'. Within each class 160 entry name, see `bibtex-sort-entry-class'. Within each class
161 the entries are sorted alphabetically. 161 the entries are sorted alphabetically.
162 See also `bibtex-sort-ignore-string-entries'." 162 See also `bibtex-sort-ignore-string-entries'."
163 :group 'bibtex 163 :group 'bibtex
164 :type '(choice (const nil) 164 :type '(choice (const nil)
165 (const plain) 165 (const plain)
170 '(("String") 170 '(("String")
171 (catch-all) 171 (catch-all)
172 ("Book" "Proceedings")) 172 ("Book" "Proceedings"))
173 "*List of classes of BibTeX entry names, used for sorting entries. 173 "*List of classes of BibTeX entry names, used for sorting entries.
174 If value of `bibtex-maintain-sorted-entries' is `entry-class' 174 If value of `bibtex-maintain-sorted-entries' is `entry-class'
175 entries are ordered according to the classes they belong to. Each 175 entries are ordered according to the classes they belong to. Each
176 class contains a list of entry names. An entry `catch-all' applies 176 class contains a list of entry names. An entry `catch-all' applies
177 to all entries not explicitely mentioned.") 177 to all entries not explicitely mentioned.")
178 178
179 (defcustom bibtex-sort-ignore-string-entries t 179 (defcustom bibtex-sort-ignore-string-entries t
180 "*If non-nil, BibTeX @String entries are not sort-significant. 180 "*If non-nil, BibTeX @String entries are not sort-significant.
181 That means they are ignored when determining ordering of the buffer 181 That means they are ignored when determining ordering of the buffer
638 :group 'bibtex-autokey 638 :group 'bibtex-autokey
639 :type 'integer) 639 :type 'integer)
640 640
641 (defcustom bibtex-autokey-titleword-ignore 641 (defcustom bibtex-autokey-titleword-ignore
642 '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das" 642 '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das"
643 "[^A-Z].*" ".*[^a-zA-Z0-9].*") 643 "[^A-Z].*" ".*[^A-Z0-9].*")
644 "*Determines words from the title that are not to be used in the key. 644 "*Determines words from the title that are not to be used in the key.
645 Each item of the list is a regexp. If a word of the title matchs a 645 Each item of the list is a regexp. If a word of the title matchs a
646 regexp from that list, it is not included in the title part of the key. 646 regexp from that list, it is not included in the title part of the key.
647 See `bibtex-generate-autokey' for details." 647 See `bibtex-generate-autokey' for details."
648 :group 'bibtex-autokey 648 :group 'bibtex-autokey
760 760
761 (defcustom bibtex-autofill-types '("Proceedings") 761 (defcustom bibtex-autofill-types '("Proceedings")
762 "Automatically fill fields if possible for those BibTeX entry types." 762 "Automatically fill fields if possible for those BibTeX entry types."
763 :type '(repeat string)) 763 :type '(repeat string))
764 764
765 (defcustom bibtex-complete-key-cleanup nil 765 (defcustom bibtex-generate-url-list
766 "*Function called by `bibtex-complete' after insertion of a key fragment." 766 '((("url" . ".*:.*"))
767 :group 'bibtex-autokey 767 ;; Example of a complex setup.
768 :type '(choice (const :tag "None" nil) 768 (("journal" . "\\<\\(PR[ABCDEL]?\\|RMP\\)\\>")
769 (function :tag "Cleanup function"))) 769 "http://publish.aps.org/abstract/"
770 ("journal" ".*" downcase)
771 "/v"
772 ("volume" ".*" 0)
773 "/p"
774 ("pages" "\\`\\([0-9]+\\)" 1)))
775 "List of schemes for generating the URL of a BibTeX entry.
776 These schemes are used by `bibtex-url'.
777
778 Each scheme is of the form ((FIELD . REGEXP) STEP...).
779
780 FIELD is a field name as returned by `bibtex-parse-entry'.
781 REGEXP is matched against the text of FIELD. If the match succeed, then
782 this scheme will be used. If no STEPS are specified the matched text is used
783 as the URL, otherwise the URL is built by concatenating the STEPS.
784
785 A STEP can be a string or a list (FIELD REGEXP REPLACE) in which case
786 the text of FIELD is matched against REGEXP, and is replaced with REPLACE.
787 REPLACE can be a string, or a number (which selects the corresponding submatch)
788 or a function called with the field's text as argument and with the
789 `match-data' properly set.
790
791 Case is always ignored. Always remove the field delimiters."
792 :group 'bibtex
793 :type '(repeat
794 (list :tag "Scheme"
795 (cons :tag "Matcher" :extra-offset 4
796 (string :tag "BibTeX field")
797 (regexp :tag "Regexp"))
798 (repeat :tag "Steps to generate URL" :inline t
799 (choice
800 (string :tag "Literal text")
801 (list (string :tag "BibTeX field")
802 (regexp :tag "Regexp")
803 (choice (string :tag "Replacement")
804 (integer :tag "Sub-match")
805 (function :tag "Filter"))))))))
770 806
771 ;; bibtex-font-lock-keywords is a user option as well, but since the 807 ;; bibtex-font-lock-keywords is a user option as well, but since the
772 ;; patterns used to define this variable are defined in a later 808 ;; patterns used to define this variable are defined in a later
773 ;; section of this file, it is defined later. 809 ;; section of this file, it is defined later.
774 810
799 (define-key km "\C-c\"" 'bibtex-remove-delimiters) 835 (define-key km "\C-c\"" 'bibtex-remove-delimiters)
800 (define-key km "\C-c{" 'bibtex-remove-delimiters) 836 (define-key km "\C-c{" 'bibtex-remove-delimiters)
801 (define-key km "\C-c}" 'bibtex-remove-delimiters) 837 (define-key km "\C-c}" 'bibtex-remove-delimiters)
802 (define-key km "\C-c\C-c" 'bibtex-clean-entry) 838 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
803 (define-key km "\C-c\C-q" 'bibtex-fill-entry) 839 (define-key km "\C-c\C-q" 'bibtex-fill-entry)
840 (define-key km "\C-c\C-s" 'bibtex-find-entry)
804 (define-key km "\C-c?" 'bibtex-print-help-message) 841 (define-key km "\C-c?" 'bibtex-print-help-message)
805 (define-key km "\C-c\C-p" 'bibtex-pop-previous) 842 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
806 (define-key km "\C-c\C-n" 'bibtex-pop-next) 843 (define-key km "\C-c\C-n" 'bibtex-pop-next)
807 (define-key km "\C-c\C-k" 'bibtex-kill-field) 844 (define-key km "\C-c\C-k" 'bibtex-kill-field)
808 (define-key km "\C-c\M-k" 'bibtex-copy-field-as-kill) 845 (define-key km "\C-c\M-k" 'bibtex-copy-field-as-kill)
819 (define-key km "\C-\M-l" 'bibtex-reposition-window) 856 (define-key km "\C-\M-l" 'bibtex-reposition-window)
820 (define-key km "\C-\M-h" 'bibtex-mark-entry) 857 (define-key km "\C-\M-h" 'bibtex-mark-entry)
821 (define-key km "\C-c\C-b" 'bibtex-entry) 858 (define-key km "\C-c\C-b" 'bibtex-entry)
822 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry) 859 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
823 (define-key km "\C-c\C-rw" 'widen) 860 (define-key km "\C-c\C-rw" 'widen)
861 (define-key km "\C-c\C-l" 'bibtex-url)
824 (define-key km "\C-c\C-o" 'bibtex-remove-OPT-or-ALT) 862 (define-key km "\C-c\C-o" 'bibtex-remove-OPT-or-ALT)
825 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings) 863 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
826 (define-key km "\C-c\C-ei" 'bibtex-InCollection) 864 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
827 (define-key km "\C-c\C-eI" 'bibtex-InBook) 865 (define-key km "\C-c\C-eI" 'bibtex-InBook)
828 (define-key km "\C-c\C-e\C-a" 'bibtex-Article) 866 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
852 ["Beginning of Entry" bibtex-beginning-of-entry t] 890 ["Beginning of Entry" bibtex-beginning-of-entry t]
853 ["End of Entry" bibtex-end-of-entry t]) 891 ["End of Entry" bibtex-end-of-entry t])
854 ("Moving in BibTeX Buffer" 892 ("Moving in BibTeX Buffer"
855 ["Find Entry" bibtex-find-entry t] 893 ["Find Entry" bibtex-find-entry t]
856 ["Find Crossref Entry" bibtex-find-crossref t]) 894 ["Find Crossref Entry" bibtex-find-crossref t])
857 ("Operating on Current Entry"
858 ["Fill Entry" bibtex-fill-entry t]
859 ["Clean Entry" bibtex-clean-entry t]
860 "--" 895 "--"
861 ["Kill Entry" bibtex-kill-entry t]
862 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t]
863 ["Paste Most Recently Killed Entry" bibtex-yank t]
864 ["Paste Previously Killed Entry" bibtex-yank-pop t]
865 "--"
866 ["Ispell Entry" bibtex-ispell-entry t]
867 ["Ispell Entry Abstract" bibtex-ispell-abstract t]
868 ["Narrow to Entry" bibtex-narrow-to-entry t]
869 "--"
870 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
871 (fboundp 'reftex-view-crossref-from-bibtex)])
872 ("Operating on Current Field" 896 ("Operating on Current Field"
873 ["Fill Field" fill-paragraph t] 897 ["Fill Field" fill-paragraph t]
874 ["Remove Delimiters" bibtex-remove-delimiters t] 898 ["Remove Delimiters" bibtex-remove-delimiters t]
875 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t] 899 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t]
876 ["Clear Field" bibtex-empty-field t] 900 ["Clear Field" bibtex-empty-field t]
886 ["Snatch from Similar Preceding Field" bibtex-pop-previous t] 910 ["Snatch from Similar Preceding Field" bibtex-pop-previous t]
887 "--" 911 "--"
888 ["String or Key Complete" bibtex-complete t] 912 ["String or Key Complete" bibtex-complete t]
889 "--" 913 "--"
890 ["Help about Current Field" bibtex-print-help-message t]) 914 ["Help about Current Field" bibtex-print-help-message t])
915 ("Operating on Current Entry"
916 ["Fill Entry" bibtex-fill-entry t]
917 ["Clean Entry" bibtex-clean-entry t]
918 ["Update Entry" bibtex-entry-update t]
919 "--"
920 ["Kill Entry" bibtex-kill-entry t]
921 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t]
922 ["Paste Most Recently Killed Entry" bibtex-yank t]
923 ["Paste Previously Killed Entry" bibtex-yank-pop t]
924 "--"
925 ["Ispell Entry" bibtex-ispell-entry t]
926 ["Ispell Entry Abstract" bibtex-ispell-abstract t]
927 ["Narrow to Entry" bibtex-narrow-to-entry t]
928 "--"
929 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
930 (fboundp 'reftex-view-crossref-from-bibtex)])
891 ("Operating on Buffer or Region" 931 ("Operating on Buffer or Region"
892 ["Validate Entries" bibtex-validate t] 932 ["Validate Entries" bibtex-validate t]
893 ["Sort Entries" bibtex-sort-buffer t] 933 ["Sort Entries" bibtex-sort-buffer t]
894 ["Reformat Entries" bibtex-reformat t] 934 ["Reformat Entries" bibtex-reformat t]
895 ["Count Entries" bibtex-count-entries t]) 935 ["Count Entries" bibtex-count-entries t]
896 ("Miscellaneous" 936 "--"
897 ["Convert Alien Buffer" bibtex-convert-alien t]))) 937 ["Convert Alien Buffer" bibtex-convert-alien t])))
898 938
899 (easy-menu-define 939 (easy-menu-define
900 bibtex-entry-menu bibtex-mode-map "Entry-Types Menu in BibTeX mode" 940 bibtex-entry-menu bibtex-mode-map "Entry-Types Menu in BibTeX mode"
901 (list "Entry-Types" 941 (list "Entry-Types"
913 ["Unpublished" bibtex-Unpublished t] 953 ["Unpublished" bibtex-Unpublished t]
914 ["Miscellaneous" bibtex-Misc t] 954 ["Miscellaneous" bibtex-Misc t]
915 ["String" bibtex-String t] 955 ["String" bibtex-String t]
916 ["Preamble" bibtex-Preamble t])) 956 ["Preamble" bibtex-Preamble t]))
917 957
958 (defvar bibtex-url-map
959 (let ((km (make-sparse-keymap)))
960 (define-key km [(mouse-2)] 'bibtex-url)
961 km)
962 "Local keymap for clickable URLs.")
963 (fset 'bibtex-url-map bibtex-url-map)
964
918 965
919 ;; Internal Variables 966 ;; Internal Variables
920 967
921 (defvar bibtex-pop-previous-search-point nil 968 (defvar bibtex-pop-previous-search-point nil
922 "Next point where `bibtex-pop-previous' starts looking for a similar entry.") 969 "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
952 (lazy-completion-table bibtex-reference-keys bibtex-parse-keys nil nil t) 999 (lazy-completion-table bibtex-reference-keys bibtex-parse-keys nil nil t)
953 "Completion table for BibTeX reference keys.") 1000 "Completion table for BibTeX reference keys.")
954 (make-variable-buffer-local 'bibtex-reference-keys) 1001 (make-variable-buffer-local 'bibtex-reference-keys)
955 1002
956 (defvar bibtex-buffer-last-parsed-tick nil 1003 (defvar bibtex-buffer-last-parsed-tick nil
957 "Last value returned by `buffer-modified-tick' when buffer 1004 "Value of `buffer-modified-tick' last time buffer was parsed for keys.")
958 was parsed for keys the last time.")
959 1005
960 (defvar bibtex-parse-idle-timer nil 1006 (defvar bibtex-parse-idle-timer nil
961 "Stores if timer is already installed.") 1007 "Stores if timer is already installed.")
962 1008
963 (defvar bibtex-progress-lastperc nil 1009 (defvar bibtex-progress-lastperc nil
1038 1084
1039 1085
1040 (defconst bibtex-empty-field-re "\"\"\\|{}" 1086 (defconst bibtex-empty-field-re "\"\"\\|{}"
1041 "Regexp matching an empty field.") 1087 "Regexp matching an empty field.")
1042 1088
1043 (defconst bibtex-quoted-string-re
1044 (concat "\""
1045 "\\("
1046 "[^\"\\]" ; anything but quote or backslash
1047 "\\|"
1048 "\\("
1049 "\\\\\\(.\\|\n\\)" ; any backslash quoted character
1050 "\\)"
1051 "\\)*"
1052 "\"")
1053 "Regexp matching a field string enclosed by quotes.")
1054
1055 (defconst bibtex-font-lock-syntactic-keywords 1089 (defconst bibtex-font-lock-syntactic-keywords
1056 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start 0 1) "\\)" 1090 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start 0 1) "\\)"
1057 (substring bibtex-comment-start 1) "\\>") 1091 (substring bibtex-comment-start 1) "\\>")
1058 1 '(11)))) 1092 1 '(11))))
1059 1093
1060 (defvar bibtex-font-lock-keywords 1094 (defvar bibtex-font-lock-keywords
1061 (list 1095 ;; entry type and reference key
1062 ;; entry type and reference key 1096 `((,bibtex-entry-maybe-empty-head
1063 (list bibtex-entry-maybe-empty-head 1097 (,bibtex-type-in-head font-lock-function-name-face)
1064 (list bibtex-type-in-head 'font-lock-function-name-face) 1098 (,bibtex-key-in-head font-lock-constant-face nil t))
1065 (list bibtex-key-in-head 'font-lock-constant-face nil t)) 1099 ;; optional field names (treated as comments)
1066 ;; optional field names (treated as comments) 1100 (,(concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
1067 (list 1101 1 font-lock-comment-face)
1068 (concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=") 1102 ;; field names
1069 1 'font-lock-comment-face) 1103 (,(concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1070 ;; field names 1104 1 font-lock-variable-name-face)
1071 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=") 1105 ;; url
1072 1 'font-lock-variable-name-face)) 1106 (bibtex-font-lock-url 0 '(face nil mouse-face highlight
1107 keymap bibtex-url-map)))
1073 "*Default expressions to highlight in BibTeX mode.") 1108 "*Default expressions to highlight in BibTeX mode.")
1074 1109
1110 (defvar bibtex-font-lock-url-regexp
1111 (concat "\\<" (regexp-opt (mapcar 'caar bibtex-generate-url-list) t)
1112 "\\>[ \t]*=[ \t]*")
1113 "Regexp for `bibtex-font-lock-url'.")
1114
1075 (defvar bibtex-field-name-for-parsing nil 1115 (defvar bibtex-field-name-for-parsing nil
1076 "Temporary variable storing the name string to be parsed by the callback 1116 "Regexp of field name to be parsed by function `bibtex-parse-field-name'.
1077 function `bibtex-parse-field-name'.") 1117 Passed by dynamic scoping.")
1078 1118
1079 (defvar bibtex-sort-entry-class-alist 1119 (defvar bibtex-sort-entry-class-alist
1080 (let ((i -1) alist) 1120 (let ((i -1) alist)
1081 (dolist (class bibtex-sort-entry-class alist) 1121 (dolist (class bibtex-sort-entry-class alist)
1082 (setq i (1+ i)) 1122 (setq i (1+ i))
1083 (dolist (entry class) 1123 (dolist (entry class)
1084 ;; all entry names should be downcase (for ease of comparison) 1124 ;; all entry names should be downcase (for ease of comparison)
1085 (push (cons (if (stringp entry) (downcase entry) entry) i) alist)))) 1125 (push (cons (if (stringp entry) (downcase entry) entry) i) alist))))
1086 "Alist for the classes of the entry types if the value of 1126 "Alist mapping entry types to their sorting index.
1087 `bibtex-maintain-sorted-entries' is `entry-class'.") 1127 Auto-generated from `bibtex-sort-entry-class'.
1128 Used when `bibtex-maintain-sorted-entries' is `entry-class'.")
1088 1129
1089 1130
1090 ;; Special support taking care of variants 1131 ;; Special support taking care of variants
1091 (defvar zmacs-regions) 1132 (defvar zmacs-regions)
1092 (if (boundp 'mark-active) 1133 (defalias 'bibtex-mark-active
1093 (defun bibtex-mark-active () 1134 (if (boundp 'mark-active)
1094 ;; In Emacs mark-active indicates if mark is active. 1135 ;; In Emacs mark-active indicates if mark is active.
1095 mark-active) 1136 (lambda () mark-active)
1096 (defun bibtex-mark-active ()
1097 ;; In XEmacs (mark) returns nil when not active. 1137 ;; In XEmacs (mark) returns nil when not active.
1098 (if zmacs-regions (mark) (mark t)))) 1138 (lambda () (if zmacs-regions (mark) (mark t)))))
1099 1139
1100 (if (fboundp 'run-with-idle-timer) 1140 (defalias 'bibtex-run-with-idle-timer
1101 ;; timer.el is distributed with Emacs 1141 (if (fboundp 'run-with-idle-timer)
1102 (fset 'bibtex-run-with-idle-timer 'run-with-idle-timer) 1142 ;; timer.el is distributed with Emacs
1103 ;; timer.el is not distributed with XEmacs 1143 'run-with-idle-timer
1104 ;; Notice that this does not (yet) pass the arguments, but they 1144 ;; timer.el is not distributed with XEmacs
1105 ;; are not used (yet) in bibtex.el. Fix if needed. 1145 ;; Notice that this does not (yet) pass the arguments, but they
1106 (defun bibtex-run-with-idle-timer (secs repeat function &rest args) 1146 ;; are not used (yet) in bibtex.el. Fix if needed.
1107 (start-itimer "bibtex" function secs (if repeat secs nil) t))) 1147 (lambda (secs repeat function &rest args)
1148 (start-itimer "bibtex" function secs (if repeat secs nil) t))))
1108 1149
1109 1150
1110 ;; Support for hideshow minor mode 1151 ;; Support for hideshow minor mode
1111 (defun bibtex-hs-forward-sexp (arg) 1152 (defun bibtex-hs-forward-sexp (arg)
1112 "Replacement for `forward-sexp' to be used by `hs-minor-mode'." 1153 "Replacement for `forward-sexp' to be used by `hs-minor-mode'.
1113 (if (< arg 0) 1154 ARG is ignored."
1114 (backward-sexp 1) 1155 (if (looking-at "@\\S(*\\s(")
1115 (if (looking-at "@\\S(*\\s(") 1156 (goto-char (1- (match-end 0))))
1116 (progn 1157 (forward-sexp 1))
1117 (goto-char (match-end 0))
1118 (forward-char -1)
1119 (forward-sexp 1))
1120 (forward-sexp 1))))
1121 1158
1122 (add-to-list 1159 (add-to-list
1123 'hs-special-modes-alist 1160 'hs-special-modes-alist
1124 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil)) 1161 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil))
1125 1162
1142 1179
1143 (defun bibtex-parse-field-name () 1180 (defun bibtex-parse-field-name ()
1144 "Parse the field name stored in `bibtex-field-name-for-parsing'. 1181 "Parse the field name stored in `bibtex-field-name-for-parsing'.
1145 If the field name is found, return a triple consisting of the position of the 1182 If the field name is found, return a triple consisting of the position of the
1146 very first character of the match, the actual starting position of the name 1183 very first character of the match, the actual starting position of the name
1147 part and end position of the match. Move point to end of field name. 1184 part and end position of the match. Move point to end of field name.
1148 If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceeding 1185 If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceeding
1149 BibTeX field as necessary." 1186 BibTeX field as necessary."
1150 (cond ((looking-at ",[ \t\n]*") 1187 (cond ((looking-at ",[ \t\n]*")
1151 (let ((start (point))) 1188 (let ((start (point)))
1152 (goto-char (match-end 0)) 1189 (goto-char (match-end 0))
1204 (defun bibtex-parse-field-text () 1241 (defun bibtex-parse-field-text ()
1205 "Parse the text part of a BibTeX field. 1242 "Parse the text part of a BibTeX field.
1206 The text part is either a string, or an empty string, or a constant followed 1243 The text part is either a string, or an empty string, or a constant followed
1207 by one or more <# (string|constant)> pairs. If a syntactically correct text 1244 by one or more <# (string|constant)> pairs. If a syntactically correct text
1208 is found, a pair containing the start and end position of the text is 1245 is found, a pair containing the start and end position of the text is
1209 returned, nil otherwise. Move point to end of field text." 1246 returned, nil otherwise. Move point to end of field text."
1210 (let ((starting-point (point)) 1247 (let ((starting-point (point))
1211 end-point failure boundaries) 1248 end-point failure boundaries)
1212 (while (not (or end-point failure)) 1249 (while (not (or end-point failure))
1213 (cond ((looking-at bibtex-field-const) 1250 (cond ((looking-at bibtex-field-const)
1214 (goto-char (match-end 0))) 1251 (goto-char (match-end 0)))
1215 ((setq boundaries (bibtex-parse-field-string)) 1252 ((setq boundaries (bibtex-parse-field-string))
1216 (goto-char (cdr boundaries))) 1253 (goto-char (cdr boundaries)))
1217 ((setq failure t))) 1254 ((setq failure t)))
1218 (if (not (looking-at "[ \t\n]*#[ \t\n]*")) 1255 (if (looking-at "[ \t\n]*#[ \t\n]*")
1219 (setq end-point (point)) 1256 (goto-char (match-end 0))
1220 (goto-char (match-end 0)))) 1257 (setq end-point (point))))
1221 (if (and (not failure) 1258 (if (and (not failure)
1222 end-point) 1259 end-point)
1223 (cons starting-point end-point)))) 1260 (cons starting-point end-point))))
1224 1261
1225 (defun bibtex-parse-field (name) 1262 (defun bibtex-parse-field (name)
1232 1269
1233 (defun bibtex-search-forward-field (name &optional bound) 1270 (defun bibtex-search-forward-field (name &optional bound)
1234 "Search forward to find a field of name NAME. 1271 "Search forward to find a field of name NAME.
1235 If a syntactically correct field is found, a pair containing the boundaries of 1272 If a syntactically correct field is found, a pair containing the boundaries of
1236 the name and text parts of the field is returned. The search is limited by 1273 the name and text parts of the field is returned. The search is limited by
1237 optional arg BOUND. If BOUND is t the search is limited by the end of the current 1274 optional arg BOUND. If BOUND is t the search is limited by the end of the
1238 entry. Do not move point." 1275 current entry. Do not move point."
1239 (save-match-data 1276 (save-match-data
1240 (save-excursion 1277 (save-excursion
1241 (unless (integer-or-marker-p bound) 1278 (unless (integer-or-marker-p bound)
1242 (setq bound (if bound 1279 (setq bound (if bound
1243 (save-excursion (bibtex-end-of-entry)) 1280 (save-excursion (bibtex-end-of-entry))
1259 1296
1260 (defun bibtex-search-backward-field (name &optional bound) 1297 (defun bibtex-search-backward-field (name &optional bound)
1261 "Search backward to find a field of name NAME. 1298 "Search backward to find a field of name NAME.
1262 If a syntactically correct field is found, a pair containing the boundaries of 1299 If a syntactically correct field is found, a pair containing the boundaries of
1263 the name and text parts of the field is returned. The search is limited by 1300 the name and text parts of the field is returned. The search is limited by
1264 optional arg BOUND. If BOUND is t the search is limited by the beginning of the 1301 optional arg BOUND. If BOUND is t the search is limited by the beginning of the
1265 current entry. Do not move point." 1302 current entry. Do not move point."
1266 (save-match-data 1303 (save-match-data
1267 (save-excursion 1304 (save-excursion
1268 (unless (integer-or-marker-p bound) 1305 (unless (integer-or-marker-p bound)
1269 (setq bound (if bound 1306 (setq bound (if bound
1270 (save-excursion (bibtex-beginning-of-entry)) 1307 (save-excursion (bibtex-beginning-of-entry))
1292 (defsubst bibtex-start-of-text-in-field (bounds) 1329 (defsubst bibtex-start-of-text-in-field (bounds)
1293 (cadr bounds)) 1330 (cadr bounds))
1294 (defsubst bibtex-end-of-text-in-field (bounds) 1331 (defsubst bibtex-end-of-text-in-field (bounds)
1295 (cddr bounds)) 1332 (cddr bounds))
1296 1333
1297 (defun bibtex-name-in-field (bounds) 1334 (defun bibtex-name-in-field (bounds &optional remove-opt-alt)
1298 "Get content of name in BibTeX field defined via BOUNDS." 1335 "Get content of name in BibTeX field defined via BOUNDS.
1299 (buffer-substring-no-properties (nth 1 (car bounds)) 1336 If optional arg REMOVE-OPT-ALT is non-nil remove \"OPT\" and \"ALT\"."
1300 (nth 2 (car bounds)))) 1337 (let ((name (buffer-substring-no-properties (nth 1 (car bounds))
1338 (nth 2 (car bounds)))))
1339 (if (and remove-opt-alt
1340 (string-match "\\`\\(OPT\\|ALT\\)" name))
1341 (substring name 3)
1342 name)))
1301 1343
1302 (defun bibtex-text-in-field-bounds (bounds &optional remove-delim) 1344 (defun bibtex-text-in-field-bounds (bounds &optional remove-delim)
1303 "Get content of text in BibTeX field defined via BOUNDS. 1345 "Get content of text in BibTeX field defined via BOUNDS.
1304 If optional arg REMOVE-DELIM is non-nil remove enclosing field delimiters 1346 If optional arg REMOVE-DELIM is non-nil remove enclosing field delimiters
1305 if present." 1347 if present."
1309 (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content)) 1351 (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content))
1310 (substring content (match-beginning 1) (match-end 1)) 1352 (substring content (match-beginning 1) (match-end 1))
1311 content))) 1353 content)))
1312 1354
1313 (defun bibtex-text-in-field (field &optional follow-crossref) 1355 (defun bibtex-text-in-field (field &optional follow-crossref)
1314 "Get content of field FIELD of current BibTeX entry. Return nil if not found. 1356 "Get content of field FIELD of current BibTeX entry. Return nil if not found.
1315 If optional arg FOLLOW-CROSSREF is non-nil, follow crossref." 1357 If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1316 (save-excursion 1358 (save-excursion
1317 (save-restriction 1359 (save-restriction
1318 ;; We want to jump back and forth while searching FIELD 1360 ;; We want to jump back and forth while searching FIELD
1319 (bibtex-narrow-to-entry) 1361 (bibtex-narrow-to-entry)
1349 1391
1350 (defun bibtex-parse-string-postfix () 1392 (defun bibtex-parse-string-postfix ()
1351 "Parse the postfix part of a BibTeX string entry, including the text. 1393 "Parse the postfix part of a BibTeX string entry, including the text.
1352 If the string postfix is found, return a triple consisting of the position of 1394 If the string postfix is found, return a triple consisting of the position of
1353 the actual starting and ending position of the text and the very last 1395 the actual starting and ending position of the text and the very last
1354 character of the string entry. Move point past BibTeX string entry." 1396 character of the string entry. Move point past BibTeX string entry."
1355 (let* ((case-fold-search t) 1397 (let* ((case-fold-search t)
1356 (bounds (bibtex-parse-field-text))) 1398 (bounds (bibtex-parse-field-text)))
1357 (when bounds 1399 (when bounds
1358 (goto-char (cdr bounds)) 1400 (goto-char (cdr bounds))
1359 (when (looking-at "[ \t\n]*[})]") 1401 (when (looking-at "[ \t\n]*[})]")
1371 'bibtex-parse-string-postfix)) 1413 'bibtex-parse-string-postfix))
1372 1414
1373 (defun bibtex-search-forward-string () 1415 (defun bibtex-search-forward-string ()
1374 "Search forward to find a BibTeX string entry. 1416 "Search forward to find a BibTeX string entry.
1375 If a syntactically correct entry is found, a pair containing the boundaries of 1417 If a syntactically correct entry is found, a pair containing the boundaries of
1376 the reference key and text parts of the string is returned. Do not move point." 1418 the reference key and text parts of the string is returned. Do not move point."
1377 (save-excursion 1419 (save-excursion
1378 (save-match-data 1420 (save-match-data
1379 (let ((case-fold-search t) 1421 (let ((case-fold-search t)
1380 boundaries) 1422 boundaries)
1381 (while (and (not boundaries) 1423 (while (and (not boundaries)
1387 boundaries)))) 1429 boundaries))))
1388 1430
1389 (defun bibtex-search-backward-string () 1431 (defun bibtex-search-backward-string ()
1390 "Search backward to find a BibTeX string entry. 1432 "Search backward to find a BibTeX string entry.
1391 If a syntactically correct entry is found, a pair containing the boundaries of 1433 If a syntactically correct entry is found, a pair containing the boundaries of
1392 the reference key and text parts of the field is returned. Do not move point." 1434 the reference key and text parts of the field is returned. Do not move point."
1393 (save-excursion 1435 (save-excursion
1394 (save-match-data 1436 (save-match-data
1395 (let ((case-fold-search t) 1437 (let ((case-fold-search t)
1396 boundaries) 1438 boundaries)
1397 (while (and (not boundaries) 1439 (while (and (not boundaries)
1428 ;; ignore @ 1470 ;; ignore @
1429 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head)) 1471 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head))
1430 (match-end bibtex-type-in-head))) 1472 (match-end bibtex-type-in-head)))
1431 1473
1432 (defun bibtex-key-in-head (&optional empty) 1474 (defun bibtex-key-in-head (&optional empty)
1433 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty." 1475 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
1434 (if (match-beginning bibtex-key-in-head) 1476 (if (match-beginning bibtex-key-in-head)
1435 (buffer-substring-no-properties (match-beginning bibtex-key-in-head) 1477 (buffer-substring-no-properties (match-beginning bibtex-key-in-head)
1436 (match-end bibtex-key-in-head)) 1478 (match-end bibtex-key-in-head))
1437 empty)) 1479 empty))
1438 1480
1439 ;; Helper Functions 1481 ;; Helper Functions
1440 1482
1483 (defsubst bibtex-string= (str1 str2)
1484 "Return t if STR1 and STR2 are equal, ignoring case."
1485 (eq t (compare-strings str1 0 nil str2 0 nil t)))
1486
1441 (defun bibtex-delete-whitespace () 1487 (defun bibtex-delete-whitespace ()
1442 "Delete all whitespace starting at point." 1488 "Delete all whitespace starting at point."
1443 (if (looking-at "[ \t\n]+") 1489 (if (looking-at "[ \t\n]+")
1444 (delete-region (point) (match-end 0)))) 1490 (delete-region (point) (match-end 0))))
1445 1491
1446 (defun bibtex-current-line () 1492 (defun bibtex-current-line ()
1447 "Compute line number of point regardless whether the buffer is narrowed." 1493 "Compute line number of point regardless whether the buffer is narrowed."
1448 (+ (count-lines 1 (point)) 1494 (+ (count-lines 1 (point))
1449 (if (equal (current-column) 0) 1 0))) 1495 (if (equal (current-column) 0) 1 0)))
1450 1496
1451 (defun bibtex-member-of-regexp (string list)
1452 "Return non-nil if STRING is exactly matched by an element of LIST.
1453 The value is actually the tail of LIST whose car matches STRING."
1454 (let (case-fold-search)
1455 (while (and list
1456 (not (string-match (concat "\\`\\(?:" (car list) "\\)\\'") string)))
1457 (setq list (cdr list)))
1458 list))
1459
1460 (defun bibtex-skip-to-valid-entry (&optional backward) 1497 (defun bibtex-skip-to-valid-entry (&optional backward)
1461 "Unless at beginning of a valid BibTeX entry, move point to beginning of the 1498 "Move point to beginning of the next valid BibTeX entry.
1462 next valid one. With optional argument BACKWARD non-nil, move backward to 1499 Do not move if we are already at beginning of a valid BibTeX entry.
1463 beginning of previous valid one. A valid entry is a syntactical correct one 1500 With optional argument BACKWARD non-nil, move backward to
1501 beginning of previous valid one. A valid entry is a syntactical correct one
1464 with type contained in `bibtex-entry-field-alist' or, if 1502 with type contained in `bibtex-entry-field-alist' or, if
1465 `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string 1503 `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
1466 entry. Return buffer position of beginning and ending of entry if a valid 1504 entry. Return buffer position of beginning and ending of entry if a valid
1467 entry is found, nil otherwise." 1505 entry is found, nil otherwise."
1468 (interactive "P") 1506 (interactive "P")
1469 (let ((case-fold-search t) 1507 (let ((case-fold-search t)
1470 found) 1508 found)
1471 (while (not (or found (if backward (bobp) (eobp)))) 1509 (while (not (or found (if backward (bobp) (eobp))))
1486 (forward-char -1)))))) 1524 (forward-char -1))))))
1487 found)) 1525 found))
1488 1526
1489 (defun bibtex-map-entries (fun) 1527 (defun bibtex-map-entries (fun)
1490 "Call FUN for each BibTeX entry starting with the current. 1528 "Call FUN for each BibTeX entry starting with the current.
1491 Do this to the end of the file. FUN is called with three arguments, the key of 1529 Do this to the end of the file. FUN is called with three arguments, the key of
1492 the entry and the buffer positions (marker) of beginning and end of entry. 1530 the entry and the buffer positions (marker) of beginning and end of entry.
1493 Point is inside the entry. If `bibtex-sort-ignore-string-entries' is non-nil, 1531 Point is inside the entry. If `bibtex-sort-ignore-string-entries' is non-nil,
1494 FUN will not be called for @String entries." 1532 FUN will not be called for @String entries."
1495 (let ((case-fold-search t)) 1533 (let ((case-fold-search t))
1496 (bibtex-beginning-of-entry) 1534 (bibtex-beginning-of-entry)
1497 (while (re-search-forward bibtex-entry-head nil t) 1535 (while (re-search-forward bibtex-entry-head nil t)
1498 (let ((entry-type (bibtex-type-in-head)) 1536 (let ((entry-type (bibtex-type-in-head))
1499 (key (bibtex-key-in-head "")) 1537 (key (bibtex-key-in-head ""))
1500 (beg (copy-marker (match-beginning 0))) 1538 (beg (copy-marker (match-beginning 0)))
1501 (end (copy-marker (save-excursion (bibtex-end-of-entry))))) 1539 (end (copy-marker (save-excursion (bibtex-end-of-entry)))))
1502 (save-excursion 1540 (save-excursion
1503 (if (or (and (not bibtex-sort-ignore-string-entries) 1541 (if (or (and (not bibtex-sort-ignore-string-entries)
1504 (string-equal "string" (downcase entry-type))) 1542 (bibtex-string= entry-type "string"))
1505 (assoc-string entry-type bibtex-entry-field-alist t)) 1543 (assoc-string entry-type bibtex-entry-field-alist t))
1506 (funcall fun key beg end))) 1544 (funcall fun key beg end)))
1507 (goto-char end))))) 1545 (goto-char end)))))
1508 1546
1509 (defun bibtex-progress-message (&optional flag interval) 1547 (defun bibtex-progress-message (&optional flag interval)
1554 "}" 1592 "}"
1555 ")")) 1593 ")"))
1556 1594
1557 (defun bibtex-search-entry (empty-head &optional bound noerror backward) 1595 (defun bibtex-search-entry (empty-head &optional bound noerror backward)
1558 "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t). 1596 "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t).
1559 BOUND and NOERROR are exactly as in `re-search-forward'. If BACKWARD 1597 BOUND and NOERROR are exactly as in `re-search-forward'. If BACKWARD
1560 is non-nil, search is done in reverse direction. Point is moved past the 1598 is non-nil, search is done in reverse direction. Point is moved past the
1561 closing delimiter (at the beginning of entry if BACKWARD is non-nil). 1599 closing delimiter (at the beginning of entry if BACKWARD is non-nil).
1562 Return a cons pair with buffer positions of beginning and end of entry. 1600 Return a cons pair with buffer positions of beginning and end of entry.
1563 After call to this function MATCH-BEGINNING and MATCH-END functions 1601 After call to this function MATCH-BEGINNING and MATCH-END functions
1564 are defined, but only for the head part of the entry 1602 are defined, but only for the head part of the entry
1565 \(especially (match-end 0) just gives the end of the head part)." 1603 \(especially (match-end 0) just gives the end of the head part)."
1573 (re-search-backward entry-head-re bound noerror)) 1611 (re-search-backward entry-head-re bound noerror))
1574 (setq found (bibtex-search-entry empty-head pnt t))) 1612 (setq found (bibtex-search-entry empty-head pnt t)))
1575 (if found 1613 (if found
1576 (progn (goto-char (match-beginning 0)) 1614 (progn (goto-char (match-beginning 0))
1577 found) 1615 found)
1578 (cond ((equal noerror nil) 1616 (cond ((not noerror)
1579 ;; yell 1617 ;; yell
1580 (error "Backward search of BibTeX entry failed")) 1618 (error "Backward search of BibTeX entry failed"))
1581 ((equal noerror t) 1619 ((equal noerror t)
1582 ;; don't move 1620 ;; don't move
1583 (goto-char pnt))) 1621 (goto-char pnt)))
1658 (re-search-forward "^[ \t]*[@\n]" nil 'move) 1696 (re-search-forward "^[ \t]*[@\n]" nil 'move)
1659 (backward-char 1))) 1697 (backward-char 1)))
1660 (skip-chars-forward " \t\n"))) 1698 (skip-chars-forward " \t\n")))
1661 1699
1662 (defun bibtex-beginning-of-first-entry () 1700 (defun bibtex-beginning-of-first-entry ()
1663 "Go to the beginning of the first BibTeX entry in buffer. Return point." 1701 "Go to the beginning of the first BibTeX entry in buffer. Return point."
1664 (goto-char (point-min)) 1702 (goto-char (point-min))
1665 (if (re-search-forward "^[ \t]*@" nil 'move) 1703 (if (re-search-forward "^[ \t]*@" nil 'move)
1666 (beginning-of-line)) 1704 (beginning-of-line))
1667 (point)) 1705 (point))
1668 1706
1682 (if (or (= (preceding-char) ?}) 1720 (if (or (= (preceding-char) ?})
1683 (= (preceding-char) ?\")) 1721 (= (preceding-char) ?\"))
1684 (forward-char -1))) 1722 (forward-char -1)))
1685 1723
1686 (defun bibtex-enclosing-field (&optional noerr) 1724 (defun bibtex-enclosing-field (&optional noerr)
1687 "Search for BibTeX field enclosing point. Point moves to end of field. 1725 "Search for BibTeX field enclosing point.
1688 Use `match-beginning' and `match-end' to parse the field. If NOERR is non-nil, 1726 Use `match-beginning' and `match-end' to parse the field. If NOERR is non-nil,
1689 no error is signalled. In this case, bounds are returned on success, 1727 no error is signalled. In this case, bounds are returned on success,
1690 nil otherwise." 1728 nil otherwise. Does not move point."
1691 (let ((bounds (bibtex-search-backward-field bibtex-field-name t))) 1729 (let ((bounds (bibtex-search-backward-field bibtex-field-name t)))
1692 (if (and bounds 1730 (if (and bounds
1693 (<= (bibtex-start-of-field bounds) (point)) 1731 (<= (bibtex-start-of-field bounds) (point))
1694 (>= (bibtex-end-of-field bounds) (point))) 1732 (>= (bibtex-end-of-field bounds) (point)))
1695 bounds 1733 bounds
1696 (unless noerr 1734 (unless noerr
1697 (error "Can't find enclosing BibTeX field"))))) 1735 (error "Can't find enclosing BibTeX field")))))
1698 1736
1699 (defun bibtex-enclosing-entry-maybe-empty-head () 1737 (defun bibtex-enclosing-entry-maybe-empty-head ()
1700 "Search for BibTeX entry enclosing point. Move point to end of entry. 1738 "Search for BibTeX entry enclosing point. Move point to end of entry.
1701 Beginning (but not end) of entry is given by (`match-beginning' 0)." 1739 Beginning (but not end) of entry is given by (`match-beginning' 0)."
1702 (let ((case-fold-search t) 1740 (let ((case-fold-search t)
1703 (old-point (point))) 1741 (old-point (point)))
1704 (unless (re-search-backward bibtex-entry-maybe-empty-head nil t) 1742 (unless (re-search-backward bibtex-entry-maybe-empty-head nil t)
1705 (goto-char old-point) 1743 (goto-char old-point)
1730 (forward-char))) 1768 (forward-char)))
1731 (set-mark (point)) 1769 (set-mark (point))
1732 (message "Mark set") 1770 (message "Mark set")
1733 (bibtex-make-field (list (elt current 1) nil (elt current 2)) t)) 1771 (bibtex-make-field (list (elt current 1) nil (elt current 2)) t))
1734 ((equal bibtex-last-kill-command 'entry) 1772 ((equal bibtex-last-kill-command 'entry)
1735 (if (not (eobp)) 1773 (unless (eobp) (bibtex-beginning-of-entry))
1736 (bibtex-beginning-of-entry))
1737 (set-mark (point)) 1774 (set-mark (point))
1738 (message "Mark set") 1775 (message "Mark set")
1739 (insert (elt current 1))) 1776 (insert (elt current 1)))
1740 (t 1777 (t
1741 (error "Unknown tag field: %s. Please submit a bug report" 1778 (error "Unknown tag field: %s. Please submit a bug report"
1742 bibtex-last-kill-command)))))) 1779 bibtex-last-kill-command))))))
1743
1744 (defun bibtex-assoc-regexp (regexp alist)
1745 "Return non-nil if REGEXP matches the car of an element of ALIST.
1746 The value is actually the element of ALIST matched by REGEXP.
1747 Case is ignored if `case-fold-search' is non-nil in the current buffer."
1748 (while (and alist
1749 (not (string-match regexp (caar alist))))
1750 (setq alist (cdr alist)))
1751 (car alist))
1752 1780
1753 (defun bibtex-format-entry () 1781 (defun bibtex-format-entry ()
1754 "Helper function for `bibtex-clean-entry'. 1782 "Helper function for `bibtex-clean-entry'.
1755 Formats current entry according to variable `bibtex-entry-format'." 1783 Formats current entry according to variable `bibtex-entry-format'."
1756 (save-excursion 1784 (save-excursion
1762 numerical-fields 1790 numerical-fields
1763 last-comma page-dashes delimiters 1791 last-comma page-dashes delimiters
1764 unify-case inherit-booktitle) 1792 unify-case inherit-booktitle)
1765 bibtex-entry-format)) 1793 bibtex-entry-format))
1766 crossref-key bounds alternatives-there non-empty-alternative 1794 crossref-key bounds alternatives-there non-empty-alternative
1767 entry-list req-field-list field-done field-list) 1795 entry-list req-field-list field-list)
1768 1796
1769 ;; identify entry type 1797 ;; identify entry type
1770 (goto-char (point-min)) 1798 (goto-char (point-min))
1771 (re-search-forward bibtex-entry-type) 1799 (re-search-forward bibtex-entry-type)
1772 (let ((beg-type (1+ (match-beginning 0))) 1800 (let ((beg-type (1+ (match-beginning 0)))
1790 1818
1791 ;; determine if entry has crossref field and if at least 1819 ;; determine if entry has crossref field and if at least
1792 ;; one alternative is non-empty 1820 ;; one alternative is non-empty
1793 (goto-char (point-min)) 1821 (goto-char (point-min))
1794 (let* ((fields-alist (bibtex-parse-entry)) 1822 (let* ((fields-alist (bibtex-parse-entry))
1795 (case-fold-search t) 1823 (field (assoc-string "crossref" fields-alist t)))
1796 (field (bibtex-assoc-regexp "\\`\\(OPT\\)?crossref\\'"
1797 fields-alist)))
1798 (setq crossref-key (and field 1824 (setq crossref-key (and field
1799 (not (string-match bibtex-empty-field-re 1825 (not (string-match bibtex-empty-field-re
1800 (cdr field))) 1826 (cdr field)))
1801 (cdr field)) 1827 (cdr field))
1802 req-field-list (if crossref-key 1828 req-field-list (if crossref-key
1804 (nth 0 (nth 1 entry-list)))) ; required part 1830 (nth 0 (nth 1 entry-list)))) ; required part
1805 1831
1806 (dolist (rfield req-field-list) 1832 (dolist (rfield req-field-list)
1807 (when (nth 3 rfield) ; we should have an alternative 1833 (when (nth 3 rfield) ; we should have an alternative
1808 (setq alternatives-there t 1834 (setq alternatives-there t
1809 field (bibtex-assoc-regexp 1835 field (assoc-string (car rfield) fields-alist t))
1810 (concat "\\`\\(ALT\\)?" (car rfield) "\\'")
1811 fields-alist))
1812 (if (and field 1836 (if (and field
1813 (not (string-match bibtex-empty-field-re 1837 (not (string-match bibtex-empty-field-re
1814 (cdr field)))) 1838 (cdr field))))
1815 (cond ((not non-empty-alternative) 1839 (cond ((not non-empty-alternative)
1816 (setq non-empty-alternative t)) 1840 (setq non-empty-alternative t))
1885 (delete-char 1) 1909 (delete-char 1)
1886 (insert (bibtex-field-right-delimiter)))) 1910 (insert (bibtex-field-right-delimiter))))
1887 1911
1888 ;; update page dashes 1912 ;; update page dashes
1889 (if (and (memq 'page-dashes format) 1913 (if (and (memq 'page-dashes format)
1890 (string-match "\\`\\(OPT\\)?pages\\'" field-name) 1914 (bibtex-string= field-name "pages")
1891 (progn (goto-char beg-text) 1915 (progn (goto-char beg-text)
1892 (looking-at 1916 (looking-at
1893 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)"))) 1917 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1894 (replace-match "\\1-\\2")) 1918 (replace-match "\\1-\\2"))
1895 1919
1896 ;; use book title of crossref'd entry 1920 ;; use book title of crossref'd entry
1897 (if (and (memq 'inherit-booktitle format) 1921 (if (and (memq 'inherit-booktitle format)
1898 empty-field 1922 empty-field
1899 (equal (downcase field-name) "booktitle") 1923 (bibtex-string= field-name "booktitle")
1900 crossref-key) 1924 crossref-key)
1901 (let ((title (save-restriction 1925 (let ((title (save-restriction
1902 (widen) 1926 (widen)
1903 (if (bibtex-find-entry crossref-key) 1927 (if (bibtex-find-entry crossref-key)
1904 (bibtex-text-in-field "title"))))) 1928 (bibtex-text-in-field "title")))))
1907 (goto-char (1+ beg-text)) 1931 (goto-char (1+ beg-text))
1908 (insert title)))) 1932 (insert title))))
1909 1933
1910 ;; Use booktitle to set a missing title. 1934 ;; Use booktitle to set a missing title.
1911 (if (and empty-field 1935 (if (and empty-field
1912 (equal (downcase field-name) "title")) 1936 (bibtex-string= field-name "title"))
1913 (let ((booktitle (bibtex-text-in-field "booktitle"))) 1937 (let ((booktitle (bibtex-text-in-field "booktitle")))
1914 (when booktitle 1938 (when booktitle
1915 (setq empty-field nil) 1939 (setq empty-field nil)
1916 (goto-char (1+ beg-text)) 1940 (goto-char (1+ beg-text))
1917 (insert booktitle)))) 1941 (insert booktitle))))
1988 2012
1989 2013
1990 (defun bibtex-autokey-abbrev (string len) 2014 (defun bibtex-autokey-abbrev (string len)
1991 "Return an abbreviation of STRING with at least LEN characters. 2015 "Return an abbreviation of STRING with at least LEN characters.
1992 If LEN is positive the abbreviation is terminated only after a consonant 2016 If LEN is positive the abbreviation is terminated only after a consonant
1993 or at the word end. If LEN is negative the abbreviation is strictly 2017 or at the word end. If LEN is negative the abbreviation is strictly
1994 enforced using abs (LEN) characters. If LEN is not a number, STRING 2018 enforced using abs (LEN) characters. If LEN is not a number, STRING
1995 is returned unchanged." 2019 is returned unchanged."
1996 (cond ((or (not (numberp len)) 2020 (cond ((or (not (numberp len))
1997 (<= (length string) (abs len))) 2021 (<= (length string) (abs len)))
1998 string) 2022 string)
1999 ((equal len 0) 2023 ((equal len 0)
2005 (if abort-char 2029 (if abort-char
2006 (substring string 0 (1+ abort-char)) 2030 (substring string 0 (1+ abort-char))
2007 string))))) 2031 string)))))
2008 2032
2009 (defun bibtex-autokey-get-field (field &optional change-list) 2033 (defun bibtex-autokey-get-field (field &optional change-list)
2010 "Get content of BibTeX field FIELD. Return empty string if not found. 2034 "Get content of BibTeX field FIELD. Return empty string if not found.
2011 Optional arg CHANGE-LIST is a list of substitution patterns that is 2035 Optional arg CHANGE-LIST is a list of substitution patterns that is
2012 applied to the content of FIELD. It is an alist with pairs 2036 applied to the content of FIELD. It is an alist with pairs
2013 \(OLD-REGEXP . NEW-STRING\)." 2037 \(OLD-REGEXP . NEW-STRING\)."
2014 (let ((content (bibtex-text-in-field field bibtex-autokey-use-crossref)) 2038 (let ((content (bibtex-text-in-field field bibtex-autokey-use-crossref))
2015 case-fold-search) 2039 case-fold-search)
2016 (unless content (setq content "")) 2040 (unless content (setq content ""))
2017 (dolist (pattern change-list content) 2041 (dolist (pattern change-list content)
2021 2045
2022 (defun bibtex-autokey-get-names () 2046 (defun bibtex-autokey-get-names ()
2023 "Get contents of the name field of the current entry. 2047 "Get contents of the name field of the current entry.
2024 Do some modifications based on `bibtex-autokey-name-change-strings' 2048 Do some modifications based on `bibtex-autokey-name-change-strings'
2025 and return results as a list." 2049 and return results as a list."
2026 (let ((case-fold-search t)) 2050 (let ((case-fold-search t)
2027 (mapcar 'bibtex-autokey-demangle-name 2051 (names (bibtex-autokey-get-field "author\\|editor"
2028 (split-string (bibtex-autokey-get-field 2052 bibtex-autokey-name-change-strings)))
2029 "author\\|editor" 2053 ;; Some entries do not have a name field.
2030 bibtex-autokey-name-change-strings) 2054 (unless (string= "" names)
2031 "[ \t\n]+and[ \t\n]+")))) 2055 (mapcar 'bibtex-autokey-demangle-name
2056 (split-string names "[ \t\n]+and[ \t\n]+")))))
2032 2057
2033 (defun bibtex-autokey-demangle-name (fullname) 2058 (defun bibtex-autokey-demangle-name (fullname)
2034 "Get the last part from a well-formed name and perform abbreviations." 2059 "Get the last part from a well-formed FULLNAME and perform abbreviations."
2035 (let* (case-fold-search 2060 (let* (case-fold-search
2036 (name (cond ((string-match "\\([A-Z][^, ]*\\)[^,]*," fullname) 2061 (name (cond ((string-match "\\([A-Z][^, ]*\\)[^,]*," fullname)
2037 ;; Name is of the form "von Last, First" or 2062 ;; Name is of the form "von Last, First" or
2038 ;; "von Last, Jr, First" 2063 ;; "von Last, Jr, First"
2039 ;; --> Take the first capital part before the comma 2064 ;; --> Take the first capital part before the comma
2057 (funcall bibtex-autokey-name-case-convert name) 2082 (funcall bibtex-autokey-name-case-convert name)
2058 bibtex-autokey-name-length))) 2083 bibtex-autokey-name-length)))
2059 2084
2060 (defun bibtex-autokey-get-title () 2085 (defun bibtex-autokey-get-title ()
2061 "Get title field contents up to a terminator." 2086 "Get title field contents up to a terminator."
2062 (let ((titlestring 2087 (let ((case-fold-search t)
2088 (titlestring
2063 (bibtex-autokey-get-field "title" 2089 (bibtex-autokey-get-field "title"
2064 bibtex-autokey-titleword-change-strings))) 2090 bibtex-autokey-titleword-change-strings)))
2065 ;; ignore everything past a terminator 2091 ;; ignore everything past a terminator
2066 (let ((case-fold-search t)) 2092 (dolist (terminator bibtex-autokey-title-terminators)
2067 (dolist (terminator bibtex-autokey-title-terminators) 2093 (if (string-match terminator titlestring)
2068 (if (string-match terminator titlestring) 2094 (setq titlestring (substring titlestring 0 (match-beginning 0)))))
2069 (setq titlestring (substring titlestring 0 (match-beginning 0))))))
2070 ;; gather words from titlestring into a list. Ignore 2095 ;; gather words from titlestring into a list. Ignore
2071 ;; specific words and use only a specific amount of words. 2096 ;; specific words and use only a specific amount of words.
2072 (let ((counter 0) 2097 (let ((counter 0)
2073 case-fold-search titlewords titlewords-extra titleword end-match) 2098 titlewords titlewords-extra titleword end-match)
2074 (while (and (or (not (numberp bibtex-autokey-titlewords)) 2099 (while (and (or (not (numberp bibtex-autokey-titlewords))
2075 (< counter (+ bibtex-autokey-titlewords 2100 (< counter (+ bibtex-autokey-titlewords
2076 bibtex-autokey-titlewords-stretch))) 2101 bibtex-autokey-titlewords-stretch)))
2077 (string-match "\\b\\w+" titlestring)) 2102 (string-match "\\b\\w+" titlestring))
2078 (setq end-match (match-end 0) 2103 (setq end-match (match-end 0)
2079 titleword (substring titlestring 2104 titleword (substring titlestring
2080 (match-beginning 0) end-match)) 2105 (match-beginning 0) end-match))
2081 (unless (bibtex-member-of-regexp titleword 2106 (unless (let ((lst bibtex-autokey-titleword-ignore))
2082 bibtex-autokey-titleword-ignore) 2107 (while (and lst
2108 (not (string-match (concat "\\`\\(?:" (car lst)
2109 "\\)\\'") titleword)))
2110 (setq lst (cdr lst)))
2111 lst)
2083 (setq titleword 2112 (setq titleword
2084 (funcall bibtex-autokey-titleword-case-convert titleword)) 2113 (funcall bibtex-autokey-titleword-case-convert titleword))
2085 (if (or (not (numberp bibtex-autokey-titlewords)) 2114 (if (or (not (numberp bibtex-autokey-titlewords))
2086 (< counter bibtex-autokey-titlewords)) 2115 (< counter bibtex-autokey-titlewords))
2087 (setq titlewords (append titlewords (list titleword))) 2116 (setq titlewords (append titlewords (list titleword)))
2095 2124
2096 (defun bibtex-autokey-demangle-title (titleword) 2125 (defun bibtex-autokey-demangle-title (titleword)
2097 "Do some abbreviations on TITLEWORD. 2126 "Do some abbreviations on TITLEWORD.
2098 The rules are defined in `bibtex-autokey-titleword-abbrevs' 2127 The rules are defined in `bibtex-autokey-titleword-abbrevs'
2099 and `bibtex-autokey-titleword-length'." 2128 and `bibtex-autokey-titleword-length'."
2100 (let ((case-folde-search t) 2129 (let ((case-fold-search t)
2101 (alist bibtex-autokey-titleword-abbrevs)) 2130 (alist bibtex-autokey-titleword-abbrevs))
2102 (while (and alist 2131 (while (and alist
2103 (not (string-match (concat "\\`\\(?:" (caar alist) "\\)\\'") 2132 (not (string-match (concat "\\`\\(?:" (caar alist) "\\)\\'")
2104 titleword))) 2133 titleword)))
2105 (setq alist (cdr alist))) 2134 (setq alist (cdr alist)))
2117 use it as the name part of the key. 2146 use it as the name part of the key.
2118 3. Change any substring found in 2147 3. Change any substring found in
2119 `bibtex-autokey-name-change-strings' to the corresponding new 2148 `bibtex-autokey-name-change-strings' to the corresponding new
2120 one (see documentation of this variable for further detail). 2149 one (see documentation of this variable for further detail).
2121 4. For every of at least first `bibtex-autokey-names' names in 2150 4. For every of at least first `bibtex-autokey-names' names in
2122 the name field, determine the last name. If there are maximal 2151 the name field, determine the last name. If there are maximal
2123 `bibtex-autokey-names' + `bibtex-autokey-names-stretch' 2152 `bibtex-autokey-names' + `bibtex-autokey-names-stretch'
2124 names, all names are used. 2153 names, all names are used.
2125 5. From every last name, take at least `bibtex-autokey-name-length' 2154 5. From every last name, take at least `bibtex-autokey-name-length'
2126 characters (abort only after a consonant or at a word end). 2155 characters (abort only after a consonant or at a word end).
2127 6. Convert all last names according to the conversion function 2156 6. Convert all last names according to the conversion function
2128 `bibtex-autokey-name-case-convert'. 2157 `bibtex-autokey-name-case-convert'.
2129 7. Build the name part of the key by concatenating all 2158 7. Build the name part of the key by concatenating all
2130 abbreviated last names with the string 2159 abbreviated last names with the string
2131 `bibtex-autokey-name-separator' between any two. If there are 2160 `bibtex-autokey-name-separator' between any two. If there are
2132 more names than are used in the name part, prepend the string 2161 more names than are used in the name part, prepend the string
2133 contained in `bibtex-autokey-additional-names'. 2162 contained in `bibtex-autokey-additional-names'.
2134 8. Build the year part of the key by truncating the contents of 2163 8. Build the year part of the key by truncating the contents of
2135 the year field to the rightmost `bibtex-autokey-year-length' 2164 the year field to the rightmost `bibtex-autokey-year-length'
2136 digits (useful values are 2 and 4). If the year field (or any 2165 digits (useful values are 2 and 4). If the year field (or any
2137 other field required to generate the key) is absent, but the entry 2166 other field required to generate the key) is absent, but the entry
2138 has a valid crossref field and the variable 2167 has a valid crossref field and the variable
2139 `bibtex-autokey-use-crossref' is non-nil, use the field of the 2168 `bibtex-autokey-use-crossref' is non-nil, use the field of the
2140 crossreferenced entry instead. 2169 crossreferenced entry instead.
2141 9. For the title part of the key change the contents of the 2170 9. For the title part of the key change the contents of the
2147 the first occurrence of a regexp matched by the items of 2176 the first occurrence of a regexp matched by the items of
2148 `bibtex-autokey-title-terminators' and delete those words which 2177 `bibtex-autokey-title-terminators' and delete those words which
2149 appear in `bibtex-autokey-titleword-ignore'. 2178 appear in `bibtex-autokey-titleword-ignore'.
2150 Build the title part of the key by using at least the first 2179 Build the title part of the key by using at least the first
2151 `bibtex-autokey-titlewords' words from this 2180 `bibtex-autokey-titlewords' words from this
2152 abbreviated title. If the abbreviated title ends after 2181 abbreviated title. If the abbreviated title ends after
2153 maximal `bibtex-autokey-titlewords' + 2182 maximal `bibtex-autokey-titlewords' +
2154 `bibtex-autokey-titlewords-stretch' words, all 2183 `bibtex-autokey-titlewords-stretch' words, all
2155 words from the abbreviated title are used. 2184 words from the abbreviated title are used.
2156 11. Convert all used titlewords according to the conversion function 2185 11. Convert all used titlewords according to the conversion function
2157 `bibtex-autokey-titleword-case-convert'. 2186 `bibtex-autokey-titleword-case-convert'.
2168 15. At least, to get the key, concatenate 2197 15. At least, to get the key, concatenate
2169 `bibtex-autokey-prefix-string', the name part, the year part 2198 `bibtex-autokey-prefix-string', the name part, the year part
2170 and the title part with `bibtex-autokey-name-year-separator' 2199 and the title part with `bibtex-autokey-name-year-separator'
2171 between the name part and the year part if both are non-empty 2200 between the name part and the year part if both are non-empty
2172 and `bibtex-autokey-year-title-separator' between the year 2201 and `bibtex-autokey-year-title-separator' between the year
2173 part and the title part if both are non-empty. If the year 2202 part and the title part if both are non-empty. If the year
2174 part is empty, but not the other two parts, 2203 part is empty, but not the other two parts,
2175 `bibtex-autokey-year-title-separator' is used as well. 2204 `bibtex-autokey-year-title-separator' is used as well.
2176 16. If the value of `bibtex-autokey-before-presentation-function' 2205 16. If the value of `bibtex-autokey-before-presentation-function'
2177 is non-nil, it must be a function taking one argument. This 2206 is non-nil, it must be a function taking one argument. This
2178 function is then called with the generated key as the 2207 function is then called with the generated key as the
2179 argument. The return value of this function (a string) is 2208 argument. The return value of this function (a string) is
2180 used as the key. 2209 used as the key.
2181 17. If the value of `bibtex-autokey-edit-before-use' is non-nil, 2210 17. If the value of `bibtex-autokey-edit-before-use' is non-nil,
2182 the key is then presented in the minibuffer to the user, 2211 the key is then presented in the minibuffer to the user,
2183 where it can be edited. The key given by the user is then 2212 where it can be edited. The key given by the user is then
2184 used." 2213 used."
2228 (defun bibtex-parse-keys (&optional add abortable verbose) 2257 (defun bibtex-parse-keys (&optional add abortable verbose)
2229 "Set `bibtex-reference-keys' to the keys used in the whole buffer. 2258 "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2230 The buffer might possibly be restricted. 2259 The buffer might possibly be restricted.
2231 Find both entry keys and crossref entries. 2260 Find both entry keys and crossref entries.
2232 If ADD is non-nil add the new keys to `bibtex-reference-keys' instead of 2261 If ADD is non-nil add the new keys to `bibtex-reference-keys' instead of
2233 simply resetting it. If ADD is an alist of keys, also add ADD to 2262 simply resetting it. If ADD is an alist of keys, also add ADD to
2234 `bibtex-reference-keys'. If ABORTABLE is non-nil abort on user 2263 `bibtex-reference-keys'. If ABORTABLE is non-nil abort on user
2235 input. If VERBOSE is non-nil gives messages about progress. 2264 input. If VERBOSE is non-nil gives messages about progress.
2236 Return alist of keys if parsing was completed, `aborted' otherwise." 2265 Return alist of keys if parsing was completed, `aborted' otherwise."
2237 (let ((reference-keys (if (and add 2266 (let ((reference-keys (if (and add
2238 (listp bibtex-reference-keys)) 2267 (listp bibtex-reference-keys))
2239 bibtex-reference-keys))) 2268 bibtex-reference-keys)))
2240 (if (listp add) 2269 (if (listp add)
2294 2323
2295 (defun bibtex-parse-strings (&optional add abortable) 2324 (defun bibtex-parse-strings (&optional add abortable)
2296 "Set `bibtex-strings' to the string definitions in the whole buffer. 2325 "Set `bibtex-strings' to the string definitions in the whole buffer.
2297 The buffer might possibly be restricted. 2326 The buffer might possibly be restricted.
2298 If ADD is non-nil add the new strings to `bibtex-strings' instead of 2327 If ADD is non-nil add the new strings to `bibtex-strings' instead of
2299 simply resetting it. If ADD is an alist of strings, also add ADD to 2328 simply resetting it. If ADD is an alist of strings, also add ADD to
2300 `bibtex-strings'. If ABORTABLE is non-nil abort on user input. 2329 `bibtex-strings'. If ABORTABLE is non-nil abort on user input.
2301 Return alist of strings if parsing was completed, `aborted' otherwise." 2330 Return alist of strings if parsing was completed, `aborted' otherwise."
2302 (save-excursion 2331 (save-excursion
2303 (save-match-data 2332 (save-match-data
2304 (goto-char (point-min)) 2333 (goto-char (point-min))
2305 (let ((strings (if (and add 2334 (let ((strings (if (and add
2306 (listp bibtex-strings)) 2335 (listp bibtex-strings))
2307 bibtex-strings)) 2336 bibtex-strings))
2308 bounds key) 2337 bounds key)
2309 (if (listp add) 2338 (if (listp add)
2310 (dolist (string add) 2339 (dolist (string add)
2311 (unless (assoc (car string) strings) 2340 (unless (assoc-string (car string) strings t)
2312 (push string strings)))) 2341 (push string strings))))
2313 (catch 'userkey 2342 (catch 'userkey
2314 (while (setq bounds (bibtex-search-forward-string)) 2343 (while (setq bounds (bibtex-search-forward-string))
2315 (if (and abortable 2344 (if (and abortable
2316 (input-pending-p)) 2345 (input-pending-p))
2317 ;; user has aborted by typing a key --> return `aborted' 2346 ;; user has aborted by typing a key --> return `aborted'
2318 (throw 'userkey 'aborted)) 2347 (throw 'userkey 'aborted))
2319 (setq key (bibtex-reference-key-in-string bounds)) 2348 (setq key (bibtex-reference-key-in-string bounds))
2320 (if (not (assoc key strings)) 2349 (unless (assoc-string key strings t)
2321 (push (cons key (bibtex-text-in-string bounds t)) 2350 (push (cons key (bibtex-text-in-string bounds t))
2322 strings)) 2351 strings))
2323 (goto-char (bibtex-end-of-text-in-string bounds))) 2352 (goto-char (bibtex-end-of-text-in-string bounds)))
2324 ;; successful operation --> return `bibtex-strings' 2353 ;; successful operation --> return `bibtex-strings'
2325 (setq bibtex-strings strings)))))) 2354 (setq bibtex-strings strings))))))
2326 2355
2327 (defun bibtex-string-files-init () 2356 (defun bibtex-string-files-init ()
2355 (error "File %s not in paths defined via bibtex-string-file-path" 2384 (error "File %s not in paths defined via bibtex-string-file-path"
2356 filename)))) 2385 filename))))
2357 (append bibtex-predefined-strings (nreverse compl))))) 2386 (append bibtex-predefined-strings (nreverse compl)))))
2358 2387
2359 (defun bibtex-parse-buffers-stealthily () 2388 (defun bibtex-parse-buffers-stealthily ()
2360 "Called by `bibtex-run-with-idle-timer'. Whenever emacs has been idle 2389 "Parse buffer in the background during idle time.
2390 Called by `bibtex-run-with-idle-timer'. Whenever Emacs has been idle
2361 for `bibtex-parse-keys-timeout' seconds, all BibTeX buffers (starting 2391 for `bibtex-parse-keys-timeout' seconds, all BibTeX buffers (starting
2362 with the current) are parsed." 2392 with the current) are parsed."
2363 (save-excursion 2393 (save-excursion
2364 (let ((buffers (buffer-list)) 2394 (let ((buffers (buffer-list))
2365 (strings-init (bibtex-string-files-init))) 2395 (strings-init (bibtex-string-files-init)))
2379 ;; remember that parsing was successful 2409 ;; remember that parsing was successful
2380 (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick))))) 2410 (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick)))))
2381 (setq buffers (cdr buffers)))))) 2411 (setq buffers (cdr buffers))))))
2382 2412
2383 (defun bibtex-complete-internal (completions) 2413 (defun bibtex-complete-internal (completions)
2384 "Complete word fragment before point to longest prefix of one 2414 "Complete word fragment before point to longest prefix of COMPLETIONS.
2385 string defined in list COMPLETIONS. If point is not after the part 2415 COMPLETIONS should be a list of strings. If point is not after the part
2386 of a word, all strings are listed. Return completion." 2416 of a word, all strings are listed. Return completion."
2387 (let* ((case-fold-search t) 2417 (let* ((case-fold-search t)
2388 (beg (save-excursion 2418 (beg (save-excursion
2389 (re-search-backward "[ \t{\"]") 2419 (re-search-backward "[ \t{\"]")
2390 (forward-char) 2420 (forward-char)
2391 (point))) 2421 (point)))
2407 completions))) 2437 completions)))
2408 (message "Making completion list...done") 2438 (message "Making completion list...done")
2409 ;; return value is handled by choose-completion-string-functions 2439 ;; return value is handled by choose-completion-string-functions
2410 nil)))) 2440 nil))))
2411 2441
2412 (defun bibtex-complete-string-cleanup (str) 2442 (defun bibtex-complete-string-cleanup (str strings-alist)
2413 "Cleanup after inserting string STR. 2443 "Cleanup after inserting string STR.
2414 Remove enclosing field delimiters for string STR. Display message with 2444 Remove enclosing field delimiters for string STR. Display message with
2415 expansion of STR." 2445 expansion of STR using expansion list STRINGS-ALIST."
2416 (let ((pair (assoc str bibtex-strings))) 2446 (let ((pair (if (stringp str)
2447 (assoc-string str strings-alist t))))
2417 (when pair 2448 (when pair
2418 (if (cdr pair) 2449 (if (cdr pair)
2419 (message "Abbreviation for `%s'" (cdr pair))) 2450 (message "Abbreviation for `%s'" (cdr pair)))
2420 (save-excursion 2451 (save-excursion
2421 (bibtex-inside-field) 2452 (bibtex-inside-field)
2424 (let ((boundaries (bibtex-parse-field-string))) 2455 (let ((boundaries (bibtex-parse-field-string)))
2425 (if (and boundaries 2456 (if (and boundaries
2426 (equal (cdr boundaries) 2457 (equal (cdr boundaries)
2427 (bibtex-end-of-text-in-field bounds))) 2458 (bibtex-end-of-text-in-field bounds)))
2428 (bibtex-remove-delimiters)))))))) 2459 (bibtex-remove-delimiters))))))))
2460
2461 (defun bibtex-complete-key-cleanup (key)
2462 "Display message on entry KEY after completion of a crossref key."
2463 (save-excursion
2464 ;; Don't do anything if we completed the key of an entry.
2465 (let ((pnt (bibtex-beginning-of-entry)))
2466 (if (and (stringp key)
2467 (bibtex-find-entry key)
2468 (/= pnt (point)))
2469 (let* ((bibtex-autokey-name-case-convert 'identity)
2470 (bibtex-autokey-name-length 'infty)
2471 (nl (bibtex-autokey-get-names))
2472 (name (concat (nth 0 nl) (if (nth 1 nl) " etal")))
2473 (year (bibtex-autokey-get-field "year"))
2474 (bibtex-autokey-titlewords 5)
2475 (bibtex-autokey-titlewords-stretch 2)
2476 (bibtex-autokey-titleword-case-convert 'identity)
2477 (bibtex-autokey-titleword-length 5)
2478 (title (mapconcat 'identity
2479 (bibtex-autokey-get-title) " "))
2480 (journal (bibtex-autokey-get-field
2481 "journal" bibtex-autokey-transcriptions))
2482 (volume (bibtex-autokey-get-field "volume"))
2483 (pages (bibtex-autokey-get-field "pages" '(("-.*\\'" . "")))))
2484 (message "Ref:%s"
2485 (mapconcat (lambda (arg)
2486 (if (not (string= "" (cdr arg)))
2487 (concat (car arg) (cdr arg))))
2488 `((" " . ,name) (" " . ,year)
2489 (": " . ,title) (", " . ,journal)
2490 (" " . ,volume) (":" . ,pages))
2491 "")))))))
2429 2492
2430 (defun bibtex-choose-completion-string (choice buffer mini-p base-size) 2493 (defun bibtex-choose-completion-string (choice buffer mini-p base-size)
2431 ;; Code borrowed from choose-completion-string: 2494 ;; Code borrowed from choose-completion-string:
2432 ;; We must duplicate the code from choose-completion-string 2495 ;; We must duplicate the code from choose-completion-string
2433 ;; because it runs the hook choose-completion-string-functions 2496 ;; because it runs the hook choose-completion-string-functions
2448 ;; Update point in the window that BUFFER is showing in. 2511 ;; Update point in the window that BUFFER is showing in.
2449 (let ((window (get-buffer-window buffer t))) 2512 (let ((window (get-buffer-window buffer t)))
2450 (set-window-point window (point)))) 2513 (set-window-point window (point))))
2451 2514
2452 (defun bibtex-pop (arg direction) 2515 (defun bibtex-pop (arg direction)
2453 "Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'." 2516 "Fill current field from the ARG'th same field's text in DIRECTION.
2517 Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
2454 (let (bibtex-help-message) 2518 (let (bibtex-help-message)
2455 (bibtex-find-text nil)) 2519 (bibtex-find-text nil))
2456 (save-excursion 2520 (save-excursion
2457 ;; parse current field 2521 ;; parse current field
2458 (bibtex-inside-field) 2522 (bibtex-inside-field)
2459 (let* ((case-fold-search t) 2523 (let* ((case-fold-search t)
2460 (bounds (bibtex-enclosing-field)) 2524 (bounds (bibtex-enclosing-field))
2461 (start-old-text (bibtex-start-of-text-in-field bounds)) 2525 (start-old-text (bibtex-start-of-text-in-field bounds))
2462 (stop-old-text (bibtex-end-of-text-in-field bounds)) 2526 (stop-old-text (bibtex-end-of-text-in-field bounds))
2463 (start-name (bibtex-start-of-name-in-field bounds)) 2527 (field-name (bibtex-name-in-field bounds t)))
2464 (stop-name (bibtex-end-of-name-in-field bounds))
2465 ;; construct regexp for field with same name as this one,
2466 ;; ignoring possible OPT's or ALT's
2467 (field-name (progn
2468 (goto-char start-name)
2469 (buffer-substring-no-properties
2470 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)")
2471 (match-end 0)
2472 (point))
2473 stop-name))))
2474 ;; if executed several times in a row, start each search where 2528 ;; if executed several times in a row, start each search where
2475 ;; the last one was finished 2529 ;; the last one was finished
2476 (unless (eq last-command 'bibtex-pop) 2530 (unless (eq last-command 'bibtex-pop)
2477 (bibtex-enclosing-entry-maybe-empty-head) 2531 (bibtex-enclosing-entry-maybe-empty-head)
2478 (setq bibtex-pop-previous-search-point (match-beginning 0) 2532 (setq bibtex-pop-previous-search-point (match-beginning 0)
2521 "Major mode for editing BibTeX files. 2575 "Major mode for editing BibTeX files.
2522 2576
2523 General information on working with BibTeX mode: 2577 General information on working with BibTeX mode:
2524 2578
2525 You should use commands such as \\[bibtex-Book] to get a template for a 2579 You should use commands such as \\[bibtex-Book] to get a template for a
2526 specific entry. You should then fill in all desired fields using 2580 specific entry. You should then fill in all desired fields using
2527 \\[bibtex-next-field] to jump from field to field. After having filled 2581 \\[bibtex-next-field] to jump from field to field. After having filled
2528 in all desired fields in the entry, you should clean the new entry 2582 in all desired fields in the entry, you should clean the new entry
2529 with the command \\[bibtex-clean-entry]. 2583 with the command \\[bibtex-clean-entry].
2530 2584
2531 Some features of BibTeX mode are available only by setting the variable 2585 Some features of BibTeX mode are available only by setting the variable
2532 `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode will 2586 `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode will
2533 work only with buffers containing valid (syntactical correct) entries 2587 work only with buffers containing valid (syntactical correct) entries
2534 and with entries being sorted. This is usually the case, if you have 2588 and with entries being sorted. This is usually the case, if you have
2535 created a buffer completely with BibTeX mode and finished every new 2589 created a buffer completely with BibTeX mode and finished every new
2536 entry with \\[bibtex-clean-entry]. 2590 entry with \\[bibtex-clean-entry].
2537 2591
2538 For third party BibTeX files, call the function `bibtex-convert-alien' 2592 For third party BibTeX files, call the function `bibtex-convert-alien'
2539 to fully take advantage of all features of BibTeX mode. 2593 to fully take advantage of all features of BibTeX mode.
2637 ;; entries should be fontified in the same way as 2691 ;; entries should be fontified in the same way as
2638 ;; brace-delimited ones 2692 ;; brace-delimited ones
2639 ) 2693 )
2640 nil 2694 nil
2641 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords) 2695 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords)
2696 (font-lock-extra-managed-props . (mouse-face keymap))
2642 (font-lock-mark-block-function 2697 (font-lock-mark-block-function
2643 . (lambda () 2698 . (lambda ()
2644 (set-mark (bibtex-end-of-entry)) 2699 (set-mark (bibtex-end-of-entry))
2645 (bibtex-beginning-of-entry))))) 2700 (bibtex-beginning-of-entry)))))
2646 (setq imenu-generic-expression 2701 (setq imenu-generic-expression
2647 (list (list nil bibtex-entry-head bibtex-key-in-head))) 2702 (list (list nil bibtex-entry-head bibtex-key-in-head)))
2648 (make-local-variable 'choose-completion-string-functions) 2703 (make-local-variable 'choose-completion-string-functions)
2649 (setq imenu-case-fold-search t) 2704 (setq imenu-case-fold-search t)
2679 optional)) 2734 optional))
2680 (setq optional (append optional bibtex-user-optional-fields)) 2735 (setq optional (append optional bibtex-user-optional-fields))
2681 (cons required optional))) 2736 (cons required optional)))
2682 2737
2683 (defun bibtex-entry (entry-type) 2738 (defun bibtex-entry (entry-type)
2684 "Insert a new BibTeX entry. 2739 "Insert a new BibTeX entry of type ENTRY-TYPE.
2685 After insertion it calls the functions in `bibtex-add-entry-hook'." 2740 After insertion it calls the functions in `bibtex-add-entry-hook'."
2686 (interactive (let* ((completion-ignore-case t) 2741 (interactive (let* ((completion-ignore-case t)
2687 (e-t (completing-read 2742 (e-t (completing-read
2688 "Entry Type: " 2743 "Entry Type: "
2689 bibtex-entry-field-alist 2744 bibtex-entry-field-alist
2696 (error "Entry with key `%s' already exists" key)) 2751 (error "Entry with key `%s' already exists" key))
2697 (indent-to-column bibtex-entry-offset) 2752 (indent-to-column bibtex-entry-offset)
2698 (insert "@" entry-type (bibtex-entry-left-delimiter)) 2753 (insert "@" entry-type (bibtex-entry-left-delimiter))
2699 (if key (insert key)) 2754 (if key (insert key))
2700 (save-excursion 2755 (save-excursion
2701 (mapcar 'bibtex-make-field (car field-list)) 2756 (mapc 'bibtex-make-field (car field-list))
2702 (mapcar 'bibtex-make-optional-field (cdr field-list)) 2757 (mapc 'bibtex-make-optional-field (cdr field-list))
2703 (if bibtex-comma-after-last-field 2758 (if bibtex-comma-after-last-field
2704 (insert ",")) 2759 (insert ","))
2705 (insert "\n") 2760 (insert "\n")
2706 (indent-to-column bibtex-entry-offset) 2761 (indent-to-column bibtex-entry-offset)
2707 (insert (bibtex-entry-right-delimiter) "\n\n")) 2762 (insert (bibtex-entry-right-delimiter) "\n\n"))
2720 ;; For inserting new fields, we use the fact that 2775 ;; For inserting new fields, we use the fact that
2721 ;; bibtex-parse-entry moves point to the end of the last field. 2776 ;; bibtex-parse-entry moves point to the end of the last field.
2722 (let* ((fields-alist (bibtex-parse-entry)) 2777 (let* ((fields-alist (bibtex-parse-entry))
2723 (field-list (bibtex-field-list 2778 (field-list (bibtex-field-list
2724 (substring (cdr (assoc "=type=" fields-alist)) 2779 (substring (cdr (assoc "=type=" fields-alist))
2725 1))) ; don't want @ 2780 1)))) ; don't want @
2726 (case-fold-search t))
2727 (dolist (field (car field-list)) 2781 (dolist (field (car field-list))
2728 (unless (bibtex-assoc-regexp (concat "\\`\\(ALT\\)?" (car field) "\\'") 2782 (unless (assoc-string (car field) fields-alist t)
2729 fields-alist)
2730 (bibtex-make-field field))) 2783 (bibtex-make-field field)))
2731 (dolist (field (cdr field-list)) 2784 (dolist (field (cdr field-list))
2732 (unless (bibtex-assoc-regexp (concat "\\`\\(OPT\\)?" (car field) "\\'") 2785 (unless (assoc-string (car field) fields-alist t)
2733 fields-alist)
2734 (bibtex-make-optional-field field)))))) 2786 (bibtex-make-optional-field field))))))
2735 2787
2736 (defun bibtex-parse-entry () 2788 (defun bibtex-parse-entry ()
2737 "Parse entry at point, return an alist. 2789 "Parse entry at point, return an alist.
2738 The alist elements have the form (FIELD . TEXT), where FIELD can also be 2790 The alist elements have the form (FIELD . TEXT), where FIELD can also be
2739 the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\" 2791 the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\"
2740 TEXT may be nil. Move point to the end of the last field." 2792 TEXT may be nil. Remove \"OPT\" and \"ALT\" from FIELD.
2793 Move point to the end of the last field."
2741 (let (alist bounds) 2794 (let (alist bounds)
2742 (when (looking-at bibtex-entry-maybe-empty-head) 2795 (when (looking-at bibtex-entry-maybe-empty-head)
2743 (push (cons "=type=" (match-string bibtex-type-in-head)) alist) 2796 (push (cons "=type=" (match-string bibtex-type-in-head)) alist)
2744 (push (cons "=key=" (match-string bibtex-key-in-head)) alist) 2797 (push (cons "=key=" (match-string bibtex-key-in-head)) alist)
2745 (goto-char (match-end 0)) 2798 (goto-char (match-end 0))
2746 (while (setq bounds (bibtex-parse-field bibtex-field-name)) 2799 (while (setq bounds (bibtex-parse-field bibtex-field-name))
2747 (push (cons (bibtex-name-in-field bounds) 2800 (push (cons (bibtex-name-in-field bounds t)
2748 (bibtex-text-in-field-bounds bounds)) 2801 (bibtex-text-in-field-bounds bounds))
2749 alist) 2802 alist)
2750 (goto-char (bibtex-end-of-field bounds)))) 2803 (goto-char (bibtex-end-of-field bounds))))
2751 alist)) 2804 alist))
2752 2805
2768 (save-excursion 2821 (save-excursion
2769 (goto-char (1- (match-beginning 0))) 2822 (goto-char (1- (match-beginning 0)))
2770 (bibtex-beginning-of-entry) 2823 (bibtex-beginning-of-entry)
2771 (when (and 2824 (when (and
2772 (looking-at bibtex-entry-head) 2825 (looking-at bibtex-entry-head)
2773 (equal type (match-string bibtex-type-in-head)) 2826 (bibtex-string= type (match-string bibtex-type-in-head))
2774 ;; In case we found ourselves :-( 2827 ;; In case we found ourselves :-(
2775 (not (equal key (setq tmp (match-string bibtex-key-in-head))))) 2828 (not (equal key (setq tmp (match-string bibtex-key-in-head)))))
2776 (setq other-key tmp) 2829 (setq other-key tmp)
2777 (setq other (point)))) 2830 (setq other (point))))
2778 (save-excursion 2831 (save-excursion
2779 (bibtex-end-of-entry) 2832 (bibtex-end-of-entry)
2780 (bibtex-skip-to-valid-entry) 2833 (bibtex-skip-to-valid-entry)
2781 (when (and 2834 (when (and
2782 (looking-at bibtex-entry-head) 2835 (looking-at bibtex-entry-head)
2783 (equal type (match-string bibtex-type-in-head)) 2836 (bibtex-string= type (match-string bibtex-type-in-head))
2784 ;; In case we found ourselves :-( 2837 ;; In case we found ourselves :-(
2785 (not (equal key (setq tmp (match-string bibtex-key-in-head)))) 2838 (not (equal key (setq tmp (match-string bibtex-key-in-head))))
2786 (or (not other-key) 2839 (or (not other-key)
2787 ;; Check which is the best match. 2840 ;; Check which is the best match.
2788 (< (length (try-completion "" (list key other-key))) 2841 (< (length (try-completion "" (list key other-key)))
2792 ;; Then fill the new entry's fields with the chosen other entry. 2845 ;; Then fill the new entry's fields with the chosen other entry.
2793 (when other 2846 (when other
2794 (setq other (save-excursion (goto-char other) (bibtex-parse-entry))) 2847 (setq other (save-excursion (goto-char other) (bibtex-parse-entry)))
2795 (setq key-end (point)) ;In case parse-entry changed the buffer. 2848 (setq key-end (point)) ;In case parse-entry changed the buffer.
2796 (while (setq bounds (bibtex-parse-field bibtex-field-name)) 2849 (while (setq bounds (bibtex-parse-field bibtex-field-name))
2797 (goto-char (bibtex-start-of-name-in-field bounds)) 2850 (let ((text (assoc-string (bibtex-name-in-field bounds t)
2798 (let* ((name (buffer-substring 2851 other t)))
2799 (if (looking-at "ALT\\|OPT") (match-end 0) (point))
2800 (bibtex-end-of-name-in-field bounds)))
2801 (text (assoc-string name other t)))
2802 (goto-char (bibtex-start-of-text-in-field bounds)) 2852 (goto-char (bibtex-start-of-text-in-field bounds))
2803 (if (not (and (looking-at bibtex-empty-field-re) text)) 2853 (if (not (and (looking-at bibtex-empty-field-re) text))
2804 (goto-char (bibtex-end-of-field bounds)) 2854 (goto-char (bibtex-end-of-field bounds))
2805 (delete-region (point) (bibtex-end-of-text-in-field bounds)) 2855 (delete-region (point) (bibtex-end-of-text-in-field bounds))
2806 (insert (cdr text))))) 2856 (insert (cdr text)))))
2819 (defun bibtex-print-help-message () 2869 (defun bibtex-print-help-message ()
2820 "Print helpful information about current field in current BibTeX entry." 2870 "Print helpful information about current field in current BibTeX entry."
2821 (interactive) 2871 (interactive)
2822 (save-excursion 2872 (save-excursion
2823 (let* ((case-fold-search t) 2873 (let* ((case-fold-search t)
2824 (bounds (bibtex-enclosing-field)) 2874 (field-name (bibtex-name-in-field (bibtex-enclosing-field) t))
2825 (mb (bibtex-start-of-name-in-field bounds))
2826 (field-name (buffer-substring-no-properties
2827 (if (progn (goto-char mb)
2828 (looking-at "OPT\\|ALT"))
2829 (match-end 0) mb)
2830 (bibtex-end-of-name-in-field bounds)))
2831 (field-list (bibtex-field-list (progn (re-search-backward 2875 (field-list (bibtex-field-list (progn (re-search-backward
2832 bibtex-entry-maybe-empty-head nil t) 2876 bibtex-entry-maybe-empty-head nil t)
2833 (bibtex-type-in-head)))) 2877 (bibtex-type-in-head))))
2834 (comment (assoc-string field-name 2878 (comment (assoc-string field-name
2835 (append (car field-list) 2879 (append (car field-list)
2841 2885
2842 (defun bibtex-make-field (field &optional called-by-yank) 2886 (defun bibtex-make-field (field &optional called-by-yank)
2843 "Make a field named FIELD in current BibTeX entry. 2887 "Make a field named FIELD in current BibTeX entry.
2844 FIELD is either a string or a list of the form 2888 FIELD is either a string or a list of the form
2845 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in 2889 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
2846 `bibtex-entry-field-alist'." 2890 `bibtex-entry-field-alist'.
2891 If CALLED-BY-YANK is non-nil, don't insert delimiters."
2847 (interactive 2892 (interactive
2848 (list (let ((completion-ignore-case t) 2893 (list (let ((completion-ignore-case t)
2849 (field-list (bibtex-field-list 2894 (field-list (bibtex-field-list
2850 (save-excursion 2895 (save-excursion
2851 (bibtex-enclosing-entry-maybe-empty-head) 2896 (bibtex-enclosing-entry-maybe-empty-head)
2866 (insert (car field) " ") 2911 (insert (car field) " ")
2867 (if bibtex-align-at-equal-sign 2912 (if bibtex-align-at-equal-sign
2868 (indent-to-column (+ bibtex-entry-offset 2913 (indent-to-column (+ bibtex-entry-offset
2869 (- bibtex-text-indentation 2)))) 2914 (- bibtex-text-indentation 2))))
2870 (insert "= ") 2915 (insert "= ")
2871 (if (not bibtex-align-at-equal-sign) 2916 (unless bibtex-align-at-equal-sign
2872 (indent-to-column (+ bibtex-entry-offset 2917 (indent-to-column (+ bibtex-entry-offset
2873 bibtex-text-indentation))) 2918 bibtex-text-indentation)))
2874 (if (not called-by-yank) (insert (bibtex-field-left-delimiter))) 2919 (unless called-by-yank (insert (bibtex-field-left-delimiter)))
2875 (let ((init (nth 2 field))) 2920 (let ((init (nth 2 field)))
2876 (cond ((stringp init) 2921 (cond ((stringp init)
2877 (insert init)) 2922 (insert init))
2878 ((fboundp init) 2923 ((fboundp init)
2879 (insert (funcall init))))) 2924 (insert (funcall init)))))
2880 (if (not called-by-yank) (insert (bibtex-field-right-delimiter))) 2925 (unless called-by-yank (insert (bibtex-field-right-delimiter)))
2881 (when (interactive-p) 2926 (when (interactive-p)
2882 (forward-char -1) 2927 (forward-char -1)
2883 (bibtex-print-help-message))) 2928 (bibtex-print-help-message)))
2884 2929
2885 (defun bibtex-beginning-of-entry () 2930 (defun bibtex-beginning-of-entry ()
2886 "Move to beginning of BibTeX entry (beginning of line). 2931 "Move to beginning of BibTeX entry (beginning of line).
2887 If inside an entry, move to the beginning of it, otherwise move to the 2932 If inside an entry, move to the beginning of it, otherwise move to the
2888 beginning of the previous entry. If point is ahead of all BibTeX entries 2933 beginning of the previous entry. If point is ahead of all BibTeX entries
2889 move point to the beginning of buffer. Return the new location of point." 2934 move point to the beginning of buffer. Return the new location of point."
2890 (interactive) 2935 (interactive)
2891 (skip-chars-forward " \t") 2936 (skip-chars-forward " \t")
2892 (if (looking-at "@") 2937 (if (looking-at "@")
2893 (forward-char)) 2938 (forward-char))
2894 (re-search-backward "^[ \t]*@" nil 'move) 2939 (re-search-backward "^[ \t]*@" nil 'move)
2895 (point)) 2940 (point))
2896 2941
2897 (defun bibtex-end-of-entry () 2942 (defun bibtex-end-of-entry ()
2898 "Move to end of BibTeX entry (past the closing brace). 2943 "Move to end of BibTeX entry (past the closing brace).
2899 If inside an entry, move to the end of it, otherwise move to the end 2944 If inside an entry, move to the end of it, otherwise move to the end
2900 of the previous entry. Do not move if ahead of first entry. 2945 of the previous entry. Do not move if ahead of first entry.
2901 Return the new location of point." 2946 Return the new location of point."
2902 (interactive) 2947 (interactive)
2903 (let ((case-fold-search t) 2948 (let ((case-fold-search t)
2904 (org (point)) 2949 (org (point))
2905 (pnt (bibtex-beginning-of-entry)) 2950 (pnt (bibtex-beginning-of-entry))
2995 (widen) 3040 (widen)
2996 (narrow-to-region (bibtex-beginning-of-entry) 3041 (narrow-to-region (bibtex-beginning-of-entry)
2997 (bibtex-end-of-entry)))) 3042 (bibtex-end-of-entry))))
2998 3043
2999 (defun bibtex-entry-index () 3044 (defun bibtex-entry-index ()
3000 "Return the index of the BibTeX entry at point. Move point. 3045 "Return the index of the BibTeX entry at point. Move point.
3001 The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting 3046 The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting
3002 the entries of the BibTeX buffer. Return nil if no entry found." 3047 the entries of the BibTeX buffer. Return nil if no entry found."
3003 (let ((case-fold-search t)) 3048 (let ((case-fold-search t))
3004 (if (re-search-forward bibtex-entry-maybe-empty-head nil t) 3049 (if (re-search-forward bibtex-entry-maybe-empty-head nil t)
3005 (let ((key (bibtex-key-in-head)) 3050 (let ((key (bibtex-key-in-head))
3006 ;; all entry names should be downcase (for ease of comparison) 3051 ;; all entry names should be downcase (for ease of comparison)
3007 (entry-name (downcase (bibtex-type-in-head)))) 3052 (entry-name (downcase (bibtex-type-in-head))))
3047 (string-lessp (car index1) (car index2))))) 3092 (string-lessp (car index1) (car index2)))))
3048 3093
3049 (defun bibtex-sort-buffer () 3094 (defun bibtex-sort-buffer ()
3050 "Sort BibTeX buffer alphabetically by key. 3095 "Sort BibTeX buffer alphabetically by key.
3051 The predicate for sorting is defined via `bibtex-maintain-sorted-entries'. 3096 The predicate for sorting is defined via `bibtex-maintain-sorted-entries'.
3052 If its value is nil use plain sorting. Text outside of BibTeX entries is not 3097 If its value is nil use plain sorting. Text outside of BibTeX entries is not
3053 affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries 3098 affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
3054 will be ignored." 3099 will be ignored."
3055 (interactive) 3100 (interactive)
3056 (save-restriction 3101 (save-restriction
3057 (narrow-to-region (bibtex-beginning-of-first-entry) 3102 (narrow-to-region (bibtex-beginning-of-first-entry)
3058 (save-excursion (goto-char (point-max)) 3103 (save-excursion (goto-char (point-max))
3082 (let ((pos (save-excursion (bibtex-find-entry crossref-key)))) 3127 (let ((pos (save-excursion (bibtex-find-entry crossref-key))))
3083 (if (and pos (> (point) pos)) 3128 (if (and pos (> (point) pos))
3084 (error "This entry must not follow the crossrefed entry!")) 3129 (error "This entry must not follow the crossrefed entry!"))
3085 (goto-char pos))) 3130 (goto-char pos)))
3086 3131
3087 (defun bibtex-find-entry (key) 3132 (defun bibtex-find-entry (key &optional start)
3088 "Move point to the beginning of BibTeX entry named KEY. 3133 "Move point to the beginning of BibTeX entry named KEY.
3089 Return position of entry if KEY is found or nil if not found." 3134 Return position of entry if KEY is found or nil if not found.
3090 (interactive (list (bibtex-read-key "Find key: "))) 3135 Optional arg START is buffer position where the search starts.
3136 If it is nil, start search at beginning of buffer.
3137 With prefix arg, the value of START is position of point."
3138 (interactive (list (bibtex-read-key "Find key: ")
3139 (if current-prefix-arg (point))))
3091 (let* (case-fold-search 3140 (let* (case-fold-search
3092 (pnt (save-excursion 3141 (pnt (save-excursion
3093 (goto-char (point-min)) 3142 (goto-char (or start (point-min)))
3094 (if (re-search-forward (concat "^[ \t]*\\(" 3143 (if (re-search-forward (concat "^[ \t]*\\("
3095 bibtex-entry-type 3144 bibtex-entry-type
3096 "\\)[ \t]*[({][ \t\n]*\\(" 3145 "\\)[ \t]*[({][ \t\n]*\\("
3097 (regexp-quote key) 3146 (regexp-quote key)
3098 "\\)[ \t\n]*[,=]") 3147 "\\)[ \t\n]*[,=]")
3106 (defun bibtex-prepare-new-entry (index) 3155 (defun bibtex-prepare-new-entry (index)
3107 "Prepare a new BibTeX entry with index INDEX. 3156 "Prepare a new BibTeX entry with index INDEX.
3108 INDEX is a list (KEY CROSSREF-KEY ENTRY-NAME). 3157 INDEX is a list (KEY CROSSREF-KEY ENTRY-NAME).
3109 Move point where the entry KEY should be placed. 3158 Move point where the entry KEY should be placed.
3110 If `bibtex-maintain-sorted-entries' is non-nil, perform a binary 3159 If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
3111 search to look for place for KEY. This will fail if buffer is not in 3160 search to look for place for KEY. This will fail if buffer is not in
3112 sorted order, see \\[bibtex-validate].) 3161 sorted order, see \\[bibtex-validate].)
3113 Return t if preparation was successful or nil if entry KEY already exists." 3162 Return t if preparation was successful or nil if entry KEY already exists."
3114 (let ((key (nth 0 index)) 3163 (let ((key (nth 0 index))
3115 key-exist) 3164 key-exist)
3116 (cond ((or (null key) 3165 (cond ((or (null key)
3155 (when (or (not actual-index) 3204 (when (or (not actual-index)
3156 (bibtex-lessp actual-index index)) 3205 (bibtex-lessp actual-index index))
3157 ;; buffer contains no valid entries or 3206 ;; buffer contains no valid entries or
3158 ;; greater than last entry --> append 3207 ;; greater than last entry --> append
3159 (bibtex-end-of-entry) 3208 (bibtex-end-of-entry)
3160 (if (not (bobp)) 3209 (unless (bobp) (newline (forward-line 2)))
3161 (newline (forward-line 2)))
3162 (beginning-of-line))))) 3210 (beginning-of-line)))))
3163 (unless key-exist t))) 3211 (unless key-exist t)))
3164 3212
3165 (defun bibtex-validate (&optional test-thoroughly) 3213 (defun bibtex-validate (&optional test-thoroughly)
3166 "Validate if buffer or region is syntactically correct. 3214 "Validate if buffer or region is syntactically correct.
3231 3279
3232 (when test-thoroughly 3280 (when test-thoroughly
3233 (goto-char (point-min)) 3281 (goto-char (point-min))
3234 (bibtex-progress-message 3282 (bibtex-progress-message
3235 "Checking required fields and month fields") 3283 "Checking required fields and month fields")
3236 (let ((bibtex-sort-ignore-string-entries t) 3284 (let ((bibtex-sort-ignore-string-entries t))
3237 (questionable-month
3238 (regexp-opt (mapcar 'car bibtex-predefined-month-strings))))
3239 (bibtex-map-entries 3285 (bibtex-map-entries
3240 (lambda (key beg end) 3286 (lambda (key beg end)
3241 (bibtex-progress-message) 3287 (bibtex-progress-message)
3242 (let* ((entry-list (progn 3288 (let* ((entry-list (progn
3243 (goto-char beg) 3289 (goto-char beg)
3249 crossref-there bounds) 3295 crossref-there bounds)
3250 (goto-char beg) 3296 (goto-char beg)
3251 (while (setq bounds (bibtex-search-forward-field 3297 (while (setq bounds (bibtex-search-forward-field
3252 bibtex-field-name end)) 3298 bibtex-field-name end))
3253 (goto-char (bibtex-start-of-text-in-field bounds)) 3299 (goto-char (bibtex-start-of-text-in-field bounds))
3254 (let ((field-name (downcase (bibtex-name-in-field bounds))) 3300 (let ((field-name (bibtex-name-in-field bounds)))
3255 case-fold-search) 3301 (if (and (bibtex-string= field-name "month")
3256 (if (and (equal field-name "month") 3302 (not (assoc-string (bibtex-text-in-field-bounds bounds)
3257 (not (string-match questionable-month 3303 bibtex-predefined-month-strings t)))
3258 (bibtex-text-in-field-bounds bounds))))
3259 (push (list (bibtex-current-line) 3304 (push (list (bibtex-current-line)
3260 "Questionable month field") 3305 "Questionable month field")
3261 error-list)) 3306 error-list))
3262 (setq req (delete (assoc-string field-name req t) req) 3307 (setq req (delete (assoc-string field-name req t) req)
3263 creq (delete (assoc-string field-name creq t) creq)) 3308 creq (delete (assoc-string field-name creq t) creq))
3264 (if (equal field-name "crossref") 3309 (if (bibtex-string= field-name "crossref")
3265 (setq crossref-there t)))) 3310 (setq crossref-there t))))
3266 (if crossref-there 3311 (if crossref-there
3267 (setq req creq)) 3312 (setq req creq))
3268 (if (or (> (length req) 1) 3313 (if (or (> (length req) 1)
3269 (and (= (length req) 1) 3314 (and (= (length req) 1)
3303 "") 3348 "")
3304 "\n") 3349 "\n")
3305 (dolist (err error-list) 3350 (dolist (err error-list)
3306 (insert bufnam ":" (number-to-string (elt err 0)) 3351 (insert bufnam ":" (number-to-string (elt err 0))
3307 ": " (elt err 1) "\n")) 3352 ": " (elt err 1) "\n"))
3308 (compilation-parse-errors nil nil)
3309 (setq compilation-old-error-list compilation-error-list)
3310 ;; this is necessary to avoid reparsing of buffer if you
3311 ;; switch to compilation buffer and enter `compile-goto-error'
3312 (set-buffer-modified-p nil) 3353 (set-buffer-modified-p nil)
3313 (toggle-read-only 1) 3354 (toggle-read-only 1)
3314 (goto-char (point-min)) 3355 (goto-char (point-min))
3315 (other-window -1) 3356 (other-window -1)
3316 ;; return nil 3357 ;; return nil
3393 (defun bibtex-remove-delimiters () 3434 (defun bibtex-remove-delimiters ()
3394 "Remove \"\" or {} around string." 3435 "Remove \"\" or {} around string."
3395 (interactive) 3436 (interactive)
3396 (save-excursion 3437 (save-excursion
3397 (bibtex-inside-field) 3438 (bibtex-inside-field)
3398 (let ((bounds (bibtex-enclosing-field))) 3439 (let* ((bounds (bibtex-enclosing-field))
3399 (goto-char (bibtex-start-of-text-in-field bounds)) 3440 (end (bibtex-end-of-text-in-field bounds))
3400 (delete-char 1) 3441 (start (bibtex-start-of-text-in-field bounds)))
3401 (goto-char (1- (bibtex-end-of-text-in-field bounds))) 3442 (if (memq (char-before end) '(?\} ?\"))
3402 (delete-backward-char 1)))) 3443 (delete-region (1- end) end))
3444 (if (memq (char-after start) '(?\{ ?\"))
3445 (delete-region start (1+ start))))))
3403 3446
3404 (defun bibtex-kill-field (&optional copy-only) 3447 (defun bibtex-kill-field (&optional copy-only)
3405 "Kill the entire enclosing BibTeX field. 3448 "Kill the entire enclosing BibTeX field.
3406 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring', 3449 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
3407 but do not actually kill it." 3450 but do not actually kill it."
3453 (unless copy-only 3496 (unless copy-only
3454 (delete-region beg end)))) 3497 (delete-region beg end))))
3455 (setq bibtex-last-kill-command 'entry)) 3498 (setq bibtex-last-kill-command 'entry))
3456 3499
3457 (defun bibtex-copy-entry-as-kill () 3500 (defun bibtex-copy-entry-as-kill ()
3501 "Copy the entire enclosing BibTeX entry to `bibtex-entry-kill-ring'."
3458 (interactive) 3502 (interactive)
3459 (bibtex-kill-entry t)) 3503 (bibtex-kill-entry t))
3460 3504
3461 (defun bibtex-yank (&optional n) 3505 (defun bibtex-yank (&optional n)
3462 "Reinsert the last BibTeX item. 3506 "Reinsert the last BibTeX item.
3480 If N is negative, this is a more recent kill. 3524 If N is negative, this is a more recent kill.
3481 3525
3482 The sequence of kills wraps around, so that after the oldest one 3526 The sequence of kills wraps around, so that after the oldest one
3483 comes the newest one." 3527 comes the newest one."
3484 (interactive "*p") 3528 (interactive "*p")
3485 (if (not (eq last-command 'bibtex-yank)) 3529 (unless (eq last-command 'bibtex-yank)
3486 (error "Previous command was not a BibTeX yank")) 3530 (error "Previous command was not a BibTeX yank"))
3487 (setq this-command 'bibtex-yank) 3531 (setq this-command 'bibtex-yank)
3488 (let ((inhibit-read-only t)) 3532 (let ((inhibit-read-only t))
3489 (delete-region (point) (mark t)) 3533 (delete-region (point) (mark t))
3490 (bibtex-insert-current-kill n))) 3534 (bibtex-insert-current-kill n)))
3491 3535
3517 (defun bibtex-clean-entry (&optional new-key called-by-reformat) 3561 (defun bibtex-clean-entry (&optional new-key called-by-reformat)
3518 "Finish editing the current BibTeX entry and clean it up. 3562 "Finish editing the current BibTeX entry and clean it up.
3519 Check that no required fields are empty and formats entry dependent 3563 Check that no required fields are empty and formats entry dependent
3520 on the value of `bibtex-entry-format'. 3564 on the value of `bibtex-entry-format'.
3521 If the reference key of the entry is empty or a prefix argument is given, 3565 If the reference key of the entry is empty or a prefix argument is given,
3522 calculate a new reference key. (Note: this will only work if fields in entry 3566 calculate a new reference key. (Note: this will only work if fields in entry
3523 begin on separate lines prior to calling `bibtex-clean-entry' or if 3567 begin on separate lines prior to calling `bibtex-clean-entry' or if
3524 'realign is contained in `bibtex-entry-format'.) 3568 'realign is contained in `bibtex-entry-format'.)
3525 Don't call `bibtex-clean-entry' on @Preamble entries. 3569 Don't call `bibtex-clean-entry' on @Preamble entries.
3526 At end of the cleaning process, the functions in 3570 At end of the cleaning process, the functions in
3527 `bibtex-clean-entry-hook' are called with region narrowed to entry." 3571 `bibtex-clean-entry-hook' are called with region narrowed to entry."
3531 (let ((case-fold-search t) 3575 (let ((case-fold-search t)
3532 entry-type key) 3576 entry-type key)
3533 (bibtex-beginning-of-entry) 3577 (bibtex-beginning-of-entry)
3534 (save-excursion 3578 (save-excursion
3535 (when (re-search-forward bibtex-entry-maybe-empty-head nil t) 3579 (when (re-search-forward bibtex-entry-maybe-empty-head nil t)
3536 (setq entry-type (downcase (bibtex-type-in-head))) 3580 (setq entry-type (bibtex-type-in-head))
3537 (setq key (bibtex-key-in-head)))) 3581 (setq key (bibtex-key-in-head))))
3538 ;; formatting 3582 ;; formatting
3539 (cond ((equal entry-type "preamble") 3583 (cond ((bibtex-string= entry-type "preamble")
3540 ;; (bibtex-format-preamble) 3584 ;; (bibtex-format-preamble)
3541 (error "No clean up of @Preamble entries")) 3585 (error "No clean up of @Preamble entries"))
3542 ((equal entry-type "string")) 3586 ((bibtex-string= entry-type "string"))
3543 ;; (bibtex-format-string) 3587 ;; (bibtex-format-string)
3544 (t (bibtex-format-entry))) 3588 (t (bibtex-format-entry)))
3545 ;; set key 3589 ;; set key
3546 (when (or new-key (not key)) 3590 (when (or new-key (not key))
3547 (setq key (bibtex-generate-autokey)) 3591 (setq key (bibtex-generate-autokey))
3548 (if bibtex-autokey-edit-before-use 3592 ;; Sometimes bibtex-generate-autokey returns an empty string
3593 (if (or bibtex-autokey-edit-before-use (string= "" key))
3549 (setq key (bibtex-read-key "Key to use: " key))) 3594 (setq key (bibtex-read-key "Key to use: " key)))
3550 (re-search-forward bibtex-entry-maybe-empty-head) 3595 (re-search-forward bibtex-entry-maybe-empty-head)
3551 (if (match-beginning bibtex-key-in-head) 3596 (if (match-beginning bibtex-key-in-head)
3552 (delete-region (match-beginning bibtex-key-in-head) 3597 (delete-region (match-beginning bibtex-key-in-head)
3553 (match-end bibtex-key-in-head))) 3598 (match-end bibtex-key-in-head)))
3561 (goto-char (match-beginning 0))) 3606 (goto-char (match-beginning 0)))
3562 (point))) 3607 (point)))
3563 (entry (buffer-substring start end)) 3608 (entry (buffer-substring start end))
3564 (index (progn (goto-char start) 3609 (index (progn (goto-char start)
3565 (bibtex-entry-index))) 3610 (bibtex-entry-index)))
3566 no-error) 3611 error)
3567 (if (and bibtex-maintain-sorted-entries 3612 (if (and bibtex-maintain-sorted-entries
3568 (not (and bibtex-sort-ignore-string-entries 3613 (not (and bibtex-sort-ignore-string-entries
3569 (equal entry-type "string")))) 3614 (bibtex-string= entry-type "string"))))
3570 (progn 3615 (progn
3571 (delete-region start end) 3616 (delete-region start end)
3572 (setq no-error (bibtex-prepare-new-entry index)) 3617 (setq error (not (bibtex-prepare-new-entry index)))
3573 (insert entry) 3618 (insert entry)
3574 (forward-char -1) 3619 (forward-char -1)
3575 (bibtex-beginning-of-entry) ; moves backward 3620 (bibtex-beginning-of-entry) ; moves backward
3576 (re-search-forward bibtex-entry-head)) 3621 (re-search-forward bibtex-entry-head))
3577 (setq no-error (bibtex-find-entry (car index)))) 3622 (bibtex-find-entry key)
3578 (unless no-error 3623 (setq error (or (/= (point) start)
3624 (bibtex-find-entry key end))))
3625 (if error
3579 (error "New inserted entry yields duplicate key")))) 3626 (error "New inserted entry yields duplicate key"))))
3580 ;; final clean up 3627 ;; final clean up
3581 (unless called-by-reformat 3628 (unless called-by-reformat
3582 (save-excursion 3629 (save-excursion
3583 (save-restriction 3630 (save-restriction
3584 (bibtex-narrow-to-entry) 3631 (bibtex-narrow-to-entry)
3585 ;; Only update the list of keys if it has been built already. 3632 ;; Only update the list of keys if it has been built already.
3586 (cond ((equal entry-type "string") 3633 (cond ((bibtex-string= entry-type "string")
3587 (if (listp bibtex-strings) (bibtex-parse-strings t))) 3634 (if (listp bibtex-strings) (bibtex-parse-strings t)))
3588 ((listp bibtex-reference-keys) (bibtex-parse-keys t))) 3635 ((listp bibtex-reference-keys) (bibtex-parse-keys t)))
3589 (run-hooks 'bibtex-clean-entry-hook)))))) 3636 (run-hooks 'bibtex-clean-entry-hook))))))
3590 3637
3591 (defun bibtex-fill-field-bounds (bounds justify &optional move) 3638 (defun bibtex-fill-field-bounds (bounds justify &optional move)
3750 (message "Buffer is now parsable. Please save it."))) 3797 (message "Buffer is now parsable. Please save it.")))
3751 3798
3752 (defun bibtex-complete () 3799 (defun bibtex-complete ()
3753 "Complete word fragment before point according to context. 3800 "Complete word fragment before point according to context.
3754 If point is inside key or crossref field perform key completion based on 3801 If point is inside key or crossref field perform key completion based on
3755 `bibtex-reference-keys'. Inside any other field perform string 3802 `bibtex-reference-keys'. Inside a month field perform key completion
3756 completion based on `bibtex-strings'. An error is signaled if point 3803 based on `bibtex-predefined-month-strings'. Inside any other field
3757 is outside key or BibTeX field." 3804 perform string completion based on `bibtex-strings'. An error is
3805 signaled if point is outside key or BibTeX field."
3758 (interactive) 3806 (interactive)
3759 (let* ((pnt (point)) 3807 (let ((pnt (point))
3760 (case-fold-search t) 3808 (case-fold-search t)
3761 bounds compl) 3809 bounds name compl)
3762 (save-excursion 3810 (save-excursion
3763 (if (and (setq bounds (bibtex-enclosing-field t)) 3811 (if (and (setq bounds (bibtex-enclosing-field t))
3764 (>= pnt (bibtex-start-of-text-in-field bounds)) 3812 (>= pnt (bibtex-start-of-text-in-field bounds))
3765 (<= pnt (bibtex-end-of-text-in-field bounds))) 3813 (<= pnt (bibtex-end-of-text-in-field bounds)))
3766 (progn 3814 (setq name (bibtex-name-in-field bounds t)
3767 (goto-char (bibtex-start-of-name-in-field bounds)) 3815 compl (cond ((bibtex-string= name "crossref")
3768 (setq compl (if (string= "crossref" 3816 'key)
3769 (downcase 3817 ((bibtex-string= name "month")
3770 (buffer-substring-no-properties 3818 bibtex-predefined-month-strings)
3771 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)") 3819 (t (if (listp bibtex-strings)
3772 (match-end 0) 3820 bibtex-strings
3773 (point)) 3821 ;; so that bibtex-complete-string-cleanup
3774 (bibtex-end-of-name-in-field bounds)))) 3822 ;; can do its job
3775 'key 3823 (bibtex-parse-strings
3776 'str))) 3824 (bibtex-string-files-init))))))
3777 (bibtex-beginning-of-entry) 3825 (bibtex-beginning-of-entry)
3778 (if (and (re-search-forward bibtex-entry-maybe-empty-head nil t) 3826 (if (and (re-search-forward bibtex-entry-maybe-empty-head nil t)
3779 ;; point is inside a key 3827 ;; point is inside a key
3780 (or (and (match-beginning bibtex-key-in-head) 3828 (or (and (match-beginning bibtex-key-in-head)
3781 (>= pnt (match-beginning bibtex-key-in-head)) 3829 (>= pnt (match-beginning bibtex-key-in-head))
3787 3835
3788 (cond ((equal compl 'key) 3836 (cond ((equal compl 'key)
3789 ;; key completion 3837 ;; key completion
3790 (setq choose-completion-string-functions 3838 (setq choose-completion-string-functions
3791 (lambda (choice buffer mini-p base-size) 3839 (lambda (choice buffer mini-p base-size)
3792 (bibtex-choose-completion-string choice buffer mini-p base-size) 3840 (bibtex-choose-completion-string choice buffer mini-p base-size)
3793 (if bibtex-complete-key-cleanup 3841 (bibtex-complete-key-cleanup choice)
3794 (funcall bibtex-complete-key-cleanup choice))
3795 ;; return t (required by choose-completion-string-functions) 3842 ;; return t (required by choose-completion-string-functions)
3796 t)) 3843 t))
3797 (let ((choice (bibtex-complete-internal bibtex-reference-keys))) 3844 (bibtex-complete-key-cleanup (bibtex-complete-internal
3798 (if bibtex-complete-key-cleanup 3845 bibtex-reference-keys)))
3799 (funcall bibtex-complete-key-cleanup choice)))) 3846
3800 3847 (compl
3801 ((equal compl 'str)
3802 ;; string completion 3848 ;; string completion
3803 (setq choose-completion-string-functions 3849 (setq choose-completion-string-functions
3804 (lambda (choice buffer mini-p base-size) 3850 `(lambda (choice buffer mini-p base-size)
3805 (bibtex-choose-completion-string choice buffer mini-p base-size) 3851 (bibtex-choose-completion-string choice buffer mini-p base-size)
3806 (bibtex-complete-string-cleanup choice) 3852 (bibtex-complete-string-cleanup choice ',compl)
3807 ;; return t (required by choose-completion-string-functions) 3853 ;; return t (required by choose-completion-string-functions)
3808 t)) 3854 t))
3809 (bibtex-complete-string-cleanup (bibtex-complete-internal bibtex-strings))) 3855 (bibtex-complete-string-cleanup (bibtex-complete-internal compl)
3856 compl))
3810 3857
3811 (t (error "Point outside key or BibTeX field"))))) 3858 (t (error "Point outside key or BibTeX field")))))
3812 3859
3813 (defun bibtex-Article () 3860 (defun bibtex-Article ()
3814 "Insert a new BibTeX @Article entry; see also `bibtex-entry'." 3861 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
3878 (defun bibtex-String (&optional key) 3925 (defun bibtex-String (&optional key)
3879 "Insert a new BibTeX @String entry with key KEY." 3926 "Insert a new BibTeX @String entry with key KEY."
3880 (interactive (list (completing-read "String key: " bibtex-strings 3927 (interactive (list (completing-read "String key: " bibtex-strings
3881 nil nil nil 'bibtex-key-history))) 3928 nil nil nil 'bibtex-key-history)))
3882 (let ((bibtex-maintain-sorted-entries 3929 (let ((bibtex-maintain-sorted-entries
3883 (if (not bibtex-sort-ignore-string-entries) 3930 (unless bibtex-sort-ignore-string-entries
3884 bibtex-maintain-sorted-entries)) 3931 bibtex-maintain-sorted-entries))
3885 endpos) 3932 endpos)
3886 (unless (bibtex-prepare-new-entry (list key nil "String")) 3933 (unless (bibtex-prepare-new-entry (list key nil "String"))
3887 (error "Entry with key `%s' already exists" key)) 3934 (error "Entry with key `%s' already exists" key))
3888 (if (zerop (length key)) (setq key nil)) 3935 (if (zerop (length key)) (setq key nil))
3889 (indent-to-column bibtex-entry-offset) 3936 (indent-to-column bibtex-entry-offset)
3911 (let ((endpos (point))) 3958 (let ((endpos (point)))
3912 (insert (bibtex-entry-right-delimiter) 3959 (insert (bibtex-entry-right-delimiter)
3913 "\n") 3960 "\n")
3914 (goto-char endpos))) 3961 (goto-char endpos)))
3915 3962
3963 (defun bibtex-url (&optional event)
3964 "Browse a URL for the BibTeX entry at position PNT.
3965 The URL is generated using the schemes defined in `bibtex-generate-url-list'
3966 \(see there\). Then the URL is passed to `browse-url'."
3967 (interactive (list last-input-event))
3968 (save-excursion
3969 (if event (posn-set-point (event-end event)))
3970 (bibtex-beginning-of-entry)
3971 (let ((fields-alist (bibtex-parse-entry))
3972 (case-fold-search t)
3973 (lst bibtex-generate-url-list)
3974 field url scheme)
3975 (while (setq scheme (car lst))
3976 (when (and (setq field (cdr (assoc-string (caar scheme)
3977 fields-alist t)))
3978 (progn
3979 (if (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" field)
3980 (setq field (match-string 1 field)))
3981 (string-match (cdar scheme) field)))
3982 (setq lst nil)
3983 (if (null (cdr scheme))
3984 (setq url (match-string 0 field)))
3985 (dolist (step (cdr scheme))
3986 (cond ((stringp step)
3987 (setq url (concat url step)))
3988 ((setq field (assoc-string (car step) fields-alist t))
3989 ;; always remove field delimiters
3990 (let* ((text (if (string-match "\\`[{\"]\\(.*\\)[}\"]\\'"
3991 (cdr field))
3992 (match-string 1 (cdr field))
3993 (cdr field)))
3994 (str (if (string-match (nth 1 step) text)
3995 (cond
3996 ((functionp (nth 2 step))
3997 (funcall (nth 2 step) text))
3998 ((numberp (nth 2 step))
3999 (match-string (nth 2 step) text))
4000 (t
4001 (replace-match (nth 2 step) nil nil text)))
4002 ;; If the scheme is set up correctly,
4003 ;; we should never reach this point
4004 (error "Match failed: %s" text))))
4005 (setq url (concat url str))))
4006 ;; If the scheme is set up correctly,
4007 ;; we should never reach this point
4008 (t (error "Step failed: %s" step))))
4009 (message "%s" url)
4010 (browse-url url))
4011 (setq lst (cdr lst)))
4012 (unless url (message "No URL known.")))))
4013
4014 (defun bibtex-font-lock-url (bound)
4015 "Font-lock for URLs."
4016 (let ((case-fold-search t)
4017 (bounds (bibtex-enclosing-field t))
4018 (pnt (point))
4019 found field)
4020 ;; We use start-of-field as syntax-begin
4021 (goto-char (if bounds (bibtex-start-of-field bounds) pnt))
4022 (while (and (not found)
4023 (prog1 (re-search-forward bibtex-font-lock-url-regexp bound t)
4024 (setq field (match-string-no-properties 1)))
4025 (setq bounds (bibtex-parse-field-text))
4026 (>= bound (car bounds))
4027 (>= (car bounds) pnt))
4028 (let ((lst bibtex-generate-url-list) url)
4029 (goto-char (car bounds))
4030 (while (and (not found)
4031 (setq url (caar lst)))
4032 (when (bibtex-string= field (car url))
4033 (setq found (re-search-forward (cdr url) (cdr bounds) t)))
4034 (setq lst (cdr lst))))
4035 (goto-char (cdr bounds)))
4036 found))
4037
3916 4038
3917 ;; Make BibTeX a Feature 4039 ;; Make BibTeX a Feature
3918 4040
3919 (provide 'bibtex) 4041 (provide 'bibtex)
3920 4042