EMACS, Function call highlight - emacs

(EMACS 24.2 ) I need to highliight function call. I found this on internet
(add-hook 'c-mode-hook (lambda ()
(font-lock-add-keywords nil '(
("\\<\\(\\sw+\\) ?(" . 'font-lock-function-name-face))t)))
It works but it highlight also the following open parenthesis.
I am non confident with regular expression, please, How can I modify match string to avoid parenthesis highlighting?

The regular expression is fine, you just need to highlight the first group in the match, not the whole of it. Replace . 'font-lock-function-name-face with 1 'font-lock-function-name-face.
Another thing to change, just a recommendation, is that font-lock-add-keywords accepts the mode name as the first argument. So you don't need to use the hook.
Result:
(font-lock-add-keywords
'c-mode
'(("\\<\\(\\sw+\\) ?(" 1 'font-lock-function-name-face)))

Related

Make Emacs prettify-symbols-mode work on non-whitespace separated words.

The new prettify-symbols-mode in Emacs works beautifully for translating:
lambda something -> λ something
I'd also like to make:
lambda.something -> λsomething
Sadly, prettify-symbols-mode only recognizes spaces as word/symbol separators by default.
Any ideas on how to use '.' as a token separator?
The code that does the actual substitution is prettify-symbols--compose-symbol in prog-mode.el. It excludes matches if the character before or after the word has the character type word or symbol. In many mode, for example emacs-lisp-mode the . character has symbol type.
You could either change the syntax code for . in the major mode, you could tell font-lock to use a different character code when highlighting (see the variable font-lock-defaults for details), or you could do ju-jutsu on the prettify-symbols--compose-symbol mode like modifying it using defadvice or simply replace it with your own.
Thanks to Lindydancer, I ended up doing this:
(add-hook 'python-mode-hook
(lambda ()
(push '("self" . ?◎) prettify-symbols-alist)
(modify-syntax-entry ?. ".")))
which results in a reasonable compromise output of:
◎.method

How do I font lock dollar signs (math mode delimiters) in AUCTeX buffer only outside comments?

I wrote the following code to highlight dollar signs in AUCTeX buffers in different colors, but then I found that it's even highlighting dollar signs in comments, which was unintended, but I am starting to like it. But now just for curiosity, I wonder if that can be avoided.
(defun my-LaTeX-mode-dollars ()
(font-lock-add-keywords
nil
`((,(rx "$") (0 'success t)))
t))
(add-hook 'LaTeX-mode-hook 'my-LaTeX-mode-dollars)
From the documentation of font-lock-keywords:
MATCH-HIGHLIGHT should be of the form:
(SUBEXP FACENAME [OVERRIDE [LAXMATCH]])
OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing
fontification can be overwritten. If keep', only parts not already
fontified are highlighted. Ifprepend' or `append', existing
fontification is merged with the new, in which the new or existing
fontification, respectively, takes precedence.
In other words, if you drop the t after 'success, it will no longer fontify dollar signs in comments and strings.
EDIT:
Apparently, the above solution is not sufficient in this situation, probably because dollar signs have been colored using another face earlier.
One way that might work is to not pass the HOW parameter (currently t) to font-lock-add-keywords. This means that they should be added to the end of the list. However, this might cause other things to stop working.
If we need a bigger hammer, you can write a bit more advanced rule that inspects the current fontification, and decides what to do upon this. For example, the following is used by Emacs to add a warning face to parentheses placed at column 0 in strings:
"^\\s("
(0
(if
(memq
(get-text-property
(match-beginning 0)
'face)
'(font-lock-string-face font-lock-doc-face font-lock-comment-face))
(list 'face font-lock-warning-face 'help-echo "Looks like a toplevel defun: escape the parenthesis"))
prepend)
A third way to do this is to replace the regexp (rx "$") with the name of function that could search for $ and check that it appears in the correct context. One example of such font-lock rules can be found in the standard Emacs package cwarn.

Syntax read error: )

I'm trying to set up an emacs environment like this guy, but I keep getting this darn error:
Syntax read error: )
Here's what my init.el file looks like, it's not long, but I have NO idea what any of it means. I'm brand spankin new to lisp.
(add-to-list 'load-path' "~/.emacs.d/auto-complete-1.3.1")
;Load the default configuration
(require 'auto-complete-config')
;Make sure we can find the dictionaries
(add-to-list 'ac-dictionary-directories' "~/.emacs.d/auto-complete-1.3.1/dict")
;Use dictionaries by default
(setq-default ac-sources (add-to-list 'ac-sources' 'ac-source-dictionary'))
(global-auto-complete-mode t)
;Start auto completion after two characters of a word
(setq ac-auto-start 2)
; case sensitivity is important when finding matches
(setq ac-ignore-case nil)
(add-hook 'js-mode-hook'
(lambda ()
;;Scan the file for nested code blocks
(imenu-add-menubar-index)
;;Activate folding mode
(hs-minor-mode t))
You have unbalanced parentheses in the call to add-hook at the end.
Basically, add an extra ) to the two at the end.
If you type M-x show-paren-mode RET and then position the cursor after a closing parenthesis, it'll highlight the matching opening parenthesis (and vice-versa if you position the cursor on an opening paren).
You have extra quotes at the end of some symbols. In lisp, you quote an expression by putting a single quote (') immediately before it and nowhere else: 'correct, 'incorrect'. String literals are like many other languages, with double quotes both before and after: "a string literal".
Also, the missing paren as pointed out by phils.
In general, you can start emacs with --debug-init to make it do a backtrace on any errors in your init file. In this case it's not very useful because the code isn't even evaluated.

Emacs Auctex custom syntax highlight

I'd like to highlight a new command I created in LaTeX:
\newcommand{\conceito}[3]{
\subsection{#1} (Original: \textit{#2} #3).
}
I use this code in this way:
\conceito{Foo}{Bar}{Bla}
I followed the manual and put this code in my ~/.emacs, but it didn't work:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'((""\\<\\(\\conceito)\\>"" 1 font-lock-warning-face t)))))
What's wrong?
EDIT: Deokhwan Kim originally pointed out that your regexp contains two consecutive double quotes, and that the closing parenthesis ) needs to be escaped with double quotes as well:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\<\\(\\conceito\\)\\>" 1 font-lock-warning-face t)))))
In addition to the points pointed out by Deokhwan Kim, there are also the following two issues:
You need four backslashs instead of two in front of 'conceito': \\\\conceito
The backslash sequence \\< matches the empty string only at the beginning of a word, however, the backslash at the beginning of your new LaTeX command is not considered part of a word, so \\< will not match.
Try this instead:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\(\\\\conceito\\)\\>" 1 font-lock-warning-face t)))
EDIT: Another good observation that Deokhwan Kim made is that in this particular case, you don't really need the parenthesis at all, because you're attempting to match the whole expression anyway. So an alternative to the last line could be:
'(("\\\\conceito\\>" 0 font-lock-warning-face t)))))
The point about the parenthesis is correct, but you could actually extend your regexp to only match when an opening curly brace { follows the word "conceito". But since you don't really want to highlight that brace, using sub-groups defined by parentheses is the way to go:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\(\\\\conceito\\)\\s-*{" 1 font-lock-warning-face t)))
Note that since we're testing for a { that follows directly after "conceito" (unless there's whitespace in between), we don't need the test for \\> any more at all.
In general, try M-x re-builder to craft regular expression interactively: you can edit a new regexp in a small buffer and instantly see what is highlighted in the buffer from which you invoked the re-builder.
GNU AUCTeX has a built-in way of defining custom highlighting to user-defined macros. Have a look at the variable font-latex-user-keyword-classes and the AUCTeX documentation.
Here's a simple example (my configuration):
(setq font-latex-user-keyword-classes
'(("shadow-hidden" (("hide" "{")) shadow command)
("shadow-mycomment" (("mycomment" "{")) shadow command)
("shadow-comment" (("comment" "{")) shadow command)))
This will show the contents of \hide{}, \mycomment{}, and \comment{} macros in the dim shadow face.

In Emacs can you evaluate an Emacs Lisp expression and replace it with the result?

For example if I have the text:
Sum of items is (+ 1 2 3)
I want to move to the end of the line, evaluate the expression and replace it with the result, so that it reads:
Sum of items is 6
With the cursor at the end of the line, C-u C-x C-e will insert the value of the preceding parenthesized expression into the buffer. You could do that, then manually back up and delete the original expression. If that's too much work, here's a command that evaluates the preceding expression and replaces it with its value:
(defun replace-last-sexp ()
(interactive)
(let ((value (eval (preceding-sexp))))
(kill-sexp -1)
(insert (format "%S" value))))
Related to this, you might like Luke Gorrie's "lively.el", which provides live replacement of emacs lisp expressions within a text buffer. It's a neat hack.
I was having a go at a solution for this when I came across one in a Google search result.
(defun fc-eval-and-replace ()
"Replace the preceding sexp with its value."
(interactive)
(backward-kill-sexp)
(prin1 (eval (read (current-kill 0)))
(current-buffer)))
http://emacs.wordpress.com/2007/01/17/eval-and-replace-anywhere/
replace-regex functions can execute lisp to generate the replacements.
In the trivial instance where the sexp in question is on a single line, and is the only thing containing parenthesis, then you could match "(.+)" and replace with "\,(eval (read \&))".
If you are using emacs starter kit by technomancy there is "esk-eval-and-replace" function which evaluates the elisp sexp and replace them. Its bind to C-c e by default.
look to the function eval-print-last-sexp, you can build something using it
My emacs-fu isn't so strong, so I don't know if there's a single command to do this, but you can make yourself a (somewhat fragile) macro for it ... drop these lines in your .emacs:
(fset 'eval-sexp-in-place
[?\M-x ?e ?v ?a ?l ?- ?p ?r ?i ?n tab return ?\M-^ ?\M-^ ?\C-\M-b ?\C-\M-k ?\C-d])
(global-set-key [(control x) (control a)] 'eval-sexp-in-place)
This works fine, but there's one issue with it: you need to be at the end of the sexp (i.e. after the last right paren) to get it to work.
Also, I picked a random unbound key (C-x C-a) -- feel free to change that to something more to your liking.