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