dotfiles/.emacs.d/emacs.org
Correl Roush ae8a21e850 [emacs] Some useful snippets
Scavenged from Sacha Chau's emacs config
2014-11-12 01:12:46 -05:00

14 KiB

Emacs Configuration

Startup

Disable the emacs startup screen.

  (setq inhibit-startup-screen +1)

Global key bindings

  (global-set-key (kbd "C-,") 'kill-whole-line)

Look and Feel

  (load-theme 'solarized-dark 't)

Autocomplete

  (require 'auto-complete-config)
  (ac-config-default)

Package Configuration

Ido

  (ido-mode +1)
  (setq ido-enable-flex-matching t)

Smex

  (global-set-key (kbd "M-x") 'smex)
  (global-set-key (kbd "M-X") 'smex-major-mode-commands)

Expand Region

  (global-set-key (kbd "C-=") 'er/expand-region)

Flycheck

  (add-hook 'after-init-hook #'global-flycheck-mode)

Git-Gutter

  (global-git-gutter-mode t)

  (defadvice ediff-make-temp-file (before make-temp-file-suspend-ll
                                          activate compile preactivate)
    "Disable git-gutter when running ediff"
    (global-git-gutter-mode 0))

  (add-hook 'ediff-cleanup-hook
            '(lambda ()
               (global-git-gutter-mode t)))

Magit

  (defun magit-fullscreen ()
    (defadvice magit-status (around magit-fullscreen activate)
(window-configuration-to-register :magit-fullscreen)
ad-do-it
(delete-other-windows))

    (defadvice magit-quit-window (around magit-restore-screen activate)
ad-do-it
(jump-to-register :magit-fullscreen)))

  (eval-after-load 'magit '(magit-fullscreen))

Markdown

  (setq auto-mode-alist
        (cons '("\\.md" . markdown-mode) auto-mode-alist))

Org

Babel

Syntax highlighting
  (setq-default org-src-fontify-natively t)
Language evaluation
  (defvar my/org-babel-evaluated-languages
    '(emacs-lisp)
    "List of languages that may be evaluated in Org documents")

  <<org-babel-languages>>

  (org-babel-do-load-languages
   'org-babel-load-languages
   (mapcar (lambda (lang)
             (cons lang t))
           my/org-babel-evaluated-languages))
Graphviz
  (add-to-list 'org-src-lang-modes (quote ("dot" . graphviz-dot)))

  (add-to-list 'my/org-babel-evaluated-languages 'dot)
Ditaa
  (add-to-list 'my/org-babel-evaluated-languages 'ditaa)
PlantUML
  (add-to-list 'my/org-babel-evaluated-languages 'plantuml)
Mscgen

A message sequence chart renderer.

  (add-to-list 'my/org-babel-evaluated-languages 'mscgen)

Powerline

  (powerline-default-theme)

Projectile

  (projectile-global-mode)

Web Mode

  (add-hook 'web-mode-hook (lambda ()
                             (setq web-mode-markup-indent-offset 4)
                             (setq web-mode-css-indent-offset 4)
                             (setq web-mode-code-indent-offset 4)))

Gnus

  (defun my/configure-gnus ()
    <<gnus>>
    )

  (setq gnus-before-startup-hook 'my/configure-gnus)

Avoid rich-text messages

  (setq mm-discouraged-alternatives '("text/html" "text/richtext"))

Summary view formatting

Taken from http://groups.google.com/group/gnu.emacs.gnus/browse_thread/thread/a673a74356e7141f

  (when window-system
    (setq gnus-sum-thread-tree-indent "  ")
    (setq gnus-sum-thread-tree-root "") ;; "● ")
    (setq gnus-sum-thread-tree-false-root "") ;; "◯ ")
    (setq gnus-sum-thread-tree-single-indent "") ;; "◎ ")
    (setq gnus-sum-thread-tree-vertical        "│")
    (setq gnus-sum-thread-tree-leaf-with-other "├─► ")
    (setq gnus-sum-thread-tree-single-leaf     "╰─► "))
  (setq gnus-summary-line-format
        (concat
         "%0{%U%R%z%}"
         "%3{│%}" "%1{%d%}" "%3{│%}" ;; date
         "  "
         "%4{%-20,20f%}"               ;; name
         "  "
         "%3{│%}"
         " "
         "%1{%B%}"
         "%s\n"))
  (setq gnus-summary-display-arrow t)

Inline images

  (add-to-list 'mm-attachment-override-types "image/.*")
  (setq mm-inline-large-images t)

Programming

Lisps

  ;; SLIME
  (if (file-exists-p "~/quicklisp/slime-helper.el")
      (load (expand-file-name "~/quicklisp/slime-helper.el")))

  (setq inferior-lisp-program "clisp")

  (mapcar (lambda (mode-hook)
            (eval-after-load "paredit" `(add-hook ',mode-hook #'enable-paredit-mode))
            (eval-after-load "rainbow-delimiters" `(add-hook ',mode-hook #'rainbow-delimiters-mode))
            (eval-after-load "rainbow-identifiers" `(add-hook ',mode-hook #'rainbow-identifiers-mode))
            (add-hook mode-hook (lambda ()
                             (show-paren-mode)
                             (electric-indent-mode 1)
                             (paredit-mode 1)
                             (rainbow-delimiters-mode 1)
                             (rainbow-identifiers-mode 1)))
            )
          '(lisp-mode-hook
            emacs-lisp-mode-hook
            scheme-mode-hook
            lfe-mode-hook
            clojure-mode-hook))

Erlang

  (add-hook 'erlang-mode-hook
            (lambda ()
              (setq inferior-erlang-machine-options '("-sname" "emacs"
                                                      "-hidden"))))

PHP

  (add-to-list 'auto-mode-alist '("\\.php$" . php-mode))
  (add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))

  (defun my-php-mode-hook ()
    "Customize PHP indentation"

    (c-set-offset 'arglist-cont-nonempty 'c-lineup-arglist)
    (c-set-offset 'substatement-open 0)
    (c-set-offset 'case-label '+))

  (add-hook 'php-mode-hook 'my-php-mode-hook)

Ruby

  (require 'rvm)
  (rvm-use-default)

Other functionality

Rename file and buffer

Taken from Emacs Redux

  ;; emacsredux.com
  (defun rename-file-and-buffer ()
    "Rename the current buffer and file it is visiting."
    (interactive)
    (let ((filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
   (message "Buffer is not visiting a file!")
 (let ((new-name (read-file-name "New name: " filename)))
   (cond
    ((vc-backend filename) (vc-rename-file filename new-name))
    (t
     (rename-file filename new-name t)
     (rename-buffer new-name)
     (set-visited-file-name new-name)
     (set-buffer-modified-p nil)))))))

Eval and Replace

Taken from Emacs Redux

  (defun eval-and-replace ()
    "Replace the preceding sexp with its value."
    (interactive)
    (backward-kill-sexp)
    (condition-case nil
        (prin1 (eval (read (current-kill 0)))
               (current-buffer))
      (error (message "Invalid expression")
             (insert (current-kill 0)))))

  (global-set-key (kbd "C-)") 'eval-and-replace)

Smarter navigation to the beginning of a line

Taken from Emacs Redux

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

Edit file with sudo

Taken from EmacsWiki

  (require 'dired)
  (defun sudo-edit-current-file ()
    (interactive)
    (let ((my-file-name) ; fill this with the file to open
          (position))    ; if the file is already open save position
      (if (equal major-mode 'dired-mode) ; test if we are in dired-mode 
          (progn
            (setq my-file-name (dired-get-file-for-visit))
            (find-alternate-file (prepare-tramp-sudo-string my-file-name)))
        (setq my-file-name (buffer-file-name); hopefully anything else is an already opened file
              position (point))
        (find-alternate-file (prepare-tramp-sudo-string my-file-name))
        (goto-char position))))


  (defun prepare-tramp-sudo-string (tempfile)
    (if (file-remote-p tempfile)
        (let ((vec (tramp-dissect-file-name tempfile)))

          (tramp-make-tramp-file-name
           "sudo"
           (tramp-file-name-user nil)
           (tramp-file-name-host vec)
           (tramp-file-name-localname vec)
           (format "ssh:%s@%s|"
                   (tramp-file-name-user vec)
                   (tramp-file-name-host vec))))
      (concat "/sudo:root@localhost:" tempfile)))

  (define-key dired-mode-map [s-return] 'sudo-edit-current-file)

Backups

Borrowed from Sacha Chua https://github.com/sachac/.emacs.d/

This is one of the things people usually want to change right away. By default, Emacs saves backup files in the current directory. These are the files ending in ~ that are cluttering up your directory lists. The following code stashes them all in ~/.emacs.d/backups, where I can find them with C-x C-f (find-file) if I really need to.

  (setq backup-directory-alist '(("." . "~/.emacs.d/backups")))

Disk space is cheap. Save lots.

  (setq delete-old-versions -1)
  (setq version-control t)
  (setq vc-make-backup-files t)
  (setq auto-save-file-name-transforms '((".*" "~/.emacs.d/auto-save-list/" t)))

History

From http://www.wisdomandwonder.com/wordpress/wp-content/uploads/2014/03/C3F.html

  (setq savehist-file "~/.emacs.d/savehist")
  (savehist-mode 1)
  (setq history-length t)
  (setq history-delete-duplicates t)
  (setq savehist-save-minibuffer-history 1)
  (setq savehist-additional-variables
        '(kill-ring
          search-ring
          regexp-search-ring))

Copy filename to clipboard

http://emacsredux.com/blog/2013/03/27/copy-filename-to-the-clipboard/ https://github.com/bbatsov/prelude

  (defun prelude-copy-file-name-to-clipboard ()
    "Copy the current buffer file name to the clipboard."
    (interactive)
    (let ((filename (if (equal major-mode 'dired-mode)
                        default-directory
                      (buffer-file-name))))
      (when filename
        (kill-new filename)
        (message "Copied buffer file name '%s' to the clipboard." filename))))

Custom settings

Store options set via customize-* in a separate file (Emacs stores them in init.el by default).

  (setq custom-file "~/.emacs.d/custom.el")
  (if (file-exists-p custom-file)
      (load custom-file))

Auto-loading elisp files

Any elisp files dropped into ~/.emacs.local.d/ will be automatically loaded.

  (defun my/load-elisp-directory (path)
    (let ((file-pattern "\\.elc?$"))
      (when (file-directory-p path)
        (mapcar (lambda (lisp-file)
                  (load-file lisp-file))
                (directory-files (expand-file-name path) t file-pattern)))))

  (my/load-elisp-directory "~/.emacs.d/init.d")
  (my/load-elisp-directory "~/.emacs.local.d")

Configuration file layout

Here I define the emacs.el file that gets generated by the code in this org file.

  ;;;; Do not modify this file by hand.  It was automatically generated
  ;;;; from `emacs.org` in the same directory. See that file for more
  ;;;; information.
  ;;;;

  <<custom-settings>>
  <<look-and-feel>>
  <<autocomplete>>
  <<packages>>
  <<programming>>
  <<other>>
  <<auto-load>>
  <<startup>>