How to discover which faces are used in helm buffers? - emacs

I wish to change the appearance of something in a *helm ag* buffer. None of my usual tricks for discovering which face is being used at some point (my favourite being M-x customize-face with the point in the region of interest) work, as there is no (obvious) way of getting control of a cursor in helm buffers.
My questions are:
Teach me to fish, feed me for life: How can I discover the faces used in a buffer in which I cannot place the cursor?
Give me a fish, feed me for a day: Which face is used in the *helm ag* buffer to highlight the pattern match on the currently selected line?
Update
In the case of *helm-ag* buffers created by the helm-ag command, the relevant face is helm-match. However, in *helm ag* buffers (no dash!) created by the helm-do-grep-ag command, the helm-match face seems to have no effect, as described in the further information below.
Further information
Here is a picture of an emacs session in which no custom themes have been enabled.
In the lower left there is a *helm ag* buffer searching for defun. The third line in the buffer is selected. The match (defun) is highlighted in all other lines, but not on the selected one.
On the right are some face customization buffers for likely candidates. helm-match has been set to have a red foreground, but this is not reflected in the *helm-ag* buffer. This seems to suggest that helm-match is not the fish I'm looking for.

A similar approach to #elethan's #3:
Call list-faces-display, which will show you a list of all faces
in alphabetical order.
Search for "helm".

