This is part of the Emacs Starter Kit.
(setq auto-mode-alist (cons '("\\.h$" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.ipp$" . c++-mode) auto-mode-alist))
(add-hook 'c-mode-common-hook 'run-starter-kit-coding-hook
'hideshowvis-minor-mode)
(add-hook 'c-mode-common-hook
(lambda ()
;; hungry delete
;;(c-toggle-hungry-state t)
;;(whitespace-mode t)
))
(setq compilation-scroll-output t)
(defun compile-notify (buffer message)
(if (string-match "^finished" message)
(sk-popup "Emacs compilation" message "normal")
(sk-popup "Emacs compilation fails" message "normal")))
(setq compilation-finish-function 'compile-notify)
If compilation is successful namely neither errors nor warnings, the compilation buffer will disappear after 1 second. Stolen from stackoverflow.
(defun sk-bury-compile-buffer-if-successful (buffer string)
"Bury a compilation buffer if succeeded without warnings "
(if (and
(string-match "compilation" (buffer-name buffer))
(string-match "finished" string)
(not
(with-current-buffer buffer
(search-forward "warning" nil t))))
(run-with-timer 1 nil
(lambda (buf)
(bury-buffer buf)
(switch-to-prev-buffer (get-buffer-window buf) 'kill))
buffer)))
(add-hook 'compilation-finish-functions 'sk-bury-compile-buffer-if-successful)
From https://github.com/redguardtoo/cpputils-cmake
(add-hook 'c++-mode-hook (lambda () (cppcm-reload-all)))
(setq cppcm-build-dirname "__build-Linux-x86_64")
The following code makes use to etags
program to generate a list of tags and
to ease the code navigation.
(defun sk-create-tags(dirname name)
"Create tags file."
(interactive "DDirectory: \nsName suffix: ")
(shell-command
(format "find %s -type f \\( -name '*.cc' -o -name '*.h' \\) | etags - && mv %s/TAGS %s/TAGS-%s" dirname default-directory temporary-file-directory name)))
(setq tags-file-name (concat temporary-file-directory "TAGS"))
;; (defconst my-cc-style
;; '("cc-mode"
;; (c-offsets-alist . ((innamespace . [0])))))
;;
;; (c-add-style "my-cc-style" my-cc-style)
(defun my-c-setup ()
(c-set-offset 'innamespace [0]))
(add-hook 'c++-mode-hook 'my-c-setup)
From http://oremacs.com/2015/01/15/c++-smart-dot/
(defconst c++-var-regex "[A-Za-z][A-Za-z0-9_]*"
"The regex for C++ variable name.")
(defun c++-get-recent-var ()
"Return the closest thing that looks like an object.
The search is performed backwards through code."
(save-excursion
(when (or
;; variable dot chain
(looking-back
(format " \\(%s\\)\\.%s.*\n[\t ]*"
c++-var-regex
c++-var-regex))
;; variable constructor init
(looking-back
(format "[\t ]+\\(%s\\)\\(?:([^)]*)\\)?;[\t\n ]*"
c++-var-regex))
;; variable dot, first on line
(re-search-backward
(format "^[ \t]*\\(%s\\)\\." c++-var-regex) nil t))
(match-string-no-properties 1))))
(defun c++-smart-dot ()
"Insert a dot or an object name plus dot when appropriate."
(interactive)
(let (var-name)
(if (and (looking-back "^[ \t]*")
(setq var-name (c++-get-recent-var)))
(insert var-name ".")
(insert "."))))
(eval-after-load "cc-mode"
`(define-key c++-mode-map "." 'c++-smart-dot))