Colorize snippets of text in emacs - 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.

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.

How to change variable color in Emacs/ESS syntax highlighting?

I am using Emacs 24.3 and ESS 13.05 with the theme tangotango.el. While the theme is restful on the eyes, variable names in R don't appear to be highlighted. In tangotango-theme.el I can find the following line:
`(font-lock-variable-name-face ((t (:foreground "tomato"))))
but this doesn't appear to have any effect. For example, in the screenshot below I would expect the variable orl to be highlighted in some shade of red. Instead it is the standard text colour for this theme.
If I delve into ESS there is a file named ess-font-lock.el which contains a few references to the variable name face, like this one:
(set-face-foreground 'font-lock-variable-name-face "Black"))
So it looks as if font-lock-variable-name-face has competing definitions. I don't understand the interaction between Emacs themes and these ESS definitions. Is ESS overriding the tangotango theme and if so, will changing the above line in ess-font-lock.el restore variable name highlighting? Or should I be looking somewhere else entirely?
Edit: note that Cperl mode does seem to respect the font lock:
You are looking in a wrong place. ess-font-lock defines themes. Some 10 years ago that was useful. Now there are generic themes like your tango-tango and ESS doesn't interfere with them.
The issue is that ESS does not define a font lock keyword that you are looking for. The reason is that <- is an assignment operator, and there is no an explisit variable definition statement in R. ESS only treats function definitions. That is, assignment of a function will be highlighted:
foo <- function(){}
Believe me or not, but you really don't want to highlight all your assignments. You can try it though with:
(defvar ess-R-fl-keyword:assign-vars
(cons "\\(\\(?2:\\s\"\\).+\\2\\|\\sw+\\)\\s-*\\(<-\\)"
'(1 font-lock-variable-name-face)))
(add-to-list 'ess-R-font-lock-keywords '(ess-R-fl-keyword:assign-vars . t) t)
ESS implements a flexible font lock customisation mechanism on top of emacs font-lock system. See ESS>font-lock submenu.
Yes, it sounds like it. If you see the problem only in that mode, and that mode explicitly changes the face, then that sounds like the culprit. You should not need to change the source code, however. Just do something like this (untested):
(add-hook 'ess-mode (lambda () (set-face-foreground "tomato")))
(I assume that's the right mode name; if not, correct it.)
But this is an ugly workaround -- you should not need to do that. Consider filing a bug against the ess-mode.el code. It should not trample on user settings such as faces that way. If it wants to change the appearance by default then it should give users a new face that they can customize, instead of simply screwing with an existing face in a hard-coded way.

Emacs - Toggling between Visual Markers?

I'm trying to figure out if this functionality exists, or if not how difficult it would be to program.
Essentially what I'm looking for is the ability to mark multiple lines in a file with some form of highlighting to point out that they're important lines. From there on it would be great to be able to toggle on/off the marker, and be able to toggle through them - for larger files it would be great to be able to do this for important lines.
The markers don't have to persist, just for that session would be great.
I took a look into the "Overview of Markers" page - but I'm not sure this is exactly what I want, and if it's worth the time to try and implement it if it's not.
Thanks for any/all help!
It looks like bm.el does exactly what you want.
You want quick, perhaps temporary bookmarks that highlight the location (e.g. line): Bookmark+.
Autonamed bookmarks: hit the same key to create/delete.
Temporary bookmarks: any bookmarks can be temporary; quick to toggle temp/permanent.
Highlighting bookmark locations: any bookmarks can be highlighted, in various ways.
FWIW, Bookmark+ does everything bm.el does, and more.
This is somewhat similar to what highlight-regexp does, except arbitrary text instead of a regexp. Based on that, I think something like this should work:
(defun highlight-text ()
"Highlight the current region."
(interactive)
(let ((overlay (make-overlay (region-beginning) (region-end))))
(overlay-put overlay 'face 'hi-yellow)))

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

How to highlight all occurrences of a word in an Emacs buffer?

Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?
It doesn't necessarily have to work exactly like Notepad++ (i.e., via selection); ideally, I would like to set up a key binding that causes all occurrences of the word under cursor to be highlighted.
It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.
Also, it would be useful if there was a solution that made it possible to navigate between highlights (using custom key bindings).
The hi-lock suggestions are good. I think it's easier to use the M-x versions, though:
M-x highlight-regexp RET <REGEXP>
M-x highlight-phrase RET <REGEXP>
highlight-phrase is just a bit of sugar around highlight-regexp that ignores case and translates a space in the regex to match arbitrary whitespace. Handy.
Maybe highlight-symbol.el at http://nschum.de/src/emacs/highlight-symbol/ is what you are looking for:
Type C-s, then type the current word or type C-w. As a bonus, you can now hit C-s again to search for the word.
This is called incremental search.
What I use is idle-highlight
http://www.emacswiki.org/emacs/IdleHighlight
M-x idle-highlight sets an idle timer that highlights all occurences in the buffer of the word under the point.
To enable it for all programming modes, in ~/.emacs.d/init.el:
;; highlight words
(add-hook 'prog-mode-hook (lambda () (idle-highlight-mode t)))
Light-symbol will highlight whatever symbol point is over.
Alternately, you can use occur, which lists all lines matching a regexp. It's useful to quickly see all functions in a class.
Nobody mentioned symbol-overlay mode. It's basically a better rewrite of highlight-symbol-mode. "Better" as in, lacks bugs of original highlight-symbol (such as, temporary highlight getting stuck, or the temporary highlight disappearing for moving inside the highlighted word; or not being able to highlight symbols like *), better integrated, and maintained. See "Advantages" paragraph of its README.
You can install it as usual, with M-xpackage-install (make sure to update package list beforehand with package-list-packages). For reference, at the bottom I attached code I use to enable the mode and disable a few of the more advanced features which you may or may not want.
Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?
Once you enable overlay-symbol, occurrences on the screen will be shown for every word that you put cursor upon after a timeout (timeout by default is 0.5s, can be configured with symbol-overlay-idle-time variable). If a word don't get highlighted, this means there's just one match on the screen (the one you put cursor upon), hence there's no need to highlight it.
It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.
To highlight the word under cursor permanently there's a function symbol-overlay-put. To unhighlight call it once again.
In my config example it's bound to Logo+` key.
(require 'symbol-overlay)
(defun enable-symbol-overlay-mode ()
(unless (or (minibufferp)
(derived-mode-p 'magit-mode)
(derived-mode-p 'xref--xref-buffer-mode))
(symbol-overlay-mode t)))
(define-global-minor-mode global-symbol-overlay-mode ;; name of the new global mode
symbol-overlay-mode ;; name of the minor mode
enable-symbol-overlay-mode)
(global-symbol-overlay-mode) ;; enable it
(global-set-key (kbd "s-`") 'symbol-overlay-put)
(setq symbol-overlay-ignore-functions nil) ;; don't ignore keywords in various languages
(setq symbol-overlay-map (make-sparse-keymap)) ;; disable special cmds on overlays
This may not be as nice as what you were hoping but if you put
(global-hi-lock-mode 1)
in your .emacs file then you can type C-x w h REGEX <RET> <RET> to highlight all occurances of REGEX, and C-x w r REGEX <RET> to unhighlight them again. Again, not as elegant as you'd probably like, but it'll work.
Try http://www.emacswiki.org/emacs/msearch.el
All occurences of the text selected with the cursor are highlighted.
You have to drag over the string which you want to highlight. That enables you to easily change the selection without changing the highlight.
If you want to preserve the highlighting of a string you can freeze it.
You can enslave a buffer to another buffer. Text selected in the master buffer will also be highlighted in the slave buffer. That is useful for comparing buffers.
It is also useful for taking notes in one buffer while you investigate the text in another one. You can have a collection of keywords in the notes buffer. Drag over such a keyword and its occurences in the investigated text will be highlighted.
I am using this stuff for years now. I added the freezing quite recently. So, maybe something is broken. If this is the case leave me a note on http://www.emacswiki.org/emacs/msearch or here.
Check Interactive Highlighting
Should be:
C-x w h word <RET> <RET>
Try iedit. It highlights the word at point and lets you edit all occurrences of it easily. With an additional keystroke (C-'), it hides all the lines without that word in it. Very handy!
Commands in library highlight.el
let you (un)highlight text matching a regexp (in this case a symbol), using overlays or text properties. You can cycle among the occurrences. Highlighting can be temporary or persistent. (more info).
This maybe won't highlight but will search for a word without you needing to type it...
when you've reached the word you wanted to search, C-S, then read the full word with C-W then you can C-S and it will search for it. In my Emacs it also highlights all instances in the document.
This package available in Melpa works, you can customize the highlight style as well.
https://github.com/ignacy/idle-highlight-in-visible-buffers-mode