2014-04-06 17:15:57 +00:00
- [About ](#about )
- [Configurations ](#configurations )
- [Meta ](#meta )
- [Package ](#package )
- [Mac OS X ](#mac-os-x )
- [Require ](#require )
- [Sane defaults ](#sane-defaults )
- [Modes ](#modes )
- [Visual ](#visual )
- [Ido ](#ido )
- [Calendar ](#calendar )
- [Mail ](#mail )
- [Flyspell ](#flyspell )
- [Org ](#org )
- [Interactive functions ](#interactive-functions )
- [Advice ](#advice )
2014-06-18 11:55:46 +00:00
- [Presentation-mode ](#presentation-mode )
2014-10-25 15:42:53 +00:00
- [Mode specific ](#mode-specific )
- [Shell ](#shell )
2014-04-06 17:15:57 +00:00
- [Lisp ](#lisp )
- [Emacs Lisp ](#emacs-lisp )
- [Common lisp ](#common-lisp )
- [Scheme ](#scheme )
- [Java and C ](#java-and-c )
- [Assembler ](#assembler )
- [LaTeX ](#latex )
2014-09-17 22:35:52 +00:00
- [Markdown ](#markdown )
2014-04-06 17:15:57 +00:00
- [Python ](#python )
- [Haskell ](#haskell )
- [Matlab ](#matlab )
2014-10-25 15:42:53 +00:00
- [Key bindings ](#key-bindings )
- [License ](#license )
2014-04-06 17:15:57 +00:00
2014-06-18 11:55:46 +00:00
# About<a id="sec-1" name="sec-1"></a>
2014-04-06 01:25:47 +00:00
2014-09-17 22:35:52 +00:00
This is an Emacs configuration file written in [Org mode ](http://orgmode.org ). It is an attempt
to keep my `~/.emacs.d` tidy, but still be able to keep it all in one
file. I aim to briefly explain all my configurations as I go along!
I would not recommend using this configuration *as-is* , because it
probably contains a lot you don't really need. I do, however, hope people
find some golden nuggets that they can smuggle into their own configs.
If you really do want to try this config out, this is how I'd go about it:
**Clone the repo.**
```sh
git clone https://github.com/larstvei/dot-emacs
```
**Backup your old `~/.emacs.d` (if necessary).**
```sh
mv ~/.emacs.d ~/.emacs.d-bak
```
**Backup your old `~/.emacs` -file (if necessary).**
```sh
mv ~/.emacs ~/.emacs-bak
```
**And finally**
```sh
mv dot-emacs ~/.emacs.d
```
On first run it should install a bunch of packages (this might take a
while), and you might have to restart your Emacs the first time. If you
experience bugs, please let me know!
2014-04-06 01:25:47 +00:00
2014-06-18 11:55:46 +00:00
# Configurations<a id="sec-2" name="sec-2"></a>
2014-04-06 01:25:47 +00:00
2014-06-18 11:55:46 +00:00
## Meta<a id="sec-2-1" name="sec-2-1"></a>
2014-04-06 01:25:47 +00:00
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(defun tangle-init ()
2014-04-06 01:25:47 +00:00
"If the current buffer is 'init.org' the code-blocks are
tangled, and the tangled file is compiled."
(when (equal (buffer-file-name)
(expand-file-name (concat user-emacs-directory "init.org")))
2014-10-25 15:42:53 +00:00
;; Avoid running hooks when tangling.
(let ((prog-mode-hook nil))
(org-babel-tangle)
(byte-compile-file (concat user-emacs-directory "init.el")))))
2014-04-06 01:25:47 +00:00
2014-06-18 11:55:46 +00:00
(add-hook 'after-save-hook 'tangle-init)
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
## Package<a id="sec-2-2" name="sec-2-2"></a>
2014-04-06 01:25:47 +00:00
2014-09-17 22:35:52 +00:00
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` . `cl` is a library that contains many functions from
Common Lisp, and comes in handy quite often, so we want to make sure it's
loaded, along with `package` , which is obviously needed.
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(require 'cl)
2014-04-06 01:25:47 +00:00
(require 'package)
(setq package-enable-at-startup nil)
(package-initialize)
```
Packages can be fetched from different mirrors, [melpa ](http://melpa.milkbox.net/#/ ) is the largest
archive and is well maintained.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("org" . "http://orgmode.org/elpa/")
("MELPA" . "http://melpa.milkbox.net/packages/")))
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
We can define a predicate that tells us whether or not the newest version
2014-04-06 01:25:47 +00:00
of a package is installed.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun newest-package-installed-p (package)
"Return true if the newest available PACKAGE is installed."
(when (package-installed-p package)
(let* ((local-pkg-desc (or (assq package package-alist)
(assq package package--builtins)))
(newest-pkg-desc (assq package package-archive-contents)))
(and local-pkg-desc newest-pkg-desc
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun upgrade-or-install-package (package)
"Unless the newest available version of PACKAGE is installed
PACKAGE is installed and the current version is deleted."
(unless (newest-package-installed-p package)
2014-10-25 15:42:53 +00:00
(let ((get-desc (if (version< emacs-version " 24 . 4 " ) ' cdr ' cadr ) )
(pkg-desc (assq package package-alist)))
2014-04-06 01:25:47 +00:00
(when pkg-desc
2014-10-25 15:42:53 +00:00
(if (version< emacs-version " 24 . 4 " )
(package-delete (symbol-name package)
(package-version-join
(package-desc-vers (get-desc pkg-desc))))
(package-delete pkg-desc)))
2014-06-18 11:55:46 +00:00
(and (assq package package-archive-contents)
(package-install package)))))
2014-04-06 01:25:47 +00:00
```
Also, we will need a function to find all dependencies from a given package.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun dependencies (package)
"Returns a list of dependencies from a given PACKAGE."
(let* ((pkg-desc (assq package package-alist))
(reqs (and pkg-desc (package-desc-reqs (cdr pkg-desc)))))
(mapcar 'car reqs)))
```
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
2014-06-18 11:55:46 +00:00
second specifies whether one should update during the initialization. The
2014-04-06 01:25:47 +00:00
third is a path to a file where a time-stamp is stored in order to check
when packages were updated last.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(defvar days-between-updates 7)
2014-04-06 01:25:47 +00:00
(defvar do-package-update-on-init t)
(defvar package-last-update-file
(expand-file-name (concat user-emacs-directory ".package-last-update")))
```
2014-06-18 11:55:46 +00:00
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.
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(require 'time-stamp)
;; Open the package-last-update-file
(with-temp-file package-last-update-file
(if (file-exists-p package-last-update-file)
(progn
;; Insert it's original content's.
(insert-file-contents package-last-update-file)
(let ((start (re-search-forward time-stamp-start nil t))
(end (re-search-forward time-stamp-end nil t)))
(when (and start end)
;; Assuming we have found a time-stamp, we check determine if it's
;; time to update.
(setq do-package-update-on-init
(< = days-between-updates
(days-between
(current-time-string)
(buffer-substring-no-properties start end))))
;; Remember to update the time-stamp.
(when do-package-update-on-init
(time-stamp)))))
;; If no such file exists it is created with a time-stamp.
(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).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(when (and do-package-update-on-init
(y-or-n-p "Update all packages?"))
(package-refresh-contents)
(let* ((packages
'(ac-geiser ; Auto-complete backend for geiser
ac-slime ; An auto-complete source using slime completions
ace-jump-mode ; quick cursor location minor mode
auto-compile ; automatically compile Emacs Lisp libraries
auto-complete ; auto completion
2014-09-17 22:35:52 +00:00
centered-window ; Center the text when there's only one window
2014-04-06 01:25:47 +00:00
elscreen ; window session manager
expand-region ; Increase selected region by semantic units
flx-ido ; flx integration for ido
2014-09-17 22:45:42 +00:00
idle-require ; load elisp libraries while Emacs is idle
2014-04-06 01:25:47 +00:00
ido-vertical-mode ; Makes ido-mode display vertically.
geiser ; GNU Emacs and Scheme talk to each other
haskell-mode ; A Haskell editing mode
jedi ; Python auto-completion for Emacs
2014-06-18 11:55:46 +00:00
js2-mode ; Improved JavaScript editing mode
2014-04-06 01:25:47 +00:00
magit ; control Git from Emacs
markdown-mode ; Emacs Major mode for Markdown-formatted files.
matlab-mode ; MATLAB integration with Emacs.
monokai-theme ; A fruity color theme for Emacs.
move-text ; Move current line or region with M-up or M-down
multiple-cursors ; Multiple cursors for Emacs.
org ; Outline-based notes management and organizer
paredit ; minor mode for editing parentheses
powerline ; Rewrite of Powerline
pretty-lambdada ; the word `lambda' as the Greek letter.
2014-09-17 22:35:52 +00:00
slime ; Superior Lisp Interaction Mode for Emacs
2014-04-06 01:25:47 +00:00
smex ; M-x interface with Ido-style fuzzy matching.
undo-tree)) ; Treat undo history as a tree
;; Fetch dependencies from all packages.
(reqs (mapcar 'dependencies packages))
;; Append these to the original list, and remove any duplicates.
(packages (delete-dups (apply 'append packages reqs))))
(dolist (package packages)
(upgrade-or-install-package package)))
;; This package is only relevant for Mac OS X.
(when (memq window-system '(mac ns))
(upgrade-or-install-package 'exec-path-from-shell))
(package-initialize))
```
2014-06-18 11:55:46 +00:00
## Mac OS X<a id="sec-2-3" name="sec-2-3"></a>
2014-04-06 01:25:47 +00:00
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(when (memq window-system '(mac ns))
(setq mac-option-modifier nil
mac-command-modifier 'meta
x-select-enable-clipboard t)
2014-10-25 15:42:53 +00:00
(exec-path-from-shell-initialize))
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
## Require<a id="sec-2-4" name="sec-2-4"></a>
2014-04-06 01:25:47 +00:00
Some features are not loaded by default to minimize initialization time,
2014-06-18 11:55:46 +00:00
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.
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(require 'idle-require) ; Need in order to use idle-require
(require 'auto-complete-config) ; a configuration for auto-complete-mode
2014-04-06 01:25:47 +00:00
(dolist (feature
'(auto-compile ; auto-compile .el files
jedi ; auto-completion for python
matlab ; matlab-mode
ob-matlab ; org-babel matlab
ox-latex ; the latex-exporter (from org)
ox-md ; Markdown exporter (from org)
pretty-lambdada ; show 'lambda' as the greek letter.
recentf ; recently opened files
smex ; M-x interface Ido-style.
tex-mode)) ; TeX, LaTeX, and SliTeX mode commands
2014-06-18 11:55:46 +00:00
(idle-require feature))
(setq idle-require-idle-delay 5)
(idle-require-mode 1)
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
## Sane defaults<a id="sec-2-5" name="sec-2-5"></a>
2014-04-06 01:25:47 +00:00
These are what *I* consider to be saner defaults.
We can set variables to whatever value we'd like using `setq` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(setq default-input-method "TeX" ; Use TeX when toggling input method.
2014-06-18 11:55:46 +00:00
doc-view-continuous t ; At page edge goto next/previous.
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.
2014-09-17 22:35:52 +00:00
;; Save undo history between sessions, if you have an undo-dir
undo-tree-auto-save-history
(file-exists-p
(concat user-emacs-directory "undo"))
2014-06-18 11:55:46 +00:00
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)))))
2014-04-06 01:25:47 +00:00
;; 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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(fset 'yes-or-no-p 'y-or-n-p)
```
To avoid file system clutter we put all auto saved files in a single
directory.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defvar emacs-autosave-directory
(concat user-emacs-directory "autosaves/")
"This variable dictates where to put auto saves. It is set to a
directory called autosaves located wherever your .emacs.d/ is
located.")
;; Sets all files to be backed up and auto saved in a single directory.
(setq backup-directory-alist
`((".*" . ,emacs-autosave-directory))
auto-save-file-name-transforms
`((".*" ,emacs-autosave-directory t)))
```
Set `utf-8` as preferred coding system.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(put 'narrow-to-region 'disabled nil)
```
Call `auto-complete` default configuration, which enables `auto-complete`
globally.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(eval-after-load 'auto-complete-config `(ac-config-default))
2014-04-06 01:25:47 +00:00
```
Automaticly revert `doc-view` -buffers when the file changes on disk.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(add-hook 'doc-view-mode-hook 'auto-revert-mode)
```
2014-06-18 11:55:46 +00:00
## Modes<a id="sec-2-6" name="sec-2-6"></a>
2014-04-06 01:25:47 +00:00
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(dolist (mode
'(abbrev-mode ; E.g. sopl -> System.out.println.
column-number-mode ; Show column number in mode line.
delete-selection-mode ; Replace selected text.
2014-10-25 15:42:53 +00:00
dirtrack-mode ; directory tracking in *shell*
2014-04-06 01:25:47 +00:00
recentf-mode ; Recently opened files.
show-paren-mode ; Highlight matching parentheses.
global-undo-tree-mode)) ; Undo as a tree.
(funcall mode 1))
2014-06-18 11:55:46 +00:00
2014-10-25 15:42:53 +00:00
(when (version< emacs-version " 24 . 4 " )
(eval-after-load 'auto-compile
'((auto-compile-on-save-mode 1)))) ; compile .el files on save.
2014-04-06 01:25:47 +00:00
```
This makes `.md` -files open in `markdown-mode` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
```
2014-06-18 11:55:46 +00:00
## Visual<a id="sec-2-7" name="sec-2-7"></a>
2014-04-06 01:25:47 +00:00
Change the color-theme to `monokai` (downloaded using `package` ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(load-theme 'monokai t)
```
Use the [Inconsolata ](http://www.levien.com/type/myfonts/inconsolata.html ) font if it's installed on the system.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(when (member "Inconsolata-g" (font-family-list))
(set-face-attribute 'default nil :font "Inconsolata-g-11"))
```
2014-06-18 11:55:46 +00:00
## Ido<a id="sec-2-8" name="sec-2-8"></a>
2014-04-06 01:25:47 +00:00
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(dolist (mode
'(ido-mode ; Interactivly do.
ido-everywhere ; Use Ido for all buffer/file reading.
ido-vertical-mode ; Makes ido-mode display vertically.
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(smex-initialize)
```
2014-06-18 11:55:46 +00:00
## Calendar<a id="sec-2-9" name="sec-2-9"></a>
2014-04-06 01:25:47 +00:00
Define a function to display week numbers in `calender-mode` . The snippet
is from [EmacsWiki ](http://www.emacswiki.org/emacs/CalendarWeekNumbers ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun calendar-show-week (arg)
"Displaying week number in calendar-mode."
(interactive "P")
(copy-face font-lock-constant-face 'calendar-iso-week-face)
(set-face-attribute
'calendar-iso-week-face nil :height 0.7)
(setq calendar-intermonth-text
(and arg
'(propertize
(format
"%2d"
(car (calendar-iso-from-absolute
(calendar-absolute-from-gregorian
(list month day year)))))
'font-lock-face 'calendar-iso-week-face))))
```
Evaluate the `calendar-show-week` function.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(calendar-show-week t)
```
Set Monday as the first day of the week, and set my location.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(setq calendar-week-start-day 1
calendar-latitude 60.0
calendar-longitude 10.7
calendar-location-name "Oslo, Norway")
```
2014-06-18 11:55:46 +00:00
## Mail<a id="sec-2-10" name="sec-2-10"></a>
2014-04-06 01:25:47 +00:00
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
installed we bind `load-mail-setup` to `nil` . If the value is changed to
a `non-nil` value mail is setup.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defvar load-mail-setup nil)
(when load-mail-setup
2014-06-18 11:55:46 +00:00
(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
mu4e-drafts-folder "/INBOX.Drafts" ; unfinished messages
mu4e-trash-folder "/INBOX.Trash" ; trashed messages
mu4e-refile-folder "/INBOX.Archive" ; saved messages
mu4e-get-mail-command "offlineimap" ; offlineimap to fetch mail
mu4e-compose-signature "- Lars" ; Sign my name
mu4e-update-interval (* 5 60) ; update every 5 min
mu4e-confirm-quit nil ; just quit
mu4e-view-show-images t ; view images
mu4e-html2text-command
"html2text -utf8") ; use utf-8
;; Setup for sending mail.
(setq user-full-name
"Lars Tveito" ; Your full name
user-mail-address
"larstvei@ifi.uio.no" ; And email-address
smtpmail-smtp-server
"smtp.uio.no" ; Host to mail-server
smtpmail-smtp-service 465 ; Port to mail-server
smtpmail-stream-type 'ssl ; Protocol used for sending
send-mail-function 'smtpmail-send-it ; Use smpt to send
mail-user-agent 'mu4e-user-agent) ; Use mu4e!
;; Register file types that can be handled by ImageMagick.
(when (fboundp 'imagemagick-register-types)
(imagemagick-register-types))))
(autoload 'mu4e "mu4e" nil t)
2014-04-06 01:25:47 +00:00
(global-set-key (kbd "C-x m") 'mu4e))
```
2014-06-18 11:55:46 +00:00
## Flyspell<a id="sec-2-11" name="sec-2-11"></a>
2014-04-06 01:25:47 +00:00
Flyspell offers on-the-fly spell checking. We can enable flyspell for all
text-modes with this snippet.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(add-hook 'text-mode-hook 'turn-on-flyspell)
```
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
2014-06-18 11:55:46 +00:00
(eval-after-load 'auto-complete
'(ac-flyspell-workaround))
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
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 ).
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(defun cycle-languages ()
"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)))))))
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
`Flyspell` signals an error if there is no spell-checking tool is
2014-09-17 22:45:42 +00:00
installed. We can advice `turn-on-flyspell` and `flyspell-prog-mode` to
2014-06-18 11:55:46 +00:00
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` .
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(defadvice turn-on-flyspell (before check nil activate)
2014-06-18 11:55:46 +00:00
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
2014-09-17 22:35:52 +00:00
(local-set-key (kbd "C-c l") (cycle-languages))))
2014-06-18 11:55:46 +00:00
```
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(defadvice flyspell-prog-mode (before check nil activate)
2014-06-18 11:55:46 +00:00
"Turns on flyspell only if a spell-checking tool is installed."
(when (executable-find ispell-program-name)
2014-09-17 22:35:52 +00:00
(local-set-key (kbd "C-c l") (cycle-languages))))
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
## Org<a id="sec-2-12" name="sec-2-12"></a>
2014-04-06 01:25:47 +00:00
I use `org-agenda` for appointments and such.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(setq org-src-fontify-natively t
org-confirm-babel-evaluate nil)
2014-09-17 22:45:42 +00:00
```
This is quite an ugly fix for allowing code markup for expressions like
`"this string"` , because the quotation marks causes problems.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(require 'org)
(setcar (nthcdr 2 org-emphasis-regexp-components) " \t\n,")
(custom-set-variables `(org-emphasis-alist ',org-emphasis-alist))
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
## Interactive functions<a id="sec-2-13" name="sec-2-13"></a>
2014-04-06 01:25:47 +00:00
< a id = "sec:defuns" name = "sec:defuns" > < / a >
To search recent files useing `ido-mode` we add this snippet from
[EmacsWiki ](http://www.emacswiki.org/emacs/CalendarWeekNumbers ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun recentf-ido-find-file ()
"Find a recent file using Ido."
(interactive)
(let ((f (ido-completing-read "Choose recent file: " recentf-list nil t)))
(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
2014-10-25 15:42:53 +00:00
function around it to be able to bind it to a key. In Emacs 24.4
`cycle-spacing` was introduced, and it works like just one space, but
when run in succession it cycles between one, zero and the original
number of spaces.
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(defun cycle-spacing-delete-newlines ()
2014-04-06 01:25:47 +00:00
"Removes whitespace before and after the point."
(interactive)
2014-10-25 15:42:53 +00:00
(if (version< emacs-version " 24 . 4 " )
(just-one-space -1)
(cycle-spacing -1)))
```
2014-10-26 23:00:29 +00:00
Often I want to find other occurrences of a word I'm at, or more
specifically the symbol (or tag) I'm at. The
`isearch-forward-symbol-at-point` in Emacs 24.4 works well for this, but
I don't want to be bothered with the `isearch` interface. Rather jump
quickly between occurrences of a symbol, or if non is found, don't do
anything.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-26 23:00:29 +00:00
(defun jump-to-symbol-internal (& optional backwardp)
"Jumps to the next symbol near the point if such a symbol
exists. If BACKWARDP is non-nil it jumps backward."
2014-10-25 15:42:53 +00:00
(let* ((point (point))
2014-10-26 23:00:29 +00:00
(bounds (find-tag-default-bounds))
(beg (car bounds)) (end (cdr bounds))
(str (isearch-symbol-regexp (find-tag-default)))
(search (if backwardp 'search-backward-regexp
'search-forward-regexp)))
(goto-char (if backwardp beg end))
(funcall search str nil t)
(cond ((< = beg (point) end) (goto-char point))
(backwardp (forward-char (- point beg)))
(t (backward-char (- end point))))))
(defun jump-to-previous-like-this ()
"Jumps to the previous occurrence of the symbol at point."
2014-10-25 15:42:53 +00:00
(interactive)
2014-10-26 23:00:29 +00:00
(jump-to-symbol-internal t))
2014-10-25 15:42:53 +00:00
2014-10-26 23:00:29 +00:00
(defun jump-to-next-like-this ()
"Jumps to the next occurrence of the symbol at point."
2014-10-25 15:42:53 +00:00
(interactive)
2014-10-26 23:00:29 +00:00
(jump-to-symbol-internal))
2014-04-06 01:25:47 +00:00
```
2014-10-25 15:42:53 +00:00
I sometimes regret killing the `*scratch*` -buffer, and have realized I
never want to actually kill it. I just want to get it out of the way, and
clean it up. The function below does just this for the
`*scratch*` -buffer, and works like `kill-this-buffer` for any other
buffer. It removes all buffer content and buries the buffer (this means
making it the least likely candidate for `other-buffer` ).
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(defun kill-this-buffer-unless-scratch ()
"Works like `kill-this-buffer' unless the current buffer is the
*scratch* buffer. In witch case the buffer content is deleted and
the buffer is buried."
2014-04-06 01:25:47 +00:00
(interactive)
2014-10-25 15:42:53 +00:00
(if (not (string= (buffer-name) "*scratch*"))
(kill-this-buffer)
(delete-region (point-min) (point-max))
(switch-to-buffer (other-buffer))
(bury-buffer "*scratch*")))
2014-04-06 01:25:47 +00:00
```
To duplicate either selected text or a line we define this interactive
function.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun duplicate-thing ()
2014-09-17 22:35:52 +00:00
"Duplicates the current line, or the region if active."
2014-04-06 01:25:47 +00:00
(interactive)
(save-excursion
(let ((start (if (region-active-p) (region-beginning) (point-at-bol)))
(end (if (region-active-p) (region-end) (point-at-eol))))
(goto-char end)
(unless (region-active-p)
(newline))
(insert (buffer-substring start end)))))
```
To tidy up a buffer we define this function borrowed from [simenheg ](https://github.com/simenheg ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun tidy ()
"Ident, untabify and unwhitespacify current buffer, or region if active."
(interactive)
(let ((beg (if (region-active-p) (region-beginning) (point-min)))
(end (if (region-active-p) (region-end) (point-max))))
(indent-region beg end)
(whitespace-cleanup)
(untabify beg (if (< end ( point-max ) ) end ( point-max ) ) ) ) )
```
2014-09-23 19:46:41 +00:00
If you have a link to a raw `.el` -file, run `M-x try` and yank an URL
into the minibuffer, and the file will be evaluated.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-23 19:46:41 +00:00
(defun try (url)
"Takes an URL to a .el-file, and evaluates it."
(interactive (list (read-from-minibuffer "url: ")))
(with-current-buffer (url-retrieve-synchronously url)
(eval-region (search-forward-regexp "^$") (point-max))))
```
2014-10-25 15:42:53 +00:00
## Advice<a id="sec-2-14" name="sec-2-14"></a>
2014-04-06 01:25:47 +00:00
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defadvice eval-last-sexp (around replace-sexp (arg) activate)
"Replace sexp when called with a prefix argument."
(if arg
(let ((pos (point)))
ad-do-it
(goto-char pos)
(backward-kill-sexp)
(forward-sexp))
ad-do-it))
```
2014-06-18 11:55:46 +00:00
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.
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(defadvice load-theme
(before disable-before-load (theme & optional no-confirm no-enable) activate)
(mapc 'disable-theme custom-enabled-themes))
```
2014-10-25 15:42:53 +00:00
## Presentation-mode<a id="sec-2-15" name="sec-2-15"></a>
2014-06-18 11:55:46 +00:00
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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(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))))
2014-04-06 01:25:47 +00:00
```
2014-06-18 11:55:46 +00:00
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` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(require 'face-remap)
(define-globalized-minor-mode
global-text-scale-mode
text-scale-mode
(lambda () (text-scale-mode 1)))
2014-04-06 01:25:47 +00:00
```
2014-10-25 15:42:53 +00:00
# Mode specific<a id="sec-3" name="sec-3"></a>
## Shell<a id="sec-3-1" name="sec-3-1"></a>
I use `shell` whenever i want to use access the command line in Emacs. I
keep a symlink between my `~/.bash_profile` (because I run OS X) and
`~/.emacs_bash` , to make the transition between my standard terminal and
the shell as small as possible. To be able to quickly switch back and
forth between a shell I make use of this little function.
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(defun toggle-shell ()
"Jumps to eshell or back."
(interactive)
(if (string= (buffer-name) "*shell*")
(switch-to-prev-buffer)
(shell)))
```
I'd like the `C-l` to work more like the standard terminal (which works
like running `clear` ), and resolve this by simply removing the
buffer-content. Mind that this is not how `clear` works, it simply adds a
bunch of newlines, and puts the prompt at the top of the window, so it
does not remove anything. In Emacs removing stuff is less of a worry,
since we can always undo!
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(defun clear-shell ()
"Runs `comint-truncate-buffer' with the
`comint-buffer-maximum-size' set to zero."
(interactive)
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer)))
```
Lastly we should bind our functions. The `toggle-shell` should be a
global binding (because we want to be able to switch to a shell from any
buffer), but the `clear-shell` should only affect `shell-mode` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(add-hook 'shell-mode-hook (lambda () (local-set-key (kbd "C-l") 'clear-shell)))
```
## Lisp<a id="sec-3-2" name="sec-3-2"></a>
2014-04-06 01:25:47 +00:00
`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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(dolist (mode '(slime-repl-mode geiser-repl-mode ielm-mode clojure-mode
cider-repl-mode))
2014-04-06 01:25:47 +00:00
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(dolist (mode pretty-lambda-auto-modes)
;; add paredit-mode to all mode-hooks
(add-hook (intern (concat (symbol-name mode) "-hook")) 'paredit-mode))
```
2014-10-25 15:42:53 +00:00
### Emacs Lisp<a id="sec-3-2-1" name="sec-3-2-1"></a>
2014-04-06 01:25:47 +00:00
In `emacs-lisp-mode` we can enable `eldoc-mode` to display information
about a function or a variable in the echo area.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
```
2014-10-25 15:42:53 +00:00
### Common lisp<a id="sec-3-2-2" name="sec-3-2-2"></a>
2014-04-06 01:25:47 +00:00
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
Common Lisp developer. [Quicklisp ](http://www.quicklisp.org/beta/ ) is a library manager for Common Lisp,
and you can install Slime following the instructions from the site along
with this snippet.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(when (file-exists-p "~/.quicklisp/slime-helper.el")
(load (expand-file-name "~/.quicklisp/slime-helper.el")))
2014-04-06 01:25:47 +00:00
```
We can specify what Common Lisp program Slime should use (I use SBCL).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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))
```
2014-10-25 15:42:53 +00:00
More sensible `loop` indentation, borrowed from [simenheg ](https://github.com/simenheg ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
(setq lisp-loop-forms-indentation 6
2014-10-25 15:42:53 +00:00
lisp-simple-loop-indentation 2
lisp-loop-keyword-indentation 6)
```
2014-11-02 11:00:49 +00:00
```emacs-lisp
(define-key slime-repl-mode-map (kbd "C-l") 'slime-repl-clear-buffer)
```
2014-10-25 15:42:53 +00:00
### Scheme<a id="sec-3-2-3" name="sec-3-2-3"></a>
2014-04-06 01:25:47 +00:00
[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,
and specify which scheme-interpreter we prefer.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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))
2014-06-18 11:55:46 +00:00
(eval-after-load "geiser"
'(add-to-list 'geiser-active-implementations 'plt-r5rs)) ;'(racket))
2014-04-06 01:25:47 +00:00
```
2014-10-25 15:42:53 +00:00
## Java and C<a id="sec-3-3" name="sec-3-3"></a>
2014-04-06 01:25:47 +00:00
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` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun c-setup ()
(local-set-key (kbd "C-c C-c") 'compile))
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(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.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun java-setup ()
(abbrev-mode t)
(setq-local compile-command (concat "javac " (buffer-name))))
(add-hook 'java-mode-hook 'java-setup)
```
2014-10-25 15:42:53 +00:00
## Assembler<a id="sec-3-4" name="sec-3-4"></a>
2014-04-06 01:25:47 +00:00
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` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(defun asm-setup ()
(setq comment-start "#")
(local-set-key (kbd "C-c C-c") 'compile))
(add-hook 'asm-mode-hook 'asm-setup)
```
2014-10-25 15:42:53 +00:00
## LaTeX<a id="sec-3-5" name="sec-3-5"></a>
2014-04-06 01:25:47 +00:00
`.tex` -files should be associated with `latex-mode` instead of
`tex-mode` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(add-to-list 'auto-mode-alist '("\\.tex\\'" . latex-mode))
```
I like using the [Minted ](https://code.google.com/p/minted/ ) package for source blocks in LaTeX. To make org
use this we add the following snippet.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(eval-after-load 'org
'(add-to-list 'org-latex-packages-alist '("" "minted")))
2014-04-06 01:25:47 +00:00
(setq org-latex-listings 'minted)
```
Because [Minted ](https://code.google.com/p/minted/ ) uses [Pygments ](http://pygments.org ) (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).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(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)))
2014-04-06 01:25:47 +00:00
2014-06-18 11:55:46 +00:00
(eval-after-load 'tex-mode
'(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape "))
2014-04-06 01:25:47 +00:00
```
2014-10-25 15:42:53 +00:00
## Markdown<a id="sec-3-6" name="sec-3-6"></a>
2014-09-17 22:35:52 +00:00
I sometimes use a specialized markdown format, where inline math-blocks
can be achieved by surrounding a LaTeX formula with `$math$` and
`$/math$` . Writing these out became tedious, so I wrote a small function.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(defun insert-markdown-inline-math-block ()
"Inserts an empty math-block if no region is active, otherwise wrap a
math-block around the region."
(interactive)
(let* ((beg (region-beginning))
(end (region-end))
(body (if (region-active-p) (buffer-substring beg end) "")))
(when (region-active-p)
(delete-region beg end))
(insert (concat "$math$ " body " $/math$"))
(search-backward " $/math$")))
```
Most of my writing in this markup is in Norwegian, so the dictionary is
set accordingly. The markup is also sensitive to line breaks, so
`auto-fill-mode` is disabled. Of course we want to bind our lovely
function to a key!
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-09-17 22:35:52 +00:00
(add-hook 'markdown-mode-hook
(lambda ()
(auto-fill-mode 0)
(ispell-change-dictionary "norsk")
(local-set-key (kbd "C-c b") 'insert-markdown-inline-math-block)) t)
```
2014-10-25 15:42:53 +00:00
## Python<a id="sec-3-7" name="sec-3-7"></a>
2014-04-06 01:25:47 +00:00
2014-10-25 15:42:53 +00:00
## Haskell<a id="sec-3-8" name="sec-3-8"></a>
2014-04-06 01:25:47 +00:00
`haskell-doc-mode` is similar to `eldoc` , it displays documentation in
the echo area. Haskell has several indentation modes - I prefer using
`haskell-indent` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-04-06 01:25:47 +00:00
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
```
2014-10-25 15:42:53 +00:00
## Matlab<a id="sec-3-9" name="sec-3-9"></a>
2014-04-06 01:25:47 +00:00
2014-06-18 11:55:46 +00:00
`Matlab-mode` works pretty good out of the box, but we can do without the
splash screen.
2014-04-06 01:25:47 +00:00
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-06-18 11:55:46 +00:00
(eval-after-load 'matlab
'(add-to-list 'matlab-shell-command-switches "-nosplash"))
2014-04-06 01:25:47 +00:00
```
2014-10-25 15:42:53 +00:00
# Key bindings<a id="sec-4" name="sec-4"></a>
Inspired by [this StackOverflow post ](http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs ) I keep a `custom-bindings-map` that
holds all my custom bindings. This map can be activated by toggling a
simple `minor-mode` that does nothing more than activating the map. This
inhibits other `major-modes` to override these bindings. I keep this at
the end of the init-file to make sure that all functions are actually
defined.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(defvar custom-bindings-map (make-keymap)
"A keymap for custom bindings.")
```
Bindings for [expand-region ](https://github.com/magnars/expand-region.el ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "C-'") 'er/expand-region)
(define-key custom-bindings-map (kbd "C-;") 'er/contract-region)
```
Bindings for [multiple-cursors ](https://github.com/magnars/multiple-cursors.el ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "C-c e") 'mc/edit-lines)
(define-key custom-bindings-map (kbd "C-c a") 'mc/mark-all-like-this)
(define-key custom-bindings-map (kbd "C-c n") 'mc/mark-next-like-this)
```
Bindings for [Magit ](http://magit.github.io ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "C-c m") 'magit-status)
```
Bindings for [ace-jump-mode ](https://github.com/winterTTr/ace-jump-mode ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "C-c SPC") 'ace-jump-mode)
```
Bindings for [Helm ](http://emacs-helm.github.io/helm/ ).
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "C-c h g") 'helm-google-suggest)
```
Bindings for [smex ](https://github.com/nonsequitur/smex ). This overrides the standard `M-x` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "M-x") 'smex)
```
Bindings for `move-text` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "< M-S-up > ") 'move-text-up)
(define-key custom-bindings-map (kbd "< M-S-down > ") 'move-text-down)
```
Bind some native Emacs functions.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "C-j") 'newline-and-indent)
(define-key custom-bindings-map (kbd "C-c s") 'ispell-word)
(define-key custom-bindings-map (kbd "C-c t") 'org-agenda-list)
(define-key custom-bindings-map (kbd "C-x C-r") 'recentf-ido-find-file)
```
Bind the functions defined above.
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-26 23:00:29 +00:00
(define-key custom-bindings-map (kbd "M-,") 'jump-to-previous-like-this)
(define-key custom-bindings-map (kbd "M-.") 'jump-to-next-like-this)
2014-10-25 15:42:53 +00:00
(define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch)
(define-key custom-bindings-map (kbd "C-x t") 'toggle-shell)
(define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines)
(define-key custom-bindings-map (kbd "C-c d") 'duplicate-thing)
(define-key custom-bindings-map (kbd "< C-tab > ") 'tidy)
```
Lastly we need to activate the map by creating and activating the
`minor-mode` .
2014-11-02 11:00:49 +00:00
```emacs-lisp
2014-10-25 15:42:53 +00:00
(define-minor-mode custom-bindings-mode
"A mode that activates custom-bindings."
t nil custom-bindings-map)
```
# License<a id="sec-5" name="sec-5"></a>
2014-10-26 23:00:29 +00:00
My Emacs configurations written in Org mode.
2014-10-25 15:42:53 +00:00
Copyright (c) 2013 - 2014 Lars Tveito
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see < http: / / www . gnu . org / licenses / > .