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