Sep 15, 2014; 09:41
Jolle Carlestam
Using Lasso as Text Filter in Bbedit - A non Weekly Tidbit
One of Ke Carltons many qualities is that the code he produces always looks so clean and structured. As an example, he always makes sure that similar blocks are aligned on equal signs.
Like this:
define dsyotem() => ds(::c185302_yotem2)
define dsyo_ptl_role() => dsyotem -> table(::ptl_role)
define dsyo_operator() => dsyotem -> table(::operator)
define dsyo_latest_search() => dsyotem -> table(::latest_search)
or
local(
template = 'basic.inc',
main_content = string,
noerror = true,
showform = true,
error_fields = array,
resetid = wrp('D') or wrp('resetid')
)
It does make the reading easier, doesn=92t it!
The other day I found out that he=92s using a cheat. Or, not really cheating, but rather a plugin to his editor of choice, Sublime. It=92s a smart plugin that analyzes the selected text block and makes sure it aligns as shown above.
That gave me no rest. Surely there must be something similar floating around for Bbedit. A lot of googling later and some testing of found results (meager) did not satisfy me. Until I realized that the text filters used in Bbedit is nothing but Unix script files called from within Bbedit. Most of them seems to be written in Perl. But Bbedit doesn=92t care what language they are written in. As long as they can be run as command line scripts, accepts text input when called and returns a result. And lo and behold, Lasso 9 can do that!
It took me less than a minute to do an initial test. A Lasso script file put in the designated folder, a restart of Bbedit and it worked. To actually have it produce the outcome I desired was trickier. But I think I have that part worked out too. See code accompanying this email.
Here=92re the steps you need to take.
1 Save the code to a text file. It can have any suffix, I called it Valign.lasso. Make sure it starts with the row: #!/usr/bin/lasso9
2 Put the file in ~/Library/Application Support/BBEdit/Text Filters/
3 Restart Bbedit. This will produce a new Text Filter that you can find under the menu Text/Apply Text Filter/Valign.
You can also find it in the palette Text Filters
4 In the palette window Text Filters you can also assign it a keyboard shortcut. I have given my filter the shortcut Control - T
5 Select a block of text and hit Control - T, or select Valign From Text Filters. Voila, text is aligned on equal sign! If you don=92t select a text block the filter will work on the entire file
Should you have the need of tweaking my code, then go ahead. Edit the file Valign.lasso and save. There is no need to restart Bbedit, changes will be in effect at the next call to the filter.
There are for example two settings hardcoded into the file.
The number of spaces that equal a tab width. I have mine set to 4 but it is a personal preference that you might have set differently.
And the linefeed being used. I have Bbedit set to use Unix linefeeds, \n, but again yours could be something else. If you have different linefeeds in different files then you would need to expand the code with a linefeed check. An easy exercise that I leave to you.
Finally, here=92s the code:
#!/usr/bin/lasso9
//[
local(
// Your settings
tabwidth = 4, // Set this to whatever your Tab Width is set to in Bbedit
linefeedchar = '\n', // set this to the linefeed you're using in your Bbedit files
// The rest
textinput = file_stdin -> readbytes,
regexp = regexp(`^(.*?)(\t| )+=(.*?)$`),
maxlength = 0,
rows = array,
output = array,
textlength = {
local(
inputtext = #1,
tabcount = #inputtext -> split('\t') -> size - 1,
textsize = #inputtext -> size
)
return (#tabcount * #tabwidth) + #textsize - #tabcount
} -> detach,
parts, partone, parttwo, partsize, extratab
)
with line in string(#textinput) -> split(#linefeedchar) do {
#regexp -> input = #line
if(#regexp -> find) => {
#partone = #regexp -> matchstring(1) -> asstring
#parttwo = #regexp -> matchstring(3) -> asstring
#partsize = #textlength(#partone)
#parts = (: #partone, #parttwo, #partsize)
#maxlength = max(#maxlength, #partsize)
#rows -> insert(#parts)
else
#rows -> insert(#line)
}
}
#maxlength % #tabwidth == 0 ? #extratab = '\t'
with line in #rows do {
if(#line -> isa(::staticarray)) => {
#output -> insert(
#line -> first +
#extratab +
('\t' * (integer((decimal(#maxlength - #line -> last
+ ((#line -> last % #tabwidth))) / decimal(#tabwidth)) -> ceil)))
+ '=' + #line -> second
)
else
#output -> insert(#line)
}
}
#output -> last -> size == 0 ? #output -> removelast
#output -> join(#linefeedchar)
//]
Have fun
Jolle
#############################################################
Attend the Lasso Developer Conference 2014!
October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
http://www.lassosoft.com/LDC-newmarket-2014
#############################################################
This message is sent to you because you are subscribed to
the mailing list Lasso Lasso@lists.lassosoft.com
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <Lasso-unsubscribe@lists.lassosoft.com>
Send administrative queries to <Lasso-request@lists.lassosoft.com>
Sep 15, 2014; 19:48
Ke Carlton
Re: Using Lasso as Text Filter in Bbedit - A non Weekly Tidbit