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

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

Related

Change color of buffer name in mode-line

It would be nice to have the buffer/file name more visible inside the mode-line specially when working with many files.
How to change the color of buffer name inside mode-line?
You can also use:
(set-face-attribute 'mode-line-buffer-id nil :foreground "black")
or for carbon emacs:
(set-face-attribute 'modeline-buffer-id nil :foreground "black")
Just customize face mode-line-buffer-id: M-x customize-face.
I use this value, for instance (from my custom-file):
(mode-line-buffer-id ((t (:overline "red" :underline "red"))))
IOW, I have both an overline and an underline, for the buffer name.

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.

Getting Faces to Share Background Color in Emacs

I'm in the process of configuring linum, hlinum, and hl-line for Emacs 24, and something I'm running into some trouble with is getting the hlinum highlight to match/inherit the background color of the line highlight.
In my Elisp file responsible for face customizations, I have the following:
(eval-after-load 'hl-line
'(progn
(set-face-attribute 'hl-line nil
:background "#222222")))
(eval-after-load 'hlinum
'(progn
(set-face-attribute 'linum-highlight-face nil
:inherit 'hl-line)))
However, the hlinum face remains unchanged. Specifying the linum-highlight-face background manually works, but I'd rather have it pick up on the color for hl-line.
How can I get linum-highlight-face to use the same background color as hl-line?
Related to this StackOverflow question, I added
:background 'unspecified
below the :inherit line for hlinum, and it used the same color.

Customizing highlighting faces in Emacs: Only change the background color

Is there any way to define a face in Emacs (e.g. highlight such as hl-line) so that it only changes the background color (and have Emacs use the foreground color as if the word was not highlighted).
More specifically, I tried the following on the tango-dark theme
(custom-set-faces
'(region ((t (:inherit nil :background "RoyalBlue4"))))
'(highlight ((t (:inherit region :background "dark olive green"))))
'(hl-line ((t (:inherit highlight)))))
and, as can be seen below, region highlighting does respect the foreground font (i.e. it only changes the background color):
                     
but the highlighting of the current line does't:
                       
Why? and how can I get the same effect with hl-line?
Update:
This seems to a bug in the tango-dark theme (a builtin theme of Emacs). The code works well with the default theme (which loads with emacs -Q). I posted this on the official bugs mailing list.
I struggled with this some time ago, and it seems to be a bug of the color theme.
I've come up with a workaround, however. This works for me:
(load-theme 'tango-dark t)
(set-face-attribute 'highlight nil :foreground 'unspecified)
I had a bit similar problem:
(add-hook 'after-make-frame-functions
(lambda (frame)
(select-frame frame)
(when (display-graphic-p frame)
(custom-set-faces '(region ((t (:inherit nil :background "RoyalBlue4")))))
)

emacs minor mode for temporarily modifying the default face

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.