* tramp-tests.el (tramp-test29-vc-registered):
[emacs.git] / lisp / progmodes / perl-mode.el
blobef372a34fdb11a6dd153b71e0f2664d3a778c820
1 ;;; perl-mode.el --- Perl code editing commands for GNU Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1990, 1994, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: William F. Mann
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Adapted-By: ESR
8 ;; Keywords: languages
10 ;; Adapted from C code editing commands 'c-mode.el', Copyright 1987 by the
11 ;; Free Software Foundation, under terms of its General Public License.
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;; To enter perl-mode automatically, add (autoload 'perl-mode "perl-mode")
31 ;; to your init file and change the first line of your perl script to:
32 ;; #!/usr/bin/perl -- # -*-Perl-*-
33 ;; With arguments to perl:
34 ;; #!/usr/bin/perl -P- # -*-Perl-*-
35 ;; To handle files included with do 'filename.pl';, add something like
36 ;; (setq auto-mode-alist (append (list (cons "\\.pl\\'" 'perl-mode))
37 ;; auto-mode-alist))
38 ;; to your init file; otherwise the .pl suffix defaults to prolog-mode.
40 ;; This code is based on the 18.53 version c-mode.el, with extensive
41 ;; rewriting. Most of the features of c-mode survived intact.
43 ;; I added a new feature which adds functionality to TAB; it is controlled
44 ;; by the variable perl-tab-to-comment. With it enabled, TAB does the
45 ;; first thing it can from the following list: change the indentation;
46 ;; move past leading white space; delete an empty comment; reindent a
47 ;; comment; move to end of line; create an empty comment; tell you that
48 ;; the line ends in a quoted string, or has a # which should be a \#.
50 ;; If your machine is slow, you may want to remove some of the bindings
51 ;; to perl-electric-terminator. I changed the indenting defaults to be
52 ;; what Larry Wall uses in perl/lib, but left in all the options.
54 ;; I also tuned a few things: comments and labels starting in column
55 ;; zero are left there by perl-indent-exp; perl-beginning-of-function
56 ;; goes back to the first open brace/paren in column zero, the open brace
57 ;; in 'sub ... {', or the equal sign in 'format ... ='; perl-indent-exp
58 ;; (meta-^q) indents from the current line through the close of the next
59 ;; brace/paren, so you don't need to start exactly at a brace or paren.
61 ;; It may be good style to put a set of redundant braces around your
62 ;; main program. This will let you reindent it with meta-^q.
64 ;; Known problems (these are all caused by limitations in the Emacs Lisp
65 ;; parsing routine (parse-partial-sexp), which was not designed for such
66 ;; a rich language; writing a more suitable parser would be a big job):
67 ;; 2) The globbing syntax <pattern> is not recognized, so special
68 ;; characters in the pattern string must be backslashed.
69 ;; 3) The << quoting operators are not recognized; see below.
70 ;; 5) To make '$' work correctly, $' is not recognized as a variable.
71 ;; Use "$'" or $POSTMATCH instead.
73 ;; If you don't use font-lock, additional problems will appear:
74 ;; 1) Regular expression delimiters do not act as quotes, so special
75 ;; characters such as `'"#:;[](){} may need to be backslashed
76 ;; in regular expressions and in both parts of s/// and tr///.
77 ;; 4) The q and qq quoting operators are not recognized; see below.
78 ;; 5) To make variables such a $' and $#array work, perl-mode treats
79 ;; $ just like backslash, so '$' is not treated correctly.
80 ;; 6) Unfortunately, treating $ like \ makes ${var} be treated as an
81 ;; unmatched }. See below.
82 ;; 7) When ' (quote) is used as a package name separator, perl-mode
83 ;; doesn't understand, and thinks it is seeing a quoted string.
85 ;; Here are some ugly tricks to bypass some of these problems: the perl
86 ;; expression /`/ (that's a back-tick) usually evaluates harmlessly,
87 ;; but will trick perl-mode into starting a quoted string, which
88 ;; can be ended with another /`/. Assuming you have no embedded
89 ;; back-ticks, this can used to help solve problem 3:
91 ;; /`/; $ugly = q?"'$?; /`/;
93 ;; The same trick can be used for problem 6 as in:
94 ;; /{/; while (<${glob_me}>)
95 ;; but a simpler solution is to add a space between the $ and the {:
96 ;; while (<$ {glob_me}>)
98 ;; Problem 7 is even worse, but this 'fix' does work :-(
99 ;; $DB'stop#'
100 ;; [$DB'line#'
101 ;; ] =~ s/;9$//;
103 ;;; Code:
105 (defgroup perl nil
106 "Major mode for editing Perl code."
107 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
108 :prefix "perl-"
109 :group 'languages)
111 (defvar perl-mode-abbrev-table nil
112 "Abbrev table in use in perl-mode buffers.")
113 (define-abbrev-table 'perl-mode-abbrev-table ())
115 (defvar perl-mode-map
116 (let ((map (make-sparse-keymap)))
117 (define-key map "\e\C-a" 'perl-beginning-of-function)
118 (define-key map "\e\C-e" 'perl-end-of-function)
119 (define-key map "\e\C-h" 'perl-mark-function)
120 (define-key map "\e\C-q" 'perl-indent-exp)
121 (define-key map "\177" 'backward-delete-char-untabify)
122 map)
123 "Keymap used in Perl mode.")
125 (defvar perl-mode-syntax-table
126 (let ((st (make-syntax-table (standard-syntax-table))))
127 (modify-syntax-entry ?\n ">" st)
128 (modify-syntax-entry ?# "<" st)
129 ;; `$' is also a prefix char so I was tempted to say "/ p",
130 ;; but the `p' thingy basically overrides the `/' :-( -- Stef
131 (modify-syntax-entry ?$ "/" st)
132 (modify-syntax-entry ?% ". p" st)
133 (modify-syntax-entry ?@ ". p" st)
134 (modify-syntax-entry ?& "." st)
135 (modify-syntax-entry ?\' "\"" st)
136 (modify-syntax-entry ?* "." st)
137 (modify-syntax-entry ?+ "." st)
138 (modify-syntax-entry ?- "." st)
139 (modify-syntax-entry ?/ "." st)
140 (modify-syntax-entry ?< "." st)
141 (modify-syntax-entry ?= "." st)
142 (modify-syntax-entry ?> "." st)
143 (modify-syntax-entry ?\\ "\\" st)
144 (modify-syntax-entry ?` "\"" st)
145 (modify-syntax-entry ?| "." st)
147 "Syntax table in use in `perl-mode' buffers.")
149 (defvar perl-imenu-generic-expression
150 '(;; Functions
151 (nil "^[ \t]*sub\\s-+\\([-[:alnum:]+_:]+\\)" 1)
152 ;;Variables
153 ("Variables" "^\\(?:my\\|our\\)\\s-+\\([$@%][-[:alnum:]+_:]+\\)\\s-*=" 1)
154 ("Packages" "^[ \t]*package\\s-+\\([-[:alnum:]+_:]+\\);" 1)
155 ("Doc sections" "^=head[0-9][ \t]+\\(.*\\)" 1))
156 "Imenu generic expression for Perl mode. See `imenu-generic-expression'.")
158 ;; Regexps updated with help from Tom Tromey <tromey@cambric.colorado.edu> and
159 ;; Jim Campbell <jec@murzim.ca.boeing.com>.
161 (defconst perl--prettify-symbols-alist
162 '(("->" . ?→)
163 ("=>" . ?⇒)
164 ("::" . ?∷)))
166 (defconst perl-font-lock-keywords-1
167 '(;; What is this for?
168 ;;("\\(--- .* ---\\|=== .* ===\\)" . font-lock-string-face)
170 ;; Fontify preprocessor statements as we do in `c-font-lock-keywords'.
171 ;; Ilya Zakharevich <ilya@math.ohio-state.edu> thinks this is a bad idea.
172 ;; ("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
173 ;; ("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
174 ;; ("^#[ \t]*if\\>"
175 ;; ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
176 ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t)))
177 ;; ("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
178 ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t))
180 ;; Fontify function and package names in declarations.
181 ("\\<\\(package\\|sub\\)\\>[ \t]*\\(\\sw+\\)?"
182 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
183 ("\\<\\(import\\|no\\|require\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
184 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t)))
185 "Subdued level highlighting for Perl mode.")
187 (defconst perl-font-lock-keywords-2
188 (append
189 perl-font-lock-keywords-1
190 `( ;; Fontify keywords, except those fontified otherwise.
191 ,(concat "\\<"
192 (regexp-opt '("if" "until" "while" "elsif" "else" "unless"
193 "do" "dump" "for" "foreach" "exit" "die"
194 "BEGIN" "END" "return" "exec" "eval") t)
195 "\\>")
197 ;; Fontify local and my keywords as types.
198 ("\\<\\(local\\|my\\)\\>" . font-lock-type-face)
200 ;; Fontify function, variable and file name references.
201 ("&\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-function-name-face)
202 ;; Additionally underline non-scalar variables. Maybe this is a bad idea.
203 ;;'("[$@%*][#{]?\\(\\sw+\\)" 1 font-lock-variable-name-face)
204 ("[$*]{?\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-variable-name-face)
205 ("\\([@%]\\|\\$#\\)\\(\\sw+\\(::\\sw+\\)*\\)"
206 (2 (cons font-lock-variable-name-face '(underline))))
207 ("<\\(\\sw+\\)>" 1 font-lock-constant-face)
209 ;; Fontify keywords with/and labels as we do in `c++-font-lock-keywords'.
210 ("\\<\\(continue\\|goto\\|last\\|next\\|redo\\)\\>[ \t]*\\(\\sw+\\)?"
211 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
212 ("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-constant-face)))
213 "Gaudy level highlighting for Perl mode.")
215 (defvar perl-font-lock-keywords perl-font-lock-keywords-1
216 "Default expressions to highlight in Perl mode.")
218 (defvar perl-quote-like-pairs
219 '((?\( . ?\)) (?\[ . ?\]) (?\{ . ?\}) (?\< . ?\>)))
221 ;; FIXME: handle here-docs and regexps.
222 ;; <<EOF <<"EOF" <<'EOF' (no space)
223 ;; see `man perlop'
224 ;; ?...?
225 ;; /.../
226 ;; m [...]
227 ;; m /.../
228 ;; q /.../ = '...'
229 ;; qq /.../ = "..."
230 ;; qx /.../ = `...`
231 ;; qr /.../ = precompiled regexp =~=~ m/.../
232 ;; qw /.../
233 ;; s /.../.../
234 ;; s <...> /.../
235 ;; s '...'...'
236 ;; tr /.../.../
237 ;; y /.../.../
239 ;; <file*glob>
240 (defun perl-syntax-propertize-function (start end)
241 (let ((case-fold-search nil))
242 (goto-char start)
243 (perl-syntax-propertize-special-constructs end)
244 (funcall
245 (syntax-propertize-rules
246 ;; Turn POD into b-style comments. Place the cut rule first since it's
247 ;; more specific.
248 ("^=cut\\>.*\\(\n\\)" (1 "> b"))
249 ("^\\(=\\)\\sw" (1 "< b"))
250 ;; Catch ${ so that ${var} doesn't screw up indentation.
251 ;; This also catches $' to handle 'foo$', although it should really
252 ;; check that it occurs inside a '..' string.
253 ("\\(\\$\\)[{']" (1 (unless (and (eq ?\' (char-after (match-end 1)))
254 (save-excursion
255 (not (nth 3 (syntax-ppss
256 (match-beginning 0))))))
257 (string-to-syntax ". p"))))
258 ;; Handle funny names like $DB'stop.
259 ("\\$ ?{?^?[_[:alpha:]][_[:alnum:]]*\\('\\)[_[:alpha:]]" (1 "_"))
260 ;; format statements
261 ("^[ \t]*format.*=[ \t]*\\(\n\\)"
262 (1 (prog1 "\"" (perl-syntax-propertize-special-constructs end))))
263 ;; Funny things in `sub' arg-specs like `sub myfun ($)' or `sub ($)'.
264 ;; Be careful not to match "sub { (...) ... }".
265 ("\\<sub\\(?:[[:space:]]+[^{}[:punct:][:space:]]+\\)?[[:space:]]*(\\([^)]+\\))"
266 (1 "."))
267 ;; Turn __DATA__ trailer into a comment.
268 ("^\\(_\\)_\\(?:DATA\\|END\\)__[ \t]*\\(?:\\(\n\\)#.-\\*-.*perl.*-\\*-\\|\n.*\\)"
269 (1 "< c") (2 "> c")
270 (0 (ignore (put-text-property (match-beginning 0) (match-end 0)
271 'syntax-multiline t))))
272 ;; Regexp and funny quotes. Distinguishing a / that starts a regexp
273 ;; match from the division operator is ...interesting.
274 ;; Basically, / is a regexp match if it's preceded by an infix operator
275 ;; (or some similar separator), or by one of the special keywords
276 ;; corresponding to builtin functions that can take their first arg
277 ;; without parentheses. Of course, that presume we're looking at the
278 ;; *opening* slash. We can afford to mis-match the closing ones
279 ;; here, because they will be re-treated separately later in
280 ;; perl-font-lock-special-syntactic-constructs.
281 ((concat "\\(?:\\(?:^\\|[^$@&%[:word:]]\\)"
282 (regexp-opt '("split" "if" "unless" "until" "while" "split"
283 "grep" "map" "not" "or" "and" "for" "foreach"))
284 "\\|[-?:.,;|&+*=!~({[]\\|\\(^\\)\\)[ \t\n]*\\(/\\)")
285 (2 (ignore
286 (if (and (match-end 1) ; / at BOL.
287 (save-excursion
288 (goto-char (match-end 1))
289 (forward-comment (- (point-max)))
290 (put-text-property (point) (match-end 2)
291 'syntax-multiline t)
292 (not (memq (char-before)
293 '(?? ?: ?. ?, ?\; ?= ?! ?~ ?\( ?\[)))))
294 nil ;; A division sign instead of a regexp-match.
295 (put-text-property (match-beginning 2) (match-end 2)
296 'syntax-table (string-to-syntax "\""))
297 (perl-syntax-propertize-special-constructs end)))))
298 ("\\(^\\|[?:.,;=!~({[ \t]\\)\\([msy]\\|q[qxrw]?\\|tr\\)\\>\\s-*\\(?:\\([^])}>= \n\t]\\)\\|\\(?3:=\\)[^>]\\)"
299 ;; Nasty cases:
300 ;; /foo/m $a->m $#m $m @m %m
301 ;; \s (appears often in regexps).
302 ;; -s file
303 ;; y => 3
304 ;; sub tr {...}
305 (3 (ignore
306 (if (save-excursion (goto-char (match-beginning 0))
307 (forward-word -1)
308 (looking-at-p "sub[ \t\n]"))
309 ;; This is defining a function.
311 (put-text-property (match-beginning 3) (match-end 3)
312 'syntax-table
313 (if (assoc (char-after (match-beginning 3))
314 perl-quote-like-pairs)
315 (string-to-syntax "|")
316 (string-to-syntax "\"")))
317 (perl-syntax-propertize-special-constructs end)))))
318 ;; Here documents.
319 ;; TODO: Handle <<WORD. These are trickier because you need to
320 ;; disambiguate with the shift operator.
321 ("<<[ \t]*\\('[^'\n]*'\\|\"[^\"\n]*\"\\|\\\\[[:alpha:]][[:alnum:]]*\\).*\\(\n\\)"
322 (2 (let* ((st (get-text-property (match-beginning 2) 'syntax-table))
323 (name (match-string 1)))
324 (goto-char (match-end 1))
325 (if (save-excursion (nth 8 (syntax-ppss (match-beginning 0))))
326 ;; Leave the property of the newline unchanged.
328 (cons (car (string-to-syntax "< c"))
329 ;; Remember the names of heredocs found on this line.
330 (cons (pcase (aref name 0)
331 (`?\\ (substring name 1))
332 (_ (substring name 1 -1)))
333 (cdr st)))))))
334 ;; We don't call perl-syntax-propertize-special-constructs directly
335 ;; from the << rule, because there might be other elements (between
336 ;; the << and the \n) that need to be propertized.
337 ("\\(?:$\\)\\s<"
338 (0 (ignore (perl-syntax-propertize-special-constructs end))))
340 (point) end)))
342 (defvar perl-empty-syntax-table
343 (let ((st (copy-syntax-table)))
344 ;; Make all chars be of punctuation syntax.
345 (dotimes (i 256) (aset st i '(1)))
346 (modify-syntax-entry ?\\ "\\" st)
348 "Syntax table used internally for processing quote-like operators.")
350 (defun perl-quote-syntax-table (char)
351 (let ((close (cdr (assq char perl-quote-like-pairs)))
352 (st (copy-syntax-table perl-empty-syntax-table)))
353 (if (not close)
354 (modify-syntax-entry char "\"" st)
355 (modify-syntax-entry char "(" st)
356 (modify-syntax-entry close ")" st))
357 st))
359 (defun perl-syntax-propertize-special-constructs (limit)
360 "Propertize special constructs like regexps and formats."
361 (let ((state (syntax-ppss))
362 char)
363 (cond
364 ((eq 2 (nth 7 state))
365 ;; A Here document.
366 (let ((names (cdr (get-text-property (nth 8 state) 'syntax-table))))
367 (when (cdr names)
368 (setq names (reverse names))
369 ;; Multiple heredocs on a single line, we have to search from the
370 ;; beginning, since we don't know which names might be
371 ;; before point.
372 (goto-char (nth 8 state)))
373 (while (and names
374 (re-search-forward
375 (concat "^" (regexp-quote (pop names)) "\n")
376 limit 'move))
377 (unless names
378 (put-text-property (1- (point)) (point) 'syntax-table
379 (string-to-syntax "> c"))))))
380 ((or (null (setq char (nth 3 state)))
381 (and (characterp char) (eq (char-syntax (nth 3 state)) ?\")))
382 ;; Normal text, or comment, or docstring, or normal string.
383 nil)
384 ((eq (nth 3 state) ?\n)
385 ;; A `format' command.
386 (when (re-search-forward "^\\s *\\.\\s *\n" limit 'move)
387 (put-text-property (1- (point)) (point)
388 'syntax-table (string-to-syntax "\""))))
390 ;; This is regexp like quote thingy.
391 (setq char (char-after (nth 8 state)))
392 (let ((startpos (point))
393 (twoargs (save-excursion
394 (goto-char (nth 8 state))
395 (skip-syntax-backward " ")
396 (skip-syntax-backward "w")
397 (member (buffer-substring
398 (point) (progn (forward-word 1) (point)))
399 '("tr" "s" "y"))))
400 (close (cdr (assq char perl-quote-like-pairs)))
401 (st (perl-quote-syntax-table char)))
402 (when (with-syntax-table st
403 (if close
404 ;; For paired delimiters, Perl allows nesting them, but
405 ;; since we treat them as strings, Emacs does not count
406 ;; those delimiters in `state', so we don't know how deep
407 ;; we are: we have to go back to the beginning of this
408 ;; "string" and count from there.
409 (condition-case nil
410 (progn
411 ;; Start after the first char since it doesn't have
412 ;; paren-syntax (an alternative would be to let-bind
413 ;; parse-sexp-lookup-properties).
414 (goto-char (1+ (nth 8 state)))
415 (up-list 1)
417 ;; In case of error, make sure we don't move backward.
418 (scan-error (goto-char startpos) nil))
419 (not (or (nth 8 (parse-partial-sexp
420 ;; Since we don't know if point is within
421 ;; the first or the scond arg, we have to
422 ;; start from the beginning.
423 (if twoargs (1+ (nth 8 state)) (point))
424 limit nil nil state 'syntax-table))
425 ;; If we have a self-paired opener and a twoargs
426 ;; command, the form is s/../../ so we have to skip
427 ;; a second time.
428 ;; In the case of s{...}{...}, we only handle the
429 ;; first part here and the next below.
430 (when (and twoargs (not close))
431 (nth 8 (parse-partial-sexp
432 (point) limit
433 nil nil state 'syntax-table)))))))
434 ;; Point is now right after the arg(s).
435 (when (eq (char-before (1- (point))) ?$)
436 (put-text-property (- (point) 2) (1- (point))
437 'syntax-table '(1)))
438 (put-text-property (1- (point)) (point)
439 'syntax-table
440 (if close
441 (string-to-syntax "|")
442 (string-to-syntax "\"")))
443 ;; If we have two args with a non-self-paired starter (e.g.
444 ;; s{...}{...}) we're right after the first arg, so we still have to
445 ;; handle the second part.
446 (when (and twoargs close)
447 ;; Skip whitespace and make sure that font-lock will
448 ;; refontify the second part in the proper context.
449 (put-text-property
450 (point) (progn (forward-comment (point-max)) (point))
451 'syntax-multiline t)
453 (when (< (point) limit)
454 (put-text-property (point) (1+ (point))
455 'syntax-table
456 (if (assoc (char-after)
457 perl-quote-like-pairs)
458 ;; Put an `e' in the cdr to mark this
459 ;; char as "second arg starter".
460 (string-to-syntax "|e")
461 (string-to-syntax "\"e")))
462 (forward-char 1)
463 ;; Re-use perl-syntax-propertize-special-constructs to handle the
464 ;; second part (the first delimiter of second part can't be
465 ;; preceded by "s" or "tr" or "y", so it will not be considered
466 ;; as twoarg).
467 (perl-syntax-propertize-special-constructs limit)))))))))
469 (defun perl-font-lock-syntactic-face-function (state)
470 (cond
471 ((and (nth 3 state)
472 (eq ?e (cdr-safe (get-text-property (nth 8 state) 'syntax-table)))
473 ;; This is a second-arg of s{..}{...} form; let's check if this second
474 ;; arg is executable code rather than a string. For that, we need to
475 ;; look for an "e" after this second arg, so we have to hunt for the
476 ;; end of the arg. Depending on whether the whole arg has already
477 ;; been syntax-propertized or not, the end-char will have different
478 ;; syntaxes, so let's ignore syntax-properties temporarily so we can
479 ;; pretend it has not been syntax-propertized yet.
480 (let* ((parse-sexp-lookup-properties nil)
481 (char (char-after (nth 8 state)))
482 (paired (assq char perl-quote-like-pairs)))
483 (with-syntax-table (perl-quote-syntax-table char)
484 (save-excursion
485 (if (not paired)
486 (parse-partial-sexp (point) (point-max)
487 nil nil state 'syntax-table)
488 (condition-case nil
489 (progn
490 (goto-char (1+ (nth 8 state)))
491 (up-list 1))
492 (scan-error (goto-char (point-max)))))
493 (put-text-property (nth 8 state) (point)
494 'jit-lock-defer-multiline t)
495 (looking-at "[ \t]*\\sw*e")))))
496 nil)
497 (t (funcall (default-value 'font-lock-syntactic-face-function) state))))
499 (defcustom perl-indent-level 4
500 "Indentation of Perl statements with respect to containing block."
501 :type 'integer)
503 ;; Is is not unusual to put both things like perl-indent-level and
504 ;; cperl-indent-level in the local variable section of a file. If only
505 ;; one of perl-mode and cperl-mode is in use, a warning will be issued
506 ;; about the variable. Autoload these here, so that no warning is
507 ;; issued when using either perl-mode or cperl-mode.
508 ;;;###autoload(put 'perl-indent-level 'safe-local-variable 'integerp)
509 ;;;###autoload(put 'perl-continued-statement-offset 'safe-local-variable 'integerp)
510 ;;;###autoload(put 'perl-continued-brace-offset 'safe-local-variable 'integerp)
511 ;;;###autoload(put 'perl-brace-offset 'safe-local-variable 'integerp)
512 ;;;###autoload(put 'perl-brace-imaginary-offset 'safe-local-variable 'integerp)
513 ;;;###autoload(put 'perl-label-offset 'safe-local-variable 'integerp)
515 (defcustom perl-continued-statement-offset 4
516 "Extra indent for lines not starting new statements."
517 :type 'integer)
518 (defcustom perl-continued-brace-offset -4
519 "Extra indent for substatements that start with open-braces.
520 This is in addition to `perl-continued-statement-offset'."
521 :type 'integer)
522 (defcustom perl-brace-offset 0
523 "Extra indentation for braces, compared with other text in same context."
524 :type 'integer)
525 (defcustom perl-brace-imaginary-offset 0
526 "Imagined indentation of an open brace that actually follows a statement."
527 :type 'integer)
528 (defcustom perl-label-offset -2
529 "Offset of Perl label lines relative to usual indentation."
530 :type 'integer)
531 (defcustom perl-indent-continued-arguments nil
532 "If non-nil offset of argument lines relative to usual indentation.
533 If nil, continued arguments are aligned with the first argument."
534 :type '(choice integer (const nil)))
536 (defcustom perl-indent-parens-as-block nil
537 "Non-nil means that non-block ()-, {}- and []-groups are indented as blocks.
538 The closing bracket is aligned with the line of the opening bracket,
539 not the contents of the brackets."
540 :version "24.3"
541 :type 'boolean)
543 (defcustom perl-tab-always-indent tab-always-indent
544 "Non-nil means TAB in Perl mode always indents the current line.
545 Otherwise it inserts a tab character if you type it past the first
546 nonwhite character on the line."
547 :type 'boolean)
549 ;; I changed the default to nil for consistency with general Emacs
550 ;; conventions -- rms.
551 (defcustom perl-tab-to-comment nil
552 "Non-nil means TAB moves to eol or makes a comment in some cases.
553 For lines which don't need indenting, TAB either indents an
554 existing comment, moves to end-of-line, or if at end-of-line already,
555 create a new comment."
556 :type 'boolean)
558 (defcustom perl-nochange "\f"
559 "Lines starting with this regular expression are not auto-indented."
560 :type 'regexp
561 :options '(";?#\\|\f\\|\\s(\\|\\(\\w\\|\\s_\\)+:[^:]"))
563 ;; Outline support
565 (defvar perl-outline-regexp
566 (concat (mapconcat 'cadr perl-imenu-generic-expression "\\|")
567 "\\|^=cut\\>"))
569 (defun perl-outline-level ()
570 (cond
571 ((looking-at "[ \t]*\\(package\\)\\s-")
572 (- (match-beginning 1) (match-beginning 0)))
573 ((looking-at "[ \t]*s\\(ub\\)\\s-")
574 (- (match-beginning 1) (match-beginning 0)))
575 ((looking-at "=head[0-9]") (- (char-before (match-end 0)) ?0))
576 ((looking-at "=cut") 1)
577 (t 3)))
579 (defun perl-current-defun-name ()
580 "The `add-log-current-defun' function in Perl mode."
581 (save-excursion
582 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
583 (match-string-no-properties 1))))
586 (defvar perl-mode-hook nil
587 "Normal hook to run when entering Perl mode.")
589 ;;;###autoload
590 (define-derived-mode perl-mode prog-mode "Perl"
591 "Major mode for editing Perl code.
592 Expression and list commands understand all Perl brackets.
593 Tab indents for Perl code.
594 Comments are delimited with # ... \\n.
595 Paragraphs are separated by blank lines only.
596 Delete converts tabs to spaces as it moves back.
597 \\{perl-mode-map}
598 Variables controlling indentation style:
599 `perl-tab-always-indent'
600 Non-nil means TAB in Perl mode should always indent the current line,
601 regardless of where in the line point is when the TAB command is used.
602 `perl-tab-to-comment'
603 Non-nil means that for lines which don't need indenting, TAB will
604 either delete an empty comment, indent an existing comment, move
605 to end-of-line, or if at end-of-line already, create a new comment.
606 `perl-nochange'
607 Lines starting with this regular expression are not auto-indented.
608 `perl-indent-level'
609 Indentation of Perl statements within surrounding block.
610 The surrounding block's indentation is the indentation
611 of the line on which the open-brace appears.
612 `perl-continued-statement-offset'
613 Extra indentation given to a substatement, such as the
614 then-clause of an if or body of a while.
615 `perl-continued-brace-offset'
616 Extra indentation given to a brace that starts a substatement.
617 This is in addition to `perl-continued-statement-offset'.
618 `perl-brace-offset'
619 Extra indentation for line if it starts with an open brace.
620 `perl-brace-imaginary-offset'
621 An open brace following other text is treated as if it were
622 this far to the right of the start of its line.
623 `perl-label-offset'
624 Extra indentation for line that is a label.
625 `perl-indent-continued-arguments'
626 Offset of argument lines relative to usual indentation.
628 Various indentation styles: K&R BSD BLK GNU LW
629 perl-indent-level 5 8 0 2 4
630 perl-continued-statement-offset 5 8 4 2 4
631 perl-continued-brace-offset 0 0 0 0 -4
632 perl-brace-offset -5 -8 0 0 0
633 perl-brace-imaginary-offset 0 0 4 0 0
634 perl-label-offset -5 -8 -2 -2 -2
636 Turning on Perl mode runs the normal hook `perl-mode-hook'."
637 :abbrev-table perl-mode-abbrev-table
638 (setq-local paragraph-start (concat "$\\|" page-delimiter))
639 (setq-local paragraph-separate paragraph-start)
640 (setq-local paragraph-ignore-fill-prefix t)
641 (setq-local indent-line-function #'perl-indent-line)
642 (setq-local comment-start "# ")
643 (setq-local comment-end "")
644 (setq-local comment-start-skip "\\(^\\|\\s-\\);?#+ *")
645 (setq-local comment-indent-function #'perl-comment-indent)
646 (setq-local parse-sexp-ignore-comments t)
648 ;; Tell font-lock.el how to handle Perl.
649 (setq font-lock-defaults '((perl-font-lock-keywords
650 perl-font-lock-keywords-1
651 perl-font-lock-keywords-2)
652 nil nil ((?\_ . "w")) nil
653 (font-lock-syntactic-face-function
654 . perl-font-lock-syntactic-face-function)))
655 (setq-local prettify-symbols-alist perl--prettify-symbols-alist)
656 (setq-local syntax-propertize-function #'perl-syntax-propertize-function)
657 (add-hook 'syntax-propertize-extend-region-functions
658 #'syntax-propertize-multiline 'append 'local)
659 ;; Electricity.
660 ;; FIXME: setup electric-layout-rules.
661 (setq-local electric-indent-chars
662 (append '(?\{ ?\} ?\; ?\:) electric-indent-chars))
663 (add-hook 'electric-indent-functions #'perl-electric-noindent-p nil t)
664 ;; Tell imenu how to handle Perl.
665 (setq-local imenu-generic-expression perl-imenu-generic-expression)
666 (setq imenu-case-fold-search nil)
667 ;; Setup outline-minor-mode.
668 (setq-local outline-regexp perl-outline-regexp)
669 (setq-local outline-level 'perl-outline-level)
670 (setq-local add-log-current-defun-function #'perl-current-defun-name))
672 ;; This is used by indent-for-comment
673 ;; to decide how much to indent a comment in Perl code
674 ;; based on its context.
675 (defun perl-comment-indent ()
676 (if (and (bolp) (not (eolp)))
677 0 ;Existing comment at bol stays there.
678 comment-column))
680 (define-obsolete-function-alias 'electric-perl-terminator
681 'perl-electric-terminator "22.1")
682 (defun perl-electric-noindent-p (_char)
683 (unless (eolp) 'no-indent))
685 (defun perl-electric-terminator (arg)
686 "Insert character and maybe adjust indentation.
687 If at end-of-line, and not in a comment or a quote, correct the indentation."
688 (interactive "P")
689 (let ((insertpos (point)))
690 (and (not arg) ; decide whether to indent
691 (eolp)
692 (save-excursion
693 (beginning-of-line)
694 (and (not ; eliminate comments quickly
695 (and comment-start-skip
696 (re-search-forward comment-start-skip insertpos t)) )
697 (or (/= last-command-event ?:)
698 ;; Colon is special only after a label ....
699 (looking-at "\\s-*\\(\\w\\|\\s_\\)+$"))
700 (let ((pps (parse-partial-sexp
701 (perl-beginning-of-function) insertpos)))
702 (not (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))
703 (progn ; must insert, indent, delete
704 (insert-char last-command-event 1)
705 (perl-indent-line)
706 (delete-char -1))))
707 (self-insert-command (prefix-numeric-value arg)))
708 (make-obsolete 'perl-electric-terminator 'electric-indent-mode "24.4")
710 ;; not used anymore, but may be useful someday:
711 ;;(defun perl-inside-parens-p ()
712 ;; (condition-case ()
713 ;; (save-excursion
714 ;; (save-restriction
715 ;; (narrow-to-region (point)
716 ;; (perl-beginning-of-function))
717 ;; (goto-char (point-max))
718 ;; (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
719 ;; (error nil)))
721 (defun perl-indent-command (&optional arg)
722 "Indent Perl code in the active region or current line.
723 In Transient Mark mode, when the region is active, reindent the region.
724 Otherwise, with a prefix argument, reindent the current line
725 unconditionally.
727 Otherwise, if `perl-tab-always-indent' is nil and point is not in
728 the indentation area at the beginning of the line, insert a tab.
730 Otherwise, indent the current line. If point was within the
731 indentation area, it is moved to the end of the indentation area.
732 If the line was already indented properly and point was not
733 within the indentation area, and if `perl-tab-to-comment' is
734 non-nil (the default), then do the first possible action from the
735 following list:
737 1) delete an empty comment
738 2) move forward to start of comment, indenting if necessary
739 3) move forward to end of line
740 4) create an empty comment
741 5) move backward to start of comment, indenting if necessary."
742 (interactive "P")
743 (cond ((use-region-p) ; indent the active region
744 (indent-region (region-beginning) (region-end)))
745 (arg
746 (perl-indent-line "\f")) ; just indent this line
747 ((and (not perl-tab-always-indent)
748 (> (current-column) (current-indentation)))
749 (insert-tab))
751 (let* ((oldpnt (point))
752 (lsexp (progn (beginning-of-line) (point)))
753 (bof (perl-beginning-of-function))
754 (delta (progn
755 (goto-char oldpnt)
756 (perl-indent-line "\f\\|;?#" bof))))
757 (and perl-tab-to-comment
758 (= oldpnt (point)) ; done if point moved
759 (if (listp delta) ; if line starts in a quoted string
760 (setq lsexp (or (nth 2 delta) bof))
761 (= delta 0)) ; done if indenting occurred
762 (let ((eol (progn (end-of-line) (point)))
763 state)
764 (cond ((= (char-after bof) ?=)
765 (if (= oldpnt eol)
766 (message "In a format statement")))
767 ((progn (setq state (parse-partial-sexp lsexp eol))
768 (nth 3 state))
769 (if (= oldpnt eol) ; already at eol in a string
770 (message "In a string which starts with a %c."
771 (nth 3 state))))
772 ((not (nth 4 state))
773 (if (= oldpnt eol) ; no comment, create one?
774 (indent-for-comment)))
775 ((progn (beginning-of-line)
776 (and comment-start-skip
777 (re-search-forward
778 comment-start-skip eol 'move)))
779 (if (eolp)
780 (progn ; delete existing comment
781 (goto-char (match-beginning 0))
782 (skip-chars-backward " \t")
783 (delete-region (point) eol))
784 (if (or (< oldpnt (point)) (= oldpnt eol))
785 (indent-for-comment) ; indent existing comment
786 (end-of-line))))
787 ((/= oldpnt eol)
788 (end-of-line))
790 (message "Use backslash to quote # characters.")
791 (ding t)))))))))
792 (make-obsolete 'perl-indent-command 'indent-according-to-mode "24.4")
794 (defun perl-indent-line (&optional nochange parse-start)
795 "Indent current line as Perl code.
796 Return the amount the indentation
797 changed by, or (parse-state) if line starts in a quoted string."
798 (let ((case-fold-search nil)
799 (pos (- (point-max) (point)))
800 (bof (or parse-start (save-excursion
801 ;; Don't consider text on this line as a
802 ;; valid BOF from which to indent.
803 (goto-char (line-end-position 0))
804 (perl-beginning-of-function))))
805 beg indent shift-amt)
806 (beginning-of-line)
807 (setq beg (point))
808 (setq shift-amt
809 (cond ((eq (char-after bof) ?=) 0)
810 ((listp (setq indent (perl-calculate-indent bof))) indent)
811 ((eq 'noindent indent) indent)
812 ((looking-at (or nochange perl-nochange)) 0)
814 (skip-chars-forward " \t\f")
815 (setq indent (perl-indent-new-calculate nil indent bof))
816 (- indent (current-column)))))
817 (skip-chars-forward " \t\f")
818 (if (and (numberp shift-amt) (/= 0 shift-amt))
819 (progn (delete-region beg (point))
820 (indent-to indent)))
821 ;; If initial point was within line's indentation,
822 ;; position after the indentation. Else stay at same point in text.
823 (if (> (- (point-max) pos) (point))
824 (goto-char (- (point-max) pos)))
825 shift-amt))
827 (defun perl-continuation-line-p (limit)
828 "Move to end of previous line and return non-nil if continued."
829 ;; Statement level. Is it a continuation or a new statement?
830 ;; Find previous non-comment character.
831 (perl-backward-to-noncomment)
832 ;; Back up over label lines, since they don't
833 ;; affect whether our line is a continuation.
834 (while (or (eq (preceding-char) ?\,)
835 (and (eq (preceding-char) ?:)
836 (memq (char-syntax (char-after (- (point) 2)))
837 '(?w ?_))))
838 (if (eq (preceding-char) ?\,)
839 (perl-backward-to-start-of-continued-exp limit)
840 (beginning-of-line))
841 (perl-backward-to-noncomment))
842 ;; Now we get the answer.
843 (not (memq (preceding-char) '(?\; ?\} ?\{))))
845 (defun perl-hanging-paren-p ()
846 "Non-nil if we are right after a hanging parenthesis-like char."
847 (and (looking-at "[ \t]*$")
848 (save-excursion
849 (skip-syntax-backward " (") (not (bolp)))))
851 (defun perl-indent-new-calculate (&optional virtual default parse-start)
853 (and virtual (save-excursion (skip-chars-backward " \t") (bolp))
854 (current-column))
855 (and (looking-at "\\(\\w\\|\\s_\\)+:[^:]")
856 (max 1 (+ (or default (perl-calculate-indent parse-start))
857 perl-label-offset)))
858 (and (= (char-syntax (following-char)) ?\))
859 (save-excursion
860 (forward-char 1)
861 (when (condition-case nil (progn (forward-sexp -1) t)
862 (scan-error nil))
863 (perl-indent-new-calculate
864 ;; Recalculate the parsing-start, since we may have jumped
865 ;; dangerously close (typically in the case of nested functions).
866 'virtual nil (save-excursion (perl-beginning-of-function))))))
867 (and (and (= (following-char) ?{)
868 (save-excursion (forward-char) (perl-hanging-paren-p)))
869 (+ (or default (perl-calculate-indent parse-start))
870 perl-brace-offset))
871 (or default (perl-calculate-indent parse-start))))
873 (defun perl-calculate-indent (&optional parse-start)
874 "Return appropriate indentation for current line as Perl code.
875 In usual case returns an integer: the column to indent to.
876 Returns (parse-state) if line starts inside a string.
877 Optional argument PARSE-START should be the position of `beginning-of-defun'."
878 (save-excursion
879 (let ((indent-point (point))
880 (case-fold-search nil)
881 (colon-line-end 0)
882 state containing-sexp)
883 (if parse-start ;used to avoid searching
884 (goto-char parse-start)
885 (perl-beginning-of-function))
886 ;; We might be now looking at a local function that has nothing to
887 ;; do with us because `indent-point' is past it. In this case
888 ;; look further back up for another `perl-beginning-of-function'.
889 (while (and (looking-at "{")
890 (save-excursion
891 (beginning-of-line)
892 (looking-at "\\s-+sub\\>"))
893 (> indent-point (save-excursion
894 (condition-case nil
895 (forward-sexp 1)
896 (scan-error nil))
897 (point))))
898 (perl-beginning-of-function))
899 (while (< (point) indent-point) ;repeat until right sexp
900 (setq state (parse-partial-sexp (point) indent-point 0))
901 ;; state = (depth_in_parens innermost_containing_list
902 ;; last_complete_sexp string_terminator_or_nil inside_commentp
903 ;; following_quotep minimum_paren-depth_this_scan)
904 ;; Parsing stops if depth in parentheses becomes equal to third arg.
905 (setq containing-sexp (nth 1 state)))
906 (cond
907 ;; Don't auto-indent in a quoted string or a here-document.
908 ((or (nth 3 state) (eq 2 (nth 7 state))) 'noindent)
909 ((null containing-sexp) ; Line is at top level.
910 (skip-chars-forward " \t\f")
911 (if (memq (following-char)
912 (if perl-indent-parens-as-block '(?\{ ?\( ?\[) '(?\{)))
913 0 ; move to beginning of line if it starts a function body
914 ;; indent a little if this is a continuation line
915 (perl-backward-to-noncomment)
916 (if (or (bobp)
917 (memq (preceding-char) '(?\; ?\})))
918 0 perl-continued-statement-offset)))
919 ((/= (char-after containing-sexp) ?{)
920 ;; line is expression, not statement:
921 ;; indent to just after the surrounding open.
922 (goto-char (1+ containing-sexp))
923 (if (perl-hanging-paren-p)
924 ;; We're indenting an arg of a call like:
925 ;; $a = foobarlongnamefun (
926 ;; arg1
927 ;; arg2
928 ;; );
929 (progn
930 (skip-syntax-backward "(")
931 (condition-case nil
932 (while (save-excursion
933 (skip-syntax-backward " ") (not (bolp)))
934 (forward-sexp -1))
935 (scan-error nil))
936 (+ (current-column) perl-indent-level))
937 (if perl-indent-continued-arguments
938 (+ perl-indent-continued-arguments (current-indentation))
939 (skip-chars-forward " \t")
940 (current-column))))
942 ;; Statement level. Is it a continuation or a new statement?
943 (if (perl-continuation-line-p containing-sexp)
944 ;; This line is continuation of preceding line's statement;
945 ;; indent perl-continued-statement-offset more than the
946 ;; previous line of the statement.
947 (progn
948 (perl-backward-to-start-of-continued-exp containing-sexp)
949 (+ (if (save-excursion
950 (perl-continuation-line-p containing-sexp))
951 ;; If the continued line is itself a continuation
952 ;; line, then align, otherwise add an offset.
953 0 perl-continued-statement-offset)
954 (current-column)
955 (if (save-excursion (goto-char indent-point)
956 (looking-at
957 (if perl-indent-parens-as-block
958 "[ \t]*[{(\[]" "[ \t]*{")))
959 perl-continued-brace-offset 0)))
960 ;; This line starts a new statement.
961 ;; Position at last unclosed open.
962 (goto-char containing-sexp)
964 ;; Is line first statement after an open-brace?
965 ;; If no, find that first statement and indent like it.
966 (save-excursion
967 (forward-char 1)
968 ;; Skip over comments and labels following openbrace.
969 (while (progn
970 (skip-chars-forward " \t\f\n")
971 (cond ((looking-at ";?#")
972 (forward-line 1) t)
973 ((looking-at "\\(\\w\\|\\s_\\)+:[^:]")
974 (setq colon-line-end (line-end-position))
975 (search-forward ":")))))
976 ;; The first following code counts
977 ;; if it is before the line we want to indent.
978 (and (< (point) indent-point)
979 (if (> colon-line-end (point))
980 (- (current-indentation) perl-label-offset)
981 (current-column))))
982 ;; If no previous statement,
983 ;; indent it relative to line brace is on.
984 ;; For open paren in column zero, don't let statement
985 ;; start there too. If perl-indent-level is zero,
986 ;; use perl-brace-offset + perl-continued-statement-offset
987 ;; For open-braces not the first thing in a line,
988 ;; add in perl-brace-imaginary-offset.
989 (+ (if (and (bolp) (zerop perl-indent-level))
990 (+ perl-brace-offset perl-continued-statement-offset)
991 perl-indent-level)
992 ;; Move back over whitespace before the openbrace.
993 ;; If openbrace is not first nonwhite thing on the line,
994 ;; add the perl-brace-imaginary-offset.
995 (progn (skip-chars-backward " \t")
996 (if (bolp) 0 perl-brace-imaginary-offset))
997 ;; If the openbrace is preceded by a parenthesized exp,
998 ;; move to the beginning of that;
999 ;; possibly a different line
1000 (progn
1001 (if (eq (preceding-char) ?\))
1002 (forward-sexp -1))
1003 ;; Get initial indentation of the line we are on.
1004 (current-indentation))))))))))
1006 (defun perl-backward-to-noncomment ()
1007 "Move point backward to after the first non-white-space, skipping comments."
1008 (interactive)
1009 (forward-comment (- (point-max))))
1011 (defun perl-backward-to-start-of-continued-exp (lim)
1012 (if (= (preceding-char) ?\))
1013 (forward-sexp -1))
1014 (beginning-of-line)
1015 (if (<= (point) lim)
1016 (goto-char (1+ lim)))
1017 (skip-chars-forward " \t\f"))
1019 ;; note: this may be slower than the c-mode version, but I can understand it.
1020 (defalias 'indent-perl-exp 'perl-indent-exp)
1021 (defun perl-indent-exp ()
1022 "Indent each line of the Perl grouping following point."
1023 (interactive)
1024 (let* ((case-fold-search nil)
1025 (oldpnt (point-marker))
1026 (bof-mark (save-excursion
1027 (end-of-line 2)
1028 (perl-beginning-of-function)
1029 (point-marker)))
1030 eol last-mark lsexp-mark delta)
1031 (if (= (char-after (marker-position bof-mark)) ?=)
1032 (message "Can't indent a format statement")
1033 (message "Indenting Perl expression...")
1034 (setq eol (line-end-position))
1035 (save-excursion ; locate matching close paren
1036 (while (and (not (eobp)) (<= (point) eol))
1037 (parse-partial-sexp (point) (point-max) 0))
1038 (setq last-mark (point-marker)))
1039 (setq lsexp-mark bof-mark)
1040 (beginning-of-line)
1041 (while (< (point) (marker-position last-mark))
1042 (setq delta (perl-indent-line nil (marker-position bof-mark)))
1043 (if (numberp delta) ; unquoted start-of-line?
1044 (progn
1045 (if (eolp)
1046 (delete-horizontal-space))
1047 (setq lsexp-mark (point-marker))))
1048 (end-of-line)
1049 (setq eol (point))
1050 (if (nth 4 (parse-partial-sexp (marker-position lsexp-mark) eol))
1051 (progn ; line ends in a comment
1052 (beginning-of-line)
1053 (if (or (not (looking-at "\\s-*;?#"))
1054 (listp delta)
1055 (and (/= 0 delta)
1056 (= (- (current-indentation) delta) comment-column)))
1057 (if (and comment-start-skip
1058 (re-search-forward comment-start-skip eol t))
1059 (indent-for-comment))))) ; indent existing comment
1060 (forward-line 1))
1061 (goto-char (marker-position oldpnt))
1062 (message "Indenting Perl expression...done"))))
1064 (defun perl-beginning-of-function (&optional arg)
1065 "Move backward to next beginning-of-function, or as far as possible.
1066 With argument, repeat that many times; negative args move forward.
1067 Returns new value of point in all cases."
1068 (interactive "p")
1069 (or arg (setq arg 1))
1070 (if (< arg 0) (forward-char 1))
1071 (and (/= arg 0)
1072 (re-search-backward
1073 "^\\s(\\|^\\s-*sub\\b[ \t\n]*\\_<[^{]+{\\|^\\s-*format\\b[^=]*=\\|^\\."
1074 nil 'move arg)
1075 (goto-char (1- (match-end 0))))
1076 (point))
1078 ;; note: this routine is adapted directly from emacs lisp.el, end-of-defun;
1079 ;; no bugs have been removed :-)
1080 (defun perl-end-of-function (&optional arg)
1081 "Move forward to next end-of-function.
1082 The end of a function is found by moving forward from the beginning of one.
1083 With argument, repeat that many times; negative args move backward."
1084 (interactive "p")
1085 (or arg (setq arg 1))
1086 (let ((first t))
1087 (while (and (> arg 0) (< (point) (point-max)))
1088 (let ((pos (point)))
1089 (while (progn
1090 (if (and first
1091 (progn
1092 (forward-char 1)
1093 (perl-beginning-of-function 1)
1094 (not (bobp))))
1096 (or (bobp) (forward-char -1))
1097 (perl-beginning-of-function -1))
1098 (setq first nil)
1099 (forward-list 1)
1100 (skip-chars-forward " \t")
1101 (if (looking-at "[#\n]")
1102 (forward-line 1))
1103 (<= (point) pos))))
1104 (setq arg (1- arg)))
1105 (while (< arg 0)
1106 (let ((pos (point)))
1107 (perl-beginning-of-function 1)
1108 (forward-sexp 1)
1109 (forward-line 1)
1110 (if (>= (point) pos)
1111 (if (progn (perl-beginning-of-function 2) (not (bobp)))
1112 (progn
1113 (forward-list 1)
1114 (skip-chars-forward " \t")
1115 (if (looking-at "[#\n]")
1116 (forward-line 1)))
1117 (goto-char (point-min)))))
1118 (setq arg (1+ arg)))))
1120 (defalias 'mark-perl-function 'perl-mark-function)
1121 (defun perl-mark-function ()
1122 "Put mark at end of Perl function, point at beginning."
1123 (interactive)
1124 (push-mark (point))
1125 (perl-end-of-function)
1126 (push-mark (point))
1127 (perl-beginning-of-function)
1128 (backward-paragraph))
1130 (provide 'perl-mode)
1132 ;;; perl-mode.el ends here