First here is your "fish": I think the face you are referring to is helm-match.
Here are few different strategies that I would personally try if I needed to find a given face and can't place point on the text with that face:
Use M-x describe-face, guess at what the first part of the name is likely to be (in this case helm), and scan through the likely candidates that start with that.
Go to the code where that face is likely defined (in this case helm-ag.el which you can find with M-x describe-function RET helm-ag), and search for face in that file to find a likely match.
Do M-x customize-face and enter and 'all faces', look for helm-* faces and try to find a name and face (since you can see a sample of the face in this buffer) that matches the one you are looking for.
Probably none of these methods is as direct as you are hoping for, and there may be a quicker solution, but this is what I would do (and have done). In this case I found the face with method #2.
Update:
Here is a screenshot from my setup:
Notice that for me the relevant face is helm-match which inherits from match in replace.el. Also, note that the difference between the way the match appears in the highlighted/selected line compared to the other lines is not due to a different face, but caused by how the background color of the line highlighting affects the color, as can be seen when I highlight the sample text here:
Update 2:
It turns out OP was using helm-ag-do-grep which is defined in a different file - helm-grep.el. Here is the face-setting portion of that code:
;;; Faces
;;
;;
(defgroup helm-grep-faces nil
"Customize the appearance of helm-grep."
:prefix "helm-"
:group 'helm-grep
:group 'helm-faces)
(defface helm-grep-match
'((((background light)) :foreground "#b00000")
(((background dark)) :foreground "gold1"))
"Face used to highlight grep matches."
:group 'helm-grep-faces)
(defface helm-grep-file
'((t (:foreground "BlueViolet"
:underline t)))
"Face used to highlight grep results filenames."
:group 'helm-grep-faces)
(defface helm-grep-lineno
'((t (:foreground "Darkorange1")))
"Face used to highlight grep number lines."
:group 'helm-grep-faces)
(defface helm-grep-finish
'((t (:foreground "Green")))
"Face used in mode line when grep is finish."
:group 'helm-grep-faces)
(defface helm-grep-cmd-line
'((t (:inherit diff-added)))
"Face used to highlight grep command line when no results."
:group 'helm-grep-faces)
I think helm-grep-match is what you are looking for. If not, the face in question is likely to be in the above code snippet, and all of those faces should be customizable using customize-face. This code also tracked down using method #2 described above.

ag itself uses colours to highlight matches. Helm uses these colours and ignores helm-grep-match unless helm-grep-ag-command contains the --nocolors option.
There are therefore two approaches:
Set the colours you want with the --color-match option to ag in helm-grep-ag-command.
Disable ag's match highlighting with the --nocolor opiton in helm-grep-ag-command and set Emacs' helm-match or helm-grep-match (not entirely sure which one is the right one here) to specify the match colours. As this second option uses elisp to deal with the colouring, it's likely to be slower than the first.
In both cases, match highlighting will be overriden by helm-selection, so the only way to get any highlighting of the match on the selected line is to have helm-selection not specify either the background or the foreground, thereby leaving the opportuinity for the match highlighting to be seen.
Reference: https://github.com/emacs-helm/helm/issues/1660#

Related

How to change the cursor's color to red when reaching row 90 in Emacs Lisp?

I have opened a buffer with some text on 100 lines.
I would like to change the color of my cursor to red when I reach the row 90?
How would such Elisp function I could put in my init file look like?
Let's say the hook should work for all modes, for simplicity.
Disclaimer: I did not know how to do that before answering. I will tell you how I did find the solution using Emacs.
You can change the colour of the cursor by changing the :background attribute of the cursor face (as seen when using describe-face, or by reading the "Cursor Display" section of the Emacs manual - which is built-in and can be read from Emacs)
I am not aware of a "good" hook that could be used to do this, though. An idea could be to use post-command-hook, but it might be slow.
A (possibly, and probably bad, not thoroughly tested) solution:
(defun my/switch-cursor-color ()
(if (< (line-number-at-pos) 90)
(set-face-attribute 'cursor nil :background "#abcd12") ;; hex-code for your colour
(set-face-attribute 'cursor nil :background "#1234ef")))
(add-hook 'post-command-hook 'my/switch-cursor-color)
Of course, to be safe, you should probably do other checks (what happens in pdf-view-mode/doc-view-mode, etc), but this "should work".
How to get all this information:
Inside Emacs:
C-h i opens the Info directory
Navigate (or use m) to the Emacs one
Press i and search for cursor, or search in the Index directly, or search with s the "cursor" regexp ... until you find the "Cursor Display" section. If you forgot how to do that, as usual in this kind of mode, try pressing h, or ?, or C-h m (they don't necessarily do the same thing, but are all helping you)
In this node, you find that
To customize its color, change the ‘:background’ attribute
of the face named ‘cursor’ (see Face Customization).
Click on the "Face Customization" link to view how to do it via the "Customization Interface". To do it programmatically (i.e. as I did above, using the set-face-attribute function), repeat the steps above to view how to do it.
You can also use the function set-face-background, a simple wrapper around set-face-attribute. To discover this function, you can (and should) also use Emacs: a proper completion/selection system, or the function apropos-command, bound to C-h a, with e.g. the search "face background", and the aforementioned function is then immediately found.
Aaaand if you forgot how to look for help, then use C-h C-h. This command is shown in the tutorial, itself accessible from the menus, or from the initial buffer when starting Emacs (by default), or ... etc.

highlight "" in strings for emacs

I am a bit of an emacs noob, but not quite sure how to even find this. I use solarized light color theme, which is a low contrast theme with intelligent accents to keep things readable. One thing they do in the vim version is highlight the string delimiters (meaning double quote and single quote in some languages) differently then the rest of the string to make them stand out more.
The emacs port of the theme does not have this, however I have seen some pretty crazy stuff happen with the font locking mechanism (like this http://www.emacswiki.org/emacs/HexColour), so I figured it was possible.
So is it possible to highlight string delimeters? if so, where should I look for more info on how to do it?
EDIT:
trying to get Jon O.'s answer working. First I tried
(defface my-string-delimiter-face
'((t (:foreground "red" :weight bold)))
"My custom face for string delimiters")
(add-hook 'after-change-major-mode-hook
(lambda ()
(font-lock-add-keywords nil '(("\\s\"\\|\\s|" 0 solarized-string-delimiter-face t)))))
in an attempt to add it to every mode (didn't work)
So then I tried replacing the hook expression with
(font-lock-add-keywords 'emacs-lisp '(("\\s\"\\|\\s|" 0 solarized-string-delimiter-face t)))
In an attempt to just get things working, same thing (didn't work)
You can use font-lock-add-keywords to highlight string delimiters by doing something like the following:
(font-lock-add-keywords 'foo-mode '(("\\s\"\\|\\s|" 0 'my-string-delimiter-face t)))
The regexp "\\s\"\\|\\s|" matches a single character, which must have syntax class "string quote" (the \\s\" part) or "generic string delimiter" (the \\s| part). \s matches various different character syntax-classes, which you can read about in the Elisp manual under (elisp)Syntax Tables and (elisp)Regexp Backslash.
It's a little easier to read if you see it without string escaping: \s"\|\s|
The 0 means to apply the face to the entire matched string, and the t at the end makes this face override any faces that are already present on the string (since many modes will highlight the entire string including the delimiters with font-lock-string-face or similar)
'foo-mode could be the quoted name of any mode (e.g. 'emacs-lisp-mode, 'php-mode), or nil to make this change buffer-local in the current buffer only. (In that case you should probably put this in the mode-hooks of the modes you want to apply it to)
my-string-delimiter-face could be any existing face, or you could define one by doing something like the following:
(defface my-string-delimiter-face
'((t (:foreground "red" :weight bold)))
"My custom face for string delimiters")

Is there a way to prevent font-locking from changing the font family (and change color only)?

Admittedly, this is something of a first world problem, but I'm sort of picky about the appearance of the display, and I find it really annoying when some mode sets a new font size, family, bold/italic, etc. What I'd like to do is put (set-frame-font "Menlo-10") near the top of my .emacs, and then force emacs to never change any aspect from that default font except for color.
I can sort of get the effect I want by doing something like this:
(mapc (lambda (face)
(set-face-attribute face nil
:family "Menlo"
;; something like (cdr (assoc 'font (frame-parameters)) would be better
;; for the :family, but it didn't immediately work
:width 'normal
:height 1.0
:weight 'normal
:underline nil
:slant 'normal))
(remove 'default (face-list)))
but that only works after I've loaded a new buffer that has created font-lock faces to be changed, and it's a dreadful hack regardless. I suspect there just isn't really in facility in font-locking for ignoring some parts of what a mode requests, but I thought I'd ask.
Also, AUCTeX is by far the worst offender here, so if there's alternately just an AUCTeX setting somewhere to prevent it from requesting changes in family, size, etc. in the first place, that would at least make the problem less annoying.
I'm currently using a recent Emacs 24 pulled from HEAD.
Running customize-face with your cursor on the face you are interested in will allow you to see how that face is defined (and change it). Doing this on the section title gives me font-latex-sectioning-1-face. This inherits from font-latex-sectioning-2-face etc. down to font-latex-sectioning-5-face which in turn inherits form variable-pitch (which is what changes the font family). The documentation also mentions that it's best to change the base face font-latex-sectioning-5-face, or the variable font-latex-fontify-sectioning. You can set this last to 'color which will do what you want (I think). Alternately, you can customize font-latex-sectioning-5-face to not inherit from variable-pitch, or change variable-pitch to not be variable pitch.

Colorize snippets of text in emacs

Suppose I have a few words I would like to highlight, so I want to change the color of those few words only to, say, green.
Is there an easy way to do this in emacs?
Thank you.
This is what I've done, using font-lock-add-keywords. I wanted to highlight the words TODO:, HACK:, and FIXME: in my code.
(defface todo-face
'((t ()))
"Face for highlighting comments like TODO: and HACK:")
(set-face-background 'todo-face cyan-name)
;; Add keywords we want highlighted
(defun add-todo-to-current-mode ()
(font-lock-add-keywords nil
'(("\\(TODO\\|HACK\\|FIXME\\):" 1 'todo-face prepend))
t))
Use library HighLight. You can use overlays or text properties. You can save the highlighting permanently or let it be temporary. You can highlight in many ways (regexp, mouse-drag,...). Lots of possibilities.
The highlight package has hlt-highlight-regexp-region and hlt-highlight-regexp-to-end, which do exactly what you want.
http://www.emacswiki.org/cgi-bin/wiki/highlight.el
Use the function font-lock-add-keywords to define a new matcher for the string in question, binding that matcher to some face you've defined that will display as green. For example:
(font-lock-add-keywords nil
'("\\<foo\\>" 0 my-green-face))
Note that you can specify a particular mode where I wrote nil above, and the matching forms can take on any of six different styles. See the documentation for the variable font-lock-keywords for the rules and a few examples.
If you want them highlighted only temporarily, I find M-x highlight-regexp command very helpful, it is especially nice for looking through log files of sorts. For example you made yourself a logging class that outputs some tracing info like MyClass::function() > when function is run and MyClass::function() < when it exits (can be especially useful sometimes when debugging multithreading issues) then you just ask emacs to highlight some of them green and other red and then you can see how did the execution go.
I use what Dimitri suggested. In particular, I have the following two lines in my .emacs
(global-hi-lock-mode t)
(global-set-key (kbd "C-M-h") 'highlight-regexp)
Every-time I need to highlight a certain word (or regex) in a buffer, I hit "C-M-h", which then prompts me for the word (or regex) I want to be displayed differently and then for a face to display it in.

Emacs - recolor matching lines in ERC

Is there a way to get ERC to highlight all lines that come in that match a certain regexp? For context, I'm using ERC to connect to a bitlbee server and wish that when I issue a 'blist' command, my friends who are online are highlighted in green and those away are highlighted in red.
With erc come several modules. Customise erc-modules so it contains the match module. Then customise erc-keywords, which can contain regexps and cons cells where the regexp is in the car and the face in the cdr.
Don't know about the regexp to distinguish online and away. Is the output from blist different for both?
Edit:
I can't figure out, how to insert custom faces (I mean not existing symbols like the default face) in the customize buffer. So here it is with setting the variable directly:
(setq erc-keywords '(("online-regexp" (:foreground "green"))
("away-regexp" (:foreground "red"))))
I never used ERC, but highlight regex search in emacs can be actived by M-x highlight-regexp