2013-06-12 23:40:20 +02:00
|
|
|
;;==============================================================================
|
2013-07-03 13:55:42 +02:00
|
|
|
;; C/C++
|
2013-06-12 23:40:20 +02:00
|
|
|
;;==============================================================================
|
2014-03-08 17:45:29 +01:00
|
|
|
;; Note: maybe this should be split in mode-c and mode-c++.
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2014-02-26 17:22:42 +01:00
|
|
|
(defcustom cc-ldlibs "-lm -pthread"
|
2014-03-08 16:31:41 +01:00
|
|
|
"Custom linker flags for C/C++ linkage."
|
2013-07-03 13:55:42 +02:00
|
|
|
:safe 'stringp)
|
2014-03-08 16:31:41 +01:00
|
|
|
(make-variable-buffer-local 'cc-ldlibs)
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2014-02-26 17:22:42 +01:00
|
|
|
(defcustom cc-ldflags ""
|
2014-03-08 16:31:41 +01:00
|
|
|
"Custom linker libs for C/C++ linkage."
|
2013-06-12 23:40:20 +02:00
|
|
|
:safe 'stringp)
|
2014-03-08 16:31:41 +01:00
|
|
|
(make-variable-buffer-local 'cc-ldflags)
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2013-07-04 10:37:18 +02:00
|
|
|
(defun cc-set-compiler ()
|
2014-02-21 20:22:20 +01:00
|
|
|
"Set compile command to be nearest Makefile.
|
|
|
|
The Makefile is looked up in parent folders. If no Makefile is
|
|
|
|
found, then a configurable command line is provided.\n
|
|
|
|
Requires `get-closest-pathname'."
|
|
|
|
(require 'functions)
|
2013-06-12 23:40:20 +02:00
|
|
|
(interactive)
|
2014-03-08 16:31:41 +01:00
|
|
|
(let ((makefile (get-closest-pathname)))
|
|
|
|
(if makefile
|
|
|
|
(set (make-local-variable 'compile-command) (format "make -k -f %s" makefile))
|
|
|
|
(set (make-local-variable 'compile-command)
|
|
|
|
(let
|
|
|
|
((is-cpp (eq major-mode 'c++-mode))
|
|
|
|
(file (file-name-nondirectory buffer-file-name)))
|
|
|
|
(format "%s %s -o %s %s %s %s"
|
|
|
|
(if is-cpp
|
|
|
|
(or (getenv "CXX") "g++")
|
|
|
|
(or (getenv "CC") "gcc"))
|
|
|
|
file
|
|
|
|
(file-name-sans-extension file)
|
|
|
|
(if is-cpp
|
|
|
|
(or (getenv "CPPFLAGS") "-Wall -Wextra -Wshadow -DDEBUG=9 -g3 -O0")
|
|
|
|
(or (getenv "CFLAGS") "-ansi -pedantic -std=c99 -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)
|
|
|
|
(when (get-closest-pathname)
|
|
|
|
(let ((compile-command-backup compile-command))
|
2014-02-26 17:22:42 +01:00
|
|
|
(compile (format "make -k -f '%s' clean" (get-closest-pathname)))
|
2013-07-06 20:02:05 +02:00
|
|
|
(setq compile-command compile-command-backup))))
|
|
|
|
|
2013-07-03 13:55:42 +02:00
|
|
|
;;==============================================================================
|
|
|
|
;; C-mode
|
|
|
|
;;==============================================================================
|
|
|
|
|
2014-02-23 18:02:40 +01:00
|
|
|
(c-add-style
|
|
|
|
"peter"
|
|
|
|
'((c-basic-offset . 4)
|
|
|
|
(c-comment-only-line-offset . 0)
|
|
|
|
(c-hanging-braces-alist
|
|
|
|
(brace-list-open)
|
|
|
|
(brace-entry-open)
|
|
|
|
(substatement-open after)
|
|
|
|
(block-close . c-snug-do-while)
|
|
|
|
(arglist-cont-nonempty))
|
|
|
|
(c-cleanup-list brace-else-brace)
|
|
|
|
(c-offsets-alist
|
|
|
|
(statement-block-intro . +)
|
|
|
|
(knr-argdecl-intro . 0)
|
|
|
|
(substatement-open . 0)
|
|
|
|
(substatement-label . 0)
|
|
|
|
(label . 0)
|
|
|
|
(case-label . +)
|
|
|
|
(statement-cont . +))))
|
2014-02-22 19:13:19 +01:00
|
|
|
|
2014-02-13 15:56:13 +01:00
|
|
|
(mapcar
|
|
|
|
(lambda (mode-hook)
|
|
|
|
(add-hook
|
|
|
|
mode-hook
|
|
|
|
(lambda ()
|
2014-02-23 18:02:40 +01:00
|
|
|
(c-set-style "peter")
|
2014-02-13 15:56:13 +01:00
|
|
|
(cc-set-compiler)
|
|
|
|
(local-set-key (kbd "<f9>") 'cc-clean)
|
|
|
|
(local-set-key (kbd "M-TAB") 'semantic-complete-analyze-inline)
|
2014-02-20 14:06:47 +01:00
|
|
|
(local-set-key (kbd "C-c C-f") 'cc-fori)
|
|
|
|
(local-set-key (kbd "C-c m") 'cc-main)
|
|
|
|
(local-set-key (kbd "C-c C-i") 'cc-if)
|
|
|
|
(local-set-key (kbd "C-c i") 'cc-include)
|
|
|
|
(local-set-key (kbd "C-c I") 'cc-include-local)
|
2014-02-13 15:56:13 +01:00
|
|
|
;; (local-set-key "." 'semantic-complete-self-insert) ; This is a bit slow.
|
|
|
|
;; (local-set-key ">" 'semantic-complete-self-insert)
|
|
|
|
(local-set-key (kbd "C-M-e") (lambda () (interactive) (c-beginning-of-defun -1))))))
|
|
|
|
'(c-mode-hook c++-mode-hook))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2014-02-26 18:04:00 +01:00
|
|
|
;; WARNING: cc-modes do not need to run hooks for first buffer, since their hook
|
|
|
|
;; are run twice already. This behaviour may change in future Emacs version, in
|
|
|
|
;; which case we need to use add-hook-and-eval instead.
|
2014-02-26 17:22:42 +01:00
|
|
|
|
2013-06-12 23:40:20 +02:00
|
|
|
;;==============================================================================
|
|
|
|
;; Qt semantic support
|
|
|
|
;;==============================================================================
|
|
|
|
|
|
|
|
;; Qt base directory, meaning the directory where the 'Qt' directory can be found.
|
|
|
|
;; Adapt accordingly.
|
2013-07-09 11:25:15 +02:00
|
|
|
(when (fboundp 'semantic-add-system-include)
|
|
|
|
(setq qt4-base-dir "/usr/include/qt4")
|
|
|
|
(setq qt4-gui-dir (concat qt4-base-dir "/QtGui"))
|
|
|
|
(semantic-add-system-include qt4-base-dir 'c++-mode)
|
|
|
|
(semantic-add-system-include qt4-gui-dir 'c++-mode)
|
|
|
|
(add-to-list 'auto-mode-alist (cons qt4-base-dir 'c++-mode))
|
|
|
|
(add-hook
|
|
|
|
'c++-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qt4-base-dir "/Qt/qconfig.h"))
|
|
|
|
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qt4-base-dir "/Qt/qconfig-large.h"))
|
|
|
|
(add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qt4-base-dir "/Qt/qglobal.h")))))
|
2014-02-11 00:45:27 +01:00
|
|
|
|
|
|
|
;;==============================================================================
|
|
|
|
;; Skel
|
|
|
|
;;==============================================================================
|
2014-02-20 14:06:47 +01:00
|
|
|
|
2014-03-08 16:31:41 +01: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
|
|
|
|
|
|
|
(define-skeleton cc-printf
|
2014-02-21 20:22:20 +01:00
|
|
|
"fprintf/printf snippet.
|
2014-02-11 01:56:20 +01:00
|
|
|
If no file descriptor is provided, switch do printf. The format
|
2014-02-21 20:22:20 +01:00
|
|
|
string is properly parsed (%% are not taken into account).\n
|
|
|
|
Requires `count-percents'."
|
2014-02-13 15:56:13 +01:00
|
|
|
nil
|
2014-02-21 20:22:20 +01:00
|
|
|
(require 'functions)
|
2014-02-13 15:56:13 +01:00
|
|
|
'(setq v1 (skeleton-read "File desc: " "stderr"))
|
|
|
|
(if (string= v1 "") "printf (" (concat "fprintf (" v1 ", "))
|
|
|
|
"\"" (setq v1 (skeleton-read "Format string: " "%s\\n")) "\""
|
|
|
|
'(setq v2 (count-percents v1))
|
|
|
|
'(setq v1 0)
|
|
|
|
'(while (< v1 v2)
|
|
|
|
(setq v1 (1+ v1))
|
|
|
|
(skeleton-insert '(nil (concat ", " (skeleton-read "Value: ")))))
|
2014-02-13 17:22:52 +01:00
|
|
|
@ ");")
|
2014-02-13 15:56:13 +01:00
|
|
|
|
2014-02-20 14:06:47 +01:00
|
|
|
(define-skeleton cc-fori
|
2014-02-13 17:22:52 +01:00
|
|
|
"for i loop."
|
2014-02-13 15:56:13 +01:00
|
|
|
nil
|
2014-02-13 17:22:52 +01:00
|
|
|
> "for (" @ (skeleton-read "" "i = 0") "; " @ (skeleton-read "" "i < N") "; " @ (skeleton-read "" "i++") ")" \n
|
|
|
|
"{" > \n
|
|
|
|
@ _ \n
|
|
|
|
"}" > )
|
2014-02-12 17:48:29 +01:00
|
|
|
|
2014-02-20 14:06:47 +01:00
|
|
|
(define-skeleton cc-main
|
|
|
|
"Insert main function with basic includes."
|
|
|
|
nil
|
|
|
|
> "#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{" \n
|
|
|
|
> @ _ \n
|
|
|
|
> "return 0;
|
|
|
|
}" \n)
|
|
|
|
|
|
|
|
(define-skeleton cc-include
|
|
|
|
"Insert system include."
|
|
|
|
"Header: "
|
|
|
|
\n "#include <" @ str ">" \n)
|
|
|
|
|
|
|
|
(define-skeleton cc-include-local
|
|
|
|
"Insert local include."
|
|
|
|
"Header: "
|
|
|
|
\n "#include \"" @ str "\"" \n)
|
|
|
|
|
2014-02-23 19:48:24 +01:00
|
|
|
;; TODO: solve indentation issues in if-skeleton.
|
2014-02-20 14:06:47 +01:00
|
|
|
(define-skeleton cc-if
|
|
|
|
"Insert an if statement."
|
|
|
|
"Condition: "
|
|
|
|
> "if (" @ str ") {" \n
|
|
|
|
> @ _ \n
|
|
|
|
( "Other condition, %s: "
|
|
|
|
> "} else if (" @ str ") {" > \n
|
|
|
|
> @ \n)
|
|
|
|
"} else {" > \n
|
|
|
|
> @ \n
|
|
|
|
resume:
|
|
|
|
"}" > \n)
|
|
|
|
|
2014-02-12 17:48:29 +01:00
|
|
|
(provide 'mode-cc)
|