annotate lisp/modes/perl-mode.el @ 12:bcdc7deadc19 r19-15b7

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