aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoland Winkler2008-02-17 21:02:29 +0000
committerRoland Winkler2008-02-17 21:02:29 +0000
commit7af32e665c0e9636633813cc9934159c1262e61e (patch)
tree16f7df15b43e6efecb54d954e582d96b267979c2
parentd79dd1b083dbdd633b143355fedf4d89ca237763 (diff)
downloademacs-7af32e665c0e9636633813cc9934159c1262e61e.tar.gz
emacs-7af32e665c0e9636633813cc9934159c1262e61e.zip
(bibtex-search-entry): Rename from bibtex-find-entry.
Add outoload cookie. (bibtex-find-entry): Alias for bibtex-search-entry. (bibtex-search-crossref): Rename from bibtex-find-crossref. (bibtex-find-crossref): Alias for bibtex-search-crossref. (bibtex-clean-entry): atomic-change-group removed. (bibtex-format-entry): Use atomic-change-group. Use unwind-protect to locate buffer location where error occured. Make error messages more specific. (bibtex-parse-keys): Only parse if buffer uses bibtex-mode.
-rw-r--r--lisp/textmodes/bibtex.el705
1 files changed, 366 insertions, 339 deletions
diff --git a/lisp/textmodes/bibtex.el b/lisp/textmodes/bibtex.el
index 1544e4fd24f..b724c5c0e90 100644
--- a/lisp/textmodes/bibtex.el
+++ b/lisp/textmodes/bibtex.el
@@ -935,7 +935,7 @@ The following is a complex example, see http://link.aps.org/linkfaq.html.
935Each rule should be of the form (REGEXP . SUBEXP), where SUBEXP 935Each rule should be of the form (REGEXP . SUBEXP), where SUBEXP
936specifies which parenthesized expression in REGEXP is a cited key. 936specifies which parenthesized expression in REGEXP is a cited key.
937Case is significant. 937Case is significant.
938Used by `bibtex-find-crossref' and for font-locking." 938Used by `bibtex-search-crossref' and for font-locking."
939 :group 'bibtex 939 :group 'bibtex
940 :type '(repeat (cons (regexp :tag "Regexp") 940 :type '(repeat (cons (regexp :tag "Regexp")
941 (integer :tag "Number")))) 941 (integer :tag "Number"))))
@@ -978,8 +978,8 @@ Used by `bibtex-find-crossref' and for font-locking."
978 (define-key km "\C-c}" 'bibtex-remove-delimiters) 978 (define-key km "\C-c}" 'bibtex-remove-delimiters)
979 (define-key km "\C-c\C-c" 'bibtex-clean-entry) 979 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
980 (define-key km "\C-c\C-q" 'bibtex-fill-entry) 980 (define-key km "\C-c\C-q" 'bibtex-fill-entry)
981 (define-key km "\C-c\C-s" 'bibtex-find-entry) 981 (define-key km "\C-c\C-s" 'bibtex-search-entry)
982 (define-key km "\C-c\C-x" 'bibtex-find-crossref) 982 (define-key km "\C-c\C-x" 'bibtex-search-crossref)
983 (define-key km "\C-c\C-t" 'bibtex-copy-summary-as-kill) 983 (define-key km "\C-c\C-t" 'bibtex-copy-summary-as-kill)
984 (define-key km "\C-c?" 'bibtex-print-help-message) 984 (define-key km "\C-c?" 'bibtex-print-help-message)
985 (define-key km "\C-c\C-p" 'bibtex-pop-previous) 985 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
@@ -1035,8 +1035,8 @@ Used by `bibtex-find-crossref' and for font-locking."
1035 "--" 1035 "--"
1036 ["Make Entry Visible" bibtex-reposition-window t]) 1036 ["Make Entry Visible" bibtex-reposition-window t])
1037 ("Moving in BibTeX Buffers" 1037 ("Moving in BibTeX Buffers"
1038 ["Find Entry" bibtex-find-entry t] 1038 ["Search Entry" bibtex-search-entry t]
1039 ["Find Crossref Entry" bibtex-find-crossref t]) 1039 ["Search Crossref Entry" bibtex-search-crossref t])
1040 "--" 1040 "--"
1041 ("Operating on Current Field" 1041 ("Operating on Current Field"
1042 ["Fill Field" fill-paragraph t] 1042 ["Fill Field" fill-paragraph t]
@@ -1546,7 +1546,7 @@ If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1546 (setq bounds (bibtex-search-forward-field 1546 (setq bounds (bibtex-search-forward-field
1547 "\\(OPT\\)?crossref" end)))) 1547 "\\(OPT\\)?crossref" end))))
1548 (let ((crossref-field (bibtex-text-in-field-bounds bounds t))) 1548 (let ((crossref-field (bibtex-text-in-field-bounds bounds t)))
1549 (if (bibtex-find-crossref crossref-field) 1549 (if (bibtex-search-crossref crossref-field)
1550 ;; Do not pass FOLLOW-CROSSREF because we want 1550 ;; Do not pass FOLLOW-CROSSREF because we want
1551 ;; to follow crossrefs only one level of recursion. 1551 ;; to follow crossrefs only one level of recursion.
1552 (bibtex-text-in-field field)))))))) 1552 (bibtex-text-in-field field))))))))
@@ -1887,254 +1887,274 @@ Formats current entry according to variable `bibtex-entry-format'."
1887 (setq bibtex-field-strings-opt 1887 (setq bibtex-field-strings-opt
1888 (bibtex-field-re-init bibtex-field-strings-alist 'strings))) 1888 (bibtex-field-re-init bibtex-field-strings-alist 'strings)))
1889 1889
1890 (save-excursion 1890 (let ((case-fold-search t)
1891 (save-restriction 1891 (format (if (eq bibtex-entry-format t)
1892 (bibtex-narrow-to-entry) 1892 '(realign opts-or-alts required-fields numerical-fields
1893 (let ((case-fold-search t) 1893 page-dashes whitespace inherit-booktitle
1894 (format (if (eq bibtex-entry-format t) 1894 last-comma delimiters unify-case braces
1895 '(realign opts-or-alts required-fields numerical-fields 1895 strings)
1896 page-dashes whitespace inherit-booktitle 1896 bibtex-entry-format))
1897 last-comma delimiters unify-case braces 1897 bounds crossref-key req-field-list default-field-list field-list
1898 strings) 1898 alt-fields error-field-name)
1899 bibtex-entry-format)) 1899 (unwind-protect
1900 bounds crossref-key req-field-list default-field-list field-list) 1900 ;; formatting (undone if error occurs)
1901 1901 (atomic-change-group
1902 ;; There are more elegant high-level functions for several tasks 1902 (save-excursion
1903 ;; done by `bibtex-format-entry'. However, they contain some 1903 (save-restriction
1904 ;; redundancy compared with what we need to do anyway. 1904 (bibtex-narrow-to-entry)
1905 ;; So for speed-up we avoid using them. 1905
1906 ;; (`bibtex-format-entry' is called many times by `bibtex-reformat'.) 1906 ;; There are more elegant high-level functions for several tasks
1907 1907 ;; done by `bibtex-format-entry'. However, they contain some
1908 ;; identify entry type 1908 ;; redundancy compared with what we need to do anyway.
1909 (goto-char (point-min)) 1909 ;; So for speed-up we avoid using them.
1910 (or (re-search-forward bibtex-entry-type nil t) 1910 ;; (`bibtex-format-entry' is called often by `bibtex-reformat'.)
1911 (error "Not inside a BibTeX entry")) 1911
1912 (let* ((beg-type (1+ (match-beginning 0))) 1912 ;; identify entry type
1913 (end-type (match-end 0)) 1913 (goto-char (point-min))
1914 (entry-list (assoc-string (buffer-substring-no-properties 1914 (or (re-search-forward bibtex-entry-type nil t)
1915 beg-type end-type) 1915 (error "Not inside a BibTeX entry"))
1916 bibtex-entry-field-alist t))) 1916 (let* ((beg-type (1+ (match-beginning 0)))
1917 1917 (end-type (match-end 0))
1918 ;; unify case of entry name 1918 (entry-list (assoc-string (buffer-substring-no-properties
1919 (when (memq 'unify-case format) 1919 beg-type end-type)
1920 (delete-region beg-type end-type) 1920 bibtex-entry-field-alist t)))
1921 (insert (car entry-list))) 1921
1922 1922 ;; unify case of entry name
1923 ;; update left entry delimiter 1923 (when (memq 'unify-case format)
1924 (when (memq 'delimiters format) 1924 (delete-region beg-type end-type)
1925 (goto-char end-type) 1925 (insert (car entry-list)))
1926 (skip-chars-forward " \t\n") 1926
1927 (delete-char 1) 1927 ;; update left entry delimiter
1928 (insert (bibtex-entry-left-delimiter))) 1928 (when (memq 'delimiters format)
1929 1929 (goto-char end-type)
1930 ;; Do we have a crossref key? 1930 (skip-chars-forward " \t\n")
1931 (goto-char (point-min))
1932 (if (setq bounds (bibtex-search-forward-field "crossref"))
1933 (let ((text (bibtex-text-in-field-bounds bounds t)))
1934 (unless (equal "" text)
1935 (setq crossref-key text))))
1936
1937 ;; list of required fields appropriate for an entry with
1938 ;; or without crossref key.
1939 (setq req-field-list (if (and crossref-key (nth 2 entry-list))
1940 (car (nth 2 entry-list))
1941 (car (nth 1 entry-list)))
1942 ;; default list of fields that may appear in this entry
1943 default-field-list (append (nth 0 (nth 1 entry-list))
1944 (nth 1 (nth 1 entry-list))
1945 bibtex-user-optional-fields)))
1946
1947 ;; process all fields
1948 (bibtex-beginning-first-field (point-min))
1949 (while (setq bounds (bibtex-parse-field))
1950 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))
1951 (end-field (copy-marker (bibtex-end-of-field bounds) t))
1952 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds)))
1953 (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))
1954 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds)))
1955 (end-text (copy-marker (bibtex-end-of-text-in-field bounds) t))
1956 (opt-alt (string-match "OPT\\|ALT"
1957 (buffer-substring-no-properties
1958 beg-name (+ beg-name 3))))
1959 (field-name (buffer-substring-no-properties
1960 (if opt-alt (+ beg-name 3) beg-name) end-name))
1961 (empty-field (equal "" (bibtex-text-in-field-bounds bounds t)))
1962 deleted)
1963
1964 (if (memq 'opts-or-alts format)
1965 ;; delete empty optional and alternative fields
1966 ;; (but keep empty required fields)
1967 (cond ((and empty-field
1968 (or opt-alt
1969 (let ((field (assoc-string
1970 field-name req-field-list t)))
1971 (or (not field) ; OPT field
1972 (nth 3 field))))) ; ALT field
1973 (delete-region beg-field end-field)
1974 (setq deleted t))
1975 ;; otherwise nonempty field: delete "OPT" or "ALT"
1976 (opt-alt
1977 (goto-char beg-name)
1978 (delete-char 3))))
1979
1980 (unless deleted
1981 (push field-name field-list)
1982
1983 ;; remove delimiters from purely numerical fields
1984 (when (and (memq 'numerical-fields format)
1985 (progn (goto-char beg-text)
1986 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1987 (goto-char end-text)
1988 (delete-char -1)
1989 (goto-char beg-text)
1990 (delete-char 1))
1991
1992 ;; update delimiters
1993 (when (memq 'delimiters format)
1994 (goto-char beg-text)
1995 (when (looking-at "[{\"]")
1996 (delete-char 1)
1997 (insert (bibtex-field-left-delimiter)))
1998 (goto-char (1- (marker-position end-text)))
1999 (when (looking-at "[}\"]")
2000 (delete-char 1) 1931 (delete-char 1)
2001 (insert (bibtex-field-right-delimiter)))) 1932 (insert (bibtex-entry-left-delimiter)))
2002 1933
2003 ;; update page dashes 1934 ;; Do we have a crossref key?
2004 (if (and (memq 'page-dashes format) 1935 (goto-char (point-min))
2005 (bibtex-string= field-name "pages") 1936 (if (setq bounds (bibtex-search-forward-field "crossref"))
2006 (progn (goto-char beg-text) 1937 (let ((text (bibtex-text-in-field-bounds bounds t)))
2007 (looking-at 1938 (unless (equal "" text)
2008 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)"))) 1939 (setq crossref-key text))))
2009 (replace-match "\\1-\\2")) 1940
2010 1941 ;; list of required fields appropriate for an entry with
2011 ;; remove whitespace at beginning and end of field 1942 ;; or without crossref key.
2012 (when (memq 'whitespace format) 1943 (setq req-field-list (if (and crossref-key (nth 2 entry-list))
2013 (goto-char beg-text) 1944 (car (nth 2 entry-list))
2014 (if (looking-at "\\([{\"]\\)[ \t\n]+") 1945 (car (nth 1 entry-list)))
2015 (replace-match "\\1")) 1946 ;; default list of fields that may appear in this entry
2016 (goto-char end-text) 1947 default-field-list (append (nth 0 (nth 1 entry-list))
2017 (if (looking-back "[ \t\n]+\\([}\"]\\)" beg-text t) 1948 (nth 1 (nth 1 entry-list))
2018 (replace-match "\\1"))) 1949 bibtex-user-optional-fields)))
2019 1950
2020 ;; enclose field text by braces according to 1951 ;; process all fields
2021 ;; `bibtex-field-braces-alist'. 1952 (bibtex-beginning-first-field (point-min))
2022 (let (case-fold-search temp) ; Case-sensitive search 1953 (while (setq bounds (bibtex-parse-field))
2023 (when (and (memq 'braces format) 1954 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))
2024 (setq temp (cdr (assoc-string field-name 1955 (end-field (copy-marker (bibtex-end-of-field bounds) t))
2025 bibtex-field-braces-opt t)))) 1956 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds)))
2026 (goto-char beg-text) 1957 (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))
2027 (while (re-search-forward temp end-text t) 1958 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds)))
2028 (let ((beg (match-beginning 0)) 1959 (end-text (copy-marker (bibtex-end-of-text-in-field bounds) t))
2029 (bounds (bibtex-find-text-internal nil t))) 1960 (opt-alt (string-match "OPT\\|ALT"
2030 (unless (or (nth 4 bounds) ; string constant 1961 (buffer-substring-no-properties
2031 ;; match already surrounded by braces 1962 beg-name (+ beg-name 3))))
2032 ;; (braces are inside field delimiters) 1963 (field-name (buffer-substring-no-properties
2033 (and (< (point) (1- (nth 2 bounds))) 1964 (if opt-alt (+ beg-name 3) beg-name) end-name))
2034 (< (1+ (nth 1 bounds)) beg) 1965 (empty-field (equal "" (bibtex-text-in-field-bounds bounds t)))
2035 (looking-at "}") 1966 deleted)
2036 (save-excursion (goto-char (1- beg)) 1967
2037 (looking-at "{")))) 1968 ;; keep track of alternatives
2038 (insert "}") 1969 (if (nth 3 (assoc-string field-name req-field-list t))
2039 (goto-char beg) 1970 (push field-name alt-fields))
2040 (insert "{"))))) 1971
2041 1972 (if (memq 'opts-or-alts format)
2042 ;; replace field text by BibTeX string constants according to 1973 ;; delete empty optional and alternative fields
2043 ;; `bibtex-field-strings-alist'. 1974 ;; (but keep empty required fields)
2044 (when (and (memq 'strings format) 1975 (cond ((and empty-field
2045 (setq temp (cdr (assoc-string field-name 1976 (or opt-alt
2046 bibtex-field-strings-opt t)))) 1977 (let ((field (assoc-string
2047 (goto-char beg-text) 1978 field-name req-field-list t)))
2048 (dolist (re temp) 1979 (or (not field) ; OPT field
2049 (while (re-search-forward (car re) end-text t) 1980 (nth 3 field))))) ; ALT field
2050 (let ((bounds (save-match-data 1981 (delete-region beg-field end-field)
2051 (bibtex-find-text-internal nil t)))) 1982 (setq deleted t))
2052 (unless (nth 4 bounds) 1983 ;; otherwise nonempty field: delete "OPT" or "ALT"
2053 ;; if match not at right subfield boundary... 1984 (opt-alt
2054 (if (< (match-end 0) (1- (nth 2 bounds))) 1985 (goto-char beg-name)
2055 (insert " # " (bibtex-field-left-delimiter)) 1986 (delete-char 3))))
2056 (delete-char 1)) 1987
2057 (replace-match (cdr re)) 1988 (unless deleted
2058 (goto-char (match-beginning 0)) 1989 (push field-name field-list)
2059 ;; if match not at left subfield boundary... 1990
2060 (if (< (1+ (nth 1 bounds)) (match-beginning 0)) 1991 ;; remove delimiters from purely numerical fields
2061 (insert (bibtex-field-right-delimiter) " # ") 1992 (when (and (memq 'numerical-fields format)
2062 (delete-backward-char 1)))))))) 1993 (progn (goto-char beg-text)
2063 1994 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
2064 ;; use book title of crossref'd entry 1995 (goto-char end-text)
2065 (if (and (memq 'inherit-booktitle format) 1996 (delete-char -1)
2066 empty-field 1997 (goto-char beg-text)
2067 (bibtex-string= field-name "booktitle") 1998 (delete-char 1))
2068 crossref-key) 1999
2069 (let ((title (save-excursion 2000 ;; update delimiters
2070 (save-restriction 2001 (when (memq 'delimiters format)
2071 (widen) 2002 (goto-char beg-text)
2072 (if (bibtex-find-entry crossref-key t) 2003 (when (looking-at "[{\"]")
2073 (bibtex-text-in-field "title")))))) 2004 (delete-char 1)
2074 (when title 2005 (insert (bibtex-field-left-delimiter)))
2075 (setq empty-field nil) 2006 (goto-char (1- (marker-position end-text)))
2076 (goto-char (1+ beg-text)) 2007 (when (looking-at "[}\"]")
2077 (insert title)))) 2008 (delete-char 1)
2078 2009 (insert (bibtex-field-right-delimiter))))
2079 ;; if empty field is a required field, complain 2010
2080 (if (and empty-field 2011 ;; update page dashes
2081 (memq 'required-fields format) 2012 (if (and (memq 'page-dashes format)
2082 (assoc-string field-name req-field-list t)) 2013 (bibtex-string= field-name "pages")
2083 (error "Mandatory field `%s' is empty" field-name)) 2014 (progn (goto-char beg-text)
2084 2015 (looking-at
2085 ;; unify case of field name 2016 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
2086 (if (memq 'unify-case format) 2017 (replace-match "\\1-\\2"))
2087 (let ((fname (car (assoc-string field-name 2018
2088 default-field-list t)))) 2019 ;; remove whitespace at beginning and end of field
2089 (if fname 2020 (when (memq 'whitespace format)
2090 (progn 2021 (goto-char beg-text)
2091 (delete-region beg-name end-name) 2022 (if (looking-at "\\([{\"]\\)[ \t\n]+")
2092 (goto-char beg-name) 2023 (replace-match "\\1"))
2093 (insert fname)) 2024 (goto-char end-text)
2094 ;; there are no rules we could follow 2025 (if (looking-back "[ \t\n]+\\([}\"]\\)" beg-text t)
2095 (downcase-region beg-name end-name)))) 2026 (replace-match "\\1")))
2096 2027
2097 ;; update point 2028 ;; enclose field text by braces according to
2098 (goto-char end-field)))) 2029 ;; `bibtex-field-braces-alist'.
2099 2030 (let (case-fold-search temp) ; Case-sensitive search
2100 ;; check whether all required fields are present 2031 (when (and (memq 'braces format)
2101 (if (memq 'required-fields format) 2032 (setq temp (cdr (assoc-string field-name
2102 (let ((found 0) alt-list) 2033 bibtex-field-braces-opt t))))
2103 (dolist (fname req-field-list) 2034 (goto-char beg-text)
2104 (cond ((nth 3 fname) ; t if field has alternative flag 2035 (while (re-search-forward temp end-text t)
2105 (push (car fname) alt-list) 2036 (let ((beg (match-beginning 0))
2106 (if (member-ignore-case (car fname) field-list) 2037 (bounds (bibtex-find-text-internal nil t)))
2107 (setq found (1+ found)))) 2038 (unless (or (nth 4 bounds) ; string constant
2108 ((not (member-ignore-case (car fname) field-list)) 2039 ;; match already surrounded by braces
2109 (error "Mandatory field `%s' is missing" (car fname))))) 2040 ;; (braces are inside field delimiters)
2110 (if alt-list 2041 (and (< (point) (1- (nth 2 bounds)))
2111 (cond ((= found 0) 2042 (< (1+ (nth 1 bounds)) beg)
2112 (error "Alternative mandatory field `%s' is missing" 2043 (looking-at "}")
2113 alt-list)) 2044 (save-excursion (goto-char (1- beg))
2114 ((> found 1) 2045 (looking-at "{"))))
2115 (error "Alternative fields `%s' are defined %s times" 2046 (insert "}")
2116 alt-list found)))))) 2047 (goto-char beg)
2117 2048 (insert "{")))))
2118 ;; update comma after last field 2049
2119 (if (memq 'last-comma format) 2050 ;; replace field text by BibTeX string constants
2120 (cond ((and bibtex-comma-after-last-field 2051 ;; according to `bibtex-field-strings-alist'.
2121 (not (looking-at ","))) 2052 (when (and (memq 'strings format)
2122 (insert ",")) 2053 (setq temp (cdr (assoc-string field-name
2123 ((and (not bibtex-comma-after-last-field) 2054 bibtex-field-strings-opt t))))
2124 (looking-at ",")) 2055 (goto-char beg-text)
2125 (delete-char 1)))) 2056 (dolist (re temp)
2126 2057 (while (re-search-forward (car re) end-text t)
2127 ;; update right entry delimiter 2058 (let ((bounds (save-match-data
2128 (if (looking-at ",") 2059 (bibtex-find-text-internal nil t))))
2129 (forward-char)) 2060 (unless (nth 4 bounds)
2130 (when (memq 'delimiters format) 2061 ;; if match not at right subfield boundary...
2131 (skip-chars-forward " \t\n") 2062 (if (< (match-end 0) (1- (nth 2 bounds)))
2132 (delete-char 1) 2063 (insert " # " (bibtex-field-left-delimiter))
2133 (insert (bibtex-entry-right-delimiter))) 2064 (delete-char 1))
2134 2065 (replace-match (cdr re))
2135 ;; realign and fill entry 2066 (goto-char (match-beginning 0))
2136 (if (memq 'realign format) 2067 ;; if match not at left subfield boundary...
2137 (bibtex-fill-entry)))))) 2068 (if (< (1+ (nth 1 bounds)) (match-beginning 0))
2069 (insert (bibtex-field-right-delimiter) " # ")
2070 (delete-backward-char 1))))))))
2071
2072 ;; use book title of crossref'd entry
2073 (if (and (memq 'inherit-booktitle format)
2074 empty-field
2075 (bibtex-string= field-name "booktitle")
2076 crossref-key)
2077 (let ((title (save-excursion
2078 (save-restriction
2079 (widen)
2080 (if (bibtex-search-entry crossref-key t)
2081 (bibtex-text-in-field "title"))))))
2082 (when title
2083 (setq empty-field nil)
2084 (goto-char (1+ beg-text))
2085 (insert title))))
2086
2087 ;; if empty field is a required field, complain
2088 (when (and empty-field
2089 (memq 'required-fields format)
2090 (assoc-string field-name req-field-list t))
2091 (setq error-field-name field-name)
2092 (error "Mandatory field `%s' is empty" field-name))
2093
2094 ;; unify case of field name
2095 (if (memq 'unify-case format)
2096 (let ((fname (car (assoc-string field-name
2097 default-field-list t))))
2098 (if fname
2099 (progn
2100 (delete-region beg-name end-name)
2101 (goto-char beg-name)
2102 (insert fname))
2103 ;; there are no rules we could follow
2104 (downcase-region beg-name end-name))))
2105
2106 ;; update point
2107 (goto-char end-field))))
2108
2109 ;; check whether all required fields are present
2110 (if (memq 'required-fields format)
2111 (let ((found 0) alt-list)
2112 (dolist (fname req-field-list)
2113 (cond ((nth 3 fname) ; t if field has alternative flag
2114 (push (car fname) alt-list)
2115 (if (member-ignore-case (car fname) field-list)
2116 (setq found (1+ found))))
2117 ((not (member-ignore-case (car fname) field-list))
2118 (error "Mandatory field `%s' is missing" (car fname)))))
2119 (if alt-list
2120 (cond ((= found 0)
2121 (if alt-fields
2122 (setq error-field-name (car (last alt-fields))))
2123 (error "Alternative mandatory field `%s' is missing"
2124 alt-list))
2125 ((> found 1)
2126 (if alt-fields
2127 (setq error-field-name (car (last alt-fields))))
2128 (error "Alternative fields `%s' are defined %s times"
2129 alt-list found))))))
2130
2131 ;; update comma after last field
2132 (if (memq 'last-comma format)
2133 (cond ((and bibtex-comma-after-last-field
2134 (not (looking-at ",")))
2135 (insert ","))
2136 ((and (not bibtex-comma-after-last-field)
2137 (looking-at ","))
2138 (delete-char 1))))
2139
2140 ;; update right entry delimiter
2141 (if (looking-at ",")
2142 (forward-char))
2143 (when (memq 'delimiters format)
2144 (skip-chars-forward " \t\n")
2145 (delete-char 1)
2146 (insert (bibtex-entry-right-delimiter)))
2147
2148 ;; realign and fill entry
2149 (if (memq 'realign format)
2150 (bibtex-fill-entry)))))
2151
2152 ;; Unwindform: move to location where error occured if possible
2153 (when error-field-name
2154 (bibtex-beginning-of-entry)
2155 (goto-char (bibtex-start-of-text-in-field
2156 (bibtex-search-forward-field error-field-name)))
2157 (bibtex-find-text)))))
2138 2158
2139(defun bibtex-field-re-init (regexp-alist type) 2159(defun bibtex-field-re-init (regexp-alist type)
2140 "Calculate optimized value for bibtex-regexp-TYPE-opt. 2160 "Calculate optimized value for bibtex-regexp-TYPE-opt.
@@ -2425,61 +2445,62 @@ on user input. If VERBOSE is non-nil give messages about progress.
2425Return alist of keys if parsing was completed, `aborted' otherwise. 2445Return alist of keys if parsing was completed, `aborted' otherwise.
2426If `bibtex-parse-keys-fast' is non-nil, use fast but simplified algorithm 2446If `bibtex-parse-keys-fast' is non-nil, use fast but simplified algorithm
2427for parsing BibTeX keys. If parsing fails, try to set this variable to nil." 2447for parsing BibTeX keys. If parsing fails, try to set this variable to nil."
2428 (let (ref-keys crossref-keys) 2448 (if (eq major-mode 'bibtex-mode)
2429 (save-excursion 2449 (let (ref-keys crossref-keys)
2430 (save-match-data 2450 (save-excursion
2431 (if verbose 2451 (save-match-data
2432 (bibtex-progress-message 2452 (if verbose
2433 (concat (buffer-name) ": parsing reference keys"))) 2453 (bibtex-progress-message
2434 (catch 'userkey 2454 (concat (buffer-name) ": parsing reference keys")))
2435 (goto-char (point-min)) 2455 (catch 'userkey
2436 (if bibtex-parse-keys-fast 2456 (goto-char (point-min))
2437 (let ((case-fold-search t) 2457 (if bibtex-parse-keys-fast
2438 (re (concat bibtex-entry-head "\\|" 2458 (let ((case-fold-search t)
2439 ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*" 2459 (re (concat bibtex-entry-head "\\|"
2440 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]"))) 2460 ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*"
2441 (while (re-search-forward re nil t) 2461 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]")))
2442 (if (and abortable (input-pending-p)) 2462 (while (re-search-forward re nil t)
2443 ;; user has aborted by typing a key --> return `aborted' 2463 (if (and abortable (input-pending-p))
2444 (throw 'userkey 'aborted)) 2464 ;; user has aborted by typing a key: return `aborted'
2445 (cond ((match-end 3) 2465 (throw 'userkey 'aborted))
2446 ;; This is a crossref. 2466 (cond ((match-end 3)
2447 (let ((key (buffer-substring-no-properties 2467 ;; This is a crossref.
2448 (1+ (match-beginning 3)) (1- (match-end 3))))) 2468 (let ((key (buffer-substring-no-properties
2469 (1+ (match-beginning 3)) (1- (match-end 3)))))
2449 (unless (assoc key crossref-keys) 2470 (unless (assoc key crossref-keys)
2450 (push (list key) crossref-keys)))) 2471 (push (list key) crossref-keys))))
2451 ;; only keys of known entries 2472 ;; only keys of known entries
2452 ((assoc-string (bibtex-type-in-head) 2473 ((assoc-string (bibtex-type-in-head)
2453 bibtex-entry-field-alist t) 2474 bibtex-entry-field-alist t)
2454 ;; This is an entry. 2475 ;; This is an entry.
2455 (let ((key (bibtex-key-in-head))) 2476 (let ((key (bibtex-key-in-head)))
2456 (unless (assoc key ref-keys) 2477 (unless (assoc key ref-keys)
2457 (push (cons key t) ref-keys))))))) 2478 (push (cons key t) ref-keys)))))))
2458 2479
2459 (let (;; ignore @String entries because they are handled 2480 (let (;; ignore @String entries because they are handled
2460 ;; separately by `bibtex-parse-strings' 2481 ;; separately by `bibtex-parse-strings'
2461 (bibtex-sort-ignore-string-entries t) 2482 (bibtex-sort-ignore-string-entries t)
2462 bounds) 2483 bounds)
2463 (bibtex-map-entries 2484 (bibtex-map-entries
2464 (lambda (key beg end) 2485 (lambda (key beg end)
2465 (if (and abortable 2486 (if (and abortable
2466 (input-pending-p)) 2487 (input-pending-p))
2467 ;; user has aborted by typing a key --> return `aborted' 2488 ;; user has aborted by typing a key: return `aborted'
2468 (throw 'userkey 'aborted)) 2489 (throw 'userkey 'aborted))
2469 (if verbose (bibtex-progress-message)) 2490 (if verbose (bibtex-progress-message))
2470 (unless (assoc key ref-keys) 2491 (unless (assoc key ref-keys)
2471 (push (cons key t) ref-keys)) 2492 (push (cons key t) ref-keys))
2472 (if (and (setq bounds (bibtex-search-forward-field "crossref" end)) 2493 (if (and (setq bounds (bibtex-search-forward-field "crossref" end))
2473 (setq key (bibtex-text-in-field-bounds bounds t)) 2494 (setq key (bibtex-text-in-field-bounds bounds t))
2474 (not (assoc key crossref-keys))) 2495 (not (assoc key crossref-keys)))
2475 (push (list key) crossref-keys)))))) 2496 (push (list key) crossref-keys))))))
2476 2497
2477 (dolist (key crossref-keys) 2498 (dolist (key crossref-keys)
2478 (unless (assoc (car key) ref-keys) (push key ref-keys))) 2499 (unless (assoc (car key) ref-keys) (push key ref-keys)))
2479 (if verbose 2500 (if verbose
2480 (bibtex-progress-message 'done)) 2501 (bibtex-progress-message 'done))
2481 ;; successful operation --> return `bibtex-reference-keys' 2502 ;; successful operation --> return `bibtex-reference-keys'
2482 (setq bibtex-reference-keys ref-keys)))))) 2503 (setq bibtex-reference-keys ref-keys)))))))
2483 2504
2484(defun bibtex-parse-strings (&optional add abortable) 2505(defun bibtex-parse-strings (&optional add abortable)
2485 "Set `bibtex-strings' to the string definitions in the whole buffer. 2506 "Set `bibtex-strings' to the string definitions in the whole buffer.
@@ -2700,7 +2721,7 @@ expansion of STR using expansion list COMPL."
2700Use `bibtex-summary-function' to generate summary." 2721Use `bibtex-summary-function' to generate summary."
2701 (save-excursion 2722 (save-excursion
2702 (if (and (stringp key) 2723 (if (and (stringp key)
2703 (bibtex-find-entry key t)) 2724 (bibtex-search-entry key t))
2704 (message "Ref: %s" (funcall bibtex-summary-function))))) 2725 (message "Ref: %s" (funcall bibtex-summary-function)))))
2705 2726
2706(defun bibtex-copy-summary-as-kill (&optional arg) 2727(defun bibtex-copy-summary-as-kill (&optional arg)
@@ -2856,7 +2877,7 @@ If NO-BUTTON is non-nil do not generate buttons."
2856 (setq start (1+ (match-beginning 1)) 2877 (setq start (1+ (match-beginning 1))
2857 end (1- (match-end 1)) 2878 end (1- (match-end 1))
2858 found (>= start pnt))) 2879 found (>= start pnt)))
2859 (if found (bibtex-button start end 'bibtex-find-crossref 2880 (if found (bibtex-button start end 'bibtex-search-crossref
2860 (buffer-substring-no-properties start end) 2881 (buffer-substring-no-properties start end)
2861 start t)) 2882 start t))
2862 found)) 2883 found))
@@ -2869,7 +2890,7 @@ BOUND limits the search."
2869 (if (re-search-forward (car matcher) bound t) 2890 (if (re-search-forward (car matcher) bound t)
2870 (let ((start (match-beginning (cdr matcher))) 2891 (let ((start (match-beginning (cdr matcher)))
2871 (end (match-end (cdr matcher)))) 2892 (end (match-end (cdr matcher))))
2872 (bibtex-button start end 'bibtex-find-crossref 2893 (bibtex-button start end 'bibtex-search-crossref
2873 (buffer-substring-no-properties start end) 2894 (buffer-substring-no-properties start end)
2874 start t t) 2895 start t t)
2875 t)))) 2896 t))))
@@ -2884,9 +2905,9 @@ BOUND limits the search."
2884 'bibtex-function 'bibtex-url 2905 'bibtex-function 'bibtex-url
2885 'help-echo (purecopy "mouse-2, RET: follow URL")) 2906 'help-echo (purecopy "mouse-2, RET: follow URL"))
2886 2907
2887(define-button-type 'bibtex-find-crossref 2908(define-button-type 'bibtex-search-crossref
2888 'action 'bibtex-button-action 2909 'action 'bibtex-button-action
2889 'bibtex-function 'bibtex-find-crossref 2910 'bibtex-function 'bibtex-search-crossref
2890 'help-echo (purecopy "mouse-2, RET: follow crossref")) 2911 'help-echo (purecopy "mouse-2, RET: follow crossref"))
2891 2912
2892(defun bibtex-button (beg end type &rest args) 2913(defun bibtex-button (beg end type &rest args)
@@ -3452,7 +3473,7 @@ are ignored."
3452 nil ; ENDKEY function 3473 nil ; ENDKEY function
3453 'bibtex-lessp)) ; PREDICATE 3474 'bibtex-lessp)) ; PREDICATE
3454 3475
3455(defun bibtex-find-crossref (crossref-key &optional pnt split noerror) 3476(defun bibtex-search-crossref (crossref-key &optional pnt split noerror)
3456 "Move point to the beginning of BibTeX entry CROSSREF-KEY. 3477 "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3457If `bibtex-files' is non-nil, search all these files. 3478If `bibtex-files' is non-nil, search all these files.
3458Otherwise the search is limited to the current buffer. 3479Otherwise the search is limited to the current buffer.
@@ -3497,7 +3518,7 @@ for a crossref key, t otherwise."
3497 3518
3498 (let (buffer pos eqb) 3519 (let (buffer pos eqb)
3499 (save-excursion 3520 (save-excursion
3500 (setq pos (bibtex-find-entry crossref-key t) 3521 (setq pos (bibtex-search-entry crossref-key t)
3501 buffer (current-buffer))) 3522 buffer (current-buffer)))
3502 (setq eqb (eq buffer (current-buffer))) 3523 (setq eqb (eq buffer (current-buffer)))
3503 (cond ((not pos) 3524 (cond ((not pos)
@@ -3514,13 +3535,15 @@ for a crossref key, t otherwise."
3514 (beginning-of-line) 3535 (beginning-of-line)
3515 (if (and eqb (> pnt pos) (not noerror)) 3536 (if (and eqb (> pnt pos) (not noerror))
3516 (error "The referencing entry must precede the crossrefed entry!")))) 3537 (error "The referencing entry must precede the crossrefed entry!"))))
3517 ;; `bibtex-find-crossref' is called noninteractively during 3538 ;; `bibtex-search-crossref' is called noninteractively during
3518 ;; clean-up of an entry. Then it is not possible to check 3539 ;; clean-up of an entry. Then it is not possible to check
3519 ;; whether the current entry and the crossrefed entry have 3540 ;; whether the current entry and the crossrefed entry have
3520 ;; the correct sorting order. 3541 ;; the correct sorting order.
3521 (eqb (goto-char pos)) 3542 (eqb (goto-char pos))
3522 (t (set-buffer buffer) (goto-char pos))) 3543 (t (set-buffer buffer) (goto-char pos)))
3523 pos)) 3544 pos))
3545;; backward compatibility
3546(defalias 'bibtex-find-crossref 'bibtex-search-crossref)
3524 3547
3525(defun bibtex-dist (pos beg end) 3548(defun bibtex-dist (pos beg end)
3526 "Return distance between POS and region delimited by BEG and END." 3549 "Return distance between POS and region delimited by BEG and END."
@@ -3528,26 +3551,30 @@ for a crossref key, t otherwise."
3528 ((< pos beg) (- beg pos)) 3551 ((< pos beg) (- beg pos))
3529 (t (- pos end)))) 3552 (t (- pos end))))
3530 3553
3531(defun bibtex-find-entry (key &optional global start display) 3554;;;###autoload
3555(defun bibtex-search-entry (key &optional global start display)
3532 "Move point to the beginning of BibTeX entry named KEY. 3556 "Move point to the beginning of BibTeX entry named KEY.
3533Return position of entry if KEY is found or nil if not found. 3557Return position of entry if KEY is found or nil if not found.
3534With prefix arg GLOBAL non-nil, search KEY in `bibtex-files'. 3558With GLOBAL non-nil, search KEY in `bibtex-files'. Otherwise the search
3535Otherwise the search is limited to the current buffer. 3559is limited to the current buffer. Optional arg START is buffer position
3536Optional arg START is buffer position where the search starts. 3560where the search starts. If it is nil, start search at beginning of buffer.
3537If it is nil, start search at beginning of buffer.
3538If DISPLAY is non-nil, display the buffer containing KEY. 3561If DISPLAY is non-nil, display the buffer containing KEY.
3539Otherwise, use `set-buffer'. DISPLAY is t when called interactively." 3562Otherwise, use `set-buffer'.
3540 (interactive (list (bibtex-read-key "Find key: " nil current-prefix-arg) 3563When called interactively, GLOBAL is t if there is a prefix arg or the current
3541 current-prefix-arg nil t)) 3564mode is not `bibtex-mode', START is nil, and DISPLAY is t."
3565 (interactive
3566 (let ((global (or current-prefix-arg (not (eq major-mode 'bibtex-mode)))))
3567 (list (bibtex-read-key "Find key: " nil global) global nil t)))
3542 (if (and global bibtex-files) 3568 (if (and global bibtex-files)
3543 (let ((buffer-list (bibtex-initialize t)) 3569 (let ((buffer-list (bibtex-initialize t))
3544 buffer found) 3570 buffer found)
3545 (while (and (not found) 3571 (while (and (not found)
3546 (setq buffer (pop buffer-list))) 3572 (setq buffer (pop buffer-list)))
3547 (with-current-buffer buffer 3573 (with-current-buffer buffer
3548 (if (cdr (assoc-string key bibtex-reference-keys)) 3574 (if (and (listp bibtex-reference-keys)
3549 ;; `bibtex-find-entry' moves point if key found 3575 (cdr (assoc-string key bibtex-reference-keys)))
3550 (setq found (bibtex-find-entry key))))) 3576 ;; `bibtex-search-entry' moves point if key found
3577 (setq found (bibtex-search-entry key)))))
3551 (cond ((and found display) 3578 (cond ((and found display)
3552 (let ((same-window-buffer-names 3579 (let ((same-window-buffer-names
3553 (cons (buffer-name buffer) same-window-buffer-names))) 3580 (cons (buffer-name buffer) same-window-buffer-names)))
@@ -3572,6 +3599,8 @@ Otherwise, use `set-buffer'. DISPLAY is t when called interactively."
3572 (if display (bibtex-reposition-window))) 3599 (if display (bibtex-reposition-window)))
3573 (display (message "Key `%s' not found" key))) 3600 (display (message "Key `%s' not found" key)))
3574 pnt))) 3601 pnt)))
3602;; backward compatibility
3603(defalias 'bibtex-find-entry 'bibtex-search-entry)
3575 3604
3576(defun bibtex-prepare-new-entry (index) 3605(defun bibtex-prepare-new-entry (index)
3577 "Prepare a new BibTeX entry with index INDEX. 3606 "Prepare a new BibTeX entry with index INDEX.
@@ -3590,7 +3619,7 @@ Return t if preparation was successful or nil if entry KEY already exists."
3590 (cond ((or (null key) 3619 (cond ((or (null key)
3591 (and (stringp key) 3620 (and (stringp key)
3592 (string-equal key "")) 3621 (string-equal key ""))
3593 (and (not (setq key-exist (bibtex-find-entry key))) 3622 (and (not (setq key-exist (bibtex-search-entry key)))
3594 (not bibtex-maintain-sorted-entries))) 3623 (not bibtex-maintain-sorted-entries)))
3595 (bibtex-move-outside-of-entry)) 3624 (bibtex-move-outside-of-entry))
3596 ;; if key-exist is non-nil due to the previous cond clause 3625 ;; if key-exist is non-nil due to the previous cond clause
@@ -3708,7 +3737,7 @@ Return t if test was successful, nil otherwise."
3708 (dolist (key (with-current-buffer buffer bibtex-reference-keys)) 3737 (dolist (key (with-current-buffer buffer bibtex-reference-keys))
3709 (when (and (cdr key) 3738 (when (and (cdr key)
3710 (cdr (assoc-string (car key) bibtex-reference-keys))) 3739 (cdr (assoc-string (car key) bibtex-reference-keys)))
3711 (bibtex-find-entry (car key)) 3740 (bibtex-search-entry (car key))
3712 (push (cons (bibtex-current-line) 3741 (push (cons (bibtex-current-line)
3713 (format "Duplicate key `%s' in %s" (car key) 3742 (format "Duplicate key `%s' in %s" (car key)
3714 (abbreviate-file-name (buffer-file-name buffer)))) 3743 (abbreviate-file-name (buffer-file-name buffer))))
@@ -3830,7 +3859,7 @@ Return t if test was successful, nil otherwise."
3830 (dolist (buffer buffer-list) 3859 (dolist (buffer buffer-list)
3831 (dolist (key (cdr (assq buffer buffer-key-list))) 3860 (dolist (key (cdr (assq buffer buffer-key-list)))
3832 (when (assoc-string key current-keys) 3861 (when (assoc-string key current-keys)
3833 (bibtex-find-entry key) 3862 (bibtex-search-entry key)
3834 (push (format "%s:%d: Duplicate key `%s' in %s\n" 3863 (push (format "%s:%d: Duplicate key `%s' in %s\n"
3835 (buffer-file-name) (bibtex-current-line) key 3864 (buffer-file-name) (bibtex-current-line) key
3836 (abbreviate-file-name (buffer-file-name buffer))) 3865 (abbreviate-file-name (buffer-file-name buffer)))
@@ -4143,15 +4172,13 @@ At end of the cleaning process, the functions in
4143 (error "Not inside a BibTeX entry"))) 4172 (error "Not inside a BibTeX entry")))
4144 (entry-type (bibtex-type-in-head)) 4173 (entry-type (bibtex-type-in-head))
4145 (key (bibtex-key-in-head))) 4174 (key (bibtex-key-in-head)))
4146 ;; formatting (undone if error occurs) 4175 (cond ((bibtex-string= entry-type "preamble")
4147 (atomic-change-group 4176 ;; (bibtex-format-preamble)
4148 (cond ((bibtex-string= entry-type "preamble") 4177 (error "No clean up of @Preamble entries"))
4149 ;; (bibtex-format-preamble) 4178 ((bibtex-string= entry-type "string")
4150 (error "No clean up of @Preamble entries")) 4179 (setq entry-type 'string))
4151 ((bibtex-string= entry-type "string") 4180 ;; (bibtex-format-string)
4152 (setq entry-type 'string)) 4181 (t (bibtex-format-entry)))
4153 ;; (bibtex-format-string)
4154 (t (bibtex-format-entry))))
4155 ;; set key 4182 ;; set key
4156 (when (or new-key (not key)) 4183 (when (or new-key (not key))
4157 (setq key (bibtex-generate-autokey)) 4184 (setq key (bibtex-generate-autokey))
@@ -4190,9 +4217,9 @@ At end of the cleaning process, the functions in
4190 (setq error (not (bibtex-prepare-new-entry index)) 4217 (setq error (not (bibtex-prepare-new-entry index))
4191 start (point)) ; update start 4218 start (point)) ; update start
4192 (save-excursion (insert entry))) 4219 (save-excursion (insert entry)))
4193 (bibtex-find-entry key) 4220 (bibtex-search-entry key)
4194 (setq error (or (/= (point) start) 4221 (setq error (or (/= (point) start)
4195 (bibtex-find-entry key nil end)))) 4222 (bibtex-search-entry key nil end))))
4196 (if error 4223 (if error
4197 (error "New inserted entry yields duplicate key")) 4224 (error "New inserted entry yields duplicate key"))
4198 (dolist (buffer (bibtex-initialize)) 4225 (dolist (buffer (bibtex-initialize))