Function: bibtex-search-entries

bibtex-search-entries is an interactive and byte-compiled function defined in bibtex.el.gz.

Signature

(bibtex-search-entries FIELD REGEXP &optional GLOBAL DISPLAY)

Documentation

Search BibTeX entries for FIELD matching REGEXP.

REGEXP may be a regexp to search for. If REGEXP is a function, it is called for each entry with two args, the buffer positions of beginning and end of entry. Then an entry is accepted if this function returns non-nil. If FIELD is an empty string perform search for REGEXP in whole entry. With GLOBAL non-nil, search in bibtex-files. Otherwise the search is limited to the current buffer. If DISPLAY is non-nil, display search results in bibtex-search-buffer. When called interactively, DISPLAY is t. Also, GLOBAL is t if bibtex-search-entry-globally is non-nil. A prefix arg negates the value of bibtex-search-entry-globally. Return alist with elements (KEY FILE ENTRY), where FILE is the BibTeX file of ENTRY.

Probably introduced at or before Emacs version 24.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/bibtex.el.gz
;; We could combine multiple search results with set operations
;; AND, OR, MINUS, and NOT.  Would this be useful?
;; How complicated are searches in real life?
;; We could also have other searches such as "publication year newer than...".
(defun bibtex-search-entries (field regexp &optional global display)
  "Search BibTeX entries for FIELD matching REGEXP.
REGEXP may be a regexp to search for.
If REGEXP is a function, it is called for each entry with two args,
the buffer positions of beginning and end of entry.  Then an entry
is accepted if this function returns non-nil.
If FIELD is an empty string perform search for REGEXP in whole entry.
With GLOBAL non-nil, search in `bibtex-files'.  Otherwise the search
is limited to the current buffer.
If DISPLAY is non-nil, display search results in `bibtex-search-buffer'.
When called interactively, DISPLAY is t.
Also, GLOBAL is t if `bibtex-search-entry-globally' is non-nil.
A prefix arg negates the value of `bibtex-search-entry-globally'.
Return alist with elements (KEY FILE ENTRY),
where FILE is the BibTeX file of ENTRY."
  (interactive
   (list (completing-read
          "Field: "
          (delete-dups
           (apply #'append
                  bibtex-user-optional-fields
                  (mapcar (lambda (x) (mapcar #'car (apply #'append (nthcdr 2 x))))
                          bibtex-entry-alist)))
          nil t)
         (read-string "Regexp: ")
         (if bibtex-search-entry-globally
             (not current-prefix-arg)
           current-prefix-arg)
         t))
  (let ((funp (functionp regexp))
        entries text file)
    ;; If REGEXP is a function, the value of FIELD is ignored anyway.
    ;; Yet to ensure the code below does not fail, we make FIELD
    ;; a non-empty string.
    (if (and funp (string= "" field)) (setq field "unrestricted"))
    (dolist (buffer (if (and global bibtex-files)
                        (bibtex-initialize t)
                      (list (current-buffer))))
      (with-current-buffer buffer
        (setq file (if buffer-file-name
                       (file-name-nondirectory buffer-file-name)
                     (buffer-name buffer)))
        (save-excursion
          (goto-char (point-min))
          (if (string= "" field)
              ;; Unrestricted search.
              (while (re-search-forward regexp nil t)
                (save-excursion
                  (let ((mbeg (match-beginning 0))
                        (mend (match-end 0))
                        (beg (bibtex-beginning-of-entry))
                        (end (bibtex-end-of-entry))
                        key)
                    (if (and (<= beg mbeg)
                             (<= mend end)
                             (progn
                               (goto-char beg)
                               (looking-at bibtex-entry-head))
                             (setq key (bibtex-key-in-head))
                             (not (assoc key entries)))
                        (push (list key file
                                    (buffer-substring-no-properties beg end))
                              entries)))))
            ;; The following is slow.  But it works reliably even in more
            ;; complicated cases with BibTeX string constants and crossrefed
            ;; entries.  If you prefer speed over reliability, perform an
            ;; unrestricted search.
            (bibtex-map-entries
             (lambda (key beg end)
               (if (and (cond (funp (funcall regexp beg end))
                              ((and (setq text (bibtex-text-in-field field t))
                                    (string-match regexp text))))
                        (not (assoc key entries)))
                   (push (list key file
                               (buffer-substring-no-properties beg end))
                         entries))))))))
    (if display
        (if entries
            (bibtex-display-entries entries)
          (message "No BibTeX entries %smatching `%s'"
                   (if (string= "" field) ""
                     (format-message "with field `%s' " field))
                   regexp)))
    entries))