Function: reftex-do-citation

reftex-do-citation is a byte-compiled function defined in reftex-cite.el.gz.

Signature

(reftex-do-citation &optional ARG NO-INSERT FORMAT-KEY)

Documentation

This really does the work of reftex-citation.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/reftex-cite.el.gz
(defun reftex-do-citation (&optional arg no-insert format-key)
  "This really does the work of `reftex-citation'."
  (let* ((format (reftex-figure-out-cite-format arg no-insert format-key))
         (docstruct-symbol reftex-docstruct-symbol)
         (selected-entries (reftex-offer-bib-menu))
         (insert-entries selected-entries)
         entry string cite-view)

    (unless selected-entries (error "Quit"))

    (if (stringp selected-entries)
        ;; Nonexistent entry
        (setq insert-entries (list (list selected-entries
                                         (cons "&key" selected-entries)))
	      selected-entries nil)
      ;; It makes sense to compute the cite-view strings.
      (setq cite-view t))

    (when (eq (car selected-entries) 'concat)
      ;; All keys go into a single command - we need to trick a little
      ;; FIXME: Unfortunately, this means that commenting does not work right.
      (pop selected-entries)
      (let ((concat-keys (mapconcat #'car selected-entries
				    reftex-cite-key-separator)))
        (setq insert-entries
              (list (list concat-keys (cons "&key" concat-keys))))))

    (unless no-insert

      ;; We shall insert this into the buffer...
      (message "Formatting...")

      (while (setq entry (pop insert-entries))
        ;; Format the citation and insert it
        (setq string (if reftex-format-cite-function
                         (funcall reftex-format-cite-function
                                  (reftex-get-bib-field "&key" entry)
                                  format)
                       (reftex-format-citation entry format)))
        (when (or (eq reftex-cite-prompt-optional-args t)
                  (and reftex-cite-prompt-optional-args
                       (equal arg '(4))))
          (let ((start 0) (nth 0) value)
            (while (setq start (string-match "\\[\\]" string start))
              (setq value (save-match-data
			    (read-string (format "Optional argument %d: "
						 (setq nth (1+ nth))))))
              (setq string (replace-match (concat "[" value "]") t t string))
              (setq start (1+ start)))))
        ;; Should we cleanup empty optional arguments?
        ;; if the first is empty, it can be removed.  If the second is empty,
        ;; it has to go.  If there is only a single arg and empty, it can go
        ;; as well.
        (when reftex-cite-cleanup-optional-args
          (cond
           ((string-match "\\([a-zA-Z0-9]\\)\\[\\]{" string)
            (setq string (replace-match "\\1{" nil nil string)))
           ((string-match "\\[\\]\\(\\[[a-zA-Z0-9., ]+\\]\\)" string)
            (setq string (replace-match "\\1" nil nil string)))
           ((string-match "\\[\\]\\[\\]" string)
            (setq string (replace-match "" t t string)))))
        (insert string))

      ;; Reposition cursor?
      (when (string-search "?" string)
        (search-backward "?")
        (delete-char 1))

      ;; Tell AUCTeX
      (when (and reftex-mode
                 (fboundp 'LaTeX-add-bibitems)
                 reftex-plug-into-AUCTeX)
        (apply #'LaTeX-add-bibitems (mapcar #'car selected-entries)))

      ;; Produce the cite-view strings
      (when (and reftex-mode reftex-cache-cite-echo cite-view)
        (mapc (lambda (entry)
                (reftex-make-cite-echo-string entry docstruct-symbol))
              selected-entries))

      (message ""))

    (set-marker reftex-select-return-marker nil)
    (reftex-kill-buffer "*RefTeX Select*")

    ;; Check if the prefix arg was numeric, and call recursively
    (when (integerp arg)
      (if (> arg 1)
          (progn
            (skip-chars-backward "}")
            (decf arg)
            (reftex-do-citation arg))
        (forward-char 1)))

    ;; Return the citation key
    (mapcar #'car selected-entries)))