Revert "Support all perl variable declarators and prefixes"
[emacs.git] / lisp / progmodes / perl-mode.el
blobc9bfb1acdfeedaad755e534761e0ec04ccc6ae1c
1 ;;; perl-mode.el --- Perl code editing commands for GNU Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1990, 1994, 2001-2018 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 <https://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.
70 ;; Here are some ugly tricks to bypass some of these problems: the perl
71 ;; expression /`/ (that's a back-tick) usually evaluates harmlessly,
72 ;; but will trick perl-mode into starting a quoted string, which
73 ;; can be ended with another /`/. Assuming you have no embedded
74 ;; back-ticks, this can used to help solve problem 3:
76 ;; /`/; $ugly = q?"'$?; /`/;
78 ;; The same trick can be used for problem 6 as in:
79 ;; /{/; while (<${glob_me}>)
80 ;; but a simpler solution is to add a space between the $ and the {:
81 ;; while (<$ {glob_me}>)
83 ;; Problem 7 is even worse, but this 'fix' does work :-(
84 ;; $DB'stop#'
85 ;; [$DB'line#'
86 ;; ] =~ s/;9$//;
88 ;;; Code:
90 (defgroup perl nil
91 "Major mode for editing Perl code."
92 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
93 :prefix "perl-"
94 :group 'languages)
96 (defvar perl-mode-abbrev-table nil
97 "Abbrev table in use in perl-mode buffers.")
98 (define-abbrev-table 'perl-mode-abbrev-table ())
100 (defvar perl-mode-map
101 (let ((map (make-sparse-keymap)))
102 (define-key map "\e\C-a" 'perl-beginning-of-function)
103 (define-key map "\e\C-e" 'perl-end-of-function)
104 (define-key map "\e\C-h" 'perl-mark-function)
105 (define-key map "\e\C-q" 'perl-indent-exp)
106 (define-key map "\177" 'backward-delete-char-untabify)
107 map)
108 "Keymap used in Perl mode.")
110 (defvar perl-mode-syntax-table
111 (let ((st (make-syntax-table (standard-syntax-table))))
112 (modify-syntax-entry ?\n ">" st)
113 (modify-syntax-entry ?# "<" st)
114 ;; `$' is also a prefix char so I was tempted to say "/ p",
115 ;; but the `p' thingy basically overrides the `/' :-( -- Stef
116 (modify-syntax-entry ?$ "/" st)
117 (modify-syntax-entry ?% ". p" st)
118 (modify-syntax-entry ?@ ". p" st)
119 (modify-syntax-entry ?& "." st)
120 (modify-syntax-entry ?\' "\"" st)
121 (modify-syntax-entry ?* "." st)
122 (modify-syntax-entry ?+ "." st)
123 (modify-syntax-entry ?- "." st)
124 (modify-syntax-entry ?/ "." st)
125 (modify-syntax-entry ?< "." st)
126 (modify-syntax-entry ?= "." st)
127 (modify-syntax-entry ?> "." st)
128 (modify-syntax-entry ?\\ "\\" st)
129 (modify-syntax-entry ?` "\"" st)
130 (modify-syntax-entry ?| "." st)
132 "Syntax table in use in `perl-mode' buffers.")
134 (defvar perl-imenu-generic-expression
135 '(;; Functions
136 (nil "^[ \t]*sub\\s-+\\([-[:alnum:]+_:]+\\)" 1)
137 ;;Variables
138 ("Variables" "^\\(?:my\\|our\\)\\s-+\\([$@%][-[:alnum:]+_:]+\\)\\s-*=" 1)
139 ("Packages" "^[ \t]*package\\s-+\\([-[:alnum:]+_:]+\\);" 1)
140 ("Doc sections" "^=head[0-9][ \t]+\\(.*\\)" 1))
141 "Imenu generic expression for Perl mode. See `imenu-generic-expression'.")
143 ;; Regexps updated with help from Tom Tromey <tromey@cambric.colorado.edu> and
144 ;; Jim Campbell <jec@murzim.ca.boeing.com>.
146 (defconst perl--prettify-symbols-alist
147 '(("->" . ?→)
148 ("=>" . ?⇒)
149 ("::" . ?∷)))
151 (defconst perl-font-lock-keywords-1
152 '(;; What is this for?
153 ;;("\\(--- .* ---\\|=== .* ===\\)" . font-lock-string-face)
155 ;; Fontify preprocessor statements as we do in `c-font-lock-keywords'.
156 ;; Ilya Zakharevich <ilya@math.ohio-state.edu> thinks this is a bad idea.
157 ;; ("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
158 ;; ("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
159 ;; ("^#[ \t]*if\\>"
160 ;; ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
161 ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t)))
162 ;; ("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
163 ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t))
165 ;; Fontify function and package names in declarations.
166 ("\\<\\(package\\|sub\\)\\>[ \t]*\\(\\sw+\\)?"
167 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
168 ("\\<\\(import\\|no\\|require\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
169 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t)))
170 "Subdued level highlighting for Perl mode.")
172 (defconst perl-font-lock-keywords-2
173 (append
174 perl-font-lock-keywords-1
175 `( ;; Fontify keywords, except those fontified otherwise.
176 ,(concat "\\<"
177 (regexp-opt '("if" "until" "while" "elsif" "else" "unless"
178 "do" "dump" "for" "foreach" "exit" "die"
179 "BEGIN" "END" "return" "exec" "eval") t)
180 "\\>")
182 ;; Fontify local and my keywords as types.
183 ("\\<\\(local\\|my\\)\\>" . font-lock-type-face)
185 ;; Fontify function, variable and file name references.
186 ("&\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-function-name-face)
187 ;; Additionally underline non-scalar variables. Maybe this is a bad idea.
188 ;;'("[$@%*][#{]?\\(\\sw+\\)" 1 font-lock-variable-name-face)
189 ("[$*]{?\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-variable-name-face)
190 ("\\([@%]\\|\\$#\\)\\(\\sw+\\(::\\sw+\\)*\\)"
191 (2 (cons font-lock-variable-name-face '(underline))))
192 ("<\\(\\sw+\\)>" 1 font-lock-constant-face)
194 ;; Fontify keywords with/and labels as we do in `c++-font-lock-keywords'.
195 ("\\<\\(continue\\|goto\\|last\\|next\\|redo\\)\\>[ \t]*\\(\\sw+\\)?"
196 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
197 ("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-constant-face)))
198 "Gaudy level highlighting for Perl mode.")
200 (defvar perl-font-lock-keywords perl-font-lock-keywords-1
201 "Default expressions to highlight in Perl mode.")
203 (defvar perl-quote-like-pairs
204 '((?\( . ?\)) (?\[ . ?\]) (?\{ . ?\}) (?\< . ?\>)))
206 (eval-and-compile
207 (defconst perl--syntax-exp-intro-keywords
208 '("split" "if" "unless" "until" "while" "print"
209 "grep" "map" "not" "or" "and" "for" "foreach"))
211 (defconst perl--syntax-exp-intro-regexp
212 (concat "\\(?:\\(?:^\\|[^$@&%[:word:]]\\)"
213 (regexp-opt perl--syntax-exp-intro-keywords)
214 "\\|[-?:.,;|&+*=!~({[]\\|\\(^\\)\\)[ \t\n]*")))
216 (defun perl-syntax-propertize-function (start end)
217 (let ((case-fold-search nil))
218 (goto-char start)
219 (perl-syntax-propertize-special-constructs end)
220 (funcall
221 (syntax-propertize-rules
222 ;; Turn POD into b-style comments. Place the cut rule first since it's
223 ;; more specific.
224 ("^=cut\\>.*\\(\n\\)" (1 "> b"))
225 ("^\\(=\\)\\sw" (1 "< b"))
226 ;; Catch ${ so that ${var} doesn't screw up indentation.
227 ;; This also catches $' to handle 'foo$', although it should really
228 ;; check that it occurs inside a '..' string.
229 ("\\(\\$\\)[{']" (1 (unless (and (eq ?\' (char-after (match-end 1)))
230 (save-excursion
231 (not (nth 3 (syntax-ppss
232 (match-beginning 0))))))
233 (string-to-syntax ". p"))))
234 ;; Handle funny names like $DB'stop.
235 ("\\$ ?{?^?[_[:alpha:]][_[:alnum:]]*\\('\\)[_[:alpha:]]" (1 "_"))
236 ;; format statements
237 ("^[ \t]*format.*=[ \t]*\\(\n\\)"
238 (1 (prog1 "\"" (perl-syntax-propertize-special-constructs end))))
239 ;; Propertize perl prototype chars `$%&*;+@\[]' as punctuation
240 ;; in `sub' arg-specs like `sub myfun ($)' and `sub ($)'. But
241 ;; don't match subroutine signatures like `sub add ($a, $b)', or
242 ;; anonymous subs like "sub { (...) ... }".
243 ("\\<sub\\(?:[\s\t\n]+\\(?:\\sw\\|\\s_\\)+\\)?[\s\t\n]*(\\([][$%&*;+@\\]+\\))"
244 (1 "."))
245 ;; Turn __DATA__ trailer into a comment.
246 ("^\\(_\\)_\\(?:DATA\\|END\\)__[ \t]*\\(?:\\(\n\\)#.-\\*-.*perl.*-\\*-\\|\n.*\\)"
247 (1 "< c") (2 "> c")
248 (0 (ignore (put-text-property (match-beginning 0) (match-end 0)
249 'syntax-multiline t))))
250 ;; Regexp and funny quotes. Distinguishing a / that starts a regexp
251 ;; match from the division operator is ...interesting.
252 ;; Basically, / is a regexp match if it's preceded by an infix operator
253 ;; (or some similar separator), or by one of the special keywords
254 ;; corresponding to builtin functions that can take their first arg
255 ;; without parentheses. Of course, that presume we're looking at the
256 ;; *opening* slash. We can afford to mis-match the closing ones
257 ;; here, because they will be re-treated separately later in
258 ;; perl-font-lock-special-syntactic-constructs.
259 ((concat perl--syntax-exp-intro-regexp "\\(/\\)")
260 (2 (ignore
261 (if (and (match-end 1) ; / at BOL.
262 (save-excursion
263 (goto-char (match-end 1))
264 (forward-comment (- (point-max)))
265 (put-text-property (point) (match-end 2)
266 'syntax-multiline t)
267 (not (or (and (eq ?w (char-syntax (preceding-char)))
268 (let ((end (point)))
269 (backward-sexp 1)
270 (member (buffer-substring (point) end)
271 perl--syntax-exp-intro-keywords)))
272 (memq (char-before)
273 '(?? ?: ?. ?, ?\; ?= ?! ?~ ?\( ?\[))))))
274 nil ;; A division sign instead of a regexp-match.
275 (put-text-property (match-beginning 2) (match-end 2)
276 'syntax-table (string-to-syntax "\""))
277 (perl-syntax-propertize-special-constructs end)))))
278 ("\\(^\\|[?:.,;=!~({[ \t]\\)\\([msy]\\|q[qxrw]?\\|tr\\)\\>\\s-*\\(?:\\([^])}>= \n\t]\\)\\|\\(?3:=\\)[^>]\\)"
279 ;; Nasty cases:
280 ;; /foo/m $a->m $#m $m @m %m
281 ;; \s (appears often in regexps).
282 ;; -s file
283 ;; y => 3
284 ;; sub tr {...}
285 (3 (ignore
286 (if (save-excursion (goto-char (match-beginning 0))
287 (forward-word-strictly -1)
288 (looking-at-p "sub[ \t\n]"))
289 ;; This is defining a function.
291 (unless (nth 8 (save-excursion (syntax-ppss (match-beginning 1))))
292 ;; Don't add this syntax-table property if
293 ;; within a string, which would misbehave in cases such as
294 ;; $a = "foo y \"toto\" bar" where we'd end up changing the
295 ;; syntax of the backslash and hence de-escaping the embedded
296 ;; double quote.
297 (put-text-property (match-beginning 3) (match-end 3)
298 'syntax-table
299 (if (assoc (char-after (match-beginning 3))
300 perl-quote-like-pairs)
301 (string-to-syntax "|")
302 (string-to-syntax "\"")))
303 (perl-syntax-propertize-special-constructs end))))))
304 ;; Here documents.
305 ((concat
306 "\\(?:"
307 ;; << "EOF", << 'EOF', or << \EOF
308 "<<\\(~\\)?[ \t]*\\('[^'\n]*'\\|\"[^\"\n]*\"\\|\\\\[[:alpha:]][[:alnum:]]*\\)"
309 ;; The <<EOF case which needs perl--syntax-exp-intro-regexp, to
310 ;; disambiguate with the left-bitshift operator.
311 "\\|" perl--syntax-exp-intro-regexp "<<\\(?2:\\sw+\\)\\)"
312 ".*\\(\n\\)")
313 (4 (let* ((st (get-text-property (match-beginning 4) 'syntax-table))
314 (name (match-string 2))
315 (indented (match-beginning 1)))
316 (goto-char (match-end 2))
317 (if (save-excursion (nth 8 (syntax-ppss (match-beginning 0))))
318 ;; Leave the property of the newline unchanged.
320 (cons (car (string-to-syntax "< c"))
321 ;; Remember the names of heredocs found on this line.
322 (cons (cons (pcase (aref name 0)
323 (`?\\ (substring name 1))
324 ((or `?\" `?\' `?\`) (substring name 1 -1))
325 (_ name))
326 indented)
327 (cdr st)))))))
328 ;; We don't call perl-syntax-propertize-special-constructs directly
329 ;; from the << rule, because there might be other elements (between
330 ;; the << and the \n) that need to be propertized.
331 ("\\(?:$\\)\\s<"
332 (0 (ignore (perl-syntax-propertize-special-constructs end))))
334 (point) end)))
336 (defvar perl-empty-syntax-table
337 (let ((st (copy-syntax-table)))
338 ;; Make all chars be of punctuation syntax.
339 (dotimes (i 256) (aset st i '(1)))
340 (modify-syntax-entry ?\\ "\\" st)
342 "Syntax table used internally for processing quote-like operators.")
344 (defun perl-quote-syntax-table (char)
345 (let ((close (cdr (assq char perl-quote-like-pairs)))
346 (st (copy-syntax-table perl-empty-syntax-table)))
347 (if (not close)
348 (modify-syntax-entry char "\"" st)
349 (modify-syntax-entry char "(" st)
350 (modify-syntax-entry close ")" st))
351 st))
353 (defun perl-syntax-propertize-special-constructs (limit)
354 "Propertize special constructs like regexps and formats."
355 (let ((state (syntax-ppss))
356 char)
357 (cond
358 ((eq 2 (nth 7 state))
359 ;; A Here document.
360 (let ((names (cdr (get-text-property (nth 8 state) 'syntax-table))))
361 (when (cdr names)
362 (setq names (reverse names))
363 ;; Multiple heredocs on a single line, we have to search from the
364 ;; beginning, since we don't know which names might be
365 ;; before point.
366 (goto-char (nth 8 state)))
367 (while (and names
368 (re-search-forward
369 (pcase-let ((`(,name . ,indented) (pop names)))
370 (concat "^" (if indented "[ \t]*")
371 (regexp-quote name) "\n"))
372 limit 'move))
373 (unless names
374 (put-text-property (1- (point)) (point) 'syntax-table
375 (string-to-syntax "> c"))))))
376 ((or (null (setq char (nth 3 state)))
377 (and (characterp char) (eq (char-syntax (nth 3 state)) ?\")))
378 ;; Normal text, or comment, or docstring, or normal string.
379 nil)
380 ((eq (nth 3 state) ?\n)
381 ;; A `format' command.
382 (when (re-search-forward "^\\s *\\.\\s *\n" limit 'move)
383 (put-text-property (1- (point)) (point)
384 'syntax-table (string-to-syntax "\""))))
386 ;; This is regexp like quote thingy.
387 (setq char (char-after (nth 8 state)))
388 (let ((startpos (point))
389 (twoargs (save-excursion
390 (goto-char (nth 8 state))
391 (skip-syntax-backward " ")
392 (skip-syntax-backward "w")
393 (member (buffer-substring
394 (point) (progn (forward-word-strictly 1)
395 (point)))
396 '("tr" "s" "y"))))
397 (close (cdr (assq char perl-quote-like-pairs)))
398 (st (perl-quote-syntax-table char)))
399 (when (with-syntax-table st
400 (if close
401 ;; For paired delimiters, Perl allows nesting them, but
402 ;; since we treat them as strings, Emacs does not count
403 ;; those delimiters in `state', so we don't know how deep
404 ;; we are: we have to go back to the beginning of this
405 ;; "string" and count from there.
406 (condition-case nil
407 (progn
408 ;; Start after the first char since it doesn't have
409 ;; paren-syntax (an alternative would be to let-bind
410 ;; parse-sexp-lookup-properties).
411 (goto-char (1+ (nth 8 state)))
412 (up-list 1)
414 ;; In case of error, make sure we don't move backward.
415 (scan-error (goto-char startpos) nil))
416 (not (or (nth 8 (parse-partial-sexp
417 ;; Since we don't know if point is within
418 ;; the first or the scond arg, we have to
419 ;; start from the beginning.
420 (if twoargs (1+ (nth 8 state)) (point))
421 limit nil nil state 'syntax-table))
422 ;; If we have a self-paired opener and a twoargs
423 ;; command, the form is s/../../ so we have to skip
424 ;; a second time.
425 ;; In the case of s{...}{...}, we only handle the
426 ;; first part here and the next below.
427 (when (and twoargs (not close))
428 (nth 8 (parse-partial-sexp
429 (point) limit
430 nil nil state 'syntax-table)))))))
431 ;; Point is now right after the arg(s).
432 (when (eq (char-before (1- (point))) ?$)
433 (put-text-property (- (point) 2) (1- (point))
434 'syntax-table '(1)))
435 (put-text-property (1- (point)) (point)
436 'syntax-table
437 (if close
438 (string-to-syntax "|")
439 (string-to-syntax "\"")))
440 ;; If we have two args with a non-self-paired starter (e.g.
441 ;; s{...}{...}) we're right after the first arg, so we still have to
442 ;; handle the second part.
443 (when (and twoargs close)
444 ;; Skip whitespace and make sure that font-lock will
445 ;; refontify the second part in the proper context.
446 (put-text-property
447 (point) (progn (forward-comment (point-max)) (point))
448 'syntax-multiline t)
450 (when (< (point) limit)
451 (put-text-property (point) (1+ (point))
452 'syntax-table
453 (if (assoc (char-after)
454 perl-quote-like-pairs)
455 ;; Put an `e' in the cdr to mark this
456 ;; char as "second arg starter".
457 (string-to-syntax "|e")
458 (string-to-syntax "\"e")))
459 (forward-char 1)
460 ;; Re-use perl-syntax-propertize-special-constructs to handle the
461 ;; second part (the first delimiter of second part can't be
462 ;; preceded by "s" or "tr" or "y", so it will not be considered
463 ;; as twoarg).
464 (perl-syntax-propertize-special-constructs limit)))))))))
466 (defun perl-font-lock-syntactic-face-function (state)
467 (cond
468 ((and (nth 3 state)
469 (eq ?e (cdr-safe (get-text-property (nth 8 state) 'syntax-table)))
470 ;; This is a second-arg of s{..}{...} form; let's check if this second
471 ;; arg is executable code rather than a string. For that, we need to
472 ;; look for an "e" after this second arg, so we have to hunt for the
473 ;; end of the arg. Depending on whether the whole arg has already
474 ;; been syntax-propertized or not, the end-char will have different
475 ;; syntaxes, so let's ignore syntax-properties temporarily so we can
476 ;; pretend it has not been syntax-propertized yet.
477 (let* ((parse-sexp-lookup-properties nil)
478 (char (char-after (nth 8 state)))
479 (paired (assq char perl-quote-like-pairs)))
480 (with-syntax-table (perl-quote-syntax-table char)
481 (save-excursion
482 (if (not paired)
483 (parse-partial-sexp (point) (point-max)
484 nil nil state 'syntax-table)
485 (condition-case nil
486 (progn
487 (goto-char (1+ (nth 8 state)))
488 (up-list 1))
489 (scan-error (goto-char (point-max)))))
490 (put-text-property (nth 8 state) (point)
491 'jit-lock-defer-multiline t)
492 (looking-at "[ \t]*\\sw*e")))))
493 nil)
494 (t (funcall (default-value 'font-lock-syntactic-face-function) state))))
496 (defcustom perl-indent-level 4
497 "Indentation of Perl statements with respect to containing block."
498 :type 'integer)
500 ;; Is is not unusual to put both things like perl-indent-level and
501 ;; cperl-indent-level in the local variable section of a file. If only
502 ;; one of perl-mode and cperl-mode is in use, a warning will be issued
503 ;; about the variable. Autoload these here, so that no warning is
504 ;; issued when using either perl-mode or cperl-mode.
505 ;;;###autoload(put 'perl-indent-level 'safe-local-variable 'integerp)
506 ;;;###autoload(put 'perl-continued-statement-offset 'safe-local-variable 'integerp)
507 ;;;###autoload(put 'perl-continued-brace-offset 'safe-local-variable 'integerp)
508 ;;;###autoload(put 'perl-brace-offset 'safe-local-variable 'integerp)
509 ;;;###autoload(put 'perl-brace-imaginary-offset 'safe-local-variable 'integerp)
510 ;;;###autoload(put 'perl-label-offset 'safe-local-variable 'integerp)
512 (defcustom perl-continued-statement-offset 4
513 "Extra indent for lines not starting new statements."
514 :type 'integer)
515 (defcustom perl-continued-brace-offset -4
516 "Extra indent for substatements that start with open-braces.
517 This is in addition to `perl-continued-statement-offset'."
518 :type 'integer)
519 (defcustom perl-brace-offset 0
520 "Extra indentation for braces, compared with other text in same context."
521 :type 'integer)
522 (defcustom perl-brace-imaginary-offset 0
523 "Imagined indentation of an open brace that actually follows a statement."
524 :type 'integer)
525 (defcustom perl-label-offset -2
526 "Offset of Perl label lines relative to usual indentation."
527 :type 'integer)
528 (defcustom perl-indent-continued-arguments nil
529 "If non-nil offset of argument lines relative to usual indentation.
530 If nil, continued arguments are aligned with the first argument."
531 :type '(choice integer (const nil)))
533 (defcustom perl-indent-parens-as-block nil
534 "Non-nil means that non-block ()-, {}- and []-groups are indented as blocks.
535 The closing bracket is aligned with the line of the opening bracket,
536 not the contents of the brackets."
537 :version "24.3"
538 :type 'boolean)
540 (defcustom perl-tab-always-indent tab-always-indent
541 "Non-nil means TAB in Perl mode always indents the current line.
542 Otherwise it inserts a tab character if you type it past the first
543 nonwhite character on the line."
544 :type 'boolean)
546 ;; I changed the default to nil for consistency with general Emacs
547 ;; conventions -- rms.
548 (defcustom perl-tab-to-comment nil
549 "Non-nil means TAB moves to eol or makes a comment in some cases.
550 For lines which don't need indenting, TAB either indents an
551 existing comment, moves to end-of-line, or if at end-of-line already,
552 create a new comment."
553 :type 'boolean)
555 (defcustom perl-nochange "\f"
556 "Lines starting with this regular expression are not auto-indented."
557 :type 'regexp
558 :options '(";?#\\|\f\\|\\s(\\|\\(\\w\\|\\s_\\)+:[^:]"))
560 ;; Outline support
562 (defvar perl-outline-regexp
563 (concat (mapconcat 'cadr perl-imenu-generic-expression "\\|")
564 "\\|^=cut\\>"))
566 (defun perl-outline-level ()
567 (cond
568 ((looking-at "[ \t]*\\(package\\)\\s-")
569 (- (match-beginning 1) (match-beginning 0)))
570 ((looking-at "[ \t]*s\\(ub\\)\\s-")
571 (- (match-beginning 1) (match-beginning 0)))
572 ((looking-at "=head[0-9]") (- (char-before (match-end 0)) ?0))
573 ((looking-at "=cut") 1)
574 (t 3)))
576 (defun perl-current-defun-name ()
577 "The `add-log-current-defun' function in Perl mode."
578 (save-excursion
579 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
580 (match-string-no-properties 1))))
583 ;;; Flymake support
584 (defcustom perl-flymake-command '("perl" "-w" "-c")
585 "External tool used to check Perl source code.
586 This is a non empty list of strings, the checker tool possibly
587 followed by required arguments. Once launched it will receive
588 the Perl source to be checked as its standard input."
589 :version "26.1"
590 :group 'perl
591 :type '(repeat string))
593 (defvar-local perl--flymake-proc nil)
595 ;;;###autoload
596 (defun perl-flymake (report-fn &rest _args)
597 "Perl backend for Flymake. Launches
598 `perl-flymake-command' (which see) and passes to its standard
599 input the contents of the current buffer. The output of this
600 command is analyzed for error and warning messages."
601 (unless (executable-find (car perl-flymake-command))
602 (error "Cannot find a suitable checker"))
604 (when (process-live-p perl--flymake-proc)
605 (kill-process perl--flymake-proc))
607 (let ((source (current-buffer)))
608 (save-restriction
609 (widen)
610 (setq
611 perl--flymake-proc
612 (make-process
613 :name "perl-flymake" :noquery t :connection-type 'pipe
614 :buffer (generate-new-buffer " *perl-flymake*")
615 :command perl-flymake-command
616 :sentinel
617 (lambda (proc _event)
618 (when (eq 'exit (process-status proc))
619 (unwind-protect
620 (if (with-current-buffer source (eq proc perl--flymake-proc))
621 (with-current-buffer (process-buffer proc)
622 (goto-char (point-min))
623 (cl-loop
624 while (search-forward-regexp
625 "^\\(.+\\) at - line \\([0-9]+\\)"
626 nil t)
627 for msg = (match-string 1)
628 for (beg . end) = (flymake-diag-region
629 source
630 (string-to-number (match-string 2)))
631 for type =
632 (if (string-match
633 "\\(Scalar value\\|Useless use\\|Unquoted string\\)"
634 msg)
635 :warning
636 :error)
637 collect (flymake-make-diagnostic source
640 type
641 msg)
642 into diags
643 finally (funcall report-fn diags)))
644 (flymake-log :debug "Canceling obsolete check %s"
645 proc))
646 (kill-buffer (process-buffer proc)))))))
647 (process-send-region perl--flymake-proc (point-min) (point-max))
648 (process-send-eof perl--flymake-proc))))
651 (defvar perl-mode-hook nil
652 "Normal hook to run when entering Perl mode.")
654 ;;;###autoload
655 (define-derived-mode perl-mode prog-mode "Perl"
656 "Major mode for editing Perl code.
657 Expression and list commands understand all Perl brackets.
658 Tab indents for Perl code.
659 Comments are delimited with # ... \\n.
660 Paragraphs are separated by blank lines only.
661 Delete converts tabs to spaces as it moves back.
662 \\{perl-mode-map}
663 Variables controlling indentation style:
664 `perl-tab-always-indent'
665 Non-nil means TAB in Perl mode should always indent the current line,
666 regardless of where in the line point is when the TAB command is used.
667 `perl-tab-to-comment'
668 Non-nil means that for lines which don't need indenting, TAB will
669 either delete an empty comment, indent an existing comment, move
670 to end-of-line, or if at end-of-line already, create a new comment.
671 `perl-nochange'
672 Lines starting with this regular expression are not auto-indented.
673 `perl-indent-level'
674 Indentation of Perl statements within surrounding block.
675 The surrounding block's indentation is the indentation
676 of the line on which the open-brace appears.
677 `perl-continued-statement-offset'
678 Extra indentation given to a substatement, such as the
679 then-clause of an if or body of a while.
680 `perl-continued-brace-offset'
681 Extra indentation given to a brace that starts a substatement.
682 This is in addition to `perl-continued-statement-offset'.
683 `perl-brace-offset'
684 Extra indentation for line if it starts with an open brace.
685 `perl-brace-imaginary-offset'
686 An open brace following other text is treated as if it were
687 this far to the right of the start of its line.
688 `perl-label-offset'
689 Extra indentation for line that is a label.
690 `perl-indent-continued-arguments'
691 Offset of argument lines relative to usual indentation.
693 Various indentation styles: K&R BSD BLK GNU LW
694 perl-indent-level 5 8 0 2 4
695 perl-continued-statement-offset 5 8 4 2 4
696 perl-continued-brace-offset 0 0 0 0 -4
697 perl-brace-offset -5 -8 0 0 0
698 perl-brace-imaginary-offset 0 0 4 0 0
699 perl-label-offset -5 -8 -2 -2 -2
701 Turning on Perl mode runs the normal hook `perl-mode-hook'."
702 :abbrev-table perl-mode-abbrev-table
703 (setq-local paragraph-start (concat "$\\|" page-delimiter))
704 (setq-local paragraph-separate paragraph-start)
705 (setq-local paragraph-ignore-fill-prefix t)
706 (setq-local indent-line-function #'perl-indent-line)
707 (setq-local comment-start "# ")
708 (setq-local comment-end "")
709 (setq-local comment-start-skip "\\(^\\|\\s-\\);?#+ *")
710 (setq-local comment-indent-function #'perl-comment-indent)
711 (setq-local parse-sexp-ignore-comments t)
713 ;; Tell font-lock.el how to handle Perl.
714 (setq font-lock-defaults '((perl-font-lock-keywords
715 perl-font-lock-keywords-1
716 perl-font-lock-keywords-2)
717 nil nil ((?\_ . "w")) nil
718 (font-lock-syntactic-face-function
719 . perl-font-lock-syntactic-face-function)))
720 (setq-local prettify-symbols-alist perl--prettify-symbols-alist)
721 (setq-local syntax-propertize-function #'perl-syntax-propertize-function)
722 (add-hook 'syntax-propertize-extend-region-functions
723 #'syntax-propertize-multiline 'append 'local)
724 ;; Electricity.
725 ;; FIXME: setup electric-layout-rules.
726 (setq-local electric-indent-chars
727 (append '(?\{ ?\} ?\; ?\:) electric-indent-chars))
728 (add-hook 'electric-indent-functions #'perl-electric-noindent-p nil t)
729 ;; Tell imenu how to handle Perl.
730 (setq-local imenu-generic-expression perl-imenu-generic-expression)
731 (setq imenu-case-fold-search nil)
732 ;; Setup outline-minor-mode.
733 (setq-local outline-regexp perl-outline-regexp)
734 (setq-local outline-level 'perl-outline-level)
735 (setq-local add-log-current-defun-function #'perl-current-defun-name)
736 ;; Setup Flymake
737 (add-hook 'flymake-diagnostic-functions #'perl-flymake nil t))
739 ;; This is used by indent-for-comment
740 ;; to decide how much to indent a comment in Perl code
741 ;; based on its context.
742 (defun perl-comment-indent ()
743 (if (and (bolp) (not (eolp)))
744 0 ;Existing comment at bol stays there.
745 comment-column))
747 (define-obsolete-function-alias 'electric-perl-terminator
748 'perl-electric-terminator "22.1")
749 (defun perl-electric-noindent-p (_char)
750 ;; To reproduce the old behavior, ;, {, }, and : are made electric, but
751 ;; we only want them to be electric at EOL.
752 (unless (or (bolp) (eolp)) 'no-indent))
754 (defun perl-electric-terminator (arg)
755 "Insert character and maybe adjust indentation.
756 If at end-of-line, and not in a comment or a quote, correct the indentation."
757 (interactive "P")
758 (let ((insertpos (point)))
759 (and (not arg) ; decide whether to indent
760 (eolp)
761 (save-excursion
762 (beginning-of-line)
763 (and (not ; eliminate comments quickly
764 (and comment-start-skip
765 (re-search-forward comment-start-skip insertpos t)) )
766 (or (/= last-command-event ?:)
767 ;; Colon is special only after a label ....
768 (looking-at "\\s-*\\(\\w\\|\\s_\\)+$"))
769 (let ((pps (parse-partial-sexp
770 (perl-beginning-of-function) insertpos)))
771 (not (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))
772 (progn ; must insert, indent, delete
773 (insert-char last-command-event 1)
774 (perl-indent-line)
775 (delete-char -1))))
776 (self-insert-command (prefix-numeric-value arg)))
777 (make-obsolete 'perl-electric-terminator 'electric-indent-mode "24.4")
779 ;; not used anymore, but may be useful someday:
780 ;;(defun perl-inside-parens-p ()
781 ;; (condition-case ()
782 ;; (save-excursion
783 ;; (save-restriction
784 ;; (narrow-to-region (point)
785 ;; (perl-beginning-of-function))
786 ;; (goto-char (point-max))
787 ;; (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
788 ;; (error nil)))
790 (defun perl-indent-command (&optional arg)
791 "Indent Perl code in the active region or current line.
792 In Transient Mark mode, when the region is active, reindent the region.
793 Otherwise, with a prefix argument, reindent the current line
794 unconditionally.
796 Otherwise, if `perl-tab-always-indent' is nil and point is not in
797 the indentation area at the beginning of the line, insert a tab.
799 Otherwise, indent the current line. If point was within the
800 indentation area, it is moved to the end of the indentation area.
801 If the line was already indented properly and point was not
802 within the indentation area, and if `perl-tab-to-comment' is
803 non-nil (the default), then do the first possible action from the
804 following list:
806 1) delete an empty comment
807 2) move forward to start of comment, indenting if necessary
808 3) move forward to end of line
809 4) create an empty comment
810 5) move backward to start of comment, indenting if necessary."
811 (interactive "P")
812 (cond ((use-region-p) ; indent the active region
813 (indent-region (region-beginning) (region-end)))
814 (arg
815 (perl-indent-line "\f")) ; just indent this line
816 ((and (not perl-tab-always-indent)
817 (> (current-column) (current-indentation)))
818 (insert-tab))
820 (let* ((oldpnt (point))
821 (lsexp (progn (beginning-of-line) (point)))
822 (bof (perl-beginning-of-function))
823 (delta (progn
824 (goto-char oldpnt)
825 (perl-indent-line "\f\\|;?#"))))
826 (and perl-tab-to-comment
827 (= oldpnt (point)) ; done if point moved
828 (if (listp delta) ; if line starts in a quoted string
829 (setq lsexp (or (nth 2 delta) bof))
830 (= delta 0)) ; done if indenting occurred
831 (let ((eol (progn (end-of-line) (point)))
832 state)
833 (cond ((= (char-after bof) ?=)
834 (if (= oldpnt eol)
835 (message "In a format statement")))
836 ((progn (setq state (parse-partial-sexp lsexp eol))
837 (nth 3 state))
838 (if (= oldpnt eol) ; already at eol in a string
839 (message "In a string which starts with a %c."
840 (nth 3 state))))
841 ((not (nth 4 state))
842 (if (= oldpnt eol) ; no comment, create one?
843 (indent-for-comment)))
844 ((progn (beginning-of-line)
845 (and comment-start-skip
846 (re-search-forward
847 comment-start-skip eol 'move)))
848 (if (eolp)
849 (progn ; delete existing comment
850 (goto-char (match-beginning 0))
851 (skip-chars-backward " \t")
852 (delete-region (point) eol))
853 (if (or (< oldpnt (point)) (= oldpnt eol))
854 (indent-for-comment) ; indent existing comment
855 (end-of-line))))
856 ((/= oldpnt eol)
857 (end-of-line))
859 (message "Use backslash to quote # characters.")
860 (ding t)))))))))
861 (make-obsolete 'perl-indent-command 'indent-according-to-mode "24.4")
863 (defun perl-indent-line (&optional nochange)
864 "Indent current line as Perl code.
865 Return the amount the indentation
866 changed by, or (parse-state) if line starts in a quoted string."
867 (let ((case-fold-search nil)
868 (pos (- (point-max) (point)))
869 beg indent shift-amt)
870 (beginning-of-line)
871 (setq beg (point))
872 (setq shift-amt
873 (cond ((eq 1 (nth 7 (syntax-ppss))) 0) ;For doc sections!
874 ((listp (setq indent (perl-calculate-indent))) indent)
875 ((eq 'noindent indent) indent)
876 ((looking-at (or nochange perl-nochange)) 0)
878 (skip-chars-forward " \t\f")
879 (setq indent (perl-indent-new-calculate nil indent))
880 (- indent (current-column)))))
881 (skip-chars-forward " \t\f")
882 (if (and (numberp shift-amt) (/= 0 shift-amt))
883 (progn (delete-region beg (point))
884 (indent-to indent)))
885 ;; If initial point was within line's indentation,
886 ;; position after the indentation. Else stay at same point in text.
887 (if (> (- (point-max) pos) (point))
888 (goto-char (- (point-max) pos)))
889 shift-amt))
891 (defun perl-continuation-line-p ()
892 "Move to end of previous line and return non-nil if continued."
893 ;; Statement level. Is it a continuation or a new statement?
894 ;; Find previous non-comment character.
895 (perl-backward-to-noncomment)
896 ;; Back up over label lines, since they don't
897 ;; affect whether our line is a continuation.
898 (while (and (eq (preceding-char) ?:)
899 (memq (char-syntax (char-after (- (point) 2)))
900 '(?w ?_)))
901 (beginning-of-line)
902 (perl-backward-to-noncomment))
903 ;; Now we get the answer.
904 (unless (memq (preceding-char) '(?\; ?\} ?\{))
905 (preceding-char)))
907 (defun perl-hanging-paren-p ()
908 "Non-nil if we are right after a hanging parenthesis-like char."
909 (and (looking-at "[ \t]*$")
910 (save-excursion
911 (skip-syntax-backward " (") (not (bolp)))))
913 (defun perl-indent-new-calculate (&optional virtual default)
915 (and virtual (save-excursion (skip-chars-backward " \t") (bolp))
916 (current-column))
917 (and (looking-at "\\(\\w\\|\\s_\\)+:[^:]")
918 (max 1 (+ (or default (perl-calculate-indent))
919 perl-label-offset)))
920 (and (= (char-syntax (following-char)) ?\))
921 (save-excursion
922 (forward-char 1)
923 (when (condition-case nil (progn (forward-sexp -1) t)
924 (scan-error nil))
925 (perl-indent-new-calculate 'virtual))))
926 (and (and (= (following-char) ?{)
927 (save-excursion (forward-char) (perl-hanging-paren-p)))
928 (+ (or default (perl-calculate-indent))
929 perl-brace-offset))
930 (or default (perl-calculate-indent))))
932 (defun perl-calculate-indent ()
933 "Return appropriate indentation for current line as Perl code.
934 In usual case returns an integer: the column to indent to.
935 Returns (parse-state) if line starts inside a string."
936 (save-excursion
937 (let ((indent-point (point))
938 (case-fold-search nil)
939 (colon-line-end 0)
940 prev-char
941 state containing-sexp)
942 (setq containing-sexp (nth 1 (syntax-ppss indent-point)))
943 (cond
944 ;; Don't auto-indent in a quoted string or a here-document.
945 ((or (nth 3 state) (eq 2 (nth 7 state))) 'noindent)
946 ((null containing-sexp) ; Line is at top level.
947 (skip-chars-forward " \t\f")
948 (if (memq (following-char)
949 (if perl-indent-parens-as-block '(?\{ ?\( ?\[) '(?\{)))
950 0 ; move to beginning of line if it starts a function body
951 ;; indent a little if this is a continuation line
952 (perl-backward-to-noncomment)
953 (if (or (bobp)
954 (memq (preceding-char) '(?\; ?\})))
955 0 perl-continued-statement-offset)))
956 ((/= (char-after containing-sexp) ?{)
957 ;; line is expression, not statement:
958 ;; indent to just after the surrounding open.
959 (goto-char (1+ containing-sexp))
960 (if (perl-hanging-paren-p)
961 ;; We're indenting an arg of a call like:
962 ;; $a = foobarlongnamefun (
963 ;; arg1
964 ;; arg2
965 ;; );
966 (progn
967 (skip-syntax-backward "(")
968 (condition-case nil
969 (while (save-excursion
970 (skip-syntax-backward " ") (not (bolp)))
971 (forward-sexp -1))
972 (scan-error nil))
973 (+ (current-column) perl-indent-level))
974 (if perl-indent-continued-arguments
975 (+ perl-indent-continued-arguments (current-indentation))
976 (skip-chars-forward " \t")
977 (current-column))))
978 ;; Statement level. Is it a continuation or a new statement?
979 ((setq prev-char (perl-continuation-line-p))
980 ;; This line is continuation of preceding line's statement;
981 ;; indent perl-continued-statement-offset more than the
982 ;; previous line of the statement.
983 (perl-backward-to-start-of-continued-exp)
984 (+ (if (or (save-excursion
985 (perl-continuation-line-p))
986 (and (eq prev-char ?\,)
987 (looking-at "[[:alnum:]_]+[ \t\n]*=>")))
988 ;; If the continued line is itself a continuation
989 ;; line, then align, otherwise add an offset.
990 0 perl-continued-statement-offset)
991 (current-column)
992 (if (save-excursion (goto-char indent-point)
993 (looking-at
994 (if perl-indent-parens-as-block
995 "[ \t]*[{([]" "[ \t]*{")))
996 perl-continued-brace-offset 0)))
998 ;; This line starts a new statement.
999 ;; Position at last unclosed open.
1000 (goto-char containing-sexp)
1002 ;; Is line first statement after an open-brace?
1003 ;; If no, find that first statement and indent like it.
1004 (save-excursion
1005 (forward-char 1)
1006 ;; Skip over comments and labels following openbrace.
1007 (while (progn
1008 (skip-chars-forward " \t\f\n")
1009 (cond ((looking-at ";?#")
1010 (forward-line 1) t)
1011 ((looking-at "\\(\\w\\|\\s_\\)+:[^:]")
1012 (setq colon-line-end (line-end-position))
1013 (search-forward ":")))))
1014 ;; The first following code counts
1015 ;; if it is before the line we want to indent.
1016 (and (< (point) indent-point)
1017 (if (> colon-line-end (point))
1018 (- (current-indentation) perl-label-offset)
1019 (current-column))))
1020 ;; If no previous statement,
1021 ;; indent it relative to line brace is on.
1022 ;; For open paren in column zero, don't let statement
1023 ;; start there too. If perl-indent-level is zero,
1024 ;; use perl-brace-offset + perl-continued-statement-offset
1025 ;; For open-braces not the first thing in a line,
1026 ;; add in perl-brace-imaginary-offset.
1027 (+ (if (and (bolp) (zerop perl-indent-level))
1028 (+ perl-brace-offset perl-continued-statement-offset)
1029 perl-indent-level)
1030 ;; Move back over whitespace before the openbrace.
1031 ;; If openbrace is not first nonwhite thing on the line,
1032 ;; add the perl-brace-imaginary-offset.
1033 (progn (skip-chars-backward " \t")
1034 (if (bolp) 0 perl-brace-imaginary-offset))
1035 ;; If the openbrace is preceded by a parenthesized exp,
1036 ;; move to the beginning of that;
1037 ;; possibly a different line
1038 (progn
1039 (if (eq (preceding-char) ?\))
1040 (forward-sexp -1))
1041 ;; Get initial indentation of the line we are on.
1042 (current-indentation)))))))))
1044 (defun perl-backward-to-noncomment ()
1045 "Move point backward to after the first non-white-space, skipping comments."
1046 (forward-comment (- (point-max))))
1048 (defun perl-backward-to-start-of-continued-exp ()
1049 (while
1050 (let ((c (preceding-char)))
1051 (cond
1052 ((memq c '(?\; ?\{ ?\[ ?\()) (forward-comment (point-max)) nil)
1053 ((memq c '(?\) ?\] ?\} ?\"))
1054 (forward-sexp -1) (forward-comment (- (point))) t)
1055 ((eq ?w (char-syntax c))
1056 (forward-word-strictly -1) (forward-comment (- (point))) t)
1057 (t (forward-char -1) (forward-comment (- (point))) t)))))
1059 ;; note: this may be slower than the c-mode version, but I can understand it.
1060 (defalias 'indent-perl-exp 'perl-indent-exp)
1061 (defun perl-indent-exp ()
1062 "Indent each line of the Perl grouping following point."
1063 (interactive)
1064 (let* ((case-fold-search nil)
1065 (oldpnt (point-marker))
1066 (bof-mark (save-excursion
1067 (end-of-line 2)
1068 (perl-beginning-of-function)
1069 (point-marker)))
1070 eol last-mark lsexp-mark delta)
1071 (if (= (char-after (marker-position bof-mark)) ?=)
1072 (message "Can't indent a format statement")
1073 (message "Indenting Perl expression...")
1074 (setq eol (line-end-position))
1075 (save-excursion ; locate matching close paren
1076 (while (and (not (eobp)) (<= (point) eol))
1077 (parse-partial-sexp (point) (point-max) 0))
1078 (setq last-mark (point-marker)))
1079 (setq lsexp-mark bof-mark)
1080 (beginning-of-line)
1081 (while (< (point) (marker-position last-mark))
1082 (setq delta (perl-indent-line nil))
1083 (if (numberp delta) ; unquoted start-of-line?
1084 (progn
1085 (if (eolp)
1086 (delete-horizontal-space))
1087 (setq lsexp-mark (point-marker))))
1088 (end-of-line)
1089 (setq eol (point))
1090 (if (nth 4 (parse-partial-sexp (marker-position lsexp-mark) eol))
1091 (progn ; line ends in a comment
1092 (beginning-of-line)
1093 (if (or (not (looking-at "\\s-*;?#"))
1094 (listp delta)
1095 (and (/= 0 delta)
1096 (= (- (current-indentation) delta) comment-column)))
1097 (if (and comment-start-skip
1098 (re-search-forward comment-start-skip eol t))
1099 (indent-for-comment))))) ; indent existing comment
1100 (forward-line 1))
1101 (goto-char (marker-position oldpnt))
1102 (message "Indenting Perl expression...done"))))
1104 (defun perl-beginning-of-function (&optional arg)
1105 "Move backward to next beginning-of-function, or as far as possible.
1106 With argument, repeat that many times; negative args move forward.
1107 Returns new value of point in all cases."
1108 (interactive "p")
1109 (or arg (setq arg 1))
1110 (if (< arg 0) (forward-char 1))
1111 (and (/= arg 0)
1112 (re-search-backward
1113 "^\\s(\\|^\\s-*sub\\b[ \t\n]*\\_<[^{]+{\\|^\\s-*format\\b[^=]*=\\|^\\."
1114 nil 'move arg)
1115 (goto-char (1- (match-end 0))))
1116 (point))
1118 ;; note: this routine is adapted directly from emacs lisp.el, end-of-defun;
1119 ;; no bugs have been removed :-)
1120 (defun perl-end-of-function (&optional arg)
1121 "Move forward to next end-of-function.
1122 The end of a function is found by moving forward from the beginning of one.
1123 With argument, repeat that many times; negative args move backward."
1124 (interactive "p")
1125 (or arg (setq arg 1))
1126 (let ((first t))
1127 (while (and (> arg 0) (< (point) (point-max)))
1128 (let ((pos (point)))
1129 (while (progn
1130 (if (and first
1131 (progn
1132 (forward-char 1)
1133 (perl-beginning-of-function 1)
1134 (not (bobp))))
1136 (or (bobp) (forward-char -1))
1137 (perl-beginning-of-function -1))
1138 (setq first nil)
1139 (forward-list 1)
1140 (skip-chars-forward " \t")
1141 (if (looking-at "[#\n]")
1142 (forward-line 1))
1143 (<= (point) pos))))
1144 (setq arg (1- arg)))
1145 (while (< arg 0)
1146 (let ((pos (point)))
1147 (perl-beginning-of-function 1)
1148 (forward-sexp 1)
1149 (forward-line 1)
1150 (if (>= (point) pos)
1151 (if (progn (perl-beginning-of-function 2) (not (bobp)))
1152 (progn
1153 (forward-list 1)
1154 (skip-chars-forward " \t")
1155 (if (looking-at "[#\n]")
1156 (forward-line 1)))
1157 (goto-char (point-min)))))
1158 (setq arg (1+ arg)))))
1160 (defalias 'mark-perl-function 'perl-mark-function)
1161 (defun perl-mark-function ()
1162 "Put mark at end of Perl function, point at beginning."
1163 (interactive)
1164 (push-mark)
1165 (perl-end-of-function)
1166 (push-mark)
1167 (perl-beginning-of-function)
1168 (backward-paragraph))
1170 (provide 'perl-mode)
1172 ;;; perl-mode.el ends here