changeset 102038:b91b8e9fcb55

(bibtex-global-key-alist, bibtex-initialize): Exclude current buffer if it does not use bibtex-mode. (bibtex-parse-strings, bibtex-strings, bibtex-initialize) (bibtex-sort-buffer, bibtex-search-entry) (bibtex-prepare-new-entry, bibtex-clean-entry): Use functionp to test whether bibtex-strings and bibtex-reference-keys have their initial values.
author Roland Winkler <Roland.Winkler@physik.uni-erlangen.de>
date Sat, 14 Feb 2009 16:57:05 +0000
parents 53e826bf3d94
children 53a6f4b7cab5
files lisp/textmodes/bibtex.el
diffstat 1 files changed, 36 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/textmodes/bibtex.el	Sat Feb 14 14:12:15 2009 +0000
+++ b/lisp/textmodes/bibtex.el	Sat Feb 14 16:57:05 2009 +0000
@@ -2440,8 +2440,10 @@
       (apply 'append
              (mapcar (lambda (buf)
                        (with-current-buffer buf bibtex-reference-keys))
-                     (bibtex-initialize t)))
-    bibtex-reference-keys))
+                     ;; include current buffer only if it uses `bibtex-mode'
+                     (bibtex-initialize (eq major-mode 'bibtex-mode))))
+    (if (eq major-mode 'bibtex-mode)
+        bibtex-reference-keys)))
 
 (defun bibtex-read-key (prompt &optional key global)
   "Read BibTeX key from minibuffer using PROMPT and default KEY.
@@ -2531,8 +2533,7 @@
   (save-excursion
     (save-match-data
       (goto-char (point-min))
-      (let ((strings (if (and add
-                              (listp bibtex-strings))
+      (let ((strings (if (and add (not (functionp bibtex-strings)))
                          bibtex-strings))
             bounds key)
         (if (listp add)
@@ -2555,8 +2556,9 @@
 
 (defun bibtex-strings ()
   "Return `bibtex-strings'.  Initialize this variable if necessary."
-  (if (listp bibtex-strings) bibtex-strings
-    (bibtex-parse-strings (bibtex-string-files-init))))
+  (if (functionp bibtex-strings)
+      (bibtex-parse-strings (bibtex-string-files-init))
+    bibtex-strings))
 
 (defun bibtex-string-files-init ()
   "Return initialization for `bibtex-strings'.
@@ -2668,7 +2670,11 @@
     (dolist (file file-list)
       (if (file-readable-p file)
         (push (find-file-noselect file) buffer-list)))
-    ;; include current buffer iff we want it
+    ;; Include current buffer iff we want it.
+    ;; Exclude current buffer if it doesn't use `bibtex-mode'.
+    ;; Thus calling `bibtex-initialize' gives meaningful results for
+    ;; any current buffer.
+    (unless (and current (eq major-mode 'bibtex-mode)) (setq current nil))
     (cond ((and current (not (memq (current-buffer) buffer-list)))
            (push (current-buffer) buffer-list))
           ((and (not current) (memq (current-buffer) buffer-list))
@@ -2676,7 +2682,7 @@
     ;; parse keys
     (dolist (buffer buffer-list)
       (with-current-buffer buffer
-        (if (or force (nlistp bibtex-reference-keys))
+        (if (or force (functionp bibtex-reference-keys))
             (bibtex-parse-keys))))
     ;; select BibTeX buffer
     (if select
@@ -3484,7 +3490,7 @@
   (bibtex-beginning-of-first-entry)     ; Needed by `sort-subr'
   (bibtex-init-sort-entry-class-alist)  ; Needed by `bibtex-lessp'.
   (if (and (eq bibtex-maintain-sorted-entries 'crossref)
-           (nlistp bibtex-reference-keys))
+           (functionp bibtex-reference-keys))
       (bibtex-parse-keys))              ; Needed by `bibtex-lessp'.
   (sort-subr nil
              'bibtex-skip-to-valid-entry   ; NEXTREC function
@@ -3591,8 +3597,7 @@
         (while (and (not found)
                     (setq buffer (pop buffer-list)))
           (with-current-buffer buffer
-            (if (and (listp bibtex-reference-keys)
-                     (cdr (assoc-string key bibtex-reference-keys)))
+            (if (cdr (assoc-string key bibtex-reference-keys))
                 ;; `bibtex-search-entry' moves point if key found
                 (setq found (bibtex-search-entry key)))))
         (cond ((and found display)
@@ -3632,7 +3637,7 @@
 Return t if preparation was successful or nil if entry KEY already exists."
   (bibtex-init-sort-entry-class-alist)  ; Needed by `bibtex-lessp'.
   (if (and (eq bibtex-maintain-sorted-entries 'crossref)
-           (nlistp bibtex-reference-keys))
+           (functionp bibtex-reference-keys))
       (bibtex-parse-keys))              ; Needed by `bibtex-lessp'.
   (let ((key (nth 0 index))
         key-exist)
@@ -4247,23 +4252,27 @@
             (if (cdr (assoc-string key bibtex-reference-keys))
                 (error "Duplicate key in %s" (buffer-file-name)))))
 
-        ;; Only update the list of keys if it has been built already.
+        ;; Only update `bibtex-strings' and `bibtex-reference-keys'
+        ;; if they have been built already.
         (cond ((eq entry-type 'string)
-               (if (and (listp bibtex-strings)
-                        (not (assoc key bibtex-strings)))
-                   (push (cons key (bibtex-text-in-string
-                                    (bibtex-parse-string) t))
-                           bibtex-strings)))
+               ;; We have a @String entry.
+               (unless (or (functionp bibtex-strings)
+                           (assoc key bibtex-strings))
+                 (push (cons key (bibtex-text-in-string
+                                  (bibtex-parse-string) t))
+                       bibtex-strings)))
               ;; We have a normal entry.
-              ((listp bibtex-reference-keys)
-               (cond ((not (assoc key bibtex-reference-keys))
-                      (push (cons key t) bibtex-reference-keys))
-                     ((not (cdr (assoc key bibtex-reference-keys)))
-                      ;; Turn a crossref key into a header key
-                      (setq bibtex-reference-keys
-                            (cons (cons key t)
-                                  (delete (list key) bibtex-reference-keys)))))
-               ;; Handle crossref key.
+              ((not (functionp bibtex-reference-keys))
+               (let ((found (assoc key bibtex-reference-keys)))
+                 (cond ((not found)
+                        (push (cons key t) bibtex-reference-keys))
+                       ((not (cdr found))
+                        ;; Turn a crossref key into a header key
+                        (setq bibtex-reference-keys
+                              (cons (cons key t)
+                                    (delete (list key) bibtex-reference-keys))))))
+               ;; If entry has a crossref key, it goes into the list
+               ;; `bibtex-reference-keys', too.
                (if (and (nth 1 index)
                         (not (assoc (nth 1 index) bibtex-reference-keys)))
                    (push (list (nth 1 index)) bibtex-reference-keys)))))