Using Perltidy with Emacs

samtregar on 2006-07-05T20:48:18

I noticed this on the CPAN radar today:

perl-tidy-mode

From the docs it sounds like it runs perltidy when you save your file. That doesn't sound terrible, but I like my solution better. I have bindings so I can run perltidy on-demand, either on the whole file or on a particular region. Check it:

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

(defun perltidy-all ()
    "Run perltidy on the current region."
    (interactive)
    (let ((p (point)))
      (save-excursion        
        (shell-command-on-region (point-min) (point-max) "perltidy -q" nil t)
        )      
      (goto-char p)
      (cperl-mode)))

(global-set-key "\M-t" `perltidy-region)
(global-set-key "\M-T" `perltidy-all)

-sam


My version.

Dom2 on 2006-07-06T06:36:28

The one I use attempts to mark the current function if you haven't explicitly marked anything. Otherwise, it runs it over the whole file (not sure why).
(defun perltidy ()
  "Run perltidy on the current region or buffer."
  (interactive)
  (save-excursion
    (unless mark-active (mark-defun))
    (shell-command-on-region (point) (mark) "perltidy -q" nil t)))

-Dom

Re:My version.

Dom2 on 2006-07-06T17:00:47

Nope, it doesn't run over the whole file by default. I'll have to make it do that.

-Dom

Does too!

jjore on 2006-07-06T16:03:15

Yes... but that's in the README. I think the most useful part of the thing is that it's a minor mode so it can be automatic but it did also include just the plain "perltidy this region or the entire buffer" code.

It also goes to a little bit of trouble to fix things if you don't have a perltidy binary but have called it anyway.