2

I'm working with Emacs on windows, I usually work locally, but I want to disable magit when I'm working on a remote file.

I'm trying to add a hook that disables magit when it detects that the file is on a remote server. I tried this snippet but it freezes tramp everytime I try to open a remote file:

(use-package magit
    :ensure t
    
    :init
    (add-hook
       'magit-mode-hook
       (lambda () (when (file-remote-p default-directory) (magit-mode -1)))))
3
  • 1
    What exactly are you trying to disable? magit-mode is a major mode for magit buffers, not something that is enabled in specific source files. Jan 21, 2021 at 13:37
  • I don't want magit to run git on the remote server everytime I open a file with Tramp. I just want it to work if I'm working with a file on my local machine. I don't know if that's possible really.
    – Fabman
    Jan 21, 2021 at 14:57
  • I'm not sure if this is an answer or not for you, but Magit mentions a fix for bad performance when using TRAMP in it's docs here: you might try their snippet out. magit.vc/manual/magit/Performance.html Jan 21, 2021 at 18:21

1 Answer 1

2

I don't think Magit will be responsible for that; but vc might?

See whether vc-ignore-dir-regexp helps. That has a default value of:

"\\`\\(?:[\\/][\\/][^\\/]+[\\/]\\|/\\(?:net\\|afs\\|\\.\\.\\.\\)/\\)\\'"

For ease of editing and reading, let's use rx syntax. The excellent xr package on GNU ELPA gives me the following rx syntax for the default vc-ignore-dir-regexp value:

(seq bos (or (seq (any "/\\") (any "/\\")
                  (one-or-more (not (any "/\\")))
                  (any "/\\"))
             (seq "/" (or "net" "afs" "...") "/"))
     eos)))

So let's try the following. I'm using tramp-methods to establish the remote path syntax possibilities.

n.b. The position of the (eval-when-compile (require 'tramp)) looks very odd, but our usage of the rx macro needs tramp-methods to be defined at byte-compile time, and this achieves that without unnecessarily loading tramp in other circumstances. We could alternatively use the rx-to-string function instead of the rx macro, but this way we retain the slight compile-time performance benefit.

(with-eval-after-load "tramp"
  (eval-when-compile (require 'tramp))
  (setq vc-ignore-dir-regexp
        (rx (seq bos
                 (or (seq (any "/\\") (any "/\\")
                          (one-or-more (not (any "/\\")))
                          (any "/\\"))
                     (seq "/" (or "net" "afs" "...") "/")
                     ;; Ignore all tramp paths.
                     (seq "/"
                          (eval (cons 'or (mapcar #'car tramp-methods)))
                          ":"
                          (zero-or-more anything)))
                 eos))))
2
  • I have put the last code in my init.el file, but sometimes when I byte-compile this file, it complains about tramp-methods not being defined. Apparently this happens when tramp wasn't loaded before byte-compiling, which of course happens when I had not used any tramp files before in this session. However, I don't understand why it needs the value of tramp-methods when compiling this. I thought it would only need it after loading tramp.el, and then tramp-methods would be defined. I'm now trying to solve this by including (eval-when-compile (require 'tramp)) in my init.el. Dec 12, 2022 at 11:14
  • Ah, yes indeed. rx is a macro, so it gets expanded at compile-time. I think you'll be able to handle this with the somewhat odd nesting of (with-eval-after-load "tramp" (eval-when-compile (require 'tramp)) ...). I'll edit the answer.
    – phils
    Dec 12, 2022 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.