10.6: Format text via a Service and emacs

Oct 28, '09 07:30:05AM

Contributed by: renaud

Snow Leopard only hintOne 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:

  1. Open Automator, create a new Service.
  2. Set it up so the Service receives selected text from any application and replaces selected text.
  3. Add a new Run Shell Script action.
  4. Type in the script below, and save the Service.
Script:
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 $tmp
Hitting Control-X Control-S Control-X Control-C will quit Aquamacs and pipe its buffer in place of the previously selected text.

Notes: [robg adds: I haven't tested this one.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091012041837512