Change emacs indentation for cmake? - emacs

For the LIFE of me I can't figure out why emacs indents lines in *.cmake files with 2 spaces per indent rather than what the Standard Indent is set to in customize. I also have c-basic-offset set to 4. I can't find anything with apropos help, google, C-h C-m...
EVERYTHING ELSE works with indentation spacing of 4 -- python, c++, etc. Why does cmake indent to 2 spaces while everything else does 4? How can I change it back to use 4 spaces?

From a quick look at cmake-mode, cmake-tab-width is the variable you want to change.Try (setq cmake-tab-width 4) somewhere in your Emacs startup files.
This is with Emacs 24.3 on Ubuntu 14.04; cmake-mode.el seems to come from CMake 2.8.12.2 (all stock Ubuntu provided packages).

Related

EMACS, text seems to be shifted

Using,
EMACS 24.5.1 (encoding: UTF-8 (dos/unix))
OS: Windows 8
Terminal: mintty with UTF-8 under Cygwin
Problem:
Any existing text file that I open in EMACS does not hold its indentation (text is shifted either left or right of its original position). The same text file if I open it in VIM in the same terminal session, seems to be have the correct alignment.
This happens to every text file that I open in EMACS.
I assumed the problem might be with encoding or EOL. So I tried changing the encoding of the terminal output in EMACS(using C-x RET r RET | C-x RET t RET) but that did not help.
I have been reading the official manual but can't seem to solve this issue.
Has anybody got any suggestion of how I should correct this indentation/alignment/EOL problem?
Thanks in advance.
P.S. I saw some similar emacs encoding problems on here but none of them seemed to have helped.
The problem was with the tab size and nothing to do with the encoding.
Emacs is actually pretty awesome in detecting the encoding.
The tab size = 8 white space character in Emacs by default. All my other editors I had set to 4 white space characters hence they all seem to be shifted or lost their indentation when opened in Emacs.
I have used the below configuration in my .emacs file to correct this to tabs = 4 white space char.
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
Thanks again Chris for the solution

emacs tab and spaces indentation for visual studio projects

At my work i have switched to using emacs from Visual Studio. Since codebase is large and my other team mates use Visual Studio (VS), i can not remove the tabs.
For myself i did this :
(setq-default indent-tabs-mode nil)
;; tab-width
(setq-default tab-width 1)
So everything looks good to me, but when i am putting code for review, it looks unintended for them or in other editors.
changing tab-width to 4 made things better but i have to remember to do C-q <TAB> again and again.
I start facing the problem :
The code has lines of average of length 130. Some go as far as 200. And i work on multiple buffers like 4 at a time. This makes code hard to read.
I then let the emacs default take charge and remove both indent-tabs-mode nil and tab-width line and i found that it was much better. It was automatically inserting tab and everything for me.
However, i had little bad experience at some places which are looking good in VS.
Also i had put these configurations from default-basics github repo. Contrastingly, there is another article on spaces are evil. Internet is full of one or another and i am confused.
What is the general guidelines that i can follow ?
SO i see Tab as 1 space it would be great. But in file they should go as "they should be".
Looking at the documentation of
tab-width
Documentation:
Distance between tab stops (for display of tab characters), in columns.
This should be an integer greater than zero.
and
indent-tabs-mode
Documentation:
Indentation can insert tabs if this is non-nil.
I removed the (setq-default indent-tabs-mode nil)
and set the (set tab-width 1)
so now emacs handles the indentation and i see the tabs as 1 column width.

Why does emacs display tabs differently?

