Having my :vim and M-x emacs-ing it too

In 2008 or so, I switched from Textmate to vim. Then from vim to emacs. Then emacs to vim. Back to emacs. Vim. Emacs. Vim.

Then I discovered Vimpulse for emacs, and that was that.

Vimpulse is now Evil and it’s better than ever.

Here are some great tweaks.

(evil-define-motion evil-little-word (count)
  :type exclusive
  (let* ((case-fold-search nil)
		 (count (if count count 1)))
	(while (> count 0)
	  (forward-char)
	  (search-forward-regexp "[_A-Z]\\|\\W" nil t)
	  (backward-char)
	  (decf count))))

This is essentially clw from a previous post, but here we’re defining a new unit of editing. This lets you:

(define-key evil-operator-state-map (kbd "lw") 'evil-little-word)

And then you’ve got clw and dlw which delete the “one” in “oneTwoThree” or in “one_two_three”. I use this all the time.

Here’s something even more useful: Ever boil over because in emacs (and vim for that matter) every text deletion pulls that text into your kill-ring (register)? I sure do. Sometimes, I just want something gone, and I never want to see it again!

More often, I want to paste something from my clipboard over something else, which requires that my clipboard not be messed with by the pasting process.

(Emacs translation: More often, I want to yank something from my kill-ring…)
(Vim translation: More often, I want to put something from my default register…)

(evil-define-operator evil-destroy (beg end type register yank-handler)
  (evil-delete beg end type ?_ yank-handler))

There is some unfortunate copy and paste code going on here, and I’d be happy to hear about how to eliminate it, but in essence this is just the d operator (delete), but without any clipboard operations going on. (Note the updated, cleaner implementation, inspired by Russell’s comment.)

You can then bind this to something, I use X, and utterly and irrevocably DESTROY some text. But that’s not end game. Next step is this:

(evil-define-operator evil-destroy-replace (beg end type register yank-handler)
  (evil-destroy beg end type register yank-handler)
  (evil-paste-before 1 register))

This will replace a bit of text with whatever’s in the clipboard without altering the clipboard, and I use this all the time.

I bind it to q, which, yeah, is supposed to be for evil macros, but I’ve got emacs macro bindings, so that’s fine.

This works with motion and text objects, so you can do things like yi" to copy between double-quotes and then qi" to paste inside different double-quotes (assuming you’ve bound to q). qi(, qi' and qi{ all also work as you’d expect.

This is incredibly useful, and once you have it, you won’t know how you lived without it. It is also possible in vim. Read all about it here (the second response) http://stackoverflow.com/questions/2471175/vim-replace-word-with-contents-of-paste-buffer.

One last thing, and this one is great!

(defun whitespace-only-p (string)
  (equal "" (replace-regexp-in-string "[ \t\n]" "" string)))

(defadvice evil-delete (around evil-delete-yank activate)
  (if (whitespace-only-p (buffer-substring beg end))
	  (evil-destroy beg end type register yank-handler)
	ad-do-it))

What does this do? Only the best thing ever! With regular, pull-into-the-clipboard deletion, things work as usual. However, should you delete a blank line (or any whitespace-only region), the clipboard is left alone.

Ooooooh lordy, that’s nice. No more pushing blank line deletions into your clipboard! Who wanted that anyway?

(If anyone knows how to build a similar solution for vim, post to the comments.)

Posted in Emacs, Programming, Vim | Tagged , , | 2 Comments

Emacs Config: You’re Doing It Wrong

Prelude

Every few months or so, I get an episode of .emacs.d OCD, where my setup suddenly seems hopelessly disorganized and non-optimal, and I can’t do another thing until I fix it. I went through such an episode recently, causing me to spend hours looking through various setup strategies, but ultimately not finding that je ne sais quoi I was looking for.

The whole exercise made me step back and reflect on what it is that causes me the most trouble when trying to organize an emacs config, and after a moment of reflection, I came up with 1) Where should I put things? 2) How should I init things?

It’s Load Time!

Where to put things is a problem that spans well beyond emacs configuration, and may well be the third-hardest topic in computer science, so I’ll get back to it. Number 2, however, is a little more particular to emacs. The issue is this: If you’re like me, you’re using dozens of emacs plugins to optimize your environment, and layering a handful of personal customizations and settings on each. That’s great. That’s what emacs is all about. But then, there’s autoloading.

