Does anybody know how to inhibit emacs from indenting the name of functions or classes after a template clause?
Currenty the result is:
template <typename T>
class A {
/* ... */
};
where I would like to have:
template <typename T>
class A {
/* ... */
};
Thank you very much for your help.
EDIT 1
I'm using c++-mode with java as indent style for c++. I customized the c-offset-alist in this way:
(custom-set-variables
;;
'(c-offsets-alist (quote ((case-label . +) (innamespace . 0))))
Go to the class line and hit TAB to perform the (unsolicited) indentation.
Then press Control-CControl-Oto display the indent mode fortopmost-intro-cont`
Press ENTER, then you can change the indent number (3 to 0 for instance).
At the end of your .emacs you can set permanently that instruction:
(c-set-offset 'topmost-intro-cont 0 nil)
There are different styles for indentation for Emacs' C++ mode. Quoting EmacsWiki:
A partial list of the better known C styles:
“gnu”: The default style for GNU projects
“k&r”: What Kernighan and Ritchie, the authors of C used in their book
“bsd”: What BSD developers use, aka “Allman style” after Eric Allman.
“stroustrup”: What Stroustrup, the author of C++ used in his book
“linux”: What the Linux developers use for kernel development
“python”: What Python developers use for extension modules
“java”: The default style for java-mode (see below)
“user”: When you want to define your own style
The c-default-style variable is what you need to change. Perhaps one of them will be what you need. Don't have Emacs right now, so I can't check them out.
I don't know, but I imagine your mode makes a difference. In what mode are you editing? I assume c++-mode cause you have c++ as a tag.
For me, in c++-mode, it turned out like this:
template <typename T>
class A {
/* ... */
};
With the comments indented, but class A not indented.
A couple of different things to check:
I've seen similar issues when editing C++ .h files in C-mode instead of C++-mode. By default, .h files are C-mode, not C++-mode. [You can check this by looking for "C++" or "C" in parenthesis at the bottom of your window.] You can setup emacs to always open .h files as C++ by using the following in your .emacs
(setq auto-mode-alist (append '(("\\.h\\'" . c++-mode)
)
auto-mode-alist
))
The other thing to check is how you have setup your c-default-style. The info page for "CC Mode" goes into a lot more detail on all of the possibilities.
Related
I am seeking a plugin to do autocompletion popup for c++ development in emacs. what I have tried are Cedet Semantics and the Autocompletion mode, they are pretty neat in terms of completing the variable and function names as long as I have a few words already. For example, I have a class named foo and a function that returns an integer 1
class foo{
int getInt(){return 1};
};
In the main method, so long as I started typing this
int main(){
foo bar;
bar.get...
}
the plugins have no problem popping up suggestions like bar.getInt(). However, what I am really looking for is something like in Eclipse, as soon as I press the "dot", possible choices could be generated for me. Is that possible in Emacs? Thanks
It depends on your settings of auto-complete & CEDET. It looks like that auto-complete is setup to show possible completions only after several characters will be typed. You can check value of the ac-auto-start variable - if this is a number, then auto-complete will be called after this number of characters.
Another important thing is a what is in your ac-sources variable - for work with CEDET you need to use ac-source-semantic-raw or ac-source-semantic completion source.
To automatic completion after . or -> you can try to use Semantic's built-in completion with something like:
(defun my-c-mode-cedet-hook ()
(local-set-key "." 'semantic-complete-self-insert)
(local-set-key ">" 'semantic-complete-self-insert))
(add-hook 'c-mode-common-hook 'my-c-mode-cedet-hook)
P.S. Had you seen my article on CEDET & C++?
I have found that cedet is really underwhelming, especially under cmake projects.
I would recommend using
https://github.com/Andersbakken/rtags
It underlines errors as you type as well as using smart completions. Just add this to your init file after obtaining the required emacs packages
(require 'rtags)
(require 'popup)
(require 'rtags-ac)
(setq rtags-completions-enabled t)
(rtags-enable-standard-keybindings c-mode-base-map)
(add-hook 'c++-mode-hook
(lambda ()
(setq ac-sources '(ac-source-rtags)
)))
Is there a way to make a spell check of comments in C++ codes using emacs?
The lisp-snippet below in .emacs got it to work for me on Ubuntu Linux
(add-hook 'c-mode-common-hook 'flyspell-prog-mode)
There exist alternative setups. But I think you can find them by googling flyspell-prog-mode.
To spell check comments already in the file:
M-x ispell-comments-and-strings
To spell check comments as you type:
M-x flyspell-prog-mode
and the .emacs hooks kindahero suggested.
as mirk said flyspell-prog-mode is the obvious way.
To share my config,
;;; for prog modes turn on flyspell-prog-mode (checks spell only in comments)
(dolist (hook '(lisp-mode-hook
emacs-lisp-mode-hook
ruby-mode-hook
yaml-mode
python-mode-hook
shell-mode-hook
php-mode-hook
css-mode-hook
nxml-mode-hook
crontab-mode-hook
perl-mode-hook
javascript-mode-hook
LaTeX-mode-hook))
(add-hook hook 'flyspell-prog-mode))
Remove those modes you don't use/want.
Edit -> Spelling -> Ispell -> Spell-Check Comments
Visual Studio keeps trying to indent the code inside namespaces.
For example:
namespace Foo
{
void Bar();
void Bar()
{
}
}
Now, if I un-indent it manually then it stays that way. But unfortunately if I add something right before void Bar(); - such as a comment - VS will keep trying to indent it.
This is so annoying that basically because of this only reason I almost never use namespaces in C++. I can't understand why it tries to indent them (what's the point in indenting 1 or even 5 tabs the whole file?), or how to make it stop.
Is there a way to stop this behavior? A config option, an add-in, a registry setting, hell even a hack that modifies devenv.exe directly.
As KindDragon points out, Visual Studio 2013 Update 2 has an option to stop indenting.
You can uncheck TOOLS -> Options -> Text Editor -> C/C++ -> Formatting -> Indentation -> Indent namespace contents.
Just don't insert anything before the first line of code. You could try the following approach to insert a null line of code (it seems to work in VS2005):
namespace foo
{; // !<---
void Test();
}
This seems to suppress the indentation, but compilers may issue warnings and code reviewers/maintainers may be surprised! (And quite rightly, in the usual case!)
Probably not what you wanted to hear, but a lot of people work around this by using macros:
#define BEGIN_NAMESPACE(x) namespace x {
#define END_NAMESPACE }
Sounds dumb, but you'd be surprised how many system headers use this. (glibc's stl implentation, for instance, has _GLIBCXX_BEGIN_NAMESPACE() for this.)
I actually prefer this way, because I always tend to cringe when I see un-indented lines following a {. That's just me though.
Here is a macro that could help you. It will remove indentation if it detects that you are currently creating a namespace. It is not perfect but seems to work so far.
Public Sub aftekeypress(ByVal key As String, ByVal sel As TextSelection, ByVal completion As Boolean) _
Handles TextDocumentKeyPressEvents.AfterKeyPress
If (Not completion And key = vbCr) Then
'Only perform this if we are using smart indent
If DTE.Properties("TextEditor", "C/C++").Item("IndentStyle").Value = 2 Then
Dim textDocument As TextDocument = DTE.ActiveDocument.Object("TextDocument")
Dim startPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim matchPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim findOptions As Integer = vsFindOptions.vsFindOptionsMatchCase + vsFindOptions.vsFindOptionsMatchWholeWord + vsFindOptions.vsFindOptionsBackwards
If startPoint.FindPattern("namespace", findOptions, matchPoint) Then
Dim lines = matchPoint.GetLines(matchPoint.Line, sel.ActivePoint.Line)
' Make sure we are still in the namespace {} but nothing has been typed
If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace[\s\w]+)?[\s\{]+$") Then
sel.Unindent()
End If
End If
End If
End If
End Sub
Since it is running all the time, you need to make sure you are installing the macro inside in your EnvironmentEvents project item inside MyMacros. You can only access this module in the Macro Explorer (Tools->Macros->Macro Explorer).
One note, it does not currently support "packed" namespaces such as
namespace A { namespace B {
...
}
}
EDIT
To support "packed" namespaces such as the example above and/or support comments after the namespace, such as namespace A { /* Example */, you can try to use the following line instead:
If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace.+)?[\s\{]+$") Then
I haven't had the chance to test it a lot yet, but it seems to be working.
You could also forward declare your types (or whatever) inside the namespace then implement outside like this:
namespace test {
class MyClass;
}
class test::MyClass {
//...
};
Visual Studio 2017+
You can get to this "Indent namespace contents" setting under Tools->Options then Text Editor->C/C++->Formatting->Indention. It's deep in the menus but extremely helpful once found.
I understand the problem when there are nested namespaces. I used to pack all the namespaces in a single line to avoid the multiple indentation. It will leave one level, but that's not as bad as many levels. It's been so long since I have used VS that I hardly remember those days.
namespace outer { namespace middle { namespace inner {
void Test();
.....
}}}
I often write classes with a DLL export/import specification, but this seems to confuse emacs' syntax parser. I end up with something like:
class myDllSpec Foo {
public:
Foo( void );
};
Notice that the "public:" access spec is indented incorrectly, as well as everything that follows it.
When I ask emacs to describe the syntax at the beginning of the line containing public, I get a return of:
((label 352))
If I remove the myDllSpec, the indentation is correct, and emacs tells me that the syntax there is:
((inclass 352) (access-label 352))
Which seems correct and reasonable. So I conclude that the syntax parser is not able to handle the DLL export spec, and that this is what's causing my indentation trouble.
Unfortunately, I don't know how to teach the parser about my labels. Seems that this is pretty common practice, so I'm hoping there's a way around it.
From http://www.emacswiki.org/emacs/IndentingC#toc13 you can set up a "microsoft" style.
Drop this into your .emacs:
(c-add-style "microsoft"
'("stroustrup"
(c-offsets-alist
(innamespace . -)
(inline-open . 0)
(inher-cont . c-lineup-multi-inher)
(arglist-cont-nonempty . +)
(template-args-cont . +))))
(setq c-default-style "microsoft")
or leave the default and set it manually via M-x c-set-style to microsoft.
Your example renders this indentation:
class myDllSpec Foo {
public:
Foo( void );
};
In C++ Builder, how can I make sure that even correctly nested code like:
void func () {
...
..
)
C++ Builder correctly nested only doing so:
void func ()
{
...
...
}
This is very stressful, because I always have to correct by hand. So how can I make which indents code as well in the first instance?
The code formatter in C++Builder 2010 should do this automatically for you. (It is invoked with CTRL-D) You’ll have to set the preferences to how you like your code to be formatted, but this is a real time saver new with this most recent release.
Select the block, then press CTRL+SHIFT+I. This is in Borland C++ 6.
I am using this free tools:
http://www.cnpack.org/index.php?lang=en
using tabs to shift marked blocks to left or right.
I don't have a copy of this to run, but the documentation mentions "Indentation, Spaces, and Line breaks" as some of the options under the formatter tab of the "Tools > Options" dialog.
What you're looking for is probably under the "Line Breaks" section.