I currently try to figure out, how I can handle some lines specifically without messing up the indentation of the rest of the buffer. Consider the following function MyFunc as an example:

void MyFunc()
  unsigned y = 0;


  unsigned int x = 5 + 2;
  return x + 12;

Here, I want the line DONOTINDENT; to be indented to column 0, whereas the rest of the function should be indented normally. I tried to solve this using the following custom c-special-indent-hook:

(defun my-c-mode-indent-hook ()
  (let (p1 p2 myline)
    (setq p1 (line-beginning-position))
    (setq p2 (line-end-position))
    (setq myLine (buffer-substring-no-properties p1 p2))

    (if (string-match-p "DONOTINDENT" myLine)
          (indent-line-to 0))

(defun my-c-mode-common-hook ()
  ;; my customizations for all of c-mode and related modes
  (setq c-special-indent-hook 'my-c-mode-indent-hook)

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

OK, this works quite fine for the line DONOTINDENT;, however it does not work for all the subsequent lines, as these are indented relative to DONOTINDENT;. So, if DONOTINDENT; is indented to column 0, unsigned int x = 5 + 2; will be indented to column 0, too, but it should be indented to column c-basic-offset.

How can I fix up the indentation for the following lines? Is there some internal variable that I can use to influence the computation of the indent-level of the subsequent lines, or is this computation purely based on the preceding indentation. I think, it should be possible to somehow accomplish this as labels in switch-case-statements are indented in a similar way, but I struggled to figure this out when digging into the cc-mode code.

Thanks in advance!

Best regards, Fabian

1 Answer 1


I think it is possible, depending on the nature of the DONOTINDENT line.

A custom cc-offsets-alist rule allows different indentation rules depending on the syntactic element to indent. You could try to combine this with the correct syntactic symbols to indent specific lines as you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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