6

Is it possible to configure emacs so it uses different theme for tramp buffers than for defaults? Say, I use twilight by default, but while editing sth via ssh I'd like to switch to pastels-on-dark...

Changing colors are great at providing a feeling that ”something is different”…

This question in fact covers a few problems:

a) whether there is any natural way to use per-buffer theme (I failed to find one)

b) if not, what is the best way to hook emacs so it switches theme depending on the current buffer (so far I have preliminary solution I am not happy with, see below)

c) and, specifically, how to adapt that to tramp case

Note: I found load-theme-buffer-local. For some reason it does not work for me (once I call it on some buffer, the same theme remains active for every other buffer too).


Here is a partial solution for b) case I found some-time ago. I am not happy with it (it fires only when I open new file and does not react to buffer switch, it fails to revert to default in some cases, and it works only for per-dir-tree case), but maybe it could be improved:

;; in .emacs

(defvar my-local-theme nil 
   "Name of current theme (intended to be overriden where necessary)")

(defun my-set-local-theme ()
   "If variable mkylocal-theme is set, loads this theme."
   (interactive)
   (when (bound-and-true-p my-local-theme)
       (unless(member my-local-theme custom-enabled-themes)
       (message "Setting local theme to %s on %s" my-local-theme (current-buffer))
       (load-theme my-local-theme t))))

(add-hook 'hack-local-variables-hook 'my-set-local-theme)

;; In .dir-locals.el on top of some source tree

(
  (nil
    (my-local-theme . twilight-anti-bright)
  ))

For completeness: I also use the following trick to load themes cleanly, so the code above does not unload themes.

(add-hook 'after-init-hook
      (lambda nil
        (defadvice load-theme 
          (before theme-dont-propagate activate)
          (mapcar #'disable-theme custom-enabled-themes))))

2 Answers 2

4

I don't know how to enable/disable custom-themes for a buffer only. The following works for changing the global custom-theme when you enter a Tramp buffer, or when you leave it.

(add-hook
  'buffer-list-update-hook
  (lambda ()
    (if (and (not (window-minibuffer-p))
        (file-remote-p default-directory))
       (enable-theme 'pastels-on-dark)
     (disable-theme 'pastels-on-dark))))
4
  • It is interesting and goes into the right direction. Still I have some mysterious problem here (likely interaction with some module(s) I use - mahaps projectile, ido, or flex-ido): whenever I open tramp file, emacs starts to flicker, and either keeps doing that, or leaves empty window). Once I Ctrl-G this state, and later C-x b to remote buffer, it has proper theme. Still, flickering happens to reappear also in other cases (like C-x 1 which removed remote window...)
    – Mekk
    Mar 2, 2015 at 7:48
  • (my first instinct is to replace not window-minibuffer-p with some "positive" requirement, checking that it is true editor buffer; also, mayhaps some variable checking whether thing is in progress just now). WIll give it a try. (thank you very much for pointing buffer-list-update-hook, this is crucial thing here...)
    – Mekk
    Mar 2, 2015 at 7:49
  • It turned out I had to work a bit more until I get more-or-less satisfactory effect, but this suggestion put me on the proper track, so thank you and I accept your answer as the solution.
    – Mekk
    Mar 2, 2015 at 22:56
  • This buffer-list-update-hook fired all the time for me. default-directory also didn't play well with tab-bar.
    – HappyFace
    Mar 24, 2021 at 10:19
2

I tried to develop Michael's suggestion, the result turned out to be a few pages of code, so I versioned it: https://bitbucket.org/Mekk/emacs_libs-theme-switcher/

Any suggestions how to improve this code are welcome.

4
  • 3
    These days I've pushed tramp-theme to GNU ELPA. It uses another approach, using face-remapping-alist. You might try it if still interested in. Feb 19, 2016 at 8:55
  • This repo is down.
    – HappyFace
    Mar 24, 2021 at 10:33
  • @MichaelAlbinus How do I set different themes in tramp-theme-face-remapping-alist? It seems to ask for details such as the background color, while I just want to use a predefined theme.
    – HappyFace
    Mar 24, 2021 at 10:34
  • Hmm, this looks rather like a feature request (but I haven't thought about in detail). Pls contact us via the [email protected] ML; I fear, SX is not well suited for this kind of analysis. Mar 24, 2021 at 12:17

Your Answer

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

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