Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

samtregar (2699)

samtregar
  (email not shown publicly)
http://sam.tregar.com/

Journal of samtregar (2699)

Wednesday July 05, 2006
03:48 PM

Using Perltidy with Emacs

[ #30185 ]
I noticed this on the CPAN radar today:

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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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

  • 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.