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