(defun my-perltidy-replace (arg) "Replace the contents of the current buffer with the output of perltidy, and make a backup of the current buffer. Before and after modifications to contents of buffers are being made, all buffers that need to be auto-saved are being auto-saved. The major-mode of the buffer containing the backup is set to cperl-mode." (interactive "P") (do-auto-save) (let ((tidy_buffer (generate-new-buffer (generate-new-buffer-name (concat "TidyBackup-" (buffer-name)))))) (with-current-buffer (buffer-name) ;; swapping the text can screw up linum mode with two buffers in ;; the same frame when automatically enabled by cperl-mode (linum-mode -1) (shell-command-on-region (point-min) (point-max) "perltidy --standard-output" tidy_buffer) (buffer-swap-text tidy_buffer)) (with-current-buffer tidy_buffer (auto-save-mode nil) (do-auto-save) (cperl-mode)) (linum-mode 1) (message "buffer contents replaced with output of perltidy; backup is in %s" tidy_buffer))) (eval-after-load 'cperl-mode '(define-key cperl-mode-map (kbd "C-x t i") 'my-perltidy-replace))