Tuesday, July 14, 2015

Emacs cc-mode indenting

Use cc-mode to set emacs coding styles for different languages. The configuration basics shows how to use hooks in the .emacs file. I wanted to  customize indentation and you can interactively determine which parameters to change and test different values.
   The default GNU style indents C++ as shown below.
for (unsigned i = 0; i<10; i++)
  {
    std::cout << i << std::endl;
  }
I want to indent this way
for (unsigned i = 0; i<10; i++)
{
  std::cout << i << std::endl;
}
So I added the my-c++-mode-hook at the bottom of my /home/USER/.emacs file.  I've copied the entire file to have a record of my current settings.

;;;Use font lock mode
(global-font-lock-mode 1)

;;;keep the display simple
(scroll-bar-mode 0)
(menu-bar-mode 0)
(tool-bar-mode 0)

;;;Pretty colors
(set-background-color "darkslategrey")
(set-foreground-color "lightsteelblue")
(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.
  '(inhibit-startup-screen t))

(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.
)

;; change the substatement indenting to 0 so opening bracket on next line is below first letter of conditional statement
(defun my-c++-mode-hook()
  (c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)