How to read ranges in the syntax table of Emacs? - emacs

E.g. Below syntax table is from text-mode (invoked by C-h s).
The parent syntax table is:
C-# .. C-h . which means: punctuation
TAB .. C-j which means: whitespace
C-k . which means: punctuation
C-l .. RET which means: whitespace
C-n .. C-_ . which means: punctuation
By intuition I guess the .. means a list range. However I could not find confirmation in the GNU emacs manual. May I confirm about it here?
In addition, may I also confirm that the range is sorted by the ASCII code number? i.e. C-# .. C-h corresponds to ASCII code 0-10.
My apology if this looks too basic. I wish I could find related document from the reference manual but it is not

I can't definitively confirm this, but based on observation I agree 100% with your assessment.
The instances of A .. Z and a .. z and 0 .. 9 make for good supporting evidence.
Clearly the control characters are being rendered in a readable format, and sometimes the selected option isn't the ideal choice.
e.g. C-l .. RET would be clearer as the equivalent C-l .. C-m; and similarly if TAB .. C-j appeared as C-i .. C-j
The section on syntax table internals points out that they are implemented as char tables, which are "much like a vector [but] indexed by character codes". As such, syntax tables are based upon sequences of character codes, and the code which displays them undoubtedly iterates through the table, ascertaining the start and finish codes for every range of characters with an identical syntax, and then using the .. format to display each range.

Related

How to type unicode characters by name in spacemacs?

