Is it possible to have a specific org-mode file use a custom theme or face?

1.4k Views Asked by At

I would like to be able to specify a theme or set of custom faces that are specific to a file that will be opened in org-mode. Is there any way I can do that? I already have https://github.com/vic/color-theme-buffer-local but that doesn't provide a way to automatically apply it when a specific file opens.

An ideal solution would not involve hardcoding in my init.el, but would involve setting a variable or calling elisp in the loaded file (or a referenced setupfile).

For an example of why this would be useful, some files are very flat, and are more readable with one set of styles, and other files are deeply nested, and benefit from another set of styles.

2

There are 2 best solutions below

4
On BEST ANSWER

The original poster has cited a particular library called color-theme-buffer-local ( https://github.com/vic/color-theme-buffer-local ), which the original poster wants to apply to a file opened in the major-mode known as org-mode -- only if a certain variable is t.

The following example uses the code cited in the instructions for color-theme-buffer-local by calling the line of code: (load-theme-buffer-local 'misterioso (current-buffer)) The variable my-favorite-variable will control when the previous line of code is called when opening an org-mode buffer -- i.e., when non-nil it applies, when nil it does not apply.


EDIT (November 16, 2014):  The following is a revised answer based upon the desire of the original poster to use file-local variables:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html#Specifying-File-Variables

http://www.gnu.org/software/emacs/manual/html_node/elisp/File-Local-Variables.html#File-Local-Variables

The behavior described by the original poster in the comment underneath this answer is due to the fact that the regular org-mode-hook is run before the file-local variables are taken into consideration. Therefore, the variable my-favorite-variable was still nil when the org-mode-hook ran its course (using the initial answer). The following revised answer uses the hack-local-variables-hook, which runs after the file-local variables are taken into consideration.

(defvar my-favorite-variable nil)

(defun my-favorite-function ()
(interactive)
  (when
      (with-current-buffer (current-buffer)
        my-favorite-variable)
    (load-theme-buffer-local 'misterioso (current-buffer))))

(add-hook 'hack-local-variables-hook 'my-favorite-function)
2
On

To my knowledge, color themes are global, for the whole Emacs session running. The same applies for background color which I'd like dark for shell buffers, and light otherwise; not possible ATM.

If you like light backgrounds, you can have a look at my color theme "Leuven" (in Emacs 24.4, in MELPA or on GitHub) and report improvements you'd find useful.