So I've heard about the goodness of emacs and have only recently started using it. Forgive me if this is a stupid question, but why does emacs display tabs, differently? It seems as though it doubles the number of spaces, but it doesn't, at least, I guess. Here're some pictures to describe what I'm talking about:
And this is what it looks like in emacs:
As I've previously stated, it seems as though it doubles the number of spaces. When I add this line to .emacs:
(setq c-basic-offset 4)
and reindenting the code using C-x h C-M-\ makes it look normal in emacs, but the secondary indentation are in-line with the first indentation (as in 2 tabs are now 1 tab) when viewed in other text editors, and again, I couldn't understand why. Changing it to
(setq c-basic-offset 8)
makes it save and display normally in other text editors though. At this point I'm really, really confused.
Can someone please explain why? Thanks.
The variable tab-width is the distance between tab spaces in columns, and defaults to 8. If you'd like it to default to 4, you can (setq-default tab-width 4). If you'd like to untabify everything and convert tabs to spaces, you can do M-: (untabify (point-min) (point-max)).
And you might find this thread helpful, especially the point on tab-stop-list when you want to ADD your own tabs.
You can also adopt sanity and not use TAB chars in your code. ;-)
To prevent inserting TAB chars when you hit the TAB key (and RET or C-j, depending on your Emacs version) set the value of option indent-tabs-mode to nil.
To remove pre-existing TAB chars from code you are editing, use command untabify.
See also Tabs Are Evil and Untabify Upon Save.
And note that, in Emacs, whether or not you use TAB chars is unrelated to how much and whether code is indented. For example, option c-basic-offset governs indentation amount regardless of whether TABs are used for some of the indenting.
Note too that after you kick the TAB habit, any TAB chars left in your code that are meaningful to the code are much easier to find. They are not lost in an ocean of insignificant-whitespace TABs.
Finally, note that there are various ways to highlight TAB chars. Command hc-toggle-highlight-tabs in library highlight-chars.el is one way. See Show Whitespace.

Using tabs alone for indentation in Emacs

I love Emacs but don't like how it does indentation: either an uncontrollable mix of tabs-and-spaces or just spaces ((setq indent-tabs-mode nil)).
I want Emacs to do do indentation:
With tabs alone.
Do the indentation to a fixed number of places (not 6 sometimes, 8 other times and 4 in some other places).
Be able to set one level of indentation as being equal to 4 (or 2) spaces.
If I change the value of the tab stop, all newly-opened or reloaded files should use the new value (or can this change be affected only by re-starting Emacs?)
Is all of the above possible? Some settings in .emacs or a package?
Most IDEs (e.g. Eclipse) offer the above.
smart tabs would insert tabs and spaces contextually.
Personally I only use spaces for both indentation and alignment (at least for my own projects). Here is another article on emacswiki I found very useful about the topic
For C/C++/Java, you could try adding to your mode hook an identical tab-width, indent-level and c-basic-offet:
(defun my-c-mode-common-hook ()
(setq c-indent-level 3
c-brace-offset -3)
(setq c-basic-offset 3)
(setq-default tab-width 3)
(setq tab-width 3))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
This makes it so when you're in cc-mode, tabs and indenting are equal so emacs will always choose tabs over spaces. See also Indenting C
The tab-width can be set elsewhere and you can apply it to other modes in conjunction with indent length variables like python's python-indent.
Edit:
Actually, it looks like kindahero's link pretty much does this: http://www.emacswiki.org/SmartTabs
I use tabs for indentation. But when someone else using a different editor, they could see that the indentation is gone. So, you can select the piece of code that you indented using tabs and run "M-x untabify". This replaces the tabs with white space, so the first said issue won't be there for the users using a different editor.

How to indent 4 spaces under SGML mode

You know under SGML mode (PSGML) Emacs could indent the XML buffer automatically.
C-x-h ;; Select all
M-C-\ ;; Indent
But all the indent was 2 spaces. How can I set to make auto indent to 4 spaces?
I tried (setq-default sgml-indent-step 4), but it doesn't work.
Current view is:
<TESTCASE>
<NAME>00001<NAME>
<TIMEOUT>600000</TIMEOUT>
My expectation is
<TESTCASE>
<NAME>00001<NAME>
<TIMEOUT>600000</TIMEOUT>
Thanks!
Using GNU Emacs 22.2.1 (i386-mingw-nt5.1.2600)
of 2008-03-26 on RELEASE
You're looking for sgml-basic-offset
Edit: Actually, I should add how I figured that out. No one should understate the power that a self-documented editor provides:
Open an sgml document
C-h k tab says it's bound to a function that looks at variable indent-line-function.
C-h v indent-line-function told me to look at function sgml-indent-line
C-h f sgml-indent-line told me very little. Nothing really. But it also provided where it was implemented.
Well, looking at the implementation, I found that it's using sgml-calculate-indent. That's the meat of the algorithm... Go down and you can find the tag case, where the last element does
(+ (current-column) sgml-basic-offset)
A quick C-h v sgml-basic-offset will make sure we're looking at the one variable that is supposed to be customized. It indeed is.