A piece of intertube about the Clojure programming language, algorithms and artificial intelligence.

Tuesday, January 29, 2013

Searching in Clojure and ClojureScript files with Ack

"Ack is a tool like grep, optimized for programmers". I knew the existence of Ack since a few years but I never gave it try. Now that I did, I'm positively surprised: it's easy to use and fast.

I was always fighting in Emacs between grep (what is the syntax?), igrep-find (why did it suddenly stopped to prompt for directories?), tags-search (ooops some of my files are not tagged) and rgrep (oh it did exist?). I installed ack-and-a-half for Emacs and its great. Ack-and-half tries to find automatically the root directory of your project but you can set ack-and-a-half-prompt-for-directory to true with (setq ack-and-a-half-prompt-for-directory t) for it to ask which directory to search in.

The Ack version on the website provides support for Clojure files. For ClojureScript files, you need to add the following lines to your ~/.ackrc file:

--type-add
clojure=.cljs

Et voilĂ , enjoy!

Tuesday, January 15, 2013

Duplicating s-expressions on a line

Duplicating a line is extremely useful when editing text and specially when programming. There is no native command in Emacs to achieve that but it's easy to add one.

It's easy to get addicted to the use of this new command but a problem remains when programming in Lisp with paredit.el: duplicating sometimes lead to invalid s-expressions being inserted.

I decided to give it a try and made an implementation that duplicate the s-expressions after the point (cursor). For instance in Clojure if you are editing this code:

(ns myns
  (:use [clojure.string :only [escape]]))
You can duplicate the first vector by placing the cursor on the square bracket, invoking the command with one keystroke and then easily obtain this code:
(ns myns
  (:use [clojure.string :only [escape]]
        [clojure.set :only [union]]))
Here is the code to duplicate the line; to my knowledge there is no such command in paredit.el:

Not really pretty but it does the work, feel free to provide a nicer implementation in the comments or add it to your ~/.emacs.d/init.el file with:
(eval-after-load "paredit"
  '(progn (define-key paredit-mode-map (kbd "C-S-d") 'paredit-duplicate-after-point)))
Edit: Here a fix for when the sexp is the last expression in the buffer.

Followers