One very nice emacs feature is its text formatting functions. Combining the Snow Leopard Automator Services with emacs' scripting capabilities, one can easily, from any application, refill a paragraph to lines of 70 characters and indent list items:
- Open Automator, create a new Service.
- Set it up so the Service receives selected text from any application and replaces selected text.
- Add a new Run Shell Script action.
- Type in the script below, and save the Service.
tmp=/tmp/emacsmail`date "+%s"`.txt cat /dev/stdin | tr '\r' '\n' > $tmp /usr/bin/emacs -batch $tmp \ -eval "(prefer-coding-system 'utf-8)" \ -eval '(setq paragraph-start "\\-\\|$" paragraph-separate "$")' \ -eval '(fill-region (point-min) (point-max) nil)' \ -eval '(untabify (point-min) (point-max))' \ -f save-buffer cat $tmp
Provided that Aquamacs is installed, one can also edit text interactively with this version of the service:
tmp=/tmp/emacsmail`date "+%s"`.txt cat /dev/stdin | tr '\r' '\n' > $tmp /Applications/Aquamacs\ Emacs.app/Contents/MacOS/Aquamacs\ Emacs \ -eval "(prefer-coding-system 'utf-8)" \ $tmp cat $tmpHitting Control-X Control-S Control-X Control-C will quit Aquamacs and pipe its buffer in place of the previously selected text.
Notes:
- The script above can be adapted to tools other than Emacs. Yet, within Cocoa windows, text is usually UTF-8 with CR line breaks. Since some UNIX tools only detect LF breaks, you may need to filter text with tr '\r' '\n' | command | tr '\n' '\r'.
- Hot keys can easily be assigned to services: System Preferences » Keyboard » Keyboard Shortcuts Tab » Services Menu.
•
[5,788 views]

