emacs.d

My emacs configuration, done in a literate programming fashion using org-mode

git clone git://git.shimmy1996.com/emacs.d.git
commit 9fe01d64ac6546ad28745e7fe39d90fb55668432
parent c6d09773620402d2ec29de66022fd897af495a48
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sun, 24 Dec 2017 11:29:11 -0600

Added settings for yasnippet and company. Misc fixes to flycheck, whitespace and backups.

Diffstat:
M.gitignore | 5+++++
MREADME.org | 227++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Asnippets/latex-mode/align | 8++++++++
Asnippets/latex-mode/figure | 12++++++++++++
Asnippets/latex-mode/title | 10++++++++++
5 files changed, 236 insertions(+), 26 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -9,3 +9,7 @@
 # emacs init
 !init.el
 
+# yasnippets
+!snippets
+!snippets/*
+!snippets/*/*+
\ No newline at end of file
diff --git a/README.org b/README.org
@@ -8,16 +8,19 @@ After installation, you may need to run =M-x list-packages= and reload =init.el=
 
 ** Module Descriptions
 Here's a short description for each module.
-| Module         | Notes                 |
-|----------------+-----------------------|
-| [[Packages]]   | Package management.   |
-| [[Aesthetics]] | UI and theme configs. |
-| [[Org-mode]]   | Org-mode settings.    |
-| [[Helm]]       | Autocompletion.       |
-| [[Auctex]]     | Latex settings.       |
-| [[Ox-hugo]]    | =hugo= exporter.      |
-| [[Magit]]      | Git interface.        |
-| [[CC]]         | CC mode.              |
+| Module             | Notes                       |
+|--------------------+-----------------------------|
+| [[Packages]]           | Package management.         |
+| [[Aesthetics]]         | UI and theme.               |
+| [[Org-mode]]           | Org-mode settings.          |
+| [[Helm]]               | Minibuffer auto-completion. |
+| [[Auctex]]             | Latex settings.             |
+| [[Ox-hugo]]            | =hugo= exporter.            |
+| [[Magit]]              | Git interface.              |
+| [[CC]]                 | CC mode.                    |
+| [[Flycheck]]           | Syntax checking.            |
+| [[Yasnippet]]          | Snippet templates.          |
+| [[Company]]            | Buffer auto-completion.     |
 
 * Packages
 Manage my package settings.
@@ -37,7 +40,8 @@ Initialize Emacs Package Manager and add repositories (only melpa for now).
 #+END_SRC
 
 ** Base packages to install
