Emacs custom background color by mode - emacs

I use emacs to edit a number of file types, and would like an easy visual queue to tell .c files from .vhd or .py files, for instance. How can I add a custom background color to the major mode for that language?

You can do this via hooks. Among other things you can hook is when a new major mode starts. Put something like this into your .emacs file, and emacs will set your background color to purple every time you go into Python mode.
(add-hook 'python-mode-hook
(lambda ()
(set-background-color "purple")))
Resetting the background color to the default in the case that you switch back to a mode that doesn't have an explicit set-background hook for it is left as an exercise for the reader.
You can also manually set the background color with M-x set-background-color

For posterity, as this thread is 4 years old, it is now possible in Emacs 24.4+ to change faces on a buffer local level. Simply define a face and use (face-remap-add-relative) to swap out whatever face you want with it.
Define a defface:
(defface my-special-face '((t :background "aqua")))
Then add a hook to the mode of your choice:
(add-hook 'python-mode-hook
(lambda ()
(face-remap-add-relative 'default 'my-special-face)))

You cannot set the background color on a buffer-by-buffer basis. See the SU question How can I change the background colour of a single emacs buffer?.
The first answer there shows how you can change the background for a single Emacs frame, which might work for you if you have one frame per file (or per mode).

Related

Emacs 24 client not loading color theme properly

I want to use the color theme solarized-light. It works just fine if I do (load-theme 'solarized-light t), however I am having issues with having it load automatically.
If I simply add (load-theme 'solarized-light t) to my .emacs, the color theme won't load properly. Some of the colors are applied but others aren't, I assume this is because it's not running in a GUI so it assumes the terminal can't support the colors or something.
Logically, I tried putting the call to load the theme in the after-make-frame-functions variable. I thought that if it only tries to load the theme after I'm already in a GUI, then it should work right?
It doesn't. When I create the first frame the same thing happens with the terminal and the colors aren't loaded properly. However, if I make another frame while I already have one open then it works properly.
Is there any way to apply the theme properly when I make the first frame, or even before that if possible?
I'm on Arch Linux running Emacs 24.3.1 as a daemon. In case this behavior is tied to the window manager, I've tried it in both StumpWM and Openbox and it doesn't work in either.
Edit:
As a temporary workaround I have made a command in my .stumpwmrc that runs the shell command emacsclient --eval "(load-theme 'solarized-light t)" after making the frame. This works but it doesn't answer my original question (nor is it likely to help people with other window managers).
add
(package-initialize)
(setq package-enable-at-startup nil)
to the top of your .emacs file. then set the them from M-x customize-groups: themes, select theme in the menu and save to your .emacs file, also disable foreground and background from default faces group. restart emacs or M-x eval-buffers to check.

How to customize a face on Emacs, but only when hl-line is active?

I am using Emacs 24, console (8 colors) with the Solarized theme.
I want to make the comments less intrusive using:
(set-face-foreground 'font-lock-comment-delimiter-face "black")
(set-face-foreground 'font-lock-comment-face "black")
That way the comments would be colored with the same color as the hl-line face we can see on the picture. However, when moving to a line containing comments, I would like them to be inverted (black background from the hl-line, normal foreground instead of black) so I can still read them.
As an alternative, you can also temporarily hide comments. See command hide/show-comments-toggle in library hide-comnt.el. Bind it to a handy key --- pretty simple.
Please see the code in the attached link: Emacs colors. why it is gray on current line? zenburn theme

Multiterm background color is wrong

I added multi-term to my emacs config. It works great, but any text in console and some whitespaces (including these in vim from ssh) have white background and since I use deeper-blue style with dark background it does not look nice. How to fix this?
Here is screen: http://tinypic.com/view.php?pic=rh1q9v&s=6
Colors in term.el does not work well sometimes, depending on the timing it is loaded. Calling this after setting your color theme may solve the problem.
(setq term-default-fg-color (face-foreground 'default))
(setq term-default-bg-color (face-background 'default))

Emacs -fs (fullscreen)

I'm trying to use emacs in fullscreen mode with a decent font. I have an nvidia-based laptop running Ubuntu. When it first loads, the font is huge think 16pt font. I used the menu options to set a decent font (8pt).
Now when I run emacs in fullscreen mode, it adjusts the window sort of for the huge font then loads my 8pt font. Now half of my screen is the minibuffer. How do I correctly set the font so that I can use fullscreen mode.
I have tried specifying displaysize in my x config and X does not start. I hear theres something else you need to do for nvidia drivers... but not sure how that works.
I use the following (from http://www.emacswiki.org/emacs/FullScreen) in my .emacs:
(defun toggle-fullscreen (&optional f)
(interactive)
(let ((current-value (frame-parameter nil 'fullscreen)))
(set-frame-parameter nil 'fullscreen
(if (equal 'fullboth current-value)
(if (boundp 'old-fullscreen) old-fullscreen nil)
(progn (setq old-fullscreen current-value)
'fullboth)))))
(global-set-key [f11] 'toggle-fullscreen)
;; Make new frames fullscreen by default. Note: this hook doesn't do
;; anything to the initial frame if it's in your .emacs, since that file is
;; read _after_ the initial frame is created.
(add-hook 'after-make-frame-functions 'toggle-fullscreen)
Then F11 will toggle fullscreen on/off.
This is tough since I can't replicate the problem on my machine. I just add -fs and it seems to work. If you just want more real estate... do these work for you:
ditch the menu/toolbar
add (menu-bar-mode 0) and (tool-bar-mode 0) to .emacs
start up and maximze
you should just have the regular DE frame but nothing else
toggle from emacs using M-x menu-bar-mode and M-x tool-bar-mode as needed
manually fullscreen emacs
I can just start emacs, right click the window bar and choose fullscreen from xfce
I have F11 set to do this, but it doesn't seem to work from emacs (emacs seems to try and interpret it as a key binding rather than passing the key to the DE)
if this works, perhaps find a binding in your DE that emacs doesn't care about and just maximize normally
combine the two
I start emacs regularly (/usr/bin/emacs)
I disabled both tool-bar-mode and menu-bar-mode disabled
I manually fullscreen with a right mouse click on the window bar
I get THIS
that's the entire screen, not just a shot of the window contents
What do you think of those ideas? Sorry I can't help more with Ubuntu and/or nvidia. No experience and tough if one can't replicate the problem! Maybe if emacs starts regularly for you the above can help achieve close (or fully) what you're hoping for.

Modifying an existing emacs color theme

I like the Emacs color theme clarity. I start the theme with M-x color-theme-clarity. However, I would rather have the background was always black, rather than the mixed black and white as seen in my screenshot. It seems like if there is a line that has never had text on it, it will be white rather than black. How do I fix this?
Thank you and best regards.
That is odd. I also use the clarity color theme and I sometimes notice that if I switch to it from another theme some of the colors are off in certain areas.
Try setting it as the initial color theme in your .emacs file so that it is the first theme used when Emacs is opened:
(require 'color-theme)
(setq color-theme-is-global t)
(color-theme-clarity)
This might help if the issue is caused by a conflict with a previously used theme.
This has been driving me crazy for a while, finally I think I have found the culprit.
It seems that it only happens when you have custom-set-faces settings in your .emacs configuration file. Try comment the settings to see if fixes the problem.
If the the problem is indeed caused by custom-set-faces you can simply set :background "yourcolour" :foreground "yourcolour2" of the custom-set-faces to fix it. "youcolour" and "yourcolour2" should be the colours used by your preferred theme.