emacs minor mode for temporarily modifying the default face - emacs

For example, how can I make a minor mode that changes the default foreground color to gray, and upon exiting the minor mode, the foreground color is back to black? This mode might be useful when you don't like certain text to be seen over your shoulders.
For now, I run the following code to turn text color to gray:
(set-face-attribute 'default (selected-frame) :foreground "darkgrey")
And the following code to get back.
(set-face-attribute 'default (selected-frame) :foreground "SystemWindowText")
But this affects all buffers.

You can use buffer-face-mode based on overlays, it also lets to change the background of text in your buffer.
(make-face 'hard-to-read-font)
(set-face-attribute 'hard-to-read-font nil :background "darkgrey" :foreground "grey")
(define-minor-mode hard-to-read-mode
"This mode might be useful when you don't like certain text to be seen over your shoulders."
:init-value nil :lighter " hard-to-read" :keymap nil
(if hard-to-read-mode
(progn
(font-lock-mode nil)
(buffer-face-mode t)
(buffer-face-set 'hard-to-read-font))
(progn
(font-lock-mode t)
(buffer-face-mode nil))))
;; change this to mode you need
;; or turn it on manually
;; (add-hook 'text-mode-hook
;; (lambda ()
;; (hard-to-read-mode t)))
update: You know, this isn't secure. It works against human eyes, but anyone can use camera and then just increase contrast to read the text.

Related

Current line highlight (hl-line) does not respect existing highlights

I am trying to configure hl-line or hl-line+ in emacs to respect the existing highlights/text background colors in the buffer.
I have configured hl-line in ~/.emacs as follows:
(require 'hl-line)
(global-hl-line-mode 1)
Then I highlight the symbol at point using highlight-symbol-at-point (M-s h .). This highlights the symbol under the cursor all over the buffer with a yellow background.
However, when I move the cursor over a line containing that symbol, the hl-line overlay hides the yellow background. My expectation would be for the line to be highlighted, but for the yellow background to be respected.
After doing some digging, I also tried with the hl-line+ package as it has an overlay priority option that sounds promising. I downloaded the hl-line+.el file and setup my ~/.emacs as follows:
(add-to-list 'load-path (expand-file-name "~/.emacs.d/packages/hl-line+/"))
(require 'hl-line+)
(global-hl-line-mode 1)
(setq-default hl-line-overlay-priority -100)
However, this still has the same incorrect behivour to hide the existing yellow background.
Anybody knows how to configure either of these packages to respect the existing background colors?
highlight-symbol uses font-lock, which adds text properties (face or font-lock-face) to chars in the buffer.
Overlay properties (including face and `font-lock-face) are not applied to chars in the buffer. They're applied to buffer positions. They "overlay" the buffer contents; they're not part of the buffer contents.
Overlay properties always take priority over text properties. This means overlay highlighting always overrides text-property highlighting (e.g. by font-lock).
Overlay priorities only specify the relative priority among overlays. Again, any overlay highlighting overrides any text-property highlighting.
So I think the answer is that you can't do what you request. (Someone else will correct me, if there's a way around this.)
Reading the hi-lock.el documentation, this explanation stood out:
"In buffers where Font Lock mode is enabled, patterns are highlighted
using font lock. In buffers where Font Lock mode is disabled,
patterns are applied using overlays"
So as it turns out, the hi-lock.el package is able to highlight using overlays, but only when font lock mode is disabled. I tested this by disabling font lock with M-x font-lock-mode, and I can confirm the highlighting is now properly displayed when also highlighting the current line with hl-line. The problem is that all language syntax highlighting is now gone, so this option is no good.
If anybody knows how to configure hi-lock.el to use overlays with font-lock-mode enabled, then that would be the best answer.
In the meantime, there is an alternative MELPA package that supports overlay highlighting, and it woks like a charm with hl-line: symbol-overlay.el.
Having said that, I am not too fond of default key bindings and highlighting colors. For reference I leave here my configuration:
(require 'symbol-overlay)
(global-set-key (kbd "<f2>") 'symbol-overlay-jump-next)
(global-set-key (kbd "S-<f2>") 'symbol-overlay-jump-prev)
(global-set-key (kbd "C-<f2>") 'symbol-overlay-put)
(global-set-key (kbd "M-<f2>") 'symbol-overlay-query-replace)
(global-set-key (kbd "<f7>") 'symbol-overlay-mode)
(global-set-key (kbd "<f8>") 'symbol-overlay-remove-all)
(set-face-attribute 'symbol-overlay-default-face nil :background "coral1" :foreground "black")
(set-face-attribute 'symbol-overlay-face-1 nil :background "gold2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-2 nil :background "chocolate2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-3 nil :background "PaleGreen2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-4 nil :background "SkyBlue2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-5 nil :background "PaleVioletRed2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-6 nil :background "IndianRed2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-7 nil :background "PaleTurquoise2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-8 nil :background "MediumOrchid2" :foreground "black")

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

How to override override modeline customization from org-faces.el?

org-faces.el contains this code
(org-copy-face 'mode-line 'org-mode-line-clock
"Face used for clock display in mode line.")
;; ...snip...
(provide 'org-faces)
;;; org-faces.el ends here
Which makes the right side of my modeline (the org clock display) have the same face as 'mode-line. Using my .emacs I'd like to change this behavior so that the org clock display uses the same face as 'mode-line-inactive.
I tried adding this to .emacs:
(require 'org-faces) ;;necessary?
(org-copy-face 'mode-line-inactive 'org-mode-line-clock
"Face used for clock display in mode line."
:background "blue")
(provide 'org-faces) ;;necessary?
but the modeline is unchanged after evaluating .emacs. Where am I going wrong? I'm pretty new to Lisp. Thanks for any help.
You should redefine the face, as I do for "org-todo":
(set-face-attribute 'org-todo nil
:weight 'bold :box '(:line-width 1 :color "#D8ABA7")
:foreground "#D8ABA7" :background "#FFE6E4")
You may (or must, maybe, depending on where you place the above lines) leave the requiring of org-faces, but clearly not the provide line.
Alternatively, use a color theme that improves Org's use (such as my Emacs Leuven theme, see https://github.com/fniessen/emacs-leuven-theme), eventually customizing it to suit your taste.
This seems to work:
(eval-after-load "org-faces"
'(set-face-attribute 'org-mode-line-clock nil
:inherit nil))
That is, make the org-mode-line-clock no longer inherit attributes from the mode-line face. It seems like it gets the attributes from mode-line or mode-line-inactive as appropriate, when displayed in the mode line.

Emacs AucTex Latex syntax prevents monospaced font

My emacs (Aquamacs with AucTex) changes font size (in e.g. LaTeX mode) to show the syntax - like this:
Unfortunately this ruins the point of a monospaced font - e.g. my comments do not align. How do I solve this problem?
For the specific example of sections, chapters, etc., add the following to your .emacs:
(setq font-latex-fontify-sectioning 'color)
Edit
Here is the config I usually use to customise the AUCTeX formatting:
;; Only change sectioning colour
(setq font-latex-fontify-sectioning 'color)
;; super-/sub-script on baseline
(setq font-latex-script-display (quote (nil)))
;; Do not change super-/sub-script font
(custom-set-faces
'(font-latex-subscript-face ((t nil)))
'(font-latex-superscript-face ((t nil)))
)
;; Exclude bold/italic from keywords
(setq font-latex-deactivated-keyword-classes
'("italic-command" "bold-command" "italic-declaration" "bold-declaration"))
If you find a solution to this, the beers are on me. The best I've been able to come up with so far is to put the following in my .emacs somewhere and run the function after loading a mode that does this (org-mode does it too).
(defun fix-fonts ()
(interactive)
(mapc
(lambda (face)
(set-face-attribute face nil
;; :family (if (string= system-type "darwin")
;; "Menlo"
;; "Inconsolata")
:width 'normal
:height 1.0
:weight 'normal
:underline nil
:slant 'normal))
(remove 'default (face-list))))
I don't do the family thing anymore, because I didn't have time to figure out a good way to programatically get it right and it doesn't seem to matter, but your mileage might vary. Also, I don't set anything on the "default" font because some of the other values are relative and need that fixed reference point.

emacsclient unable to load color "unspecified-bg"

I'm getting the error Unable to load color "unspecified-bg" [16 times] when using emacsclient -c. I've started up emacs using emacs --daemon. This seems to mean that my custom faces won't load.
When starting emacs as usual, and then using M-x server-start, then this problem doesn't happen at all. How can I get emacsclient -c to load the faces properly?
Here's the relevant code:
(custom-set-faces '(default ((t (:inherit nil :stipple nil :background "black" :foreground "white" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 120 :width normal :foundry "unknown" :family "Inconsolata")))))
I'm not 100% sure this would fix your problem, but you really should be using color-theme for syntax highlighting. Custom is meant for beginning emacs users, so I'd suggest you try out color-theme and see if it works. Here's how I have it set up on my machine:
Download the package from the color-theme homepage.
Put the color-theme folder somewhere like ~/.emacs.d/color-theme/.
Make sure this folder is in your load-path. I took the following code from a Steve Yegge post:
In your .emacs:
(defvar emacs-root "~/.emacs.d/")
(labels
((add-path
(p)
(add-to-list
'load-path
(concat emacs-root p))))
(add-path "lisp")
(add-path "color-theme-6.6.0")
(add-path "cedet-1.0"))
(require 'color-theme)
Then you define your color theme:
;; Color-theme
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
;; Set custom color theme
(defun color-theme-mine ()
"My custom color theme"
(interactive)
(set-cursor-color "#ffffff")
(color-theme-install
'(color-theme-mine
;; Super-light grey on Dark grey
((foreground-color . "#e0e0e0")
(background-color . "#151515")
(background-mode . dark))
(font-lock-comment-face ((t (:foreground "#106010")))) ;; Forest Green
;; More definitions below
;; ...
(color-theme-mine)) ;; end eval-after-load
This will load color-them-mine when you start emacs. You can see all available color themes by typing M-x color-theme <TAB>. To see the full list of faces available, use the command M-x list-faces-display.
Sounds like this might be bug #4776: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=4776#5. If not, consider filing a bug report for this one, using M-x report-emacs-bug.