http://www.perlmonks.org?node_id=633399

silent11 has asked for the wisdom of the Perl Monks concerning the following question:

I'm not a recent convert to Emacs, however I have yet to get too deep into its internals. After one day of using perltidy-mode I've become very fond of it. With every save, the file gets run through perl tidy, reformatting and beautifying my code.

The problem is that with each save (and reformatting) I loose some/most of the syntax highlighting. I have to M-x cperl-mode after each save to re-highlight the source code.

Has anyone else run into this before or have any suggestions?

I want to keep cperl-mode (syntax highlighting) and continue to have the benefit of having my code *pretty* as well .

TIA.



-silent11

Replies are listed 'Best First'.
Re: Emacs and perltidy-mode
by diotalevi (Canon) on Aug 17, 2007 at 22:50 UTC

    Perhaps you aren't properly associating cperl-mode with your file. Here's some lisp-fu for you. The file is also available at http://diotalevi.isa-geek.net/~josh/emacs/my-cperl.el

    ;; Autoloading perltidy (autoload #'perltidy "perltidy" nil t) (autoload #'perltidy-mode "perltidy" nil t) (autoload #'perltidy-unless-readonly-mode "perltidy" nil t) (eval-after-load "cperl-mode" '(add-hook 'cperl-mode-hook #'perltidy-u +nless-readonly-mode)) (eval-after-load "perl-mode" '(add-hook 'perl-mode-hook #'perltidy-u +nless-readonly-mode)) ;; Autoloading perl-syntax (autoload #'perl-syntax "perl-syntax-mode" nil t) (autoload #'perl-syntax-region "perl-syntax-mode" nil t) (autoload #'perl-syntax-mode "perl-syntax-mode" nil t) (defalias 'perlsyntax-mode 'perl-syntax-mode) (eval-after-load "cperl-mode" '(add-hook 'cperl-mode-hook #'perl-synta +x-mode)) (eval-after-load "perl-mode" '(add-hook 'perl-mode-hook #'perl-synta +x-mode)) ;; Autoloading perlcritic (autoload 'perlcritic "perlcritic" "" t) (autoload 'perlcritic-region "perlcritic" "" t) (autoload 'perlcritic-mode "perlcritic" "" t) (eval-after-load "cperl-mode" '(add-hook 'cperl-mode-hook #'perlcritic +-mode)) (eval-after-load "perl-mode" '(add-hook 'perl-mode-hook #'perlcritic +-mode)) (defun perl-interpreter (&rest interpreter-else) (save-restriction (widen) (save-excursion (goto-char (point-min)) (if (looking-at auto-mode-interpreter-regexp) (match-string 2) (car interpreter-else))))) (defun perl (start end) "Runs the current region or buffer with perl" (interactive (if (if (boundp 'mark-active) mark-active (mark)) (list "r") (setq start (point-min)) (setq end (point-max)) (list "i"))) (shell-command-on-region start end (perl-interpreter "perl"))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Parrots ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (autoload 'pasm-mode "pasm-mode" nil t) (add-to-list 'auto-mode-alist '("\\.pasm" . pasm-mode)) (add-to-list 'interpreter-mode-alist (cons "pasm" 'pasm-mode)) (autoload 'pir-mode "pir-mode" nil t) (add-to-list 'auto-mode-alist '("\\.\\(imc\\|pir\\)" .pir-mode)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Pod ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (autoload 'pod-mode "pod-mode" nil t) (add-to-list 'auto-mode-alist '("\\.pod" . pod-mode)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Associate cperl-mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (add-to-list 'auto-mode-alist '("\\.[Pp][LlMm][Cc]?$" . cperl-mode)) (while (let ((orig (rassoc 'perl-mode auto-mode-alist))) (if orig (setcdr orig 'cperl-mode)))) (while (let ((orig (rassoc 'perl-mode interpreter-mode-alist))) (if orig (setcdr orig 'cperl-mode)))) (dolist (interpreter '("perl" "perl5" "miniperl")) (unless (assoc interpreter interpreter-mode-alist) (add-to-list 'interpreter-mode-alist (cons interpreter 'cperl-mode +)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Associate pod-mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (add-to-list 'auto-mode-alist '("\\.[Pp][Oo][Dd]$" . pod-mode)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Autocomplete ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defadvice cperl-indent-command (around cperl-indent-or-complete) "Changes \\[cperl-indent-command] so it autocompletes when at the en +d of a word." (if (looking-at "\\>") (dabbrev-expand nil) ad-do-it)) (defun cperl-dabbrev-installer () (set (make-local-variable 'dabbrev-case-fold-search) nil) (set (make-local-variable 'dabbrev-case-replace) nil)) (eval-after-load "cperl-mode" '(progn (require 'dabbrev) (ad-activate 'cperl-indent-command) (add-hook 'cperl-mode-hook #'cperl-dabbrev-installer))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Prove ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun cperl-prove () "Run the current test." (interactive) (shell-command (concat "prove -v " (buffer-file-name)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Devel::Graph ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun devel-graph () (interactive) (shell-command-on-region (point-min) (point-max) "perl -MDevel::Graph -0777 -pe '$_=Devel::G +raph->graph(\\$_)'")) (eval-after-load "cperl-mode" '(add-hook 'cperl-mode-hook (lambda () ;;(local-set-key "\C-cp" 'cperl-prove) ;;(local-set-key "\C-cdg" 'devel-graph) (local-set-key "\C-ctp" 'toggle-test-plan) ))) (defun toggle-test-plan () (interactive) (let ((new-pos)) (save-excursion (goto-char (point-min)) (cond ((re-search-forward "More[ \t]+tests[ \t]*=>[ \t]*" nil t) (replace-match "More 'no_plan'; # tests => " t t)) ((re-search-forward "More[ \t]+'no_plan';[ \t]*#[ \t]*" ni +l t) (replace-match "More " t t) (setq new-pos (point))))) (if new-pos (goto-char new-pos))))

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