< expLog

A literate emacs configuration

Packages

Set up package archives: live on the bleeding edge.

(require 'package)
(setq package-archives
      '(("melpa" . "http://melpa.org/packages/")
        ("elpa" . "http://elpa.gnu.org/packages/")
        ("org" . "http://orgmode.org/elpa/")))

Customization

With some text for comparison

Move the file out of init.el

(setq custom-file "~/.emacs.d/custom.el")
(load custom-file 'noerror)

UI

Minimal UI

Do this first to try to get the flicker in the gui out of the way quickly

(tool-bar-mode -1)
(menu-bar-mode -1)
(if (boundp 'fringe-mode)
(fringe-mode -1))
(if (boundp 'scroll-bar-mode)
    (scroll-bar-mode -1))

Margins

(setq-default left-margin-width 1 right-margin-width 1)

Mouse support

(xterm-mouse-mode)
(setq mouse-wheel-progressive-speed nil)
(setq focus-follows-mouse "auto-raise")
(setq mouse-autoselect-window 't)

Improve theme loading; from reddit

(defadvice load-theme (before clear-previous-themes activate)
  "Clear existing theme settings instead of layering them"
  (mapc #'disable-theme custom-enabled-themes))

And a minimal startup

(setq inhibit-startup-message t)
(setq inhibit-splash-screen t)
(setq initial-scratch-message nil)

Disable the bell

(setq ring-bell-function 'ignore)

Buffer Switching

(winner-mode t)

Display line numbers mode

Activate on programming modes

(add-hook 'prog-mode-hook
          'display-line-numbers-mode)

Fill column

(setq fci-rule-column 80)

Scrolling more naturally

(pixel-scroll-mode)

Compilation window output

(setq compilation-window-height 15)

Truncate lines by in code

(setq-default truncate-lines t)

Sweet Title Bar for Mac OS

(add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
(add-to-list 'default-frame-alist '(ns-appearance . light))

Evil

I can't type without vim bindings anymore.

(evil-mode t)

Allow some common typos

(evil-ex-define-cmd "W[rite]" 'save-buffer)
(evil-ex-define-cmd "V[split]" 'evil-window-vsplit)

Org

Babel

Better source code window editing

(setq org-src-window-setup 'other-window)

Highlight and indent source code blocks

(setq org-src-fontify-natively t)
(setq org-src-tab-acts-natively t)
(setq org-edit-src-content-indentation 0)

Highlight quotes

(setq org-fontify-quote-and-verse-blocks t)

Enable languages

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (dot . t)
   (ditaa . t)
   (python . t)
   (C . t)
   (rust . t)
   (shell . t)))

Prevent confirmation

(setq org-confirm-babel-evaluate nil)

Use Web mode for HTML

(add-to-list 'org-src-lang-modes
             '("html" . web))

UI

Hide markers

(setq org-hide-emphasis-markers t)

Clean bullets

(setq org-bullets-bullet-list '("."))
(add-hook 'org-mode-hook
          (lambda ()
            (org-bullets-mode 1)))

Display images

(setq org-startup-with-inline-images t)
(add-hook
 'org-babel-after-execute-hook
 (lambda ()
   (when org-inline-image-overlays
     (org-redisplay-inline-images))))

Enable auto-fill mode

(add-hook
 'org-mode-hook
 (lambda ()
   (auto-fill-mode)))

Combined with evil

(evil-define-key 'normal org-mode-map (kbd "TAB") 'org-cycle)

Bugfixes

(defun org-font-lock-ensure ()
  (font-lock-fontify-buffer))

Expert tagging

(Doesn't show the tag window till an extra C-c.)

(setq org-fast-tag-selection-single-key 'expert)

Tag clicks show sparse tree instead of agenda view

(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)

Add support for not exporting headlines

(require 'ox-extra) ; from org-plus-contrib
(ox-extras-activate '(ignore-headlines))

Add support for publishing 'web' src as is

(defun org-babel-execute:web (body params)
  body)

Emamux

Customization

;(setq emamux:use-nearest-pane t)

Some useful shortcuts

;(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)

Compiling

Keyboard shortcut

(define-key evil-normal-state-map (kbd "C-c c") 'recompile)

Man Pages

(setq Man-notify-method 'pushy)

Editing

Indentation

(setq c-basic-offset 2)
(setq tab-width 2)
(setq-default indent-tabs-mode nil)

Backups & autosaves

(setq auto-save-default nil)
(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))

Better braces

Smartparens

(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)

Highlight parenthesis

(show-paren-mode t)

Whitespace

(add-hook 'before-save-hook 'whitespace-cleanup)
(setq require-final-newline t)

Menus

Ivy

(ivy-mode 1)
(counsel-mode 1)
; (setq ivy-posframe-display-functions-alist
;       '((t . ivy-posframe-display-at-frame-center)))
; (ivy-posframe-mode 1)

Language/Project specific

BUCK

Trigger python mode

(add-to-list 'auto-mode-alist '(".*/BUCK$" . python-mode))

Scheme

Set up chicken scheme

(setq scheme-program-name "/usr/local/bin/csi -:c")

Web Mode

(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

Rust

; (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)

Version Control

Disable by default

(setq vc-handled-backends ())

Customize Monky, for when it's loaded

Use command server for speed

(setq monky-process-type 'cmdserver)

And add support for a nicer log file

(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))

Utilities

Current file name

(defun path ()
  (interactive)
  (message (buffer-file-name)))

GDB

Show all the windows on start

(setq gdb-many-windows 't)

Neotree

Simple theme

(setq neo-theme 'ascii)

Dired

Hide permissions and owners to make file lists less noisy (from Xah Lee's blog)

(add-hook 'dired-mode-hook
          (lambda ()
            (dired-hide-details-mode 1)))

Disable ls by default in dired

(setq dired-use-ls-dired nil)

Browsing

Enable cookies

(setq w3m-use-cookies t)

Auto completion

(add-hook 'prog-mode-hook 'company-mode)
(setq company-tooltip-align-annotations t)

Buffer Management

Close buffers

From StackOverflow

(defun close-all-buffers ()
  (interactive)
  (mapc 'kill-buffer (buffer-list)))

Reload files

(defun revert-all-buffers ()
  (interactive)
  (dolist (buf (buffer-list))
    (with-current-buffer buf
      (when (buffer-file-name)
        (revert-buffer t t t)))))

Desaturate

(defun desaturate-color (color-hex)
  "Converts a color string to its desaturated equivalent hex string"
  (require 'color)
  (apply
   'color-rgb-to-hex
   (append (apply
            'color-hsl-to-rgb
            (apply
             'color-desaturate-hsl
             `(,@(apply 'color-rgb-to-hsl (color-name-to-rgb color-hex)) 100)))
           '(2))))

(defun transform-theme-colors (fn)
  "Apply FN to the colors on every active face.

   FN should accept the face symbol and the current color,
   and return the new color to be applied."
  (interactive)
  (mapc
   (lambda (face)
     (mapc
      (lambda (attr)
        (let ((current (face-attribute face attr)))
          (unless (or (not current)
                      (listp current)
                      (string= current "unspecified")
                      (string= current "t"))
            (set-face-attribute face nil attr (funcall fn face current)))))
      '(:foreground :background :underline :overline :box :strike-through
                    :distant-foreground))
     (mapc
      (lambda (complex-attr)
        (let* ((full (copy-tree (face-attribute face complex-attr)))
               (current (if (listp full) (member :color full))))
          (unless (or (not current)
                      (not (listp full)))
            (setcar (cdr current) (funcall fn face (cadr current)))
            (set-face-attribute face nil complex-attr full))))
      '(:underline :overline :box)))
   (face-list)))

(defun desaturate-theme ()
  "As title: desaturate all currently active face colorsj."
  (interactive)
  (transform-theme-colors
   (lambda (face color)
     (desaturate-color color))))

(defun invert-theme ()
  "Take the complement of all currently active colors."
  (interactive)
  (require 'color)
  (transform-theme-colors
   (lambda (face color)
     (apply
      'color-rgb-to-hex
      (color-complement color))))
  (let ((current-ns-appearance (assoc 'ns-appearance default-frame-alist)))
    (cond ((eq (cdr current-ns-appearance) 'light)
           (setf (cdr current-ns-appearance) 'dark))
          ((eq (cdr current-ns-appearance) 'dark)
           (setf (cdr current-ns-appearance) 'light)))))

Mode Line

(setq mode-line-format
              (list
               "%& %b%n"
               " ~ "
               "%m"
               " ~ "
               "%l:%c"))

Speed

(setq-default xterm-query-timeout nil)

LSP

; (setq lsp-ui-doc-max-width 200)
(setq gc-cons-threshold 1000000000)
(setq read-process-output-max (* 1024 1024))
(setq lsp-idle-delay .1)

Markdown

(setq markdown-hide-urls t)
(setq markdown-hide-markup t)

Deft & ZettelDeft

(setq deft-extensions '("txt" "tex" "org"))
(setq deft-directory "/home/kunalb/Dropbox (Facebook)/fbslipbox/")
(setq deft-default-extension ".org")

Javascript

(setq js-indent-level 2)

Journal

(customize-set-variable 'org-journal-dir "/home/kunalb/log")
(customize-set-variable 'org-journal-file-type 'yearly)
(require 'org-journal)