+
+ (defun sd/company-stop-input-space ()
+ "Stop completing and input a space,a workaround of a semantic issue `https://github.com/company-mode/company-mode/issues/614'"
+ (interactive)
+ (company-abort)
+ (insert " "))
+#+END_SRC
+
+** YASnippet
+*** yasnippet
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package yasnippet
+ :ensure t
+ :defer t
+ :init
+ (add-hook 'prog-mode-hook #'yas-minor-mode)
+ :config
+ (yas-reload-all))
+#+END_SRC
+
+** YouCompleteMe
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package ycmd
+ :ensure t
+ :init
+ (use-package company-ycmd
+ :ensure t
+ :init
+ (setq company-ycmd-enable-fuzzy-matching nil)
+ :config
+ (company-ycmd-setup))
+ (setq ycmd-force-semantic-completion t)
+ (add-hook 'c++-mode-hook 'ycmd-mode)
+ (add-hook 'c-mode-hook 'ycmd-mode)
+ (set-variable 'ycmd-server-command '("python" "/Users/peng/project/ycmd/ycmd"))
+ (set-variable 'ycmd-global-config "/Users/peng/project/ycmd/cpp/ycm/.ycm_extra_conf.py"))
+#+END_SRC
+
+
+** company and yasnippet
+Add yasnippet as the company candidates
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ ;Add yasnippet support for all company backends
+ ;https://github.com/syl20bnr/spacemacs/pull/179
+ (defvar company-mode/enable-yas t
+ "Enable yasnippet for all backends.")
+
+ (defun company-mode/backend-with-yas (backend)
+ (if (or (not company-mode/enable-yas) (and (listp backend) (member 'company-yasnippet backend)))
+ backend
+ (append (if (consp backend) backend (list backend))
+ '(:with company-yasnippet))))
+
+ (setq company-backends (mapcar #'company-mode/backend-with-yas company-backends))
+#+END_SRC
+
+Refer, [[http://emacs.stackexchange.com/questions/7908/how-to-make-yasnippet-and-company-work-nicer][how-to-make-yasnippet-and-company-work-nicer]]
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (defun check-expansion ()
+ (save-excursion
+ (if (looking-at "\\_>") t
+ (backward-char 1)
+ (if (looking-at "\\.") t
+ (backward-char 1)
+ (if (looking-at "->") t nil)))))
+
+ (defun do-yas-expand ()
+ (let ((yas/fallback-behavior 'return-nil))
+ (yas/expand)))
+
+ (defun tab-indent-or-complete ()
+ (interactive)
+ (cond
+ ((minibufferp)
+ (minibuffer-complete))
+ (t
+ (indent-for-tab-command)
+ (if (or (not yas/minor-mode)
+ (null (do-yas-expand)))
+ (if (check-expansion)
+ (progn
+ (company-manual-begin)
+ (if (null company-candidates)
+ (progn
+ (company-abort)
+ (indent-for-tab-command)))))))))
+
+ (defun tab-complete-or-next-field ()
+ (interactive)
+ (if (or (not yas/minor-mode)
+ (null (do-yas-expand)))
+ (if company-candidates
+ (company-complete-selection)
+ (if (check-expansion)
+ (progn
+ (company-manual-begin)
+ (if (null company-candidates)
+ (progn
+ (company-abort)
+ (yas-next-field))))
+ (yas-next-field)))))
+
+ (defun expand-snippet-or-complete-selection ()
+ (interactive)
+ (if (or (not yas/minor-mode)
+ (null (do-yas-expand))
+ (company-abort))
+ (company-complete-selection)))
+
+ (defun abort-company-or-yas ()
+ (interactive)
+ (if (null company-candidates)
+ (yas-abort-snippet)
+ (company-abort)))
+
+ '
+ ;; (require 'company)
+ ;; (require 'yasnippet)
+
+
+ ;; (global-set-key [tab] 'tab-indent-or-complete)
+ ;; (global-set-key (kbd "TAB") 'tab-indent-or-complete)
+ ;; (global-set-key [(control return)] 'company-complete-common)
+
+ ;; (define-key company-active-map [tab] 'expand-snippet-or-complete-selection)
+ ;; (define-key company-active-map (kbd "TAB") 'expand-snippet-or-complete-selection)
+
+ ;; (define-key yas-minor-mode-map [tab] nil)
+ ;; (define-key yas-minor-mode-map (kbd "TAB") nil)
+
+ ;; (define-key yas-keymap [tab] 'tab-complete-or-next-field)
+ ;; (define-key yas-keymap (kbd "TAB") 'tab-complete-or-next-field)
+ ;; (define-key yas-keymap [(control tab)] 'yas-next-field)
+ ;; (define-key yas-keymap (kbd "C-g") 'abort-company-or-yas)
+#+END_SRC
+
+* Libs
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package s
+ :ensure t)
+#+END_SRC
+
+* Programming Language
+** Color identiifiers mode
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package color-identifiers-mode
+ :ensure t
+ :init
+ (dolist (mode '(emacs-lisp-mode-hook
+ c-mode-hook
+ c++-mode-hook))
+ (add-hook mode #'color-identifiers-mode))
+ :diminish color-identifiers-mode)
+
+ (global-prettify-symbols-mode t)
+#+END_SRC
+** Font lock face
+
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (set-face-attribute 'font-lock-keyword-face nil :foreground "#F92672" :weight 'bold)
+ (set-face-attribute 'font-lock-builtin-face nil :weight 'bold)
+ (set-face-attribute 'font-lock-function-name-face nil :foreground "#A6E22E" :weight 'normal :slant 'normal)
+ (set-face-attribute 'font-lock-variable-name-face nil :foreground "#00FF00")
+
+ (dolist (mode '(c-mode
+ c++-mode))
+ (font-lock-add-keywords mode
+ ;; highlight %s \n
+ '(("\\([%\\][a-zA-Z]\\)" (1 font-lock-keyword-face prepend))
+ ("[^[:alpha:]]\\([[:digit:]]*\\)[^[:alpha:]]" (1 font-lock-constant-face append))
+ ;; hex number
+ ("[^[:alpha:]]\\(0[x\\|X][0-9a-fA-F]*\\)[^[:alpha:]]" (1 font-lock-constant-face append))
+ ;; hightlight the function call
+ ("\\s\"?\\(\\(\\sw\\|\\s_\\)+\\(<-\\)?\\)\\s\"?*\\s-*(" (1 font-lock-function-name-face))) t))
+
+ (font-lock-add-keywords 'c++-mode
+ '(
+ ("[\s\\|::]\\(cin\\|cout\\|endl\\)[^[:alpha:][:digit:]]" (1 font-lock-constant-face append))
+ ) t)
+#+END_SRC
+
+** Emacs Lisp
+In Lisp Mode, =M-o= is defined, but I use this for global hydra window. So here disable this key
+bindings in =lispy-mode-map= after loaded. see [[http://stackoverflow.com/questions/298048/how-to-handle-conflicting-keybindings][here]]
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (use-package lispy
+ :ensure t
+ :init
+ (setq lispy-delete-backward-recenter 0)
+ (with-eval-after-load "lispy"
+ (define-key lispy-mode-map (kbd "M-o") nil)
+ (define-key lispy-mode-map (kbd "g") 'special-lispy-goto-local)
+ (define-key lispy-mode-map (kbd "G") 'special-lispy-goto)
+ (define-key lispy-mode-map (kbd "M-m") 'back-to-indentation))
+ :config
+ (add-hook 'emacs-lisp-mode-hook (apply-partially #'lispy-mode 1)))
+#+END_SRC
+
+** Perl
+*** CPerl mode
+[[https://www.emacswiki.org/emacs/CPerlMode][CPerl mode]] has more features than =PerlMode= for perl programming. Alias this to =CPerlMode=
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (defalias 'perl-mode 'cperl-mode)
+
+ ;; (setq cperl-hairy t)
+ ;; Turns on most of the CPerlMode options
+ (setq cperl-auto-newline t)
+ (setq cperl-highlight-variables-indiscriminately t)
+ ;(setq cperl-indent-level 4)
+ ;(setq cperl-continued-statement-offset 4)
+ (setq cperl-close-paren-offset -4)
+ (setq cperl-indent-parents-as-block t)
+ (setq cperl-tab-always-indent t)
+ ;(setq cperl-brace-offset 0)
+
+ (add-hook 'cperl-mode-hook (apply-partially #'cperl-set-style "C++"))
+
+ (defalias 'perldoc 'cperl-perldoc)
+#+END_SRC
+
+*** Perl template
+Refer [[https://www.emacswiki.org/emacs/AutoInsertMode][AutoInsertMode]] Wiki
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (eval-after-load 'autoinsert
+ '(define-auto-insert '("\\.pl\\'" . "Perl skeleton")
+ '(
+ "Empty"
+ "#!/usr/bin/perl -w" \n
+ \n
+ "use strict;" > \n \n
+ > _
+ )))
+#+END_SRC
+
+*** Perl Keywords
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ (font-lock-add-keywords 'cperl-mode
+ '(("\\(say\\)" . cperl-nonoverridable-face)
+ ("\\([0-9.]\\)*" . font-lock-constant-face)
+ ("\".*\\(\\\n\\).*\"" 1 font-lock-constant-face prepend)
+ ("\n" 0 font-lock-constant-face prepend)
+ ;; ("[%\\][[:alpha:]]" . font-lock-constant-face)
+ ("\\(^#!.*\\)$" . cperl-nonoverridable-face)))
+#+END_SRC
+
+** C & C++
+C/C++ ide tools
+1. completion (file name, function name, variable name)
+2. template yasnippet (keywords, if, function)
+3. tags jump
+*** c/c++ style
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ ;; (setq c-default-style "stroustrup"
+ ;; c-basic-offset 4)
+
+ ;; ;; "C-M-j" is my global binding for avy goto line below
+ ;; ;; disable it in c mode
+ ;; (mapcar #'(lambda (map)
+ ;; (define-key map (kbd "C-M-j") nil))
+ ;; (list c-mode-map
+ ;; c++-mode-map
+ ;; objc-mode-map))
+
+ ;; objective c
+ (add-to-list 'auto-mode-alist '("\\.mm\\'" . objc-mode))
+
+ (setq-default c-hungry-delete-key t)
+
+
+ ;; Looks `c-hanging-braces-alist' doesn't work as expected when `electric-pair-mode' is enabled.
+ (electric-pair-mode 1)
+
+ (defconst sd/c-style
+ '("stroustrup"
+ (c-tab-always-indent . t)
+ (c-basic-offset . 4)
+ (c-hanging-braces-alist . ((substatement-open before)
+ (statement-block-intro before)))
+ (c-hanging-colons-alist . ())
+ (c-echo-syntactic-information-p . nil)
+ (c-hanging-semi&comma-criteria . ()))
+ "My C programming style")
+
+ (c-add-style "sd-c-style" sd/c-style)
+
+ (setq c-default-style "sd-c-style")
+
+ (add-hook 'c-mode-hook '(lambda ()
+ (c-toggle-auto-newline 1)
+ (c-set-style "sd-c-style")))
+
+ ;; (advice-add 'c-electric-brace :after (lambda (arg) "Add new line after insert {}" (newline-and-indent)))
+#+END_SRC
+*** irony
+**** install irony server
+Install clang, on mac, it has =libclang.dylib=, but no develop headers. Install by =brew=
+#+BEGIN_SRC sh
+ brew install llvm --with-clang
+#+END_SRC
+
+then install irony searver, and =LIBCLANG_LIBRARY= and =LIBCLANG_INCLUDE_DIR= accordingly
+#+BEGIN_SRC emacs-lisp :tangle no :results silent
+ (irony-install-server)
+#+END_SRC
+
+#+BEGIN_SRC sh
+ cmake -DLIBCLANG_LIBRARY\=/usr/local/Cellar/llvm/4.0.1/lib/libclang.dylib \
+ -DLIBCLANG_INCLUDE_DIR=/usr/local/Cellar/llvm/4.0.1/include \
+ -DCMAKE_INSTALL_PREFIX\=/Users/peng/.emacs.d/irony/ \
+ /Users/peng/.emacs.d/elpa/irony-20160713.1245/server && cmake --build . --use-stderr --config Release --target install
+#+END_SRC
+
+**** irony config
+irony-mode-hook, copied from [[https://github.com/Sarcasm/irony-mode][irony-mode]] github
+#+BEGIN_SRC emacs-lisp :tangle yes :results silent
+ ;; (use-package irony
+ ;; :ensure t
+ ;; :init
+ ;; (add-hook 'c++-mode-hook 'irony-mode)
+ ;; (add-hook 'c-mode-hook 'irony-mode)
+ ;; (add-hook 'objc-mode-hook 'irony-mode))
+
+ ;; replace the `completion-at-point' and `complete-symbol' bindings in
+ ;; irony-mode's buffers by irony-mode's function
+
+ (defun my-irony-mode-hook ()
+ (define-key irony-mode-map [remap completion-at-point]
+ 'irony-completion-at-point-async)
+ (define-key irony-mode-map [remap complete-symbol]
+ 'irony-completion-at-point-async))
+
+ ;; (add-hook 'irony-mode-hook 'my-irony-mode-hook)
+ ;; (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
+
+ ;; (add-hook 'c++-mode-local-vars-hook #'sd/c++-mode-local-vars)
+
+ ;; add C++ completions, because by default c++ file can not complete
+ ;; c++ std functions, another method is create .dir-local.el file, for p
+ ;; for project see irony
+ (defun sd/c++-mode-local-vars ()
+ (setq irony--compile-options
+ '("-std=c++11"
+ "-stdlib=libc++"
+ "-I/usr/include/c++/4.2.1")))