Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

How to extend Smultron using Commands Apps
The Smultron editor keeps getting better and better. As a long-time vi user, I've been trying Smultron on and off for a year or so. In the latest major version, an essential feature was added: the ability to filter selected text through an arbitrary command. The way this works, you go to Tools » Handle Commands » Show Commands Window, where you will find a form where commands can be defined. I have several of these, but I want to focus on just two, and I think they'll illustrate how it all fits together.

My problem is that I need to go back and forth from the classic UNIX line-oriented approach, seen in the vi editor and the *roff family of formatters, and the GUI paragraph-oriented approach. The key problem is that in the line-oriented approach, the newline character doesn't indicate the end of a paragraph, while in GUI mode, it does. The ideal solution to this is to have some way easily to convert between the two, and thanks to the selection-filtering capacity of Smultron, it is now easy to do this.

There are four relevant fields in the command-entry pane (there is a shortcut field which I'm ignoring). The two commands I'm describing here are par w79 and par w9999, and those are entered into the Name fields for each command. Note that the par command is a common UNIX command, but is not included in the OS X distribution. You can get it here or here.

I decided to name the commands using the par argument syntax: par w79 means format paragraphs so that no line is more than 79 characters long, and par w9999 means that lines can be up to 9,999 characters long, which is very long. In both cases, I checked the Inline box and specified /bin/ksh as the Interpreter. The command itself is entered in the box below. These command both have two lines:
#!/bin/ksh
par wNNN < %%s
Just replace NNN with either 79 or 9999, as needed for each command. That's it.

Now, if you have a document previously edited with vi that you want to edit with Smultron, open it, use Command-A to select all, and then run the par w9999 command you just defined from the Tools » Commands menu. It will convert the file into the "one newline per paragraph" format that is most convenient in a GUI-based editor such as Smultron. If you want to be able to edit the file in vi, or if it must be processed by a line-edited text formatter, you can put it back into line-oriented mode by once again selecting everything and running the par w79 command.

Many other filters are possible, of course, such as sorting lines, removing or inserting columns, converting from upper to lower case and vice versa, and so on. In these examples, I selected the entire document each time, but it is quite reasonable to select just a single paragraph, sentence, word, or any other segment of the document.

Note that par is very configurable, and it is possible to get unexpected results if it is not configured correctly. Here's the configuration string I use:
PARINIT='rTbgqR B=.,?_A_a Q=_s>| P=.
[robg adds: The new Handle Commands window in Smultron is a nice addition, and I've used it myself to do a bit of text processing. I haven't tested the above par tip, however.]
    •    
  • Currently 2.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[9,817 views]  

How to extend Smultron using Commands | 8 comments | Create New Account
Click here to return to the 'How to extend Smultron using Commands' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
How to extend Smultron using Commands
Authored by: broomdodger on Jun 16, '07 06:33:32PM

par seems to truncate lines longer than 512 characters.
Is there a way around this limitation?



[ Reply to This | # ]
How to extend Smultron using Commands
Authored by: gshenaut on Jun 17, '07 06:40:59AM
I tested for this by running these two commands:
(ls /usr/bin | tr '\n' ' ' ; echo) | wc
(ls /usr/bin | tr '\n' ' ' ; echo) | par | wc
The wc(1) command writes out the number of lines, words, and characters in a stream. On my system, the output of the first command, without par, was "1 753 5754", indidcating 1 line, 753 words, and 5,754 characters (all of which were on a single line). The second command, with par, produced "84 753 5753", indicating that the single line of 5,754 chars was wrapped to produce 84 lines.

Perhaps there is some other reason that your lines are being truncated?

By the way, par will complain about words that are longer than the max line width, since it doesn't break words. Could that be what you are seeing?

[ Reply to This | # ]

How to extend Smultron using Commands
Authored by: broomdodger on Jun 17, '07 07:50:59AM

I installed par, added the environmental variable to .profile

PARINIT='rTbgqR B=.,?_A_a Q=_s>| P=.'

opened Smultron, added the commands

#!/bin/ksh
par w79 < %%s

and

#!/bin/ksh
par w9999 < %%s

selected a paragraph, used the par w79 command, and the paragraph was nicely wrapped, but truncated at 511 chars.

Could it be a Smultron problem?
Have you tried it with a long line in Smultron?



[ Reply to This | # ]
How to extend Smultron using Commands
Authored by: gshenaut on Jun 17, '07 05:57:25PM

Yes, I have tried it with long lines in smultron. Just to be sure, I re-did the "ls /usr/bin" test, but I saved the 5,753-character line in a file and edited it in smultron. Then I ran my "par w99" script and saved the result. It was fine, 76 wrapped lines, 5,753 characters.

Are you using smultron 3.1 ? Maybe that's what's going on.

Par probably isn't the ideal filter for this. You might try fmt(1), for example. I prefer par because like vi, it can be told about lines beginning with dot, like .PP as in the *roff formatters. In fact, a simple, special-purpose filter would be better than either one.



[ Reply to This | # ]
How to extend Smultron using Commands
Authored by: broomdodger on Jun 17, '07 09:03:08PM
Are you using smultron 3.1 ? Maybe that's what's going on.
Yes, version 3.1 MacOS 10.4.9 PPC
Should I try a different version?

[ Reply to This | # ]
How to extend Smultron using Commands
Authored by: broomdodger on Jun 17, '07 09:27:11PM
Are you using Smultron 3.1 ? Maybe that's what's going on.
I just now tried Smultron 2.2.7 and it gives "Invalid parameter" for the "<"

[ Reply to This | # ]
How to extend Smultron using Commands
Authored by: gshenaut on Jun 18, '07 08:35:01AM

No, an earlier version wouldn't help. The ability to filter selected text was new in 3.0, and 3.1 fixed various issues, so I was just thinking that you may have been using 3.0 and that this was a bug in that version.

How about the version of par ? I seem to have version 1.52, installed via darwinports. (You can check yours by the command "par version".)

In any case, you should compare the results of running the par command with the same input text both within smultron and from the command line, to see if you get truncated text both times. If you do, then it's undoubtedly a par problem; if you get it only within smultron, it's a smultron problem; and if you get it only on the command line, then I'd have no idea what's going on.

Greg



[ Reply to This | # ]
How to extend Smultron using Commands
Authored by: broomdodger on Jun 18, '07 08:12:03PM
How about the version of par ? I seem to have version 1.52, installed via darwinports. (You can check yours by the command "par version".)
I also have par 1.52 vial darwinports.

Right now I am more interested in using par with TextExtras inside TextEdit.
I am new to unix scripting but not programming, and do not know the syntax.

Have you tried that?

[ Reply to This | # ]