-List the base packages I would like to install here. Mostly just the core packages for each subtree.
+List the base packages I would like to install here. Mostly just the core
+packages for each subtree.
 
 #+BEGIN_SRC emacs-lisp
   (setq my-package-list '(auctex
@@ -54,6 +58,17 @@ Makes sure all packages listed above are installed.
   (mapc #'package-install my-package-list)
 #+END_SRC
 
+** Setup =use-package=
+Setup =use-package= so that package requirements can be handled in separate
+sections.
+
+#+BEGIN_SRC emacs-lisp
+  ;; Bootstrap `use-package'
+  (unless (package-installed-p 'use-package)
+    (package-refresh-contents)
+    (package-install 'use-package))
+#+END_SRC
+
 * Aesthetics
 Mostly aesthetics and quality of life changes.
 
@@ -64,8 +79,8 @@ Solarized light or monokai.
   (load-theme 'base16-monokai t)
 #+END_SRC
 
-** Customize Settings
-Save customized settings in a seperate file than =init.el=. Create the customization file if none found.
+** Customize Settings in Separate File
+Save customized settings in a separate file than =init.el=. Create the customization file if none found.
 #+BEGIN_SRC emacs-lisp
   (setq custom-file (expand-file-name "custom.el" user-emacs-directory))
   (unless (file-exists-p custom-file)
@@ -84,11 +99,11 @@ Install fcitx support.
 #+END_SRC
 
 ** Highlight Lines Over 80
-Highlight portions of line over 80 characters.
+Highlight portions of line over 80 characters in =prog-mode=.
 #+BEGIN_SRC emacs-lisp
- (require 'whitespace)
- (setq whitespace-style '(face empty tabs lines-tail trailing))
- (global-whitespace-mode t)
+  (require 'whitespace)
+  (setq whitespace-style '(face empty tabs lines-tail trailing))
+  (add-hook 'prog-mode-hook 'whitespace-mode)
 #+END_SRC
 
 ** Line Highlighting
@@ -142,12 +157,30 @@ Highlight matching parenthesis.
   (show-paren-mode t)
 #+END_SRC
 
+** Rainbow Delimiters
+=rainbow-delimiters= is a "rainbow parentheses"-like mode which highlights delimiters such as parentheses, brackets or braces according to their depth.
+
+Install =rainbow-delimiters= and enable it for =prog-mode=.
+#+BEGIN_SRC emacs-lisp
+  (use-package rainbow-delimiters
+    :ensure t
+    :init (add-hook 'prog-mode-hook #'rainbow-delimiters-mode))
+#+END_SRC
 ** Remove Trailing Whitespace
 Remove trailing whitespace upon saving.
 #+BEGIN_SRC emacs-lisp
   (add-hook 'before-save-hook 'delete-trailing-whitespace)
 #+END_SRC
 
+** Save Backups Elsewhere
+Save =*~= backups in =$(pwd)/.bak=.
+
+#+BEGIN_SRC emacs-lisp
+  (setq backup-directory-alist
+        '(("." . ".bak"))
+        )
+#+END_SRC
+
 ** UI Settings
 Hide menu, scrollbar and toolbars.
 
@@ -176,15 +209,6 @@ Use Source Code Pro as the default font.
   (setq default-frame-alist '((font . "Source Code Pro-12")))
 #+END_SRC
 
-** Save Backups Elsewhere
-Save =*~= backups in =$(pwd)/.bak=.
-
-#+BEGIN_SRC emacs-lisp
-  (setq backup-directory-alist
-        '(("." . ".bak"))
-        )
-#+END_SRC
-
 * Org-mode
 Mostly formatting settings in =org-mode=.
 
@@ -394,3 +418,154 @@ Set default indention level to 4 and style to "linux"(do not indent braces).
   (setq-default c-default-style "linux"
                 c-basic-offset 4)
 #+END_SRC
+
+* Flycheck
+Flycheck is a modern on-the-fly syntax checking extension for GNU
+Emacs, intended as replacement for the older Flymake extension which
+is part of GNU Emacs.
+
+** Installation
+Install =flycheck=.
+#+BEGIN_SRC emacs-lisp
+  (use-package flycheck
+    :ensure t
+    :init (global-flycheck-mode))
+#+END_SRC
+
+** Set C++ Standard Library
+Use =c++14= as the C++ standard.
+#+BEGIN_SRC emacs-lisp
+  (add-hook 'c++-mode-hook
+            (lambda () ((setq flycheck-clang-language-standard "c++14")
+                        (setq flycheck-gcc-language-standard "c++14"))))
+#+END_SRC
+
+* Yasnippet
+YASnippet is a template system for Emacs. It allows you to type an
+abbreviation and automatically expand it into function templates.
+
+** Installation
+Install =yasnippet=. Load =yasnippet= when =yas-minor-mode= is called
+and add the hook for =yas-minor-mode= for programming modes. Reload
+the snippets on start up.
+
+#+BEGIN_SRC emacs-lisp
+  (require 'cl)
+  (use-package yasnippet
+    :ensure t
+    :commands (yas-minor-mode)
+    :init (yas-global-mode)
+    :config (yas-reload-all))
+#+END_SRC
+
+** Install =yasnippet-snippets=
+=yasnippet-snippets= is a collection of snippets for many langulages.
+
+#+BEGIN_SRC emacs-lisp
+  (use-package yasnippet-snippets
+    :ensure t)
+#+END_SRC
+
+* Company
+Auto completion of everything with nice backends.
+
+** Installation
+Install =company= and enable it globally.
+#+BEGIN_SRC emacs-lisp
+  (use-package company
+    :ensure t
+    :init (global-company-mode))
+#+END_SRC
+
+** Zero Delay
+No delay for auto-completion.
+#+BEGIN_SRC emacs-lisp
+  (setq company-idle-delay 0)
+  (setq company-minimum-prefix-length 1)
+#+END_SRC
+
+** =yasnippet= Conflict
+Pressing tab with company mode conflicts with =yasnippets=, this is
+the only fix that I found that makes everything work as expected.
+
+#+BEGIN_SRC emacs-lisp
+  (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)
+    (if (minibufferp)
+        (minibuffer-complete)
+      (if (or (not yas/minor-mode)
+              (null (do-yas-expand)))
+          (if (check-expansion)
+              (company-complete-common)
+            (indent-for-tab-command)))))
+
+
+  (global-set-key [tab] 'tab-indent-or-complete)
+#+END_SRC
+
+** Removing =company-semantic= backend
+So, =cedet= has this thing called =semantic=, which sounds great and
+seems really clever, but it's slow. Usually using a backend specific
+for the language you're using provides more satisfying results.
+
+#+BEGIN_SRC emacs-lisp
+  (with-eval-after-load 'company
+    '(setq company-backends (delete 'company-semantic company-backends)))
+#+END_SRC
+** =company-math= Backends
+Install =company-math= and add it to =company-backends=.
+#+BEGIN_SRC emacs-lisp
+  (use-package company-math
+    :ensure t
+    :init (append '((company-math-symbols-latex
+                     company-math-symbols-unicode
+                     company-latex-commands))
+                  company-backends))
+#+END_SRC
+** Tooltip Documentation
+Install dependency =pos-tip=.
+#+BEGIN_SRC emacs-lisp
+  (use-package pos-tip
+    :ensure t)
+  (require 'pos-tip)
+#+END_SRC
+
+Install =company-quickhelp= and set delay, FG/BG colors, max lines.
+#+BEGIN_SRC emacs-lisp
+  (use-package company-quickhelp
+    :ensure t
+    :init (company-quickhelp-mode 1))
+  (setq company-quickhelp-delay 0)
+  (setq company-quickhelp-color-background "#272822")
+  (setq company-quickhelp-color-foreground "#F8F8F2")
+  (setq company-quickhelp-max-lines 20)
+  (setq company-quickhelp-use-propertized-text 1)
+#+END_SRC
+** =yasnippet= Key Completion
+Add =company-yasnippet= backend.
+#+BEGIN_SRC emacs-lisp
+  ;; 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
diff --git a/snippets/latex-mode/align b/snippets/latex-mode/align
@@ -0,0 +1,7 @@
+# -*- mode: snippet -*-
+# name: align
+# key: align
+# --
+\begin{align${1:*}}
+$0
+\end{align$1}+
\ No newline at end of file
diff --git a/snippets/latex-mode/figure b/snippets/latex-mode/figure
@@ -0,0 +1,11 @@
+# -*- mode: snippet -*-
+# name: figure
+# key: figure
+# --
+\begin{figure}[htbp!]
+  \centering
+    \includegraphics[width=${1:0.6}\textwidth]{${2:img}}
+    \caption{${3:caption}}
+\end{figure}
+\FloatBarrier
+$0+
\ No newline at end of file
diff --git a/snippets/latex-mode/title b/snippets/latex-mode/title
@@ -0,0 +1,9 @@
+# -*- mode: snippet -*-
+# name: title
+# key: title
+# --
+\title{${1:title}}
+\author{${2:author}}
+\date{${3:\today}}
+\maketitle
+$0+
\ No newline at end of file