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