2017-06-08 11:47:56 +02:00
|
|
|
;;; lang/cc/autoload.el -*- lexical-binding: t; -*-
|
2017-02-19 18:57:16 -05:00
|
|
|
|
2017-12-26 23:39:21 -05:00
|
|
|
;;;###autoload
|
2018-01-05 13:13:07 -05:00
|
|
|
(defun +cc/reload-compile-db ()
|
2017-12-27 00:11:09 -05:00
|
|
|
"Reload the current project's JSON compilation database."
|
2018-01-05 13:13:07 -05:00
|
|
|
(interactive)
|
2017-12-26 23:39:21 -05:00
|
|
|
(unless (memq major-mode '(c-mode c++-mode objc-mode))
|
|
|
|
(user-error "Not a C/C++/ObjC buffer"))
|
|
|
|
(unless (doom-project-has! "compile_commands.json")
|
|
|
|
(user-error "No compile_commands.json file"))
|
|
|
|
;; first rtag
|
|
|
|
(when (and (featurep 'rtags)
|
|
|
|
rtags-enabled
|
|
|
|
(executable-find "rc"))
|
|
|
|
(with-temp-buffer
|
|
|
|
(message "Reloaded compile commands for rtags daemon")
|
|
|
|
(rtags-call-rc :silent t "-J" (doom-project-root))))
|
|
|
|
;; then irony
|
|
|
|
(when (and (featurep 'irony) irony-mode)
|
|
|
|
(+cc|irony-init-compile-options)))
|
|
|
|
|
2017-02-19 18:57:16 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +cc-sp-point-is-template-p (id action context)
|
2017-09-17 22:01:04 +02:00
|
|
|
"Return t if point is in the right place for C++ angle-brackets."
|
2017-02-19 18:57:16 -05:00
|
|
|
(and (sp-in-code-p id action context)
|
2018-02-14 05:06:26 -05:00
|
|
|
(cond ((eq action 'insert)
|
|
|
|
(sp-point-after-word-p id action context))
|
|
|
|
((eq action 'autoskip)
|
|
|
|
(/= (char-before) 32)))))
|
2017-02-19 18:57:16 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +cc-sp-point-after-include-p (id action context)
|
2017-09-17 22:01:04 +02:00
|
|
|
"Return t if point is in an #include."
|
2017-02-19 18:57:16 -05:00
|
|
|
(and (sp-in-code-p id action context)
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (line-beginning-position))
|
|
|
|
(looking-at-p "[ ]*#include[^<]+"))))
|
2017-09-17 22:01:04 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2018-02-23 04:44:02 -05:00
|
|
|
(defun +cc-c++-lineup-inclass (langelem)
|
|
|
|
"Indent inclass lines one level further than access modifier keywords."
|
|
|
|
(when (and (eq major-mode 'c++-mode)
|
|
|
|
(or (assoc 'access-label c-syntactic-context)
|
|
|
|
(save-excursion
|
|
|
|
(save-match-data
|
|
|
|
(re-search-backward
|
|
|
|
"\\(?:p\\(?:ublic\\|r\\(?:otected\\|ivate\\)\\)\\)"
|
|
|
|
(c-langelem-pos langelem) t)))))
|
|
|
|
'++))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +cc-lineup-arglist-close (langlem)
|
|
|
|
"Line up the closing brace in an arglist with the opening brace IF cursor is
|
|
|
|
preceded by the opening brace or a comma (disregarding whitespace in between)."
|
|
|
|
(when (save-excursion
|
|
|
|
(save-match-data
|
|
|
|
(skip-chars-backward " \t\n" (c-langelem-pos langelem))
|
|
|
|
(memq (char-before) (list ?, ?\( ?\;))))
|
|
|
|
(c-lineup-arglist langlem)))
|
2017-09-17 22:01:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Hooks
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +cc|fontify-constants ()
|
|
|
|
"Better fontification for preprocessor constants"
|
2018-04-21 21:04:55 -04:00
|
|
|
(when (memq major-mode '(c-mode c++-mode))
|
|
|
|
(font-lock-add-keywords
|
|
|
|
nil '(("\\<[A-Z]*_[A-Z_]+\\>" . font-lock-constant-face)
|
|
|
|
("\\<[A-Z]\\{3,\\}\\>" . font-lock-constant-face))
|
|
|
|
t)))
|
2017-09-17 22:01:04 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-09-19 05:06:50 +02:00
|
|
|
(defun +cc|irony-init-compile-options ()
|
|
|
|
"Initialize compiler options for irony-mode. It searches for the nearest
|
2017-12-27 17:10:21 -05:00
|
|
|
compilation database and initailizes it, otherwise falling back on
|
|
|
|
`+cc-default-compiler-options' and `+cc-default-include-paths'.
|
2017-09-19 05:06:50 +02:00
|
|
|
|
|
|
|
See https://github.com/Sarcasm/irony-mode#compilation-database for details on
|
|
|
|
compilation dbs."
|
|
|
|
(when (memq major-mode '(c-mode c++-mode objc-mode))
|
|
|
|
(require 'irony-cdb)
|
|
|
|
(unless (irony-cdb-autosetup-compile-options)
|
|
|
|
(irony-cdb--update-compile-options
|
2017-12-27 17:10:21 -05:00
|
|
|
(append (delq nil (cdr-safe (assq major-mode +cc-default-compiler-options)))
|
|
|
|
(cl-loop for path in +cc-default-include-paths
|
2017-09-20 03:09:56 +02:00
|
|
|
nconc (list "-I" path)))
|
2017-09-19 05:06:50 +02:00
|
|
|
(doom-project-root)))))
|
2017-09-17 22:01:04 +02:00
|
|
|
|
2018-03-01 13:17:07 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +cc|init-rtags ()
|
|
|
|
"Start an rtags server in c-mode and c++-mode buffers."
|
2018-05-09 11:26:48 +02:00
|
|
|
(when (and (memq major-mode '(c-mode c++-mode))
|
|
|
|
(rtags-executable-find "rtags"))
|
2018-03-01 13:17:07 -05:00
|
|
|
(rtags-start-process-unless-running)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +cc|cleanup-rtags ()
|
|
|
|
"Kill rtags server(s) if there are no C/C++ buffers open."
|
|
|
|
(unless (doom-buffers-in-mode '(c-mode c++-mode) (buffer-list))
|
|
|
|
(rtags-cancel-process)))
|