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