Is there some plugin that allows you to type unicode characters by name easily?
For example :
Writing \gamma and hitting TAB would replace the \gamma with γ.
Writing \Gamma and ... with Γ.
Writing \-> ... →.
Et cetera.
C-x 8 RET lets you do this anywhere in Emacs. I imagine it works for Spacemacs too, but someone will correct me if that is not quite correct.
It prompts you for the Unicode name with completion (ignoring case).
You can alternatively enter just the numeric code point for the character, instead of completing to its name.
[Caveat: Dunno whether Spacemacs plays nicely with Icicles - haven't tried it.]
If you use Icicles, and if option icicle-read-char-by-name-multi-completion-flag is non-nil, then C-x 8 RET is enhanced in a few ways:
It shows in *Completions*, for each candidate Unicode
character, its name and code point, as well as the character
itself.
When you cycle among the matching candidates, the name and code
point of the current candidate are shown in the mode line. The
code point is shown in hexadecimal, octal, and decimal
notations.
Completion candidates are in fact multi-completions, meaning
that you can match against the name or the code point, or both.
You can even match the character itself. Why might you want to
do that? To see the corresponding Unicode character name(s),
including any old names. For example, for the character `
(grave accent) you get these two completion candidates:
GRAVE ACCENT 60 `
SPACING GRAVE 60 `
The main purpose for this is to show you the characters and code
points together with their names (WYSIWYG). The characters are
shown in *Completions* using the same font as the frame from
which you invoked C-x 8 RET. That you can also match against
the characters themselves is just a nice-to-have.
The most important features of the Icicles version of C-x 8 RET
are (a) seeing the characters and code points (WYSIWYG) and (b)
being able to use progressive completion, so you can use multiple
simple patterns instead of a single complex pattern.
I don't use spacemacs, so I'm not sure if this works there.
I set default-input-method to "TeX". I can toggle to it with toggle-input-method (C-\). When it's enabled, there's a \ on the far left of the mode line.
This isn't the same as entering unicode chars by name, but probably closer to what you described.

Emacs: relation between keystrokes and keys

I am slightly confused by the difference between keys and key-bindings in emacs after reading the first chapter of "Writing GNU Emacs Extensions" (O'Reilly book).
The chapter starts by noting that in some emacs version the keys backspace and DEL, invoke the help menu instead of deleting. In other words, they invoke what is invoked by C-h.
The chapter then goes on to show how to change this behaviour using Lisp code. This is done by binding the keystroke C-h to the delete command.
This is a bit confusing to me. In my mind DEL, Backspace and C-h are three different keystrokes (the first two consisting of a single key).
Why does remapping C-h effect what DEL and Backspace does?
I would have thought to change what backspace does for example, you would remap backspace to another command, not the keystroke C-h.
Unless remapped by a low-level keybord driver, the effect of the Backspace key is to send the character with numeric code 8, which, in certain operating systems, is exactly the same code generated by pressing Control-h. You can verify this fact by simply writing anything on a unix (or linux) shell and then pressing Backspace and Control-h: both of them have the effect of erasing the previous character, since the character with numeric code 8 is interpreted by the operating system as the control character BS, backspace, used either to erase the last character printed or to overprint it (see wikipedia). Analogously, Control-J is equivalent to the RETURN key, etc.
The same Wikipedia page describe DEL as “originally intended to be an ignored character, but now used in some systems to erase a character”.
So, when you assign a command to a keystroke you are actually assigning a command to a character code, and if two or more keys generate the same code, by pressing them you are invoking the same command.
#Renzo answered your question about how these keys are related and how binding one can seem to affect another. Here is some more information about what's going on in this particular case.
See the Emacs manual, node DEL Does Not Delete.
There you will see this, following an explanation of the problem/confusion:
To fix the problem in every Emacs session, put one of the following
lines into your initialization file (*note Init File::). For the first
case above, where BACKSPACE deletes forwards instead of backwards, use
this line to make BACKSPACE act as DEL:
(normal-erase-is-backspace-mode 0)
For the other two cases, use this line:
(normal-erase-is-backspace-mode 1)
Another way to fix the problem for every Emacs session is to
customize the variable normal-erase-is-backspace: the value t
specifies the mode where BS or BACKSPACE is DEL, and nil
specifies the other mode. *Note Easy Customization::.
See also the GNU Emacs FAQ question about Backspace invoking help. There you will see, in addition to information similar to that above, information about how to remap DEL on UNIX - use this:
stty erase '^?'
Wrt C-j and RET (not mentioned in the question, but mentioned in #Renzo's answer): The default behavior of Emacs changed in most programming modes, in Emacs 24.4.
Starting with that release, electric--indent-mode is turned on by default, which means that RET inserts a newline character and indents, whereas C-j just inserts a newline character. Prior to that release, these keys had the opposite behaviors. If you prefer the old behavior then do this, to turn off electric-indent-mode:
(when (fboundp 'electric-indent-mode)
(electric-indent-mode -1)) ; Use classic `C-j' and `RET'.

Are RET and C-m really the same exactly?

In the graphical mode, RET does the same as C-m, namely newline.
However, when I use M-a or M-e, they seem to distinguish between them. The newline produced by RET, they skip all to a double-RET point, and with the newline produced by C-m, they move the cursor line by line. Why so?
Description (C-h k) for both keys shows the same.
If it matters, it is Emacs23.3.1 on Ubuntu 12.04.
The answer could be dependent on the mode you're in as well as in the key codes generated by your operating system. When I hit Ctrl-h k Ctrl-m in the *scratch* buffer on my Windows box, I'll get the info for (newline) with the information that it is bound to RET. Theoretically, in some other mode, C-m and RET could be set to different functions. This requires, of course, that the OS doesn't generate the same key code.
What Emacs writes as RET (or as <RET>) is, yes, the same character as C-m - it is the ASCII control character 13. This is so regardless of what keyboard etc. you have.
However, the logical key (aka "key sequence") that Emacs sees when you hit the phisical (keyboard) key named Enter or Return might be different from RET (aka C-m). To see what key Emacs recognizes when you hit that keyboard key, use C-h k and hit the key. If Emacs says RET then this is the typical case, and yes, hitting that key is then identical to pressing Control and hitting m (assuming the key labeled Control does what it does typically).
In the graphical mode, the answer is yes (unless keys are redefined, of course).
Careful examination of my file showed that the effect I observed was a coincidence. It was exactly in those paragraphs which I C-m'ed that all lines would end in a dot (it was a poem).
Actually, C-h k key description says the same thing for both keys. Thanks to schaueho's answer for pushing me in this direction.

Retaining tab-to-tab-stop with Emacs replace-regexp

I have a very large text file with nearly 20 columns. Unfortunately the program which produced the file did not properly handle columns which began with a -10 (it only properly spaced data which had a total of 5 characters, not 6). Essentially I ended up with something that looks like this:
-10.072-10.179-10.2190.002
I want it to look like:
-10.072 -10.179 -10.219 0.002
and was almost able to do so with:
M-x replace-regexp RET -10\.... RET \&TAB RET
When I use TAB however, it only replaces with a space rather than tab- ideally a tab-to-tab-stop. If I manually go to one of these situations in the file and type TAB it properly does a tab-to-tab-stop to align the data with the proper column. How do I retain the tab-to-stop function within the replace-regexp?
The search and replace certainly ought to be inserting a tab. The apparent size of a tab can vary, of course, which is the only reason I can think of for it appearing to be a space. You could use whitespace-mode to make the difference more obvious.
As for tab-to-tab-stop, that's a dynamic function rather than a special kind of tab character, so you can't do that with a search and replace1. I would suggest using a keyboard macro instead, to get the same dynamic behaviour as manual typing.
F3
M-C-s -10.... RET
M-x tab-to-tab-stop RET
F4
or perhaps just: F3M-C-s -10.... RETTABF4
Then you can run the macro until it fails with C-0F4
(If you only want to run it on a portion of the buffer, you can simply narrow to the relevant region first.)
1 Not strictly true, as Emacs lets you evaluate arbitrary elisp as part of a replacement pattern; but it's not just a case of calling the tab-to-tab-stop function, so the macro is really much simpler.

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.