I am trying to build a minor mode to org-mode that highlights inline [todo: <text here> ] blocks as red and bold (org-todo face). But when I run M-: and
(progn
(font-lock-add-keywords nil '("\\[todo:[^\]]*\\]" . 'org-todo) 'append-keywords)
(font-lock-flush))
The todo elements get the font-lock-keyword-face which kind of makes sense in retrospect but by that logic wouldn't one expect there to be a font-lock-add- function for each face as well?
Secondarily I have two more questions. a) I noticed this method breaks down when todo items extend to multiple lines. Is there a simple way of mitigating that? b) I can't seem to find a way to undo the effect of font-lock-add-keywords when the minor mode is disabled.
Related
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.
I am trying to override the default syntax highlighting in org-mode and org-agenda-mode buffers.
To clarify my intention: my current reason for doing this is to highlight headings (or parts of headings) based on their tags. The built-in variable org-tag-faces only allows customisation of the tag itself, not the heading containing the tag.
With reference to the following related questions:
Emacs font lock mode: provide a custom color instead of a face
https://emacs.stackexchange.com/questions/8211/color-code-a-new-generic-character-combination
https://emacs.stackexchange.com/questions/8222/how-to-make-the-custom-font-lock-keywords-not-override-the-default-major-mode-fo#comment12615_8222
In thread 2 the accepted answer is to use font-lock for this purpose.
In thread 3 I am trying to achieve the exact opposite of the poster. The last comment by Jordon Biondo says:
take out the t from your keywords, what that t specifies is that font-lock should override already colored things.
Since I want to override already coloured things I am adding in the t but as far as I can tell the org-mode highlighting is still overriding my custom face.
In org-mode buffers this manifests as the main body of the heading text being changed but any other items such as todo-states, dates, tags etc. retaining there existing faces.
In org-agenda-mode buffers it completely fails to modify any aspect of the matched lines.
By way of a simple example here is some code I'm trying to use to set any lines containing :TT: to red in org-mode buffers:
(add-hook 'org-mode-hook
(lambda ()
(font-lock-add-keywords
'org-mode
'(
("^.*:TT:.*$" 0 '(:foreground "#FF0000") t)
))))
This mostly works for me:
(add-hook 'org-mode-hook
(lambda ()
(font-lock-add-keywords
'org-mode
'(("^.*:TT:.*$" . font-lock-warning-face)))))
The headline is red, although the tag itself is not.
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.
Background:
I'm creating a minor mode that gives the user "hints" about whether the buffer they're visiting uses tabs or spaces for indentation (simply by examining the first character of each line in the buffer). Some features I plan to add include an informational display in the mode-line and a few functions to switch between using tabs or spaces, tab-width, etc.
I'm not really concerned about the usefulness of this minor mode. In fact, I would be surprised if there's not already something out there that does this same thing. Mostly this is an exercise in writing minor modes.
Question:
What would be a clean, non-obtrusive way to insert/remove text from the mode-line when enabling/disabling my minor mode? I don't want the user to have to modify their mode-line-format, I just want non-destructively insert and remove text. Right now I'm using a function that looks something like:
(defun update-indent-hints-mode-line (what-this-buffer-loves)
(let ((indent-hints-mode-line-text (concat " " "[" what-this-buffer-loves "-loving" "]"))
(my-mode-line-buffer-identification
(remq " [Tab-loving]" (remq " [Space-loving]" mode-line-buffer-identification))))
(setq mode-line-buffer-identification
(add-to-list 'my-mode-line-buffer-identification
indent-hints-mode-line-text
t))
(force-mode-line-update)))
It's working okay but searching for and removing " [Tab-loving]" and " [Space-loving]" seems pretty hackish and ugly... Is there a cleaner way to do it?
Bonus Points:
Any comments on the humble beginnings of my equally humble minor-mode:
https://github.com/mgalgs/indent-hints-mode/blob/master/indent-hints.el
I'm obviously an elisp n00b, but I'm here to learn.
Check out the variable minor-mode-alist, which associates variables with strings in the mode-line. If you change your code to either set the variable tab-loving to t or space-loving to t (and set the other variable to the nil), you can get what you want with:
(setq minor-mode-alist (cons '(space-loving " [Space-loving]")
(cons '(tab-loving " [Tab-loving]")
minor-mode-alist)))
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.