check if the theme is available before loading it - emacs

I am using emacs with the prelude configuration.
I changed the theme and it works fine, and I added it in the preload directory like so :
;; preload color theme
(setq prelude-theme 'my-theme)
I installed the theme via prelude-require-packages, but not in the preload folder (not sure if it is available that soon). Is there a way to programmatically check if the theme is available, to replace the previous line with something more safe, like :
;; just to get the idea
(when (is-available 'my-theme)
(setq prelude-theme 'my-theme))
Edit
I tried :
;; preload color theme
(when (featurep 'my-theme)
(setq prelude-theme 'my-theme))
But in this case I get the default theme, not 'my-theme.

The load-theme function uses locate-file to find theme files. This approach is based that code:
(if (locate-file (concat (symbol-name 'my-theme) "-theme.el")
custom-theme-load-path '("" "c"))
(setq prelude-theme 'my-theme))
You can replace the entire (concat ...) construct with the theme filename string, which for this example would be "my-theme-theme.el".

Related

How to regenerate latex fragments in org-mode?

I have latex fragments in org-mode that were generated with my previous emacs theme and now that I switched my theme, the old latex fragments still have my old background color instead of the new one. How can I clear them and regenerate them (linux)?
C-c C-x C-l is bound to org-toggle-latex-fragment. Do it once to get rid of the overlay and do it again to regenerate the overlay. Depending on where you are in the buffer and whether you invoke it with one C-u or two C-u or no C-u, it will do different things (affect the current latex fragment, all fragments in a subtree, or all fragments in the buffer). You should read the doc string of the funcion with
C-h C-f org-toggle-latex-fragment RET
I'm using Doom Emacs and my previews are cached in the following directory:
~/.emacs.d/.local/cache/org-latex/
I need to delete the folder to make sure the previews are regenerated with the new foreground color.
The latex fragments are stored in a folder named "ltximg" in the same folder as where the org file is located. To recreate the fragments, delete that folder, restart emacs and do org-toggle-latex-fragment again.
Below is a solution which will change the cache folder automatically when you call load-theme. It will update the images as well. To avoid unnecessary operations, this is done only in the buffers where org-toggle-latex-fragment has been called.
The LaTeX fragments are computed for the whole buffer once a new theme is loaded, but you can remove '(16) in the last function below and it won't happen that way. Instead, only the current section will be updated (with the latex images in the rest of the buffer simply removed).
Make sure to transfer your latex fragment options to my/org-latex-set-options. That's because keeping these options within org-mode-hook will not work if you use #+STARTUP: latexpreview.
(defun my/org-latex-set-options ()
(setq org-format-latex-options (plist-put org-format-latex-options :scale 1.5)))
(defvar my/org-latex-toggle-fragment-has-been-called nil
"Tracks if org-toggle-latex-fragment has ever been called (updated locally).")
(defadvice org-toggle-latex-fragment (before my/latex-fragments-advice activate)
"Keep Org LaTeX fragments in a directory with background color name."
(if (not my/org-latex-toggle-fragment-has-been-called) (my/org-latex-set-options))
(setq-local my/org-latex-toggle-fragment-has-been-called t)
(my/org-latex-set-directory-name-to-background))
(defadvice load-theme (after my/load-theme-advice-for-latex activate)
"Conditionally update Org LaTeX fragments for current background."
(if my/org-latex-toggle-fragment-has-been-called (my/org-latex-update-fragments-for-background)))
(defadvice disable-theme (after my/disable-theme-advice-for-latex activate)
"Conditionally update Org LaTeX fragments for current background."
(if my/org-latex-toggle-fragment-has-been-called (my/org-latex-update-fragments-for-background)))
(defun my/org-latex-set-directory-name-to-background ()
"Set Org LaTeX directory name to background color name: c_Red_Green_Blue."
(setq org-preview-latex-image-directory
(concat "ltximg/c"
(let ((color (color-values (alist-get 'background-color (frame-parameters)))))
(apply 'concat (mapcar (lambda (x) (concat "_" x)) (mapcar 'int-to-string color))))
"/")))
(defun my/org-latex-update-fragments-for-background ()
"Remove Org LaTeX fragment layout, switch directory for background, turn fragments back on."
;; removes latex overlays in the whole buffer
(org-remove-latex-fragment-image-overlays)
;; background directory switch
(my/org-latex-set-directory-name-to-background)
;; recreate overlay
;; Argument '(16) is same as prefix C-u C-u,
;; means create images in the whole buffer instead of just the current section.
;; For many new images this will take time.
(org-toggle-latex-fragment '(16)))

Can't get Emacs24 to load themes

To start off, my Emacs version is GNU Emacs 24.3.1 (x86_64-pc-linux-gnu, GTK+ Version 3.12.2) of 2014-06-06 on barber, modified by Debian, and I am running Debian Jessie as the sole OS on a 2009 Macbook Pro.
So I've downloaded lots of themes off the net that I think would make working in Emacs much more soothing, and placed them in my ~/.emacs.d/themes/ folder. I've downloaded the emacs-goodies-el packages. I've set the custom load path for these themes to be in that specific folder. When I start, I either get one of two things depending on if I actually try to load the themes with (load-theme tron t), or not. Both errors are of the type Symbol's value as variable is void: <!DOCTYPE.
When I run Emacs in --debut-init, this is what I get:
Debugger entered--Lisp error: (void-variable <!DOCTYPE)
eval-buffer() ; Reading at buffer position 14
load-theme(jazz t)
eval-buffer(#<buffer *load*> nil "/home/finnds/.emacs" nil t) ;
Reading at buffer position 1203
load-with-code-conversion("/home/finnds/.emacs" "/home/finnds/.emacs" t t)
load("~/.emacs" t t)
#[0 "\205\262
When I try to load themes through M-x customize-themes, I get the error: load-theme: Symbol's value as variable is void: <!DOCTYPE, and all the colors go back to being white/light/default.
And here is my .emacs file, after the custom-set-variables and custom-set-faces (meaning this is put all the way at the bottom of the file):
(add-to-list 'custom-theme-load-path "~/.emacs.d/")
(load-theme 'jazz t)
(require 'color-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)))
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
I've tried doing exactly what the wiki tells me to do, and several other themesites, but I still keep getting this message. I tried searching for an answer, but there wasn't a particular one like my exact case, I found. Can anyone help me out here? Thanks in advance!
You are trying to load an HTML file, not an Emacs-Lisp file. It sounds like you saved the file wrong. <!DOCTYPE is what tells you this.
The article written by Bozhidar B. and cited by him is misleading. I recommend the EmacsWiki page about this instead. It fairly compares and contrasts color themes, which are provided by library color-theme.el, and custom themes, which were added to vanilla Emacs 24.
These two kinds of theme are not the same thing, and neither replaces the other, in spite of what you might hear. Each has its advantages (and disadvantages) and use cases.
And yes, you can use both -- it is not true that "you shouldn't be doing" this. Read the wiki page, get to know both of them, and then make up your own mind about what works for you.
I say this with no horse in the race. My code (Icicles and Do Re Mi) that lets you cycle themes etc. supports both kinds of themes equally: color themes and custom themes.
You're mixing the old color theme handling (based on the color-theme package) and the Emacs 24.x built-in support for themes, which you shouldn't be doing. I'd suggest having a look at this article to learn more about color themes in Emacs. Here's a minimal setup example (using the zenburn theme):
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/"))
(package-initialize)
(unless (package-installed-p 'zenburn-theme) (package-install 'zenburn-theme))
(load-theme 'zenburn t)
To load a theme that's locally available:
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'theme-name t)
This assumes that you've placed an Emacs 24 compatible theme named theme-name in your ~/.emacs.d/themes folder.

Emacs for Windows error loading color theme

I'm using emacs 24.3 on Windows 8. I have installed the solarized color theme from the marmalade repository, and am able to set it using M + x load-theme. It also sets for the current session when I use Customize, but doesn't load when I open emacs again.
I can open the customize menu and select the theme, but saving the changes does nothing, and nothing changes between that and my next session. I looked at several questions here about color themes, but most do not apply to Emacs 24, since I don't need to use color-theme to do it.
Below is a snippet from my .emacs file.
(custom-set-variables
'(custom-enabled-themes (quote (solarized-dark)))
'(custom-safe-themes (quote ("fc5fcb6f1f1c1bc01305694c59a1a861b008c534cae8d0e48e4d5e81ad718bc6" default)))
...
When I try to put (load-theme 'solarized-dark t) into my .emacs, I get the error:
error: Unable to find theme file for `solarized-dark'
I've checked the value of custom-theme-load-path after opening emacs and it includes the directory elpa uses to store the solarized theme. As mentioned above, I can load the theme manually, but something about loading it during init is breaking.
Just add
(package-initialize)
To the top of your .emacs file and you are good to go.
Side note: if the theme author has taken care of it, the theme will add itself to the custom-theme-load-path, however this is not a part of the deftheme and is down to individual theme authors implementing this behaviour.
To solve the problem, I made a quick snippet of emacslisp that will find packages with theme in their name, then add them to the custom-theme-load-path at startup.
Just add it near the top of your ~/.emacs or ~/.emacs.d/init.el (ie. before you load-theme
It has dependencies on s.el and dash.el (both available on elpa)
(require 'dash)
(require 's)
(-each
(-map
(lambda (item)
(format "~/.emacs.d/elpa/%s" item))
(-filter
(lambda (item) (s-contains? "theme" item))
(directory-files "~/.emacs.d/elpa/")))
(lambda (item)
(add-to-list 'custom-theme-load-path item)))

Why Can't I use old theme style file under Emacs 24.1?

I can use my style file under 23.1, 23.4, but after I update Emacs to 24.1, I can't use the old style files. For example, one of my style files is color-theme-arjen.el. Here is the link:
https://github.com/credmp/color-theme-arjen/blob/master/color-theme-arjen.el
In my elisp file, I use following code to load the color theme:
(load-file "~/emacs/site-lisp/color-theme/master_color-theme-arjen.el")
(color-theme-arjen)
I don't know why the color theme works under Emacs 23.1 & 23.4 but just doesn't work under Emacs 24.1.
While Emacs is loading the file, Emacs gives following error:
Symbol's function definition is void: plist-to-alist
If I uncomment above code and don't load the style file, the error is dismissed.
Does anyone know why this happens? Or how can I debug it?
Yeah , I found this bug too. It seems that the Emacs 24 dosen't have the ' plist-to-alist ' function. So probably you should write it yourself. Here is mine.
Put this function in your dot-emacs file then it will be ok.
(defun plist-to-alist (the-plist)
(defun get-tuple-from-plist (the-plist)
(when the-plist
(cons (car the-plist) (cadr the-plist))))
(let ((alist '()))
(while the-plist
(add-to-list 'alist (get-tuple-from-plist the-plist))
(setq the-plist (cddr the-plist)))
alist))
Hope it helps : )
The color theme stuff was heavily revamped in 24, there is a color theme package included with emacs (see M-x customize-themes), and as far as I know breakage of older themes is expected.
The color theme package from marmalade reportedly works as well.
You should probably open a bug report for color-theme-arjen.
I have little idea why, but when installing the solarized theme in emacs 24.3.1 on MacOS X, I found that if I put my init lines:
(load-file "~/lisp/color-theme/color-theme.el")
(load-file "~/lisp/emacs-colors-solarized/color-theme-solarized.el")
(color-theme-solarized 'dark)
after I turned off the scroll bars:
(if (featurep 'scroll-bar)
(scroll-bar-mode -1))
it worked fine. The other way around, I get the error above. I've no idea why the color-theme-alist function is affected by the absence of a scroll bar (the plist-to-alist function call seems to be only for XEmacs)
I definitely thanks wenjun.yan. But i would rather want to check if the function exist before defining it :
(unless (fboundp 'plist-to-alist)
(defun plist-to-alist (the-plist)
(defun get-tuple-from-plist (the-plist)
(when the-plist
(cons (car the-plist) (cadr the-plist))))
(let ((alist '()))
(while the-plist
(add-to-list 'alist (get-tuple-from-plist the-plist))
(setq the-plist (cddr the-plist)))
alist)))

Setting Emacs 24 color theme from .emacs

I have the following code in my .emacs:
(if (null window-system)
(progn
(require 'color-theme)
(color-theme-initialize)
(color-theme-simple-1)))
When I open Emacs on the console, I can verify that the progn block runs (by a (message "Got here.")), and I see a flash that suggests that the color theme was loaded, but if it was loaded, it is overridden by something else. If, after loading, I open my .emacs file and submit the block above using C-x C-e, it works. I've tried doing:
(add-hook 'after-init-hook
(lambda ()
(progn
(require 'color-theme)
(color-theme-initialize)
(color-theme-simple-1))))
but that acts the same.
It may be relevant that I'm using Emacs 24, and that this code is not in my .emacs, but in ~/Dropbox/.emacs, which is loaded from my .emacs.
An additional note: I've tried M-x customize-themes, but none of those work acceptably on the console. They either produce a nearly unreadable light theme, or most of the text is invisible.
Emacs 24 has built-in theming, which doesn't use statements like (require 'color-theme). As Drew points out in the comments, there are differences between color themes and custom themes, and the new direction is towards the latter. Try M-x customize-themes to take a look. From .emacs, you can do things like (load-theme 'wombat t).
But...
It may still be going wrong for you. One thing that can mess it up like this is changing the face -- maybe in the custom-set-faces part of your .emacs file. Emacs's interactive customization automatically includes the color information (both background and foreground) of whatever theme you happen to be using at the time you set it, so this can definitely make trouble with color themes. If that is what's causing it, you can just set the particular attribute you care about with something like
(set-face-attribute 'default nil :height 120)
That will change the font size without changing the colors.
Emacs 24 have own theming system.
M-x customize-themes
or
(custom-set-variables
....
'(custom-enabled-themes (quote (selected-theme)))
)