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