Mercurial > emacs
annotate lisp/progmodes/perl-mode.el @ 3203:1055aa1b7547
* xfns.c (x_set_frame_parameters): Use the first
position/size parameter we find, not the last.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Fri, 28 May 1993 06:44:48 +0000 |
parents | 10e417efb12a |
children | 507f64624555 |
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 |
882 | 3 ;; Copyright (C) 1990 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 | |
36 ;; (setq auto-mode-alist (append (list (cons "\\.pl$" 'perl-mode)) | |
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): | |
67 ;; 1) Regular expression delimitors do not act as quotes, so special | |
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 | |
149 (defconst perl-indent-level 4 | |
150 "*Indentation of Perl statements with respect to containing block.") | |
151 (defconst perl-continued-statement-offset 4 | |
152 "*Extra indent for lines not starting new statements.") | |
153 (defconst perl-continued-brace-offset -4 | |
154 "*Extra indent for substatements that start with open-braces. | |
155 This is in addition to perl-continued-statement-offset.") | |
156 (defconst perl-brace-offset 0 | |
157 "*Extra indentation for braces, compared with other text in same context.") | |
158 (defconst perl-brace-imaginary-offset 0 | |
159 "*Imagined indentation of an open brace that actually follows a statement.") | |
160 (defconst perl-label-offset -2 | |
161 "*Offset of Perl label lines relative to usual indentation.") | |
162 | |
163 (defconst perl-tab-always-indent t | |
164 "*Non-nil means TAB in Perl mode should always indent the current line, | |
165 regardless of where in the line point is when the TAB command is used.") | |
166 | |
167 (defconst perl-tab-to-comment t | |
168 "*Non-nil means that for lines which don't need indenting, TAB will | |
169 either indent an existing comment, move to end-of-line, or if at end-of-line | |
170 already, create a new comment.") | |
171 | |
172 (defconst perl-nochange ";?#\\|\f\\|\\s(\\|\\(\\w\\|\\s_\\)+:" | |
173 "*Lines starting with this regular expression will not be auto-indented.") | |
174 | |
175 (defun perl-mode () | |
176 "Major mode for editing Perl code. | |
177 Expression and list commands understand all Perl brackets. | |
178 Tab indents for Perl code. | |
179 Comments are delimited with # ... \\n. | |
180 Paragraphs are separated by blank lines only. | |
181 Delete converts tabs to spaces as it moves back. | |
182 \\{perl-mode-map} | |
183 Variables controlling indentation style: | |
184 perl-tab-always-indent | |
185 Non-nil means TAB in Perl mode should always indent the current line, | |
186 regardless of where in the line point is when the TAB command is used. | |
187 perl-tab-to-comment | |
188 Non-nil means that for lines which don't need indenting, TAB will | |
189 either delete an empty comment, indent an existing comment, move | |
190 to end-of-line, or if at end-of-line already, create a new comment. | |
191 perl-nochange | |
192 Lines starting with this regular expression will not be auto-indented. | |
193 perl-indent-level | |
194 Indentation of Perl statements within surrounding block. | |
195 The surrounding block's indentation is the indentation | |
196 of the line on which the open-brace appears. | |
197 perl-continued-statement-offset | |
198 Extra indentation given to a substatement, such as the | |
199 then-clause of an if or body of a while. | |
200 perl-continued-brace-offset | |
201 Extra indentation given to a brace that starts a substatement. | |
202 This is in addition to perl-continued-statement-offset. | |
203 perl-brace-offset | |
204 Extra indentation for line if it starts with an open brace. | |
205 perl-brace-imaginary-offset | |
206 An open brace following other text is treated as if it were | |
207 this far to the right of the start of its line. | |
208 perl-label-offset | |
209 Extra indentation for line that is a label. | |
210 | |
211 Various indentation styles: K&R BSD BLK GNU LW | |
212 perl-indent-level 5 8 0 2 4 | |
213 perl-continued-statement-offset 5 8 4 2 4 | |
214 perl-continued-brace-offset 0 0 0 0 -4 | |
215 perl-brace-offset -5 -8 0 0 0 | |
216 perl-brace-imaginary-offset 0 0 4 0 0 | |
217 perl-label-offset -5 -8 -2 -2 -2 | |
218 | |
219 Turning on Perl mode calls the value of the variable perl-mode-hook with no | |
220 args, if that value is non-nil." | |
221 (interactive) | |
222 (kill-all-local-variables) | |
223 (use-local-map perl-mode-map) | |
224 (setq major-mode 'perl-mode) | |
225 (setq mode-name "Perl") | |
226 (setq local-abbrev-table perl-mode-abbrev-table) | |
227 (set-syntax-table perl-mode-syntax-table) | |
228 (make-local-variable 'paragraph-start) | |
229 (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
230 (make-local-variable 'paragraph-separate) | |
231 (setq paragraph-separate paragraph-start) | |
232 (make-local-variable 'paragraph-ignore-fill-prefix) | |
233 (setq paragraph-ignore-fill-prefix t) | |
234 (make-local-variable 'indent-line-function) | |
235 (setq indent-line-function 'perl-indent-line) | |
236 (make-local-variable 'require-final-newline) | |
237 (setq require-final-newline t) | |
238 (make-local-variable 'comment-start) | |
239 (setq comment-start "# ") | |
240 (make-local-variable 'comment-end) | |
241 (setq comment-end "") | |
242 (make-local-variable 'comment-column) | |
243 (setq comment-column 32) | |
244 (make-local-variable 'comment-start-skip) | |
245 (setq comment-start-skip "\\(^\\|\\s-\\);?#+ *") | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
882
diff
changeset
|
246 (make-local-variable 'comment-indent-function) |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
882
diff
changeset
|
247 (setq comment-indent-function 'perl-comment-indent) |
475 | 248 (make-local-variable 'parse-sexp-ignore-comments) |
249 (setq parse-sexp-ignore-comments nil) | |
250 (run-hooks 'perl-mode-hook)) | |
251 | |
252 ;; This is used by indent-for-comment | |
253 ;; to decide how much to indent a comment in Perl code | |
254 ;; based on its context. | |
255 (defun perl-comment-indent () | |
256 (if (and (bolp) (not (eolp))) | |
257 0 ;Existing comment at bol stays there. | |
258 (save-excursion | |
259 (skip-chars-backward " \t") | |
260 (max (1+ (current-column)) ;Else indent at comment column | |
261 comment-column)))) ; except leave at least one space. | |
262 | |
263 (defun electric-perl-terminator (arg) | |
264 "Insert character. If at end-of-line, and not in a comment or a quote, | |
265 correct the line's indentation." | |
266 (interactive "P") | |
267 (let ((insertpos (point))) | |
268 (and (not arg) ; decide whether to indent | |
269 (eolp) | |
270 (save-excursion | |
271 (beginning-of-line) | |
272 (and (not ; eliminate comments quickly | |
273 (re-search-forward comment-start-skip insertpos t)) | |
274 (or (/= last-command-char ?:) | |
275 ;; Colon is special only after a label .... | |
276 (looking-at "\\s-*\\(\\w\\|\\s_\\)+$")) | |
277 (let ((pps (parse-partial-sexp | |
278 (perl-beginning-of-function) insertpos))) | |
279 (not (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))) | |
280 (progn ; must insert, indent, delete | |
281 (insert-char last-command-char 1) | |
282 (perl-indent-line) | |
283 (delete-char -1)))) | |
284 (self-insert-command (prefix-numeric-value arg))) | |
285 | |
286 ;; not used anymore, but may be useful someday: | |
287 ;;(defun perl-inside-parens-p () | |
288 ;; (condition-case () | |
289 ;; (save-excursion | |
290 ;; (save-restriction | |
291 ;; (narrow-to-region (point) | |
292 ;; (perl-beginning-of-function)) | |
293 ;; (goto-char (point-max)) | |
294 ;; (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\())) | |
295 ;; (error nil))) | |
296 | |
297 (defun perl-indent-command (&optional arg) | |
298 "Indent current line as Perl code, or optionally, insert a tab character. | |
299 | |
300 With an argument, indent the current line, regardless of other options. | |
301 | |
302 If perl-tab-always-indent is nil and point is not in the indentation | |
303 area at the beginning of the line, simply insert a tab. | |
304 | |
305 Otherwise, indent the current line. If point was within the indentation | |
306 area it is moved to the end of the indentation area. If the line was | |
307 already indented properly and point was not within the indentation area, | |
308 and if perl-tab-to-comment is non-nil (the default), then do the first | |
309 possible action from the following list: | |
310 | |
311 1) delete an empty comment | |
312 2) move forward to start of comment, indenting if necessary | |
313 3) move forward to end of line | |
314 4) create an empty comment | |
315 5) move backward to start of comment, indenting if necessary." | |
316 (interactive "P") | |
317 (if arg ; If arg, just indent this line | |
318 (perl-indent-line "\f") | |
319 (if (and (not perl-tab-always-indent) | |
320 (<= (current-column) (current-indentation))) | |
321 (insert-tab) | |
322 (let (bof lsexp delta (oldpnt (point))) | |
323 (beginning-of-line) | |
324 (setq lsexp (point)) | |
325 (setq bof (perl-beginning-of-function)) | |
326 (goto-char oldpnt) | |
327 (setq delta (perl-indent-line "\f\\|;?#" bof)) | |
328 (and perl-tab-to-comment | |
329 (= oldpnt (point)) ; done if point moved | |
330 (if (listp delta) ; if line starts in a quoted string | |
331 (setq lsexp (or (nth 2 delta) bof)) | |
332 (= delta 0)) ; done if indenting occurred | |
333 (let (eol state) | |
334 (end-of-line) | |
335 (setq eol (point)) | |
336 (if (= (char-after bof) ?=) | |
337 (if (= oldpnt eol) | |
338 (message "In a format statement")) | |
339 (setq state (parse-partial-sexp lsexp eol)) | |
340 (if (nth 3 state) | |
341 (if (= oldpnt eol) ; already at eol in a string | |
342 (message "In a string which starts with a %c." | |
343 (nth 3 state))) | |
344 (if (not (nth 4 state)) | |
345 (if (= oldpnt eol) ; no comment, create one? | |
346 (indent-for-comment)) | |
347 (beginning-of-line) | |
348 (if (re-search-forward comment-start-skip eol 'move) | |
349 (if (eolp) | |
350 (progn ; kill existing comment | |
351 (goto-char (match-beginning 0)) | |
352 (skip-chars-backward " \t") | |
353 (kill-region (point) eol)) | |
354 (if (or (< oldpnt (point)) (= oldpnt eol)) | |
355 (indent-for-comment) ; indent existing comment | |
356 (end-of-line))) | |
357 (if (/= oldpnt eol) | |
358 (end-of-line) | |
359 (message "Use backslash to quote # characters.") | |
360 (ding t)))))))))))) | |
361 | |
362 (defun perl-indent-line (&optional nochange parse-start) | |
363 "Indent current line as Perl code. Return the amount the indentation | |
364 changed by, or (parse-state) if line starts in a quoted string." | |
365 (let ((case-fold-search nil) | |
366 (pos (- (point-max) (point))) | |
367 (bof (or parse-start (save-excursion (perl-beginning-of-function)))) | |
368 beg indent shift-amt) | |
369 (beginning-of-line) | |
370 (setq beg (point)) | |
371 (setq shift-amt | |
372 (cond ((= (char-after bof) ?=) 0) | |
373 ((listp (setq indent (calculate-perl-indent bof))) indent) | |
374 ((looking-at (or nochange perl-nochange)) 0) | |
375 (t | |
376 (skip-chars-forward " \t\f") | |
377 (cond ((looking-at "\\(\\w\\|\\s_\\)+:") | |
378 (setq indent (max 1 (+ indent perl-label-offset)))) | |
379 ((= (following-char) ?}) | |
380 (setq indent (- indent perl-indent-level))) | |
381 ((= (following-char) ?{) | |
382 (setq indent (+ indent perl-brace-offset)))) | |
383 (- indent (current-column))))) | |
384 (skip-chars-forward " \t\f") | |
385 (if (and (numberp shift-amt) (/= 0 shift-amt)) | |
386 (progn (delete-region beg (point)) | |
387 (indent-to indent))) | |
388 ;; If initial point was within line's indentation, | |
389 ;; position after the indentation. Else stay at same point in text. | |
390 (if (> (- (point-max) pos) (point)) | |
391 (goto-char (- (point-max) pos))) | |
392 shift-amt)) | |
393 | |
394 (defun calculate-perl-indent (&optional parse-start) | |
395 "Return appropriate indentation for current line as Perl code. | |
396 In usual case returns an integer: the column to indent to. | |
397 Returns (parse-state) if line starts inside a string." | |
398 (save-excursion | |
399 (beginning-of-line) | |
400 (let ((indent-point (point)) | |
401 (case-fold-search nil) | |
402 (colon-line-end 0) | |
403 state containing-sexp) | |
404 (if parse-start ;used to avoid searching | |
405 (goto-char parse-start) | |
406 (perl-beginning-of-function)) | |
407 (while (< (point) indent-point) ;repeat until right sexp | |
408 (setq parse-start (point)) | |
409 (setq state (parse-partial-sexp (point) indent-point 0)) | |
410 ; state = (depth_in_parens innermost_containing_list last_complete_sexp | |
411 ; string_terminator_or_nil inside_commentp following_quotep | |
412 ; minimum_paren-depth_this_scan) | |
413 ; Parsing stops if depth in parentheses becomes equal to third arg. | |
414 (setq containing-sexp (nth 1 state))) | |
415 (cond ((nth 3 state) state) ; In a quoted string? | |
416 ((null containing-sexp) ; Line is at top level. | |
417 (skip-chars-forward " \t\f") | |
418 (if (= (following-char) ?{) | |
419 0 ; move to beginning of line if it starts a function body | |
420 ;; indent a little if this is a continuation line | |
421 (perl-backward-to-noncomment) | |
422 (if (or (bobp) | |
423 (memq (preceding-char) '(?\; ?\}))) | |
424 0 perl-continued-statement-offset))) | |
425 ((/= (char-after containing-sexp) ?{) | |
426 ;; line is expression, not statement: | |
427 ;; indent to just after the surrounding open. | |
428 (goto-char (1+ containing-sexp)) | |
429 (current-column)) | |
430 (t | |
431 ;; Statement level. Is it a continuation or a new statement? | |
432 ;; Find previous non-comment character. | |
433 (perl-backward-to-noncomment) | |
434 ;; Back up over label lines, since they don't | |
435 ;; affect whether our line is a continuation. | |
436 (while (or (eq (preceding-char) ?\,) | |
437 (and (eq (preceding-char) ?:) | |
438 (memq (char-syntax (char-after (- (point) 2))) | |
439 '(?w ?_)))) | |
440 (if (eq (preceding-char) ?\,) | |
441 (perl-backward-to-start-of-continued-exp containing-sexp)) | |
442 (beginning-of-line) | |
443 (perl-backward-to-noncomment)) | |
444 ;; Now we get the answer. | |
445 (if (not (memq (preceding-char) '(?\; ?\} ?\{))) | |
446 ;; This line is continuation of preceding line's statement; | |
447 ;; indent perl-continued-statement-offset more than the | |
448 ;; previous line of the statement. | |
449 (progn | |
450 (perl-backward-to-start-of-continued-exp containing-sexp) | |
451 (+ perl-continued-statement-offset (current-column) | |
452 (if (save-excursion (goto-char indent-point) | |
453 (looking-at "[ \t]*{")) | |
454 perl-continued-brace-offset 0))) | |
455 ;; This line starts a new statement. | |
456 ;; Position at last unclosed open. | |
457 (goto-char containing-sexp) | |
458 (or | |
459 ;; If open paren is in col 0, close brace is special | |
460 (and (bolp) | |
461 (save-excursion (goto-char indent-point) | |
462 (looking-at "[ \t]*}")) | |
463 perl-indent-level) | |
464 ;; Is line first statement after an open-brace? | |
465 ;; If no, find that first statement and indent like it. | |
466 (save-excursion | |
467 (forward-char 1) | |
468 ;; Skip over comments and labels following openbrace. | |
469 (while (progn | |
470 (skip-chars-forward " \t\f\n") | |
471 (cond ((looking-at ";?#") | |
472 (forward-line 1) t) | |
473 ((looking-at "\\(\\w\\|\\s_\\)+:") | |
474 (save-excursion | |
475 (end-of-line) | |
476 (setq colon-line-end (point))) | |
477 (search-forward ":"))))) | |
478 ;; The first following code counts | |
479 ;; if it is before the line we want to indent. | |
480 (and (< (point) indent-point) | |
481 (if (> colon-line-end (point)) | |
482 (- (current-indentation) perl-label-offset) | |
483 (current-column)))) | |
484 ;; If no previous statement, | |
485 ;; indent it relative to line brace is on. | |
486 ;; For open paren in column zero, don't let statement | |
487 ;; start there too. If perl-indent-level is zero, | |
488 ;; use perl-brace-offset + perl-continued-statement-offset | |
489 ;; For open-braces not the first thing in a line, | |
490 ;; add in perl-brace-imaginary-offset. | |
491 (+ (if (and (bolp) (zerop perl-indent-level)) | |
492 (+ perl-brace-offset perl-continued-statement-offset) | |
493 perl-indent-level) | |
494 ;; Move back over whitespace before the openbrace. | |
495 ;; If openbrace is not first nonwhite thing on the line, | |
496 ;; add the perl-brace-imaginary-offset. | |
497 (progn (skip-chars-backward " \t") | |
498 (if (bolp) 0 perl-brace-imaginary-offset)) | |
499 ;; If the openbrace is preceded by a parenthesized exp, | |
500 ;; move to the beginning of that; | |
501 ;; possibly a different line | |
502 (progn | |
503 (if (eq (preceding-char) ?\)) | |
504 (forward-sexp -1)) | |
505 ;; Get initial indentation of the line we are on. | |
506 (current-indentation)))))))))) | |
507 | |
508 (defun perl-backward-to-noncomment () | |
509 "Move point backward to after the first non-white-space, skipping comments." | |
510 (interactive) | |
511 (let (opoint stop) | |
512 (while (not stop) | |
513 (setq opoint (point)) | |
514 (beginning-of-line) | |
515 (if (re-search-forward comment-start-skip opoint 'move 1) | |
516 (progn (goto-char (match-end 1)) | |
517 (skip-chars-forward ";"))) | |
518 (skip-chars-backward " \t\f") | |
519 (setq stop (or (bobp) | |
520 (not (bolp)) | |
521 (forward-char -1)))))) | |
522 | |
523 (defun perl-backward-to-start-of-continued-exp (lim) | |
524 (if (= (preceding-char) ?\)) | |
525 (forward-sexp -1)) | |
526 (beginning-of-line) | |
527 (if (<= (point) lim) | |
528 (goto-char (1+ lim))) | |
529 (skip-chars-forward " \t\f")) | |
530 | |
531 ;; note: this may be slower than the c-mode version, but I can understand it. | |
532 (defun indent-perl-exp () | |
533 "Indent each line of the Perl grouping following point." | |
534 (interactive) | |
535 (let* ((case-fold-search nil) | |
536 (oldpnt (point-marker)) | |
537 (bof-mark (save-excursion | |
538 (end-of-line 2) | |
539 (perl-beginning-of-function) | |
540 (point-marker))) | |
541 eol last-mark lsexp-mark delta) | |
542 (if (= (char-after (marker-position bof-mark)) ?=) | |
543 (message "Can't indent a format statement") | |
544 (message "Indenting Perl expression...") | |
545 (save-excursion (end-of-line) (setq eol (point))) | |
546 (save-excursion ; locate matching close paren | |
547 (while (and (not (eobp)) (<= (point) eol)) | |
548 (parse-partial-sexp (point) (point-max) 0)) | |
549 (setq last-mark (point-marker))) | |
550 (setq lsexp-mark bof-mark) | |
551 (beginning-of-line) | |
552 (while (< (point) (marker-position last-mark)) | |
553 (setq delta (perl-indent-line nil (marker-position bof-mark))) | |
554 (if (numberp delta) ; unquoted start-of-line? | |
555 (progn | |
556 (if (eolp) | |
557 (delete-horizontal-space)) | |
558 (setq lsexp-mark (point-marker)))) | |
559 (end-of-line) | |
560 (setq eol (point)) | |
561 (if (nth 4 (parse-partial-sexp (marker-position lsexp-mark) eol)) | |
562 (progn ; line ends in a comment | |
563 (beginning-of-line) | |
564 (if (or (not (looking-at "\\s-*;?#")) | |
565 (listp delta) | |
566 (and (/= 0 delta) | |
567 (= (- (current-indentation) delta) comment-column))) | |
568 (if (re-search-forward comment-start-skip eol t) | |
569 (indent-for-comment))))) ; indent existing comment | |
570 (forward-line 1)) | |
571 (goto-char (marker-position oldpnt)) | |
572 (message "Indenting Perl expression...done")))) | |
573 | |
574 (defun perl-beginning-of-function (&optional arg) | |
575 "Move backward to next beginning-of-function, or as far as possible. | |
576 With argument, repeat that many times; negative args move forward. | |
577 Returns new value of point in all cases." | |
578 (interactive "p") | |
579 (or arg (setq arg 1)) | |
580 (if (< arg 0) (forward-char 1)) | |
581 (and (/= arg 0) | |
582 (re-search-backward "^\\s(\\|^\\s-*sub\\b[^{]+{\\|^\\s-*format\\b[^=]*=\\|^\\." | |
583 nil 'move arg) | |
584 (goto-char (1- (match-end 0)))) | |
585 (point)) | |
586 | |
587 ;; note: this routine is adapted directly from emacs lisp.el, end-of-defun; | |
588 ;; no bugs have been removed :-) | |
589 (defun perl-end-of-function (&optional arg) | |
590 "Move forward to next end-of-function. | |
591 The end of a function is found by moving forward from the beginning of one. | |
592 With argument, repeat that many times; negative args move backward." | |
593 (interactive "p") | |
594 (or arg (setq arg 1)) | |
595 (let ((first t)) | |
596 (while (and (> arg 0) (< (point) (point-max))) | |
597 (let ((pos (point)) npos) | |
598 (while (progn | |
599 (if (and first | |
600 (progn | |
601 (forward-char 1) | |
602 (perl-beginning-of-function 1) | |
603 (not (bobp)))) | |
604 nil | |
605 (or (bobp) (forward-char -1)) | |
606 (perl-beginning-of-function -1)) | |
607 (setq first nil) | |
608 (forward-list 1) | |
609 (skip-chars-forward " \t") | |
610 (if (looking-at "[#\n]") | |
611 (forward-line 1)) | |
612 (<= (point) pos)))) | |
613 (setq arg (1- arg))) | |
614 (while (< arg 0) | |
615 (let ((pos (point))) | |
616 (perl-beginning-of-function 1) | |
617 (forward-sexp 1) | |
618 (forward-line 1) | |
619 (if (>= (point) pos) | |
620 (if (progn (perl-beginning-of-function 2) (not (bobp))) | |
621 (progn | |
622 (forward-list 1) | |
623 (skip-chars-forward " \t") | |
624 (if (looking-at "[#\n]") | |
625 (forward-line 1))) | |
626 (goto-char (point-min))))) | |
627 (setq arg (1+ arg))))) | |
628 | |
629 (defun mark-perl-function () | |
630 "Put mark at end of Perl function, point at beginning." | |
631 (interactive) | |
632 (push-mark (point)) | |
633 (perl-end-of-function) | |
634 (push-mark (point)) | |
635 (perl-beginning-of-function) | |
636 (backward-paragraph)) | |
637 | |
638 ;;;;;;;; That's all, folks! ;;;;;;;;; |