gyro91
gyro91

Reputation: 35

if-statement Indentation not working emacs

I am new to emacs. I have been using this configuration for emacs:

(setq column-number-mode t) 
(setq c-default-style "linux"
          c-basic-offset 4)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-enabled-themes (quote (wheatgrass))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

Generally, the indentation is correctly handled by emacs automatically. For an if-statement, however, even if I could see the correct indentation in emacs then in an other editor the tab is not shown.

I have been editing a *.c file and the mode is "C/l mode defined in ‘cc-mode.el’. Major mode for editing C code".

Here an example: If I create an c file in emacs with the following code:

int main() {
    int x = 1;

    if (x > 0)
        x++;
    else
        x--;

    printf("Value of x: %d\n", x);

    return 0;
}

If I visualize this code in different editor or I copy it from an emacs into another editor the output is the following:

int main() {
    int x = 1;

    if (x > 0)
    x++;
    else
    x--;

    printf("Value of x: %d\n", x);

    return 0;
}

Basically, it seems that even if it looks like it is correctly indenting the if statement it has not added any tab character in the if statement.

What is the problem? How could I fix it?

Upvotes: 0

Views: 975

Answers (2)

phils
phils

Reputation: 73334

Emacs is mixing tabs and spaces for indentation, and your other editor is using a different tab width.

Looking at your examples I can ascertain that in Emacs tab-width is 8, c-basic-offset is 4, and indent-tabs-mode is t.

If we prefix the number of spaces (s) and/or tabs (t) needed with that configuration:

0 |int main() {
4s|    int x = 1;
  |
4s|    if (x > 0)
1t|        x++;
4s|    else
1t|        x--;
  |
4s|    printf("Value of x: %d\n", x);
  |
4s|    return 0;
0 |}

In your other editor your tab width is obviously equivalent to only 4 spaces, and hence "4s" and "1t" are displayed as the same width.

If you M-x set-variable RET tab-width RET 4 RET in Emacs, you will see the same thing.

n.b. M-x whitespace-mode can visualise the indentation characters for you.

For best cross-editor compatibility, either use only spaces for indentation -- indent-tabs-mode set to nil -- or if you prefer tabs then you should ensure that tab-width and c-basic-offset are the same value, which means that every level of indentation will be a tab (at least in languages where indentation is by some specific amount, as opposed to aligning with arbitrary other code).

It seems that you do want tabs, so try this configuration:

(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
  "Custom `c-mode' behaviours."
  (setq c-basic-offset 4)
  (setq tab-width c-basic-offset)
  (setq indent-tabs-mode t))

If you were going with spaces-only then you would just disable indent-tabs-mode:

(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
  "Custom `c-mode' behaviours."
  (setq indent-tabs-mode nil))

Or you could disable it by default, rather than for a specific major mode. indent-tabs-mode is always a buffer-local value, but if you change its default value then that will affect all future buffers in which it isn't set explicitly:

(setq-default indent-tabs-mode nil)

Upvotes: 3

Picaud Vincent
Picaud Vincent

Reputation: 10982

Note sure it is the right direction, however you can check if tabs are the origin of the problem. The command:

C-u M-x untabify

transforms all tabs into spaces.

I would suggest to try:

  1. apply this untabify command to your code buffer,
  2. save it,
  3. open the file with the other text editor,

if you get now the correct indentation, we have identified to origin of the problem: the tabs.

A possible fix is to systematically use spaces instead tabs. You can have a look at: How to force spaces instead of tabs regardless of major mode?

Upvotes: 2

Related Questions