Emacs: disable theme background color in terminal - emacs

I'd like to have emacs not to have a background color when I open a frame in the terminal. I'm using a terminal with a translucent background, and characters with a background color are not "see-through". TERM is set to "xterm-256color".
How do I get emacs to use the default background color (no color at all), when the frame is not graphical?
Edit:
I've got it, sort of:
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'my-awesome-theme t)
(defun on-frame-open (frame)
(if (not (display-graphic-p frame))
(set-face-background 'default "unspecified-bg" frame)))
(on-frame-open (selected-frame))
(add-hook 'after-make-frame-functions 'on-frame-open)
I put the above code in my init file, but only suppresses the background when opening an emacsclient in a terminal, and not emacs itself (i.e. only when invoked with emacsclient -t and not when invoked with emacs). Adding an extra (unless window-system (set-face-background 'default "unspecified-bg" (selected-frame))) doesn't work and only confuses graphical frames.
Any ideas on why this might happen?

(defun on-after-init ()
(unless (display-graphic-p (selected-frame))
(set-face-background 'default "unspecified-bg" (selected-frame))))
(add-hook 'window-setup-hook 'on-after-init)
Combined with the code in your edit, it worked nicely for me for both emacsterms and newly started emacsen. As for why window-setup-hook:
http://www.gnu.org/software/emacs/manual/html_node/elisp/Startup-Summary.html
(neither of the earlier hooks seemed to work except for this one.)

I tried the method that was suggested in this answer but I had no luck getting it to work. this snippet works for me though
(defun on-frame-open (&optional frame)
"If the FRAME created in terminal don't load background color."
(unless (display-graphic-p frame)
(set-face-background 'default "unspecified-bg" frame)))
(add-hook 'after-make-frame-functions 'on-frame-open)
Although it has a setback, if the terminal has a different background settings than the theme I use (dark vs. light), default theme faces are being used which may not seem good on the light or dark background. but in my case which both terminal and theme are dark it works fine.

There are already two answers to this question, one using window-setup-hook, which is called on startup, and another using after-make-frame-functions, which is called when a new frame is made, including after invoking emacsclient. To cover all possible cases, I found that I needed to do it this way:
(defun set-background-for-terminal (&optional frame)
(or frame (setq frame (selected-frame)))
"unsets the background color in terminal mode"
(unless (display-graphic-p frame)
(set-face-background 'default "unspecified-bg" frame)))
(add-hook 'after-make-frame-functions 'set-background-for-terminal)
(add-hook 'window-setup-hook 'set-background-for-terminal)
Note that I am only using selected-frame if necessary; it seems that in client mode, the hook is called before the frame is selected, so it is important to use the frame argument in that case.

Related

emacs highlight background changes

hi: Each time I insert some text in emacs , it will highlight the newly added text. I wonder how to change the background color of the highlight, because the highlight background color is very close to the font color , as a result, I can hardly recognize the code that I am writing.
thank you soooo much
For issues with fonts (which Emacs calls faces) inside of Emacs, it is often helpfull to know the function 'list-faces-display'. You can call this with M-x and it will list all faces defined in the current frame. This can be helpfull both identifying which face is problematic, it will also give you its name which can be used to modify the face. For instance to change the foreground colour of the face named "button" you can call something like this:
(set-face-foreground 'button "cyan")
The effect will be immediately visible. Many aspects of faces can be changed, including colour, font familiy and font size.
Obviously, this will not help you if the problematic behaviour stems from the terminal emulator you are using, as it would appear from some of the comments to your question, then the problem is outside of Emacs and cannot be fixed from inside of Emacs. Even so, knowing about 'list-faces-display' is usefull.
I had this exact question and managed to solve it using the following ways. But I also had another thing in mind: a marker to show which lines are modified.
For tracking changes between the saved file and the buffer, we should use the highlight-changes-mode. But before enabling that mode, we need to prepare some stuff as explained beautifully here for the line marks:
;; a part to add the fringe marks to the gutter. To change the shape, read the explanation of it in this code.
(eval-after-load "hilit-chg"
'(progn
(defvar highlight-fringe-mark 'filled-square
"The fringe bitmap name marked at changed line.
Should be selected from `fringe-bitmaps'.")
(defadvice hilit-chg-make-ov (after hilit-chg-add-fringe activate)
(mapc (lambda (ov)
(if (overlay-get ov 'hilit-chg)
(let ((fringe-anchor (make-string 1 ?x)))
(put-text-property 0 1 'display
(list 'left-fringe highlight-fringe-mark)
fringe-anchor)
(overlay-put ov 'before-string fringe-anchor))
))
(overlays-at (ad-get-arg 1))))))
;; make the highlight-changes-mode reset when the file is saved
(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
(add-hook 'after-save-hook
(lambda ()
(when highlight-changes-mode
(save-restriction
(widen)
(highlight-changes-remove-highlight (point-min) (point-max))))))
make sure it is enabled globally except for buffers that start with ' and *
(setq highlight-changes-global-modes t)
(global-highlight-changes-mode)
make the mode to respect the syntax-highlighting
;; find the name of other faced using M-x ~list-faces-display~
(custom-set-faces
'(highlight-changes ((t (:background "dark green" :foreground nil)))))
(set-face-foreground 'highlight-changes nil)
(set-face-background 'highlight-changes "dark green")

in GNU Emacs, how to set background color by mode?

In GNU Emacs, I see that I can set different faces for foreground type in different modes, e.g.:
(custom-set-faces
'(message-header-to ((t (:foreground "LightGoldenrod1" :weight bold)))))
How can I set the background color for the frame by mode? Such that, for e.g., org-mode background would take whatever the color-theme defines it as, but message-mode background would be black?
Something like this, except that the below doesn't work:
(custom-set-faces
'(message-mode-frame ((t (:background "black")))))
Here is a quick example to do it by frame -- i.e. where it will affect every buffer in the frame:
(add-hook 'post-command-hook 'change-my-background-color)
(add-hook 'change-major-mode-hook 'change-my-background-color)
(add-hook 'window-configuration-change-hook 'change-my-background-color)
(defun change-my-background-color ()
(cond
((eq major-mode 'org-mode)
(set-background-color "honeydew"))
((eq major-mode 'text-mode)
(set-background-color "blue"))
(t
(set-background-color "red"))))
And, here is a change the buffer color example:
(defun buffer-background-red ()
(interactive)
(setq buffer-face-mode-face `(:background "red"))
(buffer-face-mode 1))
To do it on a window basis is not presently possible; however, here is a link to changing the modeline color as to the active window.
https://stackoverflow.com/a/20936397/2112489
"Entire frame, i.e. entire background of message-mode"
this phrase make me think author mixed up frame and window in Emacs. Each frame can contains several windows. While *-mode can refer to each buffer, i.e. window.
So if you want to set background color by mode for each buffer with it (but not for frame) then better use mode hooks like here

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)))
)

Changing Emacs cursor color/interaction with color themes

Because of this bug in Cocoa Emacs using the box cursor obscure the character beneath the cursor, and using the bar cursor with solarized has tended to make me not be able to tell where it is in a sea of text. So I want to have the cursor be a red bar. I thought this would work, in my .emacs:
(when window-system
(require 'color-theme-solarized)
(global-set-key (kbd "C-c l") 'color-theme-solarized))
(case window-system
('ns (progn
(defadvice color-theme-solarized (after cursor-more-visible)
"change the cursor color so it stands out more"
(set-cursor-color "red"))
(ad-activate 'color-theme-solarized)
(color-theme-solarized 'dark)
[...]
))
[...])
But the programmatic invocation of color-theme-solarized actually does not change the cursor color. The cursor color does change if I invoke color-theme-solarized interactively (or in the *scratch* buffer with C-xC-e)—so the advice is being taken, sort of.
Adding a (setq default-frame-alist '((cursor-color . "red"))) (as suggested here) doesn't seem to help. Just for kicks I tried changing (color-theme-solarized 'dark) to (call-interactively color-theme-solarized), with no success.
How can I get the cursor color to be automatically set to red at startup?
The default-frame-alist values are used for NEW frames that are created. They don't affect the current frame. You can set initial-frame-alist if you want to specify the initial frame's values in your .emacs file. To change just the cursor color in the current frame, use:
(set-cursor-color "red")

Cant apply color theme to one frame in Emacs?

My .emacs file is here. I want the theme to change when I am in shell-mode. But what happens is that the theme gets applied to all the windows. I set the variable color-theme-is-global to nil, but the problem still persists.
(add-hook 'shell-mode-hook 'color-theme-monokai-terminal)
(set-variable 'color-theme-is-global nil)
These are the corresponding lines in my .emacs file. What should I do to make it work?
I usually start Emacs as a daemon and then open frames as needed. I use different color themes for X frames and terminal frames like so:
(require 'color-theme)
(color-theme-initialize)
(defun apply-color-theme (frame)
"Apply color theme to a frame based on whether its a 'real'
window or a console window."
(select-frame frame)
(if (window-system frame)
(color-theme-tango)
(color-theme-tango-black)))
(setq color-theme-is-global nil)
(add-hook 'after-make-frame-functions 'apply-color-theme)
You can replace the (if window-system ...) part with your check for shell-script-mode and the color-theme-X parts with your favorite themes.
There is one downside to this: if you don't start Emacs as a deamon, the customization will only kick in after you create a second frame, the first one that pops up will have the standard theme.
I think your terminology is off: in emacs-speak frame means what people normally mean by window in a graphical environment. (That is, the thing that has the close, minimize and maximize buttons and a titlebar, etc, is the "frame".) Whereas the things that show up when you do a C-x 3 (split-window) are called windows, and when you do something like M-x shell-mode you get a new buffer, which may or may not be in a new window.
Color themes are always frame-global (as far as I know, and it's certainly what the documentation suggests) the variable color-theme-is-global determines whether a single theme propagates across frames.
I'm thinking that the closest thing to what you want is something like (completely untested, probably broken):
(defun shell-mode-in-new-frame ()
(interactive)
(select-frame (make-frame))
(color-theme-monokai-terminal)
(shell-mode))
Although this does create a new frame, which isn't what you want.