Emacs Lisp syntax highlighting - emacs

I want to write a syntax highlighting extension for Emacs, but I Googling of variations on "emacs syntax highlight tutorial" have all failed. How do I go about learning how to write an Emacs highlighter? What good resources are there for learning how to do such things?

You're looking in the wrong place. Look at "font-lock-mode".

There's a related question, on how to define a major mode with syntax highlighting using 'define-generic-mode. The question focuses on figuring out how to get the syntax highlighting working.

unfortunately you were searching for the wrong terms, "syntax highlighting" is not emacs vocabulary :). You should have searched for something like "write emacs mode".
There was already a question for this: "How to write an emacs mode for a new language" with some good pointers.

If you are interested in writing your own highlighting, another question covered this and may be of value to you. It included this code snippet:
(defun django-highlight-comments ()
(interactive "p")
(highlight-regexp "{%.*?%}" 'hi-orange))
(add-hook 'html-mode-hook 'django-highlight-comments)
Code courtesy of Ashutosh Mehra's answer.

http://a-kat.com/Emacs_Major_Modes.html

Related

How to add \Sexpr{} to Emacs/AUCTeX verbatim environment

I have the very same problem detailed here caused by the '$'-included in the \Sexpr command-breaking the syntax highlighting of Emacs. Unfortunately no definitive solution has been proposed yet.
Now I read a solution for a similar problem here that I am trying to adapt to my situation. The idea is to set \Sexpr as verbatim environment in Emacs' preferences.
I tried
(setq LaTeX-verbatim-environments-local '("Sexpr"))
but with no result.
Using
(add-to-list 'LaTeX-verbatim-macros-with-braces "Sexpr")
and restarting LaTeX-mode worked for me.

Emacs indent-line-function

i try to indent sql file lines in emacs, I think the c-indent-line is pretty good for me, so i write this code to my init file:
(defun my-sql-mode ()
(setq indent-line-function 'c-indent-line)
)
(add-hook 'sql-mode-hook 'my-sql-mode)
But when i use tab to indent the line, it always give me the tips of 'Wrong type argument: stringp, nil'.
Can someone help me?
Indentation in Emacs is generally intelligent, but it's not magic.
c-indent-line is a function designed for use with C and C++ code. It shouldn't be greatly surprising that it might not work in other contexts, and I'm not sure what you were expecting it to do when faced with SQL code?
I'm afraid the answer is simply: Don't do that.
If you tell us what you wanted it to do, however, someone might be able to help.

What does ad-activate do?

In an answer, I noticed:
;; Align with spaces only
(defadvice align-regexp (around align-regexp-with-spaces)
"Never use tabs for alignment."
(let ((indent-tabs-mode nil))
ad-do-it))
(ad-activate 'align-regexp)
This sounds promising, but... what does it do?!
I tried eval-region on the block of code. But for me, all it does is adding the following to the align-regexp docs:
This function is advised.
Around-advice `align-regexp-with-spaces':
Never use tabs for alignment.
I don't seem to be able to actually use align-regexp-with-spaces, if that's what should be the effect... What am I missing?
I used GNU Emacs version 24.0.96.1 (i386-mingw-nt6.1.7601).
While asking this question, I realized that I just didn't get the idea of advising functions.
It became clear to me that:
align-regexp-with-spaces isn't a function nor a variable but only a name (to enable/disable single pieces of advice)
ever since (ad-activate 'align-regexp), align-regexp just does what I 'advised' it to: not to use tabs
So: ad-activate activates the advice, effectively changing the original function's behavior. Great!
I don't get why this is 'better' than defining a function around align-regexp though. But then again I don't know much about Emacs Lisp.
I'm afraid the extra lines of documentation only added to the confusion...

Debugging emacs LaTeX config file without trial-and-error binary search

I have an extensive emacs configuration. Unfortunately, auto-fill-mode is broken within LaTeX-mode for some reason. How can I debug this without binary searching my emacs configuration for the error?
Alternatively, how can I make this function not set an undo point? It's annoying to have to press undo once per word and space in the document.
(local-set-key (kbd "SPC")
(lambda () (interactive) (fill-paragraph)
(insert " ")))
Well, you can try M-x debug-on-entry auto-fill-mode.
But my advice would be to use do the binary search you're trying avoid. There is nothing faster. My init setup is probably at least as extensive as yours, and I sometimes think there is a quicker way to guess what the problem is, and time and again I've been taught the lesson that binary search is the way to go. It just seems slow and silly at first.
Remember the parable of the wise man who convinced a ruler to pay him with one grain rice on the first chessboard square, 2 on the second square, 4 on the third, and so on. It really doesn't get any better than binary search when you have no idea where the problem is, and even often when you think you can guess the general location of the problem. Just do it.

Syntax highlight a vimrc file in Emacs?

So, this might be a heretical question, but I'm looking for an Emacs mode that handles syntax highlighting of .vimrc files. This particular question has proved pretty hard to Google for the obvious reasons, but it seems extremely likely to me that someone would have written such a mode in the 20+ years of open warfare between the two editors. Any ideas?
Googling does turn up wenbinye's vimrc-mode, a very lightweight generic mode. Here's what I have in my .emacs:
(define-generic-mode 'vimrc-generic-mode
'()
'()
'(("^[\t ]*:?\\(!\\|ab\\|map\\|unmap\\)[^\r\n\"]*\"[^\r\n\"]*\\(\"[^\r\n\"]*\"[^\r\n\"]*\\)*$"
(0 font-lock-warning-face))
("\\(^\\|[\t ]\\)\\(\".*\\)$"
(2 font-lock-comment-face))
("\"\\([^\n\r\"\\]\\|\\.\\)*\""
(0 font-lock-string-face)))
'("/vimrc\\'" "\\.vim\\(rc\\)?\\'")
'((lambda ()
(modify-syntax-entry ?\" ".")))
"Generic mode for Vim configuration files.")
There is a great package for this: https://github.com/mcandre/vimrc-mode
Install by M-x package-install vimrc-mode.
It highlights vimrc files with amazing syntax highlighting automatically when they are opened, or you can invoke it manually via (vimrc-mode).