2015-04-07 19:05:47 +00:00
#+TITLE : Emacs configuration file
#+AUTHOR : Lars Tveito
2014-06-18 11:55:46 +00:00
#+BABEL : :cache yes
2013-12-23 01:52:18 +00:00
#+LATEX_HEADER : \usepackage{parskip}
#+LATEX_HEADER : \usepackage{inconsolata}
2016-01-05 20:35:10 +00:00
#+LATEX_HEADER : \usepackage[utf8]{inputenc}
2015-08-06 15:08:19 +00:00
#+PROPERTY : header-args :tangle yes
2014-06-18 11:55:46 +00:00
2013-12-23 01:52:18 +00:00
* About
2014-09-17 22:35:52 +00:00
This is an Emacs configuration file written in [[http://orgmode.org ][Org mode ]]. 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:
2015-04-08 14:23:26 +00:00
Clone the repo.
2015-04-21 22:10:22 +00:00
#+BEGIN_SRC sh :tangle no
git clone https://github.com/larstvei/dot-emacs
#+END_SRC
2015-04-07 22:54:37 +00:00
2015-04-08 14:23:26 +00:00
Backup your old =~/.emacs.d= (if necessary).
2015-04-21 22:10:22 +00:00
#+BEGIN_SRC sh :tangle no
mv ~/.emacs.d ~ /.emacs.d-bak
#+END_SRC
2015-04-07 22:54:37 +00:00
2015-04-08 14:23:26 +00:00
Backup your old =~/.emacs= -file (if necessary).
2015-04-21 22:10:22 +00:00
#+BEGIN_SRC sh :tangle no
mv ~/.emacs ~ /.emacs-bak
#+END_SRC
2015-04-07 22:54:37 +00:00
2015-04-08 14:23:26 +00:00
And finally
2015-04-21 22:10:22 +00:00
#+BEGIN_SRC sh :tangle no
mv dot-emacs ~/.emacs.d
#+END_SRC
2014-09-17 22:35:52 +00:00
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!
2013-12-23 01:52:18 +00:00
* Configurations
** Meta
2015-04-07 22:13:52 +00:00
All changes to the configuration should be done in =init.org= , *not* in
=init.el= . Any changes in the =init.el= will be overwritten by saving
=init.org= . The =init.el= in this repo should not be tracked by git, and
is replaced the first time Emacs is started (assuming it has been renamed
to =~/.emacs.d= ).
Emacs can't load =.org= -files directly, but =org-mode= provides functions
to extract the code blocks and write them to a file. There are multiple
ways of handling this; like suggested by [[http://emacs.stackexchange.com/questions/3143/can-i-use-org-mode-to-structure-my-emacs-or-other-el-configuration-file ][this StackOverflow post ]], one
could just use =org-babel-load-file= , but I had problems with
byte-compilation. Previously I tracked both the =org.= - and =el.= -files,
but the git commits got a little messy. So here is a new approach.
When this configuration is loaded for the first time, the ~init.el~ is
the file that is loaded. It looks like this:
#+BEGIN_SRC emacs-lisp :tangle no
2015-04-21 22:10:22 +00:00
;; This file replaces itself with the actual configuration at first run.
2015-04-07 22:13:52 +00:00
2015-04-21 22:10:22 +00:00
;; We can't tangle without org!
(require 'org)
;; Open the configuration
(find-file (concat user-emacs-directory "init.org"))
;; tangle it
(org-babel-tangle)
;; load it
(load-file (concat user-emacs-directory "init.el"))
;; finally byte-compile it
(byte-compile-file (concat user-emacs-directory "init.el"))
2015-04-07 22:13:52 +00:00
#+END_SRC
It tangles the org-file, so that this file is overwritten with the actual
configuration.
2015-04-07 22:39:16 +00:00
There is no reason to track the =init.el= that is generated; by running
the following command =git= will not bother tracking it:
#+BEGIN_SRC sh :tangle no
2015-04-21 22:10:22 +00:00
git update-index --assume-unchanged init.el
2015-04-07 22:39:16 +00:00
#+END_SRC
If one wishes to make changes to the repo-version of =init.el= start
tracking again with:
#+BEGIN_SRC sh :tangle no
2015-04-21 22:10:22 +00:00
git update-index --no-assume-unchanged init.el
2015-04-07 22:39:16 +00:00
#+END_SRC
The =init.el= should (after the first run) mirror the source blocks in
the =init.org= . 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).
2013-12-23 01:52:18 +00:00
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-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(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)
(expand-file-name (concat user-emacs-directory "init.org")))
;; Avoid running hooks when tangling.
(let ((prog-mode-hook nil))
(org-babel-tangle)
(byte-compile-file (concat user-emacs-directory "init.el")))))
2013-12-24 02:02:44 +00:00
2015-04-21 22:10:22 +00:00
(add-hook 'after-save-hook 'tangle-init)
2013-12-23 01:52:18 +00:00
#+END_SRC
2014-11-17 00:10:38 +00:00
I'd like to keep a few settings private, so we load a =private.el= if it
exists after the init-file has loaded.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook
'after-init-hook
(lambda ()
(let ((private-file (concat user-emacs-directory "private.el")))
(when (file-exists-p private-file)
(load-file private-file)))))
2014-11-17 00:10:38 +00:00
#+END_SRC
2015-08-06 15:02:56 +00:00
** Packages
2013-12-23 01:52:18 +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.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(require 'cl)
(require 'package)
(package-initialize)
2013-12-23 01:52:18 +00:00
#+END_SRC
Packages can be fetched from different mirrors, [[http://melpa.milkbox.net/#/ ][melpa ]] is the largest
archive and is well maintained.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/ ")
("org" . "http://orgmode.org/elpa/ ")
2016-01-05 20:36:05 +00:00
("melpa" . "https://melpa.org/packages/ ")
("melpa-stable" . "https://stable.melpa.org/packages/ ")))
(add-to-list 'package-pinned-packages '(cider . "melpa-stable") t)
2013-12-23 01:52:18 +00:00
#+END_SRC
2015-04-07 22:00:02 +00:00
The configuration assumes that the packages listed below are
installed. To ensure we install missing packages if they are missing.
2013-12-24 01:09:02 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-09-20 14:35:47 +00:00
(let* ((packages
2015-09-20 18:02:28 +00:00
'(auto-compile ; automatically compile Emacs Lisp libraries
2015-09-20 14:35:47 +00:00
cider ; Clojure Interactive Development Environment
company ; Modular text completion framework
define-word ; display the definition of word at point
diminish ; Diminished modes from modeline
2016-01-05 20:37:34 +00:00
drag-stuff ; Drag stuff around in Emacs
2015-09-20 14:35:47 +00:00
expand-region ; Increase selected region by semantic units
focus ; Dim color of text in surrounding sections
idle-require ; load elisp libraries while Emacs is idle
geiser ; GNU Emacs and Scheme talk to each other
2015-09-20 18:07:06 +00:00
git-gutter-fringe ; Fringe version of git-gutter.el
2016-01-05 20:38:33 +00:00
golden-ratio ; Automatic resizing windows to golden ratio
2015-09-20 14:35:47 +00:00
haskell-mode ; A Haskell editing mode
helm ; Incremental and narrowing framework
helm-company ; Helm interface for company-mode
helm-projectile ; Helm integration for Projectile
helm-swoop ; Efficiently hopping squeezed lines
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
2015-09-29 21:55:08 +00:00
maude-mode ; Emacs mode for the programming language Maude
2015-09-20 14:35:47 +00:00
monokai-theme ; A fruity color theme for Emacs
2016-01-05 20:45:27 +00:00
multiple-cursors ; Multiple cursors for Emacs
2015-09-20 14:35:47 +00:00
olivetti ; Minor mode for a nice writing environment
org ; Outline-based notes management and organizer
paredit ; minor mode for editing parentheses
2016-01-05 20:45:27 +00:00
pdf-tools ; Emacs support library for PDF files
2015-09-20 14:35:47 +00:00
projectile ; Manage and navigate projects in Emacs easily
slime ; Superior Lisp Interaction Mode for Emacs
try)) ; Try out Emacs packages
;; Remove all packages already installed
(packages (remove-if 'package-installed-p packages)))
(when packages
(ignore-errors (package-refresh-contents)
(mapcar 'package-install packages)
;; This package is only relevant for Mac OS X.
(when (memq window-system '(mac ns))
(package-install 'exec-path-from-shell)))))
2014-02-01 02:10:00 +00:00
#+END_SRC
** 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
2015-04-09 19:38:21 +00:00
to include environment-variables from the shell. It makes using Emacs
2014-02-01 02:10:00 +00:00
along with external processes a lot simpler. I also prefer using the
=Command= -key as the =Meta= -key.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(when (memq window-system '(mac ns))
2015-08-06 15:08:36 +00:00
(setq ns-pop-up-frames nil
mac-option-modifier nil
2015-04-21 22:10:22 +00:00
mac-command-modifier 'meta
x-select-enable-clipboard t)
(exec-path-from-shell-initialize))
2013-12-23 01:52:18 +00:00
#+END_SRC
** Require
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.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(require 'idle-require) ; Need in order to use idle-require
2014-06-18 11:55:46 +00:00
2015-04-21 22:10:22 +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)
recentf ; recently opened files
tex-mode)) ; TeX, LaTeX, and SliTeX mode commands
(idle-require feature))
2014-06-18 11:55:46 +00:00
2015-04-21 22:10:22 +00:00
(setq idle-require-idle-delay 5)
(idle-require-mode 1)
2013-12-23 01:52:18 +00:00
#+END_SRC
** Sane defaults
These are what /I/ consider to be saner defaults.
We can set variables to whatever value we'd like using =setq= .
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2016-01-05 20:45:27 +00:00
(setq auto-revert-interval 1 ; Refresh buffers fast
custom-file (make-temp-file "") ; Discard customization's
default-input-method "TeX" ; Use TeX when toggling input method
echo-keystrokes 0.1 ; Show keystrokes asap
inhibit-startup-message t ; No splash screen please
initial-scratch-message nil ; Clean scratch buffer
recentf-max-saved-items 100 ; Show more recent files
ring-bell-function 'ignore ; Quiet
sentence-end-double-space nil) ; No double space
2015-04-21 22:10:22 +00:00
;; Some mac-bindings interfere with Emacs bindings.
(when (boundp 'mac-pass-command-to-system)
(setq mac-pass-command-to-system nil))
2013-12-23 01:52:18 +00:00
#+END_SRC
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-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-08-06 15:13:29 +00:00
(setq-default fill-column 79 ; Maximum line width
truncate-lines t ; Don't fold lines
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
2013-12-23 01:52:18 +00:00
#+END_SRC
2013-12-29 18:19:14 +00:00
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-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(let ((default-directory (concat user-emacs-directory "site-lisp/")))
(when (file-exists-p default-directory)
(setq load-path
(append
(let ((load-path (copy-sequence load-path)))
(normal-top-level-add-subdirs-to-load-path)) load-path))))
2014-01-02 15:32:31 +00:00
#+END_SRC
2013-12-29 18:19:14 +00:00
2013-12-23 01:52:18 +00:00
Answering /yes/ and /no/ to each question from Emacs can be tedious, a
single /y/ or /n/ will suffice.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(fset 'yes-or-no-p 'y-or-n-p)
2013-12-23 01:52:18 +00:00
#+END_SRC
To avoid file system clutter we put all auto saved files in a single
directory.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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.")
2013-12-23 01:52:18 +00:00
2015-04-21 22:10:22 +00:00
;; 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)))
2013-12-23 01:52:18 +00:00
#+END_SRC
Set =utf-8= as preferred coding system.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(set-language-environment "UTF-8")
2013-12-23 01:52:18 +00:00
#+END_SRC
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-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(put 'narrow-to-region 'disabled nil)
2013-12-23 01:52:18 +00:00
#+END_SRC
Automaticly revert =doc-view= -buffers when the file changes on disk.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook 'doc-view-mode-hook 'auto-revert-mode)
2013-12-23 01:52:18 +00:00
#+END_SRC
** 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.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(dolist (mode
2016-01-05 20:45:27 +00:00
'(tool-bar-mode ; No toolbars, more room for text
scroll-bar-mode ; No scroll bars either
blink-cursor-mode)) ; The blinking cursor gets old
2015-04-21 22:10:22 +00:00
(funcall mode 0))
2013-12-23 01:52:18 +00:00
#+END_SRC
Let's apply the same technique for enabling modes that are disabled by
default.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(dolist (mode
2015-09-29 22:07:48 +00:00
'(abbrev-mode ; E.g. sopl -> System.out.println
column-number-mode ; Show column number in mode line
2016-01-05 20:45:27 +00:00
delete-selection-mode ; Replace selected text
dirtrack-mode ; directory tracking in *shell*
2016-01-05 20:37:34 +00:00
drag-stuff-global-mode ; Drag stuff around
2015-09-29 22:07:48 +00:00
global-company-mode ; Auto-completion everywhere
global-git-gutter-mode ; Show changes latest commit
global-prettify-symbols-mode ; Greek letters should look greek
2016-01-05 20:38:33 +00:00
golden-ratio-mode ; Automatic resizing of windows
2016-01-05 20:45:27 +00:00
projectile-global-mode ; Manage and navigate projects
2015-09-29 22:07:48 +00:00
recentf-mode ; Recently opened files
2016-01-05 20:45:27 +00:00
show-paren-mode)) ; Highlight matching parentheses
2015-04-21 22:10:22 +00:00
(funcall mode 1))
2014-06-18 11:55:46 +00:00
2015-04-21 22:10:22 +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-06-18 11:55:46 +00:00
2013-12-23 01:52:18 +00:00
#+END_SRC
** Visual
2014-11-17 00:10:38 +00:00
Change the color-theme to =leuven= .
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(load-theme 'leuven t)
2014-11-17 00:10:38 +00:00
#+END_SRC
=leuven= is my preferred light theme, but =monokai= makes a very nice
dark theme. I want to be able to cycle between these.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun cycle-themes ()
"Returns a function that lets you cycle your themes."
(lexical-let ((themes '#1=(leuven monokai . #1#)))
(lambda ()
(interactive)
;; Rotates the thme cycle and changes the current theme.
(load-theme (car (setq themes (cdr themes))) t))))
2013-12-23 01:52:18 +00:00
#+END_SRC
Use the [[http://www.levien.com/type/myfonts/inconsolata.html ][Inconsolata ]] font if it's installed on the system.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-09-20 14:26:55 +00:00
(cond ((member "Source Code Pro" (font-family-list))
(set-face-attribute 'default nil :font "Source Code Pro-13"))
((member "Inconsolata" (font-family-list))
(set-face-attribute 'default nil :font "Inconsolata-14")))
2014-03-23 01:26:29 +00:00
#+END_SRC
2015-04-10 15:52:36 +00:00
[[http://www.eskimo.com/~seldon/diminish.el ][diminish.el ]] allows you to hide or abbreviate their presence in the
modeline. I rarely look at the modeline to find out what minor-modes are
enabled, so I disable every global minor-mode, and some for lisp editing.
2015-04-10 15:45:35 +00:00
2015-04-10 15:52:36 +00:00
To ensure that the mode is loaded before diminish it, we should use
~with-eval-after-load~ . To avoid typing this multiple times a small macro
is provided.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defmacro safe-diminish (file mode &optional new-name)
`(with-eval-after-load ,file
(diminish ,mode ,new-name)))
2015-04-10 15:52:36 +00:00
2015-04-21 22:10:22 +00:00
(diminish 'auto-fill-function)
(safe-diminish "eldoc" 'eldoc-mode)
(safe-diminish "flyspell" 'flyspell-mode)
(safe-diminish "helm-mode" 'helm-mode)
(safe-diminish "projectile" 'projectile-mode)
(safe-diminish "paredit" 'paredit-mode "()")
2015-04-10 15:52:36 +00:00
#+END_SRC
2015-04-10 15:45:35 +00:00
2015-09-20 18:07:06 +00:00
[[https://github.com/syohex/emacs-git-gutter-fringe ][git-gutter-fringe ]] gives a great visual indication of where you've made
changes since your last commit. There are several packages that performs
this task; the reason I've ended up with =git-gutter-fringe= is that it
reuses the (already present) fringe, saving a tiny bit of screen-estate.
I smuggled some configurations from [[https://github.com/torenord/.emacs.d/ ][torenord ]], providing a cleaner look.
#+BEGIN_SRC emacs-lisp
(require 'git-gutter-fringe)
(dolist (p '((git-gutter:added . "#0c0")
(git-gutter:deleted . "#c00")
(git-gutter:modified . "#c0c")))
(set-face-foreground (car p) (cdr p))
(set-face-background (car p) (cdr p)))
#+END_SRC
2015-09-29 22:07:48 +00:00
New in Emacs 24.4 is the =prettify-symbols-mode= ! It's neat.
#+BEGIN_SRC emacs-lisp
(setq-default prettify-symbols-alist '(("lambda" . ?λ)
("delta" . ?Δ)
("gamma" . ?Γ)
("phi" . ?φ)
("psi" . ?ψ)))
#+END_SRC
2015-09-20 15:00:41 +00:00
** PDF Tools
[[https://github.com/politza/pdf-tools ][PDF Tools ]] makes a huge improvement on the built-in [[http://www.gnu.org/software/emacs/manual/html_node/emacs/Document-View.html ][doc-view-mode ]]; the only
drawback is the =pdf-tools-install= (which has to be executed before the
package can be used) takes a couple of /seconds/ to execute. Instead of
running it at init-time, we'll run it whenever a PDF is opened. Note that
it's only slow on the first run!
#+BEGIN_SRC emacs-lisp
2015-09-29 21:56:54 +00:00
(add-hook 'pdf-tools-enabled-hook 'auto-revert-mode)
2015-09-20 15:00:41 +00:00
(add-to-list 'auto-mode-alist '("\\.pdf\\'" . pdf-tools-install))
#+END_SRC
2015-04-10 15:49:00 +00:00
** Completion
2015-04-10 15:45:35 +00:00
2015-04-10 15:49:00 +00:00
[[https://github.com/auto-complete/auto-complete ][Auto-Complete ]] has been a part of my config for years, but I want to try
out [[http://company-mode.github.io/ ][company-mode ]]. If I code in an environment with good completion, I've
made an habit of trying to /guess/ function-names, and looking at the
completions for the right one. So I want a pretty aggressive completion
system, hence the no delay settings and short prefix length.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(setq company-idle-delay 0
company-echo-delay 0
company-dabbrev-downcase nil
2015-08-06 15:01:15 +00:00
company-minimum-prefix-length 2
2015-04-21 22:10:22 +00:00
company-selection-wrap-around t
company-transformers '(company-sort-by-occurrence
company-sort-by-backend-importance))
2015-04-10 15:49:00 +00:00
#+END_SRC
2014-03-23 13:00:19 +00:00
2015-04-09 19:38:59 +00:00
** Helm
2013-12-23 01:52:18 +00:00
2015-08-06 15:00:40 +00:00
I've been a long time user of ~ido-mode~ along with ~ido-vertical-mode~ , and
don't have any particular complaints. Though I've got a feeling I'm missing
out on something by not using [[https://github.com/emacs-helm/helm ][helm ]]. I will [[http://tuhdo.github.io/helm-intro.html ][this excellent tutorial ]] as a
starting point, along with some of the suggested configurations.
~helm~ has a wonderful feature, being able to grep files by ~C-s~ anywhere,
which is useful. [[http://beyondgrep.com/ ][ack ]] is a great ~grep~ -replacement, and is designed to
search source code, so I want to use that if it's available.
2015-04-16 22:19:38 +00:00
Note that some changes in bindings are located in the key bindings (found
near the end of the configuration).
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(require 'helm)
(require 'helm-config)
2013-12-23 01:52:18 +00:00
2015-04-21 22:10:22 +00:00
(setq helm-split-window-in-side-p t
helm-M-x-fuzzy-match t
helm-buffers-fuzzy-matching t
helm-recentf-fuzzy-match t
helm-move-to-line-cycle-in-source t
projectile-completion-system 'helm)
2013-12-23 01:52:18 +00:00
2015-04-21 22:10:22 +00:00
(when (executable-find "ack")
(setq helm-grep-default-command
"ack -Hn --no-group --no-color %e %p %f"
helm-grep-default-recurse-command
"ack -H --no-group --no-color %e %p %f"))
2015-04-16 22:19:38 +00:00
2015-09-29 22:00:00 +00:00
(set-face-attribute 'helm-selection nil :background "cyan")
2015-04-21 22:10:22 +00:00
(helm-mode 1)
(helm-projectile-on)
(helm-adaptive-mode 1)
2013-12-23 01:52:18 +00:00
#+END_SRC
** Calendar
Define a function to display week numbers in =calender-mode= . The snippet
is from [[http://www.emacswiki.org/emacs/CalendarWeekNumbers ][EmacsWiki ]].
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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))))
2013-12-23 01:52:18 +00:00
#+END_SRC
2014-01-05 12:27:28 +00:00
Evaluate the =calendar-show-week= function.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(calendar-show-week t)
2013-12-23 01:52:18 +00:00
#+END_SRC
Set Monday as the first day of the week, and set my location.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(setq calendar-week-start-day 1
calendar-latitude 60.0
calendar-longitude 10.7
calendar-location-name "Oslo, Norway")
2013-12-23 01:52:18 +00:00
#+END_SRC
2015-04-07 17:15:19 +00:00
** mu4e and offlineimap
2014-01-03 16:07:32 +00:00
2015-04-07 17:15:19 +00:00
I might not be at a computer using my very specific mail-setup, but if my
mail-folder exists, then it's probably safe to load.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defvar load-mail-setup (file-exists-p "~/.ifimail"))
2014-01-03 16:07:32 +00:00
#+END_SRC
2015-04-07 17:15:19 +00:00
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.
*** mu4e
mu4e must be informed where it can find your mail and where the
different folders of interest are located. Some additional mu4e-tweaks
are supplied here as well.
~message-insert-signature~ is an existing Emacs function, that adds your
signature prefixed by a ~"-- "~ at the end of the email, which is a
convention I don't really follow. I redefine it as a function that adds
some newlines and my signature at the top of the email.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(when load-mail-setup
(eval-after-load 'mu4e
'(progn
;; Some basic mu4e settings.
(setq mu4e-maildir "~/.ifimail" ; top-level Maildir
mu4e-sent-folder "/Sent Items" ; folder for sent messages
mu4e-drafts-folder "/INBOX.Drafts" ; unfinished messages
mu4e-trash-folder "/INBOX.Trash" ; trashed 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))
(add-hook 'mu4e-compose-mode-hook
(lambda ()
(auto-fill-mode 0)
(visual-line-mode 1)
(ispell-change-dictionary "norsk")))
(add-hook 'mu4e-view-mode-hook (lambda () (visual-line-mode 1)))
(defun message-insert-signature ()
(goto-char (point-min))
(search-forward-regexp "^$")
(insert "\n\n\n" mu4e-compose-signature))))
(autoload 'mu4e "mu4e" nil t))
2015-04-07 17:15:19 +00:00
#+END_SRC
2013-12-23 01:52:18 +00:00
** Flyspell
Flyspell offers on-the-fly spell checking. We can enable flyspell for all
text-modes with this snippet.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook 'text-mode-hook 'turn-on-flyspell)
2013-12-23 01:52:18 +00:00
#+END_SRC
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
2015-04-09 20:02:41 +00:00
programming modes using the =prog-mode-hook= .
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
2013-12-23 01:52:18 +00:00
#+END_SRC
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 [[http://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html ][closure ]].
2014-01-05 12:13:37 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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-01-05 12:13:37 +00:00
#+END_SRC
2015-04-08 14:23:17 +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-01-05 12:13:37 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defadvice turn-on-flyspell (before 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))))
2014-06-18 11:55:46 +00:00
#+END_SRC
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defadvice flyspell-prog-mode (before 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))))
2014-01-05 12:13:37 +00:00
#+END_SRC
2013-12-23 01:52:18 +00:00
** Org
2015-10-05 21:48:18 +00:00
I use =org-agenda= along with =org-capture= for appointments and such.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2016-01-05 20:45:38 +00:00
(setq org-agenda-files '("~/Dropbox/agenda.org") ; A list of agenda files
org-agenda-default-appointment-duration 120 ; 2 hours appointments
org-capture-templates ; Template for adding tasks
2015-10-05 21:48:18 +00:00
'(("t" "Oppgave" entry (file+headline "~/Dropbox/agenda.org" "Oppgaver")
"** TODO %?" :prepend t)
2016-01-05 20:45:38 +00:00
("m" "Master" entry (file+olp "~/Dropbox/agenda.org" "Oppgaver" "Master")
"*** TODO %?" :prepend t)
2015-10-05 21:48:18 +00:00
("a" "Avtale" entry (file+headline "~/Dropbox/agenda.org" "Avtaler")
"** %?\n SCHEDULED: %T" :prepend t)))
2013-12-23 01:52:18 +00:00
#+END_SRC
When editing org-files with source-blocks, we want the source blocks to
be themed as they would in their native mode.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:07:20 +00:00
(setq org-src-fontify-natively t
org-src-tab-acts-natively t
org-confirm-babel-evaluate nil
org-edit-src-content-indentation 0)
2014-09-17 22:45:42 +00:00
#+END_SRC
This is quite an ugly fix for allowing code markup for expressions like
="this string"= , because the quotation marks causes problems.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
;;(require 'org)
(eval-after-load "org"
'(progn
(setcar (nthcdr 2 org-emphasis-regexp-components) " \t\n,")
(custom-set-variables `(org-emphasis-alist ',org-emphasis-alist))))
2013-12-23 01:52:18 +00:00
#+END_SRC
** Interactive functions
<<sec:defuns >>
2013-12-24 02:02:44 +00:00
2013-12-23 01:52:18 +00:00
=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:27:00 +00:00
function around it to be able to bind it to a key. In Emacs 24.4
2015-04-08 14:23:17 +00:00
=cycle-spacing= was introduced, and it works like =just-one-space= , but
2014-10-25 15:27:00 +00:00
when run in succession it cycles between one, zero and the original
number of spaces.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun cycle-spacing-delete-newlines ()
"Removes whitespace before and after the point."
(interactive)
(if (version< emacs-version "24.4")
(just-one-space -1)
(cycle-spacing -1)))
2014-10-25 15:27:00 +00:00
#+END_SRC
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-10-25 15:27:00 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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."
(let* ((point (point))
(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."
(interactive)
(jump-to-symbol-internal t))
(defun jump-to-next-like-this ()
"Jumps to the next occurrence of the symbol at point."
(interactive)
(jump-to-symbol-internal))
2013-12-23 01:52:18 +00:00
#+END_SRC
2014-10-25 15:27:00 +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= ).
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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."
(interactive)
(if (not (string= (buffer-name) "*scratch* "))
(kill-this-buffer)
(delete-region (point-min) (point-max))
(switch-to-buffer (other-buffer))
(bury-buffer "*scratch* ")))
2013-12-23 01:52:18 +00:00
#+END_SRC
To duplicate either selected text or a line we define this interactive
function.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun duplicate-thing (comment)
"Duplicates the current line, or the region if active. If an argument is
given, the duplicated region will be commented out."
(interactive "P")
(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))
(when comment (comment-region start end)))))
2013-12-23 01:52:18 +00:00
#+END_SRC
To tidy up a buffer we define this function borrowed from [[https://github.com/simenheg ][simenheg ]].
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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)))))
2013-12-23 01:52:18 +00:00
#+END_SRC
** 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.
2013-12-24 02:02:44 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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))
2013-12-23 01:52:18 +00:00
#+END_SRC
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
2015-04-07 19:08:54 +00:00
enabled themes.
2014-01-02 15:32:31 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defadvice load-theme
2015-04-07 19:08:54 +00:00
(before disable-before-load (theme &optional no-confirm no-enable) activate)
2015-04-21 22:10:22 +00:00
(mapc 'disable-theme custom-enabled-themes))
2014-06-18 11:55:46 +00:00
#+END_SRC
** Presentation-mode
2015-04-07 19:28:16 +00:00
When giving talks it's nice to be able to adjust the size of everything
(not just a buffer like ~text-scale-mode~ provides). This is not a
particularly neat solution, but it works OK. It simply
increases/decreases the size of the font. It assumes that your using
Inconsolata with size 14 by default. This should be probably be
generalized (or maybe be substituted by a package if it's out there).
2014-06-18 11:55:46 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun global-scale-default ()
(interactive)
(set-face-attribute 'default nil :font "Inconsolata-14"))
2014-06-18 11:55:46 +00:00
2015-04-21 22:10:22 +00:00
(lexical-let ((size 14))
(defun global-scale-up ()
(interactive)
(set-face-attribute
'default nil
:font (concat "Inconsolata-" (number-to-string (incf size)))))
2014-06-18 11:55:46 +00:00
2015-04-21 22:10:22 +00:00
(defun global-scale-down ()
(interactive)
(set-face-attribute
'default nil
:font (concat "Inconsolata-" (number-to-string (decf size))))))
2014-01-02 15:32:31 +00:00
#+END_SRC
2014-10-25 15:27:00 +00:00
* Mode specific
** Shell
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.
2015-04-07 19:08:54 +00:00
2014-10-25 15:27:00 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun toggle-shell ()
"Jumps to eshell or back."
(interactive)
(if (string= (buffer-name) "*shell* ")
(switch-to-prev-buffer)
(shell)))
2014-10-25 15:27:00 +00:00
#+END_SRC
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!
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun clear-comint ()
"Runs `comint-truncate-buffer' with the
`comint-buffer-maximum-size' set to zero."
(interactive)
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer)))
2014-10-25 15:27:00 +00:00
#+END_SRC
2015-04-07 19:08:54 +00:00
2014-10-25 15:27:00 +00:00
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= .
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook 'comint-mode-hook (lambda () (local-set-key (kbd "C-l") 'clear-comint)))
2014-10-25 15:27:00 +00:00
#+END_SRC
2013-12-23 01:52:18 +00:00
** Lisp
2015-09-29 22:07:48 +00:00
I use =Paredit= when editing lisp code, we enable this for all lisp-modes.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-09-29 22:07:48 +00:00
(dolist (mode '(cider-repl-mode
clojure-mode
ielm-mode
geiser-repl-mode
slime-repl-mode
lisp-mode
emacs-lisp-mode
lisp-interaction-mode
scheme-mode))
2015-04-21 22:10:22 +00:00
;; add paredit-mode to all mode-hooks
(add-hook (intern (concat (symbol-name mode) "-hook")) 'paredit-mode))
2013-12-23 01:52:18 +00:00
#+END_SRC
*** Emacs Lisp
In =emacs-lisp-mode= we can enable =eldoc-mode= to display information
about a function or a variable in the echo area.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
2013-12-23 01:52:18 +00:00
#+END_SRC
*** 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.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun activate-slime-helper ()
2015-09-29 21:57:29 +00:00
(when (file-exists-p "~/.quicklisp/slime-helper.el")
(load (expand-file-name "~/.quicklisp/slime-helper.el"))
2015-04-21 22:10:22 +00:00
(define-key slime-repl-mode-map (kbd "C-l")
'slime-repl-clear-buffer))
(remove-hook 'lisp-mode-hook #'activate-slime-helper))
(add-hook 'lisp-mode-hook #'activate-slime-helper)
2013-12-23 01:52:18 +00:00
#+END_SRC
We can specify what Common Lisp program Slime should use (I use SBCL).
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(setq inferior-lisp-program "sbcl")
2013-12-23 01:52:18 +00:00
#+END_SRC
2014-10-25 15:27:00 +00:00
More sensible =loop= indentation, borrowed from [[https://github.com/simenheg ][simenheg ]].
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(setq lisp-loop-forms-indentation 6
lisp-simple-loop-indentation 2
lisp-loop-keyword-indentation 6)
2014-10-25 15:27:00 +00:00
#+END_SRC
2014-11-02 11:00:49 +00:00
#+BEGIN_SRC emacs-lisp
2014-11-17 00:10:38 +00:00
2014-11-02 11:00:49 +00:00
#+END_SRC
2013-12-23 01:52:18 +00:00
*** Scheme
2013-12-24 02:02:44 +00:00
2013-12-23 01:52:18 +00:00
[[http://www.nongnu.org/geiser/ ][Geiser ]] provides features similar to Slime for Scheme editing. Everything
2014-01-15 10:50:40 +00:00
works pretty much out of the box, we only need to add auto completion,
and specify which scheme-interpreter we prefer.
2013-12-23 01:52:18 +00:00
2015-04-21 22:10:22 +00:00
#+BEGIN_SRC emacs-lisp
(eval-after-load "geiser"
'(setq geiser-active-implementations '(guile)))
#+END_SRC
2013-12-23 01:52:18 +00:00
** 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= .
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun c-setup ()
(local-set-key (kbd "C-c C-c") 'compile))
2014-04-06 01:25:13 +00:00
2015-04-21 22:10:22 +00:00
(add-hook 'c-mode-common-hook 'c-setup)
2013-12-23 01:52:18 +00:00
#+END_SRC
Some statements in Java appear often, and become tedious to write
out. We can use abbrevs to speed this up.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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)))
2013-12-23 01:52:18 +00:00
#+END_SRC
To be able to use the abbrev table defined above, =abbrev-mode= must be
2013-12-24 02:02:44 +00:00
activated.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun java-setup ()
(abbrev-mode t)
(setq-local compile-command (concat "javac " (buffer-name))))
2013-12-24 02:02:44 +00:00
2015-04-21 22:10:22 +00:00
(add-hook 'java-mode-hook 'java-setup)
2013-12-23 01:52:18 +00:00
#+END_SRC
** 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= .
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(defun asm-setup ()
(setq comment-start "#")
(local-set-key (kbd "C-c C-c") 'compile))
2013-12-23 01:52:18 +00:00
2015-04-21 22:10:22 +00:00
(add-hook 'asm-mode-hook 'asm-setup)
2013-12-23 01:52:18 +00:00
#+END_SRC
2015-04-07 19:18:31 +00:00
** LaTeX and org-mode LaTeX export
2013-12-23 01:52:18 +00:00
2013-12-23 02:26:22 +00:00
=.tex= -files should be associated with =latex-mode= instead of
2013-12-23 01:52:18 +00:00
=tex-mode= .
2013-12-24 02:02:44 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-to-list 'auto-mode-alist '("\\.tex\\'" . latex-mode))
2013-12-23 01:52:18 +00:00
#+END_SRC
2015-04-07 19:18:31 +00:00
Use ~biblatex~ for bibliography.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(setq-default bibtex-dialect 'biblatex)
2015-04-07 19:18:31 +00:00
#+END_SRC
2013-12-23 02:26:22 +00:00
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.
2013-12-23 01:52:18 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(eval-after-load 'org
'(add-to-list 'org-latex-packages-alist '("" "minted")))
(setq org-latex-listings 'minted)
2013-12-23 02:26:22 +00:00
#+END_SRC
Because [[https://code.google.com/p/minted/ ][Minted ]] uses [[http://pygments.org ][Pygments ]] (an external process), we must add the
2014-01-07 14:26:58 +00:00
=-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).
2013-12-23 02:26:22 +00:00
2015-04-07 19:18:31 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(eval-after-load 'tex-mode
'(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape "))
2015-04-07 19:18:31 +00:00
#+END_SRC
When exporting from Org to LaTeX, use ~latexmk~ for compilation.
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(eval-after-load 'ox-latex
'(setq org-latex-pdf-process
'("latexmk -pdflatex='pdflatex -shell-escape -interaction nonstopmode' -pdf -f %f")))
2015-04-07 19:18:31 +00:00
#+END_SRC
2014-04-06 01:25:13 +00:00
2015-04-07 19:18:31 +00:00
For my thesis, I need to use our university's LaTeX class, this snippet
makes that class available.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(eval-after-load "ox-latex"
'(progn
(add-to-list 'org-latex-classes
'("ifimaster"
"\\documentclass{ifimaster}
[DEFAULT-PACKAGES]
[PACKAGES]
[EXTRA]
\\usepackage{babel,csquotes,ifimasterforside,url,varioref}"
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
(custom-set-variables '(org-export-allow-bind-keywords t))))
2013-12-23 01:52:18 +00:00
#+END_SRC
2014-09-17 22:35:52 +00:00
** Markdown
2015-04-07 23:06:46 +00:00
This makes =.md= -files open in =markdown-mode= .
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
2015-04-07 23:06:46 +00:00
#+END_SRC
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.
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +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$")))
2014-09-17 22:35:52 +00:00
#+END_SRC
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!
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook 'markdown-mode-hook
(lambda ()
(auto-fill-mode 0)
2015-08-06 15:01:43 +00:00
(visual-line-mode 1)
2015-04-21 22:10:22 +00:00
(ispell-change-dictionary "norsk")
(local-set-key (kbd "C-c b") 'insert-markdown-inline-math-block)) t)
2014-09-17 22:35:52 +00:00
#+END_SRC
2013-12-23 01:52:18 +00:00
** Python
2014-10-25 15:27:00 +00:00
# [[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.
2013-12-23 01:52:18 +00:00
2014-10-25 15:27:00 +00:00
# #+BEGIN_SRC emacs-lisp
# ;; (setq jedi:server-command
# ;; (cons "python3" (cdr jedi:server-command))
# ;; python-shell-interpreter "python3")
# (add-hook 'python-mode-hook 'jedi:setup)
# (setq jedi:complete-on-dot t)
# (add-hook 'python-mode-hook 'jedi:ac-setup)
# #+END_SRC
2013-12-23 01:52:18 +00:00
** 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= .
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
2013-12-23 01:52:18 +00:00
#+END_SRC
2014-01-03 16:07:32 +00:00
2016-01-05 20:47:09 +00:00
** Maude
Use =---= for comments in Maude.
#+BEGIN_SRC emacs-lisp
(add-hook 'maude-mode-hook
(lambda ()
(setq-local comment-start "---")))
#+END_SRC
2014-01-03 16:07:32 +00:00
** Matlab
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-01-03 16:07:32 +00:00
2014-04-10 23:58:40 +00:00
#+BEGIN_SRC emacs-lisp
2015-04-21 22:10:22 +00:00
(eval-after-load 'matlab
'(add-to-list 'matlab-shell-command-switches "-nosplash"))
2014-01-03 16:07:32 +00:00
#+END_SRC
2014-10-25 15:27:00 +00:00
* Key bindings
2015-04-21 22:10:22 +00:00
Inspired by [[http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs ][this StackOverflow post ]] 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.
#+BEGIN_SRC emacs-lisp
(defvar custom-bindings-map (make-keymap)
"A keymap for custom bindings.")
#+END_SRC
2015-08-06 15:02:56 +00:00
Bindings for [[https://github.com/abo-abo/define-word ][define-word ]].
#+BEGIN_SRC emacs-lisp
(define-key custom-bindings-map (kbd "C-c D") 'define-word-at-point)
#+END_SRC
2015-04-21 22:10:22 +00:00
Bindings for [[https://github.com/magnars/expand-region.el ][expand-region ]].
#+BEGIN_SRC emacs-lisp
2015-10-05 21:49:31 +00:00
(define-key custom-bindings-map (kbd "C->") 'er/expand-region)
(define-key custom-bindings-map (kbd "C-<") 'er/contract-region)
2015-04-21 22:10:22 +00:00
#+END_SRC
Bindings for [[https://github.com/magnars/multiple-cursors.el ][multiple-cursors ]].
#+BEGIN_SRC emacs-lisp
(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)
#+END_SRC
Bindings for [[http://magit.github.io ][Magit ]].
#+BEGIN_SRC emacs-lisp
(define-key custom-bindings-map (kbd "C-c m") 'magit-status)
#+END_SRC
Bindings for [[http://company-mode.github.io/ ][company-mode ]].
#+BEGIN_SRC emacs-lisp
(define-key company-active-map (kbd "C-d") 'company-show-doc-buffer)
(define-key company-active-map (kbd "C-n") 'company-select-next)
(define-key company-active-map (kbd "C-p") 'company-select-previous)
(define-key company-active-map (kbd "<tab >") 'company-complete)
(define-key company-mode-map (kbd "C-:") 'helm-company)
(define-key company-active-map (kbd "C-:") 'helm-company)
#+END_SRC
Bindings for [[http://emacs-helm.github.io/helm/ ][Helm ]].
#+BEGIN_SRC emacs-lisp
(define-key custom-bindings-map (kbd "C-c h") 'helm-command-prefix)
(define-key custom-bindings-map (kbd "M-x") 'helm-M-x)
(define-key custom-bindings-map (kbd "M-y") 'helm-show-kill-ring)
(define-key custom-bindings-map (kbd "C-x b") 'helm-mini)
(define-key custom-bindings-map (kbd "C-x C-f") 'helm-find-files)
(define-key custom-bindings-map (kbd "C-c h o") 'helm-occur)
(define-key custom-bindings-map (kbd "C-c h g") 'helm-google-suggest)
2015-09-20 14:40:10 +00:00
(define-key custom-bindings-map (kbd "M-i") 'helm-swoop)
(define-key custom-bindings-map (kbd "M-I") 'helm-multi-swoop-all)
2015-04-21 22:10:22 +00:00
(define-key helm-map (kbd "<tab >") 'helm-execute-persistent-action)
(define-key helm-map (kbd "C-i") 'helm-execute-persistent-action)
(define-key helm-map (kbd "C-z") 'helm-select-action)
#+END_SRC
Bind some native Emacs functions.
#+BEGIN_SRC emacs-lisp
(define-key custom-bindings-map (kbd "C-j") 'newline-and-indent)
(define-key custom-bindings-map (kbd "C-c s") 'ispell-word)
2015-10-05 21:48:18 +00:00
(define-key custom-bindings-map (kbd "C-c c") 'org-capture)
2015-04-21 22:10:22 +00:00
(define-key custom-bindings-map (kbd "C-x m") 'mu4e)
2015-10-05 21:48:18 +00:00
(define-key custom-bindings-map (kbd "C-c t")
(lambda () (interactive) (org-agenda nil "n")))
2015-04-21 22:10:22 +00:00
#+END_SRC
Bind the functions defined [[sec:defuns ][above ]].
#+BEGIN_SRC emacs-lisp
(define-key global-map (kbd "M-p") 'jump-to-previous-like-this)
(define-key global-map (kbd "M-n") 'jump-to-next-like-this)
(define-key custom-bindings-map (kbd "M-,") 'jump-to-previous-like-this)
(define-key custom-bindings-map (kbd "M-.") 'jump-to-next-like-this)
(define-key custom-bindings-map (kbd "C-c .") (cycle-themes))
(define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch)
(define-key custom-bindings-map (kbd "C-c C-0") 'global-scale-default)
(define-key custom-bindings-map (kbd "C-c C-=") 'global-scale-up)
(define-key custom-bindings-map (kbd "C-c C--") 'global-scale-down)
(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)
#+END_SRC
Lastly we need to activate the map by creating and activating the
=minor-mode= .
#+BEGIN_SRC emacs-lisp
(define-minor-mode custom-bindings-mode
"A mode that activates custom-bindings."
t nil custom-bindings-map)
#+END_SRC
2014-10-25 15:42:53 +00:00
* License
2014-10-26 23:00:29 +00:00
My Emacs configurations written in Org mode.
2015-04-07 22:57:56 +00:00
Copyright (c) 2013 - 2015 Lars Tveito
2015-04-07 19:08:54 +00:00
2014-10-25 15:42:53 +00:00
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.
2015-04-07 19:08:54 +00:00
2014-10-25 15:42:53 +00:00
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.
2015-04-07 19:08:54 +00:00
2014-10-25 15:42:53 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/ >.