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