Deleting trailing whitespace for auto savers

real-auto-save is a great package if you like that sort of thing. For example, I like every file to always be saved without me worrying about doing it myself, so I stick with the default save occurring every 10 seconds. A really nice function to call on write-file-hooks is delete-trailing-whitespace, but, with 10s saves this means that in the middle of typing you have spaces eaten and this is clearly unacceptable!
Here is an attempt at a tweaked cleanup function that cleans up every line in the file but for the current line on which your cursor sits:

(defun gcr/delete-trailing-whitespace ()
  "Apply delete-trailing-whitespace to everything but the current line."
  (interactive)
  (let ((first-part-start (point-min))
        (first-part-end (point-at-bol))
        (second-part-start (point-at-eol))
        (second-part-end (point-max)))
    (delete-trailing-whitespace first-part-start first-part-end)
    (delete-trailing-whitespace second-part-start second-part-end)))

Cask for the truly impatient

Thanks to some kind Emacsers I’m now in the modern age using Cask, and what ease it brings to using Emacs. It is truly a joy; anyone not using Emacs for fear of difficulty pulling in packages can let go of their hesitation. It is as easy as writing one config file, installing the packages, and adding a couple lines to your Emacs init script. Here are the basic steps:

  • Clone the cask repo.
  • Add the bin dir to your path.
  • Create a file named Cask, add it to your VCS, and create a link to it from your .emacs.d directory
  • Add a repo and packages to the file.
  • From your .emacs.d directory, run ‘cask’
  • Add the cask load and init to your init file.
  • Start Emacs.

Excellent work by that team.

How to Choose Packages Between Two ELPA Repositories

ELPA makes Emacs v24 even more delightful to use. You may have run into a situation though where you wanted to install different packages from both Marmalade and MELPA. A common problem here is that because the newest version number always gets chosen for installation, MELPA packages always get chosen over Marmalade, and you may not want that. MELPA thankfully has a solution for that in the form of their own package.
The directions to set up MELPA are straightforward, but, one of my super-powers is not make any sense of directions, so I had a heck of a time getting it working. Aaron’s config gave me a clue, but I still didn’t have it working (I liked his namespace prefixing though so). Once I did get it working though it was really clear what I had done wrong, basically the package load and require order was incorrect, so, here is the right way to do it:

  • Install the melpa package manually as directed; this gives you package you need to use the filtering functionality.
  • Require ‘package to get the ELPA functionality and variables.
  • Add the repo(s) to ‘package-archives so that you can pull from them.
  • Call package-initialize to find the recently installed melpa package.
  • Require ‘melpa to import it and be able to use it.
  • Customize the enable and exclude melpa variables to specify what packages to include or exclude from which repositories.
  • Call package-refresh-contents to update Emacs’s database of which packages it should use as available for installation.
  • Your filtered package list is now available for use, call list-packages to verify.

Here is an example of my situation, I wanted to default to installing the newest package from either GNU or Marmalade for all but two cases where I only wanted the version that was available on MELPA: fill-column-indicator and melpa. Here is the configuration and correct order of calls to make:

(defvar gcr/packages
  '(auto-complete
    color-theme
    color-theme-solarized
    diminish
    fill-column-indicator
    fuzzy
    geiser
    graphviz-dot-mode
    lexbind-mode
    melpa
    ob-sml
    paredit
    pretty-mode-plus
    rainbow-mode
    real-auto-save
    sml-mode)
  "Packages required at runtime.")
(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(require 'melpa)
(setq package-archive-enable-alist '(("gnu")
                                     ("marmalade")
                                     ("melpa"
                                      fill-column-indicator
                                      melpa)))
(setq package-archive-exclude-alist '(("gnu"
                                       fill-column-indicator
                                       melpa)
                                      ("marmalade"
                                       fill-column-indicator
                                       melpa)))
(package-refresh-contents)
(dolist (package gcr/packages)
  (when (not (package-installed-p package))
    (condition-case err
        (package-install package)
      (error
       (message "%s" (error-message-string err))))))

Calling functions with optional arguments from the mini-buffer

Surely one of the first things VIers want to know as do the rest of us is how to move forward N characters (or backward or whatever) from the mini-buffer in EMACS. Here is how it is done, using the universal argument. So to move forward 100 chars:

C:u 100, C:f

ADDENDUM:01/13/13
Thanks FUCO for providing a better solution:

There’s also a faster way. Just hold down control key (or meta key, by default the binding is on both) and type the number, then execute the command or invoke minibuffer with M-x
So, C-2 C-0 C-f will move you 20 characters. It’s neat because you don’t have to release control, you just hit the rest of the keys in sequence.