Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 367 lines (337 sloc) 17.5 KB

Rick Dillon’s Emacs Initialization File

1 Custom Functions

1.1 Platform-independent Path Costruction

If you have a collection of code that you want to load at startup, you tend to want to modify the load-path variable by adding paths of your own. This can be cumbersome, so this function automatically chains together the path-handling calls to reduce unwieldy calls later. You simply call it with the path elements you want to construct, in order:

(construct-path "~" "foo" "bar" "baz.txt")

You can see more of how it’s used in the section on Load Path Setup

(defun construct-path (&rest path-elements)
  (defun inner (elements)
    (print (car elements))
    (if (eq (cdr elements) nil)
        (expand-file-name (car elements))
      (expand-file-name (car elements) (inner (cdr elements)))))
  (inner (reverse path-elements)))

1.2 Multi-buffer Search

Sometimes you want to execute a search across all your open buffers, for example when you have a collection of related files and need to find a reference to a particular word or phrase (writing) or variable or function (programming). This is an interactive function that does exactly that.

(defun search (regexp)
  "Search all buffers for a regexp."
  (interactive "sRegexp to search for: ")
  (multi-occur-in-matching-buffers ".*" regexp))

1.3 Org Mode for Presentations

I like to use org-mode to plan for presentations, and because of its unique ability to handle code with org-babel, it is ideal for giving technical presentations as well. These functions allow simple presentations to be given by narrowing the buffer to a headline and allowing navigation to adjacent headlines.

