#+TITLE: A literate emacs configuration

 * Packages
 ** Set up package archives: live on the bleeding edge.
 #+BEGIN_SRC emacs-lisp
   (require 'package)
   (setq package-archives '(("melpa" . "http://melpa.milkbox.net/packages/")
                            ("elpa" . "http://elpa.gnu.org/packages/")
                            ("org" . "http://orgmode.org/elpa/")))
   (package-initialize)
 #+END_SRC
 ** TODO Use use-package
 List out all packages and install them if they're available.

 * Customization
 ** Move the file out of init.el
 #+BEGIN_SRC emacs-lisp
   (setq custom-file "~/.emacs.d/custom.el")
   (load custom-file 'noerror)
 #+END_SRC

 * UI
 ** Minimal UI
 Do this first to try to get the flicker in the gui out of the way quickly
 #+BEGIN_SRC emacs-lisp
   (tool-bar-mode -1)
   (menu-bar-mode -1)
   (if (boundp 'fringe-mode)
   (fringe-mode -1))
   (if (boundp 'scroll-bar-mode)
       (scroll-bar-mode -1))
 #+END_SRC
 ** Mouse support
 #+BEGIN_SRC emacs-lisp
   (xterm-mouse-mode)
 #+END_SRC
 ** Improve theme loading; from reddit
 #+BEGIN_SRC emacs-lisp
   (defadvice load-theme (before clear-previous-themes activate)
     "Clear existing theme settings instead of layering them"
     (mapc #'disable-theme custom-enabled-themes))
 #+END_SRC
 ** And a font such that variable-pitch is neutralized
 #+BEGIN_SRC emacs-lisp
 (mapc
  (lambda (face)
    (set-face-attribute
     face
     nil
     :family "Fira Code"
     :height 130
     :width 'normal
     :weight 'normal))
  '(default variable-pitch))
 #+END_SRC
 ** And a minimal startup
 #+BEGIN_SRC emacs-lisp
   (setq inhibit-startup-message t)
   (setq inhibit-splash-screen t)
   (setq initial-scratch-message nil)
 #+END_SRC
 ** Disable the bell
 #+BEGIN_SRC emacs-lisp
   (setq ring-bell-function 'ignore)
 #+END_SRC
 ** Buffer Switching
 #+BEGIN_SRC emacs-lisp
   (winner-mode t)
 #+END_SRC
 ** Linum mode
 *** Activate linum mode with spaces
 Mechanism to set whitespace from the readme at Spacegray theme.
 #+BEGIN_SRC emacs-lisp
 (global-linum-mode 1)
 (defadvice linum-update-window (around linum-dynamic activate)
   (let* ((w (length (number-to-string
                      (count-lines (point-min) (point-max)))))
          (linum-format (concat " %" (number-to-string w) "d ")))
     ad-do-it))
 #+END_SRC

 *** Highlight the current line subtly
 #+BEGIN_SRC emacs-lisp
 (hlinum-activate)
 (set-face-attribute
  'linum-highlight-face
  nil
  :foreground "#FF0000"
  :background (face-attribute 'linum :background))
 #+END_SRC

 ** Fill column
 #+BEGIN_SRC emacs-lisp
 (setq fci-rule-column 80)
 (setq fci-rule-color (face-attribute 'linum :foreground))
 #+END_SRC
 ** Scrolling more naturally
 #+BEGIN_SRC emacs-lisp
 (setq scroll-conservatively 1000)
 #+END_SRC

 ** Compilation window output
 #+BEGIN_SRC emacs-lisp
 (setq compilation-window-height 15)
 #+END_SRC
 ** Truncate lines by default
 #+BEGIN_SRC emacs-lisp
 (toggle-truncate-lines 1)
 #+END_SRC
 * Evil
 ** I can't type without vim bindings anymore.
 #+BEGIN_SRC emacs-lisp
   (evil-mode t)
 #+END_SRC
 ** Allow some common typos
 #+BEGIN_SRC emacs-lisp
   (evil-ex-define-cmd "W[rite]" 'save-buffer)
   (evil-ex-define-cmd "V[split]" 'evil-window-vsplit)
 #+END_SRC

 * Org
 ** Babel
 *** Better source code window editing
 #+BEGIN_SRC emacs-lisp
   (setq org-src-window-setup 'other-window)
 #+END_SRC
 *** Highlight and indent source code blocks
 #+BEGIN_SRC emacs-lisp
 (setq org-src-fontify-natively t)
 (setq org-src-tab-acts-natively t)
 (setq org-edit-src-content-indentation 0)
 #+END_SRC

 *** Enable languages
 #+BEGIN_SRC emacs-lisp
 (add-to-list 'org-babel-load-languages '(emacs-lisp . t))
 (add-to-list 'org-babel-load-languages '(dot . t))
 (add-to-list 'org-babel-load-languages '(ditaa . t))
 (add-to-list 'org-babel-load-languages '(ipython . t))
 (add-to-list 'org-babel-load-languages '(python . t))
 (add-to-list 'org-babel-load-languages '(C . t))
 ; (org-babel-do-load-languages 'org-babel-load-languages '())
 #+END_SRC
 *** Prevent confirmation
 #+BEGIN_SRC emacs-lisp
   (setq org-confirm-babel-evaluate nil)
 #+END_SRC
 *** Use Web mode for HTML
 #+BEGIN_SRC emacs-lisp
   (add-to-list 'org-src-lang-modes
                '("html" . web))
 #+END_SRC
 ** UI
 *** Hide markers
 #+BEGIN_SRC emacs-lisp
   (setq org-hide-emphasis-markers t)
 #+END_SRC
 *** Clean bullets
 #+BEGIN_SRC emacs-lisp
   (add-hook 'org-mode-hook
             (lambda ()
               (org-indent-mode t)))
 #+END_SRC
 *** Display images
 #+BEGIN_SRC emacs-lisp
   (setq org-startup-with-inline-images t)
   (add-hook
    'org-babel-after-execute-hook
    (lambda ()
      (when org-inline-image-overlays
        (org-redisplay-inline-images))))
 #+END_SRC
 *** Enable auto-fill mode
 #+BEGIN_SRC emacs-lisp
   (add-hook
    'org-mode-hook
    (lambda ()
      (auto-fill-mode)))
 #+END_SRC
 ** Combined with evil
 #+BEGIN_SRC emacs-lisp
   (evil-define-key 'normal org-mode-map (kbd "TAB") 'org-cycle)
 #+END_SRC
 ** Bugfixes
 #+BEGIN_SRC emacs-lisp
   (defun org-font-lock-ensure ()
     (font-lock-fontify-buffer))
 #+END_SRC
 ** Expert tagging
 (Doesn't show the tag window till an extra C-c.)
 #+BEGIN_SRC emacs-lisp
 (setq org-fast-tag-selection-single-key 'expert)
 #+END_SRC
 ** Tag clicks show sparse tree instead of agenda view
 #+BEGIN_SRC emacs-lisp
   (defun tag-at-point-in-heading ()
     "Returns the tag at the current point in the string"
     (let ((str (buffer-string))
           (begin (point))
           (end (point)))
       (while (not (equal (aref str begin) ?:))
         (setq begin (- begin 1)))
       (while (not (equal (aref str end) ?:))
         (setq end (+ end 1)))
       (substring str (+ 1 begin) end)))

   (defun open-sparse-view ()
     "Shows a sparse tree on clicking a tag instead of org-tags-view"
     ;; From org-open-at-point, sanity checking that we're on a headline with tags
     (when (and (org-element-lineage (org-element-context)
                                     '(headline inlinetask)
                                     t)
                (progn (save-excursion (beginning-of-line)
                                       (looking-at org-complex-heading-regexp))
                       (and (match-beginning 5)
                            (> (point) (match-beginning 5)))))
       (org-match-sparse-tree nil (concat "+" (tag-at-point-in-heading)))
       't))

   (add-hook 'org-open-at-point-functions
             'open-sparse-view)
 #+END_SRC
 ** Add support for not exporting headlines
 #+BEGIN_SRC emacs-lisp
   (require 'ox-extra) ; from org-plus-contrib
   (ox-extras-activate '(ignore-headlines))
 #+END_SRC
 ** Add support for publishing 'web' src as is
 #+BEGIN_SRC emacs-lisp
   (defun org-babel-execute:web (body params)
     body)
 #+END_SRC
 * Emamux
 ** Customization
 #+BEGIN_SRC emacs-lisp
   (setq emamux:use-nearest-pane t)
 #+END_SRC
 ** Some useful shortcuts
 #+BEGIN_SRC emacs-lisp
   (define-key evil-normal-state-map (kbd "C-c r") 'emamux:run-last-command)
   (define-key evil-normal-state-map (kbd "C-c x") 'emamux:run-command)
   (define-key evil-normal-state-map (kbd "C-c i") 'emamux:inspect-runner)
 #+END_SRC
 * Compiling
 ** Keyboard shortcut
 #+BEGIN_SRC emacs-lisp
 (define-key evil-normal-state-map (kbd "C-c c") 'recompile)
 #+END_SRC
 * Man Pages
 #+BEGIN_SRC emacs-lisp
 (setq Man-notify-method 'pushy)

 #+END_SRC
 * Editing
 ** Indentation
 #+BEGIN_SRC emacs-lisp
   (setq c-basic-offset 2)
   (setq tab-width 2)
   (setq-default indent-tabs-mode nil)
 #+END_SRC
 ** Backups & autosaves
 #+BEGIN_SRC emacs-lisp
   (setq auto-save-default nil)
   (setq backup-directory-alist
         `((".*" . ,temporary-file-directory)))
   (setq auto-save-file-name-transforms
         `((".*" ,temporary-file-directory t)))
 #+END_SRC
 ** Better braces
 *** Smartparens
 #+BEGIN_SRC emacs-lisp
   (require 'smartparens-config)
   (add-hook 'prog-mode-hook 'turn-on-smartparens-mode)
   (define-key smartparens-mode-map (kbd "M-f") 'sp-forward-slurp-sexp)
   (define-key smartparens-mode-map (kbd "M-b") 'sp-backward-slurp-sexp)
   (define-key smartparens-mode-map (kbd "M-F") 'sp-forward-barf-sexp)
   (define-key smartparens-mode-map (kbd "M-B") 'sp-backward-barf-sexp)
   (define-key smartparens-mode-map (kbd "M-s") 'sp-splice-sexp)
   (define-key smartparens-mode-map (kbd "C-k") 'sp-kill-sexp)
 #+END_SRC
 *** Highlight parenthesis
 #+BEGIN_SRC emacs-lisp
   (show-paren-mode t)
 #+END_SRC
 ** Whitespace
 #+BEGIN_SRC emacs-lisp
   (add-hook 'before-save-hook 'whitespace-cleanup)
   (setq require-final-newline t)
 #+END_SRC

 * Menus
 ** IDO
 #+BEGIN_SRC emacs-lisp
   (setq ido-enable-flex-matching t)
   (setq ido-everywhere t)
   (ido-mode t)
 #+END_SRC
 ** Smex
 #+BEGIN_SRC emacs-lisp
   (global-set-key (kbd "M-x") 'smex)
   (global-set-key (kbd "M-X") 'smex-major-mode-commands)
   (global-set-key (kbd "C-c M-x") 'execute-extended-command)
 #+END_SRC

 * Language/Project specific
 ** BUCK
 *** Trigger python mode
 #+BEGIN_SRC emacs-lisp
   (add-to-list 'auto-mode-alist '(".*/BUCK$" . python-mode))
 #+END_SRC
 ** Scheme
 *** Set up chicken scheme
 #+BEGIN_SRC emacs-lisp
   (setq scheme-program-name "/usr/local/bin/csi -:c")
 #+END_SRC
 ** Web Mode
 #+BEGIN_SRC emacs-lisp
   (setq web-mode-markup-indent-offset 2)
   (setq web-mode-css-indent-offset 2)
   (setq web-mode-code-indent-offset 2)

   (setq web-mode-style-padding 2)
   (setq web-mode-script-padding 2)

   (setq web-mode-auto-quote-style 2) ; use single quotes
 #+END_SRC

 ** Rust
 #+BEGIN_SRC emacs-lisp
 (add-hook 'rust-mode-hook #'racer-mode)
 (add-hook 'rust-mode-hook
           (lambda ()
            (define-key rust-mode-map (kbd "TAB") #'company-indent-or-complete-common)))
 (add-hook 'racer-mode-hook #'eldoc-mode)
 (add-hook 'flycheck-mode-hook #'flycheck-rust-setup)
 #+END_SRC

 * Version Control
 ** Disable by default
 #+BEGIN_SRC emacs-lisp
   (setq vc-handled-backends ())
 #+END_SRC
 ** Customize Monky, for when it's loaded
 *** Use command server for speed
 #+BEGIN_SRC emacs-lisp
   (setq monky-process-type 'cmdserver)
 #+END_SRC
 *** And add support for a nicer log file
 #+BEGIN_SRC emacs-lisp
   (defun hg-file-history ()
     (interactive)
     (require 'monky)
     (monky-run-hg-async
      "log"
      "--template"
      "\n{rev}) {date|shortdate}/{author|user}\n{desc|fill68}\n↘\n"
      buffer-file-name))
 #+END_SRC

 * Utilities
 ** Current file name
 #+BEGIN_SRC emacs-lisp
   (defun path ()
     (interactive)
     (message (buffer-file-name)))
 #+END_SRC

 * GDB
 ** Show all the windows on start
 #+BEGIN_SRC emacs-lisp
   (setq gdb-many-windows 't)
 #+END_SRC
 * Neotree
 ** Simple theme
 #+BEGIN_SRC emacs-lisp
 (setq neo-theme 'ascii)
 #+END_SRC
 * Dired
 ** Hide permissions and owners to make file lists less noisy (from Xah Lee's blog)
 #+BEGIN_SRC emacs-lisp
   (add-hook 'dired-mode-hook
             (lambda ()
               (dired-hide-details-mode 1)))
 #+END_SRC
 ** Disable ls by default in dired
 #+BEGIN_SRC emacs-lisp
    (setq dired-use-ls-dired nil)
 #+END_SRC

* Browsing
** Default to mac
#+BEGIN_SRC emacs-lisp
  (setq browse-url-browser-function 'browse-url-default-macosx-browser)
#+END_SRC
** Enable cookies
#+BEGIN_SRC emacs-lisp
  (setq w3m-use-cookies t)
#+END_SRC

* Auto completion
#+BEGIN_SRC emacs-lisp
(add-hook 'prog-mode-hook 'company-mode)
(setq company-tooltip-align-annotations t)
#+END_SRC

* Buffer Management
From StackOverflow
#+BEGIN_SRC emacs-lisp
(defun close-all-buffers ()
  (interactive)
  (mapc 'kill-buffer (buffer-list)))
#+END_SRC
** Reload files
#+BEGIN_SRC emacs-lisp
  (defun revert-all-buffers ()
    (interactive)
    (dolist (buf (buffer-list))
      (with-current-buffer buf
        (when (buffer-file-name)
          (revert-buffer t t t)))))
#+END_SRC