Function: reftex-translate

reftex-translate is a byte-compiled function defined in reftex-global.el.gz.

Signature

(reftex-translate FILES SEARCH-RE TRANSLATE-ALIST ERROR-FMT TEST)

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/reftex-global.el.gz
(defun reftex-translate (files search-re translate-alist error-fmt test)
  ;; In FILES, look for SEARCH-RE and replace match 1 of it with
  ;; its association in TRANSLATE-ALIST.
  ;; If we do not find an association and TEST is non-nil, query
  ;; to ignore the problematic string.
  ;; If TEST is nil, it is ignored without query.
  ;; Return the number of replacements.
  (let ((n 0)
        (opt-re (concat "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*}[^}{]*\\)*"
                        "}[^}{]*\\)*"
                        "}[^][]*\\)*"))
        (man-re (concat "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*}[^}{]*\\)*"
                        "}[^}{]*\\)*"
                        "}[^}{]*\\)*"))
        file label match-data buf macro pos cell)
    (while (setq file (pop files))
      (setq buf (reftex-get-file-buffer-force file))
      (unless buf
        (error "No such file %s" file))
      (set-buffer buf)
      (save-excursion
        (save-restriction
          (widen)
          (goto-char (point-min))
          (while (re-search-forward search-re nil t)
            (backward-char)
            (save-excursion
              (setq label (reftex-match-string 1)
                    cell (assoc label translate-alist)
                    match-data (match-data)
                    macro (reftex-what-macro 1)
                    pos (cdr macro))
              (goto-char (or pos (point)))
              (when (and macro
                         (or (looking-at "\\\\ref")
                             (looking-at "\\\\[a-zA-Z]*ref\\(range\\)?[^a-zA-Z]")
                             (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]")
                             (looking-at (format
                                          reftex-find-label-regexp-format
                                          (regexp-quote label)))
                             ;; In case the label-keyval is inside an
                             ;; optional argument to \begin{env}
                             (looking-at (concat
                                          "\\\\begin[[:space:]]*{[^}]+}"
                                          "[[:space:]]*"
                                          "\\[[^][]*"
                                          opt-re
                                          (format
                                           reftex-find-label-regexp-format
                                           (regexp-quote label))
                                          "[^]]*\\]"))
                             ;; In case the label-keyval is inside the
                             ;; first mandatory argument to \begin{env}
                             (looking-at (concat
                                          "\\\\begin[[:space:]]*{[^}]+}"
                                          "[[:space:]]*"
                                          "{[^}{]*"
                                          man-re
                                          (format
                                           reftex-find-label-regexp-format
                                           (regexp-quote label))
                                          "[^}]*}"))))
                ;; OK, we should replace it.
                (set-match-data match-data)
                (cond
                 ((and test (not cell))
                  ;; We've got a problem
                  (unwind-protect
                      (progn
                        (reftex-highlight 1 (match-beginning 0) (match-end 0))
                        (ding)
                        (or (y-or-n-p (format error-fmt label))
                            (error "Abort")))
                    (reftex-unhighlight 1)))
                 ((and test cell)
                  (incf n))
                 ((and (not test) cell)
                  ;; Replace
                  (goto-char (match-beginning 1))
                  (delete-region (match-beginning 1) (match-end 1))
                  (insert (cdr cell)))
                 (t nil))))))))
    n))