Bug fix in menu code for XEmacs.
[bpt/emacs.git] / lisp / progmodes / perl-mode.el
CommitLineData
4821e2af
ER
1;;; perl-mode.el --- Perl code editing commands for GNU Emacs
2
8f1204db 3;; Copyright (C) 1990, 1994 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
15;; GNU Emacs is free software; you can redistribute it and/or modify
16;; it under the terms of the GNU General Public License as published by
17;; the Free Software Foundation; either version 2, or (at your option)
18;; any later version.
19
20;; GNU Emacs is distributed in the hope that it will be useful,
21;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;; GNU General Public License for more details.
24
25;; You should have received a copy of the GNU General Public License
26;; along with GNU Emacs; see the file COPYING. If not, write to
27;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
2076c87c 28
4821e2af
ER
29;;; Commentary:
30
2076c87c
JB
31;; To enter perl-mode automatically, add (autoload 'perl-mode "perl-mode")
32;; to your .emacs file and change the first line of your perl script to:
33;; #!/usr/bin/perl -- # -*-Perl-*-
34;; With argments to perl:
35;; #!/usr/bin/perl -P- # -*-Perl-*-
36;; To handle files included with do 'filename.pl';, add something like
c6818db9 37;; (setq auto-mode-alist (append (list (cons "\\.pl\\'" 'perl-mode))
2076c87c
JB
38;; auto-mode-alist))
39;; to your .emacs file; otherwise the .pl suffix defaults to prolog-mode.
40
41;; This code is based on the 18.53 version c-mode.el, with extensive
42;; rewriting. Most of the features of c-mode survived intact.
43
44;; I added a new feature which adds functionality to TAB; it is controlled
45;; by the variable perl-tab-to-comment. With it enabled, TAB does the
46;; first thing it can from the following list: change the indentation;
47;; move past leading white space; delete an empty comment; reindent a
48;; comment; move to end of line; create an empty comment; tell you that
49;; the line ends in a quoted string, or has a # which should be a \#.
50
51;; If your machine is slow, you may want to remove some of the bindings
52;; to electric-perl-terminator. I changed the indenting defaults to be
53;; what Larry Wall uses in perl/lib, but left in all the options.
54
55;; I also tuned a few things: comments and labels starting in column
56;; zero are left there by indent-perl-exp; perl-beginning-of-function
57;; goes back to the first open brace/paren in column zero, the open brace
58;; in 'sub ... {', or the equal sign in 'format ... ='; indent-perl-exp
59;; (meta-^q) indents from the current line through the close of the next
60;; brace/paren, so you don't need to start exactly at a brace or paren.
61
62;; It may be good style to put a set of redundant braces around your
63;; main program. This will let you reindent it with meta-^q.
64
282d89c0 65;; Known problems (these are all caused by limitations in the Emacs Lisp
2076c87c
JB
66;; parsing routine (parse-partial-sexp), which was not designed for such
67;; a rich language; writing a more suitable parser would be a big job):
eb8c3be9 68;; 1) Regular expression delimiters do not act as quotes, so special
2076c87c
JB
69;; characters such as `'"#:;[](){} may need to be backslashed
70;; in regular expressions and in both parts of s/// and tr///.
71;; 2) The globbing syntax <pattern> is not recognized, so special
72;; characters in the pattern string must be backslashed.
73;; 3) The q, qq, and << quoting operators are not recognized; see below.
74;; 4) \ (backslash) always quotes the next character, so '\' is
75;; treated as the start of a string. Use "\\" as a work-around.
76;; 5) To make variables such a $' and $#array work, perl-mode treats
77;; $ just like backslash, so '$' is the same as problem 5.
78;; 6) Unfortunately, treating $ like \ makes ${var} be treated as an
79;; unmatched }. See below.
80;; 7) When ' (quote) is used as a package name separator, perl-mode
81;; doesn't understand, and thinks it is seeing a quoted string.
82
83;; Here are some ugly tricks to bypass some of these problems: the perl
84;; expression /`/ (that's a back-tick) usually evaluates harmlessly,
85;; but will trick perl-mode into starting a quoted string, which
86;; can be ended with another /`/. Assuming you have no embedded
87;; back-ticks, this can used to help solve problem 3:
88;;
89;; /`/; $ugly = q?"'$?; /`/;
90;;
91;; To solve problem 6, add a /{/; before each use of ${var}:
92;; /{/; while (<${glob_me}>) ...
93;;
94;; Problem 7 is even worse, but this 'fix' does work :-(
95;; $DB'stop#'
96;; [$DB'line#'
97;; ] =~ s/;9$//;
98
4821e2af 99;;; Code:
2076c87c
JB
100
101(defvar perl-mode-abbrev-table nil
102 "Abbrev table in use in perl-mode buffers.")
103(define-abbrev-table 'perl-mode-abbrev-table ())
104
105(defvar perl-mode-map ()
106 "Keymap used in Perl mode.")
107(if perl-mode-map
108 ()
109 (setq perl-mode-map (make-sparse-keymap))
110 (define-key perl-mode-map "{" 'electric-perl-terminator)
111 (define-key perl-mode-map "}" 'electric-perl-terminator)
112 (define-key perl-mode-map ";" 'electric-perl-terminator)
113 (define-key perl-mode-map ":" 'electric-perl-terminator)
114 (define-key perl-mode-map "\e\C-a" 'perl-beginning-of-function)
115 (define-key perl-mode-map "\e\C-e" 'perl-end-of-function)
116 (define-key perl-mode-map "\e\C-h" 'mark-perl-function)
117 (define-key perl-mode-map "\e\C-q" 'indent-perl-exp)
118 (define-key perl-mode-map "\177" 'backward-delete-char-untabify)
119 (define-key perl-mode-map "\t" 'perl-indent-command))
120
121(autoload 'c-macro-expand "cmacexp"
122 "Display the result of expanding all C macros occurring in the region.
123The expansion is entirely correct because it uses the C preprocessor."
124 t)
125
126(defvar perl-mode-syntax-table nil
127 "Syntax table in use in perl-mode buffers.")
128
129(if perl-mode-syntax-table
130 ()
131 (setq perl-mode-syntax-table (make-syntax-table (standard-syntax-table)))
132 (modify-syntax-entry ?\n ">" perl-mode-syntax-table)
133 (modify-syntax-entry ?# "<" perl-mode-syntax-table)
134 (modify-syntax-entry ?$ "/" perl-mode-syntax-table)
135 (modify-syntax-entry ?% "." perl-mode-syntax-table)
136 (modify-syntax-entry ?& "." perl-mode-syntax-table)
137 (modify-syntax-entry ?\' "\"" perl-mode-syntax-table)
138 (modify-syntax-entry ?* "." perl-mode-syntax-table)
139 (modify-syntax-entry ?+ "." perl-mode-syntax-table)
140 (modify-syntax-entry ?- "." perl-mode-syntax-table)
141 (modify-syntax-entry ?/ "." perl-mode-syntax-table)
142 (modify-syntax-entry ?< "." perl-mode-syntax-table)
143 (modify-syntax-entry ?= "." perl-mode-syntax-table)
144 (modify-syntax-entry ?> "." perl-mode-syntax-table)
145 (modify-syntax-entry ?\\ "\\" perl-mode-syntax-table)
146 (modify-syntax-entry ?` "\"" perl-mode-syntax-table)
147 (modify-syntax-entry ?| "." perl-mode-syntax-table)
148)
149
9e551477
RS
150(defvar perl-font-lock-keywords
151 (list
152; ("if" "until" "while" "elsif" "else" "unless" "for" "foreach" "continue"
153; "exit" "die" "last" "goto" "next" "redo" "return" "local" "exec")
154 (concat "\\<\\("
155 "continue\\|die\\|e\\(ls\\(e\\|if\\)\\|x\\(ec\\|it\\)\\)\\|"
156 "for\\(\\|each\\)\\|goto\\|if\\|l\\(ast\\|ocal\\)\\|next\\|"
157 "re\\(do\\|turn\\)\\|un\\(less\\|til\\)\\|while"
158 "\\)\\>")
159; ("#endif" "#else" "#ifdef" "#ifndef" "#if" "#include" "#define" "#undef")
160 (cons (concat "#\\(define\\|e\\(lse\\|ndif\\)\\|"
161 "i\\(f\\(\\|def\\|ndef\\)\\|nclude\\)\\|undef\\)\\>")
162 'font-lock-reference-face)
163 '("^[ \n\t]*sub[ \t]+\\([^ \t{]+\\)[ \t]*[{]" 1 font-lock-function-name-face)
164 '("[ \n\t{]*\\(eval\\)[ \n\t(;]" 1 font-lock-function-name-face)
165 '("\\(--- .* ---\\|=== .* ===\\)" . font-lock-string-face)
166 )
167 "Additional expressions to highlight in Perl mode.")
168
10e6ca88 169(defvar perl-indent-level 4
2076c87c 170 "*Indentation of Perl statements with respect to containing block.")
10e6ca88 171(defvar perl-continued-statement-offset 4
2076c87c 172 "*Extra indent for lines not starting new statements.")
10e6ca88 173(defvar perl-continued-brace-offset -4
2076c87c 174 "*Extra indent for substatements that start with open-braces.
10e6ca88
RS
175This is in addition to `perl-continued-statement-offset'.")
176(defvar perl-brace-offset 0
2076c87c 177 "*Extra indentation for braces, compared with other text in same context.")
10e6ca88 178(defvar perl-brace-imaginary-offset 0
2076c87c 179 "*Imagined indentation of an open brace that actually follows a statement.")
10e6ca88 180(defvar perl-label-offset -2
2076c87c
JB
181 "*Offset of Perl label lines relative to usual indentation.")
182
10e6ca88
RS
183(defvar perl-tab-always-indent t
184 "*Non-nil means TAB in Perl mode always indents the current line.
185Otherwise it inserts a tab character if you type it past the first
186nonwhite character on the line.")
2076c87c 187
15cb2300
RS
188;; I changed the default to nil for consistency with general Emacs
189;; conventions -- rms.
190(defvar perl-tab-to-comment nil
10e6ca88
RS
191 "*Non-nil means TAB moves to eol or makes a comment in some cases.
192For lines which don't need indenting, TAB either indents an
193existing comment, moves to end-of-line, or if at end-of-line already,
194create a new comment.")
2076c87c 195
10e6ca88
RS
196(defvar perl-nochange ";?#\\|\f\\|\\s(\\|\\(\\w\\|\\s_\\)+:"
197 "*Lines starting with this regular expression are not auto-indented.")
2076c87c 198\f
97c48db5 199;;;###autoload
2076c87c
JB
200(defun perl-mode ()
201 "Major mode for editing Perl code.
202Expression and list commands understand all Perl brackets.
203Tab indents for Perl code.
204Comments are delimited with # ... \\n.
205Paragraphs are separated by blank lines only.
206Delete converts tabs to spaces as it moves back.
207\\{perl-mode-map}
208Variables controlling indentation style:
209 perl-tab-always-indent
210 Non-nil means TAB in Perl mode should always indent the current line,
211 regardless of where in the line point is when the TAB command is used.
212 perl-tab-to-comment
213 Non-nil means that for lines which don't need indenting, TAB will
214 either delete an empty comment, indent an existing comment, move
215 to end-of-line, or if at end-of-line already, create a new comment.
216 perl-nochange
10e6ca88 217 Lines starting with this regular expression are not auto-indented.
2076c87c
JB
218 perl-indent-level
219 Indentation of Perl statements within surrounding block.
220 The surrounding block's indentation is the indentation
221 of the line on which the open-brace appears.
222 perl-continued-statement-offset
223 Extra indentation given to a substatement, such as the
224 then-clause of an if or body of a while.
225 perl-continued-brace-offset
226 Extra indentation given to a brace that starts a substatement.
10e6ca88 227 This is in addition to `perl-continued-statement-offset'.
2076c87c
JB
228 perl-brace-offset
229 Extra indentation for line if it starts with an open brace.
230 perl-brace-imaginary-offset
231 An open brace following other text is treated as if it were
232 this far to the right of the start of its line.
233 perl-label-offset
234 Extra indentation for line that is a label.
235
236Various indentation styles: K&R BSD BLK GNU LW
237 perl-indent-level 5 8 0 2 4
238 perl-continued-statement-offset 5 8 4 2 4
239 perl-continued-brace-offset 0 0 0 0 -4
240 perl-brace-offset -5 -8 0 0 0
241 perl-brace-imaginary-offset 0 0 4 0 0
242 perl-label-offset -5 -8 -2 -2 -2
243
10e6ca88 244Turning on Perl mode runs the normal hook `perl-mode-hook'."
2076c87c
JB
245 (interactive)
246 (kill-all-local-variables)
247 (use-local-map perl-mode-map)
248 (setq major-mode 'perl-mode)
249 (setq mode-name "Perl")
250 (setq local-abbrev-table perl-mode-abbrev-table)
251 (set-syntax-table perl-mode-syntax-table)
252 (make-local-variable 'paragraph-start)
edae0c55 253 (setq paragraph-start (concat "$\\|" page-delimiter))
2076c87c
JB
254 (make-local-variable 'paragraph-separate)
255 (setq paragraph-separate paragraph-start)
256 (make-local-variable 'paragraph-ignore-fill-prefix)
257 (setq paragraph-ignore-fill-prefix t)
258 (make-local-variable 'indent-line-function)
259 (setq indent-line-function 'perl-indent-line)
260 (make-local-variable 'require-final-newline)
261 (setq require-final-newline t)
262 (make-local-variable 'comment-start)
263 (setq comment-start "# ")
264 (make-local-variable 'comment-end)
265 (setq comment-end "")
266 (make-local-variable 'comment-column)
267 (setq comment-column 32)
268 (make-local-variable 'comment-start-skip)
269 (setq comment-start-skip "\\(^\\|\\s-\\);?#+ *")
e41b2db1
ER
270 (make-local-variable 'comment-indent-function)
271 (setq comment-indent-function 'perl-comment-indent)
2076c87c 272 (make-local-variable 'parse-sexp-ignore-comments)
7ac7f4c2 273 (setq parse-sexp-ignore-comments t)
cad69fcd
SM
274 (make-local-variable 'font-lock-defaults)
275 (setq font-lock-defaults '(perl-font-lock-keywords))
2076c87c
JB
276 (run-hooks 'perl-mode-hook))
277\f
278;; This is used by indent-for-comment
279;; to decide how much to indent a comment in Perl code
280;; based on its context.
281(defun perl-comment-indent ()
282 (if (and (bolp) (not (eolp)))
283 0 ;Existing comment at bol stays there.
284 (save-excursion
285 (skip-chars-backward " \t")
012733b3
RS
286 (max (if (bolp) ;Else indent at comment column
287 0 ; except leave at least one space if
288 (1+ (current-column))) ; not at beginning of line.
289 comment-column))))
2076c87c
JB
290
291(defun electric-perl-terminator (arg)
10e6ca88
RS
292 "Insert character and adjust indentation.
293If at end-of-line, and not in a comment or a quote, correct the's indentation."
2076c87c
JB
294 (interactive "P")
295 (let ((insertpos (point)))
296 (and (not arg) ; decide whether to indent
297 (eolp)
298 (save-excursion
299 (beginning-of-line)
300 (and (not ; eliminate comments quickly
301 (re-search-forward comment-start-skip insertpos t))
302 (or (/= last-command-char ?:)
303 ;; Colon is special only after a label ....
304 (looking-at "\\s-*\\(\\w\\|\\s_\\)+$"))
305 (let ((pps (parse-partial-sexp
306 (perl-beginning-of-function) insertpos)))
307 (not (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))
308 (progn ; must insert, indent, delete
309 (insert-char last-command-char 1)
310 (perl-indent-line)
311 (delete-char -1))))
312 (self-insert-command (prefix-numeric-value arg)))
313
314;; not used anymore, but may be useful someday:
315;;(defun perl-inside-parens-p ()
316;; (condition-case ()
317;; (save-excursion
318;; (save-restriction
319;; (narrow-to-region (point)
320;; (perl-beginning-of-function))
321;; (goto-char (point-max))
322;; (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
323;; (error nil)))
324\f
325(defun perl-indent-command (&optional arg)
326 "Indent current line as Perl code, or optionally, insert a tab character.
327
328With an argument, indent the current line, regardless of other options.
329
10e6ca88 330If `perl-tab-always-indent' is nil and point is not in the indentation
2076c87c
JB
331area at the beginning of the line, simply insert a tab.
332
333Otherwise, indent the current line. If point was within the indentation
334area it is moved to the end of the indentation area. If the line was
335already indented properly and point was not within the indentation area,
10e6ca88 336and if `perl-tab-to-comment' is non-nil (the default), then do the first
2076c87c
JB
337possible action from the following list:
338
339 1) delete an empty comment
340 2) move forward to start of comment, indenting if necessary
341 3) move forward to end of line
342 4) create an empty comment
343 5) move backward to start of comment, indenting if necessary."
344 (interactive "P")
345 (if arg ; If arg, just indent this line
346 (perl-indent-line "\f")
347 (if (and (not perl-tab-always-indent)
c1033e00 348 (> (current-column) (current-indentation)))
2076c87c
JB
349 (insert-tab)
350 (let (bof lsexp delta (oldpnt (point)))
351 (beginning-of-line)
352 (setq lsexp (point))
353 (setq bof (perl-beginning-of-function))
354 (goto-char oldpnt)
355 (setq delta (perl-indent-line "\f\\|;?#" bof))
356 (and perl-tab-to-comment
357 (= oldpnt (point)) ; done if point moved
358 (if (listp delta) ; if line starts in a quoted string
359 (setq lsexp (or (nth 2 delta) bof))
360 (= delta 0)) ; done if indenting occurred
361 (let (eol state)
362 (end-of-line)
363 (setq eol (point))
364 (if (= (char-after bof) ?=)
365 (if (= oldpnt eol)
366 (message "In a format statement"))
367 (setq state (parse-partial-sexp lsexp eol))
368 (if (nth 3 state)
369 (if (= oldpnt eol) ; already at eol in a string
370 (message "In a string which starts with a %c."
371 (nth 3 state)))
372 (if (not (nth 4 state))
373 (if (= oldpnt eol) ; no comment, create one?
374 (indent-for-comment))
375 (beginning-of-line)
376 (if (re-search-forward comment-start-skip eol 'move)
377 (if (eolp)
378 (progn ; kill existing comment
379 (goto-char (match-beginning 0))
380 (skip-chars-backward " \t")
381 (kill-region (point) eol))
382 (if (or (< oldpnt (point)) (= oldpnt eol))
383 (indent-for-comment) ; indent existing comment
384 (end-of-line)))
385 (if (/= oldpnt eol)
386 (end-of-line)
387 (message "Use backslash to quote # characters.")
388 (ding t))))))))))))
389
390(defun perl-indent-line (&optional nochange parse-start)
10e6ca88
RS
391 "Indent current line as Perl code.
392Return the amount the indentation
2076c87c
JB
393changed by, or (parse-state) if line starts in a quoted string."
394 (let ((case-fold-search nil)
395 (pos (- (point-max) (point)))
396 (bof (or parse-start (save-excursion (perl-beginning-of-function))))
397 beg indent shift-amt)
398 (beginning-of-line)
399 (setq beg (point))
400 (setq shift-amt
401 (cond ((= (char-after bof) ?=) 0)
402 ((listp (setq indent (calculate-perl-indent bof))) indent)
403 ((looking-at (or nochange perl-nochange)) 0)
404 (t
405 (skip-chars-forward " \t\f")
406 (cond ((looking-at "\\(\\w\\|\\s_\\)+:")
407 (setq indent (max 1 (+ indent perl-label-offset))))
408 ((= (following-char) ?})
409 (setq indent (- indent perl-indent-level)))
410 ((= (following-char) ?{)
411 (setq indent (+ indent perl-brace-offset))))
412 (- indent (current-column)))))
413 (skip-chars-forward " \t\f")
414 (if (and (numberp shift-amt) (/= 0 shift-amt))
415 (progn (delete-region beg (point))
416 (indent-to indent)))
417 ;; If initial point was within line's indentation,
418 ;; position after the indentation. Else stay at same point in text.
419 (if (> (- (point-max) pos) (point))
420 (goto-char (- (point-max) pos)))
421 shift-amt))
422
423(defun calculate-perl-indent (&optional parse-start)
424 "Return appropriate indentation for current line as Perl code.
425In usual case returns an integer: the column to indent to.
426Returns (parse-state) if line starts inside a string."
427 (save-excursion
428 (beginning-of-line)
429 (let ((indent-point (point))
430 (case-fold-search nil)
431 (colon-line-end 0)
432 state containing-sexp)
433 (if parse-start ;used to avoid searching
434 (goto-char parse-start)
435 (perl-beginning-of-function))
436 (while (< (point) indent-point) ;repeat until right sexp
437 (setq parse-start (point))
438 (setq state (parse-partial-sexp (point) indent-point 0))
439; state = (depth_in_parens innermost_containing_list last_complete_sexp
440; string_terminator_or_nil inside_commentp following_quotep
441; minimum_paren-depth_this_scan)
442; Parsing stops if depth in parentheses becomes equal to third arg.
443 (setq containing-sexp (nth 1 state)))
444 (cond ((nth 3 state) state) ; In a quoted string?
445 ((null containing-sexp) ; Line is at top level.
446 (skip-chars-forward " \t\f")
447 (if (= (following-char) ?{)
448 0 ; move to beginning of line if it starts a function body
449 ;; indent a little if this is a continuation line
450 (perl-backward-to-noncomment)
451 (if (or (bobp)
452 (memq (preceding-char) '(?\; ?\})))
453 0 perl-continued-statement-offset)))
454 ((/= (char-after containing-sexp) ?{)
455 ;; line is expression, not statement:
456 ;; indent to just after the surrounding open.
457 (goto-char (1+ containing-sexp))
458 (current-column))
459 (t
460 ;; Statement level. Is it a continuation or a new statement?
461 ;; Find previous non-comment character.
462 (perl-backward-to-noncomment)
463 ;; Back up over label lines, since they don't
464 ;; affect whether our line is a continuation.
465 (while (or (eq (preceding-char) ?\,)
466 (and (eq (preceding-char) ?:)
467 (memq (char-syntax (char-after (- (point) 2)))
468 '(?w ?_))))
469 (if (eq (preceding-char) ?\,)
8de214e6
RS
470 (perl-backward-to-start-of-continued-exp containing-sexp)
471 (beginning-of-line))
2076c87c
JB
472 (perl-backward-to-noncomment))
473 ;; Now we get the answer.
474 (if (not (memq (preceding-char) '(?\; ?\} ?\{)))
475 ;; This line is continuation of preceding line's statement;
476 ;; indent perl-continued-statement-offset more than the
477 ;; previous line of the statement.
478 (progn
479 (perl-backward-to-start-of-continued-exp containing-sexp)
480 (+ perl-continued-statement-offset (current-column)
481 (if (save-excursion (goto-char indent-point)
482 (looking-at "[ \t]*{"))
483 perl-continued-brace-offset 0)))
484 ;; This line starts a new statement.
485 ;; Position at last unclosed open.
486 (goto-char containing-sexp)
487 (or
488 ;; If open paren is in col 0, close brace is special
489 (and (bolp)
490 (save-excursion (goto-char indent-point)
491 (looking-at "[ \t]*}"))
492 perl-indent-level)
493 ;; Is line first statement after an open-brace?
494 ;; If no, find that first statement and indent like it.
495 (save-excursion
496 (forward-char 1)
497 ;; Skip over comments and labels following openbrace.
498 (while (progn
499 (skip-chars-forward " \t\f\n")
500 (cond ((looking-at ";?#")
501 (forward-line 1) t)
502 ((looking-at "\\(\\w\\|\\s_\\)+:")
503 (save-excursion
504 (end-of-line)
505 (setq colon-line-end (point)))
506 (search-forward ":")))))
507 ;; The first following code counts
508 ;; if it is before the line we want to indent.
509 (and (< (point) indent-point)
510 (if (> colon-line-end (point))
511 (- (current-indentation) perl-label-offset)
512 (current-column))))
513 ;; If no previous statement,
514 ;; indent it relative to line brace is on.
515 ;; For open paren in column zero, don't let statement
516 ;; start there too. If perl-indent-level is zero,
517 ;; use perl-brace-offset + perl-continued-statement-offset
518 ;; For open-braces not the first thing in a line,
519 ;; add in perl-brace-imaginary-offset.
520 (+ (if (and (bolp) (zerop perl-indent-level))
521 (+ perl-brace-offset perl-continued-statement-offset)
522 perl-indent-level)
523 ;; Move back over whitespace before the openbrace.
524 ;; If openbrace is not first nonwhite thing on the line,
525 ;; add the perl-brace-imaginary-offset.
526 (progn (skip-chars-backward " \t")
527 (if (bolp) 0 perl-brace-imaginary-offset))
528 ;; If the openbrace is preceded by a parenthesized exp,
529 ;; move to the beginning of that;
530 ;; possibly a different line
531 (progn
532 (if (eq (preceding-char) ?\))
533 (forward-sexp -1))
534 ;; Get initial indentation of the line we are on.
535 (current-indentation))))))))))
536
537(defun perl-backward-to-noncomment ()
538 "Move point backward to after the first non-white-space, skipping comments."
539 (interactive)
540 (let (opoint stop)
541 (while (not stop)
542 (setq opoint (point))
543 (beginning-of-line)
544 (if (re-search-forward comment-start-skip opoint 'move 1)
545 (progn (goto-char (match-end 1))
546 (skip-chars-forward ";")))
547 (skip-chars-backward " \t\f")
548 (setq stop (or (bobp)
549 (not (bolp))
550 (forward-char -1))))))
551
552(defun perl-backward-to-start-of-continued-exp (lim)
553 (if (= (preceding-char) ?\))
554 (forward-sexp -1))
555 (beginning-of-line)
556 (if (<= (point) lim)
557 (goto-char (1+ lim)))
558 (skip-chars-forward " \t\f"))
559\f
560;; note: this may be slower than the c-mode version, but I can understand it.
561(defun indent-perl-exp ()
562 "Indent each line of the Perl grouping following point."
563 (interactive)
564 (let* ((case-fold-search nil)
565 (oldpnt (point-marker))
566 (bof-mark (save-excursion
567 (end-of-line 2)
568 (perl-beginning-of-function)
569 (point-marker)))
570 eol last-mark lsexp-mark delta)
571 (if (= (char-after (marker-position bof-mark)) ?=)
572 (message "Can't indent a format statement")
573 (message "Indenting Perl expression...")
574 (save-excursion (end-of-line) (setq eol (point)))
575 (save-excursion ; locate matching close paren
576 (while (and (not (eobp)) (<= (point) eol))
577 (parse-partial-sexp (point) (point-max) 0))
578 (setq last-mark (point-marker)))
579 (setq lsexp-mark bof-mark)
580 (beginning-of-line)
581 (while (< (point) (marker-position last-mark))
582 (setq delta (perl-indent-line nil (marker-position bof-mark)))
583 (if (numberp delta) ; unquoted start-of-line?
584 (progn
585 (if (eolp)
586 (delete-horizontal-space))
587 (setq lsexp-mark (point-marker))))
588 (end-of-line)
589 (setq eol (point))
590 (if (nth 4 (parse-partial-sexp (marker-position lsexp-mark) eol))
591 (progn ; line ends in a comment
592 (beginning-of-line)
593 (if (or (not (looking-at "\\s-*;?#"))
594 (listp delta)
595 (and (/= 0 delta)
596 (= (- (current-indentation) delta) comment-column)))
597 (if (re-search-forward comment-start-skip eol t)
598 (indent-for-comment))))) ; indent existing comment
599 (forward-line 1))
600 (goto-char (marker-position oldpnt))
601 (message "Indenting Perl expression...done"))))
602\f
603(defun perl-beginning-of-function (&optional arg)
604 "Move backward to next beginning-of-function, or as far as possible.
605With argument, repeat that many times; negative args move forward.
606Returns new value of point in all cases."
607 (interactive "p")
608 (or arg (setq arg 1))
609 (if (< arg 0) (forward-char 1))
610 (and (/= arg 0)
611 (re-search-backward "^\\s(\\|^\\s-*sub\\b[^{]+{\\|^\\s-*format\\b[^=]*=\\|^\\."
612 nil 'move arg)
613 (goto-char (1- (match-end 0))))
614 (point))
615
616;; note: this routine is adapted directly from emacs lisp.el, end-of-defun;
617;; no bugs have been removed :-)
618(defun perl-end-of-function (&optional arg)
619 "Move forward to next end-of-function.
620The end of a function is found by moving forward from the beginning of one.
621With argument, repeat that many times; negative args move backward."
622 (interactive "p")
623 (or arg (setq arg 1))
624 (let ((first t))
625 (while (and (> arg 0) (< (point) (point-max)))
626 (let ((pos (point)) npos)
627 (while (progn
628 (if (and first
629 (progn
630 (forward-char 1)
631 (perl-beginning-of-function 1)
632 (not (bobp))))
633 nil
634 (or (bobp) (forward-char -1))
635 (perl-beginning-of-function -1))
636 (setq first nil)
637 (forward-list 1)
638 (skip-chars-forward " \t")
639 (if (looking-at "[#\n]")
640 (forward-line 1))
641 (<= (point) pos))))
642 (setq arg (1- arg)))
643 (while (< arg 0)
644 (let ((pos (point)))
645 (perl-beginning-of-function 1)
646 (forward-sexp 1)
647 (forward-line 1)
648 (if (>= (point) pos)
649 (if (progn (perl-beginning-of-function 2) (not (bobp)))
650 (progn
651 (forward-list 1)
652 (skip-chars-forward " \t")
653 (if (looking-at "[#\n]")
654 (forward-line 1)))
655 (goto-char (point-min)))))
656 (setq arg (1+ arg)))))
657
658(defun mark-perl-function ()
659 "Put mark at end of Perl function, point at beginning."
660 (interactive)
661 (push-mark (point))
662 (perl-end-of-function)
663 (push-mark (point))
664 (perl-beginning-of-function)
665 (backward-paragraph))
666
667;;;;;;;; That's all, folks! ;;;;;;;;;