Emacs C-mode auto-indent fails after preprocessor macro - emacs

Emacs 24.2 for Windows.
case TYPE_LONG:
{ <------- 65383
traceCodePath(20);
tempLongVal = iterator_long(it);
tempLongVal = 10;
if (isBigEndian())
{
swap_endian64(&tempLongValCon, &tempLongVal);
traceCodePath(25); <------- 65601
if (i > 0)
{ <------ get weird from here /* ((substatement-open 65601)) */
} <------ this line aligns with the { below "case" /* ((block-close 65383)) */
} <------ the code afterward all align with { below "case" /* ((block-close 65383)) */
c-mode indentation worked well since the beginning, until I added some Preprocessor macro, #ifdef, #else, #end, after that, from some point in code, all afterward indentations get messy.
Every line's syntatic symbol is right, but the anchor positions are all wrong, seems they can not recognize the { at their same level, but anchor to some { before them at a upper level.
What's more weird, even I remove those preprocessor macro's again, indentation won't recover again.
What's more more weird, sometimes later, it recovered for no reason. I don't know what operations triggered this, but most possible I think is "save" "revert-buffer" etc.
Hope someone can figure the reason out, thanks in advance.

This may very well be related to "bug #14133 24.2; c functions recognition breaks on certain preprocessor macros":
http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-04/msg00055.html
The problem seems to be with spaces preceding #. The way I noticed this is that C-M-a and C-M-e stopped working correctly, and auto-indenting regions was strange just as in your case.
Recoveries were not consistent for me neither.
The patch proposed by Alan Mackenzie solved the problem for my case, you should try it out.
You can also try with emacs 23, I could not reproduce the error in this version.

Related

turn off "use spaces, do not use tabs" in a jslint extension for VSCode

I got a VSCode JSLint extension and I got its settings pointing to an .eslintrc file where I have the following specified for indentation:
{
...
"indent" : [1, "tab"]
...
}
The problem is, it's still putting the squiggly green lines where I have some tabs and I can't tell where anything's going wrong with any settings.
I have evidence the rc file is actually working because I was successfully able to change it from single to double-quotes. However it appears to completely ignore the indentation setting inside my VSCode.
You could simply disable the use_spaces rule. It's separate from the indent rule you changed. A bit over an oversight from JSLint.
There were quite a few complains about that rule, even here on SO. Quite a few people (not only on SO) suggest switching to JSHint instead. Personally I've only used ESLint and therefore don't know much about the differences, and I'd suggest checking those for yourself anyway.

Emacs: auto-fill-mode weird indentation behaviour

I am currently working on a major mode derived from auto-fill-mode where I need the text to show in a special indented way (it's a derivation of the screenwriter.el project), and I just got stuck with a peculiar behaviour of the auto-fill when there are multi line indentations:
(NOTE: use-hard-newlines is nil, and left-margin is 0):
How I expect it to work:
DUDE <-- here I insert a newline
(sad) <-- here I insert another newline
Hello, look how good <-- here I let the auto-fill-mode fill the text automatically
of a day it is!
But what happens is the following:
DUDE <-- here I insert a newline
(sad) <-- here I insert another newline
Hello, look how good <-- here I let the auto-fill-mode fill the text automatically
of a day it is!
So basically the fill mode acts weirdly when there are multi indented paragraphs, indenting to the second line's left margin. I could not find anywhere how to avoid such a behaviour.
Even stranger, the following works just fine, indenting to the precedent line's margin.
DUDE <-- here I insert a newline
Hello, look how good <-- here I let the auto-fill-mode fill the text automatically
of a day it is!
I wonder if it's just a bug or if there is a way to avoid this since it's quite annoying. Could anyone test if it works differently on other versions of emacs, or at least give me a hint on how to work around this? Thank you very much.
Emacs version: 24.3.1 on Windows
EDIT: I just checked and the same behaviour happens with use-hard-newlines on, so I'm oblivous to how to solve this
You could customize the adaptive-fill-mode variable, which is what causes a prefix to be automatically determined when filling text. Set the variable to nil to disable that behaviour.
For details of how the prefix is worked out, see the fill-context-prefix function.

how to freely format comments in cc-mode

I'm quite new to cc-mode and I'd like to configure it to allow me to freely format and use tabs in multiline comments. This is important to me because I want to use cog.py in my source file and need to be able to format the python source in the comment correctly. I'd be ok with comments not beeing autoindented at all, however I'd like to keep auto indenting the rest of the source code.
Example:
...
/*
[[[cog
import cog
for x in ['a','b','c']:
>cog.outl(x)
]]]
*/
...
In the line marked with > I'd like to press TAB to indent the line. cc-mode simply does nothing at all if i do so. I could use spaces there (which is inconvenient) but every (semi-)automatic re-indentation of this block would cause the spaces to vanish and therefore the python code to be incorrectly indented (which is what happens if i happen to press tab somewhere on this line after indenting it with spaces).
I tried to start emacs without my .init to be sure this is default behavior and not modified by my configuration so far. I've done google searches and read the documentation of the cc-mode variables / functions I stumbled upon (cc-mode online docs) while searching for a solution (i.e. c-indent-comments-syntactically-p, c-indent-command, c-tab-always-indent,...) but none of these seemed to solve my question.
EDIT1:
Thanks to abo-abo's idea of a "multi-major-mode" setup i've stumbled upon mmm-mode and have set up automatic switching to python mode for a cog section, which fixes most of my problems.
The only remaining problem is reindenting the whole file or a region containing a cog section. Can I somehow tell cc-mode to not change anything in comments while reindenting the file? mmm-mode + that would be a perfect solution for me.
You can use M-i to force a tab indent on the lines that you want, so you can use it to indent your comments.
You can also change your comments to use // instead. Just select your python code snippet, and do M-x comment-region:
// def foo(x):
// print 'hi'
Then the autoindent won't mess up your indentation.

`hideshow` mode not working with lines starting with comments

I'm using emacs (24.3.1) along with cc-mode and hideshow for programming c++. I am working on a project, where the coding styles requires that any keywords present in the header file must be repeated in the source file. In case that this is not allowed by the standard, the keyword must be placed in comments. Let me give you and example:
/* virtual */ void MyAwesomeFunction( int arg, int optarg /* = 0 */ ){
// stuff
}
Obviously there is a comment starting the line. It seems that hideshow can't cope with this sort of formatting. When I call hs-hide-all all code blocks are folded correctly, the same is true if I call hs-hide-block from anywhere within the function. However, if I call hs-hide-block whith point beeing somewhere in the opening line of the function (the first line of my codesnippet) now folding occurs and the error message:
(not enough comment lines to hide)
is printed. The only explicit configuration of hideshow I have done so far is:
(setq hs-hide-comments nil)
However, removing this line makes it even worse: Afterwards not even calling hs-hide-all works properly: all inner blocks are folded, but folding at the function level does not occurs for functions with a leading comment.
Anyone knows how to fix this?
It might help to explicitly hide comments first, either just within the region or throughout the buffer. You can use library hide-comnt.el to do that. A description is here.

Problem writing a snippet containing Emacs Lisp code

I've been trying to make use of a cool feature of YASnippet: write snippets containing embedded Emacs Lisp code. There is a snippet for rst-mode that surrounds the entered text with "=" that is as long as the text such as in
====
Text
====
Based on this snippet, I decided to slightly modify it (with Elisp) so that it comments out these three lines depending on the major mode you are in (I thought that such a snippet would be useful to organize the source code). So I wrote this:
${1:`(insert comment-start)`} ${2:$(make-string (string-width text) ?\-)}
$1 ${2:Text}
$1 ${2:$(make-string (string-width text) ?\-)}
$0
This code works relatively well except for one problem: the indentation of these three lines gets mixed up, depending on the major mode I'm in (e.g., in emacs-lisp-mode, the second and the third lines move more to the right than the first line).
I think the source of the problem might have something to do with what comes after the string ${1: on the first line. If I add a character, I have no problem (i.e., all three lines are correctly aligned at the end of the snippet expansion). If I add a single space after this string, the misalignment problem still continues though.
So my question is: do you know of any way of rewriting this snippet so that this misalignment does not arise? Do you know what's the source of this behaviour?
Cheers,
From Writing snippets:
yas/indent-line
The variable yas/indent-line controls the indenting. It is bound to 'auto by default, which causes your snippet to be indented according to the mode of the buffer it was inserted in.
Another variable yas/also-auto-indent-first-line, when non-nil does exactly that :-).
To use the hard-coded indentation in your snippet template, set this variable to fixed.
To control indentation on a per-snippet basis, see also the directive # expand-env: in Writing Snippets.
For backward compatibility with earlier versions of YASnippet, you can also place a $> in your snippet, an (indent-according-to-mode) will be executed there to indent the line. This only takes effect when yas/indent-line is set to something other than 'auto.
for (${int i = 0}; ${i < 10}; ${++i})
{$>
$0$>
}$>