Emacs lisp highlighting - emacs

I have wrote simple macro to define interactive function and bind it to key at same time
(That what my previous question related)
The only thing, that annoy me, that it looks ugly without highlighting.
It called such way:
(define-and-bind-command foo "C-x £" (message "Hello world"))
I want define-and-bind-command and foo be highlighted.
Well, to get define-and-bind-command highlighted, I can use wrapper around defmacro,
altho is not so pretty, and I have no idea about foo.
I know it is possible, because argument of require is highlighted with const face.
Or, probably, I am inventing wheel, and there is an another lisp mode with more
advanced highlighting?

highlighting the 'define-and-bind-command' can be achieved very easily using 'font-lock-add-keywords', usage would be
(defun my-elisp-mode-keywords()
(font-lock-add-keywords nil
'(
("\\<\\(define-and-bind-command\\)" . 'font-lock-keyword-face)
)
)
)
Of course you could change the 'font-lock-keyword-face' to any face of your liking or create and use your own face. You can find the name for a face already used very easy by moving the point (cursor) over the font-locked part of text and entering 'describe-face' followed by a return.
Edit2: Oh and of course you need to hook that defun to some hook, for elisp mode that would be:
(add-hook 'emacs-lisp-mode-hook 'my-elisp-mode-keywords)
Highlighting the foo part can be done using a regexp.
Unfortunately I can not yet help you with that part because I'm not sure how to match a regexp containing the 'define-and-bind-command' but highlighting only the word after.
Edit: unless you want to highlight both the 'define-and-bind-command' and the foo part in the same face, then it should be very easy. But I guess you want them to have different faces?
Edit3: Corrected my code, stackoverflow ate some braces before...
Edit4: Okay, I have a solution for matching the second part only, I have not tested it extensively but it seems to work. I have allowed for foo to contains any character but a space (and newline I think), I guess one could restrict that event futher to a-zA-z0-9 and "-", so feel free to change that to your liking. The Code responsible for matching only the foo part is
("\\bdefine-and-bind-command\s\\([^\s]*\\)" 1 'font-lock-function-name-face t)
Insert that in the line after the other font-lock keyword in the above function and you're good to go. The regexp matches 'define-and-bind-command' which must begin a word (that's the \b for) followed by a space and then it returns (font-locks) everything followed until a space exists.
Hope this helps!

font-lock-add-keywords can take a symbol, the mode to which to apply the new keywords. So you could do
(font-lock-add-keywords
'emacs-lisp-mode
'(("\\<\\(define-and-bind-command\\)" . 'font-lock-keyword-face)))
The disadvantage compared to using a function and a hook is that this doesn't work for derived modes; that is, if you have a mode which is derived from emacs-lisp-mode, it will not inherit these keywords.

Related

How can I define auto-indent and auto-pairing in .emacs file for emacs 24.3.1?

