1

I'm using the ellemtel style for my C code. This mode indents goto labels like this:

void f(int x)
{ 
  if (x) {
    goto LABEL0;
  } else { 
    goto LABEL1;
  }
  LABEL0:
  printf("x is zero\n");
  return;
  LABEL1:
  printf("x isn't zero\n");
  return;
}

I'd like to turn off indentation for goto labels entirely. So that my code would look like this:

void f(int x)
{ 
  if (x) {
    goto LABEL0;
  } else { 
    goto LABEL1;
  }
LABEL0:
  printf("x is zero\n");
  return;
LABEL1:
  printf("x isn't zero\n");
  return;
}

The documentation for C-mode wasn't very helpful nor was a Google search.

1
  • The question might be a duplicate. Maybe search for tag [indentation] to check. Thx.
    – Drew
    Sep 26 at 17:28
1

Indentation is configured in c-offsets-alist, which is set by a style. You need to change the indentation for the label syntactic symbol from the default (2, unchanged in the ellemtel style) to 0.

Define your own style based on ellemtel with your preferences and make it the default. Based on your sample, it should also change c-basic-offset to 2 (from 3 in ellemtel).

(defconst tkf-c-style
  '("ellemtel"
    (c-basic-offset . 2)
    (c-offsets-alist (label . 0))))
(defun my-after-load-cc-styles ()
  (c-add-style "tkf" tkf-c-style))
(eval-after-load "cc-styles" (my-after-load-cc-styles))
(setq c-default-style "tkf")
1
  • This works perfectly. Thanks.
    – tkf
    Sep 26 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.