We emacsians spend a good bit of time biting our nails over our favorite editor’s startup time, and make tremendous efforts to do as little as possible at startup time, instead letting packages load as needed.

Problem is, some package configurations require packages to be loaded while others don’t. Some packages don’t do autoload right. Sometimes you configure an autoloaded package in a running emacs session and it breaks emacs the next time you start it. And sometimes autoloading itself is unwanted sitting and staring and waiting time. (The package initialization section at http://emacswiki.org/emacs/ELPA is illustrative.) Most of these issues can be overcome, but the whole situation is messier than I’d like.

What’s more, there’s a very simple solution, and since I’ve discovered it, everything has just gotten a lot simpler. It’s not a package or a plugin or a software solution at all. It’s an attitude shift, and it struck me like a lightening bolt.

I DON’T CARE HOW LONG IT TAKE EMACS TO START UP.

What’s that?

I DON’T CARE HOW LONG IT TAKE EMACS TO START UP.

Yeah, ok, vim starts up like that! But here’s the thing. I don’t use emacs like I use vim (which I do sometimes). I start emacs up on my local machine maybe a couple times a week, and leave the session running for days. So even if it took 30 seconds to load (it takes 3.5), that’d be, like, a minute a week. I’ve already made that time up.

This is a pretty mundane realization, perhaps, but I no longer have to care about where and when things get loaded, about configuring things in the right place and time, about autoloads and eval-on-loads and related concerns. I just throw in a require and I get on with my life.

Getting On

So already in an iconoclastic state of mind, I was well positioned to tackle issue 2: Where to put things?

See, some things were in bindings.el, others in defuns.el, settings.el, [package-name-here]-config.el, and on and on and on. Where do I put hooks that depend on custom defuns? Bindings for plugins? Third-party package configs vs. built-in package configs? What’s worse, directories were nested three, four levels deep, and if I had to find something, I’d be acking all up in my .emacs.d and it was a mess. And I bet you do the same thing.

This was causing me pain, and then I remembered an incident.

The Incident

I was working on an iOS project, and it had a ton of files, and I was working in Xcode, and I hated Xcode, but needed the smart completion, which is a weak area for emacs. But I downloaded Code Pilot, and that made things a little better. And I used CTRL-6 to navigate to methods in a class (think imenu), and used project search a lot, and I started not caring about where things were, because as long as I new part of the name, I was there.

It took some work to get there in Xcode, but this is the sort of setup I strive for, whatever I’m developering, this is the reason I emacs, and this is key to productivity and happiness for my money.

My colleagues in the meantime where busy nesting directories and arranging things just so and painstakingly putting files in alphabetical order because seemingly Xcode won’t do that for you. And not only did they spend a lot of time and energy worrying about where to put things, they had to think a lot when they wanted to find them again.

For me, one of the biggest enemies of flow, and more generally, productivity, is the need to think about a thing that is not the thing you’re thinking about to solve the problem at hand. So if a particular task calls for jumping around to several files, any time and effort spent thinking about where some file is, or even just going through the mechanics of navigating directories, is likely to dissipate my fragile flow, kill my productivity, raise my hackles, and generally make me grumpier.

In It

So that’s the incident I remembered, and I realized that if you have good navigation support, it doesn’t matter where you put things. And if it doesn’t matter where you put things, why not take the simplest, stupidist, most obvious approach? The approach you probably took the first time you wrote a bit of emacs configuration code.

So where to put things? How about init.el? What things?

ALL THE THINGS!
x-all-the-things-template

I was initially incredulous of my own bat-shit idea, but I went through with it anyway. And it has been liberating.

You see, when everything you care about is in one place, you no longer have to think about where you put stuff. Where are my bindings? init.el. Hooks? init.el. Configuration for package X, Y, and most especially Z? init.el. Then fold in tools like occur and isearch and imenu and then do something like:

(defun imenu-elisp-sections ()
  (setq imenu-prev-index-position-function nil)
  (add-to-list 'imenu-generic-expression '("Sections" "^;;;; \\(.+\\)$" 1) t))

(add-hook 'emacs-lisp-mode-hook 'imenu-elisp-sections)

Which provides rough structure to the init file with section headings that look like

;;;; BINDINGS
...
;;;; HOOKS
...

So when I imenu (using helm-imenu in my case), I get this:

Screen Shot 2013-01-19 at 2.40.52 PM

And boy that’s nice.

How about this:

(defun init-imenu (p)
  (interactive "P")
  (find-file-existing "~/.emacs.d/init.el")
  (widen)
  (helm-imenu)
  (if p (init-narrow-to-section)))

Bind that globally, and any time you get the urge to change your emacs config, you can immediately jump to the desired part of your config, by section or by function. init-narrow-to-secion is just:

(defun init-narrow-to-section ()
  (interactive)
  (save-excursion
	(beginning-of-line)
	(unless (looking-at "^;;;;")
	  (re-search-backward "^;;;;" nil t))
	(push-mark)
	(next-line)
	(re-search-forward "^;;;;" nil t)
	(previous-line)
	(narrow-to-region (region-beginning) (region-end))))

Which is sometimes nice.

The downside is that dependency is now handled by order of code (from top to bottom) rather than the more robust require system. But it’s a small downside, which hasn’t caused me much pain, and one that can be remedied as needed by pulling any of your more library-ish stuff into a separate file and requiring it when desired.

(Probably obvious, but I’m talking about settings and hooks and bindings and one-off functions here. My plugins are not inlined in my init.el. That would be craaazy.)

You?

I challenge you to challenge yourself to consider why you care about emacs startup time. Is it the same reason I cared? Because all the emacs literature told you you cared?

Or perhaps someone out there has a head-slappingly simple system for auto-loading and configuring everything with no added complexity, in which case, I’m very curious to hear about it.

I’m also interested how folks with non-trivial emacs configurations are organizing them. Or if you adopt the one-big-ass-init-file approach, I’m curious to hear how your milage varies.

Posted in Emacs, Programming | Tagged | 5 Comments

Pwning Yr Forms with Firebug

You can read my latest post at the Clockwork blog.

Posted in Uncategorized | Leave a comment

Some Music

<a href="http://sela.bandcamp.com/track/arid">Arid by Sela</a>

Posted in Music | Tagged | Leave a comment

Bike Bingo in Clojure

So I’m admittedly a total newb in Clojure, but in the spirit of expanding my repertoire, I like to tackle problems in it now and again. So here’s what I want from you rare folks who somehow find these posts: enlighten me. Here’s my attempt at solving a trivial problem in what is surely not idiomatic Clojure. So why not shine a little light my way and tell me how you’d do it.

The Challenge

I’m in a biking-for-beer competition and the side game this month is bike bingo. All bikers know the potential tiles (which come in such flavors as “Bike at least 30 miles in one day” or “Pass someone on a tall bike”), but we don’t know the board layout. We spend the month completing as many of the tiles as possible, and the person with the most bingos at the end of the month wins beer. And sweet, sweet, arrogant pride.

So now we need a board. I figure we only need one board, because we’ll all have earned different tiles (opposite of standard bingo where everyone plays with the same numbers but different boards). The board should be somewhat random, but we’d also like to reward those who completed the hardest tiles.

The Solution

Here’s my solution. Tear it apart.

(ns bike-bingo
  (:require
   ;; We'll use cl-format to draw the board.
   [clojure.contrib.pprint :as pp]))

;; Here's our 5x5 bingo board. We're counting horizontal, vertical,
;; diagonal, four corners, and blackout.  The numbers in each square
;; represent how many bingos that square is involved in (excluding
;; blackout).
;;
;; It's been predetermined that the middle square, [2 2], will be "Ride
;; more than 1 mile", so I'm leaving it out.
;;
;;    | 0 | 1 | 2 | 3 | 4 |
;; ---+---+---+---+---+---|
;;  0 | 4 | 2 | 2 | 2 | 4 |
;; ---+---+---+---+---+---|
;;  1 | 2 | 3 | 2 | 3 | 2 |
;; ---+---+---+---+---+---|
;;  2 | 2 | 2 | x | 2 | 2 |
;; ---+---+---+---+---+---|
;;  3 | 2 | 3 | 2 | 3 | 2 |
;; ---+---+---+---+---+---|
;;  4 | 4 | 2 | 2 | 2 | 4 |
;;    +---+---+---+---+---|
;;
;; Given the illustration above, we're going to fill the most valuable
;; slots first using the most difficult tiles. Here's our board
;; filling order:

(def *board-priority*
     [[0 0] [0 4] [4 0] [4 4]
      [1 1] [1 3] [3 1] [3 3]
      [0 1] [0 2] [0 3] [1 0]
      [1 2] [1 4] [2 0] [2 1]
      [2 3] [2 4] [3 0] [3 2]
      [3 4] [4 1] [4 2] [4 3]])

;; To determine which tiles are the hardest, we'll take a look at how
;; many people earned them. Not perfect, but it'll do. Here's a map of
;; tiles to times completed.

(def *freq-map*
     {"Ride more than 10 miles"                                7
      "Ride more than 20 miles"                                3
      "Ride more than 30 miles"                                3
      "Ride more than 40 miles"                                2
      "Ride more than 50 miles"                                4
      "Ride more than 100 miles"                               1
      "Ride between 12:00AM and 5:00AM"                        2
      "Ride in inclement weather"                              5
      "Get yelled at by a stranger"                            5
      "Hit 30mph (downhill OK)"                                8
      "Ride to work every day one week"                        5
      "Go mountain biking"                                     2
      "Bike home from the bar"                                 5
      "Bike up Summit (Ramsey) hill"                           1
      "Bike on the Greenway"                                   5
      "Ride the Minneapolis Grand Rounds"                      2
      "Ride from DT Minneapolis to DT St. Paul or vice versa"  1
      "Ride outside the 494-694 loop"                          5
      "Follow all traffic laws (yes, even stop signs)"         8
      "Get a flat tire"                                        5
      "Change a flat tire"                                     3
      "Get honked at by a stranger"                            5
      "Bike in a group of 5 or more people"                    4
      "Pass someone on a Segway"                               2
      "Pass someone on a tall bike"                            2
      "Participate in a public bike event"                     4
      "Watch a public bike event"                              1
      "Track stand for 30 seconds"                             2})

;; Here's a simple HTML board template with some `cl-format'
;; directives in it. `~{' and `~}' start and end loops, `~a' is
;; replaced with some value. (Todo: It might be interesting to use
;; Enlive for this instead (http://github.com/cgrand/enlive).)

(def *template*
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
  <head>
    <style type=\"text/css\" media=\"screen\">
      td {
      border: 2px solid black;
      padding: 10px;
      width: 150px;
      height: 150px;
      text-align: center;
      font-weight: bold;
      }
    </style>
  </head>
  <body>
    <table>
      ~{<tr>
        ~{<td>~a</td>
        ~}
      </tr>
      ~}
    </table>
  </body>
</html>
")

;; Each tile will be selected at random, but I'd really like to give
;; the hardest tiles a good chance to hit the most valuable
;; squares. This function will transform their weight into the number
;; of entries they'll be getting in the hat.
;;
;; If someone has a more mathematically clever solution for this
;; transformation, I'm all ears.

(defn weight-transform [x]
  (* x x x))

;; Weight is inversely proportional to frequency from our freq-map, so
;; we subtract the times seen from 1 + most frequent to determine
;; it.
;;
;; Though, would it make more sense to make this an inverse of the
;; actually frequency divided by the number of people in the
;; contest (greatest possible frequency)?

(defn freq-to-weight [freq-map]
  (let [biggest (inc (apply max (vals freq-map)))]
    (apply hash-map (flatten
                     (for [pair freq-map] (list (pair 0) (- biggest (pair 1))))))))

;; Given the weight map and our transforming function, fill the hat
;; with an appropriate number of entries for each tile.

(defn fill-hat [weight-map weight-transform]
  (flatten (for [pair weight-map] (repeat (weight-transform (pair 1)) (pair 0)))))

;; Now we build up our board in order of priority.

(defn build-board [hat priority]
  (loop [board {} hat hat priority priority]
    ;; Once we've filled all our tiles from the priority list, we'll
    ;; place "Bike more than 1 mile" at the center.
    (if (= (count priority) 0)
      (assoc board [2 2] "Bike more than 1 mile")
      ;; Pull a random item from the hat. More heavily rated items
      ;; have more entries in the hat and are more likely to be drawn.
      (let [value (nth hat (rand-int (count hat)))]
        (recur
         ;; Place the tile on the board.
         (assoc board (first priority) value)
         ;; Remove all entries for that tile from the hat. (Good thing
         ;; this isn't a real hat.)
         (remove #(= % value) hat)
         ;; Continue with the remaining priority list.
         (rest priority))))))

;; `cl-format' will really like our board if it's nested vectors. Hash
;; table, not so much.

(defn board-to-nested-vector [board]
  (vec (for [row (range 5)]
         (vec (for [col (range 5)]
                (board [row col]))))))

;; A simple bit of business

(defn print-board [board template]
  (pp/cl-format true template (board-to-nested-vector board)))

;; And finally, we do the work.

(-> *freq-map*
    (freq-to-weight)
    (fill-hat weight-transform)
    (build-board *board-priority*)
    (print-board *template*))

Here’s the result.

Posted in Clojure, Programming | Tagged , , , | Leave a comment

anything-project-files.el

;; As promised `anything-project-files'. This smattering of code
;; allows you to define and load projects. Projects, once loaded, will
;; be indexed for all interesting files, which will let you make an
;; `anything' command to rule them all.
;;
;; Note: This is not intended to be a robust Emacs package, just an
;; example. Aw-right?  Good. Let's start.
anything-project-files

anything-project-files

;; The Common Lisp library for Emacs Lisp gives us keyword arguments
;; for defun*
(require 'cl)

(defvar projects (list) "This keeps track of all available projects.")
(defvar project (list) "And here's our current project.")
(defvar project-index (list) "This will store the project index of files.")
(defvar project-default-filetypes
  '("*.el" "*.rb" "*.py" "*.rb" "*.clj" "*.php" "*.js" "*.html")
  "Files for indexing.")

;; Before we can build `anything-project-files', we need to have an
;; idea of what a project is.  For the purposes of this post, a
;; project will have a name, a root directory, and a list of filetypes
;; we want to index. Change this default file list to suit your own
;; needs.
;;  
;; By the way, `&key' gives us keyword arguments, you can use a symbol
;; or a list of the form `(keyname default-value)' to designate a
;; keyword.
;;  
;; Anything candidates can be a list of `(DISPLAY . REAL)' pairs, so
;; we throw the name up front to serve as the `DISPLAY' component."

(defun* project-create (name &key root (indexed-files project-default-filetypes))
  "Add a project to the list or projects."
  (add-to-list 'projects
               (cons name
                     `((:name . ,name)
                       (:root . ,root)
                       (:indexed-files . ,indexed-files)))))

;; The handy, dandy function below makes it real easy to get a project
;; component in the form of `(project :root)' or `(project :name)'.
;; (Elisp is a Lisp-2, so function can have the same name as
;; variables.)

(defun project (key)
  (cdr (assoc key project)))

;; Here's the code that indexes our project.

(defun project-reindex ()
  "Update your projects index of files."
  (interactive)
  ;; Travel to the project root
  (cd (project :root))
  ;; Using our `indexed-files', create a string that we can toss into a
  ;; find command.  It'll look like "'*.el' -or -name '*.rb'...".
  ;;
  ;; For a big project, we don't want to be stuck waiting while it
  ;; indexes, so we have to do a little jig-hoolery to make this work
  ;; async. Firstly, that means keeping track of the current buffer,
  ;; and switching to a temporary one in which to do our work.
  (let* ((b (current-buffer)))
    ;; And here's our temp buffer.
    (switch-to-buffer "*project-index*")
    ;; I'll use `tramp-handle-shell-command' as it returns a process
    (let ((process (tramp-handle-shell-command
                    "find . -type f -name '*.el' &" (current-buffer))))
      ;; And that lets me set a sentinel (callback) for when the
      ;; process is complete
      (set-process-sentinel process 'project-load-index))
    (message "Indexing...")
    ;; The work has been started, so we switch back to where we were
    ;; when we called the command.
    (switch-to-buffer b)))

;; Here's my callback function. `p' is the process and `s' is a
;; message string, which I'll ignore here.
(defun project-load-index (p s)
  ;; Now we'll just split-string on newline for our process buffer.
  (setq project-index
        (split-string
         (with-current-buffer (process-buffer p)
           (buffer-string))))
  ;; Goodbye
  (kill-buffer (process-buffer p))
  (message "Indexing complete."))

;; We load the project by selecting from a list an anything
;; buffer. Nice.
(defun project-load ()
  (interactive)
  (setq project
        (anything
         '((name . "Load Project")
           (candidates . projects)
           ;; `anything' usually wants to do something with our selected candidate,
           ;; but here I'm just going to return it.
           (action . (("Return" . identity))))))
  ;; The project is set, let's index it.
  (project-reindex))


;; And finally, the payload.
(defun anything-project-files ()
  (interactive)
  (anything
   '((name . "Project Find File")
     (candidates . project-index)
     ;; `display-to-real' will take the filename selection and add our
     ;; project root before any action is performed on it.
     (display-to-real . (lambda (c) (concat (project :root) "/" c)))
     (type . file))))


;; Now we'll set up our `emacs-config' project, accepting defaults for `:indexed-files',
;; though we could certainly use `'("*.el")' if we wanted to.
(project-create "emacs-config"
                :root "~/.emacs.d")


;; And there you have it. Load your `emacs-config' project, give it
;; the old `M-x anything-project-files', and you're off to the races.
;;
;; Next steps:
;;
;; Once you've got project context, you can let your imagination run
;; wild coming up with project-related tasks Emacs could be doing for
;; you.
;;
;; Also `project-load' could do more, like tagging the project. Hell,
;; you could create an `anything-project-tags' command, which would
;; rock out pretty hard.
;;
;; Indexing (and tagging) might take a long time on large projects and
;; may not be necessary on every project load. One could consider
;; saving a project index file and loading from that until you
;; explicitly reindex. It'd be pretty simple, but I'll leave that up
;; to you.
;;
;; Until next time, buenos dias! Buenos Aires!
Posted in Emacs, Programming | Tagged , , , | 2 Comments

So long old friend

As I was biking home, a wicked storm struck up.

And here’s me, pitched half sideways as the wind works to pull me down. My eyes shut tight against the rain.

A shadow, I barely saw, and the next thing I knew I was prone on the street. Struck by some branch pulled from its tree. It hit me in the side of the head.

My helmet, thank heavens. I might be dead.

I’ve felt different since. Calm, oddly. Something knocked loose.

It’s been several days, and I’ve hit the source. My anxiety, struck clear out my brain. It’s a terrible thing, to loose something you’ve held onto so tightly and for so long.

I’m not sure how all this will play out. I’d like to fear for the worst, but it just won’t come.

Posted in Uncategorized | Leave a comment

Anything.else

Last time I blabbed about why anything.el is rad, but left you hanging with just a few out-of-the-box implications or that rad-ness. This time, let’s use anything.el to make our own anything.

The simplest anything.el customization is making your own anything command. You’ll find an example of this in anything-config.el.

(defun my-anything ()
       (interactive)
       (anything-other-buffer
        '(anything-c-source-buffers
          anything-c-source-file-name-history
          anything-c-source-info-pages
          anything-c-source-info-elisp
          anything-c-source-man-pages
          anything-c-source-locate
          anything-c-source-emacs-commands)
        "*my-anything*"))

anything-other-buffer is a simplified version of anything that takes a list of sources for its first argument and a buffer name as its second. So M-x my-anything now gives you an anything selection buffer for all them sources there. You’ll find a wealth of pre-baked sources to choose from in anything-config.el.

The next level of awesomeness comes when you create your own source, which is cheese easy. Do a C-h v on anything-sources. Here you’ll get a lengthy description of how to set one of these buggers up. I’ve made the world’s simplest, most contrived, and most stupidly named source below.

(setf my-pretend-files-source
      '((name . "Name for My Source")
		(candidates . ("a" "list" "of" "files"))
		(type . file)))

There are three mandatory keys: name, candidates, and either action or type. Here I’m pretending to be looking at a list of files, so my type is file. Types provide common actions for things you’ll commonly be working with. They’re a shortcut to actually writing your own actions.

Now that we’ve got a source, let’s make an anything command that uses it.

(defun anything-for-pretend-files ()
  (interactive)
  (anything-other-buffer '(my-pretend-files-source)
                         "*anything-select-buffer-name*"))
anything-for-pretend-files

anything-for-pretend-files

Try out this custom command and hit tab on one of the items to see what you can get with the file type. You can find a list of types with a C-h v of anything-type-attributes.

The "file" type

Options for the file type

If you’re not using a type, you’re using an action. The action key takes a list of alists, each of which maps one name string to one function. Something like this:

(action . (("Action name" . (lambda (selection)
                              (do-some-crazy-shit-to selection)))))

Of course, that lambda could be a function name. Let’s make a real action. A real stupid action!

(setf things-to-say-source
      '((name . "Things To Say")
        (candidates . ("Llama" "Guano" "Mochaccino" "Colostomy"))
        (action . (("Say it!" .
                    (lambda (selection)
                      (message selection)))
                   ("Spray it!" .
                    (lambda (selection)
                      (message (concat selection "thpppt"))))))))

(defun anything-things-to-say ()
  (interactive)
  (anything-other-buffer '(things-to-say-source)
                         "*anything things to say*"))
anything-things-to-say

anything-things-to-say

“Say it!” is the default action, it’s what happens when we hit return. “Spray it!” is an alternate action, which we can select by hitting tab on an item. Remember that this action selection screen is another anything screen, so you can arrow and C-n, C-p through it, but you can also just start typing what you want.

Spray it!

Spray it!

colostomy

Yes!

Next steps

Now these examples are collosally stewpid. What’s more, they use only a tiny fraction of the shite tonne of source options anything‘ll take. If your curiosity is tingling, C-h v anything-sources and prepare to have your amazement blown.

And if you’re really and truly rapt, tune in next time, when we’ll make a for real anything-project-files source that lets you fly around the files of your gigantic, deeply nested project like an airbus on an eightball. It’ll be fun.

Posted in Emacs, Programming | Tagged , , , , | 1 Comment

What can I get for 10 dolla? Anything.el.

Actually it’s not even $10. It’s free, both as in speech and as in nachos. So in the spirit of fanning both sides of the fire, here’s a quick look at anything.el for (gasp) Emacs.

Anything.el is most certainly one of Emacs’s killer features. It’s like ido-mode on steroids, but without the rage or shrinking testicles.

It’s been described as Quicksilver for Emacs, which I guess is kinda true, but that’s not the way I use it.

The documentation for the anything function says it all:

Select anything. In Lisp program, some optional arguments can be used.

Tomas has a poet’s soul, and what he means when he says “some optional arguments can be used” is that you can go bat-shit crazy with what goes in, what gets displayed, what comes out, what happens then, where am I, why am I naked?

But before we get there, let’s cover the basics, shall we?

Installation

M-x auto-install-batch is one option. auto-install.el is here and it’s super handy. Ask for the “anything” package, and you’ll get anything.el, anything-config.el, and anything-match-plugin.el. You’ll want all three. Of course you can just follow the links and install the files yourself if that’s your thing.

Once you’ve got these boys somewhere handy, just

(add-to-list 'load-path "/path/to/anything/folder")
(require 'anything-match-plugin)
(require 'anything-config)

Eval all that, and you’re ready to go.

Usage

M-x anything

Now we’re in the *anything* buffer, and you can have a merry time typing away at any part of the filename or filepath you’re after. Behold as anything.el starts chipping away at the options. C-n and C-p behave as expected. Left and right arrow keys move you to different groups of things.

Because you’ve got the match plugin installed, you get some bonus features. Say you start typing your filename, and you’ve still got 50 matches. Add a space and you’re in a whole new pattern group. So now you start typing something unique in the path, and viola, you’re looking at a full-on double rainbow.

Once you’ve found your victim, hit enter to do the default thing, which is usually the thing you want. If you’ve got eccentric taste, hit tab instead for a list of other things you can do with the thing you’ve selected. Thing.

More Youthage

Things get real crazy when you crack open anything-config.el. Go ahead and anything-imenu up in there and type in “anything”. Here’s a list of custom anything.el commands you get for free. Who needs nachos? You’ll have a riotous time mapping keys to the bazillion-ish anything.el commands the anything-config.el crew have put together for you.

For my part, C-x b is anything-mini, which serves up a combination of buffers and recent files. C-x C-i is anything-imenu, which is awesome wearing a hat. C-x f is anything for recent files, and C-x C-f is ido-find-file, which still trumps anything.el for navigating about file systems.

But wait, there’s more!

You remember that part about going guano building your own who-knows-whats on top of anything.el? It’s anything.el‘s finest suit, but alas, a subject for another post.

Posted in Emacs, Programming | Tagged , , , , | 18 Comments

Visdom, briefly.

Wanna evaluate a line of code in your .vimrc without having to so: the whole damn file?

noremap  <C-x><C-e> :exe getline(".")<CR>

I stole the binding from Emacs (suckers). Put yer cursor on the line of vim script you want to evaluate and control-x, control-e.

Great for testing mappings and commands and all whatnot.

You do have your caps lock remapped to control, right? Don’t make me check your pinky.

Posted in Programming, Vim | Tagged , , , , | Leave a comment