2015-11-15 12:21:53 +00:00
|
|
|
|
;;; funcs.el --- C/C++ Layer functions File for Spacemacs
|
|
|
|
|
;;
|
2017-01-06 03:51:13 +00:00
|
|
|
|
;; Copyright (c) 2012-2017 Sylvain Benner & Contributors
|
2015-11-15 12:21:53 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(defun spacemacs//clang-format-on-save ()
|
2017-04-07 09:21:05 +00:00
|
|
|
|
"Format buffers with ClangFormat when they get saved."
|
2017-04-09 21:50:21 +00:00
|
|
|
|
(when c-c++-enable-clang-format-on-save
|
|
|
|
|
(clang-format-buffer)))
|
|
|
|
|
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(defun spacemacs/clang-format-on-save ()
|
2017-04-09 21:50:21 +00:00
|
|
|
|
"Add auto-save hook for ClangFormat."
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(add-hook 'before-save-hook 'spacemacs//clang-format-on-save nil t))
|
2017-04-07 09:21:05 +00:00
|
|
|
|
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(defun spacemacs/company-more-than-prefix-guesser ()
|
|
|
|
|
(spacemacs/c-c++-load-clang-args)
|
2017-01-02 05:39:04 +00:00
|
|
|
|
(company-clang-guess-prefix))
|
|
|
|
|
|
2015-11-15 12:21:53 +00:00
|
|
|
|
;; Based on the Sarcasm/irony-mode compilation database code.
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(defun spacemacs/company-find-clang-complete-file ()
|
2015-11-15 12:21:53 +00:00
|
|
|
|
(when buffer-file-name
|
|
|
|
|
(let ((dir (locate-dominating-file buffer-file-name ".clang_complete")))
|
|
|
|
|
(when dir
|
|
|
|
|
(concat (file-name-as-directory dir) ".clang_complete")))))
|
|
|
|
|
|
|
|
|
|
;; Based on the Sarcasm/irony-mode compilation database code.
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(defun spacemacs/company-load-clang-complete-file (cc-file)
|
2015-11-15 12:21:53 +00:00
|
|
|
|
"Load the flags from CC-FILE, one flag per line."
|
|
|
|
|
(let ((invocation-dir (expand-file-name (file-name-directory cc-file)))
|
|
|
|
|
(case-fold-search nil)
|
|
|
|
|
compile-flags)
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(insert-file-contents cc-file)
|
|
|
|
|
;; Replace relative paths with absolute paths (by @trishume)
|
|
|
|
|
;; (goto-char (point-min))
|
2016-08-26 16:32:42 +00:00
|
|
|
|
(while (re-search-forward "\\(-I\\|-isystem\n\\)\\(\\S-+\\)" nil t)
|
2015-11-15 12:21:53 +00:00
|
|
|
|
(replace-match (format "%s%s" (match-string 1)
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(expand-file-name (match-string 2)
|
|
|
|
|
invocation-dir))))
|
2015-11-15 12:21:53 +00:00
|
|
|
|
;; Turn lines into a list
|
|
|
|
|
(setq compile-flags
|
|
|
|
|
;; remove whitespaces at the end of each line, if any
|
|
|
|
|
(mapcar #'(lambda (line)
|
|
|
|
|
(if (string-match "[ \t]+$" line)
|
|
|
|
|
(replace-match "" t t line)
|
|
|
|
|
line))
|
|
|
|
|
(split-string (buffer-string) "\n" t))))
|
|
|
|
|
compile-flags))
|
|
|
|
|
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(defun spacemacs/c-c++-load-clang-args ()
|
2015-12-21 14:37:26 +00:00
|
|
|
|
"Sets the arguments for company-clang, the system paths for company-c-headers
|
|
|
|
|
and the arguments for flyckeck-clang based on a project-specific text file."
|
2015-11-15 12:21:53 +00:00
|
|
|
|
(unless company-clang-arguments
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(let* ((cc-file (spacemacs/company-find-clang-complete-file))
|
|
|
|
|
(flags (if cc-file
|
|
|
|
|
(spacemacs/company-load-clang-complete-file cc-file)
|
|
|
|
|
'()))
|
2015-12-21 14:37:26 +00:00
|
|
|
|
(dirs (mapcar (lambda (f) (substring f 2))
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(remove-if-not (lambda (f) (string-prefix-p "-I" f))
|
|
|
|
|
flags))))
|
2015-11-15 12:21:53 +00:00
|
|
|
|
(setq-local company-clang-arguments flags)
|
2017-04-09 21:55:50 +00:00
|
|
|
|
(setq-local company-c-headers-path-system
|
|
|
|
|
(append '("/usr/include" "/usr/local/include")
|
|
|
|
|
dirs))
|
2015-12-21 14:37:26 +00:00
|
|
|
|
(setq-local flycheck-clang-args flags))))
|
2017-05-14 17:20:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; realgud
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//short-key-state (modeon)
|
|
|
|
|
"Set evil-evilified-state explicitly."
|
|
|
|
|
(if modeon
|
|
|
|
|
(evil-evilified-state)
|
|
|
|
|
(evil-normal-state)))
|