Skip to content

Latest commit

 

History

History
1546 lines (1207 loc) · 44 KB

emacs.org

File metadata and controls

1546 lines (1207 loc) · 44 KB

My emacs configuration file

images/emacs.png

Set up load path

The variable load-path lists all the directories where Emacs should look for Elisp files.

(setq load-path
      (append
       (list
        (expand-file-name "~/.emacs.d")
        (expand-file-name "~/.emacs.d/elisp"))
       load-path))

Common lisp primitives.

(require 'cl)

Packages

Following code adapted from emacs-prelude - package-management-in-emacs (The Good, the Bad and the Ugly).

Activate all the packages.

(require 'package)
(package-initialize)

List of repositories containing packages.

  1. melpa repo - contains packages updated daily.
  2. melpa-stable repo - [alternative to Marmalade] It offers stable packages built automatically from git tags.
  3. org repo - Org’s package repository.
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/"))
(add-to-list 'package-archives
             '("melpa-stable" . "http://melpa-stable.milkbox.net/packages/"))
(add-to-list 'package-archives
             '("org" . "http://orgmode.org/elpa/"))

List of packages used in this configuration.

(setq required-packages
      (list
       'ac-slime ; auto-complete plugin for Slime.
       'ace-window ; GNU Emacs package for selecting a window to switch to
       'ace-jump-mode ; a quick cursor jump mode for emacs
       'auto-complete ; auto-completion extension for GNU Emacs.
       'auto-complete-clang ; nice C & C++ autocomplete for Emacs.
       'autopair ; automagically pair braces and quotes.
       'aurel ; Search and download AUR packages from Emacs.
       'deft ; mode for quickly browsing, filtering, and editing directories of plain text notes.
       'dired-details ; hide/show the details of each file or directory in DiredMode.
       'emms ; Emacs multimedia system.
       'elfeed ; An Emacs web feeds client.
       'elscreen ; GNU Emacs window session manager.
       'flycheck ; fly syntax checking for GNU Emacs.
       'flx-ido ; Fuzzy matching for Emacs ... a la Sublime Text.
       'guru-mode ; The Emacs guru way
       'htmlize ; Convert buffer text and decorations to HTML.
       'ido-vertical-mode ; makes ido-mode display vertically.
       'js2-mode ; javascript-mode for emacs.
       'magit ; Emacs mode for Git.
       'markdown-mode ; Emacs mode for Markdown-formatted files.
       'multi-term ; managing multiple terminal buffers in Emacs.
       'multiple-cursors ; Multiple cursors for emacs.
       'org ; an Emacs Mode for Notes, Planning, and Authoring.
       'org-plus-contrib ; contrib files for org.
       'paredit ; minor mode for editing parentheses.
       'pkgbuild-mode ; Major mode for editing PKGBUILD files.
       'rainbow-mode ; Colorize color names in buffers.
       'rainbow-delimiters ; rainbow delimiters mode.
       'recentf ; is a minor mode that builds a list of recently opened files.
       'slime ; Emacs mode for Common Lisp development.
       'smex ; M-x enhancement for Emacs. Built on top of IDO.
       'web-mode ; web template editing mode for emacs.
       'xkcd ; Read xkcd from emacs.
       'volatile-highlights ; highlights changes to the buffer.
       'zenburn-theme ; The Zenburn colour theme ported to Emacs.
       ))

Check if all packages are installed.

if not all packages are installed, check one by one and install the missing ones.

(defun packages-installed-p ()
  (loop for p in required-packages
        when (not (package-installed-p p)) do (return nil)
        finally (return t)))

;; if not all packages are installed, check one by one and install the missing ones.
(unless (packages-installed-p)
  ;; check for new packages (package versions)
  (message "%s" "Emacs is now refreshing its package database...")
  (package-refresh-contents)
  (message "%s" " done.")
  ;; install the missing packages
  (dolist (p required-packages)
    (when (not (package-installed-p p))
      (package-install p))))

Visual appearance

I turn off mouse interface early in startup to avoid momentary display.

(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'blink-cursor-mode) (blink-cursor-mode -1))
(if (fboundp 'use-file-dialog) (setq use-file-dialog nil))
(if (fboundp 'use-dialog-box) (setq use-dialog-box nil))

For the most part I prefer to disable Emacs’ fringe.

(fringe-mode '(0 . 0))

I have simple function to Enable|Disable fringe.

(defun enable-fringe ()
  (interactive)
  (fringe-mode '(nil . nil) ))

(defun disable-fringe ()
  (interactive)
  (fringe-mode '(0 . 0) ))

Set font for all windows.

(add-to-list 'default-frame-alist
                '(font . "DejaVu Sans Mono-10"))

Load theme zenburn.

(load-theme 'zenburn t)

Make cursor a vertical bar.

(setq-default cursor-type 'bar)

Highlight the current line

(global-hl-line-mode +1)

Hide mouse cursor while typing.

(setq make-pointer-invisible t)

Highlight matching parentheses when the point is on them.

(show-paren-mode t)
(setq show-paren-delay 0)

Volatile-highlight. Highlight the latest changes in the buffer (like text inserted from: yank, undo, etc.) until the next command is run.

(when (require 'volatile-highlights nil 'noerror)
  (volatile-highlights-mode t))

Do not break lines.

(set-default 'truncate-lines t)

Defaults

Prompt Behavior

Use y/n instead of yes/no in confirmation dialogs.

(fset 'yes-or-no-p 'y-or-n-p)

Settings

(setq initial-scratch-message ";; scratch buffer created -- Happy Hacking ivo!!"
      inhibit-startup-message t       ; No splash screen please.
      visible-bell t                  ; blink instead of beep
      auto-image-file-mode 1          ; open graphic files such as JPEG/PNG format files.
      doc-view-continuous t           ; At page edge goto next/previous.
      frame-title-format '(buffer-file-name "%f" ("%b")) ; show the full file name in the title
      echo-keystrokes 0.1)            ; Show keystrokes in progress.

Modeline settings

(line-number-mode t)
(column-number-mode t)
(size-indication-mode t)

Clipboard

Enable copy/paste from emacs to other apps.

(setq
 interprogram-cut-function 'x-select-text
 interprogram-paste-function 'x-selection-value
 save-interprogram-paste-before-kill t
 select-active-regions t
 x-select-enable-clipboard t
 x-select-enable-primary t)

Files

Set UTF Encoding.

Make sure UTF-8 is used everywhere.

(set-language-environment 'UTF-8)
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
(setq default-buffer-file-coding-system 'utf-8-unix)

Opening Large Files - Warn when opening files bigger than 100MB.

(setq large-file-warning-threshold 100000000)

Enable global-auto-revert-mode

(global-auto-revert-mode 1)
(setq auto-revert-verbose nil)
(setq global-auto-revert-non-file-buffers t)

Remove ^M symbols

(add-hook 'comint-output-filter-functions
          'comint-strip-ctrl-m)

Keep Backup and Auto-save Files Out of the Way

Store all backup and autosave files in the tmp/ dir.

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))

Move files to trash when deleting.

(setq delete-by-moving-to-trash t)

Automatically make scripts starting with #! executable.

(add-hook 'after-save-hook
          'executable-make-buffer-file-executable-if-script-p)

Lines.

(setq require-final-newline t
      kill-whole-line t
      indicate-empty-lines t)

Every buffer would be cleaned up before it’s saved.

(add-hook 'before-save-hook 'whitespace-cleanup)

Whitespaces.

(require 'whitespace)
(global-whitespace-mode)
(setq whitespace-line-column 80) ;; limit line length
(setq whitespace-style '(face tabs empty trailing lines-tail))
  1. Always use spaces for indentation
  2. Default to 2-space tabs
(setq indent-tabs-mode nil)
(setq-default tab-width 2)

Start server

(require 'server)
(unless (server-running-p)
  (server-start))

Modes

Autocomplete

Turn on auto complete.

(require 'auto-complete-config)
(ac-config-default)
(setq ac-auto-show-menu 0.5)
(setq ac-quick-help-height 50)
(setq ac-quick-help-delay 1)
(setq ac-use-fuzzy t)
(setq ac-disable-faces nil)
(setq ac-quick-help-prefer-x nil)

(define-key ac-completing-map (kbd "C-n") 'ac-next)
(define-key ac-completing-map (kbd "C-p") 'ac-previous)

Flycheck mode

Modern on the fly syntax checking for GNU Emacs http://flycheck.rtfd.org/

The modes where flycheck should be enabled.

(add-hook 'php-mode-hook 'flycheck-mode)
(add-hook 'sh-mode-hook 'flycheck-mode)
(add-hook 'json-mode-hook 'flycheck-mode)
(add-hook 'nxml-mode-hook 'flycheck-mode)
(add-hook 'emacs-lisp 'flycheck-mode)
(add-hook 'ruby-mode-hook 'flycheck-mode)
(add-hook 'sass 'flycheck-mode)
(add-hook 'scss 'flycheck-mode)

Web-mode

web-mode.el is an emacs major mode for editing web templates aka HTML files embedding parts (CSS/JavaScript) and blocks (pre rendered by client/server side engines).

Github: https://github.com/fxbois/web-mode

Enable web-mode for html buffers.

(require 'web-mode)
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))

Markdown-mode

I love org-mode, but lots of other systems use markdown, github wiki pages being a very good example.

(require 'markdown-mode)
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))

Rainbow mode

Useful for editing themes and CSS http://julien.danjou.info/rainbow-mode.html

(add-hook 'css-mode-hook 'rainbow-mode)

Uniquify

Making buffer names unique.

When several buffers have the same name, make the name uniqe by including part of path in name.

(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward)

Org mode

Enable org-mode for .org, .org_archive and .txt files by default.

(add-to-list 'auto-mode-alist '("\\.\\(org\\|org_archive\\)$" . org-mode))

Notes / Tasks / TODOs

(setq org-todo-keywords
      '((sequence "TODO(t)" "WAIT(w@/!)" "|" "DONE(d!)" "CANCELED(c@)")))

Set global keys for the most important org commands.

(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb)

Org Publishing

The brilliance of org-mode is the ability to publish your notes as HTML files into a web server.

(require 'htmlize)
(setq org-html-htmlize-output-type 'inline-css)
(setq org-html-validation-link nil)

(setq org-export-default-language "bg"
      org-export-html-extension "html"
)

(setq org-publish-project-alist
      '(("blog"
         :components ("blog-content" "blog-static"))
        ("blog-content"
         ;; Directory for source files in org format
         :base-directory "~/Dropbox/blog/org/"
         :base-extension "org"
         ;; Path to exported HTML files
         :publishing-directory "~/Dropbox/blog/public_html/"
         ;;:publishing-function org-publish-org-to-html
         :publishing-function org-html-publish-to-html
         :recursive t
         :htmlized-source t
         :headline-levels 4
;; :html-head "<link rel='stylesheet' href='css/blog.css' />"
         :auto-preamble t
         :auto-sitemap t
         :sitemap-title "Sitemap"
         :sitemap-filename "sitemap.org"
         :sitemap-sort-files anti-chronologically
         ;;:sitemap-file-entry-format "%t (%d)"
         )
        ;; Path to Static files
        ("blog-static"
         :base-directory "~/Dropbox/blog/files/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
         :publishing-directory "~/Dropbox/blog/public_html/files/"
         :recursive t
         :publishing-function org-publish-attachment
         )))

Org babel

Enable syntax highlighting in src blocks.

(setq-default org-src-fontify-natively t)

List of languages that may be evaluated in Org documents.

(org-babel-do-load-languages
 'org-babel-load-languages
 '(
   (C .t)
   (sh . t)
   (python . t)
   (R . t)
   (ruby . t)
   (emacs-lisp . t)
   (lisp .t)
   (scheme . t)
   (haskell . t)
   (perl . t)
   (js . t)
   ))

Slime

SLIME, the Superior Lisp Interaction Mode for Emacs, is an Emacs mode for developing Common Lisp applications.

(require 'ac-slime)
(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))

Js2-mode

Improved JavaScript editing mode for GNU Emacs.

Github: https://github.com/mooz/js2-mode

(autoload 'js2-mode "js2-mode" nil t)

(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))
(add-to-list 'auto-mode-alist '("\\.json$" . js2-mode))
(add-hook 'js2-mode-hook 'flycheck-mode)

cc-mode

;; c++
(add-hook 'c++-mode-hook
          '(lambda()
             (setq indent-tabs-mode nil)
             (c-set-style "cc-mode")
             (setq c-indent-level 4)
             (setq c-tab-width 4)
             (setq tab-width 4)
             (setq c-basic-offset tab-width)))
;; c
(add-hook 'c-mode-hook
          '(lambda()
             (setq indent-tabs-mode nil)
             (c-set-style "k&r") ;; What Kernighan and Ritchie, the authors of C used in their book
             (setq c-indent-level 4)
             (setq c-tab-width 4)
             (setq tab-width 4)
             (setq c-basic-indent 2)
             (setq c-basic-offset tab-width)))

(define-key c-mode-base-map (kbd "RET") 'newline-and-indent)

(require 'autopair)
(autopair-global-mode 1)
;; tells autopair to automatically wrap the selection region with the delimiters you’re trying to insert.
(setq autopair-autowrap t)

(require 'auto-complete-clang)
(define-key c++-mode-map (kbd "C-S-<return>") 'ac-complete-clang)

Dired mode

It shows a directory (folder) listing that you can use to perform various operations on files and subdirectories in the directory.

(require 'dired)

;; refresh buffers
(setq-default dired-auto-revert-buffer t)
(setq global-auto-revert-non-file-buffers t)
(setq auto-revert-verbose nil)

;; listing options
(setq ired-listing-switches "-alhv --group-directories-first")

;; Move files between split panes
(setq dired-dwim-target t)

;; Delete or copy a whole directory
;;(setq dired-recursive-copies 'always) ; Always means no asking
;;(setq dired-recursive-deletes 'top) ; Top means ask once for top dir only

;; No confirmation on file delete - clever hack
;;(setq dired-deletion-confirmer '(lambda (x) t))

;; hide uninteresting files, such as backup files and AutoSave files
(setq-default dired-omit-mode t
dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$\\|^\\.")

Dired-detailts

Hide or show the file and directory details in a Dired listing, to save space and clutter.

(require 'dired-details)
(setq-default dired-details-hidden-string "..> ")
(define-key dired-mode-map (kbd "TAB") 'dired-details-toggle)

Dired+

Enable some really cool extensions like C-x C-j(dired-jump)

Jump from file to containing directory.

(require 'dired-x)
(global-set-key (kbd "C-x C-j") 'dired-jump) (autoload 'dired-jump "dired")

Ido mode

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.

(require 'ido-vertical-mode)
(require 'flx-ido)

(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))

(setq ido-vertical-define-keys 'C-n-C-p-up-down)

Recenf

Recentf is a minor mode that builds a list of recently opened files. This list is automatically saved across Emacs sessions.

Enable recentf

(require 'recentf)
(recentf-mode 1)
(setq recentf-max-saved-items 50)

Find a recent file using Ido.

(defun ido-recentf-open ()
  "Use `ido-completing-read' to \\[find-file] a recent file"
  (interactive)
  (if (find-file (ido-completing-read "Find recent file: " recentf-list))
      (message "Opening file...")
    (message "Aborting")))

(global-set-key (kbd "C-c f") 'ido-recentf-open)

Aurel

Search, vote and download AUR packages from Emacs.

Github: https://github.com/alezost/aurel

(require 'aurel)

;;; autoloads for the interactive functions.
(autoload 'aurel-package-info "aurel" nil t)
(autoload 'aurel-package-search "aurel" nil t)
(autoload 'aurel-maintainer-search "aurel" nil t)
(autoload 'aurel-installed-packages "aurel" nil t)

;;; directory where the packages will be downloaded.
(setq aurel-download-directory "~/abs")

(global-set-key [f6] 'aurel-package-search)

Pkgbuild-mode

Major mode for editing PKGBUILD files.

Github: https://github.com/juergenhoetzel/pkgbuild-mode

(require 'pkgbuild-mode)

;; enable autoloading of pkgbuild-mode and auto-recognition of "PKGBUILD" files:
(autoload 'pkgbuild-mode "pkgbuild-mode.el" "PKGBUILD mode." t)
(setq auto-mode-alist (append '(("/PKGBUILD$" . pkgbuild-mode))
                              auto-mode-alist))

Guru-mode

Guru mode disables some common keybindings and suggests the use of the established Emacs alternatives instead.

Enable guru-mode globally.

(require 'guru-mode)
(guru-global-mode +1)

Github: https://github.com/bbatsov/guru-mode

Smex

A smart M-x enhancement for Emacs.

Built on top of Ido, it provides a convenient interface to your recently and most frequently used commands.

(require 'smex)
(smex-initialize)
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
;; This is your old M-x.
(global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)

Github: https://github.com/nonsequitur/smex

Multiple Cursors

Multiple cursors, for editing multiple lines.

(require '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)

Github: https://github.com/magnars/multiple-cursors.el

Magit

Magit is an emacs mode for interacting with the Git version control system.

Magit-status shortcut.

(require 'magit)
(global-set-key [f5] 'magit-status)

Github: https://github.com/magit/magit

Ace-window

Quickly switch windows using ace-jump-mode.

(require 'ace-window)
(global-set-key (kbd "C-x o") 'other-window)
(global-set-key [remap other-window] 'ace-window)
(setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))

Github: https://github.com/abo-abo/ace-window

Ace-jump-mode

a quick cursor jump mode for emacs.

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

Github: https://github.com/winterTTr/ace-jump-mode

Conkeror

Every fan of Emacs should use Conkeror. No exceptions.

Open URLs from GNU Emacs in Conkeror in a new buffer.

(setq browse-url-browser-function 'browse-url-generic
      browse-url-generic-program "conkeror"
      ido-handle-duplicate-virtual-buffers 2)

Dired function to view a file in a web browser.

(defun my-dired-browser-find-file ()
  "Dired function to view a file in a web browser"
  (interactive)
  (browse-url (browse-url-file-url (dired-get-filename))))

;; Bind a Key in Emacs's Dired-Mode to View a File in the Default Browser
(add-hook 'dired-mode-hook
          (lambda ()
            (define-key dired-mode-map "b" 'my-dired-browser-find-file)))

Edit conkeror-rc as Js-mode.

(add-to-list 'auto-mode-alist '(".conkerorrc" . js-mode))

Deft

Deft is an Emacs mode for quickly browsing, filtering, and editing directories of plain text notes.

(when (require 'deft nil 'noerror)
  (setq
     deft-use-filename-as-title t
     deft-extension "org"
     deft-directory "~/Dropbox/Enotes/"
     deft-text-mode 'org-mode))

(global-set-key [f8] 'deft)

Elfeed

Elfeed is an extensible web feed reader for Emacs, supporting both Atom and RSS.

(require 'elfeed)

(setq-default elfeed-search-filter "-junk @1-week-ago +unread")

;;; My Feed list
(setq elfeed-feeds
      '(("http://rss.gmane.org/gmane.comp.misc.suckless" dwm)
        ("http://rss.gmane.org/gmane.comp.window-managers.ratpoison.devel" gmane ratpoison)
        ("http://rss.gmane.org/gmane.comp.window-managers.stumpwm.devel" gmane stumpwm)
        ("http://onethingwell.org/rss" software)
        ("https://aur.archlinux.org/rss" archlunux software)
        ("http://news.ycombinator.com/rss" news)
        ("http://nullprogram.com/feed/" emacs)
        ("http://planet.emacsen.org/atom.xml" emacs )
        ("http://www.masteringemacs.org/feed" emacs)
        ("http://feeds.sachachua.com/sachac" emacs)
        ("http://emacsrocks.com/atom.xml" emacs)
        ("http://emacs-fu.blogspot.com/feeds/posts/default" emacs)))

;; global binding for elfeed
(global-set-key [f7] 'elfeed)

Show selected title in Ratpoison.

(defun rat-message (message)
  "Show MSG in Ratpoison"
  (call-process "ratpoison" nil 0 nil "-c" (concat "echo " (or message))))

(defun elfeed-ratpoison-message ()
  (interactive)
  (let ((entry (elfeed-search-selected :single)))
    (rat-message (elfeed-entry-title entry))))

(define-key elfeed-search-mode-map "x" #'elfeed-ratpoison-message)

Emms

EMMS is the Emacs Multimedia System.

Website: https://www.gnu.org/software/emms/

(require 'emms-setup)
(require 'emms-streams)
(require 'emms-info)
(require 'emms-info-mp3info)
(require 'emms-browser)
(emms-standard)
(emms-default-players)

;; When asked for emms-play-directory, always start from this one
(setq emms-source-file-default-directory "/home/ivo/Music/")
(add-to-list 'emms-info-functions 'emms-info-mp3info)

;; Show the current track each time EMMS
;; starts to play a track with "NP : "
(add-hook 'emms-player-started-hook 'emms-show)
        (setq emms-show-format "NP: %s"
              emms-repeat-playlist t)

;; Buffer name
(setq emms-playlist-buffer-name "*EMMS Playlist*"
      emms-playlist-mode-open-playlists t)

Mu4e

Email client http://www.djcbsoftware.nl/code/mu/mu4e.html

(require 'mu4e)

;; default's mu4e
(setq mu4e-maildir "~/Maildir"
      mu4e-drafts-folder "/[Gmail].Drafts"
      mu4e-sent-folder "/[Gmail].Sent Mail"
      mu4e-trash-folder "/[Gmail].Trash"
      ;; allow for updating mail using 'U' in the main view:
      mu4e-get-mail-command "offlineimap"
      ;; Number of seconds between automatic calls to retrieve mail and update the database:
      mu4e-update-interval 60
      ;; allow the mu4e user to disable the confirmation message when quitting mu4e
      mu4e-confirm-quit nil
      ;; Date format
      mu4e-headers-date-format "%d/%b/%Y %H:%M"
      ;; Displaying rich-text messages
      ;;mu4e-html2text-command "html2text -utf8 -width 72"
      mu4e-html2text-command "w3m -dump -T text/html"
      ;; display images
      mu4e-view-show-images t
      mu4e-view-image-max-width 800
      ;; Use fancy chars
      mu4e-headers-seen-mark '("S" . "")
      mu4e-headers-new-mark '("N" . "")
      mu4e-headers-replied-mark '("R" . "")
      mu4e-headers-passed-mark '("P" . "")
      mu4e-headers-encrypted-mark '("x" . "")
      mu4e-headers-signed-mark '("s" . "")
      mu4e-headers-empty-parent-prefix '("-" . "")
      mu4e-headers-first-child-prefix '("\\" . "")
      mu4e-use-fancy-chars t
      ;; don't save message to Sent Messages, Gmail/IMAP takes care of this
      mu4e-sent-messages-behavior 'delete
      ;; setup some handy shortcuts
      ;; you can quickly switch to your Inbox -- press ``ji''
      ;; then, when you want archive some messages, move them to
      ;; the 'All Mail' folder by pressing ``ma''.
      mu4e-maildir-shortcuts
      '( ("/INBOX" . ?i)
         ("/[Gmail].Sent Mail" . ?s)
         ("/[Gmail].Trash" . ?t)
         ("/[Gmail].All Mail" . ?a))
)

;; personal
(setq
 user-mail-address "ivkuzev@gmail.com"
 user-full-name "Ivaylo Kuzev"
 mu4e-compose-signature "Ivaylo Kuzev | @ivo")

;; use imagemagick, if available to display images in mu4e:
(when (fboundp 'imagemagick-register-types)
  (imagemagick-register-types))

;; Setting the default emacs mail program
(setq mail-user-agent 'mu4e-user-agent)

;; SMTP ;;
(require 'smtpmail)
(setq message-send-mail-function 'smtpmail-send-it
    smtpmail-stream-type 'starttls
    smtpmail-default-smtp-server "smtp.gmail.com"
    smtpmail-smtp-server "smtp.gmail.com"
    smtpmail-smtp-service 587)

Mu4e and Org Contacts

I want to use org-contacts.

(require 'org)
(require 'org-contacts)
(setq mu4e-org-contacts-file "~/Dropbox/Org/contacts.org")
(add-to-list 'mu4e-headers-actions
             '("org-contact-add" . mu4e-action-add-org-contact) t)
(add-to-list 'mu4e-view-actions
             '("org-contact-add" . mu4e-action-add-org-contact) t)

And a hot key

And we want to be able to switch to mu4e quickly so let’s set f9 to be the hot key.

(global-set-key [f9] 'mu4e)

Multiterm

Use Emacs terminfo, not system terminfo

;; tic -o ~/.terminfo /usr/share/emacs/24.3/etc/e/eterm-color.ti
(setq system-uses-terminfo nil)

Multiterm setup.

;; http://www.emacswiki.org/emacs/MultiTerm
(when (require 'multi-term nil t)
  (global-set-key [f1] 'multi-term)
  (global-set-key (kbd "C-c n") 'multi-term-next)
  (global-set-key (kbd "C-c p") 'multi-term-prev)
      (setq multi-term-buffer-name "eterm"
      multi-term--program "/bin/zsh"))

;; Turn off current-line-highlighting and auto-pair.
(defadvice term-char-mode (after term-char-mode-fixes ())
  (autopair-mode -1)
  (set (make-local-variable 'hl-line-mode) nil)
  (set (make-local-variable 'global-hl-line-mode) nil))
(ad-activate 'term-char-mode)

;; fix copy/paste
(add-hook
 'term-mode-hook
 (lambda ()
   (define-key term-raw-map (kbd "C-y") 'term-paste)
   (define-key term-raw-map (kbd "C-v") 'term-paste)
   (define-key term-raw-map (kbd "s-v") 'term-paste)))

;; unlimited terminal buffer
(add-hook 'term-mode-hook
          (lambda ()
            (setq term-buffer-maximum-size 0)))

Ratpoison

Ratpoison support for Emacs.

You have to install Ratpoison first.

(require 'ratpoison)
(add-to-list 'auto-mode-alist '("\\.ratpoisonrc$" . ratpoisonrc-mode))

Shell mode

(setq shell-file-name "/bin/zsh")
(add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))

Useful functions

Hide the mode-line in the current buffer.

;; http://bzg.fr/emacs-hide-mode-line.html
(defvar-local hidden-mode-line-mode nil)

(define-minor-mode hidden-mode-line-mode
  "Minor mode to hide the mode-line in the current buffer."
  :init-value nil
  :global nil
  :variable hidden-mode-line-mode
  :group 'editing-basics
  (if hidden-mode-line-mode
      (setq hide-mode-line mode-line-format
            mode-line-format nil)
    (setq mode-line-format hide-mode-line
          hide-mode-line nil))
  (when (and (called-interactively-p 'interactive)
             hidden-mode-line-mode)
    (run-with-idle-timer
     0 nil 'message
     (concat "Hidden Mode Line Mode enabled. "
             "Use M-x hidden-mode-line-mode RET to make the mode-line appear."))))

slick-copy: make copy-past a bit more intelligent.

;; from: http://www.emacswiki.org/emacs/SlickCopy
(defadvice kill-ring-save (before slick-copy activate compile)
    "When called interactively with no active region, copy a single
line instead."
    (interactive
     (if mark-active (list (region-beginning) (region-end))
       (message "Copied line")
       (list (line-beginning-position)
             (line-beginning-position 2)))))
(defadvice kill-region (before slick-cut activate compile)
    "When called interactively with no active region, kill a single
line instead."
    (interactive
     (if mark-active (list (region-beginning) (region-end))
       (list (line-beginning-position)
             (line-beginning-position 2)))))

enable slime with slime helper.

;;(condition-case ex ; if slime-helper is not installed do not give an error
;; (progn
;; (load (expand-file-name "~/quicklisp/slime-helper.el"))

      ;; Replace "sbcl" with the path to your implementation
;; (setq inferior-lisp-program "sbcl")

      ;; connect slime automatically
      ;; when slime-mode is opened
;; (defun cliki:start-slime ()
;; (unless (slime-connected-p)
;; (save-excursion (slime))))
      ;; add full linking set
;; (add-hook 'slime-mode-hook 'cliki:start-slime))
;; ('error (message "slime could not be loaded")))

Search in Google.

;; from @bbatsov
(defun google ()
  "Google the selected region if any, display a query prompt otherwise."
  (interactive)
  (browse-url
   (concat
    "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
    (url-hexify-string (if mark-active
                           (buffer-substring (region-beginning) (region-end))
                         (read-string "Google: "))))))

Search in Youtube.

;; http://emacsredux.com/blog/2013/08/26/search-youtube/
(defun youtube ()
  "Search YouTube with a query or region if any."
  (interactive)
  (browse-url
   (concat
    "http://www.youtube.com/results?search_query="
    (url-hexify-string (if mark-active
                           (buffer-substring (region-beginning) (region-end))
                         (read-string "Search YouTube: "))))))
(defun kill-emacs-or-frame (arg)
  (interactive "P")
  (if (not server-buffer-clients)
      (if (and (not arg) (> (length (visible-frame-list)) 1))
          (delete-frame)
        (save-buffers-kill-emacs))
    (save-buffer)
    (server-buffer-done (current-buffer))))

rename buffer and file opened in emacs.

(defun rename-file-and-buffer (new-name)
  "Renames both current buffer and file it's visiting to NEW-NAME."
  (interactive "sNew name: ")
  (let ((name (buffer-name))
        (filename (buffer-file-name)))
    (if (not filename)
        (message "Buffer '%s' is not visiting a file!" name)
      (if (get-buffer new-name)
          (message "A buffer named '%s' already exists!" new-name)
        (progn
          (rename-file name new-name 1)
          (rename-buffer new-name)
          (set-visited-file-name new-name)
                    (set-buffer-modified-p nil))))))

Delete the current file, and kill the buffer.

;; from @purcell
(defun delete-this-file ()
  "Delete the current file, and kill the buffer."
  (interactive)
  (or (buffer-file-name) (error "No file is currently being edited"))
  (when (yes-or-no-p (format "Really delete '%s'?"
                             (file-name-nondirectory buffer-file-name)))
    (delete-file (buffer-file-name))
    (kill-this-buffer)))

Move line up|down.

;; http://emacsredux.com/blog/2013/04/02/move-current-line-up-or-down/
;; also check https://github.com/rejeep/drag-stuff.el
(defun move-line-up ()
  "Move up the current line."
  (interactive)
  (transpose-lines 1)
  (forward-line -2)
  (indent-according-to-mode))

(defun move-line-down ()
  "Move down the current line."
  (interactive)
  (forward-line 1)
  (transpose-lines 1)
  (forward-line -1)
  (indent-according-to-mode))

Insert date.

(defun insert-date ()
  "Insert the current date."
  (interactive)
  (insert (format-time-string "%Y-%m-%dT%T%z")))

Show line numbers temporarily, while prompting for the line number input.

;; from @magnars https://gist.github.com/magnars/3292872
(defun goto-line-with-feedback (&optional line)
  "Show line numbers temporarily, while prompting for the line number input"
  (interactive "P")
  (if line
      (goto-line line)
    (unwind-protect
        (progn
          (linum-mode 1)
          (goto-line (read-number "Goto line: ")))
      (linum-mode -1))))

Kill other buffers.

;; http://www.emacswiki.org/emacs/KillingBuffers
(defun kill-other-buffers ()
  "Kill all other buffers."
  (interactive)
  (mapc 'kill-buffer (delq (current-buffer) (buffer-list))))

(defun kill-all-dired-buffers ()
      "Kill all dired buffers."
      (interactive)
      (save-excursion
        (let ((count 0))
          (dolist (buffer (buffer-list))
            (set-buffer buffer)
            (when (equal major-mode 'dired-mode)
              (setq count (1+ count))
              (kill-buffer buffer)))
          (message "Killed %i dired buffer(s)." count))))

Create all the intermediate dirs if they weren’t existing when save a file.

;; https://stackoverflow.com/posts/6830894/revisions
(add-hook 'before-save-hook
          (lambda ()
            (let ((dir (file-name-directory buffer-file-name)))
              (unless (file-exists-p dir)
                (make-directory dir t)))))

Sudo edit.

(defun sudo-edit (&optional arg)
  "Open the current buffer (or prompt for file if ARG is non-nill) using sudo to edit as root."
  (interactive "p")
  (if (or arg (not buffer-file-name))
      (find-file (concat "/sudo::" (ido-read-file-name "File: ")))
    (find-alternate-file (concat "/sudo::" buffer-file-name))))

Insert a lorem ipsum.

(defun lorem ()
  "Insert a lorem ipsum."
  (interactive)
  (insert "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
          "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim"
          "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
          "aliquip ex ea commodo consequat. Duis aute irure dolor in "
          "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
          "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
          "culpa qui officia deserunt mollit anim id est laborum."))

Translate the following region in lingvo.

;; from @snosov1
(defun lingvo-it ()
  "Translate the following region in lingvo, display a query
prompt otherwise."
  (interactive)
  (browse-url
   (concat
    "http://lingvopro.abbyyonline.com/en/Translate/en-es/"
    (url-hexify-string (if mark-active
                           (buffer-substring (region-beginning) (region-end))
                         (read-string "Lingvo: "))))))

Edit the shell init file in another window.

;; http://emacsredux.com/blog/page/2/
(defun find-shell-init-file ()
  "Edit the shell init file in another window."
  (interactive)
  (let* ((shell (car (reverse (split-string (getenv "SHELL") "/"))))
         (shell-init-file (cond
                           ((string-equal "zsh" shell) ".zshrc")
                           ((string-equal "bash" shell) ".bashrc")
                           (t (error "Unknown shell")))))
    (find-file-other-window (expand-file-name shell-init-file (getenv "HOME")))))
(global-set-key (kbd "C-c S") 'find-shell-init-file)

Create a new scratch buffer.

(defun create-scratch-buffer ()
  "Create a new scratch buffer."
  (interactive)
  (progn
    (switch-to-buffer
     (get-buffer-create (generate-new-buffer-name "*scratch*")))
    (emacs-lisp-mode)))

Smarter Navigation to the Beginning of a Line.

Read this for details.

(defun smarter-move-beginning-of-line (arg)
  "Move point back to indentation of beginning of line.

Move point to the first non-whitespace character on this line.
If point is already there, move to the beginning of the line.
Effectively toggle between the first non-whitespace character and
the beginning of the line.

If ARG is not nil or 1, move forward ARG - 1 lines first.  If
point reaches the beginning or end of the buffer, stop there."
  (interactive "^p")
  (setq arg (or arg 1))

  ;; Move lines first
  (when (/= arg 1)
    (let ((line-move-visual nil))
      (forward-line (1- arg))))

  (let ((orig-point (point)))
    (back-to-indentation)
    (when (= orig-point (point))
      (move-beginning-of-line 1))))

;; remap C-a to `smarter-move-beginning-of-line'
(global-set-key [remap move-beginning-of-line]
                'smarter-move-beginning-of-line)

Global key bindings.

Goto line with feedback.

(global-set-key (kbd "C-x g") 'goto-line)
(global-set-key [remap goto-line] 'goto-line-with-feedback)

Split window.

(global-set-key (kbd "M-3") 'split-window-horizontally)
(global-set-key (kbd "M-2") 'split-window-vertically)
(global-set-key (kbd "M-1") 'delete-other-windows)
(global-set-key (kbd "M-0") 'delete-window)

Resize window.

(global-set-key (kbd "C-c <up>") 'shrink-window)
(global-set-key (kbd "C-c <down>") 'enlarge-window)
(global-set-key (kbd "C-c <left>") 'shrink-window-horizontally)
(global-set-key (kbd "C-c <right>") 'enlarge-window-horizontally)

Buffer switching.

(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <right>") 'windmove-right)
(global-set-key (kbd "C-x <left>") 'windmove-left)

Move-current-line-up-or-down.

(global-set-key (kbd "M-<up>") 'move-line-up)
(global-set-key (kbd "M-<down>") 'move-line-down)

iBuffer.

(autoload 'ibuffer "ibuffer" "List buffers." t)
(global-set-key (kbd "C-x C-b") 'ibuffer)

Kill buffer without questions.

(global-set-key (kbd "C-x k") 'kill-this-buffer)

Kills all buffers, except the current one.

(global-set-key (kbd "C-x M-k") 'kill-other-buffers)

Kill emacs or frame.

(global-set-key "\C-x\C-c" 'kill-emacs-or-frame)

Clipboard.

(global-set-key (kbd "C-w") 'clipboard-kill-region)
(global-set-key (kbd "M-w") 'clipboard-kill-ring-save)
(global-set-key (kbd "C-y") 'clipboard-yank)

Delete words with C-w and rebind kill region to C-x C-k

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

Files.

(global-set-key (kbd "C-x f") 'ido-find-file)
(global-set-key (kbd "C-o") 'ido-find-file)
(global-set-key (kbd "C-x s") 'save-buffer)
(global-set-key (kbd "C-x C-s") 'save-some-buffers)
(global-set-key (kbd "C-x c") 'save-buffers-kill-emacs)

increase|decrease font size.

(define-key global-map (kbd "C-+") 'text-scale-increase)
(define-key global-map (kbd "C--") 'text-scale-decrease)

Browse Url.

(define-key global-map (kbd "C-x M-g") 'google)
(define-key global-map (kbd "C-x M-y") 'youtube)
(define-key global-map (kbd "C-x M-b") 'browse-url-at-point)

Proced.

(global-set-key (kbd "C-x p") 'proced)