Emacs indentation for multi-level nesting of C code

人走茶凉 提交于 2019-12-08 06:01:59

问题


I'm completely new to emacs (mostly used vim and eclipse/netbeans etc.) I was playing with multi-level nesting of C code and wrote a sample code in emacs to test how it indents codes where nesting is way too deep (not real life code though).

int foo()
{
    if (something) {
        if (anotherthing) {
            if (something_else) {
                if (oh_yes) {
                    if (ah_now_i_got_it) {
                        printf("Yes!!!\n");
                    }
                }
            }
        }
    }
}

This looked exactly like this as I typed in emacs and saved it. But opening it on a different text editor showed the actual text saved is this:

int foo()
{
    if (something) {
    if (anotherthing) {
        if (something_else) {
        if (oh_yes) {
            if (ah_now_i_got_it) {
            printf("Yes!!!\n");
            }
        }
        }
    }
    }
}

So I was wondering is there any way in emacs to save the text the way it actually displays?

My current c-default-style is set to "linux".

EDIT:

Ok, I was using Notepad++/Vim to view the file saved by emacs and it showed that "wrong" indentation, but looks like, opening with good old notepad (or even doing a cat file.c) shows the correct indentation as displayed by emacs. Will try the other approaches mentioned here. Thanks!


回答1:


Try using spaces instead of tabs for indentation. Add the following to your init.el:

(setq-default indent-tabs-mode nil)

This will make all buffers use spaces by default. You will want to add the following exception for makefiles:

(add-hook 'makefile-mode-hook (lambda () (setq indent-tabs-mode t)))


来源:https://stackoverflow.com/questions/14136090/emacs-indentation-for-multi-level-nesting-of-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!