2013-07-03 13:55:42 +02:00
|
|
|
;; C/C++
|
2016-10-14 14:03:30 +02:00
|
|
|
|
2017-05-15 07:46:20 +02:00
|
|
|
;; Should we split this into mode-c and mode-c++?
|
|
|
|
|
|
|
|
(local-set-key (kbd "C-c m") 'cc-main)
|
|
|
|
(local-set-key (kbd "<f9>") 'cc-clean)
|
|
|
|
(local-set-key (kbd "M-.") 'semantic-ia-fast-jump)
|
|
|
|
(local-set-key (kbd "C-c C-d") 'semantic-ia-show-summary)
|
|
|
|
(local-set-key (kbd "M-TAB") 'semantic-complete-analyze-inline)
|
|
|
|
(local-set-key (kbd "C-c o") 'ff-find-other-file)
|
|
|
|
|
2017-05-28 12:32:48 +02:00
|
|
|
(when (require 'company nil t)
|
|
|
|
(local-set-key (kbd "M-TAB") (if (require 'helm-company nil t) 'helm-company 'company-complete)))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2017-05-11 19:38:01 +02:00
|
|
|
(defvar-local cc-ldlibs "-lm -pthread"
|
|
|
|
"Custom linker flags for C/C++ linkage.")
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2017-05-11 19:38:01 +02:00
|
|
|
(defvar-local cc-ldflags ""
|
|
|
|
"Custom linker libs for C/C++ linkage.")
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2014-03-24 19:33:33 +01:00
|
|
|
(defun cc-set-compiler (&optional nomakefile)
|
|
|
|
"Set compile command to be nearest Makefile or a generic command.
|
2014-02-21 20:22:20 +01:00
|
|
|
The Makefile is looked up in parent folders. If no Makefile is
|
2014-03-24 19:33:33 +01:00
|
|
|
found (or if NOMAKEFILE is non-nil or if function was called with
|
|
|
|
universal argument), then a configurable command line is
|
|
|
|
provided.\n Requires `get-closest-pathname'."
|
|
|
|
(interactive "P")
|
2014-03-13 13:50:56 +01:00
|
|
|
(require 'functions)
|
|
|
|
(hack-local-variables)
|
2014-03-08 16:31:41 +01:00
|
|
|
(let ((makefile (get-closest-pathname)))
|
2017-05-11 19:38:01 +02:00
|
|
|
(if (and makefile (not nomakefile))
|
2017-05-12 08:34:17 +02:00
|
|
|
(setq compile-command (concat "make -k -C " (shell-quote-argument (file-name-directory makefile))))
|
|
|
|
(setq compile-command
|
2017-05-28 12:32:48 +02:00
|
|
|
(let
|
|
|
|
((c++-p (eq major-mode 'c++-mode))
|
|
|
|
(file (file-name-nondirectory buffer-file-name)))
|
|
|
|
(format "%s %s -o '%s' %s %s %s"
|
|
|
|
(if c++-p
|
|
|
|
(or (getenv "CXX") "g++")
|
|
|
|
(or (getenv "CC") "gcc"))
|
|
|
|
(shell-quote-argument file)
|
|
|
|
(shell-quote-argument (file-name-sans-extension file))
|
|
|
|
(if c++-p
|
|
|
|
(or (getenv "CXXFLAGS") "-Wall -Wextra -Wshadow -DDEBUG=9 -g3 -O0")
|
|
|
|
(or (getenv "CFLAGS") "-ansi -pedantic -std=c11 -Wall -Wextra -Wshadow -DDEBUG=9 -g3 -O0"))
|
|
|
|
(or (getenv "LDFLAGS") cc-ldflags)
|
|
|
|
(or (getenv "LDLIBS") cc-ldlibs)))))))
|
2013-07-03 13:55:42 +02:00
|
|
|
|
2013-07-06 20:02:05 +02:00
|
|
|
(defun cc-clean ()
|
|
|
|
"Find Makefile and call the `clean' rule. If no Makefile is
|
|
|
|
found, no action is taken. The previous `compile' command is then
|
|
|
|
restored."
|
|
|
|
(interactive)
|
2016-10-14 14:32:46 +02:00
|
|
|
(let (compile-command (makefile (get-closest-pathname)))
|
|
|
|
(when makefile
|
|
|
|
(compile (format "make -k -f '%s' clean" makefile)))))
|
2013-07-06 20:02:05 +02:00
|
|
|
|
2017-05-28 12:32:48 +02:00
|
|
|
;;; It is tempting to add `cc-fmt' to the hook:
|
|
|
|
;; (add-hook 'before-save-hook 'cc-fmt nil t)
|
|
|
|
;;; Unlike Go however, there is no formatting standard and thus this would break
|
|
|
|
;;; the formatting rules of every third-party C file that does not follow the
|
|
|
|
;;; same style.
|
2014-11-05 16:42:53 +01:00
|
|
|
(defun cc-fmt ()
|
2016-06-12 22:48:36 +02:00
|
|
|
"Run uncrustify(1) on current buffer or region."
|
2014-11-05 16:42:53 +01:00
|
|
|
(interactive)
|
2017-05-14 08:30:34 +02:00
|
|
|
(let (status
|
|
|
|
start end
|
|
|
|
(formatbuf (get-buffer-create "*C format buffer*")))
|
2017-05-24 22:44:11 +02:00
|
|
|
(if (use-region-p)
|
2017-05-14 08:30:34 +02:00
|
|
|
(setq start (region-beginning) end (region-end))
|
|
|
|
(setq start (point-min) end (point-max)))
|
2014-11-05 16:42:53 +01:00
|
|
|
(setq status
|
2017-05-14 08:30:34 +02:00
|
|
|
(call-process-region start end "uncrustify" nil formatbuf nil "-lc" "-q" "-c" (concat (getenv "HOME") "/.uncrustify.cfg")))
|
2014-11-05 16:42:53 +01:00
|
|
|
(if (/= status 0)
|
2017-02-15 13:41:15 +01:00
|
|
|
(error "error running uncrustify")
|
2017-05-14 08:30:34 +02:00
|
|
|
(delete-region start end)
|
2017-05-24 00:27:02 +02:00
|
|
|
(insert-buffer-substring formatbuf)
|
2014-11-05 16:42:53 +01:00
|
|
|
(kill-buffer formatbuf))))
|
|
|
|
|
2016-10-14 14:32:46 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2017-05-28 12:32:48 +02:00
|
|
|
;;; Options
|
2016-06-12 22:48:36 +02:00
|
|
|
|
2017-05-28 12:32:48 +02:00
|
|
|
;;; Semanticdb folders must be set before starting semantic.
|
2016-06-12 22:48:36 +02:00
|
|
|
(setq semanticdb-default-save-directory (concat emacs-cache-folder "semanticdb"))
|
|
|
|
(semantic-mode 1)
|
|
|
|
|
2017-05-28 12:32:48 +02:00
|
|
|
;;; Extra semantic support
|
|
|
|
;;; Example:
|
|
|
|
;; (when (fboundp 'semantic-add-system-include)
|
|
|
|
;; (semantic-add-system-include "new/header/dir" 'c++-mode)
|
|
|
|
;; (add-to-list 'auto-mode-alist (cons qt4-base-dir 'c++-mode))
|
|
|
|
;; (add-hook
|
|
|
|
;; 'c++-mode-hook
|
|
|
|
;; (lambda ()
|
|
|
|
;; (when semantic-mode
|
|
|
|
;; (add-to-list 'semantic-lex-c-preprocessor-symbol-file "new/header/dir/config.h")))))
|
2013-07-03 13:55:42 +02:00
|
|
|
|
2014-02-23 18:02:40 +01:00
|
|
|
(c-add-style
|
2015-07-11 12:33:45 +02:00
|
|
|
"ambrevar"
|
2014-10-26 18:04:11 +01:00
|
|
|
`((c-comment-only-line-offset . 0)
|
2014-10-31 13:06:26 +01:00
|
|
|
(c-auto-align-backslashes . nil)
|
|
|
|
(c-basic-offset . ,tab-width)
|
|
|
|
(c-offsets-alist
|
|
|
|
(arglist-cont-nonempty . +)
|
|
|
|
(arglist-intro . +)
|
|
|
|
(c . 0)
|
|
|
|
(case-label . 0)
|
|
|
|
(cpp-define-intro . 0)
|
2014-11-14 21:48:09 +01:00
|
|
|
(cpp-macro . 0)
|
2014-10-31 13:06:26 +01:00
|
|
|
(knr-argdecl-intro . 0)
|
|
|
|
(label . 0)
|
|
|
|
(statement-block-intro . +)
|
|
|
|
(statement-cont . +)
|
|
|
|
(substatement-label . 0)
|
|
|
|
(substatement-open . 0)
|
|
|
|
)))
|
2014-02-22 19:13:19 +01:00
|
|
|
|
2017-05-28 12:32:48 +02:00
|
|
|
;;; Note that in Emacs 24, cc-mode calls its hooks manually in each mode init
|
|
|
|
;;; function. Since cc modes belong to prog-mode, each hook is called another
|
|
|
|
;;; time at the end of the initialization. No big deal since we only set some
|
|
|
|
;;; variables.
|
2017-05-22 16:24:25 +02:00
|
|
|
(dolist (hook '(c-mode-hook c++-mode-hook))
|
2017-05-28 12:03:28 +02:00
|
|
|
(add-hook-and-eval
|
|
|
|
hook
|
|
|
|
(lambda ()
|
|
|
|
(c-set-style "ambrevar"))) ;; We override existing values.
|
|
|
|
(when (require 'company nil t)
|
|
|
|
(add-hook-and-eval hook 'company-mode))
|
|
|
|
(add-hook-and-eval hook 'cc-set-compiler))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2016-10-14 14:32:46 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2017-05-28 12:32:48 +02:00
|
|
|
;;; Skeletons
|
2014-02-20 14:06:47 +01:00
|
|
|
|
2017-05-28 12:32:48 +02:00
|
|
|
;;; Note that it is possible to extend the skel syntax with
|
|
|
|
;;; `skeleton-further-elements'. For instance:
|
|
|
|
; (setq skeleton-further-elements '((q "\"")))
|
2014-02-20 14:06:47 +01:00
|
|
|
|
2014-03-10 00:11:55 +01:00
|
|
|
(define-skeleton cc-debug
|
|
|
|
"Insert debug macros."
|
2014-02-13 15:56:13 +01:00
|
|
|
nil
|
2014-10-31 13:06:26 +01:00
|
|
|
> "#ifdef DEBUG
|
2014-03-10 00:11:55 +01:00
|
|
|
#define DEBUG_CMD(CMD) do {CMD;} while(0)
|
|
|
|
#else
|
|
|
|
#define DEBUG_CMD(CMD) do {} while(0)
|
|
|
|
#endif
|
|
|
|
|
2014-10-31 13:06:26 +01:00
|
|
|
"
|
|
|
|
'(insert-and-indent
|
2017-05-08 11:59:19 +02:00
|
|
|
"#define DEBUG_PRINT(...) DEBUG_CMD( \\
|
|
|
|
fprintf(stderr, \"%s:%d:\\t(%s)\\t\", __FILE__, __LINE__, __func__); \\
|
2014-10-31 13:06:26 +01:00
|
|
|
fprintf(stderr, __VA_ARGS__); \\
|
|
|
|
fprintf(stderr, \"\\n\"); \\
|
|
|
|
)"))
|
2014-02-13 15:56:13 +01:00
|
|
|
|
2014-03-20 13:09:50 +01:00
|
|
|
(define-skeleton cc-getopt
|
|
|
|
"Insert a getopt template."
|
|
|
|
nil
|
2014-10-31 13:06:26 +01:00
|
|
|
> "int opt;" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"while ((opt = getopt(argc, argv, \":hV\")) != -1) {" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"switch(opt) {" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"case 'h':" > \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"usage(argv[0]);" \n
|
|
|
|
"return 0;" \n
|
|
|
|
"case 'V':" > \n
|
|
|
|
"version();" \n
|
|
|
|
"return 0;" \n
|
|
|
|
"case ':':" > \n
|
|
|
|
"fprintf(stderr, \"ERROR: -%c needs an argument.\\nTry '%s -h' for more information.\\n\", optopt, argv[0]);" \n
|
|
|
|
"return EXIT_FAILURE;" \n
|
|
|
|
"case '?':" > \n
|
|
|
|
"fprintf(stderr, \"ERROR: Unknown argument %c.\\nTry '%s -h' for more information.\\n\", optopt, argv[0]);" \n
|
|
|
|
"return EXIT_FAILURE;" \n
|
|
|
|
"default:" > \n
|
|
|
|
"usage(argv[0]);" \n
|
|
|
|
"return EXIT_SUCCESS;" \n
|
|
|
|
"}" > \n
|
|
|
|
"}" > "\n" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"if (optind >= argc) {" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"fprintf(stderr, \"Expected argument after options\\n\");" \n
|
|
|
|
"exit(EXIT_FAILURE);" \n
|
|
|
|
"}" > \n)
|
2014-03-20 13:09:50 +01:00
|
|
|
|
2014-03-10 00:11:55 +01:00
|
|
|
(define-skeleton cc-loadfile
|
|
|
|
"Insert loadfile function."
|
|
|
|
nil
|
2014-11-14 21:48:09 +01:00
|
|
|
"unsigned long loadfile(const char *path, char **buffer_ptr) {" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"#define MAX_FILESIZE 1073741824 /* One gigabyte */" > "\n" \n
|
|
|
|
"/* Handle variable. */" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"char *buffer;" "\n" \n
|
|
|
|
"FILE *file = fopen(path, \"rb\");" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"if (file == NULL) {" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"perror(path);" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"return 0;" \n
|
|
|
|
"}" > "\n" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"fseek(file, 0, SEEK_END);" \n
|
|
|
|
"long length = ftell(file);" \n
|
|
|
|
"/* fprintf(stdout, \"Note: file %s is %u bytes long.\\n\", path, length); */" "\n" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"if (length > MAX_FILESIZE) {" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"fprintf(stderr, \"%s size %ld is bigger than %d bytes.\\n\", path, length, MAX_FILESIZE);" \n
|
|
|
|
"fclose(file);" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"return 0;" \n
|
|
|
|
"}" > "\n" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"fseek(file, 0, SEEK_SET);" \n
|
|
|
|
"buffer = (char *)malloc(length + 1);" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"if (buffer == NULL) {" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"perror(\"malloc\");" \n
|
|
|
|
"fclose(file);" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"return 0;" \n
|
|
|
|
"}" > "\n" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"if (fread(buffer, 1, length, file) == 0) {" \n
|
|
|
|
"fclose(file);" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"return 0;" \n
|
|
|
|
"}" > "\n" \n
|
|
|
|
"buffer[length] = '\\0';" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"fclose(file);" "\n" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"*buffer_ptr = buffer;" \n
|
|
|
|
"return length;" \n
|
|
|
|
"}" > \n)
|
2014-03-10 00:11:55 +01:00
|
|
|
|
2014-02-20 14:06:47 +01:00
|
|
|
(define-skeleton cc-main
|
|
|
|
"Insert main function with basic includes."
|
|
|
|
nil
|
2014-03-20 13:09:50 +01:00
|
|
|
> "#include <inttypes.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2014-02-20 14:06:47 +01:00
|
|
|
#include <stdio.h>
|
2014-03-20 13:09:50 +01:00
|
|
|
#include <stdlib.h>
|
2014-02-20 14:06:47 +01:00
|
|
|
#include <string.h>
|
2014-03-20 13:09:50 +01:00
|
|
|
#include <unistd.h>
|
2014-02-20 14:06:47 +01:00
|
|
|
|
2014-11-14 21:48:09 +01:00
|
|
|
int main(int argc, char **argv) {" \n
|
2014-02-20 14:06:47 +01:00
|
|
|
> @ _ \n
|
2014-10-31 13:06:26 +01:00
|
|
|
> "return 0;
|
2014-02-20 14:06:47 +01:00
|
|
|
}" \n)
|
|
|
|
|
2014-03-20 13:09:50 +01:00
|
|
|
(define-skeleton cc-usage-version
|
|
|
|
"Insert usage() and version() functions."
|
2014-10-31 13:06:26 +01:00
|
|
|
"Synopsis: "
|
|
|
|
> "static void usage(const char *executable) {" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"printf(\"Usage: %s [OPTIONS]\\n\\n\", executable);" \n
|
|
|
|
"puts(\"" str "\\n\");" "\n" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
|
2014-11-14 21:48:09 +01:00
|
|
|
"puts(\"Options:\");" \n
|
|
|
|
"puts(\" -h Print this help.\");" \n
|
|
|
|
"puts(\" -V Print version information.\");" "\n" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
|
2014-11-14 21:48:09 +01:00
|
|
|
"puts(\"\");" \n
|
|
|
|
"printf(\"See %s for more information.\\n\", MANPAGE);" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"}" > "\n" \n
|
|
|
|
|
|
|
|
"static void version() {" \n
|
2014-11-14 21:48:09 +01:00
|
|
|
"printf(\"%s %s\\n\", APPNAME, VERSION);" \n
|
|
|
|
"printf(\"Copyright © %s %s\\n\", YEAR, AUTHOR);" \n
|
2014-10-31 13:06:26 +01:00
|
|
|
"}" > \n)
|
2014-03-20 13:09:50 +01:00
|
|
|
|
2014-02-12 17:48:29 +01:00
|
|
|
(provide 'mode-cc)
|