Umut Tabak
Umut Tabak

Reputation: 1942

google-style file for C++ for emacs

I use the google-style file for emacs. It also looks like a good one to start learning some emacs lisp, not that long. However there is sth I am trying configure in that file, maybe some already did that before, for coding a class, I wrote,

namespace A
{
  class A_A
  {
 public:
    A_A();
 private:
    int a;
  };
}

however public/private keywords are not at the right places, I did not understand why it places them like this out of the box, how can fix this? I am not good at emacs lisp yet unfortunately.

EDIT: I wanted to have sth like

namespace A
{
  class A_A
  {
    public:
      A_A();
    private:
      int a;
  };
}

Upvotes: 2

Views: 6611

Answers (2)

DdD
DdD

Reputation: 612

Probably nowadays a good solution is given by the config file provided in Github by Google.
In the repository styleguide there is the config file google-c-style.el that, as described in the file,

;; Provides the google C/C++ coding style. You may wish to add
;; `google-set-c-style' to your `c-mode-common-hook' after requiring this
;; file. For example:
;;
;;    (add-hook 'c-mode-common-hook 'google-set-c-style)
;;
;; If you want the RETURN key to go to the next line and space over
;; to the right place, add this to your .emacs right after the load-file:
;;
;;    (add-hook 'c-mode-common-hook 'google-make-newline-indent)

The file is also distributed via MELT package system as google-c-style.el.

Upvotes: 3

gavenkoa
gavenkoa

Reputation: 48753

To get indent you like use such debug techniques:

(setq c-echo-syntactic-information-p t)

When you press TAB for indenting you will see something like:

syntax: ((inclass 33) (access-label 33))

As you can see access-label identify how indent priv/pub modifiers. So change to what you want:

(defconst my-c-style
  '(
    (c-tab-always-indent . t)
    (c-offsets-alist
     . (
        (access-label . /)  ; XXXXXX LOOK HERE!!!!!!!
        ))
   )
  "My C Programming Style")

(defun my-c-mode-style-hook ()
  (c-add-style "my" my-c-style t)
  ;; If set 'c-default-style' before 'c-add-style'
  ;; "Undefined style: my" error occured from 'c-get-style-variables'.
  (setq c-default-style
        '(
          (java-mode . "my") (c-mode . "my") (csharp-mode . "my") (c++-mode . "my") 
          (other . "my")
          ))
  )
(add-hook 'c-mode-common-hook 'my-c-mode-style-hook)

In example I remove half-level indent as inclass add one full indent (to get 1/2 of indent. For offset syntax read C-h v c-offsets-alist RET. For example:

  If OFFSET is one of the symbols `+', `-', `++', `--', `*', or `/'
  then a positive or negative multiple of `c-basic-offset' is added to
  the base indentation; 1, -1, 2, -2, 0.5, and -0.5, respectively.

Upvotes: 4

Related Questions