Function: tuareg--switch-outside-build

tuareg--switch-outside-build is a natively compiled function defined in tuareg.el.

Signature

(tuareg--switch-outside-build)

Documentation

If the current buffer refers to a file under a _build directory and a corresponding file exists outside the _build directory, propose the user to switch to it. Return t if the switch was made.

Source Code

;; Defined in /nix/store/6mbh851dhnag8f92qfrlh9ckldzwf6c6-emacs-packages-deps/share/emacs/site-lisp/elpa/tuareg-20260626.936/tuareg.el
(defun tuareg--switch-outside-build ()
  "If the current buffer refers to a file under a _build
directory and a corresponding file exists outside the _build
directory, propose the user to switch to it.  Return t if the
switch was made."
  (unless noninteractive
    (let ((fpath (buffer-file-name))
	  (p nil)
          (in-build nil)
          base b)
      (when fpath
        ;; Inspired by `locate-dominating-file'.
        (setq fpath (abbreviate-file-name fpath))
        (setq base (file-name-nondirectory fpath))
        (setq fpath (file-name-directory fpath))
        (while (not (or in-build
                        (null fpath)
                        (string-match-p locate-dominating-stop-dir-regexp fpath)))
          (setq b (file-name-nondirectory (directory-file-name fpath)))
          (if (string= b "_build")
              (setq in-build t)
            (push (file-name-as-directory b) p)
            (if (equal fpath (setq fpath (file-name-directory
                                          (directory-file-name fpath))))
                (setq fpath nil))))
        (when in-build
	  ;; Make `fpath' the path without _build.
	  (setq fpath (file-name-directory (directory-file-name fpath)))
          ;; jbuilder prefixes the path with a <context> dir, not ocamlbuild
          (let* ((context (pop p))
                 (rel-fpath (concat (apply #'concat p) base))
                 (alt0 (concat fpath rel-fpath))         ; jbuilder
                 (alt1 (concat fpath context rel-fpath)) ; ocamlbuild
                 (alt (if (file-readable-p alt0) alt0))
                 (alt (or alt (if (file-readable-p alt1) alt1))))
            (if (and alt
                     (y-or-n-p "File in _build.  Switch to corresponding \
file outside _build? "))
                (progn
                  (kill-buffer)
                  (find-file alt)
                  t)
              (read-only-mode)
              (message "File in _build.  C-x C-q to edit.")
              nil)))))))