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

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.

Related

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.

How to change Hash color in rhtml-mode in Emacs

I'm using rhtml-mode in Emacs.
When I write a Hash in a way like :key => "value" then :key is properly colorized.
But with key: "value" style the colorizing doesn't work. Only color of : is changed.
I tried to change rhtml-mode a bit. The mode seems to load ruby-mode internally if the text if is braced in <% %> tag.
Oddly when I write a Hash in ruby-mode both type of writing are properly colorized.
I'm using default ruby-mode in Emacs24.
How can I find the place (by line number) where the color of Hash key is defined?
The short answer: C-h vrhtml-in-erb-keywords. This will open a buffer showing you the regexp for rhtml keywords. There will be a link straight to where it is defined in the elisp file. You can see its definition here.
Add the following to your .emacs file:
(add-hook 'rhtml-mode
(lambda ()
(font-lock-add-keywords nil
'(("\\([0-9a-zA-Z_]*:\\)" 1
font-lock-constant-face t)))))
This will make Emacs apply the colouring determined by font-lock-constant-face to anything that matches the regexp "\\([0-9a-zA-Z_]*:\\)". This might match more than you want, so you may want to fine tune it.
I'm not sure there's a particularly easy way to find out exactly where the colour for a given keyword is found. You can always do M-xdescribe-face with the point over the word you want information on. This will tell you how Emacs thinks it should be coloured - something like font-lock-keyword-face. C-h vfont-lock-keyword will tell you how Emacs decided that, but not in a very helpful way.
The simplest way is probably just to open the source code for the mode you're in and search in that for where it defines keywords. You can open the source code with C-h frhtml-mode, which will open a help buffer with a link to the source.

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