Just FYI, I am new to the .emacs file.
I would lik to set up my .emacs file to auto-indent and auto-pair a certain way to make writing code a little faster. I have found some info as to how to do these things independently but I'm not sure how to put it all together for the emacs version that I have. Ultimately, I would like to set up these definitions specific to which ever language I am coding in. Just to get me started I will use java as an example.
Obviously auto-pairing for ", (, ' are pretty straigforward. I would just like it to auto insert a closing ", ), ' and place the cursor in the middle.
For {, I would like it auto insert two newlines and the closing } whith the cursor in the middle.
Example
while (true) {
<--- cursor would be here with auto-indent of 2 spaces
}
I would also like this to work for nested curly braces which the appropriate indentation.
Example
while(true) {
if (...) {
}
}
Here is what I have so far in my .emacs file:
(defun java-autoindent ()
(when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
(newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)
Obviously this just inserts a line and auto indents, but I also want the closed } to be included on the line below. I also tried using electric-pair but that didn't work.
My wish list may be a little unrealistic. I'm not even sure that this is possible, but I would be happy with the closest that I could get.
Any help to get me going in the right direction would be greatly appreciated.
Emacs defines modes for each type of language you code in. Some modes are derived from others and there is a mode called prog-mode which most programming modes are derived from.
The mode for a language is where things like indentation are defined because these tend to be language specific. The rules for indentation can be quite complicated, which is why people often use a mode with similar indentation style as the parent and derive a new mode from that.
Have a look at modes and derived mode in the emacs elisp manual.
With respect to adding matching/closing delimiters, have a look at electric-pair-mode (I think it was in emacs 24.4 - I'm running 25 and forget when it was introduced).
With respect to your requirement to enter some code, some newlines and position the cursor in a specific place, you probably want to look at one of emacs' template solutions. yasnippet is a popular choice and it is easy to define new templates in it. There are also many existing packaged yasnippet templates you can download/install. If you don't like yasnippet, google emacs template and have a look there are quitre a few frameworks.

How do I make emacs treat '_' as a normal character, not a separation one? [duplicate]

In python mode, when I forward-word. The cursor jumps from H to d (Hello_World). But in another mode(shell-mode or c-mode), the cursor jumps from H to _.
I want the result which i get in Python mode, even in the other mode. What should I do?
PS: I saw a similar question before, I have searched, but I could't find it.
I think you're looking for this:
(modify-syntax-entry ?_ "w")
Underscores will be treated as part of a word. This command will change the syntax table of the mode you're currently in. AFAIK there's no way to change syntax globally. However, you could try modifying the standard syntax table. Most major modes inherit the standard-syntax-table.
(modify-syntax-entry ?_ "w" standard-syntax-table)
If that doesn't work, I guess you have to add mode-hooks for all modes you're using and modify their syntax tables individually.
Things got a little simpler since Emacs 24.4. There's now a M-x superword-mode that has the desired effect.

Emacs: abbrev-mode not expanding simple abbrev, possible causes?

I would like to assign the string 'target="_blank"' to the abbreviation 'tgt' as I use this a lot and it's annoying to have to type out each time.
The string above probably needs escapes and so on, so as a simpler starting point I have tried to assign 'target' to 'tgt'. Despite switching on abbrev-mode and defining this as a global abbrev, if I type 'tgt' and hit space, nothing happens. If I do M-x list-abbrevs I get this:
(global-abbrev-table)
"target" 1 "tgt"
So it seems to be recorded. I would have expected tgt to be expanded to target when I hit a space after tgt, but that doesn't happen. What am I missing? Have I completely misunderstood the nature of abbrevs? I have looked at the Emacs wiki page but like many Emacs pages it gives a number of complex solutions and annoyingly sparse coverage of the basics.
EDIT: embarassingly enough this looks like it was a combination of the wrong abbrev file and an assignment that was reversed, so that typing 'target' produced 'tgt'. I now have 'tgt' producing 'target="_blank"' as desired. Apologies everybody.
However, I now have a related question. This abbreviation expands when I hit space and typically I do not want a space after the 'target="_blank"' string. Is there a way to automatically remove the space?
You have to define the abbrev the other way around. The way you did it will expand "target" into "tgt".
To do the right thing, first type into a buffer what you want the abbrev to expand into. In your case that would be "target". Then, with the point right after the word, type C-x a g. This will prompt you for an abbrev for which you would type "tgt".
Et voilà: if abbrev-mode is turned on, typing tgt will now expand into "target".
There are other ways to define an abbrev, e.g. via M-x define-global-abbrev, thus it's best to check out the documentation.
The problem with removing the space after the abbrev is that Emacs will insert it after the abbrev has been expanded. So hitting space will basically two things: trigger the expansion of the abbrev and then run the normal self-insert command.
One simple way to avoid this is to type C-x ' or C-x a e to explicitly expand an abbrev rather then turning on abbrev-mode. Except, that's a bit annoying. If we look at the documentation again, however, we find:
Function: define-abbrev table name expansion &optional hook count
[...]
If hook is a non-nil symbol whose no-self-insert property is
non-nil, hook can explicitly control whether to insert the
self-inserting input character that triggered the expansion. If hook
returns non-nil in this case, that inhibits insertion of the
character. By contrast, if hook returns nil, expand-abbrev also
returns nil, as if expansion had not really occurred.
This means that if you put the following lines in, say, your ".emacs" file:
(defun my-after-abbrev-expand ()
(when (looking-back "\"\"\\|''\\|()\\|\\[\\]\\|{}")
(backward-char 1))
t)
(put 'my-after-abbrev-expand 'no-self-insert t)
then you can define an abbrev like so:
(define-abbrev global-abbrev-table "tgt" "target=\"\"" 'my-after-abbrev-expand)
to avoid the insertion of the space character. Also, the my-after-abbrev-expand function will move the point one position to the left if the expansion ends in two double quotes, two single quotes, or a pair of round, square or curly braces.

Modifying emacs forward-word / backward-ward behavior (to be like in vi/vim)

What would be the easiest way to have the same kind of behavior that is in vim for the word back and forth navigation? In vim when you press "w" it moves a cursor forward one word, where word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, eol). In emacs on the other hand it skips until the end of the next word and the word is defined per mode in the syntax table.
For example: having a cursor at the beginning of the line following shows where vim put a cursor when you do forward-word ("w") operation:
opt1.arg = opt2.arg
^ ^^ ^ ^ ^^ ^
In emacs it is like:
opt1.arg = opt2.arg
^ ^ ^ ^ ^
It really depends on a one's preference, but I tend to like the vim style better and I was wondering what is the easiest way to have the same in emacs. I guess I'm not alone who switched from vim to emacs so perhaps someone already has a solution, ideally for the kill-word and backward-kill-word as well :)
I know you can get something similar by combination of M-f, M-b etc., but that is not the point. I also don't want to start a discussion which approach is better - the topis is well discussed in here.
You can actually use 'viper-forward-word
(require 'viper)
(global-set-key (kbd "M-f") 'viper-forward-word)
(global-set-key (kbd "M-b") 'viper-backward-word)
Mostly a duplicate of this, which says:
(require 'misc)
Then bind whatever keys you want to forward-to-word and backward-to-word. For killing, create some simple functions that wrap these functions and do kill.
I don't know why jpkotta's answer was deleted, but here it is again:
I have a minor mode that changes word-based commands to operate on syntax changes (and also CamelCaseSubwords). It may be a bit too fine-grained for some tastes, but I find I basically ever use single character movement anymore.
https://bitbucket.org/jpkotta/syntax-subword
# mods, I don't know why this answer would be deleted, so if you choose to delete this answer too, I'd appreciate an explanation.

Emacs remember text selection

I decided that I was ready to try something new, after a few years of using gEdit for most of my coding needs, and try to learn using Emacs. I knew this would be difficult, as I have heard how complex Emacs can be, but I was lured by its power. The hardest thing has been getting used to writing ELisp in the .emacs file to change things about the editor. I can't currently do it myself, but I have found a few helpful snippets here and there to change some options.
One thing I have been having a lot of problems with is getting Emacs to remember the text I have selected after a command. For instance, I commonly highlight a section of code to mass indent it. However, if I do this in Emacs, it will move the selected text only once before unselecting all of the text. Does anyone know a way around this?
Anyway, I apologize for what seems to me to be an easy question, but after an hour of Google searching and looking around here on SO, I thought it was worth asking. I have a few more questions about Emacs, but I will save them and ask separately after I get this straightened out. Thanks!
UPDATE
A few people have asked about what mod I am using and what type of text I am entering. While I don't know much about Emacs modes, I am editing a pure text file at the moment. Something like this:
Hello, I am a simple text file
that is made up of three separate
lines.
If I highlight all three lines and hit TAB, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
This is great, however, if I use C-x C-x like some suggest below to reselect the text and hit TAB again, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
I hope this helps!
FWIW, here is the reason for the behaviour of your newly-added example. (I'm not 'solving' the issue here, but I'm posting it to demystify what you're seeing.)
This was determined with emacs -q which disables my customisations, so the following is default behaviour for emacs 23.2.
You are in text-mode. You should see (Text) or similar in the mode line at the bottom of the screen, and C-h m will tell you (under the list of minor modes) "Text mode: Major mode for editing text written for humans to read." Emacs decides (by way of the auto-mode-alist variable) that it should switch to text-mode if you visit a filename matching certain extensions (such as .txt).
In text-mode pressing TAB with a region highlighted causes indent-according-to-mode to be called on each line of the region in sequence. The slightly convoluted path to finding this out starts at C-h k TAB, which tells us that TAB is bound to indent-for-tab-command, which in this instance calls indent-region -- that function name is not stated explicitly in the help, but can be seen in the code -- which checks the buffer-local indent-region-function variable, which is nil, and: "A value of nil means really run indent-according-to-mode on each line."
indent-according-to-mode checks the indent-line-function variable, which has the buffer-local value indent-relative.
Use C-h f indent-relative RET to see the help for this function. (Read this).
Although you probably won't yet have had the experience to know how to check all that (or necessarily even want to!), and fully understand everything it tells you, this is an example of how the self-documenting aspect of Emacs enables a user to figure out what is going on (which then makes it feasible to change things). I essentially just used C-h k (describe-key), C-h f (describe-function), and C-h v (describe-variable) to follow the documentation. Looking at the source code for indent-for-tab-command was as simple as clicking the file name shown as part of its help page.
I suggest doing the following to help see what is happening when indent-relative runs on each line:
M-x set-variable x-stretch-cursor t
M-x set-variable ruler-mode-show-tab-stops t
M-x ruler-mode
Now for each line in turn, put the cursor at the very start of the line and press TAB. You'll end up with all three lines indented to the first tab-stop ('T' in the ruler).
Now repeat this -- again, ensure you are at the very start of each line, in front of the existing indentation.
The first character of the first line (which is currently a tab) is once again indented to the first tab-stop, as there is no preceding line for it to examine.
Next, the first character of the second line is indented to match the position of the first non-white-space character of the preceding line. Because the first character of the second line is also a tab, the actual text of the second line is pushed one tab further along.
The third line follows suit. Its first tab character is lined up with the first non-white-space character of the second line, with the same relative effect as before, giving you the final state in your example.
To emphasise, note what happens if you now put enter the line "a b c" above the existing lines, then move back to the start of the next line (what was previously the first line) and press TAB. The first tab character will now be indented in line with the 'b'. Provided that the indent-tabs-mode variable is true (meaning you have actual tab characters), then this will have no practical effect on the position of the words in the line, as 'indenting' a tab with spaces will not have an effect until the number of spaces exceeds the width of the tab (but that's another kettle of fish entirely!)
All this really means is that text-mode in Emacs doesn't behave the way you'd like it to in this situation. Other major modes can do completely different things when you press TAB, of course.
As is invariably the case with Emacs, things you don't like can be changed or circumvented with elisp. Some searching (especially at the Emacs Wiki) will frequently turn up useful solutions to problems you encounter.
Try typing C-x C-x after Emacs unselects it.
Then, instead of hitting tab (I never knew that tab does what you said! That's totally whacked.), do M-8 C-x C-i. Pity it's so many keys, but it ought to do what you want -- namely, shove everything over 8 columns. Obviously replace the M-8 with something else if you want some other number of columns.
What I usually do is simply type C-x C-x (exchange-point-and-mark) after a command that deactives the region.
How are you indenting, and in which mode?
The indentation rules in any programming mode should generally just get it right. (If they don't, that's probably more indicative that you want to configure the rules for that mode differently, but I suspect that's a different question which has been asked already).
If you're in text-mode or similar and just using TAB, then I can see the problem.
Note that if you're using indent-rigidly (C-x C-i, or C-x TAB which is the same thing) then you can repeatedly indent the same region simply by repeating the command, even if the highlighting has disappeared from view.
You can also use a prefix arg to indent-rigidly to make it indent many times. e.g. C-u C-u C-x C-i (easier to type than it looks) will indent 16 spaces (4 x 4, as the prefix arg defaults to 4, and it multiplies on each repeat). Similarly, M-8 C-x C-i indents 8 spaces. This is fine in some circumstances, and way too cumbersome in others.
Personally I suggest putting (cua-selection-mode 1) into your .emacs and using that for rigid indentation. Trey Jackson made a handy blog about it. With this, you can C-RET to start rectangle selection, down as many lines as you need, TAB repeatedly to indent the lines, and C-RET to exit the mode.
While the rectangle is active, RET cycles through the corners. For left-hand corners, typing inserts in front. For right-hand corners, typing inserts after. For the single-column rectangle, bottom counts as 'left' and top counts as 'right' for this purpose.
Trey's blog lists all the available features (or look in the source file: cua-base.el)
Be warned that indentation in Emacs is generally an unexpectedly complicated topic.
You can do this with something like the following:
(add-hook 'text-mode-hook (lambda ()
(set (make-local-variable 'indent-region-function)
(lambda (s e)
(indent-rigidly s e tab-width)))))
Then selecting a region and hitting TAB. will indent the region by a tab-width. You can then exchange point and mark with C-x C-x and hit TAB again to repeat.
I do, however, agree with the previous answers that suggest using indent-rigidly directly.