This is a gentle reminder that you are not forced to limit yourself to any app’s built-in features when you work on Mac OS X thanks to Services with an assist from Automator. I recently utilized Automator’s Run Shell Script action to create a needed function in Xcode’s text editor.
My problem is that I follow a style guide that limits line length to 80 characters. I usually don’t have trouble with this. Xcode will show a gutter line to help prompt me to break my lines around the limit. But when editing multi-line comments it becomes a pain to reflow the new text. Other editors can do this without my help.
The hockey assist in this story goes to Emacs which has an intelligent function for reflowing text. I knew of this, but I didn’t know that as of a recent Emacs release I could finally tie that function to Xcodes’s editor. Emacs can run elisp scripts on launch, meaning that I can pass it some text, Emacs processes it, and with the Run Script action I get the results back in my editor window.
Here’s the shell script which invokes Emacs and calls its fill-region function. Props to the StackOverflow answer here.
Save that script and make it executable, then create an Automator action to launch that script and check the options to replace selected text. Save it as a Service, and that’s it. Assign it a key command sequence for even more convenience. It’s that simple to make any text editor a beast.