You should be automatically redirected . If not, visit
http://newlisper.wordpress.com
and update your bookmarks.

14/12/2005

Running newLISP scripts in BBEdit/TextWrangler

If you're using BBEdit or TextWrangler on MacOS X, it's easy to write newLISP scripts that process text windows. Store your scripts in the ~/Library/Application Support/BBEdit/Unix Support/Unix Filters/ folder.

A basic script looks like this:

#!/usr/bin/newlisp
(dolist (file-name (2 (main-args)))
    (set 'file (open file-name "read"))
    (while (read-line file)
        (println (current-line)))
    (close file))
(exit)

Notice the 2 in (2 (main-args)): this extracts the name of the file from the list of arguments that BBEdit passes to newLISP. The arguments are as follows:

  1. argument 0: "/usr/bin/newlisp"
  2. argument 1: path to the Unix filter
  3. argument 2: path to BBEdit's temporary copy of the document

So this script processes argument 2, the temporary version of your current document that BBEdit creates.

As it stands, this script simply replaces each line of the current window with itself. To make it do something more useful, just insert more code:

(dolist (file-name (2 (main-args))) ; 2
    (set 'file (open file-name "read"))
    (while (read-line file)
        (println 
            (replace {(\d+)} 
                (current-line) 
                (string (add (int $1) 1)) 0 )))
    (close file))
(exit)

This adds 1 to every number in the text.

By the way, BBEdit/TextWrangler can run Unix filters on the current selection only, not just the whole file.

4 Comments:

At 13:28, Blogger don Lucio said...

Perhaps a typo? The index for main-args should go after (main-args). The way it is written you will extract more than one argument:

~> newlisp a b c d
newLISP v.8.7.4 on OSX, execute 'newlisp -h' for more info.

> (main-args)
("newlisp" "a" "b" "c" "d")
> (1 (main-args))
("a" "b" "c" "d")
> (2 (main-args))
("b" "c" "d")
> (3 (main-args))
("c" "d")
> (4 (main-args))
("d")

what you want goes with the index after (main-args):

> ((main-args) 1)
"a"
> ((main-args) 2)
"b"
> ((main-args) 3)
"c"
> ((main-args) 4)
"d"

And there is a short-cut for ((main-args) idx):

> (main-args 1)
"a"
> (main-args 2)
"b"
> (main-args 3)
"c"
>

Thanks for this great tip how to use newLISP scripst to work with BBEdit or TextWrangler. This could be used to write text formatters etc.

 
At 15:42, Blogger don Lucio said...

... turns out newLISPer's code was right! Because he really wants the list of files and not the single elements ... ("afile") versus "afile".

I realized then trying is out it TextWrangler.

But perhaps the little lesson about indexing in newLISP can help somebody else in a different problem ;)

But we could shorten the program, because we are really only interested in one file name: "afile" versus ("afile").

#!/usr/bin/newlisp

(set 'file (open (main-args 2) "read"))
(while (read-line file)
(println
(replace {(\d+)}
(current-line)
(string (add (int $1) 1)) 0 )))
(close file))
(exit)

 
At 16:10, Blogger don Lucio said...

a parenthesis too much in the last comment, here the correction:

#!/usr/bin/newlisp

(set 'file (open (main-args 2) "read"))
(while (read-line file)
(println
(replace {(\d+)}
(current-line)
(string (add (int $1) 1)) 0 )))
(close file)
(exit)

And here 2 useful scripts to comment and uncomment a section of code:

comment.lsp:

#!/usr/bin/newlisp

(set 'file (open (main-args 2) "read"))
(while (read-line file)
(println ";" (current-line)))
(close file)
(exit)

uncomment.lsp

#!/usr/bin/newlisp

(set 'file (open (main-args 2) "read"))
(while (read-line file)
(println (1 (current-line))))
(close file)
(exit)

 
At 17:27, Blogger newlisper said...

Thanks for the comments! Indeed, I should perhaps stress a bit more somewhere that I'm a beginner (perhaps 'newlisper' is too self-referential? :-)) and have just started learning this myself... The experts all live over at the main newLISP forum. :-)

All corrections more than welcome!

 

Post a Comment

Links to this post:

Create a Link

<< Home