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