dotfiles/emacs/.emacs.d/init.org
Tom Willemse 1979d0fe6a Remove defunct hook
Apparently there is no ‘cider-turn-on-eldoc-mode’ procedure.
2016-12-31 20:04:25 +01:00

31 KiB

Tom-Emacs Interface

This is my personal Emacs configuration. The name was inspired by "Ghost in the Shell 2: Man-Machine Interface and Ryan Rix's "Complete Computing Environment".

To start off, first I need to enable lexical binding.

  ;; -*- lexical-binding: t; -*-

Package configuration

Require package.el since I immediately start using its variables and functions anyway, no need to delay loading.

  (require 'package)

Add the MELPA and org package archives because I like living on the bleeding edge. This should be done both at run-time and compile-time so I can install packages at compile time.

  (eval-and-compile
    (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
    (add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/")))

Add the directory with my mode-specific configuration files to the load path.

  (add-to-list 'load-path (locate-user-emacs-file "init/"))

Add all my vendored packages to the load path.

  (eval-and-compile
    (mapc (lambda (d) (add-to-list 'load-path d))
          (directory-files
           (locate-user-emacs-file "vendor-lisp/") t "^[^.]")))

Initialize package.el so that packages can be loaded and used. This also needs to be done at both run-time and compile-time so packages can be installed at compile-time.

  (eval-and-compile (package-initialize))

Some actions produce a lot of output that is usually uninteresting during compilation. However, this information may be crucial when an error occurs. So for these actions I can use this macro, which stores all sent messages in a temporary buffer and prints them when an error occurs, and hides them when it doesn't.

  (defmacro silently (title &rest body)
    "Only output something when an error occurs.
  Prefix with TITLE any output that occurs while executing BODY,
  but only when an error occurs, otherwise discard it."
    (declare (indent 1))
    (let ((buffer-var (cl-gensym))
          (error-var (cl-gensym)))
     `(with-temp-buffer
        (let ((,buffer-var (current-buffer)))
          (cl-letf (((symbol-function 'message)
                     (lambda (msg &rest args)
                       (with-current-buffer ,buffer-var
                         (insert "  " (apply 'format msg args) "\n")))))
            (condition-case ,error-var
                (progn ,@body)
              (error
               (princ ,(concat title " output:\n"))
               (princ (with-current-buffer ,buffer-var (buffer-string)))
               (princ "Error:\n")
               (princ "  ")
               (princ (cadr ,error-var))
               (princ "\n"))))))))

Refresh the package contents so packages can be installed from all configured archives. Don't do this at run-time because it slows down the process too much.

  (eval-when-compile
    (let* ((not-installed (seq-remove 'package-installed-p
                                      package-selected-packages))
           (available (seq-filter (lambda (p)
                                    (assq p package-archive-contents))
                                  not-installed))
           (difference (- (length not-installed) (length available))))
      (when (> difference 0)
        (silently "Refresh packages"
          (package-refresh-contents)))
      (when available
        (mapc (lambda (p) (package-install p t)) available))))

Site lisp

Setup everything so that any autoloads in site-lisp/ get loaded and can be used.

  (eval-and-compile
    (add-to-list 'load-path (locate-user-emacs-file "site-lisp/"))
    (let ((loaddefs (locate-user-emacs-file "site-lisp/site-autoloads.el")))
      (when (file-exists-p loaddefs)
        (load loaddefs))))

Helper functions

I have some helper functions stored away in a separate file.

  (require 'oni-helpers)

Backups

I don't like having every directory filled with "filename~" files. So instead of saving backup files to the same directory, save them to a special one instead.

  (setq backup-directory-alist `((".*" . ,(oni:data-location "backup-files/"))))

Auto saves

I prefer to keep all autosave files in a single directory so they don't clog up my filesystem so much. Usually these files get deleted, but sometimes they don't, and I don't think they look pretty. Add it to the end of the list because the default value stores auto-saves for remote files in /tmp, which is fine by me.

  (add-to-list 'auto-save-file-name-transforms
               `(".*" ,(oni:data-location "auto-save-files/") t) :append)

Place the files which contain the auto save files in a similar directory.

  (setq auto-save-list-file-prefix (oni:data-location "auto-save-list/.saves-"))

Tabs

Generally I prefer using spaces over tabs. Especially for lisp-like languages.

  (setq-default indent-tabs-mode nil)

A tab-width of 8 is too wide for me, and 2 is too narrow. 4 is just right.

  (setq-default tab-width 4)

Inhibit startup screen

I've been using Emacs long enough not to need the startup screen anymore. I don't see it on my PC where I start Emacs in daemon mode, but on my laptop I always start it normally, so it gets in the way.

  (setq inhibit-startup-screen t)

Font

Set the default font to a more pleasing one, in my opinion, with a better size as well.

  (add-to-list 'default-frame-alist '(font . "Fantasque Sans Mono-15"))

Internal border

For aesthetics I like to have a thick border on the inside of my Emacs window. I have the same border in URxvt, but I haven't found out how to add it to Conkeror yet.

  (add-to-list 'default-frame-alist '(internal-border-width . 15))

Menu bar

I don't use the menu bar, so it just takes up space.

  (menu-bar-mode -1)

Tool bar

I don't use the tool bar, so it just takes up space.

  (tool-bar-mode -1)

Scroll bar

I don't use the scroll bar to either navigate my buffers or see whereabouts I am, so they just take up space.

  (scroll-bar-mode -1)

Cursor

Use a bar cursor instead of a box.

  (setq-default cursor-type '(bar . 2))

Whitespace

I hate it when trailing whitespace is left around a file. I've been using this for years, and apart from having some trouble working with people who don't pay attention to it, it has worked flawlessly.

  (require 'destroy-trailing-whitespace)
  (global-destroy-trailing-whitespace-mode)

Having a final newline at the end of the file is always a good idea. Some programs just don't work without it and others produce some strange results. Github diffs are an example.

  (setq require-final-newline t)

Long lines

By default Emacs wraps long lines around to the next line when they reach the far end of the window. However I prefer to have them truncated instead.

  (setq-default truncate-lines t)

Theme

  (add-to-list 'custom-theme-load-path
               (concat user-emacs-directory "vendor-lisp/yoshi-theme"))
  (load-theme 'yoshi :no-confirm)

Diminish

I really don't need to see some of the minor modes.

  (require 'diminish)

Ivy

Ivy is a completing read implementation that offers choises vertically. I'm surprised how much I like it. I've tried Swiper before and I didn't like that so much.

Since I immediately use and enable Ivy, there's no need to autoload it, so require it to keep the byte-compiler quiet.

  (require 'ivy)

Don't show that ivy is enabled in the mode-line. It's enabled globally and I'll notice it from other things anyway (like it showing up).

  (diminish 'ivy-mode)

Enable fuzzy matching in Ivy.

  (setq ivy-re-builders-alist '((t . ivy--regex-fuzzy))
        ivy-initial-inputs-alist nil)

Enable Ivy.

  (ivy-mode)

Counsel

Counsel is a group of functions that use Ivy to specialize on certain built-in commands, such as M-x.

Since I enable Counsel mode immediately, there's no point in leaving it to be autoloaded. Requiring it keeps the byte-compiler happy.

  (require 'counsel)

Hide dotfiles in counsel-find-file.

  (setq counsel-find-file-ignore-regexp
        (rx (or (and bos ".")
                (and ".zwc" eos))))

Enable Counsel.

  (counsel-mode)

Don't show that counsel is enabled in the mode-line. It's enabled globally and I'll notice whenever I press M-x for example.

  (diminish 'counsel-mode)

Bookmarks

Save bookmarks in my data directory so my user-emacs-directory is less cluttered.

  (eval-when-compile (require 'bookmark))
  (setq bookmark-default-file (oni:data-location "bookmarks"))

Personal info

Set some personal info for, for example, Gnus to use.

  (setq user-full-name "Tom Willemse"
        user-mail-address "tom@ryuslash.org")

Automatic alignment

Emacs has some powerful automatic alignment features.

  (eval-when-compile (require 'align))

CSS

Align CSS files like so:

  body        { color: #ffffff;            }
  .some-class { background-color: #ffffff; }
  #some-id    { width: 200px;              }

  .some-more-class {
      color:            #ffffff;
      background-color: #ffffff;
      width:            200px;
  }
  (with-eval-after-load 'align
    ;; Keep these in order. They are each added to the _front_ of the
    ;; list and are applied in order. Changing their order will change
    ;; the results.
    (add-to-list 'align-rules-list
                 `(css-closing-brace
                   (regexp . ,(rx (group (0+ whitespace)) "}" eol))
                   (group . (1))
                   (modes . '(scss-mode css-mode))))
    (add-to-list 'align-rules-list
                 `(css-colons
                   (regexp . ,(rx bol
                                  (0+ whitespace)
                                  (1+ (any (?a . ?z) ?- ?$))
                                  ":"
                                  (group (0+ whitespace))
                                  (0+ nonl)
                                  ";"
                                  eol))
                   (group . (1))
                   (modes . '(scss-mode css-mode))
                   (repeat . t)))
    (add-to-list 'align-rules-list
                 `(css-opening-brace
                   (regexp . ,(rx bol
                                  (0+ whitespace)
                                  (0+ (any ?# ?. ?, ?\s ?& ?: ?-
                                           (?a . ?z) (?A . ?Z) (?0 . ?9)))
                                  (any (?a . ?z) (?A . ?Z) (?0 . ?9))
                                  (group (0+ whitespace))
                                  "{"
                                  (0+ nonl)))
                   (group . (1))
                   (modes . '(scss-mode css-mode)))))

PHP

In PHP code it's nice to have any => aligned.

  <?php
  array(
      'foo'  => 'bar',
      'frob' => 'baz'
  );
  ?>
  (with-eval-after-load 'align
    (add-to-list 'align-rules-list
                 `(php-array-arrow
                   (regexp . ,(rx any (group whitespace) "=>" any))
                   (group . (1))
                   (modes . '(php-mode web-mode))
                   (repeat . t))))

Url browsing

Use Conkeror to open URLs.

  (eval-when-compile (require 'browse-url))

  (with-eval-after-load 'browse-url
    (setq browse-url-browser-function 'browse-url-conkeror))

Minibuffer

Enable paredit mode in the minibuffer.

  (add-hook 'minibuffer-setup-hook 'electric-pair-local-mode)

Minor modes

Company mode

A better auto completion system than auto complete.

  (with-eval-after-load 'company (load "oni-company-init"))

Paredit

Paredit is an awesome minor-mode to have when you write in any lisp-like languages. It can feel rather strict and uncomfortable at first, but once you get the hang of using it, you won't want to live without it.

Don't show that paredit is enabled, it should be obvious from the effects it has. This will save some precious real-estate on my mode line.

  (with-eval-after-load 'paredit
    (diminish 'paredit-mode))

Electric indent mode

By default `electric-indent-mode' is enabled globally, but I prefer to enable it locally where I need it.

  (electric-indent-mode -1)

Since Emacs 24 `electric-indent-mode' switches the behavior of the C-j and RET keys. I prefer the original situation because my muscle-memory still remembers to use C-j for newline-and-indent behaviour.

  (defun oni:switch-newline-keys ()
    "Switch the C-j and RET keys in the local buffer."
    (if electric-indent-mode
        (progn
          (local-set-key (kbd "C-j") 'newline)
          (local-set-key (kbd "RET") 'electric-newline-and-maybe-indent))
      (local-unset-key (kbd "C-j"))
      (local-unset-key (kbd "RET"))))

  (add-hook 'electric-indent-local-mode-hook #'oni:switch-newline-keys)

Flycheck

Flycheck lets me see (compiler) errors, warnings and info messages while writing code.

When developing packages with Cask, some special care needs to be taken to ensure the checkers work correctly.

  (add-hook 'flycheck-mode-hook 'flycheck-cask-setup)

I disable the pylint and pyflakes checkers because they don't seem to add much except noise when used together with flake8. Also pylint seems hell-bent on making Python written like a statically-typed langauge.

  (with-eval-after-load 'flycheck
    (mapc (lambda (c) (delq c flycheck-checkers))
          '(python-pylint python-pyflakes)))

Also show which columns messages appear in.

  (with-eval-after-load 'flycheck
    (setq flycheck-highlighting-mode 'columns))

Show the error message at point in a tooltip.

  (with-eval-after-load 'flycheck
    (require 'flycheck-pos-tip)
    (flycheck-pos-tip-mode))

Shorten the flycheck mode line lighter.

  (with-eval-after-load 'flycheck
    (setq flycheck-mode-line-prefix "✓"))

Auto revert mode

ARev isn't very descriptive, and fairly wide. Use a font-awesome icon instead.

  (with-eval-after-load 'autorevert
    (diminish 'auto-revert-mode
              (propertize (concat "  " (char-to-string #xf021))
                          'face '(:family "Font Awesome" :height 0.75))))

Auto fill mode

"Fill" is fine as a mode-line lighter, but I prefer something shorter.

  (diminish 'auto-fill-function
            (propertize (concat "  " (char-to-string #xf149))
                        'face '(:family "Font Awesome" :height 0.75)))

Diff highlight mode

Show the state of lines added, changed and removed since the last commit.

  (require 'diff-hl)
  (global-diff-hl-mode)

Hydra

Hydra is an interesting way of managing keybindings, I want to experiment.

Add a hydra for org.

  (global-set-key (kbd "C-c o") 'oni-hydra-org/body)

Add a hydra for magit.

  (global-set-key (kbd "C-c m") 'oni-hydra-magit/body)

Isearch

Replace the Isearch mode line lighter with a magnifying glass icon.

  (diminish 'isearch-mode
            (propertize (concat "  " (char-to-string #xf002))
                        'face '(:family "Font Awesome" :height 0.75)))

Projectile

Projectile is, thus far, the best project module for Emacs.

Set the known projects file before loading projectile because projectile loads the known projects as it's loading, not after.

  (setq projectile-known-projects-file
        (oni:data-location "projectile-bookmarks.eld"))

Since I'm just going to use it anyway, require it immediately.

  (require 'projectile)

I don't like that projectile tries to take up so much space in my mode-line, so I try to make it a little shorter.

  (with-eval-after-load 'projectile
    (setq projectile-mode-line
          '(:eval
            (if (file-remote-p default-directory)
                " P"
              (let ((name (projectile-project-name)))
                (if (string= "-" name)
                    ""
                  (format " P[%s]" name)))))))

Store projectile files in my data dir.

  (setq projectile-cache-file
        (oni:data-location "projectile.cache"))

Enable it globally so I can always switch to/from projects.

  (projectile-mode)

Use Ivy for projectile completions.

  (setq projectile-completion-system 'ivy)

Server mode

Diminish server mode with a nice icon.

  (with-eval-after-load 'server
    (diminish 'server-buffer-clients
              (propertize (concat "  " (char-to-string #xf233))
                          'face '(:family "Font Awesome" :height 0.75))))

Major modes

Configuration for major modes is specified in dedicated configuration files.

js-mode

JavaScript mode is used by me for json files. js2-mode doesn't work very nicely with json, always showing syntax errors.

  (with-eval-after-load 'js (load "oni-js-mode-init"))
php-mode

I use PHP mode for files that only contain PHP code, no HTML or anything.

  (with-eval-after-load 'php-mode (load "oni-php-mode-init"))
sh-mode

Used for most types of shell scripting (bash, zsh, etc.).

  (with-eval-after-load 'sh-mode (load "oni-sh-mode-init"))
css-mode

CSS and SCSS mode are used for stylesheets!

  (with-eval-after-load 'css-mode (load "oni-css-mode-init"))
emacs-lisp-mode

Emacs lisp is what powers all this Emacs awesomeness.

  (with-eval-after-load 'elisp-mode (load "oni-emacs-lisp-mode-init"))
scheme-mode

Scheme is an awesome lisp variant.

  (with-eval-after-load 'scheme-mode (load "oni-scheme-init"))

Inferior Emacs lisp mode (ielm)

Enable paredit mode.

  (add-hook 'ielm-mode-hook 'paredit-mode)

Mbsync configuration mode

I wrote a simple major-mode for editing my .mbsyncrc file. I might release it as a package, but for now I keep it with the rest of my configuration.

Since it isn't installed by package.el, I need to specify the autoload myself.

  (autoload 'mbsync-conf-mode "mbsync-conf-mode"
    "Major mode for editing mbsync configuration files."
    :interactive)

I also need to add it to the auto-mode-alist so .mbsyncrc is opened with mbsync conf mode.

  (add-to-list 'auto-mode-alist '("\\.mbsyncrc\\'" . mbsync-conf-mode))

Msmtprc mode

I wrote a simple major-mode for editing my .msmtprc file. I might release it as a package, but for now I keep it with the rest of my configuration.

Since it isn't installed by package.el, I need to specify the autoload myself.

  (autoload 'msmtprc-mode "msmtprc-mode"
    "Major mode for editing msmtp configuration files."
    :interactive)

I also need to add it to the auto-mode-alist so .msmtprc is opened with msmtprc mode.

  (add-to-list 'auto-mode-alist '("\\.msmtprc\\'" . msmtprc-mode))

Git commit mode

Enable electric-quote-local-mode to easily type nice-looking quotes while writing commits.

  (add-hook 'git-commit-mode-hook 'electric-quote-local-mode)

Python mode

Enable electric pair mode.

  (add-hook 'python-mode-hook 'electric-pair-local-mode)

Enable syntax and style checking with flycheck.

  (add-hook 'python-mode-hook 'flycheck-mode)

Web mode

Web mode is a good general-purpose web template mode. It works well with many template languages and PHP as well.

Enable a specialized whitespace mode for web mode that shows tabs at the beginning of a line.

  (add-hook 'web-mode-hook 'oni-whitespace-only-tabs-mode)

Makefile mode

Show tabs in Makefiles. Tabs are required at the beginning of any non-continuation line in a recipe. I don't use it for indenting, however.

  (add-hook 'makefile-mode-hook 'oni-whitespace-only-tabs-mode)

Enable electric pairing.

  (add-hook 'makefile-mode-hook 'electric-pair-local-mode)

Clojure mode

Install extra font-locking for clojure.

  (with-eval-after-load 'clojure-mode
    (require 'clojure-mode-extra-font-locking))

Enable paredit mode.

  (add-hook 'clojure-mode-hook 'paredit-mode)

Use the clojure repl as the inferior lisp mode.

  (eval-when-compile (require 'inf-lisp))

  (defun oni:clojure-set-inferior-lisp ()
    (setq inferior-lisp-program "lein repl"))

Add a little more font locking still, and some indentation. This is included in the Clojure for the Brave and True configuration.

  (add-hook 'clojure-mode-hook 'oni-clojure-add-font-lock)
  (add-hook 'clojure-mode-hook 'oni-clojure-add-indent)

Cider mode

Cider is like Slime for Common Lisp. This configuration is copied from the one provided by Clojure for the Brave and True.

Go right to the REPL buffer when it's finished connecting

  (with-eval-after-load 'cider
    (setq cider-repl-pop-to-buffer-on-connect t))

When there's a cider error, show its buffer and switch to it.

  (setq cider-show-error-buffer t
        cider-auto-select-error-buffer t)

Where to store the cider history.

  (setq cider-repl-history-file
        (oni:data-location "cider-history"))

Wrap when navigating history.

  (setq cider-repl-wrap-history t)

Enable paredit in your REPL.

  (add-hook 'cider-repl-mode-hook 'paredit-mode)

Applications

Dired

The Emacs file manager. Very powerful, and I don't use it enough yet.

  (with-eval-after-load 'dired (load "oni-dired-init"))
Magit

The Emacs git interface. By now I think I may know magit better than the git cli.

  (with-eval-after-load 'magit (load "oni-magit-init"))
Ediff

A reall diff application.

  (with-eval-after-load 'ediff (load "oni-ediff-init"))
Gnus

Gnus is one of the most extensible Email programs on the planet.

  (with-eval-after-load 'gnus (load "oni-gnus-init"))
Org

Org is the craziest and most flexible organizational application anyone's ever seen.

  (with-eval-after-load 'org (load "oni-org-init"))
Eshell

The best shell on the planet.

  (with-eval-after-load 'eshell (load "oni-eshell-init"))
Slack

A slack client for Emacs.

  (with-eval-after-load 'slack (load "oni-slack-init"))
Circe

A very nice and clean IRC client for Emacs.

  (with-eval-after-load 'circe (load "oni-circe-init"))

Linewise user-interface

This is the library used by Circe and Slack to display messages.

  (eval-when-compile (require 'lui))

Put the time stamp in lui buffers in the right margin. This gives the text some extra room.

  (with-eval-after-load 'lui
    (setq lui-time-stamp-position 'right-margin))

Remove the "[]" from the time stamp, it's not really necessary.

  (with-eval-after-load 'lui
    (setq lui-time-stamp-format "%H:%M"))

Give the right margin just enough room to show the time-stamps, no more, no less.

  (defun oni:set-circe-margin-width ()
    (setq right-margin-width 5))

  (add-hook 'lui-mode-hook #'oni:set-circe-margin-width)

Fix the wrap prefix so that text at the prompt is aligned properly.

  (defun oni:set-lui-prompt-wrap-prefix ()
    (setq wrap-prefix "  "))

  (add-hook 'lui-mode-hook #'oni:set-lui-prompt-wrap-prefix)

Enable visual line mode in lui buffers so my text doesn't go off-screen.

  (add-hook 'lui-mode-hook 'visual-line-mode)

Turn off filling in lui buffers. I use visual-line mode instead.

  (setq lui-fill-type nil)

Jabber

I like using XMPP to talk to people, jabber.el is very good at this.

  (eval-when-compile (require 'jabber))

Add my account.

  (setq jabber-account-list
        `((,(concat "ryuslash@dukgo.com/" (system-name)))
          (:connection-type . starttls)))

Store any persistent data in the data directory.

  (setq jabber-avatar-cache-directory (oni:data-location "jabber/avatars/")
        jabber-history-dir (oni:data-location "jabber/hist/"))

Change the default prompts.

  (setq jabber-chat-buffer-format         "+%n"
        jabber-chat-foreign-prompt-format "%t %u "
        jabber-chat-local-prompt-format   "%t %u "
        jabber-chat-delayed-time-format   "%H:%M"
        jabber-groupchat-buffer-format    "++%n"
        jabber-groupchat-prompt-format    "%t %u ")

Don't show avatars, publish or retrieve avatars.

  (setq jabber-chat-buffer-show-avatar nil
        jabber-vcard-avatars-publish   nil
        jabber-vcard-avatars-retrieve  nil)

Don't fill long lines in jabber chat buffers, but use visual line mode.

  (setq jabber-chat-fill-long-lines nil)

  (add-hook 'jabber-chat-mode-hook 'visual-line-mode)

Don't send notifications about chat states.

  (setq jabber-chatstates-confirm nil)

Colorize text in multi-user chats.

  (setq jabber-muc-colorize-local   t
        jabber-muc-colorize-foreign t)

Enable recording history.

  (setq jabber-history-enabled    t
        jabber-use-global-history nil)

Clean up the default view of the roster buffer.

  (setq jabber-roster-show-bindings  nil
        jabber-show-offline-contacts nil)

  (add-hook 'jabber-roster-mode-hook 'oni-jabber-set-roster-mode-line)

Use libnotify to send jabber notifications.

  (add-hook 'jabber-alert-message-hooks 'jabber-message-libnotify)
  (add-hook 'jabber-alert-muc-hooks     'jabber-muc-libnotify)

Don't echo presence changes in the mode line, show them in the relevant buffer instead.

  (with-eval-after-load 'jabber-alert
    (remove-hook 'jabber-alert-presence-hooks 'jabber-presence-echo))

  (add-hook 'jabber-alert-presence-hooks 'oni-jabber-show-status-in-buffer)

Set the default directory to my home directory for jabber chat buffers.

  (defun oni:set-default-directory ()
    (setq default-directory "~/"))

  (add-hook 'jabber-chat-mode-hook 'oni:set-default-directory)

Custom

Put the customize settings in a different file so that Emacs doesn't have to modify this file whenever something changes through customize. I put this into my init file last so any settings made in there can overwrite the ones in the rest of the file, not that I usually like to do that.

  (setq custom-file (concat user-emacs-directory "custom.el"))
  (load custom-file)