1

This is my code

(defun my/set-c-mode ()
 (local-set-key (kbd "C-c C-c") 'compile)
 (setq compilation-finish-function 'compilation-exit-autoclose-clone)
 (setq c-default-style "linux")
 (setq c-indent-level 8)
 (setq c-brace-imaginary-offset 0)
 (setq c-brace-offset -8)
 (setq c-argdecl-indent 8)
 (setq c-label-offset -8)
 (setq c-continued-statement-offset 8))
(add-hook 'c++-mode-hook      'my/set-c-mode)

Yet it does not indent correctly. This should give me the linux style indentation, but what I get is this:

/* File: ast.cc
 *

 ------------
 */

#include "ast.h"
#include "ast_type.h"
#include "ast_decl.h"
#include <string.h> // strdup
#include <stdio.h>  // printf

Node::Node(yyltype loc) {
  location = new yyltype(loc);
  parent = NULL;
}

Node::Node() {
  location = NULL;
  parent = NULL;
}

Identifier::Identifier(yyltype loc, const char *n) : Node(loc) {
  name = strdup(n);
} 

Any Idea why?

3
  • 4
    you should rework this question. First of all, it's not necessarily clear for everyone that the linux style indentation is 8 chars, and it's definitely not clear that it was the only thing you were missing. Second, most of the variables you mention have been obsoleted in GNU Emacs 19.23 (more than 20 years ago!!) and subsequently removed. So most people won't have any idea what you expect by setting those.
    – Sigma
    Commented Oct 11, 2014 at 3:48
  • @Sigma Which ones are obsolete? How could one find out which are obsolete? Is there a deprecation warning in Messages? I think that OP got those values from Linus's text about what people should use in emacs + kernel development.
    – c-o-d
    Commented Oct 11, 2014 at 15:19
  • @EdgarAroutiounian c-indent-level c-brace-imaginary-offset c-brace-offset c-argdecl-indent c-label-offset c-continued-statement-offset are all long gone. There's no deprecation warning anymore, since they've been completely retired: therefore this code just defines them anew, without anybody consuming them.
    – Sigma
    Commented Oct 11, 2014 at 15:29

2 Answers 2

2

Your code doesn't make much sense. You set the default style in the mode setup hook. The default style will be used when you subsequently open a file. The first file you open will be in what was the default style at the time (which is "gnu") with some parameters customized.

The Linux style is built in, so you don't need to do anything other than select it. Remove your hook and set c-default-style instead:

(setq c-default-style '(("c++" . "linux")
                        (java-mode . "java")
                        (awk-mode . "awk")
                        (other . "gnu")))

If you don't want to bake in the default value, add your setting after the CC Mode package is loaded.

(eval-after-load "cc-vars"
  '(setq c-default-style (cons ("c++" . "linux") c-default-style)))

Defining a key binding should also be done when the corresponding mode is loaded, not when opening a file.

(eval-after-load "cc-mode" '(define-key c-mode-map (kbd "C-c C-c") 'compile))

You can either set compilation-finish-function globally or only in C++ mode. If you want to set it globally, set it directly in .emacs. If you want to set it only in C++ mode, you need to make it buffer-local. Note that compilation-finish-function has been obsoleted by compilation-finish-functions. To set it for C++ mode:

(add-hook 'c++-mode-hook
  (lamdbda ()
    (add-to-list 'compilation-finish-functions 'compilation-exit-autoclose-clone t)))
-1

Fixed!

Just added (c-basic-offset 8)

1
  • That doesn't actually fix the problem. It only fixes the most obvious symptom. Commented Oct 12, 2014 at 1:25

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.