(tex-font-lock-keywords-2): Fix bug in \bf fontification.
[emacs.git] / lisp / progmodes / perl-mode.el
blobe1af8b0f00738abaa346406203d350ed32ddccc9
1 ;;; perl-mode.el --- Perl code editing commands for GNU Emacs
3 ;; Copyright (C) 1990, 1994, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Author: William F. Mann
7 ;; Maintainer: FSF
8 ;; Adapted-By: ESR
9 ;; Keywords: languages
11 ;; Adapted from C code editing commands 'c-mode.el', Copyright 1987 by the
12 ;; Free Software Foundation, under terms of its General Public License.
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
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 arguments 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 perl-electric-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 perl-indent-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 ... ='; perl-indent-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 ;; 2) The globbing syntax <pattern> is not recognized, so special
71 ;; characters in the pattern string must be backslashed.
72 ;; 3) The << quoting operators are not recognized; see below.
73 ;; 5) To make '$' work correctly, $' is not recognized as a variable.
74 ;; Use "$'" or $POSTMATCH instead.
76 ;; If you don't use font-lock, additional problems will appear:
77 ;; 1) Regular expression delimiters do not act as quotes, so special
78 ;; characters such as `'"#:;[](){} may need to be backslashed
79 ;; in regular expressions and in both parts of s/// and tr///.
80 ;; 4) The q and qq quoting operators are not recognized; see below.
81 ;; 5) To make variables such a $' and $#array work, perl-mode treats
82 ;; $ just like backslash, so '$' is not treated correctly.
83 ;; 6) Unfortunately, treating $ like \ makes ${var} be treated as an
84 ;; unmatched }. See below.
85 ;; 7) When ' (quote) is used as a package name separator, perl-mode
86 ;; doesn't understand, and thinks it is seeing a quoted string.
88 ;; Here are some ugly tricks to bypass some of these problems: the perl
89 ;; expression /`/ (that's a back-tick) usually evaluates harmlessly,
90 ;; but will trick perl-mode into starting a quoted string, which
91 ;; can be ended with another /`/. Assuming you have no embedded
92 ;; back-ticks, this can used to help solve problem 3:
94 ;; /`/; $ugly = q?"'$?; /`/;
96 ;; The same trick can be used for problem 6 as in:
97 ;; /{/; while (<${glob_me}>)
98 ;; but a simpler solution is to add a space between the $ and the {:
99 ;; while (<$ {glob_me}>)
101 ;; Problem 7 is even worse, but this 'fix' does work :-(
102 ;; $DB'stop#'
103 ;; [$DB'line#'
104 ;; ] =~ s/;9$//;
106 ;;; Code:
108 (eval-when-compile (require 'cl))
110 (defvar font-lock-comment-face)
111 (defvar font-lock-doc-face)
112 (defvar font-lock-string-face)
114 (defgroup perl nil
115 "Major mode for editing Perl code."
116 :prefix "perl-"
117 :group 'languages)
119 (defvar perl-mode-abbrev-table nil
120 "Abbrev table in use in perl-mode buffers.")
121 (define-abbrev-table 'perl-mode-abbrev-table ())
123 (defvar perl-mode-map
124 (let ((map (make-sparse-keymap)))
125 (define-key map "{" 'perl-electric-terminator)
126 (define-key map "}" 'perl-electric-terminator)
127 (define-key map ";" 'perl-electric-terminator)
128 (define-key map ":" 'perl-electric-terminator)
129 (define-key map "\e\C-a" 'perl-beginning-of-function)
130 (define-key map "\e\C-e" 'perl-end-of-function)
131 (define-key map "\e\C-h" 'perl-mark-function)
132 (define-key map "\e\C-q" 'perl-indent-exp)
133 (define-key map "\177" 'backward-delete-char-untabify)
134 (define-key map "\t" 'perl-indent-command)
135 map)
136 "Keymap used in Perl mode.")
138 (autoload 'c-macro-expand "cmacexp"
139 "Display the result of expanding all C macros occurring in the region.
140 The expansion is entirely correct because it uses the C preprocessor."
143 (defvar perl-mode-syntax-table
144 (let ((st (make-syntax-table (standard-syntax-table))))
145 (modify-syntax-entry ?\n ">" st)
146 (modify-syntax-entry ?# "<" st)
147 ;; `$' is also a prefix char so I was tempted to say "/ p",
148 ;; but the `p' thingy basically overrides the `/' :-( --stef
149 (modify-syntax-entry ?$ "/" st)
150 (modify-syntax-entry ?% ". p" st)
151 (modify-syntax-entry ?@ ". p" st)
152 (modify-syntax-entry ?& "." st)
153 (modify-syntax-entry ?\' "\"" st)
154 (modify-syntax-entry ?* "." st)
155 (modify-syntax-entry ?+ "." st)
156 (modify-syntax-entry ?- "." st)
157 (modify-syntax-entry ?/ "." st)
158 (modify-syntax-entry ?< "." st)
159 (modify-syntax-entry ?= "." st)
160 (modify-syntax-entry ?> "." st)
161 (modify-syntax-entry ?\\ "\\" st)
162 (modify-syntax-entry ?` "\"" st)
163 (modify-syntax-entry ?| "." st)
165 "Syntax table in use in `perl-mode' buffers.")
167 (defvar perl-imenu-generic-expression
168 '(;; Functions
169 (nil "^sub\\s-+\\([-A-Za-z0-9+_:]+\\)" 1)
170 ;;Variables
171 ("Variables" "^\\([$@%][-A-Za-z0-9+_:]+\\)\\s-*=" 1)
172 ("Packages" "^package\\s-+\\([-A-Za-z0-9+_:]+\\);" 1)
173 ("Doc sections" "^=head[0-9][ \t]+\\(.*\\)" 1))
174 "Imenu generic expression for Perl mode. See `imenu-generic-expression'.")
176 ;; Regexps updated with help from Tom Tromey <tromey@cambric.colorado.edu> and
177 ;; Jim Campbell <jec@murzim.ca.boeing.com>.
179 (defconst perl-font-lock-keywords-1
180 '(;; What is this for?
181 ;;("\\(--- .* ---\\|=== .* ===\\)" . font-lock-string-face)
183 ;; Fontify preprocessor statements as we do in `c-font-lock-keywords'.
184 ;; Ilya Zakharevich <ilya@math.ohio-state.edu> thinks this is a bad idea.
185 ;; ("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
186 ;; ("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
187 ;; ("^#[ \t]*if\\>"
188 ;; ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
189 ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t)))
190 ;; ("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
191 ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t))
193 ;; Fontify function and package names in declarations.
194 ("\\<\\(package\\|sub\\)\\>[ \t]*\\(\\sw+\\)?"
195 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
196 ("\\<\\(import\\|no\\|require\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
197 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t)))
198 "Subdued level highlighting for Perl mode.")
200 (defconst perl-font-lock-keywords-2
201 (append perl-font-lock-keywords-1
202 (list
204 ;; Fontify keywords, except those fontified otherwise.
205 (concat "\\<"
206 (regexp-opt '("if" "until" "while" "elsif" "else" "unless"
207 "do" "dump" "for" "foreach" "exit" "die"
208 "BEGIN" "END" "return" "exec" "eval") t)
209 "\\>")
211 ;; Fontify local and my keywords as types.
212 '("\\<\\(local\\|my\\)\\>" . font-lock-type-face)
214 ;; Fontify function, variable and file name references.
215 '("&\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-function-name-face)
216 ;; Additionally underline non-scalar variables. Maybe this is a bad idea.
217 ;;'("[$@%*][#{]?\\(\\sw+\\)" 1 font-lock-variable-name-face)
218 '("[$*]{?\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-variable-name-face)
219 '("\\([@%]\\|\\$#\\)\\(\\sw+\\(::\\sw+\\)*\\)"
220 (2 (cons font-lock-variable-name-face '(underline))))
221 '("<\\(\\sw+\\)>" 1 font-lock-constant-face)
223 ;; Fontify keywords with/and labels as we do in `c++-font-lock-keywords'.
224 '("\\<\\(continue\\|goto\\|last\\|next\\|redo\\)\\>[ \t]*\\(\\sw+\\)?"
225 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
226 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-constant-face)))
227 "Gaudy level highlighting for Perl mode.")
229 (defvar perl-font-lock-keywords perl-font-lock-keywords-1
230 "Default expressions to highlight in Perl mode.")
232 (defvar perl-quote-like-pairs
233 '((?\( . ?\)) (?\[ . ?\]) (?\{ . ?\}) (?\< . ?\>)))
235 ;; FIXME: handle here-docs and regexps.
236 ;; <<EOF <<"EOF" <<'EOF' (no space)
237 ;; see `man perlop'
238 ;; ?...?
239 ;; /.../
240 ;; m [...]
241 ;; m /.../
242 ;; q /.../ = '...'
243 ;; qq /.../ = "..."
244 ;; qx /.../ = `...`
245 ;; qr /.../ = precompiled regexp =~=~ m/.../
246 ;; qw /.../
247 ;; s /.../.../
248 ;; s <...> /.../
249 ;; s '...'...'
250 ;; tr /.../.../
251 ;; y /.../.../
253 ;; <file*glob>
254 (defvar perl-font-lock-syntactic-keywords
255 ;; Turn POD into b-style comments
256 '(("^\\(=\\)\\sw" (1 "< b"))
257 ("^=cut[ \t]*\\(\n\\)" (1 "> b"))
258 ;; Catch ${ so that ${var} doesn't screw up indentation.
259 ;; This also catches $' to handle 'foo$', although it should really
260 ;; check that it occurs inside a '..' string.
261 ("\\(\\$\\)[{']" (1 ". p"))
262 ;; Handle funny names like $DB'stop.
263 ("\\$ ?{?^?[_a-zA-Z][_a-zA-Z0-9]*\\('\\)[_a-zA-Z]" (1 "_"))
264 ;; format statements
265 ("^[ \t]*format.*=[ \t]*\\(\n\\)" (1 '(7)))
266 ;; Funny things in sub arg specifications like `sub myfunc ($$)'
267 ("\\<sub\\s-+\\S-+\\s-*(\\([^)]+\\))" 1 '(1))
268 ;; regexp and funny quotes
269 ("[?:.,;=!~({[][ \t\n]*\\(/\\)" (1 '(7)))
270 ("\\(^\\|[?:.,;=!~({[ \t]\\)\\([msy]\\|q[qxrw]?\\|tr\\)\\>\\s-*\\([^])}> \n\t]\\)"
271 ;; Nasty cases:
272 ;; /foo/m $a->m $#m $m @m %m
273 ;; \s (appears often in regexps).
274 ;; -s file
275 (3 (if (assoc (char-after (match-beginning 3))
276 perl-quote-like-pairs)
277 '(15) '(7))))
278 ;; TODO: here-documents ("<<\\(\\sw\\|['\"]\\)")
281 (defvar perl-empty-syntax-table
282 (let ((st (copy-syntax-table)))
283 ;; Make all chars be of punctuation syntax.
284 (dotimes (i 256) (aset st i '(1)))
285 (modify-syntax-entry ?\\ "\\" st)
287 "Syntax table used internally for processing quote-like operators.")
289 (defun perl-quote-syntax-table (char)
290 (let ((close (cdr (assq char perl-quote-like-pairs)))
291 (st (copy-syntax-table perl-empty-syntax-table)))
292 (if (not close)
293 (modify-syntax-entry char "\"" st)
294 (modify-syntax-entry char "(" st)
295 (modify-syntax-entry close ")" st))
296 st))
298 (defun perl-font-lock-syntactic-face-function (state)
299 (let ((char (nth 3 state)))
300 (cond
301 ((not char)
302 ;; Comment or docstring.
303 (if (nth 7 state) font-lock-doc-face font-lock-comment-face))
304 ((and (char-valid-p char) (eq (char-syntax (nth 3 state)) ?\"))
305 ;; Normal string.
306 font-lock-string-face)
307 ((eq (nth 3 state) ?\n)
308 ;; A `format' command.
309 (save-excursion
310 (when (and (re-search-forward "^\\s *\\.\\s *$" nil t)
311 (not (eobp)))
312 (put-text-property (point) (1+ (point)) 'syntax-table '(7)))
313 font-lock-string-face))
315 ;; This is regexp like quote thingy.
316 (setq char (char-after (nth 8 state)))
317 (save-excursion
318 (let ((twoargs (save-excursion
319 (goto-char (nth 8 state))
320 (skip-syntax-backward " ")
321 (skip-syntax-backward "w")
322 (member (buffer-substring
323 (point) (progn (forward-word 1) (point)))
324 '("tr" "s" "y"))))
325 (close (cdr (assq char perl-quote-like-pairs)))
326 (pos (point))
327 (st (perl-quote-syntax-table char)))
328 (if (not close)
329 ;; The closing char is the same as the opening char.
330 (with-syntax-table st
331 (parse-partial-sexp (point) (point-max)
332 nil nil state 'syntax-table)
333 (when twoargs
334 (parse-partial-sexp (point) (point-max)
335 nil nil state 'syntax-table)))
336 ;; The open/close chars are matched like () [] {} and <>.
337 (let ((parse-sexp-lookup-properties nil))
338 (condition-case err
339 (progn
340 (with-syntax-table st
341 (goto-char (nth 8 state)) (forward-sexp 1))
342 (when twoargs
343 (save-excursion
344 ;; Skip whitespace and make sure that font-lock will
345 ;; refontify the second part in the proper context.
346 (put-text-property
347 (point) (progn (forward-comment (point-max)) (point))
348 'font-lock-multiline t)
350 (unless
351 (save-excursion
352 (with-syntax-table
353 (perl-quote-syntax-table (char-after))
354 (forward-sexp 1))
355 (put-text-property pos (line-end-position)
356 'jit-lock-defer-multiline t)
357 (looking-at "\\s-*\\sw*e"))
358 (put-text-property (point) (1+ (point))
359 'syntax-table
360 (if (assoc (char-after)
361 perl-quote-like-pairs)
362 '(15) '(7)))))))
363 ;; The arg(s) is not terminated, so it extends until EOB.
364 (scan-error (goto-char (point-max))))))
365 ;; Point is now right after the arg(s).
366 ;; Erase any syntactic marks within the quoted text.
367 (put-text-property pos (1- (point)) 'syntax-table nil)
368 (when (eq (char-before (1- (point))) ?$)
369 (put-text-property (- (point) 2) (1- (point))
370 'syntax-table '(1)))
371 (put-text-property (1- (point)) (point)
372 'syntax-table (if close '(15) '(7)))
373 font-lock-string-face))))))
374 ;; (if (or twoargs (not (looking-at "\\s-*\\sw*e")))
375 ;; font-lock-string-face
376 ;; (font-lock-fontify-syntactically-region
377 ;; ;; FIXME: `end' is accessed via dyn-scoping.
378 ;; pos (min end (1- (point))) nil '(nil))
379 ;; nil)))))))
382 (defcustom perl-indent-level 4
383 "*Indentation of Perl statements with respect to containing block."
384 :type 'integer
385 :group 'perl)
386 (defcustom perl-continued-statement-offset 4
387 "*Extra indent for lines not starting new statements."
388 :type 'integer
389 :group 'perl)
390 (defcustom perl-continued-brace-offset -4
391 "*Extra indent for substatements that start with open-braces.
392 This is in addition to `perl-continued-statement-offset'."
393 :type 'integer
394 :group 'perl)
395 (defcustom perl-brace-offset 0
396 "*Extra indentation for braces, compared with other text in same context."
397 :type 'integer
398 :group 'perl)
399 (defcustom perl-brace-imaginary-offset 0
400 "*Imagined indentation of an open brace that actually follows a statement."
401 :type 'integer
402 :group 'perl)
403 (defcustom perl-label-offset -2
404 "*Offset of Perl label lines relative to usual indentation."
405 :type 'integer
406 :group 'perl)
407 (defcustom perl-indent-continued-arguments nil
408 "*If non-nil offset of argument lines relative to usual indentation.
409 If nil, continued arguments are aligned with the first argument."
410 :type '(choice integer (const nil))
411 :group 'perl)
413 (defcustom perl-tab-always-indent tab-always-indent
414 "Non-nil means TAB in Perl mode always indents the current line.
415 Otherwise it inserts a tab character if you type it past the first
416 nonwhite character on the line."
417 :type 'boolean
418 :group 'perl)
420 ;; I changed the default to nil for consistency with general Emacs
421 ;; conventions -- rms.
422 (defcustom perl-tab-to-comment nil
423 "*Non-nil means TAB moves to eol or makes a comment in some cases.
424 For lines which don't need indenting, TAB either indents an
425 existing comment, moves to end-of-line, or if at end-of-line already,
426 create a new comment."
427 :type 'boolean
428 :group 'perl)
430 (defcustom perl-nochange ";?#\\|\f\\|\\s(\\|\\(\\w\\|\\s_\\)+:[^:]"
431 "*Lines starting with this regular expression are not auto-indented."
432 :type 'regexp
433 :group 'perl)
435 ;; Outline support
437 (defvar perl-outline-regexp
438 (concat (mapconcat 'cadr perl-imenu-generic-expression "\\|")
439 "\\|^=cut\\>"))
441 (defun perl-outline-level ()
442 (cond
443 ((looking-at "package\\s-") 0)
444 ((looking-at "sub\\s-") 1)
445 ((looking-at "=head[0-9]") (- (char-before (match-end 0)) ?0))
446 ((looking-at "=cut") 1)
447 (t 3)))
449 (defvar perl-mode-hook nil
450 "Normal hook to run when entering Perl mode.")
452 ;;;###autoload
453 (defun perl-mode ()
454 "Major mode for editing Perl code.
455 Expression and list commands understand all Perl brackets.
456 Tab indents for Perl code.
457 Comments are delimited with # ... \\n.
458 Paragraphs are separated by blank lines only.
459 Delete converts tabs to spaces as it moves back.
460 \\{perl-mode-map}
461 Variables controlling indentation style:
462 `perl-tab-always-indent'
463 Non-nil means TAB in Perl mode should always indent the current line,
464 regardless of where in the line point is when the TAB command is used.
465 `perl-tab-to-comment'
466 Non-nil means that for lines which don't need indenting, TAB will
467 either delete an empty comment, indent an existing comment, move
468 to end-of-line, or if at end-of-line already, create a new comment.
469 `perl-nochange'
470 Lines starting with this regular expression are not auto-indented.
471 `perl-indent-level'
472 Indentation of Perl statements within surrounding block.
473 The surrounding block's indentation is the indentation
474 of the line on which the open-brace appears.
475 `perl-continued-statement-offset'
476 Extra indentation given to a substatement, such as the
477 then-clause of an if or body of a while.
478 `perl-continued-brace-offset'
479 Extra indentation given to a brace that starts a substatement.
480 This is in addition to `perl-continued-statement-offset'.
481 `perl-brace-offset'
482 Extra indentation for line if it starts with an open brace.
483 `perl-brace-imaginary-offset'
484 An open brace following other text is treated as if it were
485 this far to the right of the start of its line.
486 `perl-label-offset'
487 Extra indentation for line that is a label.
488 `perl-indent-continued-arguments'
489 Offset of argument lines relative to usual indentation.
491 Various indentation styles: K&R BSD BLK GNU LW
492 perl-indent-level 5 8 0 2 4
493 perl-continued-statement-offset 5 8 4 2 4
494 perl-continued-brace-offset 0 0 0 0 -4
495 perl-brace-offset -5 -8 0 0 0
496 perl-brace-imaginary-offset 0 0 4 0 0
497 perl-label-offset -5 -8 -2 -2 -2
499 Turning on Perl mode runs the normal hook `perl-mode-hook'."
500 (interactive)
501 (kill-all-local-variables)
502 (use-local-map perl-mode-map)
503 (setq major-mode 'perl-mode)
504 (setq mode-name "Perl")
505 (setq local-abbrev-table perl-mode-abbrev-table)
506 (set-syntax-table perl-mode-syntax-table)
507 (make-local-variable 'paragraph-start)
508 (setq paragraph-start (concat "$\\|" page-delimiter))
509 (make-local-variable 'paragraph-separate)
510 (setq paragraph-separate paragraph-start)
511 (make-local-variable 'paragraph-ignore-fill-prefix)
512 (setq paragraph-ignore-fill-prefix t)
513 (make-local-variable 'indent-line-function)
514 (setq indent-line-function 'perl-indent-line)
515 (make-local-variable 'require-final-newline)
516 (setq require-final-newline mode-require-final-newline)
517 (make-local-variable 'comment-start)
518 (setq comment-start "# ")
519 (make-local-variable 'comment-end)
520 (setq comment-end "")
521 (make-local-variable 'comment-start-skip)
522 (setq comment-start-skip "\\(^\\|\\s-\\);?#+ *")
523 (make-local-variable 'comment-indent-function)
524 (setq comment-indent-function 'perl-comment-indent)
525 (make-local-variable 'parse-sexp-ignore-comments)
526 (setq parse-sexp-ignore-comments t)
527 ;; Tell font-lock.el how to handle Perl.
528 (setq font-lock-defaults '((perl-font-lock-keywords
529 perl-font-lock-keywords-1
530 perl-font-lock-keywords-2)
531 nil nil ((?\_ . "w")) nil
532 (font-lock-syntactic-keywords
533 . perl-font-lock-syntactic-keywords)
534 (font-lock-syntactic-face-function
535 . perl-font-lock-syntactic-face-function)
536 (parse-sexp-lookup-properties . t)))
537 ;; Tell imenu how to handle Perl.
538 (set (make-local-variable 'imenu-generic-expression)
539 perl-imenu-generic-expression)
540 (setq imenu-case-fold-search nil)
541 ;; Setup outline-minor-mode.
542 (set (make-local-variable 'outline-regexp) perl-outline-regexp)
543 (set (make-local-variable 'outline-level) 'perl-outline-level)
544 (run-mode-hooks 'perl-mode-hook))
546 ;; This is used by indent-for-comment
547 ;; to decide how much to indent a comment in Perl code
548 ;; based on its context.
549 (defun perl-comment-indent ()
550 (if (and (bolp) (not (eolp)))
551 0 ;Existing comment at bol stays there.
552 comment-column))
554 (defalias 'electric-perl-terminator 'perl-electric-terminator)
555 (defun perl-electric-terminator (arg)
556 "Insert character and adjust indentation.
557 If at end-of-line, and not in a comment or a quote, correct the's indentation."
558 (interactive "P")
559 (let ((insertpos (point)))
560 (and (not arg) ; decide whether to indent
561 (eolp)
562 (save-excursion
563 (beginning-of-line)
564 (and (not ; eliminate comments quickly
565 (and comment-start-skip
566 (re-search-forward comment-start-skip insertpos t)) )
567 (or (/= last-command-char ?:)
568 ;; Colon is special only after a label ....
569 (looking-at "\\s-*\\(\\w\\|\\s_\\)+$"))
570 (let ((pps (parse-partial-sexp
571 (perl-beginning-of-function) insertpos)))
572 (not (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))
573 (progn ; must insert, indent, delete
574 (insert-char last-command-char 1)
575 (perl-indent-line)
576 (delete-char -1))))
577 (self-insert-command (prefix-numeric-value arg)))
579 ;; not used anymore, but may be useful someday:
580 ;;(defun perl-inside-parens-p ()
581 ;; (condition-case ()
582 ;; (save-excursion
583 ;; (save-restriction
584 ;; (narrow-to-region (point)
585 ;; (perl-beginning-of-function))
586 ;; (goto-char (point-max))
587 ;; (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
588 ;; (error nil)))
590 (defun perl-indent-command (&optional arg)
591 "Indent current line as Perl code, or optionally, insert a tab character.
593 With an argument, indent the current line, regardless of other options.
595 If `perl-tab-always-indent' is nil and point is not in the indentation
596 area at the beginning of the line, simply insert a tab.
598 Otherwise, indent the current line. If point was within the indentation
599 area it is moved to the end of the indentation area. If the line was
600 already indented properly and point was not within the indentation area,
601 and if `perl-tab-to-comment' is non-nil (the default), then do the first
602 possible action from the following list:
604 1) delete an empty comment
605 2) move forward to start of comment, indenting if necessary
606 3) move forward to end of line
607 4) create an empty comment
608 5) move backward to start of comment, indenting if necessary."
609 (interactive "P")
610 (if arg ; If arg, just indent this line
611 (perl-indent-line "\f")
612 (if (and (not perl-tab-always-indent)
613 (> (current-column) (current-indentation)))
614 (insert-tab)
615 (let* ((oldpnt (point))
616 (lsexp (progn (beginning-of-line) (point)))
617 (bof (perl-beginning-of-function))
618 (delta (progn
619 (goto-char oldpnt)
620 (perl-indent-line "\f\\|;?#" bof))))
621 (and perl-tab-to-comment
622 (= oldpnt (point)) ; done if point moved
623 (if (listp delta) ; if line starts in a quoted string
624 (setq lsexp (or (nth 2 delta) bof))
625 (= delta 0)) ; done if indenting occurred
626 (let ((eol (progn (end-of-line) (point)))
627 state)
628 (if (= (char-after bof) ?=)
629 (if (= oldpnt eol)
630 (message "In a format statement"))
631 (setq state (parse-partial-sexp lsexp eol))
632 (if (nth 3 state)
633 (if (= oldpnt eol) ; already at eol in a string
634 (message "In a string which starts with a %c."
635 (nth 3 state)))
636 (if (not (nth 4 state))
637 (if (= oldpnt eol) ; no comment, create one?
638 (indent-for-comment))
639 (beginning-of-line)
640 (if (and comment-start-skip
641 (re-search-forward comment-start-skip eol 'move))
642 (if (eolp)
643 (progn ; kill existing comment
644 (goto-char (match-beginning 0))
645 (skip-chars-backward " \t")
646 (kill-region (point) eol))
647 (if (or (< oldpnt (point)) (= oldpnt eol))
648 (indent-for-comment) ; indent existing comment
649 (end-of-line)))
650 (if (/= oldpnt eol)
651 (end-of-line)
652 (message "Use backslash to quote # characters.")
653 (ding t))))))))))))
655 (defun perl-indent-line (&optional nochange parse-start)
656 "Indent current line as Perl code.
657 Return the amount the indentation
658 changed by, or (parse-state) if line starts in a quoted string."
659 (let ((case-fold-search nil)
660 (pos (- (point-max) (point)))
661 (bof (or parse-start (save-excursion (perl-beginning-of-function))))
662 beg indent shift-amt)
663 (beginning-of-line)
664 (setq beg (point))
665 (setq shift-amt
666 (cond ((eq (char-after bof) ?=) 0)
667 ((listp (setq indent (perl-calculate-indent bof))) indent)
668 ((looking-at (or nochange perl-nochange)) 0)
670 (skip-chars-forward " \t\f")
671 (setq indent (perl-indent-new-calculate nil indent bof))
672 (- indent (current-column)))))
673 (skip-chars-forward " \t\f")
674 (if (and (numberp shift-amt) (/= 0 shift-amt))
675 (progn (delete-region beg (point))
676 (indent-to indent)))
677 ;; If initial point was within line's indentation,
678 ;; position after the indentation. Else stay at same point in text.
679 (if (> (- (point-max) pos) (point))
680 (goto-char (- (point-max) pos)))
681 shift-amt))
683 (defun perl-continuation-line-p (limit)
684 "Move to end of previous line and return non-nil if continued."
685 ;; Statement level. Is it a continuation or a new statement?
686 ;; Find previous non-comment character.
687 (perl-backward-to-noncomment)
688 ;; Back up over label lines, since they don't
689 ;; affect whether our line is a continuation.
690 (while (or (eq (preceding-char) ?\,)
691 (and (eq (preceding-char) ?:)
692 (memq (char-syntax (char-after (- (point) 2)))
693 '(?w ?_))))
694 (if (eq (preceding-char) ?\,)
695 (perl-backward-to-start-of-continued-exp limit)
696 (beginning-of-line))
697 (perl-backward-to-noncomment))
698 ;; Now we get the answer.
699 (not (memq (preceding-char) '(?\; ?\} ?\{))))
701 (defun perl-hanging-paren-p ()
702 "Non-nil if we are right after a hanging parenthesis-like char."
703 (and (looking-at "[ \t]*$")
704 (save-excursion
705 (skip-syntax-backward " (") (not (bolp)))))
707 (defun perl-indent-new-calculate (&optional virtual default parse-start)
709 (and virtual (save-excursion (skip-chars-backward " \t") (bolp))
710 (current-column))
711 (and (looking-at "\\(\\w\\|\\s_\\)+:[^:]")
712 (max 1 (+ (or default (perl-calculate-indent parse-start))
713 perl-label-offset)))
714 (and (= (char-syntax (following-char)) ?\))
715 (save-excursion
716 (forward-char 1)
717 (forward-sexp -1)
718 (perl-indent-new-calculate 'virtual nil parse-start)))
719 (and (and (= (following-char) ?{)
720 (save-excursion (forward-char) (perl-hanging-paren-p)))
721 (+ (or default (perl-calculate-indent parse-start))
722 perl-brace-offset))
723 (or default (perl-calculate-indent parse-start))))
725 (defun perl-calculate-indent (&optional parse-start)
726 "Return appropriate indentation for current line as Perl code.
727 In usual case returns an integer: the column to indent to.
728 Returns (parse-state) if line starts inside a string.
729 Optional argument PARSE-START should be the position of `beginning-of-defun'."
730 (save-excursion
731 (let ((indent-point (point))
732 (case-fold-search nil)
733 (colon-line-end 0)
734 state containing-sexp)
735 (if parse-start ;used to avoid searching
736 (goto-char parse-start)
737 (perl-beginning-of-function))
738 ;; We might be now looking at a local function that has nothing to
739 ;; do with us because `indent-point' is past it. In this case
740 ;; look further back up for another `perl-beginning-of-function'.
741 (while (and (looking-at "{")
742 (save-excursion
743 (beginning-of-line)
744 (looking-at "\\s-+sub\\>"))
745 (> indent-point (save-excursion (forward-sexp 1) (point))))
746 (perl-beginning-of-function))
747 (while (< (point) indent-point) ;repeat until right sexp
748 (setq state (parse-partial-sexp (point) indent-point 0))
749 ;; state = (depth_in_parens innermost_containing_list
750 ;; last_complete_sexp string_terminator_or_nil inside_commentp
751 ;; following_quotep minimum_paren-depth_this_scan)
752 ;; Parsing stops if depth in parentheses becomes equal to third arg.
753 (setq containing-sexp (nth 1 state)))
754 (cond ((nth 3 state) state) ; In a quoted string?
755 ((null containing-sexp) ; Line is at top level.
756 (skip-chars-forward " \t\f")
757 (if (= (following-char) ?{)
758 0 ; move to beginning of line if it starts a function body
759 ;; indent a little if this is a continuation line
760 (perl-backward-to-noncomment)
761 (if (or (bobp)
762 (memq (preceding-char) '(?\; ?\})))
763 0 perl-continued-statement-offset)))
764 ((/= (char-after containing-sexp) ?{)
765 ;; line is expression, not statement:
766 ;; indent to just after the surrounding open.
767 (goto-char (1+ containing-sexp))
768 (if (perl-hanging-paren-p)
769 ;; We're indenting an arg of a call like:
770 ;; $a = foobarlongnamefun (
771 ;; arg1
772 ;; arg2
773 ;; );
774 (progn
775 (skip-syntax-backward "(")
776 (condition-case err
777 (while (save-excursion
778 (skip-syntax-backward " ") (not (bolp)))
779 (forward-sexp -1))
780 (scan-error nil))
781 (+ (current-column) perl-indent-level))
782 (if perl-indent-continued-arguments
783 (+ perl-indent-continued-arguments (current-indentation))
784 (skip-chars-forward " \t")
785 (current-column))))
787 ;; Statement level. Is it a continuation or a new statement?
788 (if (perl-continuation-line-p containing-sexp)
789 ;; This line is continuation of preceding line's statement;
790 ;; indent perl-continued-statement-offset more than the
791 ;; previous line of the statement.
792 (progn
793 (perl-backward-to-start-of-continued-exp containing-sexp)
794 (+ (if (save-excursion
795 (perl-continuation-line-p containing-sexp))
796 ;; If the continued line is itself a continuation
797 ;; line, then align, otherwise add an offset.
798 0 perl-continued-statement-offset)
799 (current-column)
800 (if (save-excursion (goto-char indent-point)
801 (looking-at "[ \t]*{"))
802 perl-continued-brace-offset 0)))
803 ;; This line starts a new statement.
804 ;; Position at last unclosed open.
805 (goto-char containing-sexp)
807 ;; Is line first statement after an open-brace?
808 ;; If no, find that first statement and indent like it.
809 (save-excursion
810 (forward-char 1)
811 ;; Skip over comments and labels following openbrace.
812 (while (progn
813 (skip-chars-forward " \t\f\n")
814 (cond ((looking-at ";?#")
815 (forward-line 1) t)
816 ((looking-at "\\(\\w\\|\\s_\\)+:[^:]")
817 (save-excursion
818 (end-of-line)
819 (setq colon-line-end (point)))
820 (search-forward ":")))))
821 ;; The first following code counts
822 ;; if it is before the line we want to indent.
823 (and (< (point) indent-point)
824 (if (> colon-line-end (point))
825 (- (current-indentation) perl-label-offset)
826 (current-column))))
827 ;; If no previous statement,
828 ;; indent it relative to line brace is on.
829 ;; For open paren in column zero, don't let statement
830 ;; start there too. If perl-indent-level is zero,
831 ;; use perl-brace-offset + perl-continued-statement-offset
832 ;; For open-braces not the first thing in a line,
833 ;; add in perl-brace-imaginary-offset.
834 (+ (if (and (bolp) (zerop perl-indent-level))
835 (+ perl-brace-offset perl-continued-statement-offset)
836 perl-indent-level)
837 ;; Move back over whitespace before the openbrace.
838 ;; If openbrace is not first nonwhite thing on the line,
839 ;; add the perl-brace-imaginary-offset.
840 (progn (skip-chars-backward " \t")
841 (if (bolp) 0 perl-brace-imaginary-offset))
842 ;; If the openbrace is preceded by a parenthesized exp,
843 ;; move to the beginning of that;
844 ;; possibly a different line
845 (progn
846 (if (eq (preceding-char) ?\))
847 (forward-sexp -1))
848 ;; Get initial indentation of the line we are on.
849 (current-indentation))))))))))
851 (defun perl-backward-to-noncomment ()
852 "Move point backward to after the first non-white-space, skipping comments."
853 (interactive)
854 (forward-comment (- (point-max))))
856 (defun perl-backward-to-start-of-continued-exp (lim)
857 (if (= (preceding-char) ?\))
858 (forward-sexp -1))
859 (beginning-of-line)
860 (if (<= (point) lim)
861 (goto-char (1+ lim)))
862 (skip-chars-forward " \t\f"))
864 ;; note: this may be slower than the c-mode version, but I can understand it.
865 (defalias 'indent-perl-exp 'perl-indent-exp)
866 (defun perl-indent-exp ()
867 "Indent each line of the Perl grouping following point."
868 (interactive)
869 (let* ((case-fold-search nil)
870 (oldpnt (point-marker))
871 (bof-mark (save-excursion
872 (end-of-line 2)
873 (perl-beginning-of-function)
874 (point-marker)))
875 eol last-mark lsexp-mark delta)
876 (if (= (char-after (marker-position bof-mark)) ?=)
877 (message "Can't indent a format statement")
878 (message "Indenting Perl expression...")
879 (save-excursion (end-of-line) (setq eol (point)))
880 (save-excursion ; locate matching close paren
881 (while (and (not (eobp)) (<= (point) eol))
882 (parse-partial-sexp (point) (point-max) 0))
883 (setq last-mark (point-marker)))
884 (setq lsexp-mark bof-mark)
885 (beginning-of-line)
886 (while (< (point) (marker-position last-mark))
887 (setq delta (perl-indent-line nil (marker-position bof-mark)))
888 (if (numberp delta) ; unquoted start-of-line?
889 (progn
890 (if (eolp)
891 (delete-horizontal-space))
892 (setq lsexp-mark (point-marker))))
893 (end-of-line)
894 (setq eol (point))
895 (if (nth 4 (parse-partial-sexp (marker-position lsexp-mark) eol))
896 (progn ; line ends in a comment
897 (beginning-of-line)
898 (if (or (not (looking-at "\\s-*;?#"))
899 (listp delta)
900 (and (/= 0 delta)
901 (= (- (current-indentation) delta) comment-column)))
902 (if (and comment-start-skip
903 (re-search-forward comment-start-skip eol t))
904 (indent-for-comment))))) ; indent existing comment
905 (forward-line 1))
906 (goto-char (marker-position oldpnt))
907 (message "Indenting Perl expression...done"))))
909 (defun perl-beginning-of-function (&optional arg)
910 "Move backward to next beginning-of-function, or as far as possible.
911 With argument, repeat that many times; negative args move forward.
912 Returns new value of point in all cases."
913 (interactive "p")
914 (or arg (setq arg 1))
915 (if (< arg 0) (forward-char 1))
916 (and (/= arg 0)
917 (re-search-backward "^\\s(\\|^\\s-*sub\\b[^{]+{\\|^\\s-*format\\b[^=]*=\\|^\\."
918 nil 'move arg)
919 (goto-char (1- (match-end 0))))
920 (point))
922 ;; note: this routine is adapted directly from emacs lisp.el, end-of-defun;
923 ;; no bugs have been removed :-)
924 (defun perl-end-of-function (&optional arg)
925 "Move forward to next end-of-function.
926 The end of a function is found by moving forward from the beginning of one.
927 With argument, repeat that many times; negative args move backward."
928 (interactive "p")
929 (or arg (setq arg 1))
930 (let ((first t))
931 (while (and (> arg 0) (< (point) (point-max)))
932 (let ((pos (point)))
933 (while (progn
934 (if (and first
935 (progn
936 (forward-char 1)
937 (perl-beginning-of-function 1)
938 (not (bobp))))
940 (or (bobp) (forward-char -1))
941 (perl-beginning-of-function -1))
942 (setq first nil)
943 (forward-list 1)
944 (skip-chars-forward " \t")
945 (if (looking-at "[#\n]")
946 (forward-line 1))
947 (<= (point) pos))))
948 (setq arg (1- arg)))
949 (while (< arg 0)
950 (let ((pos (point)))
951 (perl-beginning-of-function 1)
952 (forward-sexp 1)
953 (forward-line 1)
954 (if (>= (point) pos)
955 (if (progn (perl-beginning-of-function 2) (not (bobp)))
956 (progn
957 (forward-list 1)
958 (skip-chars-forward " \t")
959 (if (looking-at "[#\n]")
960 (forward-line 1)))
961 (goto-char (point-min)))))
962 (setq arg (1+ arg)))))
964 (defalias 'mark-perl-function 'perl-mark-function)
965 (defun perl-mark-function ()
966 "Put mark at end of Perl function, point at beginning."
967 (interactive)
968 (push-mark (point))
969 (perl-end-of-function)
970 (push-mark (point))
971 (perl-beginning-of-function)
972 (backward-paragraph))
974 (provide 'perl-mode)
976 ;; arch-tag: 8c7ff68d-15f3-46a2-ade2-b7c41f176826
977 ;;; perl-mode.el ends here