UbuntuでEmacsからPerlの整形ツールperltidyを使う設定

perltidyという整形ツールがある.設定ファイルで自分好みにカスタマイズできるのが素晴らしい.Ubuntuで導入し,Emacsで使えるようにする.

インストール

% sudo aptitude install perltidy

設定ファイルを作成

設定ファイルは無くても動く.~/.perltidyrc というファイルがあればそれを読んでくれる.
Perl Best Practices には以下のように載ってるらしい(perltidyを使ってみました - libnitsuji.so)のでそれを流用.

-l=78   # Max line width is 78 cols
-i=4    # Indent level is 4 cols
-ci=4   # Continuation indent is 4 cols
-st     # Output to STDOUT
-se     # Errors to STDERR
-vt=2   # Maximal vertical tightness
-cti=0  # No extra indentation for closing brackets
-pt=1   # Medium parenthesis tightness
-bt=1   # Medium brace tightness
-sbt=1  # Medium square brace tightness
-bbt=1  # Medium block brace tightness
-nsfs   # No space before semicolons
-nolq   # Don't outdent long quoted strings
-wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="
        # Break before all operators 

これでコマンドラインからperltidyが使えるようになった.
例:

% perltidy hoge.pl

Emacsの設定

Emacsからperltidyを使えるようにする.EmacsWiki: CPerl Modeの設定を.emacsに記述.

(defun perltidy-region ()
   "Run perltidy on the current region."
   (interactive)
   (save-excursion
     (shell-command-on-region (point) (mark) "perltidy -q" nil t)))
 (defun perltidy-defun ()
   "Run perltidy on the current defun."
   (interactive)
   (save-excursion (mark-defun)
                   (perltidy-region)))

M-x perltidy-region でリージョンを整形できる.
とりあえず Ctrl-c,t に割り当てた.

(global-set-key "\C-ct" 'perltidy-region)