emacs: how to change the foreground color of highlights (e.g., links the point is over)?
-
13-04-2021 - |
Frage
I have a color theme file (similar to gruber-darker.el) and I would like to adjust a specific color. This color can be described as follows: Change to Org Agenda (the org-mode agenda buffer) via M-x org-agenda a
. Then move the cursor over a Scheduled TODO item (this is a link to the corresponding entry in your agenda file). When the cursor is over such an item, you see the item in highlighted form with a background color and a foreground color. The background color is fine, however, the foreground color is white. I would like to adjust this foreground color to something like :foreground nil
so that it is not white anymore; instead, the natural color of the item the cursor is on is inherited.
I normally use M-x describe-face RET
to figure out what the element is that I have to change. However, this does not work here since I only get the colors/element names of the item the cursors is on.
Update 1:
I just realized that I get the same behavior for all links in emacs (not only in org-mode). I found out that (highlight ((t (:background "#453D41"))))
gives me a brown background bar as highlighted region. But once the cursor is over the highlighted region, I get a white foreground color instead of the original color. Even (highlight ((t (:background "#453D41" :foreground nil))))
does not give me the original color of the item the cursor is on. How can I achieve that?
Update 2:
I figured out that (highlight ((t (:foreground "#000000" :background "#453D41"))))
indeed gives a black foreground color instead of white. But I don't know how to get the "inherited" foreground color of the item the cursor is on.
Lösung
What you have discovered is that it cannot be done. For one thing, the mouse-face
text property is independent of the face
property. For another, face attributes are defined statically; they cannot be inherited dynamically.
Consider filing an Emacs enhancement request for such a capability (it is not limited to highlight
or to mouse-face
etc.), using M-x report-emacs-bug
. (Yes, that command is for enhancement requests also.)
Andere Tipps
Try using describe-char
instead of describe-face
. That should give you more information about the various faces in use.
Interactively the function uses the character at point but you can also pass it a position, so if you find that placing the cursor on the character in question is a problem (e.g. it changes the highlighting to something else), you can do something like this:
- Move point to a non-conflicting position nearby (let's say 3 characters prior to the character you're interested in).
- M-:
(describe-char (+ 3 (point)))
RET
By using M-x customize-face
, I could not define a background color only for the highlight face without making the foreground color conflict with my theme font-lock.
However, I managed to define an inherited foreground color for the highlight face by defining the highlight face custom before loading my emacs theme.
I use dracula-theme
and below you will find an example of the very first lines of my init.el file, that solve this precise need.
(custom-set-faces
'(highlight ((t (:background "#222222" :foreground nil))))
;; Setting personal theme
(unless (package-installed-p 'dracula-theme)
(package-install 'dracula-theme))
(load-theme 'dracula t)
I restarted the emacs instance (in my case the daemon) to apply the changes, and it finally worked.