;;; ... -*- lexical-binding: t -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; C support ;;; ;;; This supports modes derived from cc-mode (defun c-backward-defun (&optional arg) "Move backwards to the beginning of this function declaration. This differs from `c-beginning-of-defun' in that it actually takes the point to the beginning of the function definition instead of just its block." (interactive "p") (unless arg (setq arg 1)) (while (> arg 0) (c-beginning-of-defun) (c-beginning-of-statement) (setq arg (1- arg)))) ;;; Setup cedet features if available (when (require 'cedet "cedet.el" t) ;; EDE: project management (global-ede-mode 1) (setq ede-locate-setup-options '(ede-locate-global ede-locate-cscope)) ;; Semantic parser support, menus ;; (semantic-load-enable-code-helpers) ;; (semantic-load-enable-all-exuberent-ctags-support) ;; Decorate tags ;; (global-semantic-decoration-mode 1) ;; (require 'semantic-decorate-include) ;; (semantic-toggle-decoration-style "semantic-decoration-on-private-members" t) ;; (semantic-toggle-decoration-style "semantic-decoration-on-protected-members" t) ;; (global-semantic-highlight-func-mode 1) ;; Idle completion (condition-case nil (progn (global-semantic-idle-completions-mode 1) ;; Enable preparsing many neighboring files. (setq semantic-idle-work-parse-neighboring-files-flag t) ) (error nil))) ;;; Define C-specific features (require 'drkp-features) (defmodefeature c-defun-jump (local-set-key "\M-p" (function c-backward-defun)) (local-set-key "\M-n" (function c-end-of-defun))) (defmodefeature c-compile (local-set-key "\C-c\C-c" (function compile))) (defmodefeature semantic-ia-complete (local-set-key (kbd "") (function semantic-ia-complete-symbol)) (local-set-key (kbd "") (function semantic-ia-fast-jump))) (defmodefeature c-auto-hungry (c-toggle-auto-hungry-state 1)) ;; (defmodefeature c-filladapt ;; (when (featurep 'filladapt) ;; (turn-on-filladapt-mode) ;; (c-setup-filladapt))) (defmodefeature c-magic-punctuation (when (require 'c-magic-punctuation nil t) (c-magic-punctuation-mode))) (defmodefeature c-show-func (when (require 'show-context-mode nil t) (show-context-mode 1))) (defmodefeature java-find-file (require 'java-find-file)) (defmodefeature java-fix-generics ;; Setting this to t at least fixes the indentation of braces for ;; constructors of generic classes. It might fix other things, too. (setq c-recognize-<>-arglists t)) (defmodefeature xcscope (require 'xcscope nil 't)) ; some systems don't have xscope ;; Put braces after an if on their own line in BSD style (vs on the ;; same line as the condition) (c-add-style "bsd" (append (list (cons 'c-hanging-braces-alist (append '((substatement-open before after) (substatement-close before after)) (copy-alist (cdr (assoc 'c-hanging-braces-alist c-fallback-style)))))) (cdr (assoc "bsd" c-style-alist))) nil) (c-add-style "pgsql" '("bsd" (indent-tabs-mode . t) (c-basic-offset . 4) (tab-width . 4) (c-offsets-alist . ((case-label . +))) ) nil ) ; t = set this mode, nil = don't (c-add-style "specpaxos" '("k&r" (indent-tabs-mode . nil) (c-basic-offset . 4) (c-offsets-alist . ((innamespace . [0])))) nil) (defmodefeature c-choose-style (let ((filename (buffer-file-name)) (hostname (system-name))) (cond ((not filename)) ((or (string-match "/jos/" filename) (string-match "/6.828/" filename)) (message "Setting style for 6.828") ;; (c-set-style "gnu") ;;(make-local-variable 'c-basic-offset) (c-set-style "bsd") (make-local-variable 'perl-indent-level) (setq perl-indent-level 8 tab-width 8 indent-tabs-mode t)) ((string-match "/qemu" filename) (message "Setting style for qemu") (setq c-basic-offset 4)) ((or (string-match "/pgsql" filename) (string-match "/postgres" filename)) (message "Setting style for pgsql") (c-set-style "pgsql")) ((and (functionp 'vmstyle-set-c-style) (string-match "/bora/" filename)) (message "Setting style for VMware") (vmstyle-set-c-style))))) ;; Set some default style settings (setq c-basic-offset 4 c-default-style '((c++-mode . "k&r") (java-mode . "java") (awk-mode . "awk") (other . "k&r"))) ;;; Set up the mode itself ;; Automatically determine the major mode of header files (unless (require 'h-auto-mode-load nil t) ;; A lot of .h files are actually C++ (add-to-list 'auto-mode-alist (cons "\\.h\\'" (function c++-mode)))) ;; Set C's features (drkp:add-mode-features 'c-mode-common-hook '(autofill flyspell-prog highlight-unhappy final-newline-always c-defun-jump c-auto-hungry ; c-filladapt c-magic-punctuation c-show-func c-compile c-choose-style xcscope semantic-ia-complete)) ;; Java features (drkp:add-mode-features 'java-mode-hook '(java-find-file java-fix-generics)) ;; Set Perl "features", for those unhappy times... (drkp:add-mode-features 'perl-mode-hook '(flyspell-prog final-newline-always c-choose-style))