Skip to content

Instantly share code, notes, and snippets.

@jahfer

jahfer/init.el

Last active Aug 29, 2015
Embed
What would you like to do?
Emacs config
;; Package
;; Managing extensions for Emacs is simplified using =package= which
;; is built in to Emacs 24 and newer. To load downloaded packages we
;; need to initialize =package=.
(require 'package)
(setq package-enable-at-startup nil)
(package-initialize)
;; Packages can be fetched from different mirrors, [[http://melpa.milkbox.net/#/][melpa]] is the largest
;; archive and is well maintained.
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("org" . "http://orgmode.org/elpa/")
("MELPA" . "http://melpa.milkbox.net/packages/")))
;; We can define a predicate that tells us whether or not the newest version
;; of a package is installed.
(defun newest-package-installed-p (package)
"Return true if the newest available PACKAGE is installed."
(when (package-installed-p package)
(let* ((get-desc (if (version< emacs-version "24.4") 'cdr 'cadr))
(builtin-version (assq package package--builtin-versions))
(local-pkg-desc (assq package package-alist))
(newest-pkg-desc (assq package package-archive-contents)))
(cond ((and local-pkg-desc newest-pkg-desc)
(version-list-= (package-desc-version
(funcall get-desc local-pkg-desc))
(package-desc-version
(funcall get-desc newest-pkg-desc))))
((and builtin-version newest-pkg-desc)
(version-list-= builtin-version
(package-desc-version
(funcall get-desc newest-pkg-desc))))))))
;; Let's write a function to install a package if it is not installed or
;; upgrades it if a new version has been released. Here our predicate comes
;; in handy.
(defun upgrade-or-install-package (package)
"Unless the newest available version of PACKAGE is installed
PACKAGE is installed and the current version is deleted."
(unless (newest-package-installed-p package)
(let ((pkg-desc (assq package package-alist)))
(when pkg-desc
(package-delete (cadr pkg-desc)))
(and (assq package package-archive-contents)
(package-install package)))))
;; Also, we will need a function to find all dependencies from a given package.
(defun dependencies (package)
"Returns a list of dependencies from a given PACKAGE."
(let* ((pkg-desc (assq package package-alist))
; (_ (message "Updating dependencies for %s" (symbol-name package)))
(reqs (and pkg-desc (package-desc-reqs (cadr 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
;; second specifies whether one should update during the initialization. The
;; third is a path to a file where a time-stamp is stored in order to check
;; when packages were updated last.
(defvar days-between-updates 7)
(defvar do-package-update-on-init t)
(defvar package-last-update-file
(expand-file-name (concat user-emacs-directory ".package-last-update")))
;; The tricky part is figuring out when packages were last updated. Here is
;; a hacky way of doing it, using [[http://www.gnu.org/software/emacs/manual/html_node/emacs/Time-Stamps.html][time-stamps]]. By adding a time-stamp to the
;; a file, we can determine whether or not to do an update. After that we
;; must run the =time-stamp=-function to update the time-stamp.
(require 'time-stamp)
;; Open the package-last-update-file
(with-temp-file package-last-update-file
(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).
(when (and do-package-update-on-init
(y-or-n-p "Update all packages?"))
(package-refresh-contents)
(let* ((packages
'(auto-compile ; automatically compile Emacs Lisp libraries
cider ; clojure integrated development environment and REPL
clojure-mode ; major mode for clojure code
company ; autocomplete
expand-region ; expand regions via language semantics
flx-ido ; flx integration for ido
ido-vertical-mode ; Makes ido-mode display vertically.
js2-mode ; Improved JavaScript editing mode
magit ; control Git from Emacs
markdown-mode ; Emacs Major mode for Markdown-formatted files.
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.
smex ; M-x interface with Ido-style fuzzy matching.
projectile-rails ; projectile support for rails projectsx
sublime-themes ; Perty themes
color-theme-solarized
htmlize
rust-mode ; Support for rustlang
toml-mode ; TOML support for Cargo
helm
helm-projectile
highlight-chars
smart-mode-line
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))
;; Mac OS X
;; I run this configuration mostly on Mac OS X, so we need a couple of
;; settings to make things work smoothly. In the package section
;; =exec-path-from-shell= is included (only if you're running OS X), this is
;; to include environment-variables from the shell. It makes useing Emacs
;; along with external processes a lot simpler. I also prefer using the
;; =Command=-key as the =Meta=-key.
(when (memq window-system '(mac ns))
(setq mac-option-modifier 'meta
x-select-enable-clipboard t)
(run-with-idle-timer 5 nil 'exec-path-from-shell-initialize)
(setq ns-use-srgb-colorspace t))
;; Some mac-bindings interfere with Emacs bindings.
(when (boundp 'mac-pass-command-to-system)
(setq mac-pass-command-to-system nil))
(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.
(set-language-environment "UTF-8")
;; Call =company-mode= default configuration, which enables =company-mode=
;; globally.
(add-hook 'after-init-hook 'global-company-mode)
;; Modes
;; There are some modes that are enabled by default that I don't find
;; particularly useful. We create a list of these modes, and disable all of
;; these.
(dolist (mode
'(tool-bar-mode ; No toolbars, more room for text.
scroll-bar-mode ; No scroll bars either.
blink-cursor-mode)) ; The blinking cursor gets old.
(funcall mode 0))
;; Let's apply the same technique for enabling modes that are disabled by
;; default.
(dolist (mode
'(;abbrev-mode ; E.g. sopl -> System.out.println.
column-number-mode ; Show column number in mode line.
delete-selection-mode ; Replace selected text.
recentf-mode ; Recently opened files.
show-paren-mode ; Highlight matching parentheses.
desktop-save-mode
helm-mode
global-undo-tree-mode)) ; Undo as a tree.
(funcall mode 1))
;; Set variables for desktop-save to function smoothly
(setq desktop-path '("~/.emacs.d/"))
(setq desktop-dirname user-emacs-directory)
(setq desktop-base-file-name "emacs-desktop")
; remove desktop after it's been read
(add-hook 'desktop-after-read-hook
'(lambda ()
;; desktop-remove clears desktop-dirname
(setq desktop-dirname-tmp desktop-dirname)
(desktop-remove)
(setq desktop-dirname desktop-dirname-tmp)))
(custom-set-variables
'(custom-safe-themes
;; smart-mode-line themes
(quote
("c74e83f8aa4c78a121b52146eadb792c9facc5b1f02c917e3dbb454fca931223"
"a27c00821ccfd5a78b01e4f35dc056706dd9ede09a8b90c6955ae6a390eb1c1e"
"3c83b3676d796422704082049fc38b6966bcad960f896669dfc21a7a37a748fa"
default))))
;; This makes =.md=-files open in =markdown-mode=.
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
;; Visual
;; Change the color-theme to =monokai= (downloaded using =package=).
(load-theme 'brin t)
;; Bump the font size so I can see
(set-face-attribute 'default nil :height 120)
;(set-frame-parameter nil 'background-mode 'dark)
;(enable-theme 'solarized)
;; set tab sanity
(setq c-basic-indent 2)
(setq tab-width 2)
(setq coffee-tab-width 2)
(setq indent-tabs-mode nil)
(require 'highlight-chars)
(add-hook 'font-lock-mode-hook 'hc-highlight-tabs)
;; remove trailing whitespace
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;; always autocomplete
(global-auto-complete-mode t)
;; hook up projectile-rails
(add-hook 'projectile-mode-hook 'projectile-rails-on)
;; Smart Mode Line
(setq rm-whitelist '("company" "Paredit" "Projectile"))
(sml/setup)
;; Ido
;; Interactive do (or =ido-mode=) changes the way you switch buffers and
;; open files/directories. Instead of writing complete file paths and buffer
;; names you can write a part of it and select one from a list of
;; possibilities. Using =ido-vertical-mode= changes the way possibilities
;; are displayed, and =flx-ido-mode= enables fuzzy matching.
(dolist (mode
'(ido-mode ; Interactivly do.
ido-everywhere ; Use Ido for all buffer/file reading.
;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.
(setq ido-file-extensions-order
'(".el" ".scm" ".lisp" ".java" ".c" ".h" ".org" ".tex"))
;; Sometimes when using =ido-switch-buffer= the =*Messages*= buffer get in
;; the way, so we set it to be ignored (it can be accessed using =C-h e=, so
;; there is really no need for it in the buffer list).
(add-to-list 'ido-ignore-buffers "*Messages*")
;; To make =M-x= behave more like =ido-mode= we can use the =smex=
;; package. It needs to be initialized, and we can replace the binding to
;; the standard =execute-extended-command= with =smex=.
(smex-initialize)
(global-set-key (kbd "M-x") 'smex)
;; Interactive functions
;; <<sec:defuns>>
;; To search recent files useing =ido-mode= we add this snippet from
;; [[http://www.emacswiki.org/emacs/CalendarWeekNumbers][EmacsWiki]].
(defun recentf-ido-find-file ()
"Find a recent file using Ido."
(interactive)
(let ((f (ido-completing-read "Choose recent file: " recentf-list nil t)))
(when f
(find-file f))))
(defun whack-whitespace (arg)
"Delete all white space from point to the next word. With prefix ARG
delete across newlines as well. The only danger in this is that you
don't have to actually be at the end of a word to make it work. It
skips over to the next whitespace and then whacks it all to the next
word."
(interactive "P")
(let ((regexp (if arg "[ \t\n]+" "[ \t]+")))
(re-search-forward regexp nil t)
(replace-match "" nil nil)))
;; Projectile configuration
(projectile-global-mode)
(setq projectile-completion-system 'helm)
(helm-projectile-on)
;; Helm configuration
(helm-autoresize-mode t)
(setq helm-M-x-fuzzy-match t) ;; optional fuzzy matching for helm-M-x
;; Lisp
;; =Pretty-lambda= provides a customizable variable
;; =pretty-lambda-auto-modes= that is a list of common lisp modes. Here we
;; can add some extra lisp-modes. We run the =pretty-lambda-for-modes=
;; function to activate =pretty-lambda-mode= in lisp modes.
(dolist (mode '(clojure-mode slime-repl-mode geiser-repl-mode clojurescript-mode))
(add-to-list 'pretty-lambda-auto-modes mode))
(pretty-lambda-for-modes)
;; I use =Paredit= when editing lisp code, we enable this for all lisp-modes
;; in the =pretty-lambda-auto-modes= list.
(dolist (mode pretty-lambda-auto-modes)
;; add paredit-mode to all mode-hooks
(add-hook (intern (concat (symbol-name mode) "-hook")) 'paredit-mode))
(setq shell-file-name "/bin/sh")
;; Increase GC threshold
(setq gc-cons-threshold 20000000)
;; Org mode
(setq org-log-done t)
(setq org-agenda-files (list "~/Dropbox/org/work.org"
"~/Dropbox/org/home.org"))
(setq erc-hide-list '("JOIN" "PART" "QUIT" "CHANGE"))
(require 'ox-publish)
(setq org-publish-project-alist
'(
("org-notes"
:base-directory "~/Dropbox/org/"
:base-extension "org"
:publishing-directory "~/src/org-public/"
:recursive t
:publishing-function org-html-publish-to-html
:headline-levels 2
:auto-preamble t)
("org-static"
:base-directory "~/Dropbox/org/"
:base-extension "css\\|js"
:publishing-directory "~/src/org-public/"
:recursive t
:publishing-function org-publish-attachment)
("org" :components ("org-notes" "org-static"))))
(setq org-agenda-custom-commands
'(("w" "Work list"
((todo "STARTED|REVIEWED"
((org-agenda-overriding-header "In Progress:")))
(todo "WAITING"
((org-agenda-overriding-header "Stalled:")))
(tags-todo "-PRIORITY=\"C\"+TODO=\"TODO\""
((org-agenda-overriding-header "Next:")))
(tags-todo "+PRIORITY=\"C\"+TODO=\"TODO\""
((org-agenda-overriding-header "Someday:"))))
((org-agenda-files '("~/Dropbox/org/work.org"))
(org-agenda-sorting-strategy '(priority-down)))
("~/Dropbox/public/agenda.html"))))
;; Key bindings
;; Bind some native Emacs functions.
(global-set-key (kbd "C-x k") 'kill-this-buffer)
(global-set-key (kbd "C-x C-r") 'recentf-ido-find-file)
;; Bindings for [[http://magit.github.io][Magit]].
(global-set-key (kbd "C-c m") 'magit-status)
;; Bindings for =move-text=.
(global-set-key (kbd "<s-up>") 'move-text-up)
(global-set-key (kbd "<s-down>") 'move-text-down)
;; Bindings for multiple-cursors
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)
;; expand-region
(global-set-key (kbd "C-@") 'er/expand-region)
;; org mode
(global-set-key (kbd "C-c l") 'org-store-link)
(global-set-key (kbd "C-c a") 'org-agenda)
(global-set-key (kbd "C-c c") 'org-capture)
(global-set-key (kbd "C-c C-w") 'whack-whitespace)
;; helm mode
(global-set-key (kbd "C-c h") 'helm-command-prefix)
(global-unset-key (kbd "C-x c"))
(global-set-key (kbd "M-x") 'helm-M-x)
(global-set-key (kbd "M-y") 'helm-show-kill-ring)
(global-set-key (kbd "C-x b") 'helm-mini)
(global-set-key (kbd "C-x C-f") 'helm-find-files)
(global-set-key (kbd "C-x C-r") 'helm-recentf)
(define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action) ; rebind tab to run persistent action
(define-key helm-map (kbd "C-i") 'helm-execute-persistent-action) ; make TAB works in terminal
(define-key helm-map (kbd "C-z") 'helm-select-action) ; list actions using C-z
(when (executable-find "curl")
(setq helm-google-suggest-use-curl-p t))
(setq helm-split-window-in-side-p t ; open helm buffer inside current window, not occupy whole other window
helm-move-to-line-cycle-in-source t ; move to end or beginning of source when reaching top or bottom of source.
helm-ff-search-library-in-sexp t ; search for library in `require' and `declare-function' sexp.
helm-scroll-amount 8 ; scroll 8 lines other window using M-<next>/M-<prior>
helm-ff-file-name-history-use-recentf t)
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.