(require 'org)
(defun org-presentation-start ()
  "Start a presentation from the first headline of the file"
  (outline-next-visible-heading 1)

(defun org-presentation-adjacent (arg)
  "An arg of 1 moves to the next heading, an arg of negative 1
moves to the previous heading."
  (outline-next-visible-heading arg)

(define-key org-mode-map (kbd "C-c s") '(lambda () (interactive) (org-presentation-start)))
(define-key org-mode-map (kbd "C-c n") '(lambda () (interactive) (org-presentation-adjacent 1)))
(define-key org-mode-map (kbd "C-c p") '(lambda () (interactive) (org-presentation-adjacent -1)))  

1.4 Cyberpunk Cursor

From Zarza’s page:

(defvar blink-cursor-colors (list  "#92c48f" "#6785c5" "#be369c" "#d9ca65")
  "On each blink the cursor will cycle to the next color in this list.")

(setq blink-cursor-count 0)
(defun blink-cursor-timer-function ()
  "Cyberpunk variant of timer `blink-cursor-timer'. OVERWRITES original version in `frame.el'.

This one changes the cursor color on each blink. Define colors in `blink-cursor-colors'."
  (when (not (internal-show-cursor-p))
    (when (>= blink-cursor-count (length blink-cursor-colors))
      (setq blink-cursor-count 0))
    (set-cursor-color (nth blink-cursor-count blink-cursor-colors))
    (setq blink-cursor-count (+ 1 blink-cursor-count))
  (internal-show-cursor nil (not (internal-show-cursor-p)))

2 Load Path Setup

This file makes use of a variety of modes and code that are included under the src sub-directory of my .emacs.d directory. This code adds those directories so the rest of the file can be executed smoothly.
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name))))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "org-mode" "lisp"))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "org-mode" "contrib" "lisp"))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "todotxt.el"))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "sunrise-commander"))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "magit"))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "ace-jump-mode"))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "Mew"))
(add-to-list 'load-path (construct-path (file-name-directory (or (buffer-file-name) load-file-name)) "emacs-jabber"))

3 Editor Behavior

3.1 Prevent backups from littering the file system

By default, Emacs provides fairly robust backup file (files that end with a ~) support, but it leaves those files in the same directories alongside the originals. This is a customization that gathers all the backups into one spot, so you can find them in one place, and don’t have to worry about, e.g. your version control system commiting them by accident.

(setq backup-directory-alist `(("." . ,(construct-path "~" "emacsbackup"))))

3.2 Region selection

The newer versions of Emacs (version 23+), turn on a more common but less powerful mode for text selection called transient-mark-mode, which only supplies an active region when text is selected. This disables that mode in favor of the traditional Emacs behavior.

(transient-mark-mode 0)

3.3 Clipboard Behavior

Emacs maintains a kill-ring independent of the system clipboard. This can be useful or annoying, and I tend to find it annoying. This variable combines the two clipboards to they interoperate.

(setq-default x-select-enable-clipboard t)

3.4 Meta Key Modifications

On many platform/keyboard combinations, keys are not placed conveniently for use in Emacs. Specifically, the control key is often placed in the lower left corner of the keyboard, which can cause strain. This is usually rebound to a more convenient location (like the key that most modern keyboards assign to Caps Lock). The other key that sometimes causes issues is the Meta key, and that is what we address here. If you’re using Emacs on Apple hardware with an Apple keyboard, for example, the “Command” key should be treated as Meta. This can be effected with:

(if (eq system-type 'darwin)
    (setq ns-command-modifier (quote meta)))

It’s worth noting that this variable doesn’t even exist on non-Apple platforms, and may or may not be present in alternative builds like Carbon Emacs and Aquamacs, which have their own methods for handling Meta key behavior on Apple platforms.

Similar techniques can be used to assign Meta on Linux, as well:

(setq x-alt-keysym 'meta)
(setq x-super-keysym 'meta)

3.5 Syntax Highlighting (font locking)

Emacs doesn’t turn on syntax highlighting by default. This enables it globally (across all buffers).

(global-font-lock-mode t)

3.6 Tabs Handling

This tends to be a relgious issue, but I prefer it when tabs are never used in any of my work. This tells Emacs to indent with spaces rather than tabs, and set a tab width to be four spaces.

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

3.7 Minibuffer Handling

There are a variety of helper modes that improve the file finding and buffer switching functionality. I used to use iswitchb-mode, which affects only buffer switching, but now use the more comprehensive ido-mode, which affects file finding as well.

(ido-mode t)

3.8 Modeline Information

By default, the modeline doesn’t contain a lot of information. These commands add time, CPU load, battery information and column number information for the current point position.

(display-time-mode t)
(column-number-mode t)
(display-battery-mode t)

3.9 Scrolling

One of the best changes to Emacs’ default behavior, this modifies the scrolling behavior to maintain better continuity. The default behavior is jumpy and disorienting for me. Most other editors are better, but only scroll when you hit the bottom or top of the screen. This modification makes Emacs behave like other editors, but places a 10-line margin at the top of the bottom of the screen so when the screen begins scrolling, you have some context surrounding the point.

(setq scroll-conservatively 10)
(setq scroll-margin 7)
(setq inhibit-startup-screen 1)

3.10 Paretheses

If the cursor is on a parenthesis, bracket or brace, this mode highlights the corresponding parenthesis, bracket or brace. Very handy in a lot of situations, most notably when programming in lisp.

(show-paren-mode t)

3.11 Browse Kill Ring

The kill ring is amazing, but Emacs doesn’t provide a good mechanism to easily access the elements of it interactively. This code does.

(require 'browse-kill-ring)

3.12 Line Wrapping

There are lots of ways to handle line wrapping, but this mode is the best I’ve found, by far. It does not insert hard newlines into your document, and behaves correctly vis a vis line numbers.

(global-visual-line-mode 1)

3.13 Emacs Server

The idiomatic way to use Emacs is to keep one Emacs session running, and open new files in that session. This can be inconvenient when you’re in another application and want to open a file it is pointing to. Often, such applications support the “Open with…” command, but you’d need a way to tell them to open the file with the currently running Emacs session. The way to do this is to have your Emacs session run in server mode (which is the piece of code below), and use the command emacsclient to open the file; emacsclient will take care of handing the file off the current Emacs session.


3.14 Applications

I don’t add on too many non-default applications, but sunrise commander is so good that I couldn’t resist. It is on OFM (orthodox file manager) that is built as a front-end to dired-mode that I find quite useful for its dual-pane functionality.

3.14.1 Sunrise Commander

(require 'sunrise-commander)

3.14.2 Magit

(require 'magit)

3.14.3 todotxt.el

(require 'todotxt)

3.14.4 Mew

Mew is probably the best mail reader for Emacs. At the very least, I think it strikes the best balance between complexity and functionality of all of them. These are some commands that configure Mew.

(require 'mew)

;; Both these functions are completely optional  
(if (boundp 'mail-user-agent)
    (setq mail-user-agent 'mew-user-agent))

(if (fboundp 'define-mail-user-agent)

(setq mew-name "R. P. Dillon")
(setq mew-user "")
(setq mew-mail-domain "")
(setq mew-smtp-server "")
(setq mew-smtp-ssl 't)
(setq mew-smtp-ssl-port 465)
(setq mew-smtp-user "")
(setq mew-smtp-auth-list '("LOGIN" "CRAM-MD5" "PLAIN")) ;; GMail uses LOGIN
(setq mew-proto "%")
(setq mew-imap-user "")
(setq mew-imap-server "")
(setq mew-imap-ssl 't)
(setq mew-imap-ssl-port 993)
(setq mew-imap-delete nil)
(setq mew-use-master-passwd 't) ;; Requires gnupg
(setq mew-ssl-verify-level 0) ;; Required because I don't want to mess with CAs
(setq mew-use-biff 't)
(setq mew-search-method 'est) ;; Hyper Estraier

;; Required because I don't install mew globally
(setq mew-dir "/home/rpdillon/emacs-config/Mew")
(setq mew-prog-mewl (concat mew-dir "/bin/mewl"))
(setq mew-prog-mime-encode (concat mew-dir "/bin/mewencode"))
(setq mew-prog-mime-decode (concat mew-dir "/bin/mewdecode"))
(setq mew-prog-est-update (concat mew-dir "/bin/mewest"))

3.15 Modes

3.15.1 ANSI Color in Terminals


3.15.2 recentf Setup

(recentf-mode t)
(setq recentf-auto-cleanup 'never)

3.15.3 Org Capture

Org offers a data capture mode much like remember-mode, which is great for capturing tasks and information that you need to remember. This simply sets up a capture destination under the “Uncategorized” bullet of in my “memex” directory.

(setq org-directory (construct-path (file-name-as-directory "~") "Dropbox" "memex"))
(setq org-default-notes-file (construct-path  org-directory ""))
(setq org-capture-templates
      '(("i" "Incoming" entry (file+headline org-default-notes-file "Uncategorized")
         "** %t: %?\n  %i\n")
        ("u" "Upcoming" entry (file+headline (construct-path org-directory "") "Upcoming")
         "** %t: %?\n  %i\n")))

3.15.4 Ace Jump

A cursor navigation system so advanced it makes C-s look slow. Thanks winterTTr.

(require 'ace-jump-mode)
(define-key global-map (kbd "C-c SPC") 'ace-jump-mode)

3.15.5 Windmove and Framemove


3.16 Org-Babel Setup

Org babel is an insanely powerful add-on for org-mode that allow code to be integrated into documents. This sets up what languages can be executed with babel.

 '((R . t)
   (scheme . t)

3.17 Accelerators

As seen in Steve Yegge’s discussion on Effective Emacs, these three settings change some default behavior to speed up common actions. Here, we enable the use of C-x C-m to provide the same functionality as M-x provides. Why? See Effective Emacs, Item 2.
(global-set-key "\C-x\C-m" 'execute-extended-command)

Also from Effective Emacs, this is item 3, which allows us to be able to backward-kill-word without having to reach for the backspace key. I’m no longer tangling this section so I can have access to the default macro handling functions.

(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)

I use several functions very often, so I like to be able to access them very quickly. These accelerators proide that quick access.

(global-set-key "\C-xi" 'ibuffer)
(global-set-key "\C-xg" 'magit-status)
(global-set-key "\C-xm" 'mew)
(global-set-key "\C-xf" 'recentf-open-files)
(global-set-key "\C-xc" 'calendar)
(global-set-key "\C-xs" 'sunrise)
(global-set-key (kbd "C-x t") 'todotxt)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cc" 'org-capture)

I have some custom functions I defined earlier that are useful to have easy access to.

(global-set-key "\C-xj" 'org-presentation-start)

3.18 GUI Settings

(tool-bar-mode -1)
(menu-bar-mode -1)
(scroll-bar-mode -1)

3.19 End of File Newlines

There are two aspects to the following customizations. First, there are a bunch of tools out there that rely on text files ending with a newline to operate correctly. This setting ensures that a newline is inserted if one does not exist when we save files with Emacs.

(setq require-final-newline t)

The second customization disables a mode in which Emacs automatically adds newline characters if you tell it to move to the next line at the end of a buffer.

(setq next-line-add-newlines nil)

3.20 Advanced Commands

There are a variety of commands that Emacs considers “advanced”, and will therefore disallow execution of until you affirm (via a prompt) that you actually want to use them. These commands tell Emacs not to do this for commands I use.

(put 'narrow-to-region 'disabled nil)
(put 'dired-find-alternate-file 'disabled nil)
(put 'set-goal-column 'disabled nil)

3.21 Color Themes

Color themes should be incuded in Emacs, but they’re not. These commands simply load the color-theme library and select my current favority color theme.

(require 'color-theme)

3.22 Browser Configuration

Sets up the default browser that Emacs uses to display HTML and visit links.

(setq browse-url-browser-function (quote browse-url-generic))
(setq browse-url-generic-program "/home/rpdillon/apps/firefox/firefox")
Something went wrong with that request. Please try again.