Changed from require -> idle-require.

This commit is contained in:
larstvei 2014-06-18 13:55:46 +02:00
parent c59ada5266
commit a120257c76
4 changed files with 910 additions and 435 deletions

273
README.md
View File

@ -15,6 +15,7 @@
- [Interactive functions](#interactive-functions)
- [Key bindings](#key-bindings)
- [Advice](#advice)
- [Presentation-mode](#presentation-mode)
- [Language mode specific](#language-mode-specific)
- [Lisp](#lisp)
- [Emacs Lisp](#emacs-lisp)
@ -28,7 +29,7 @@
- [Matlab](#matlab)
# About
# About<a id="sec-1" name="sec-1"></a>
This is a Emacs configuration file written in `org-mode`. There are a few
reasons why I wanted to do this. My `.emacs.d/` was a mess, and needed a
@ -36,9 +37,9 @@ proper clean-up. Also I like keeping all my configurations in a single
file, using `org-mode` I can keep this file *organized*. I aim to briefly
explain all my configurations.
# Configurations
# Configurations<a id="sec-2" name="sec-2"></a>
## Meta
## Meta<a id="sec-2-1" name="sec-2-1"></a>
Emacs can only load `.el`-files. We can use `C-c C-v t` to run
`org-babel-tangle`, which extracts the code blocks from the current file
@ -49,7 +50,7 @@ the `after-save-hook` ensuring to always tangle and byte-compile the
`org`-document after changes.
```lisp
(defun init-hook ()
(defun tangle-init ()
"If the current buffer is 'init.org' the code-blocks are
tangled, and the tangled file is compiled."
(when (equal (buffer-file-name)
@ -57,10 +58,10 @@ tangled, and the tangled file is compiled."
(org-babel-tangle)
(byte-compile-file (concat user-emacs-directory "init.el"))))
(add-hook 'after-save-hook 'init-hook)
(add-hook 'after-save-hook 'tangle-init)
```
## Package
## Package<a id="sec-2-2" name="sec-2-2"></a>
Managing extensions for Emacs is simplified using `package` which
is built in to Emacs 24 and newer. To load downloaded packages we
@ -76,11 +77,13 @@ Packages can be fetched from different mirrors, [melpa](http://melpa.milkbox.net
archive and is well maintained.
```lisp
(add-to-list 'package-archives
'("MELPA" . "http://melpa.milkbox.net/packages/") t)
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("org" . "http://orgmode.org/elpa/")
("MELPA" . "http://melpa.milkbox.net/packages/")))
```
We can define a predicate that tells us wither or not the newest version
We can define a predicate that tells us whether or not the newest version
of a package is installed.
```lisp
@ -109,7 +112,8 @@ PACKAGE is installed and the current version is deleted."
(package-delete (symbol-name package)
(package-version-join
(package-desc-vers (cdr pkg-desc)))))
(package-install package))))
(and (assq package package-archive-contents)
(package-install package)))))
```
Also, we will need a function to find all dependencies from a given package.
@ -126,22 +130,21 @@ The `package-refresh-contents` function downloads archive descriptions,
this is a major bottleneck in this configuration. To avoid this we can
try to only check for updates once every day or so. Here are three
variables. The first specifies how often we should check for updates. The
second specifies wither one should update during the initialization. The
second specifies whether one should update during the initialization. The
third is a path to a file where a time-stamp is stored in order to check
when packages were updated last.
```lisp
(defvar days-between-updates 1)
(defvar days-between-updates 7)
(defvar do-package-update-on-init t)
(defvar package-last-update-file
(expand-file-name (concat user-emacs-directory ".package-last-update")))
```
The tricky part is figuring out when the last time the Emacs was updated!
Here is a hacky way of doing it, using [time-stamps](http://www.gnu.org/software/emacs/manual/html_node/emacs/Time-Stamps.html). By adding a
time-stamp to the a file, we can determine wither or not to do an
update. After that we must run the `time-stamp`-function to update the
time-stamp.
The tricky part is figuring out when packages were last updated. Here is
a hacky way of doing it, using [time-stamps](http://www.gnu.org/software/emacs/manual/html_node/emacs/Time-Stamps.html). By adding a time-stamp to the
a file, we can determine whether or not to do an update. After that we
must run the `time-stamp`-function to update the time-stamp.
```lisp
(require 'time-stamp)
@ -191,6 +194,7 @@ configurations are also dependent on them).
geiser ; GNU Emacs and Scheme talk to each other
haskell-mode ; A Haskell editing mode
jedi ; Python auto-completion for Emacs
js2-mode ; Improved JavaScript editing mode
magit ; control Git from Emacs
markdown-mode ; Emacs Major mode for Markdown-formatted files.
matlab-mode ; MATLAB integration with Emacs.
@ -217,7 +221,7 @@ configurations are also dependent on them).
(package-initialize))
```
## Mac OS X
## Mac OS X<a id="sec-2-3" name="sec-2-3"></a>
I run this configuration mostly on Mac OS X, so we need a couple of
settings to make things work smoothly. In the package section
@ -231,18 +235,24 @@ along with external processes a lot simpler. I also prefer using the
(setq mac-option-modifier nil
mac-command-modifier 'meta
x-select-enable-clipboard t)
(exec-path-from-shell-initialize))
(run-with-idle-timer 5 nil 'exec-path-from-shell-initialize))
```
## Require
## Require<a id="sec-2-4" name="sec-2-4"></a>
Some features are not loaded by default to minimize initialization time,
so they have to be required (or loaded, if you will).
so they have to be required (or loaded, if you will). `require`-calls
tends to lead to the largest bottleneck's in a
configuration. `idle-require` delays the `require`-calls to a time where
Emacs is in idle. So this is great for stuff you eventually want to load,
but is not a high priority.
```lisp
(require 'idle-require) ; Need in order to use idle-require
(require 'auto-complete-config) ; a configuration for auto-complete-mode
(dolist (feature
'(auto-compile ; auto-compile .el files
auto-complete-config ; a configuration for auto-complete-mode
jedi ; auto-completion for python
matlab ; matlab-mode
ob-matlab ; org-babel matlab
@ -252,27 +262,35 @@ so they have to be required (or loaded, if you will).
recentf ; recently opened files
smex ; M-x interface Ido-style.
tex-mode)) ; TeX, LaTeX, and SliTeX mode commands
(require feature))
(idle-require feature))
(setq idle-require-idle-delay 5)
(idle-require-mode 1)
```
## Sane defaults
## Sane defaults<a id="sec-2-5" name="sec-2-5"></a>
These are what *I* consider to be saner defaults.
We can set variables to whatever value we'd like using `setq`.
```lisp
(setq initial-scratch-message nil ; Clean scratch buffer.
inhibit-startup-message t ; No splash screen please.
default-input-method "TeX" ; Use TeX when toggeling input method.
ring-bell-function 'ignore ; Quite as a mouse.
(setq default-input-method "TeX" ; Use TeX when toggeling input method.
doc-view-continuous t ; At page edge goto next/previous.
echo-keystrokes 0.1) ; Show keystrokes asap.
echo-keystrokes 0.1 ; Show keystrokes asap.
inhibit-startup-message t ; No splash screen please.
initial-scratch-message nil ; Clean scratch buffer.
ring-bell-function 'ignore ; Quiet.
undo-tree-auto-save-history t ; Save undo history between sessions.
undo-tree-history-directory-alist
;; Put undo-history files in a directory, if it exists.
(let ((undo-dir (concat user-emacs-directory "undo")))
(and (file-exists-p undo-dir)
(list (cons "." undo-dir)))))
;; Some mac-bindings interfere with Emacs bindings.
(when (boundp 'mac-pass-command-to-system)
(setq mac-pass-command-to-system nil))
```
Some variables are buffer-local, so changing them using `setq` will only
@ -340,7 +358,7 @@ Call `auto-complete` default configuration, which enables `auto-complete`
globally.
```lisp
(ac-config-default)
(eval-after-load 'auto-complete-config `(ac-config-default))
```
Automaticly revert `doc-view`-buffers when the file changes on disk.
@ -349,7 +367,7 @@ Automaticly revert `doc-view`-buffers when the file changes on disk.
(add-hook 'doc-view-mode-hook 'auto-revert-mode)
```
## Modes
## Modes<a id="sec-2-6" name="sec-2-6"></a>
There are some modes that are enabled by default that I don't find
particularly useful. We create a list of these modes, and disable all of
@ -369,14 +387,15 @@ default.
```lisp
(dolist (mode
'(abbrev-mode ; E.g. sopl -> System.out.println.
auto-compile-on-load-mode ; Compile .el files on load ...
auto-compile-on-save-mode ; ... and save.
column-number-mode ; Show column number in mode line.
delete-selection-mode ; Replace selected text.
recentf-mode ; Recently opened files.
show-paren-mode ; Highlight matching parentheses.
global-undo-tree-mode)) ; Undo as a tree.
(funcall mode 1))
(eval-after-load 'auto-compile
'((auto-compile-on-save-mode 1))) ; compile .el files on save.
```
This makes `.md`-files open in `markdown-mode`.
@ -385,7 +404,7 @@ This makes `.md`-files open in `markdown-mode`.
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
```
## Visual
## Visual<a id="sec-2-7" name="sec-2-7"></a>
Change the color-theme to `monokai` (downloaded using `package`).
@ -433,7 +452,7 @@ This is what it looks like:
![img](./powerline.png)
## Ido
## Ido<a id="sec-2-8" name="sec-2-8"></a>
Interactive do (or `ido-mode`) changes the way you switch buffers and
open files/directories. Instead of writing complete file paths and buffer
@ -475,7 +494,7 @@ the standard `execute-extended-command` with `smex`.
(global-set-key (kbd "M-x") 'smex)
```
## Calendar
## Calendar<a id="sec-2-9" name="sec-2-9"></a>
Define a function to display week numbers in `calender-mode`. The snippet
is from [EmacsWiki](http://www.emacswiki.org/emacs/CalendarWeekNumbers).
@ -513,7 +532,7 @@ Set Monday as the first day of the week, and set my location.
calendar-location-name "Oslo, Norway")
```
## Mail
## Mail<a id="sec-2-10" name="sec-2-10"></a>
I use [mu4e](http://www.djcbsoftware.nl/code/mu/mu4e.html) (which is a part of [mu](http://www.djcbsoftware.nl/code/mu/)) along with [offlineimap](http://docs.offlineimap.org/en/latest/) on one of my
computers. Because the mail-setup wont work without these programs
@ -524,9 +543,8 @@ a `non-nil` value mail is setup.
(defvar load-mail-setup nil)
(when load-mail-setup
;; We need mu4e
(require 'mu4e)
(eval-after-load 'mu4e
'(progn
;; Some basic mu4e settings.
(setq mu4e-maildir "~/.ifimail" ; top-level Maildir
mu4e-sent-folder "/INBOX.Sent" ; folder for sent messages
@ -555,25 +573,12 @@ a `non-nil` value mail is setup.
;; Register file types that can be handled by ImageMagick.
(when (fboundp 'imagemagick-register-types)
(imagemagick-register-types))
;; (defadvice mu4e (before show-mu4e (arg) activate)
;; "Always show mu4e in fullscreen and remember window
;; configuration."
;; (unless arg
;; (window-configuration-to-register :mu4e-fullscreen)
;; (mu4e-update-mail-and-index t)
;; (delete-other-windows)))
;; (defadvice mu4e-quit (after restore-windows nil activate)
;; "Restore window configuration."
;; (jump-to-register :mu4e-fullscreen))
;; Overwrite the native 'compose-mail' binding to 'show-mu4e'.
(imagemagick-register-types))))
(autoload 'mu4e "mu4e" nil t)
(global-set-key (kbd "C-x m") 'mu4e))
```
## Flyspell
## Flyspell<a id="sec-2-11" name="sec-2-11"></a>
Flyspell offers on-the-fly spell checking. We can enable flyspell for all
text-modes with this snippet.
@ -589,29 +594,53 @@ auto-complete mode, but there is a workaround provided by auto complete.
```lisp
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
(ac-flyspell-workaround)
(eval-after-load 'auto-complete
'(ac-flyspell-workaround))
```
To cycle through dictionary's we can define a variable containing a
cyclic list of installed language packs.
```lisp
(defvar ispell-languages '#1=("english" "norsk" . #1#))
```
Now we only need a small function to change set the language and shift
the list.
When working with several languages, we should be able to cycle through
the languages we most frequently use. Every buffer should have a separate
cycle of languages, so that cycling in one buffer does not change the
state in a different buffer (this problem occurs if you only have one
global cycle). We can implement this by using a [closure](http://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html).
```lisp
(defun cycle-languages ()
"Changes the ispell-dictionary to whatever is the next (or cdr) in the
LANGUAGES (cyclic) list."
"Changes the ispell dictionary to the first element in
ISPELL-LANGUAGES, and returns an interactive function that cycles
the languages in ISPELL-LANGUAGES when invoked."
(lexical-let ((ispell-languages '#1=("american" "norsk" . #1#)))
(ispell-change-dictionary (car ispell-languages))
(lambda ()
(interactive)
;; Rotates the languages cycle and changes the ispell dictionary.
(ispell-change-dictionary
(car (setq ispell-languages (cdr ispell-languages)))))
(car (setq ispell-languages (cdr ispell-languages)))))))
```
## Org
`Flyspell` signals an error if there is no spell-checking tool is
installed. We can advice `turn-on=flyspell` and `flyspell-prog-mode` to
only try to enable `flyspell` if a spell-checking tool is available. Also
we want to enable cycling the languages by typing `C-c l`, so we bind the
function returned from `cycle-languages`.
```lisp
(defadvice turn-on-flyspell (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
(local-set-key (kbd "C-c l") (cycle-languages))
ad-do-it))
```
```lisp
(defadvice flyspell-prog-mode (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
(local-set-key (kbd "C-c l") (cycle-languages))
ad-do-it))
```
## Org<a id="sec-2-12" name="sec-2-12"></a>
I use `org-agenda` for appointments and such.
@ -628,7 +657,7 @@ be themed as they would in their native mode.
(setq org-src-fontify-natively t)
```
## Interactive functions
## Interactive functions<a id="sec-2-13" name="sec-2-13"></a>
<a id="sec:defuns" name="sec:defuns"></a>
@ -696,7 +725,12 @@ To tidy up a buffer we define this function borrowed from [simenheg](https://git
(untabify beg (if (< end (point-max)) end (point-max)))))
```
## Key bindings
Presentation mode.
```lisp
```
## Key bindings<a id="sec-2-14" name="sec-2-14"></a>
Bindings for [expand-region](https://github.com/magnars/expand-region.el).
@ -744,14 +778,13 @@ Bind some native Emacs functions.
Bind the functions defined above.
```lisp
(global-set-key (kbd "C-c l") 'cycle-languages)
(global-set-key (kbd "C-c j") 'remove-whitespace-inbetween)
(global-set-key (kbd "C-x t") 'switch-to-shell)
(global-set-key (kbd "C-c d") 'duplicate-thing)
(global-set-key (kbd "<C-tab>") 'tidy)
```
## Advice
## Advice<a id="sec-2-15" name="sec-2-15"></a>
An advice can be given to a function to make it behave differently. This
advice makes `eval-last-sexp` (bound to `C-x C-e`) replace the sexp with
@ -769,27 +802,48 @@ the value.
ad-do-it))
```
`Flyspell` signals an error if there is no spell-checking tool is
installed. We can advice `turn-on=flyspell` and `flyspell-prog-mode` to
only try to enable `flyspell` if a spell-checking tool is avalible.
When interactively changing the theme (using `M-x load-theme`), the
current custom theme is not disabled. This often gives weird-looking
results; we can advice `load-theme` to always disable themes currently
enabled themes.
```lisp
(defadvice turn-on-flyspell (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
ad-do-it))
(defadvice load-theme
(before disable-before-load (theme &optional no-confirm no-enable) activate)
(mapc 'disable-theme custom-enabled-themes))
```
## Presentation-mode<a id="sec-2-16" name="sec-2-16"></a>
When giving talks it's nice to be able to scale the text
globally. `text-scale-mode` works great for a single buffer, this advice
makes this work globally.
```lisp
(defadvice flyspell-prog-mode (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
ad-do-it))
(defadvice text-scale-mode (around all-buffers (arg) activate)
(if (not global-text-scale-mode)
ad-do-it
(setq-default text-scale-mode-amount text-scale-mode-amount)
(dolist (buffer (buffer-list))
(with-current-buffer buffer
ad-do-it))))
```
# Language mode specific
We don't want this to be default behavior, so we can make a global mode
from the `text-scale-mode`, using `define-globalized-minor-mode`.
## Lisp
```lisp
(require 'face-remap)
(define-globalized-minor-mode
global-text-scale-mode
text-scale-mode
(lambda () (text-scale-mode 1)))
```
# Language mode specific<a id="sec-3" name="sec-3"></a>
## Lisp<a id="sec-3-1" name="sec-3-1"></a>
`Pretty-lambda` provides a customizable variable
`pretty-lambda-auto-modes` that is a list of common lisp modes. Here we
@ -812,7 +866,7 @@ in the `pretty-lambda-auto-modes` list.
(add-hook (intern (concat (symbol-name mode) "-hook")) 'paredit-mode))
```
### Emacs Lisp
### Emacs Lisp<a id="sec-3-1-1" name="sec-3-1-1"></a>
In `emacs-lisp-mode` we can enable `eldoc-mode` to display information
about a function or a variable in the echo area.
@ -822,7 +876,7 @@ about a function or a variable in the echo area.
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
```
### Common lisp
### Common lisp<a id="sec-3-1-2" name="sec-3-1-2"></a>
I use [Slime](http://www.common-lisp.net/project/slime/) along with `lisp-mode` to edit Common Lisp code. Slime
provides code evaluation and other great features, a must have for a
@ -831,8 +885,8 @@ and you can install Slime following the instructions from the site along
with this snippet.
```lisp
(when (file-exists-p "~/quicklisp/slime-helper.elc")
(load (expand-file-name "~/quicklisp/slime-helper.elc")))
(when (file-exists-p "~/.quicklisp/slime-helper.el")
(load (expand-file-name "~/.quicklisp/slime-helper.el")))
```
We can specify what Common Lisp program Slime should use (I use SBCL).
@ -852,7 +906,7 @@ which uses slime completions as a source.
'(add-to-list 'ac-modes 'slime-repl-mode))
```
### Scheme
### Scheme<a id="sec-3-1-3" name="sec-3-1-3"></a>
[Geiser](http://www.nongnu.org/geiser/) provides features similar to Slime for Scheme editing. Everything
works pretty much out of the box, we only need to add auto completion,
@ -863,10 +917,11 @@ and specify which scheme-interpreter we prefer.
(add-hook 'geiser-repl-mode-hook 'ac-geiser-setup)
(eval-after-load "auto-complete"
'(add-to-list 'ac-modes 'geiser-repl-mode))
(setq geiser-active-implementations '(racket))
(eval-after-load "geiser"
'(add-to-list 'geiser-active-implementations 'plt-r5rs)) ;'(racket))
```
## Java and C
## Java and C<a id="sec-3-2" name="sec-3-2"></a>
The `c-mode-common-hook` is a general hook that work on all C-like
languages (C, C++, Java, etc&#x2026;). I like being able to quickly compile
@ -903,7 +958,7 @@ activated.
(add-hook 'java-mode-hook 'java-setup)
```
## Assembler
## Assembler<a id="sec-3-3" name="sec-3-3"></a>
When writing assembler code I use `#` for comments. By defining
`comment-start` we can add comments using `M-;` like in other programming
@ -917,7 +972,7 @@ modes. Also in assembler should one be able to compile using `C-c C-c`.
(add-hook 'asm-mode-hook 'asm-setup)
```
## LaTeX
## LaTeX<a id="sec-3-4" name="sec-3-4"></a>
`.tex`-files should be associated with `latex-mode` instead of
`tex-mode`.
@ -930,7 +985,8 @@ I like using the [Minted](https://code.google.com/p/minted/) package for source
use this we add the following snippet.
```lisp
(add-to-list 'org-latex-packages-alist '("" "minted"))
(eval-after-load 'org
'(add-to-list 'org-latex-packages-alist '("" "minted")))
(setq org-latex-listings 'minted)
```
@ -941,17 +997,19 @@ Tex- and LaTeX-mode, we can add the flag with a rather dirty statement
(if anyone finds a nicer way to do this, please let me know).
```lisp
(setq org-latex-pdf-process
(eval-after-load 'ox-latex
'(setq org-latex-pdf-process
(mapcar
(lambda (str)
(concat "pdflatex -shell-escape "
(substring str (string-match "-" str))))
org-latex-pdf-process))
org-latex-pdf-process)))
(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape ")
(eval-after-load 'tex-mode
'(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape "))
```
## Python
## Python<a id="sec-3-5" name="sec-3-5"></a>
[Jedi](http://tkf.github.io/emacs-jedi/released/) offers very nice auto completion for `python-mode`. Mind that it is
dependent on some python programs as well, so make sure you follow the
@ -966,7 +1024,7 @@ instructions from the site.
(add-hook 'python-mode-hook 'jedi:ac-setup)
```
## Haskell
## Haskell<a id="sec-3-6" name="sec-3-6"></a>
`haskell-doc-mode` is similar to `eldoc`, it displays documentation in
the echo area. Haskell has several indentation modes - I prefer using
@ -977,11 +1035,12 @@ the echo area. Haskell has several indentation modes - I prefer using
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
```
## Matlab
## Matlab<a id="sec-3-7" name="sec-3-7"></a>
Matlab is very similar to Octave, which is supported by Emacs. We just
need to let `.m`-files be associated with `octave-mode`.
`Matlab-mode` works pretty good out of the box, but we can do without the
splash screen.
```lisp
(add-to-list 'matlab-shell-command-switches "-nosplash")
(eval-after-load 'matlab
'(add-to-list 'matlab-shell-command-switches "-nosplash"))
```

467
init.el
View File

@ -1,5 +1,15 @@
(defun init-hook ()
;; Meta
;; Emacs can only load =.el=-files. We can use =C-c C-v t= to run
;; =org-babel-tangle=, which extracts the code blocks from the current file
;; into a source-specific file (in this case a =.el=-file).
;; To avoid doing this each time a change is made we can add a function to
;; the =after-save-hook= ensuring to always tangle and byte-compile the
;; =org=-document after changes.
(defun tangle-init ()
"If the current buffer is 'init.org' the code-blocks are
tangled, and the tangled file is compiled."
(when (equal (buffer-file-name)
@ -7,14 +17,28 @@ tangled, and the tangled file is compiled."
(org-babel-tangle)
(byte-compile-file (concat user-emacs-directory "init.el"))))
(add-hook 'after-save-hook 'init-hook)
(add-hook 'after-save-hook 'tangle-init)
;; Package
;; Managing extensions for Emacs is simplified using =package= which
;; is built in to Emacs 24 and newer. To load downloaded packages we
;; need to initialize =package=.
(require 'package)
(setq package-enable-at-startup nil)
(package-initialize)
(add-to-list 'package-archives
'("MELPA" . "http://melpa.milkbox.net/packages/") t)
;; Packages can be fetched from different mirrors, [[http://melpa.milkbox.net/#/][melpa]] is the largest
;; archive and is well maintained.
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("org" . "http://orgmode.org/elpa/")
("MELPA" . "http://melpa.milkbox.net/packages/")))
;; We can define a predicate that tells us whether or not the newest version
;; of a package is installed.
(defun newest-package-installed-p (package)
"Return true if the newest available PACKAGE is installed."
@ -26,6 +50,10 @@ tangled, and the tangled file is compiled."
(version-list-= (package-desc-vers (cdr local-pkg-desc))
(package-desc-vers (cdr newest-pkg-desc)))))))
;; Let's write a function to install a package if it is not installed or
;; upgrades it if a new version has been released. Here our predicate comes
;; in handy.
(defun upgrade-or-install-package (package)
"Unless the newest available version of PACKAGE is installed
PACKAGE is installed and the current version is deleted."
@ -35,7 +63,10 @@ PACKAGE is installed and the current version is deleted."
(package-delete (symbol-name package)
(package-version-join
(package-desc-vers (cdr pkg-desc)))))
(package-install package))))
(and (assq package package-archive-contents)
(package-install package)))))
;; Also, we will need a function to find all dependencies from a given package.
(defun dependencies (package)
"Returns a list of dependencies from a given PACKAGE."
@ -43,11 +74,24 @@ PACKAGE is installed and the current version is deleted."
(reqs (and pkg-desc (package-desc-reqs (cdr pkg-desc)))))
(mapcar 'car reqs)))
(defvar days-between-updates 1)
;; The =package-refresh-contents= function downloads archive descriptions,
;; this is a major bottleneck in this configuration. To avoid this we can
;; try to only check for updates once every day or so. Here are three
;; variables. The first specifies how often we should check for updates. The
;; second specifies whether one should update during the initialization. The
;; third is a path to a file where a time-stamp is stored in order to check
;; when packages were updated last.
(defvar days-between-updates 7)
(defvar do-package-update-on-init t)
(defvar package-last-update-file
(expand-file-name (concat user-emacs-directory ".package-last-update")))
;; The tricky part is figuring out when packages were last updated. Here is
;; a hacky way of doing it, using [[http://www.gnu.org/software/emacs/manual/html_node/emacs/Time-Stamps.html][time-stamps]]. By adding a time-stamp to the
;; a file, we can determine whether or not to do an update. After that we
;; must run the =time-stamp=-function to update the time-stamp.
(require 'time-stamp)
;; Open the package-last-update-file
(with-temp-file package-last-update-file
@ -72,6 +116,10 @@ PACKAGE is installed and the current version is deleted."
(insert "Time-stamp: <>")
(time-stamp)))
;; Now we can use the function above to make sure packages are installed and
;; up to date. Here are some packages I find useful (some of these
;; configurations are also dependent on them).
(when (and do-package-update-on-init
(y-or-n-p "Update all packages?"))
(package-refresh-contents)
@ -89,6 +137,7 @@ PACKAGE is installed and the current version is deleted."
geiser ; GNU Emacs and Scheme talk to each other
haskell-mode ; A Haskell editing mode
jedi ; Python auto-completion for Emacs
js2-mode ; Improved JavaScript editing mode
magit ; control Git from Emacs
markdown-mode ; Emacs Major mode for Markdown-formatted files.
matlab-mode ; MATLAB integration with Emacs.
@ -114,15 +163,35 @@ PACKAGE is installed and the current version is deleted."
(upgrade-or-install-package 'exec-path-from-shell))
(package-initialize))
;; Mac OS X
;; I run this configuration mostly on Mac OS X, so we need a couple of
;; settings to make things work smoothly. In the package section
;; =exec-path-from-shell= is included (only if you're running OS X), this is
;; to include environment-variables from the shell. It makes useing Emacs
;; along with external processes a lot simpler. I also prefer using the
;; =Command=-key as the =Meta=-key.
(when (memq window-system '(mac ns))
(setq mac-option-modifier nil
mac-command-modifier 'meta
x-select-enable-clipboard t)
(exec-path-from-shell-initialize))
(run-with-idle-timer 5 nil 'exec-path-from-shell-initialize))
;; Require
;; Some features are not loaded by default to minimize initialization time,
;; so they have to be required (or loaded, if you will). =require=-calls
;; tends to lead to the largest bottleneck's in a
;; configuration. =idle-require= delays the =require=-calls to a time where
;; Emacs is in idle. So this is great for stuff you eventually want to load,
;; but is not a high priority.
(require 'idle-require) ; Need in order to use idle-require
(require 'auto-complete-config) ; a configuration for auto-complete-mode
(dolist (feature
'(auto-compile ; auto-compile .el files
auto-complete-config ; a configuration for auto-complete-mode
jedi ; auto-completion for python
matlab ; matlab-mode
ob-matlab ; org-babel matlab
@ -132,31 +201,61 @@ PACKAGE is installed and the current version is deleted."
recentf ; recently opened files
smex ; M-x interface Ido-style.
tex-mode)) ; TeX, LaTeX, and SliTeX mode commands
(require feature))
(idle-require feature))
(setq initial-scratch-message nil ; Clean scratch buffer.
inhibit-startup-message t ; No splash screen please.
default-input-method "TeX" ; Use TeX when toggeling input method.
ring-bell-function 'ignore ; Quite as a mouse.
(setq idle-require-idle-delay 5)
(idle-require-mode 1)
;; Sane defaults
;; These are what /I/ consider to be saner defaults.
;; We can set variables to whatever value we'd like using =setq=.
(setq default-input-method "TeX" ; Use TeX when toggeling input method.
doc-view-continuous t ; At page edge goto next/previous.
echo-keystrokes 0.1) ; Show keystrokes asap.
echo-keystrokes 0.1 ; Show keystrokes asap.
inhibit-startup-message t ; No splash screen please.
initial-scratch-message nil ; Clean scratch buffer.
ring-bell-function 'ignore ; Quiet.
undo-tree-auto-save-history t ; Save undo history between sessions.
undo-tree-history-directory-alist
;; Put undo-history files in a directory, if it exists.
(let ((undo-dir (concat user-emacs-directory "undo")))
(and (file-exists-p undo-dir)
(list (cons "." undo-dir)))))
;; Some mac-bindings interfere with Emacs bindings.
(when (boundp 'mac-pass-command-to-system)
(setq mac-pass-command-to-system nil))
;; Some variables are buffer-local, so changing them using =setq= will only
;; change them in a single buffer. Using =setq-default= we change the
;; buffer-local variable's default value.
(setq-default fill-column 76 ; Maximum line width.
indent-tabs-mode nil ; Use spaces instead of tabs.
split-width-threshold 100 ; Split verticly by default.
auto-fill-function 'do-auto-fill) ; Auto-fill-mode everywhere.
;; The =load-path= specifies where Emacs should look for =.el=-files (or
;; Emacs lisp files). I have a directory called =site-lisp= where I keep all
;; extensions that have been installed manually (these are mostly my own
;; projects).
(let ((default-directory (concat user-emacs-directory "site-lisp/")))
(when (file-exists-p default-directory)
(normal-top-level-add-to-load-path '("."))
(normal-top-level-add-subdirs-to-load-path)))
;; Answering /yes/ and /no/ to each question from Emacs can be tedious, a
;; single /y/ or /n/ will suffice.
(fset 'yes-or-no-p 'y-or-n-p)
;; To avoid file system clutter we put all auto saved files in a single
;; directory.
(defvar emacs-autosave-directory
(concat user-emacs-directory "autosaves/")
"This variable dictates where to put auto saves. It is set to a
@ -169,24 +268,42 @@ PACKAGE is installed and the current version is deleted."
auto-save-file-name-transforms
`((".*" ,emacs-autosave-directory t)))
;; Set =utf-8= as preferred coding system.
(set-language-environment "UTF-8")
;; By default the =narrow-to-region= command is disabled and issues a
;; warning, because it might confuse new users. I find it useful sometimes,
;; and don't want to be warned.
(put 'narrow-to-region 'disabled nil)
(ac-config-default)
;; Call =auto-complete= default configuration, which enables =auto-complete=
;; globally.
(eval-after-load 'auto-complete-config `(ac-config-default))
;; Automaticly revert =doc-view=-buffers when the file changes on disk.
(add-hook 'doc-view-mode-hook 'auto-revert-mode)
;; Modes
;; There are some modes that are enabled by default that I don't find
;; particularly useful. We create a list of these modes, and disable all of
;; these.
(dolist (mode
'(tool-bar-mode ; No toolbars, more room for text.
scroll-bar-mode ; No scroll bars either.
blink-cursor-mode)) ; The blinking cursor gets old.
(funcall mode 0))
;; Let's apply the same technique for enabling modes that are disabled by
;; default.
(dolist (mode
'(abbrev-mode ; E.g. sopl -> System.out.println.
auto-compile-on-load-mode ; Compile .el files on load ...
auto-compile-on-save-mode ; ... and save.
column-number-mode ; Show column number in mode line.
delete-selection-mode ; Replace selected text.
recentf-mode ; Recently opened files.
@ -194,13 +311,27 @@ PACKAGE is installed and the current version is deleted."
global-undo-tree-mode)) ; Undo as a tree.
(funcall mode 1))
(eval-after-load 'auto-compile
'((auto-compile-on-save-mode 1))) ; compile .el files on save.
;; This makes =.md=-files open in =markdown-mode=.
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
;; Visual
;; Change the color-theme to =monokai= (downloaded using =package=).
(load-theme 'monokai t)
;; Use the [[http://www.levien.com/type/myfonts/inconsolata.html][Inconsolata]] font if it's installed on the system.
(when (member "Inconsolata-g" (font-family-list))
(set-face-attribute 'default nil :font "Inconsolata-g-11"))
;; [[https://github.com/milkypostman/powerline][Powerline]] is an extension to customize the mode line. This is modified
;; version =powerline-nano-theme=.
(setq-default
mode-line-format
'("%e"
@ -225,6 +356,14 @@ PACKAGE is installed and the current version is deleted."
(powerline-fill nil (powerline-width rhs))
(powerline-render rhs))))))
;; Ido
;; Interactive do (or =ido-mode=) changes the way you switch buffers and
;; open files/directories. Instead of writing complete file paths and buffer
;; names you can write a part of it and select one from a list of
;; possibilities. Using =ido-vertical-mode= changes the way possibilities
;; are displayed, and =flx-ido-mode= enables fuzzy matching.
(dolist (mode
'(ido-mode ; Interactivly do.
ido-everywhere ; Use Ido for all buffer/file reading.
@ -232,14 +371,30 @@ PACKAGE is installed and the current version is deleted."
flx-ido-mode)) ; Toggle flx ido mode.
(funcall mode 1))
;; We can set the order of file selections in =ido=. I prioritize source
;; files along with =org=- and =tex=-files.
(setq ido-file-extensions-order
'(".el" ".scm" ".lisp" ".java" ".c" ".h" ".org" ".tex"))
;; Sometimes when using =ido-switch-buffer= the =*Messages*= buffer get in
;; the way, so we set it to be ignored (it can be accessed using =C-h e=, so
;; there is really no need for it in the buffer list).
(add-to-list 'ido-ignore-buffers "*Messages*")
;; To make =M-x= behave more like =ido-mode= we can use the =smex=
;; package. It needs to be initialized, and we can replace the binding to
;; the standard =execute-extended-command= with =smex=.
(smex-initialize)
(global-set-key (kbd "M-x") 'smex)
;; Calendar
;; Define a function to display week numbers in =calender-mode=. The snippet
;; is from [[http://www.emacswiki.org/emacs/CalendarWeekNumbers][EmacsWiki]].
(defun calendar-show-week (arg)
"Displaying week number in calendar-mode."
(interactive "P")
@ -256,19 +411,29 @@ PACKAGE is installed and the current version is deleted."
(list month day year)))))
'font-lock-face 'calendar-iso-week-face))))
;; Evaluate the =calendar-show-week= function.
(calendar-show-week t)
;; Set Monday as the first day of the week, and set my location.
(setq calendar-week-start-day 1
calendar-latitude 60.0
calendar-longitude 10.7
calendar-location-name "Oslo, Norway")
;; Mail
;; I use [[http://www.djcbsoftware.nl/code/mu/mu4e.html][mu4e]] (which is a part of [[http://www.djcbsoftware.nl/code/mu/][mu]]) along with [[http://docs.offlineimap.org/en/latest/][offlineimap]] on one of my
;; computers. Because the mail-setup wont work without these programs
;; installed we bind =load-mail-setup= to =nil=. If the value is changed to
;; a =non-nil= value mail is setup.
(defvar load-mail-setup nil)
(when load-mail-setup
;; We need mu4e
(require 'mu4e)
(eval-after-load 'mu4e
'(progn
;; Some basic mu4e settings.
(setq mu4e-maildir "~/.ifimail" ; top-level Maildir
mu4e-sent-folder "/INBOX.Sent" ; folder for sent messages
@ -297,43 +462,81 @@ PACKAGE is installed and the current version is deleted."
;; Register file types that can be handled by ImageMagick.
(when (fboundp 'imagemagick-register-types)
(imagemagick-register-types))
;; (defadvice mu4e (before show-mu4e (arg) activate)
;; "Always show mu4e in fullscreen and remember window
;; configuration."
;; (unless arg
;; (window-configuration-to-register :mu4e-fullscreen)
;; (mu4e-update-mail-and-index t)
;; (delete-other-windows)))
;; (defadvice mu4e-quit (after restore-windows nil activate)
;; "Restore window configuration."
;; (jump-to-register :mu4e-fullscreen))
;; Overwrite the native 'compose-mail' binding to 'show-mu4e'.
(imagemagick-register-types))))
(autoload 'mu4e "mu4e" nil t)
(global-set-key (kbd "C-x m") 'mu4e))
;; Flyspell
;; Flyspell offers on-the-fly spell checking. We can enable flyspell for all
;; text-modes with this snippet.
(add-hook 'text-mode-hook 'turn-on-flyspell)
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
(ac-flyspell-workaround)
;; To use flyspell for programming there is =flyspell-prog-mode=, that only
;; enables spell checking for comments and strings. We can enable it for all
;; programming modes using the =prog-mode-hook=. Flyspell interferes with
;; auto-complete mode, but there is a workaround provided by auto complete.
(defvar ispell-languages '#1=("english" "norsk" . #1#))
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
(eval-after-load 'auto-complete
'(ac-flyspell-workaround))
;; When working with several languages, we should be able to cycle through
;; the languages we most frequently use. Every buffer should have a separate
;; cycle of languages, so that cycling in one buffer does not change the
;; state in a different buffer (this problem occurs if you only have one
;; global cycle). We can implement this by using a [[http://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html][closure]].
(defun cycle-languages ()
"Changes the ispell-dictionary to whatever is the next (or cdr) in the
LANGUAGES (cyclic) list."
"Changes the ispell dictionary to the first element in
ISPELL-LANGUAGES, and returns an interactive function that cycles
the languages in ISPELL-LANGUAGES when invoked."
(lexical-let ((ispell-languages '#1=("american" "norsk" . #1#)))
(ispell-change-dictionary (car ispell-languages))
(lambda ()
(interactive)
;; Rotates the languages cycle and changes the ispell dictionary.
(ispell-change-dictionary
(car (setq ispell-languages (cdr ispell-languages)))))
(car (setq ispell-languages (cdr ispell-languages)))))))
;; =Flyspell= signals an error if there is no spell-checking tool is
;; installed. We can advice =turn-on=flyspell= and =flyspell-prog-mode= to
;; only try to enable =flyspell= if a spell-checking tool is available. Also
;; we want to enable cycling the languages by typing =C-c l=, so we bind the
;; function returned from =cycle-languages=.
(defadvice turn-on-flyspell (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
(local-set-key (kbd "C-c l") (cycle-languages))
ad-do-it))
(defadvice flyspell-prog-mode (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
(local-set-key (kbd "C-c l") (cycle-languages))
ad-do-it))
;; Org
;; I use =org-agenda= for appointments and such.
(setq org-agenda-start-on-weekday nil ; Show agenda from today.
org-agenda-files '("~/Dropbox/life.org") ; A list of agenda files.
org-agenda-default-appointment-duration 120) ; 2 hours appointments.
;; When editing org-files with source-blocks, we want the source blocks to
;; be themed as they would in their native mode.
(setq org-src-fontify-natively t)
;; Interactive functions
;; <<sec:defuns>>
;; To search recent files useing =ido-mode= we add this snippet from
;; [[http://www.emacswiki.org/emacs/CalendarWeekNumbers][EmacsWiki]].
(defun recentf-ido-find-file ()
"Find a recent file using Ido."
(interactive)
@ -341,11 +544,18 @@ LANGUAGES (cyclic) list."
(when f
(find-file f))))
;; =just-one-space= removes all whitespace around a point - giving it a
;; negative argument it removes newlines as well. We wrap a interactive
;; function around it to be able to bind it to a key.
(defun remove-whitespace-inbetween ()
"Removes whitespace before and after the point."
(interactive)
(just-one-space -1))
;; This interactive function switches you to a =shell=, and if triggered in
;; the shell it switches back to the previous buffer.
(defun switch-to-shell ()
"Jumps to eshell or back."
(interactive)
@ -353,6 +563,9 @@ LANGUAGES (cyclic) list."
(switch-to-prev-buffer)
(shell)))
;; To duplicate either selected text or a line we define this interactive
;; function.
(defun duplicate-thing ()
"Ethier duplicates the line or the region"
(interactive)
@ -364,6 +577,8 @@ LANGUAGES (cyclic) list."
(newline))
(insert (buffer-substring start end)))))
;; To tidy up a buffer we define this function borrowed from [[https://github.com/simenheg][simenheg]].
(defun tidy ()
"Ident, untabify and unwhitespacify current buffer, or region if active."
(interactive)
@ -373,31 +588,56 @@ LANGUAGES (cyclic) list."
(whitespace-cleanup)
(untabify beg (if (< end (point-max)) end (point-max)))))
;; Presentation mode.
;; Key bindings
;; Bindings for [[https://github.com/magnars/expand-region.el][expand-region]].
(global-set-key (kbd "C-'") 'er/expand-region)
(global-set-key (kbd "C-;") 'er/contract-region)
;; Bindings for [[https://github.com/magnars/multiple-cursors.el][multiple-cursors]].
(global-set-key (kbd "C-c e") 'mc/edit-lines)
(global-set-key (kbd "C-c a") 'mc/mark-all-like-this)
(global-set-key (kbd "C-c n") 'mc/mark-next-like-this)
;; Bindings for [[http://magit.github.io][Magit]].
(global-set-key (kbd "C-c m") 'magit-status)
;; Bindings for [[https://github.com/winterTTr/ace-jump-mode][ace-jump-mode]].
(global-set-key (kbd "C-c SPC") 'ace-jump-mode)
;; Bindings for =move-text=.
(global-set-key (kbd "<M-S-up>") 'move-text-up)
(global-set-key (kbd "<M-S-down>") 'move-text-down)
;; Bind some native Emacs functions.
(global-set-key (kbd "C-c s") 'ispell-word)
(global-set-key (kbd "C-c t") 'org-agenda-list)
(global-set-key (kbd "C-x k") 'kill-this-buffer)
(global-set-key (kbd "C-x C-r") 'recentf-ido-find-file)
(global-set-key (kbd "C-c l") 'cycle-languages)
;; Bind the functions defined [[sec:defuns][above]].
(global-set-key (kbd "C-c j") 'remove-whitespace-inbetween)
(global-set-key (kbd "C-x t") 'switch-to-shell)
(global-set-key (kbd "C-c d") 'duplicate-thing)
(global-set-key (kbd "<C-tab>") 'tidy)
;; Advice
;; An advice can be given to a function to make it behave differently. This
;; advice makes =eval-last-sexp= (bound to =C-x C-e=) replace the sexp with
;; the value.
(defadvice eval-last-sexp (around replace-sexp (arg) activate)
"Replace sexp when called with a prefix argument."
(if arg
@ -408,44 +648,108 @@ LANGUAGES (cyclic) list."
(forward-sexp))
ad-do-it))
(defadvice turn-on-flyspell (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
ad-do-it))
;; When interactively changing the theme (using =M-x load-theme=), the
;; current custom theme is not disabled. This often gives weird-looking
;; results; we can advice =load-theme= to always disable themes currently
;; enabled themes.
(defadvice flyspell-prog-mode (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
ad-do-it))
(defadvice load-theme
(before disable-before-load (theme &optional no-confirm no-enable) activate)
(mapc 'disable-theme custom-enabled-themes))
;; Presentation-mode
;; When giving talks it's nice to be able to scale the text
;; globally. =text-scale-mode= works great for a single buffer, this advice
;; makes this work globally.
(defadvice text-scale-mode (around all-buffers (arg) activate)
(if (not global-text-scale-mode)
ad-do-it
(setq-default text-scale-mode-amount text-scale-mode-amount)
(dolist (buffer (buffer-list))
(with-current-buffer buffer
ad-do-it))))
;; We don't want this to be default behavior, so we can make a global mode
;; from the =text-scale-mode=, using =define-globalized-minor-mode=.
(require 'face-remap)
(define-globalized-minor-mode
global-text-scale-mode
text-scale-mode
(lambda () (text-scale-mode 1)))
;; Lisp
;; =Pretty-lambda= provides a customizable variable
;; =pretty-lambda-auto-modes= that is a list of common lisp modes. Here we
;; can add some extra lisp-modes. We run the =pretty-lambda-for-modes=
;; function to activate =pretty-lambda-mode= in lisp modes.
(dolist (mode '(slime-repl-mode geiser-repl-mode))
(add-to-list 'pretty-lambda-auto-modes mode))
(pretty-lambda-for-modes)
;; I use =Paredit= when editing lisp code, we enable this for all lisp-modes
;; in the =pretty-lambda-auto-modes= list.
(dolist (mode pretty-lambda-auto-modes)
;; add paredit-mode to all mode-hooks
(add-hook (intern (concat (symbol-name mode) "-hook")) 'paredit-mode))
;; Emacs Lisp
;; In =emacs-lisp-mode= we can enable =eldoc-mode= to display information
;; about a function or a variable in the echo area.
(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
(when (file-exists-p "~/quicklisp/slime-helper.elc")
(load (expand-file-name "~/quicklisp/slime-helper.elc")))
;; Common lisp
;; I use [[http://www.common-lisp.net/project/slime/][Slime]] along with =lisp-mode= to edit Common Lisp code. Slime
;; provides code evaluation and other great features, a must have for a
;; Common Lisp developer. [[http://www.quicklisp.org/beta/][Quicklisp]] is a library manager for Common Lisp,
;; and you can install Slime following the instructions from the site along
;; with this snippet.
(when (file-exists-p "~/.quicklisp/slime-helper.el")
(load (expand-file-name "~/.quicklisp/slime-helper.el")))
;; We can specify what Common Lisp program Slime should use (I use SBCL).
(setq inferior-lisp-program "sbcl")
;; To improve auto completion for Common Lisp editing we can use =ac-slime=
;; which uses slime completions as a source.
(add-hook 'slime-mode-hook 'set-up-slime-ac)
(add-hook 'slime-repl-mode-hook 'set-up-slime-ac)
(eval-after-load "auto-complete"
'(add-to-list 'ac-modes 'slime-repl-mode))
;; Scheme
;; [[http://www.nongnu.org/geiser/][Geiser]] provides features similar to Slime for Scheme editing. Everything
;; works pretty much out of the box, we only need to add auto completion,
;; and specify which scheme-interpreter we prefer.
(add-hook 'geiser-mode-hook 'ac-geiser-setup)
(add-hook 'geiser-repl-mode-hook 'ac-geiser-setup)
(eval-after-load "auto-complete"
'(add-to-list 'ac-modes 'geiser-repl-mode))
(setq geiser-active-implementations '(racket))
(eval-after-load "geiser"
'(add-to-list 'geiser-active-implementations 'plt-r5rs)) ;'(racket))
;; Java and C
;; The =c-mode-common-hook= is a general hook that work on all C-like
;; languages (C, C++, Java, etc...). I like being able to quickly compile
;; using =C-c C-c= (instead of =M-x compile=), a habit from =latex-mode=.
(defun c-setup ()
(local-set-key (kbd "C-c C-c") 'compile))
@ -455,36 +759,71 @@ LANGUAGES (cyclic) list."
(add-hook 'c-mode-common-hook 'c-setup)
;; Some statements in Java appear often, and become tedious to write
;; out. We can use abbrevs to speed this up.
(define-abbrev-table 'java-mode-abbrev-table
'(("psv" "public static void main(String[] args) {" nil 0)
("sopl" "System.out.println" nil 0)
("sop" "System.out.printf" nil 0)))
;; To be able to use the abbrev table defined above, =abbrev-mode= must be
;; activated.
(defun java-setup ()
(abbrev-mode t)
(setq-local compile-command (concat "javac " (buffer-name))))
(add-hook 'java-mode-hook 'java-setup)
;; Assembler
;; When writing assembler code I use =#= for comments. By defining
;; =comment-start= we can add comments using =M-;= like in other programming
;; modes. Also in assembler should one be able to compile using =C-c C-c=.
(defun asm-setup ()
(setq comment-start "#")
(local-set-key (kbd "C-c C-c") 'compile))
(add-hook 'asm-mode-hook 'asm-setup)
;; LaTeX
;; =.tex=-files should be associated with =latex-mode= instead of
;; =tex-mode=.
(add-to-list 'auto-mode-alist '("\\.tex\\'" . latex-mode))
(add-to-list 'org-latex-packages-alist '("" "minted"))
;; I like using the [[https://code.google.com/p/minted/][Minted]] package for source blocks in LaTeX. To make org
;; use this we add the following snippet.
(eval-after-load 'org
'(add-to-list 'org-latex-packages-alist '("" "minted")))
(setq org-latex-listings 'minted)
(setq org-latex-pdf-process
;; Because [[https://code.google.com/p/minted/][Minted]] uses [[http://pygments.org][Pygments]] (an external process), we must add the
;; =-shell-escape= option to the =org-latex-pdf-process= commands. The
;; =tex-compile-commands= variable controls the default compile command for
;; Tex- and LaTeX-mode, we can add the flag with a rather dirty statement
;; (if anyone finds a nicer way to do this, please let me know).
(eval-after-load 'ox-latex
'(setq org-latex-pdf-process
(mapcar
(lambda (str)
(concat "pdflatex -shell-escape "
(substring str (string-match "-" str))))
org-latex-pdf-process))
org-latex-pdf-process)))
(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape ")
(eval-after-load 'tex-mode
'(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape "))
;; Python
;; [[http://tkf.github.io/emacs-jedi/released/][Jedi]] offers very nice auto completion for =python-mode=. Mind that it is
;; dependent on some python programs as well, so make sure you follow the
;; instructions from the site.
;; (setq jedi:server-command
;; (cons "python3" (cdr jedi:server-command))
@ -493,7 +832,19 @@ LANGUAGES (cyclic) list."
(setq jedi:complete-on-dot t)
(add-hook 'python-mode-hook 'jedi:ac-setup)
;; Haskell
;; =haskell-doc-mode= is similar to =eldoc=, it displays documentation in
;; the echo area. Haskell has several indentation modes - I prefer using
;; =haskell-indent=.
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(add-to-list 'matlab-shell-command-switches "-nosplash")
;; Matlab
;; =Matlab-mode= works pretty good out of the box, but we can do without the
;; splash screen.
(eval-after-load 'matlab
'(add-to-list 'matlab-shell-command-switches "-nosplash"))

221
init.org
View File

@ -1,5 +1,8 @@
#+BABEL: :cache yes
#+LATEX_HEADER: \usepackage{parskip}
#+LATEX_HEADER: \usepackage{inconsolata}
#+PROPERTY: header-args :tangle yes :comments org
#+TITLE: Emacs configuration file
#+AUTHOR: Lars Tveito
@ -22,7 +25,7 @@
=org=-document after changes.
#+BEGIN_SRC emacs-lisp
(defun init-hook ()
(defun tangle-init ()
"If the current buffer is 'init.org' the code-blocks are
tangled, and the tangled file is compiled."
(when (equal (buffer-file-name)
@ -30,7 +33,7 @@
(org-babel-tangle)
(byte-compile-file (concat user-emacs-directory "init.el"))))
(add-hook 'after-save-hook 'init-hook)
(add-hook 'after-save-hook 'tangle-init)
#+END_SRC
** Package
@ -49,11 +52,13 @@
archive and is well maintained.
#+BEGIN_SRC emacs-lisp
(add-to-list 'package-archives
'("MELPA" . "http://melpa.milkbox.net/packages/") t)
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("org" . "http://orgmode.org/elpa/")
("MELPA" . "http://melpa.milkbox.net/packages/")))
#+END_SRC
We can define a predicate that tells us wither or not the newest version
We can define a predicate that tells us whether or not the newest version
of a package is installed.
#+BEGIN_SRC emacs-lisp
@ -82,7 +87,8 @@
(package-delete (symbol-name package)
(package-version-join
(package-desc-vers (cdr pkg-desc)))))
(package-install package))))
(and (assq package package-archive-contents)
(package-install package)))))
#+END_SRC
Also, we will need a function to find all dependencies from a given package.
@ -99,22 +105,21 @@
this is a major bottleneck in this configuration. To avoid this we can
try to only check for updates once every day or so. Here are three
variables. The first specifies how often we should check for updates. The
second specifies wither one should update during the initialization. The
second specifies whether one should update during the initialization. The
third is a path to a file where a time-stamp is stored in order to check
when packages were updated last.
#+BEGIN_SRC emacs-lisp
(defvar days-between-updates 1)
(defvar days-between-updates 7)
(defvar do-package-update-on-init t)
(defvar package-last-update-file
(expand-file-name (concat user-emacs-directory ".package-last-update")))
#+END_SRC
The tricky part is figuring out when the last time the Emacs was updated!
Here is a hacky way of doing it, using [[http://www.gnu.org/software/emacs/manual/html_node/emacs/Time-Stamps.html][time-stamps]]. By adding a
time-stamp to the a file, we can determine wither or not to do an
update. After that we must run the =time-stamp=-function to update the
time-stamp.
The tricky part is figuring out when packages were last updated. Here is
a hacky way of doing it, using [[http://www.gnu.org/software/emacs/manual/html_node/emacs/Time-Stamps.html][time-stamps]]. By adding a time-stamp to the
a file, we can determine whether or not to do an update. After that we
must run the =time-stamp=-function to update the time-stamp.
#+BEGIN_SRC emacs-lisp
(require 'time-stamp)
@ -164,6 +169,7 @@
geiser ; GNU Emacs and Scheme talk to each other
haskell-mode ; A Haskell editing mode
jedi ; Python auto-completion for Emacs
js2-mode ; Improved JavaScript editing mode
magit ; control Git from Emacs
markdown-mode ; Emacs Major mode for Markdown-formatted files.
matlab-mode ; MATLAB integration with Emacs.
@ -204,18 +210,24 @@
(setq mac-option-modifier nil
mac-command-modifier 'meta
x-select-enable-clipboard t)
(exec-path-from-shell-initialize))
(run-with-idle-timer 5 nil 'exec-path-from-shell-initialize))
#+END_SRC
** Require
Some features are not loaded by default to minimize initialization time,
so they have to be required (or loaded, if you will).
so they have to be required (or loaded, if you will). =require=-calls
tends to lead to the largest bottleneck's in a
configuration. =idle-require= delays the =require=-calls to a time where
Emacs is in idle. So this is great for stuff you eventually want to load,
but is not a high priority.
#+BEGIN_SRC emacs-lisp
(require 'idle-require) ; Need in order to use idle-require
(require 'auto-complete-config) ; a configuration for auto-complete-mode
(dolist (feature
'(auto-compile ; auto-compile .el files
auto-complete-config ; a configuration for auto-complete-mode
jedi ; auto-completion for python
matlab ; matlab-mode
ob-matlab ; org-babel matlab
@ -225,7 +237,10 @@
recentf ; recently opened files
smex ; M-x interface Ido-style.
tex-mode)) ; TeX, LaTeX, and SliTeX mode commands
(require feature))
(idle-require feature))
(setq idle-require-idle-delay 5)
(idle-require-mode 1)
#+END_SRC
** Sane defaults
@ -235,12 +250,18 @@
We can set variables to whatever value we'd like using =setq=.
#+BEGIN_SRC emacs-lisp
(setq initial-scratch-message nil ; Clean scratch buffer.
inhibit-startup-message t ; No splash screen please.
default-input-method "TeX" ; Use TeX when toggeling input method.
ring-bell-function 'ignore ; Quite as a mouse.
(setq default-input-method "TeX" ; Use TeX when toggeling input method.
doc-view-continuous t ; At page edge goto next/previous.
echo-keystrokes 0.1) ; Show keystrokes asap.
echo-keystrokes 0.1 ; Show keystrokes asap.
inhibit-startup-message t ; No splash screen please.
initial-scratch-message nil ; Clean scratch buffer.
ring-bell-function 'ignore ; Quiet.
undo-tree-auto-save-history t ; Save undo history between sessions.
undo-tree-history-directory-alist
;; Put undo-history files in a directory, if it exists.
(let ((undo-dir (concat user-emacs-directory "undo")))
(and (file-exists-p undo-dir)
(list (cons "." undo-dir)))))
;; Some mac-bindings interfere with Emacs bindings.
(when (boundp 'mac-pass-command-to-system)
@ -313,7 +334,7 @@
globally.
#+BEGIN_SRC emacs-lisp
(ac-config-default)
(eval-after-load 'auto-complete-config `(ac-config-default))
#+END_SRC
Automaticly revert =doc-view=-buffers when the file changes on disk.
@ -342,14 +363,16 @@
#+BEGIN_SRC emacs-lisp
(dolist (mode
'(abbrev-mode ; E.g. sopl -> System.out.println.
auto-compile-on-load-mode ; Compile .el files on load ...
auto-compile-on-save-mode ; ... and save.
column-number-mode ; Show column number in mode line.
delete-selection-mode ; Replace selected text.
recentf-mode ; Recently opened files.
show-paren-mode ; Highlight matching parentheses.
global-undo-tree-mode)) ; Undo as a tree.
(funcall mode 1))
(eval-after-load 'auto-compile
'((auto-compile-on-save-mode 1))) ; compile .el files on save.
#+END_SRC
This makes =.md=-files open in =markdown-mode=.
@ -371,6 +394,7 @@
#+BEGIN_SRC emacs-lisp
(when (member "Inconsolata-g" (font-family-list))
(set-face-attribute 'default nil :font "Inconsolata-g-11"))
#+END_SRC
[[https://github.com/milkypostman/powerline][Powerline]] is an extension to customize the mode line. This is modified
@ -497,9 +521,8 @@
(defvar load-mail-setup nil)
(when load-mail-setup
;; We need mu4e
(require 'mu4e)
(eval-after-load 'mu4e
'(progn
;; Some basic mu4e settings.
(setq mu4e-maildir "~/.ifimail" ; top-level Maildir
mu4e-sent-folder "/INBOX.Sent" ; folder for sent messages
@ -528,21 +551,8 @@
;; Register file types that can be handled by ImageMagick.
(when (fboundp 'imagemagick-register-types)
(imagemagick-register-types))
;; (defadvice mu4e (before show-mu4e (arg) activate)
;; "Always show mu4e in fullscreen and remember window
;; configuration."
;; (unless arg
;; (window-configuration-to-register :mu4e-fullscreen)
;; (mu4e-update-mail-and-index t)
;; (delete-other-windows)))
;; (defadvice mu4e-quit (after restore-windows nil activate)
;; "Restore window configuration."
;; (jump-to-register :mu4e-fullscreen))
;; Overwrite the native 'compose-mail' binding to 'show-mu4e'.
(imagemagick-register-types))))
(autoload 'mu4e "mu4e" nil t)
(global-set-key (kbd "C-x m") 'mu4e))
#+END_SRC
@ -562,26 +572,50 @@
#+BEGIN_SRC emacs-lisp
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
(ac-flyspell-workaround)
(eval-after-load 'auto-complete
'(ac-flyspell-workaround))
#+END_SRC
To cycle through dictionary's we can define a variable containing a
cyclic list of installed language packs.
#+BEGIN_SRC emacs-lisp
(defvar ispell-languages '#1=("english" "norsk" . #1#))
#+END_SRC
Now we only need a small function to change set the language and shift
the list.
When working with several languages, we should be able to cycle through
the languages we most frequently use. Every buffer should have a separate
cycle of languages, so that cycling in one buffer does not change the
state in a different buffer (this problem occurs if you only have one
global cycle). We can implement this by using a [[http://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html][closure]].
#+BEGIN_SRC emacs-lisp
(defun cycle-languages ()
"Changes the ispell-dictionary to whatever is the next (or cdr) in the
LANGUAGES (cyclic) list."
"Changes the ispell dictionary to the first element in
ISPELL-LANGUAGES, and returns an interactive function that cycles
the languages in ISPELL-LANGUAGES when invoked."
(lexical-let ((ispell-languages '#1=("american" "norsk" . #1#)))
(ispell-change-dictionary (car ispell-languages))
(lambda ()
(interactive)
;; Rotates the languages cycle and changes the ispell dictionary.
(ispell-change-dictionary
(car (setq ispell-languages (cdr ispell-languages)))))
(car (setq ispell-languages (cdr ispell-languages)))))))
#+END_SRC
=Flyspell= signals an error if there is no spell-checking tool is
installed. We can advice =turn-on=flyspell= and =flyspell-prog-mode= to
only try to enable =flyspell= if a spell-checking tool is available. Also
we want to enable cycling the languages by typing =C-c l=, so we bind the
function returned from =cycle-languages=.
#+BEGIN_SRC emacs-lisp
(defadvice turn-on-flyspell (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
(local-set-key (kbd "C-c l") (cycle-languages))
ad-do-it))
#+END_SRC
#+BEGIN_SRC emacs-lisp
(defadvice flyspell-prog-mode (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
(local-set-key (kbd "C-c l") (cycle-languages))
ad-do-it))
#+END_SRC
** Org
@ -668,6 +702,12 @@
(untabify beg (if (< end (point-max)) end (point-max)))))
#+END_SRC
Presentation mode.
#+BEGIN_SRC emacs-lisp
#+END_SRC
** Key bindings
Bindings for [[https://github.com/magnars/expand-region.el][expand-region]].
@ -716,7 +756,6 @@
Bind the functions defined [[sec:defuns][above]].
#+BEGIN_SRC emacs-lisp
(global-set-key (kbd "C-c l") 'cycle-languages)
(global-set-key (kbd "C-c j") 'remove-whitespace-inbetween)
(global-set-key (kbd "C-x t") 'switch-to-shell)
(global-set-key (kbd "C-c d") 'duplicate-thing)
@ -741,22 +780,43 @@
ad-do-it))
#+END_SRC
=Flyspell= signals an error if there is no spell-checking tool is
installed. We can advice =turn-on=flyspell= and =flyspell-prog-mode= to
only try to enable =flyspell= if a spell-checking tool is avalible.
When interactively changing the theme (using =M-x load-theme=), the
current custom theme is not disabled. This often gives weird-looking
results; we can advice =load-theme= to always disable themes currently
enabled themes.
#+BEGIN_SRC emacs-lisp
(defadvice turn-on-flyspell (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
ad-do-it))
(defadvice load-theme
(before disable-before-load (theme &optional no-confirm no-enable) activate)
(mapc 'disable-theme custom-enabled-themes))
#+END_SRC
** Presentation-mode
When giving talks it's nice to be able to scale the text
globally. =text-scale-mode= works great for a single buffer, this advice
makes this work globally.
#+BEGIN_SRC emacs-lisp
(defadvice flyspell-prog-mode (around check nil activate)
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
ad-do-it))
(defadvice text-scale-mode (around all-buffers (arg) activate)
(if (not global-text-scale-mode)
ad-do-it
(setq-default text-scale-mode-amount text-scale-mode-amount)
(dolist (buffer (buffer-list))
(with-current-buffer buffer
ad-do-it))))
#+END_SRC
We don't want this to be default behavior, so we can make a global mode
from the =text-scale-mode=, using =define-globalized-minor-mode=.
#+BEGIN_SRC emacs-lisp
(require 'face-remap)
(define-globalized-minor-mode
global-text-scale-mode
text-scale-mode
(lambda () (text-scale-mode 1)))
#+END_SRC
* Language mode specific
@ -802,8 +862,8 @@
with this snippet.
#+BEGIN_SRC emacs-lisp
(when (file-exists-p "~/quicklisp/slime-helper.elc")
(load (expand-file-name "~/quicklisp/slime-helper.elc")))
(when (file-exists-p "~/.quicklisp/slime-helper.el")
(load (expand-file-name "~/.quicklisp/slime-helper.el")))
#+END_SRC
We can specify what Common Lisp program Slime should use (I use SBCL).
@ -834,7 +894,8 @@
(add-hook 'geiser-repl-mode-hook 'ac-geiser-setup)
(eval-after-load "auto-complete"
'(add-to-list 'ac-modes 'geiser-repl-mode))
(setq geiser-active-implementations '(racket))
(eval-after-load "geiser"
'(add-to-list 'geiser-active-implementations 'plt-r5rs)) ;'(racket))
#+END_SRC
** Java and C
@ -901,7 +962,8 @@
use this we add the following snippet.
#+BEGIN_SRC emacs-lisp
(add-to-list 'org-latex-packages-alist '("" "minted"))
(eval-after-load 'org
'(add-to-list 'org-latex-packages-alist '("" "minted")))
(setq org-latex-listings 'minted)
#+END_SRC
@ -912,14 +974,16 @@
(if anyone finds a nicer way to do this, please let me know).
#+BEGIN_SRC emacs-lisp
(setq org-latex-pdf-process
(eval-after-load 'ox-latex
'(setq org-latex-pdf-process
(mapcar
(lambda (str)
(concat "pdflatex -shell-escape "
(substring str (string-match "-" str))))
org-latex-pdf-process))
org-latex-pdf-process)))
(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape ")
(eval-after-load 'tex-mode
'(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape "))
#+END_SRC
** Python
@ -950,9 +1014,10 @@
** Matlab
Matlab is very similar to Octave, which is supported by Emacs. We just
need to let =.m=-files be associated with =octave-mode=.
=Matlab-mode= works pretty good out of the box, but we can do without the
splash screen.
#+BEGIN_SRC emacs-lisp
(add-to-list 'matlab-shell-command-switches "-nosplash")
(eval-after-load 'matlab
'(add-to-list 'matlab-shell-command-switches "-nosplash"))
#+END_SRC

BIN
init.pdf

Binary file not shown.