On Wednesday, June 4, 2014 9:42:41 AM UTC-5, Mars0i wrote:
>
> ... Then I add the new functions to the declare statement by hand, or I 
> periodically do something like:
>
> grep defn mysourcefile.clj >> mysourcefile.clj
> (Be careful to use two ">"s!)
>
> and then I edit the junk at the end of the file into a declare statement 
> at the top of the file.  And maybe if f I were ... lazier, I'd code a 
> script that would update the declare in one pass.
>

OK, I couldn't resist my own implicit challenge. 

#!/bin/sh
sourcefile="$1"
newsourcefile="new.$sourcefile"

newdeclare=$(echo '(declare' \
    `sed -n '/defn/s/(defn-* //p' "$sourcefile" | tr '\n' ' '` ')' \
    | sed 's/ )/)/')

sed "s/(declare .*/$newdeclare/" "$sourcefile" > "$newsourcefile"

This writes a new version of the file named new.<oldfilename>. Or if you 
either trust your script or trust your backups, and are on a system that 
includes the mighty ed <http://www.gnu.org/fun/jokes/ed.msg.html> editor, 
you can replace the last line with:

echo "1,\$s/(declare .*/$newdeclare/\nw\n" | ed "$sourcefile"

which edits the file in place, assuming that the previous version of the 
declaration was on one line.  You may want to use a different scriptable 
editor.

The messy part is the sed and tr line:

    `sed -n '/defn/s/(defn-* //p' "$sourcefile" | tr '\n' ' '`

The sed part finds all of the lines with "defn" in them, then substitutes 
the empty string for "(defn" or "(defn-".   'tr' then removes the newlines 
between the function names, replacing the newlines with spaces.  You'll 
need something a little more complicated if you put the parameter vector or 
anything else on the same line as the function name.  The 'echo' on the 
previous line, along with the final ')' adds "(declare" and its closing 
parenthesis.  Those two lines can be used by themselves to generate a 
declare statement from the command line. The 'sed' command after these 
lines isn't necessary; it just removes an unnecessary space before the 
closing parenthesis. 

Obviously, there will be source files on which this won't work.  It's not 
worth making it foolproof.

It's a certainty that others would code this more elegantly or more 
succinctly.  It could be written in Clojure, obviously, but still wouldn't 
be foolproof unless someone hacks it from the Clojure parser.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to