Emacs And Perl

This file is made of portions found from time to time in various sites. Please do not overwrite your configuration file called .emacs without making a backup.

As you know, the cperl-mode is not the standard perl mode, so there are two sections: the first to redefine the old and not well complete standard perl-mode, the second is to introduce and redefine keybindings of the outline-mode, wich is really necessary for editing big sources (it will compress all functions in only a line so the display will be very clean!).

After created/replaced this file in your home dir, when a perl file is loaded there are two new menus which can be used to work with perl!

As you can see at the end of the file there is a section which starts with “(custom-set-variables ;; custom-set-variables was added by Custom -- don't edit or cut/paste it!...” that you should no change or overwrite, so the right operation to modify your config file must be the insertion of my file from the start to the “custom set variables” point into the .emacs of your own.

;; Use cperl-mode instead of the default perl-mode
(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))



(add-hook 'cperl-mode-hook 'n-cperl-mode-hook t)
(defun n-cperl-mode-hook ()
(setq cperl-indent-level 4)
(setq cperl-continued-statement-offset 0)
(setq cperl-extra-newline-before-brace t)
(set-face-background 'cperl-array-face "wheat")
(set-face-background 'cperl-hash-face "wheat")
)


(defun my-folding-bind-keys ()
"My favourite folding keys"
(define-key folding-mode-map "\C-cf"
'folding-toggle-show-hide)
(define-key folding-mode-map "\C-co"
'folding-open-buffer)
(define-key folding-mode-map "\C-cc"
'folding-whole-buffer)
)

(setq folding-default-keys-function 'my-folding-bind-keys)

;;{{{ folding stuff

(load "folding" 'nomessage 'noerror)
(folding-mode-add-find-file-hook)

(folding-add-to-marks-list 'cperl-mode "#{{{ " "#}}}" nil)
(folding-add-to-marks-list 'Cperl-mode "#{{{ " "#}}}" nil)
(folding-add-to-marks-list 'CPerl-mode "#{{{ " "#}}}" nil)
;;}}}

(add-hook 'cperl-mode-hook 'outline-minor-mode)
(setq outline-minor-mode-prefix "\C-c")


; Outline-minor-mode key map
(define-prefix-command 'cm-map nil "Outline-")
; HIDE
(define-key cm-map "q" 'hide-sublevels) ; Hide everything but the top-level headings
(define-key cm-map "t" 'hide-body) ; Hide everything but headings (all body lines)
(define-key cm-map "o" 'hide-other) ; Hide other branches
(define-key cm-map "c" 'hide-entry) ; Hide this entry's body
(define-key cm-map "l" 'hide-leaves) ; Hide body lines in this entry and sub-entries
(define-key cm-map "d" 'hide-subtree) ; Hide everything in this entry and sub-entries
; SHOW
(define-key cm-map "a" 'show-all) ; Show (expand) everything
(define-key cm-map "e" 'show-entry) ; Show this heading's body
(define-key cm-map "i" 'show-children) ; Show this heading's immediate child sub-headings
(define-key cm-map "k" 'show-branches) ; Show all sub-headings under this heading
(define-key cm-map "s" 'show-subtree) ; Show (expand) everything in this heading & below
; MOVE
(define-key cm-map "u" 'outline-up-heading) ; Up
(define-key cm-map "n" 'outline-next-visible-heading) ; Next
(define-key cm-map "p" 'outline-previous-visible-heading) ; Previous
(define-key cm-map "f" 'outline-forward-same-level) ; Forward - same level
(define-key cm-map "b" 'outline-backward-same-level) ; Backward - same level
(global-set-key "\M-o" cm-map)

(defmacro join (join-char &rest others) `(mapconcat 'identity ',others ,join-char))

(setq my-cperl-outline-regexp
(concat
"^" ; Start of line
"[ \\t]*" ; Skip leading whitespace
"\\(" ; begin capture group \1
(join "\\|"
"=head[12]" ; POD header
"package" ; package
"=item" ; POD item
"sub" ; subroutine definition
)
"\\)" ; end capture group \1
"\\b" ; Word boundary
))




(setq cperl-mode-hook 'my-cperl-customizations)

(defun my-cperl-customizations ()
"cperl-mode customizations that must be done after cperl-mode loads"
(outline-minor-mode)
(abbrev-mode)

(defun cperl-outline-level ()
(looking-at outline-regexp)
(let ((match (match-string 1)))
(cond
((eq match "=head1" ) 1)
((eq match "package") 2)
((eq match "=head2" ) 3)
((eq match "=item" ) 4)
((eq match "sub" ) 5)
(t 7)
)))

(setq cperl-outline-regexp my-cperl-outline-regexp)
(setq outline-regexp cperl-outline-regexp)
(setq outline-level 'cperl-outline-level)
)



(custom-set-variables
;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
'(case-fold-search t)
'(current-language-environment "Latin-9")
'(default-input-method "latin-9-prefix")
'(global-font-lock-mode t nil (font-lock))
'(show-paren-mode t nil (paren))
'(transient-mark-mode t))
(custom-set-faces
;; custom-set-faces was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
)