Function: cperl-mode
cperl-mode is an autoloaded, interactive and byte-compiled function
defined in cperl-mode.el.gz.
Signature
(cperl-mode)
Documentation
Major mode for editing Perl code.
Expression and list commands understand all C brackets. Tab indents for Perl code. Paragraphs are separated by blank lines only. Delete converts tabs to spaces as it moves back.
Various characters in Perl almost always come in pairs: {}, (), [],
sometimes <>. When the user types the first, she gets the second as
well, with optional special formatting done on {}. (Disabled by
default.) You can always quote (with C-q (quoted-insert)) the left
"paren" to avoid the expansion. The processing of < is special,
since most the time you mean "less". CPerl mode tries to guess
whether you want to type pair <>, and inserts is if it
appropriate. You can set cperl-electric-parens-string to the string that
contains the parens from the above list you want to be electrical.
Electricity of parens is controlled by cperl-electric-parens.
You may also set cperl-electric-parens-mark to have electric parens
look for active mark and "embrace" a region if possible.'
CPerl mode provides expansion of the Perl control constructs:
if, else, elsif, unless, while, until, continue, do,
for, foreach, formy and foreachmy.
and POD directives (Disabled by default, see cperl-electric-keywords.)
The user types the keyword immediately followed by a space, which
causes the construct to be expanded, and the point is positioned where
she is most likely to want to be. E.g., when the user types a space
following "if" the following appears in the buffer: if () { or if ()
} { } and the cursor is between the parentheses. The user can then
type some boolean expression within the parens. Having done that,
typing M-x cperl-linefeed (cperl-linefeed) places you - appropriately indented - on a
new line between the braces (if you typed M-x cperl-linefeed (cperl-linefeed) in a POD
directive line, then appropriate number of new lines is inserted).
If CPerl decides that you want to insert "English" style construct like
bite if angry;
it will not do any expansion. See also help on variable
cperl-extra-newline-before-brace. (Note that one can switch the
help message on expansion by setting cperl-message-electric-keyword
to nil.)
M-x cperl-linefeed (cperl-linefeed) is a convenience replacement for typing carriage
return. It places you in the next line with proper indentation, or if
you type it inside the inline block of control construct, like
foreach (@lines) {print; print}
and you are on a boundary of a statement inside braces, it will
transform the construct into a multiline and will place you into an
appropriately indented blank line. If you need a usual
newline-and-indent behavior, it is on M-x newline-and-indent (newline-and-indent),
see documentation on cperl-electric-linefeed.
Use M-x cperl-invert-if-unless (cperl-invert-if-unless) to change a construction of the form
if (A) { B }
into
B if A;
cperl-indent-exp
cperl-indent-for-comment
( cperl-electric-paren
) cperl-electric-rparen
: cperl-electric-terminator
; cperl-electric-semi
< cperl-electric-paren
C-M-\ cperl-indent-region
C-M-q cperl-indent-exp
C-M-| cperl-lineup
C-c C-a cperl-toggle-auto-newline
C-c C-b cperl-find-bad-style
C-c C-d cperl-here-doc-spell
C-c C-e cperl-toggle-electric
C-c C-f auto-fill-mode
C-c C-h F cperl-info-on-command
C-c C-h P cperl-perldoc-at-point
C-c C-h a cperl-toggle-autohelp
C-c C-h f cperl-info-on-current-command
C-c C-h p cperl-perldoc
C-c C-h v cperl-get-help
C-c C-j cperl-linefeed
C-c C-k cperl-toggle-abbrev
C-c C-n cperl-narrow-to-here-doc
C-c C-p cperl-pod-spell
C-c C-t cperl-invert-if-unless
C-c C-v cperl-next-interpolated-REx
C-c C-w cperl-toggle-construct-fix
C-c C-x cperl-next-interpolated-REx-0
C-c C-y cperl-next-interpolated-REx-1
C-j newline-and-indent
DEL cperl-electric-backspace
TAB cperl-indent-command
[ cperl-electric-paren
] cperl-electric-rparen
{ cperl-electric-lbrace
} cperl-electric-brace
Setting the variable cperl-font-lock to t switches on font-lock-mode(var)/font-lock-mode(fun)
(even with older Emacsen), cperl-electric-lbrace-space to t switches
on electric space between $ and {, cperl-electric-parens-string is
the string that contains parentheses that should be electric in CPerl
(see also cperl-electric-parens-mark and cperl-electric-parens),
setting cperl-electric-keywords enables electric expansion of
control structures in CPerl. cperl-electric-linefeed governs which
one of two linefeed behavior is preferable. You can enable all these
options simultaneously (recommended mode of use) by setting
cperl-hairy to t. In this case you can switch separate options off
by setting them to null. Note that one may undo the extra
whitespace inserted by semis and braces in auto-newline-mode by
consequent M-x cperl-electric-backspace (cperl-electric-backspace).
If your site has perl5 documentation in info format, you can use commands
M-x cperl-info-on-current-command (cperl-info-on-current-command) and M-x cperl-info-on-command (cperl-info-on-command) to access it.
These keys run commands cperl-info-on-current-command and
cperl-info-on-command, which one is which is controlled by variable
cperl-info-on-command-no-prompt and cperl-clobber-lisp-bindings
(in turn affected by cperl-hairy).
Even if you have no info-format documentation, short one-liner-style
help is available on M-x cperl-get-help (cperl-get-help), and one can run perldoc or
man via menu.
It is possible to show this help automatically after some idle time.
This is regulated by variable cperl-lazy-help-time. Default with
cperl-hairy (if the value of cperl-lazy-help-time is nil) is 5
secs idle time . It is also possible to switch this on/off from the
menu, or via M-x cperl-toggle-autohelp (cperl-toggle-autohelp).
Use M-x cperl-lineup (cperl-lineup) to vertically lineup some construction - put the
beginning of the region at the start of construction, and make region
span the needed amount of lines.
Variables cperl-pod-here-scan, cperl-pod-here-fontify,
cperl-pod-face, cperl-pod-head-face control processing of POD and
here-docs sections. Results of scan are used for indentation too.
Variables controlling indentation style:
cperl-tab-always-indent
Non-nil means TAB in CPerl mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used.
cperl-indent-left-aligned-comments
Non-nil means that the comment starting in leftmost column should indent.
cperl-auto-newline
Non-nil means automatically newline before and after braces,
and after colons and semicolons, inserted in Perl code. The following
M-x cperl-electric-backspace (cperl-electric-backspace) will remove the inserted whitespace.
Insertion after colons requires both this variable and
cperl-auto-newline-after-colon set.
cperl-auto-newline-after-colon
Non-nil means automatically newline even after colons.
Subject to cperl-auto-newline setting.
cperl-indent-level
Indentation of Perl statements within surrounding block.
The surrounding block's indentation is the indentation
of the line on which the open-brace appears.
cperl-continued-statement-offset
Extra indentation given to a substatement, such as the
then-clause of an if, or body of a while, or just a statement continuation.
cperl-continued-brace-offset
Extra indentation given to a brace that starts a substatement.
This is in addition to cperl-continued-statement-offset.
cperl-brace-offset
Extra indentation for line if it starts with an open brace.
cperl-brace-imaginary-offset
An open brace following other text is treated as if it the line started
this far to the right of the actual line indentation.
cperl-label-offset
Extra indentation for line that is a label.
cperl-min-label-indent
Minimal indentation for line that is a label.
Settings for classic indent-styles: K&R BSD=C++ GNU PBP PerlStyle=Whitesmith
cperl-indent-level 5 4 2 4 4
cperl-brace-offset 0 0 0 0 0
cperl-continued-brace-offset -5 -4 0 0 0
cperl-label-offset -5 -4 -2 -2 -4
cperl-continued-statement-offset 5 4 2 4 4
CPerl knows several indentation styles, and may bulk set the
corresponding variables. Use M-x cperl-set-style (cperl-set-style) to do this or
set the cperl-file-style user option. Use
M-x cperl-set-style-back (cperl-set-style-back) to restore the memorized preexisting
values (both available from menu). See examples in
cperl-style-examples.
Part of the indentation style is how different parts of if/elsif/else
statements are broken into lines; in CPerl, this is reflected on how
templates for these constructs are created (controlled by
cperl-extra-newline-before-brace), and how reflow-logic should treat
"continuation" blocks of else/elsif/continue, controlled by the same
variable, and by cperl-extra-newline-before-brace-multiline,
cperl-merge-trailing-else, cperl-indent-region-fix-constructs.
If cperl-indent-level is 0, the statement after opening brace in
column 0 is indented on
cperl-brace-offset+cperl-continued-statement-offset.
Turning on CPerl mode calls the hooks in the variable cperl-mode-hook
with no args.
DO NOT FORGET to read micro-docs (available from Perl menu)
or as help on variables cperl-tips, cperl-problems,
cperl-praise, cperl-speed.
Probably introduced at or before Emacs version 22.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/cperl-mode.el.gz
;;;###autoload
(define-derived-mode cperl-mode prog-mode "CPerl"
"Major mode for editing Perl code.
Expression and list commands understand all C brackets.
Tab indents for Perl code.
Paragraphs are separated by blank lines only.
Delete converts tabs to spaces as it moves back.
Various characters in Perl almost always come in pairs: {}, (), [],
sometimes <>. When the user types the first, she gets the second as
well, with optional special formatting done on {}. (Disabled by
default.) You can always quote (with \\[quoted-insert]) the left
\"paren\" to avoid the expansion. The processing of < is special,
since most the time you mean \"less\". CPerl mode tries to guess
whether you want to type pair <>, and inserts is if it
appropriate. You can set `cperl-electric-parens-string' to the string that
contains the parens from the above list you want to be electrical.
Electricity of parens is controlled by `cperl-electric-parens'.
You may also set `cperl-electric-parens-mark' to have electric parens
look for active mark and \"embrace\" a region if possible.'
CPerl mode provides expansion of the Perl control constructs:
if, else, elsif, unless, while, until, continue, do,
for, foreach, formy and foreachmy.
and POD directives (Disabled by default, see `cperl-electric-keywords'.)
The user types the keyword immediately followed by a space, which
causes the construct to be expanded, and the point is positioned where
she is most likely to want to be. E.g., when the user types a space
following \"if\" the following appears in the buffer: if () { or if ()
} { } and the cursor is between the parentheses. The user can then
type some boolean expression within the parens. Having done that,
typing \\[cperl-linefeed] places you - appropriately indented - on a
new line between the braces (if you typed \\[cperl-linefeed] in a POD
directive line, then appropriate number of new lines is inserted).
If CPerl decides that you want to insert \"English\" style construct like
bite if angry;
it will not do any expansion. See also help on variable
`cperl-extra-newline-before-brace'. (Note that one can switch the
help message on expansion by setting `cperl-message-electric-keyword'
to nil.)
\\[cperl-linefeed] is a convenience replacement for typing carriage
return. It places you in the next line with proper indentation, or if
you type it inside the inline block of control construct, like
foreach (@lines) {print; print}
and you are on a boundary of a statement inside braces, it will
transform the construct into a multiline and will place you into an
appropriately indented blank line. If you need a usual
`newline-and-indent' behavior, it is on \\[newline-and-indent],
see documentation on `cperl-electric-linefeed'.
Use \\[cperl-invert-if-unless] to change a construction of the form
if (A) { B }
into
B if A;
\\{cperl-mode-map}
Setting the variable `cperl-font-lock' to t switches on `font-lock-mode'
\(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
on electric space between $ and {, `cperl-electric-parens-string' is
the string that contains parentheses that should be electric in CPerl
\(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
setting `cperl-electric-keywords' enables electric expansion of
control structures in CPerl. `cperl-electric-linefeed' governs which
one of two linefeed behavior is preferable. You can enable all these
options simultaneously (recommended mode of use) by setting
`cperl-hairy' to t. In this case you can switch separate options off
by setting them to `null'. Note that one may undo the extra
whitespace inserted by semis and braces in `auto-newline'-mode by
consequent \\[cperl-electric-backspace].
If your site has perl5 documentation in info format, you can use commands
\\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
These keys run commands `cperl-info-on-current-command' and
`cperl-info-on-command', which one is which is controlled by variable
`cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
\(in turn affected by `cperl-hairy').
Even if you have no info-format documentation, short one-liner-style
help is available on \\[cperl-get-help], and one can run perldoc or
man via menu.
It is possible to show this help automatically after some idle time.
This is regulated by variable `cperl-lazy-help-time'. Default with
`cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
secs idle time . It is also possible to switch this on/off from the
menu, or via \\[cperl-toggle-autohelp].
Use \\[cperl-lineup] to vertically lineup some construction - put the
beginning of the region at the start of construction, and make region
span the needed amount of lines.
Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
`cperl-pod-face', `cperl-pod-head-face' control processing of POD and
here-docs sections. Results of scan are used for indentation too.
Variables controlling indentation style:
`cperl-tab-always-indent'
Non-nil means TAB in CPerl mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used.
`cperl-indent-left-aligned-comments'
Non-nil means that the comment starting in leftmost column should indent.
`cperl-auto-newline'
Non-nil means automatically newline before and after braces,
and after colons and semicolons, inserted in Perl code. The following
\\[cperl-electric-backspace] will remove the inserted whitespace.
Insertion after colons requires both this variable and
`cperl-auto-newline-after-colon' set.
`cperl-auto-newline-after-colon'
Non-nil means automatically newline even after colons.
Subject to `cperl-auto-newline' setting.
`cperl-indent-level'
Indentation of Perl statements within surrounding block.
The surrounding block's indentation is the indentation
of the line on which the open-brace appears.
`cperl-continued-statement-offset'
Extra indentation given to a substatement, such as the
then-clause of an if, or body of a while, or just a statement continuation.
`cperl-continued-brace-offset'
Extra indentation given to a brace that starts a substatement.
This is in addition to `cperl-continued-statement-offset'.
`cperl-brace-offset'
Extra indentation for line if it starts with an open brace.
`cperl-brace-imaginary-offset'
An open brace following other text is treated as if it the line started
this far to the right of the actual line indentation.
`cperl-label-offset'
Extra indentation for line that is a label.
`cperl-min-label-indent'
Minimal indentation for line that is a label.
Settings for classic indent-styles: K&R BSD=C++ GNU PBP PerlStyle=Whitesmith
`cperl-indent-level' 5 4 2 4 4
`cperl-brace-offset' 0 0 0 0 0
`cperl-continued-brace-offset' -5 -4 0 0 0
`cperl-label-offset' -5 -4 -2 -2 -4
`cperl-continued-statement-offset' 5 4 2 4 4
CPerl knows several indentation styles, and may bulk set the
corresponding variables. Use \\[cperl-set-style] to do this or
set the `cperl-file-style' user option. Use
\\[cperl-set-style-back] to restore the memorized preexisting
values \(both available from menu). See examples in
`cperl-style-examples'.
Part of the indentation style is how different parts of if/elsif/else
statements are broken into lines; in CPerl, this is reflected on how
templates for these constructs are created (controlled by
`cperl-extra-newline-before-brace'), and how reflow-logic should treat
\"continuation\" blocks of else/elsif/continue, controlled by the same
variable, and by `cperl-extra-newline-before-brace-multiline',
`cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
If `cperl-indent-level' is 0, the statement after opening brace in
column 0 is indented on
`cperl-brace-offset'+`cperl-continued-statement-offset'.
Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
with no args.
DO NOT FORGET to read micro-docs (available from `Perl' menu)
or as help on variables `cperl-tips', `cperl-problems',
`cperl-praise', `cperl-speed'."
(if (cperl-val 'cperl-electric-linefeed)
(progn
(local-set-key "\C-J" 'cperl-linefeed)
(local-set-key "\C-C\C-J" 'newline-and-indent)))
(if (and
(cperl-val 'cperl-clobber-lisp-bindings)
(cperl-val 'cperl-info-on-command-no-prompt))
(progn
;; don't clobber the backspace binding:
(define-key cperl-mode-map "\C-hf" 'cperl-info-on-current-command)
(define-key cperl-mode-map "\C-c\C-hf" 'cperl-info-on-command)))
(setq local-abbrev-table cperl-mode-abbrev-table)
(if (cperl-val 'cperl-electric-keywords)
(abbrev-mode 1))
(set-syntax-table cperl-mode-syntax-table)
;; Workaround for Bug#30393, needed for Emacs 26.
(when (< emacs-major-version 27)
(setq-local open-paren-in-column-0-is-defun-start nil))
;; Until Emacs is multi-threaded, we do not actually need it local:
(make-local-variable 'cperl-font-lock-multiline-start)
(make-local-variable 'cperl-font-locking)
(setq-local outline-regexp cperl-outline-regexp)
(setq-local outline-level 'cperl-outline-level)
(setq-local add-log-current-defun-function
(lambda ()
(save-excursion
(if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
(match-string-no-properties 1)))))
(setq-local paragraph-start (concat "^$\\|" page-delimiter))
(setq-local paragraph-separate paragraph-start)
(setq-local paragraph-ignore-fill-prefix t)
(setq-local indent-line-function #'cperl-indent-line)
(setq-local require-final-newline mode-require-final-newline)
(setq-local comment-start "# ")
(setq-local comment-end "")
(setq-local comment-column cperl-comment-column)
(setq-local comment-start-skip "#+ *")
;; "[ \t]*sub"
;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
;; cperl-maybe-white-and-comment-rex ; 15=pre-block
(setq-local defun-prompt-regexp
(concat "^[ \t]*\\("
cperl-sub-regexp
(cperl-after-sub-regexp 'named 'attr-groups)
"\\|" ; per toke.c
"\\(BEGIN\\|UNITCHECK\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
"\\)"
cperl-maybe-white-and-comment-rex))
(setq-local comment-indent-function #'cperl-comment-indent)
(setq-local fill-paragraph-function #'cperl-fill-paragraph)
(setq-local parse-sexp-ignore-comments t)
(setq-local indent-region-function #'cperl-indent-region)
;;(setq auto-fill-function #'cperl-do-auto-fill) ; Need to switch on and off!
(setq-local imenu-create-index-function #'cperl-imenu--create-perl-index)
(setq-local imenu-sort-function nil)
(setq-local vc-rcs-header cperl-vc-rcs-header)
(setq-local vc-sccs-header cperl-vc-sccs-header)
(when (boundp 'compilation-error-regexp-alist-alist)
;; The let here is just a compatibility kludge for the obsolete
;; variable `cperl-compilation-error-regexp-alist'. It can be removed
;; when that variable is removed.
(let ((regexp (if (boundp 'cperl-compilation-error-regexp-alist)
(car cperl-compilation-error-regexp-alist)
cperl-compilation-error-regexp-list)))
(setq-local compilation-error-regexp-alist-alist
(cons (cons 'cperl regexp)
compilation-error-regexp-alist-alist)))
(make-local-variable 'compilation-error-regexp-alist)
(push 'cperl compilation-error-regexp-alist))
(setq-local font-lock-defaults
'((cperl-load-font-lock-keywords
cperl-load-font-lock-keywords-1
cperl-load-font-lock-keywords-2)
nil nil ((?_ . "w")) nil
(font-lock-syntactic-face-function
. cperl-font-lock-syntactic-face-function)))
;; Reset syntaxification cache.
(setq-local cperl-syntax-state nil)
(when cperl-use-syntax-table-text-property
;; Reset syntaxification cache.
(setq-local cperl-syntax-done-to nil)
(setq-local syntax-propertize-function
(lambda (start end)
(goto-char start)
;; Even if cperl-fontify-syntactically has already gone
;; beyond `start', syntax-propertize has just removed
;; syntax-table properties between start and end, so we have
;; to re-apply them.
(setq cperl-syntax-done-to start)
(cperl-fontify-syntactically end))))
(setq cperl-font-lock-multiline t) ; Not localized...
(setq-local font-lock-multiline t)
(setq-local font-lock-fontify-region-function
#'cperl-font-lock-fontify-region-function)
(make-local-variable 'cperl-old-style)
(setq-local normal-auto-fill-function
#'cperl-do-auto-fill)
(if (cperl-val 'cperl-font-lock)
(progn (or cperl-faces-init (cperl-init-faces))
(font-lock-mode 1)))
(setq-local facemenu-add-face-function
#'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
(and (boundp 'msb-menu-cond)
(not cperl-msb-fixed)
(cperl-msb-fix))
(if cperl-hook-after-change
(add-hook 'after-change-functions #'cperl-after-change-function nil t))
;; After hooks since fontification will break this
(when (and cperl-pod-here-scan
(not cperl-syntaxify-by-font-lock))
(cperl-find-pods-heres))
(when cperl-file-style
(cperl-set-style cperl-file-style))
(add-hook 'hack-local-variables-hook #'cperl--set-file-style nil t)
;; Setup Flymake
(add-hook 'flymake-diagnostic-functions #'perl-flymake nil t))