Skip to content

Latest commit

 

History

History
135 lines (121 loc) · 4.56 KB

starter-kit-c++.org

File metadata and controls

135 lines (121 loc) · 4.56 KB

Starter Kit C++

This is part of the Emacs Starter Kit.

Starter Kit C++

Set C++ mode for *.h and *.ipp files (instead of plain-old C mode)

(setq auto-mode-alist (cons '("\\.h$"   . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.ipp$" . c++-mode) auto-mode-alist))

Running starter-kit-coding-hook

(add-hook 'c-mode-common-hook 'run-starter-kit-coding-hook
          'hideshowvis-minor-mode)

Un/fold function/namespace definitions

  (add-hook 'c-mode-common-hook
            (lambda ()
              ;; hungry delete
              ;;(c-toggle-hungry-state t)
              ;;(whitespace-mode t)
))

Compilation

Scroll down compilation messages

(setq compilation-scroll-output t)

Send notification when compilation is done

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

Bury compilation buffer

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)

cpputils-cmake

From https://github.com/redguardtoo/cpputils-cmake

(add-hook 'c++-mode-hook (lambda () (cppcm-reload-all)))
(setq cppcm-build-dirname "__build-Linux-x86_64")

Tags

The following code makes use to etags program to generate a list of tags and to ease the code navigation.

Create TAGS table

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

Set TAGS filename

(setq tags-file-name (concat temporary-file-directory "TAGS"))

No indentation within namespace block

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

Automatically adding object name

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