diff options
| author | Stefan Monnier | 2001-05-25 21:16:05 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2001-05-25 21:16:05 +0000 |
| commit | 4b8dfb4345dbcd6d2bd4f454d0b615011376d27f (patch) | |
| tree | aab8b31e2187f74dcea63df8810929c1edd8ebb0 | |
| parent | 74e87f23e41b53340035157ca57410067442d2ed (diff) | |
| download | emacs-4b8dfb4345dbcd6d2bd4f454d0b615011376d27f.tar.gz emacs-4b8dfb4345dbcd6d2bd4f454d0b615011376d27f.zip | |
(perl-mode): Use define-derived-mode.
(perl-comment-indent): Simplify to let newcomment.el do its job.
(perl-electric-terminator, perl-calculate-indent, perl-indent-exp)
(perl-mark-function): Cleanup the namespace.
(perl-calculate-indent): Don't be fooled by nested functions.
| -rw-r--r-- | lisp/progmodes/perl-mode.el | 150 |
1 files changed, 69 insertions, 81 deletions
diff --git a/lisp/progmodes/perl-mode.el b/lisp/progmodes/perl-mode.el index 189b11922aa..98d2035112a 100644 --- a/lisp/progmodes/perl-mode.el +++ b/lisp/progmodes/perl-mode.el | |||
| @@ -50,13 +50,13 @@ | |||
| 50 | ;; the line ends in a quoted string, or has a # which should be a \#. | 50 | ;; the line ends in a quoted string, or has a # which should be a \#. |
| 51 | 51 | ||
| 52 | ;; If your machine is slow, you may want to remove some of the bindings | 52 | ;; If your machine is slow, you may want to remove some of the bindings |
| 53 | ;; to electric-perl-terminator. I changed the indenting defaults to be | 53 | ;; to perl-electric-terminator. I changed the indenting defaults to be |
| 54 | ;; what Larry Wall uses in perl/lib, but left in all the options. | 54 | ;; what Larry Wall uses in perl/lib, but left in all the options. |
| 55 | 55 | ||
| 56 | ;; I also tuned a few things: comments and labels starting in column | 56 | ;; I also tuned a few things: comments and labels starting in column |
| 57 | ;; zero are left there by indent-perl-exp; perl-beginning-of-function | 57 | ;; zero are left there by perl-indent-exp; perl-beginning-of-function |
| 58 | ;; goes back to the first open brace/paren in column zero, the open brace | 58 | ;; goes back to the first open brace/paren in column zero, the open brace |
| 59 | ;; in 'sub ... {', or the equal sign in 'format ... ='; indent-perl-exp | 59 | ;; in 'sub ... {', or the equal sign in 'format ... ='; perl-indent-exp |
| 60 | ;; (meta-^q) indents from the current line through the close of the next | 60 | ;; (meta-^q) indents from the current line through the close of the next |
| 61 | ;; brace/paren, so you don't need to start exactly at a brace or paren. | 61 | ;; brace/paren, so you don't need to start exactly at a brace or paren. |
| 62 | 62 | ||
| @@ -89,9 +89,6 @@ | |||
| 89 | ;; | 89 | ;; |
| 90 | ;; /`/; $ugly = q?"'$?; /`/; | 90 | ;; /`/; $ugly = q?"'$?; /`/; |
| 91 | ;; | 91 | ;; |
| 92 | ;; To solve problem 6, add a /{/; before each use of ${var}: | ||
| 93 | ;; /{/; while (<${glob_me}>) ... | ||
| 94 | ;; | ||
| 95 | ;; Problem 7 is even worse, but this 'fix' does work :-( | 92 | ;; Problem 7 is even worse, but this 'fix' does work :-( |
| 96 | ;; $DB'stop#' | 93 | ;; $DB'stop#' |
| 97 | ;; [$DB'line#' | 94 | ;; [$DB'line#' |
| @@ -99,68 +96,60 @@ | |||
| 99 | 96 | ||
| 100 | ;;; Code: | 97 | ;;; Code: |
| 101 | 98 | ||
| 99 | (eval-when-compile (require 'cl)) | ||
| 100 | |||
| 102 | (defgroup perl nil | 101 | (defgroup perl nil |
| 103 | "Major mode for editing Perl code." | 102 | "Major mode for editing Perl code." |
| 104 | :prefix "perl-" | 103 | :prefix "perl-" |
| 105 | :group 'languages) | 104 | :group 'languages) |
| 106 | 105 | ||
| 107 | (defvar perl-mode-abbrev-table nil | 106 | (defvar perl-mode-map |
| 108 | "Abbrev table in use in perl-mode buffers.") | 107 | (let ((map (make-sparse-keymap))) |
| 109 | (define-abbrev-table 'perl-mode-abbrev-table ()) | 108 | (define-key map "{" 'perl-electric-terminator) |
| 110 | 109 | (define-key map "}" 'perl-electric-terminator) | |
| 111 | (defvar perl-mode-map () | 110 | (define-key map ";" 'perl-electric-terminator) |
| 112 | "Keymap used in Perl mode.") | 111 | (define-key map ":" 'perl-electric-terminator) |
| 113 | (if perl-mode-map | 112 | (define-key map "\e\C-a" 'perl-beginning-of-function) |
| 114 | () | 113 | (define-key map "\e\C-e" 'perl-end-of-function) |
| 115 | (setq perl-mode-map (make-sparse-keymap)) | 114 | (define-key map "\e\C-h" 'perl-mark-function) |
| 116 | (define-key perl-mode-map "{" 'electric-perl-terminator) | 115 | (define-key map "\e\C-q" 'perl-indent-exp) |
| 117 | (define-key perl-mode-map "}" 'electric-perl-terminator) | 116 | (define-key map "\177" 'backward-delete-char-untabify) |
| 118 | (define-key perl-mode-map ";" 'electric-perl-terminator) | 117 | (define-key map "\t" 'perl-indent-command) |
| 119 | (define-key perl-mode-map ":" 'electric-perl-terminator) | 118 | map) |
| 120 | (define-key perl-mode-map "\e\C-a" 'perl-beginning-of-function) | 119 | "Keymap used in `perl-mode'.") |
| 121 | (define-key perl-mode-map "\e\C-e" 'perl-end-of-function) | ||
| 122 | (define-key perl-mode-map "\e\C-h" 'mark-perl-function) | ||
| 123 | (define-key perl-mode-map "\e\C-q" 'indent-perl-exp) | ||
| 124 | (define-key perl-mode-map "\177" 'backward-delete-char-untabify) | ||
| 125 | (define-key perl-mode-map "\t" 'perl-indent-command)) | ||
| 126 | 120 | ||
| 127 | (autoload 'c-macro-expand "cmacexp" | 121 | (autoload 'c-macro-expand "cmacexp" |
| 128 | "Display the result of expanding all C macros occurring in the region. | 122 | "Display the result of expanding all C macros occurring in the region. |
| 129 | The expansion is entirely correct because it uses the C preprocessor." | 123 | The expansion is entirely correct because it uses the C preprocessor." |
| 130 | t) | 124 | t) |
| 131 | 125 | ||
| 132 | (defvar perl-mode-syntax-table nil | 126 | (defvar perl-mode-syntax-table |
| 133 | "Syntax table in use in perl-mode buffers.") | 127 | (let ((st (make-syntax-table (standard-syntax-table)))) |
| 134 | 128 | (modify-syntax-entry ?\n ">" st) | |
| 135 | (if perl-mode-syntax-table | 129 | (modify-syntax-entry ?# "<" st) |
| 136 | () | 130 | (modify-syntax-entry ?$ "/" st) |
| 137 | (setq perl-mode-syntax-table (make-syntax-table (standard-syntax-table))) | 131 | (modify-syntax-entry ?% "." st) |
| 138 | (modify-syntax-entry ?\n ">" perl-mode-syntax-table) | 132 | (modify-syntax-entry ?& "." st) |
| 139 | (modify-syntax-entry ?# "<" perl-mode-syntax-table) | 133 | (modify-syntax-entry ?\' "\"" st) |
| 140 | (modify-syntax-entry ?$ "/" perl-mode-syntax-table) | 134 | (modify-syntax-entry ?* "." st) |
| 141 | (modify-syntax-entry ?% "." perl-mode-syntax-table) | 135 | (modify-syntax-entry ?+ "." st) |
| 142 | (modify-syntax-entry ?& "." perl-mode-syntax-table) | 136 | (modify-syntax-entry ?- "." st) |
| 143 | (modify-syntax-entry ?\' "\"" perl-mode-syntax-table) | 137 | (modify-syntax-entry ?/ "." st) |
| 144 | (modify-syntax-entry ?* "." perl-mode-syntax-table) | 138 | (modify-syntax-entry ?< "." st) |
| 145 | (modify-syntax-entry ?+ "." perl-mode-syntax-table) | 139 | (modify-syntax-entry ?= "." st) |
| 146 | (modify-syntax-entry ?- "." perl-mode-syntax-table) | 140 | (modify-syntax-entry ?> "." st) |
| 147 | (modify-syntax-entry ?/ "." perl-mode-syntax-table) | 141 | (modify-syntax-entry ?\\ "\\" st) |
| 148 | (modify-syntax-entry ?< "." perl-mode-syntax-table) | 142 | (modify-syntax-entry ?` "\"" st) |
| 149 | (modify-syntax-entry ?= "." perl-mode-syntax-table) | 143 | (modify-syntax-entry ?| "." st) |
| 150 | (modify-syntax-entry ?> "." perl-mode-syntax-table) | 144 | st) |
| 151 | (modify-syntax-entry ?\\ "\\" perl-mode-syntax-table) | 145 | "Syntax table in use in `perl-mode' buffers.") |
| 152 | (modify-syntax-entry ?` "\"" perl-mode-syntax-table) | ||
| 153 | (modify-syntax-entry ?| "." perl-mode-syntax-table) | ||
| 154 | ) | ||
| 155 | 146 | ||
| 156 | (defvar perl-imenu-generic-expression | 147 | (defvar perl-imenu-generic-expression |
| 157 | '( | 148 | '(;; Functions |
| 158 | ;; Functions | ||
| 159 | (nil "^sub\\s-+\\([-A-Za-z0-9+_:]+\\)\\(\\s-\\|\n\\)*{" 1 ) | 149 | (nil "^sub\\s-+\\([-A-Za-z0-9+_:]+\\)\\(\\s-\\|\n\\)*{" 1 ) |
| 160 | ;;Variables | 150 | ;;Variables |
| 161 | ("Variables" "^\\([$@%][-A-Za-z0-9+_:]+\\)\\s-*=" 1 ) | 151 | ("Variables" "^\\([$@%][-A-Za-z0-9+_:]+\\)\\s-*=" 1 ) |
| 162 | ("Packages" "^package\\s-+\\([-A-Za-z0-9+_:]+\\);" 1 ) | 152 | ("Packages" "^package\\s-+\\([-A-Za-z0-9+_:]+\\);" 1 )) |
| 163 | ) | ||
| 164 | "Imenu generic expression for Perl mode. See `imenu-generic-expression'.") | 153 | "Imenu generic expression for Perl mode. See `imenu-generic-expression'.") |
| 165 | 154 | ||
| 166 | ;; Regexps updated with help from Tom Tromey <tromey@cambric.colorado.edu> and | 155 | ;; Regexps updated with help from Tom Tromey <tromey@cambric.colorado.edu> and |
| @@ -172,13 +161,13 @@ The expansion is entirely correct because it uses the C preprocessor." | |||
| 172 | ;; | 161 | ;; |
| 173 | ;; Fontify preprocessor statements as we do in `c-font-lock-keywords'. | 162 | ;; Fontify preprocessor statements as we do in `c-font-lock-keywords'. |
| 174 | ;; Ilya Zakharevich <ilya@math.ohio-state.edu> thinks this is a bad idea. | 163 | ;; Ilya Zakharevich <ilya@math.ohio-state.edu> thinks this is a bad idea. |
| 175 | ("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face) | 164 | ;; ("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face) |
| 176 | ("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face) | 165 | ;; ("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face) |
| 177 | ("^#[ \t]*if\\>" | 166 | ;; ("^#[ \t]*if\\>" |
| 178 | ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil | 167 | ;; ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil |
| 179 | (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t))) | 168 | ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t))) |
| 180 | ("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?" | 169 | ;; ("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?" |
| 181 | (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t)) | 170 | ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t)) |
| 182 | ;; | 171 | ;; |
| 183 | ;; Fontify function and package names in declarations. | 172 | ;; Fontify function and package names in declarations. |
| 184 | ("\\<\\(package\\|sub\\)\\>[ \t]*\\(\\sw+\\)?" | 173 | ("\\<\\(package\\|sub\\)\\>[ \t]*\\(\\sw+\\)?" |
| @@ -272,7 +261,7 @@ create a new comment." | |||
| 272 | :group 'perl) | 261 | :group 'perl) |
| 273 | 262 | ||
| 274 | ;;;###autoload | 263 | ;;;###autoload |
| 275 | (defun perl-mode () | 264 | (define-derived-mode perl-mode nil "Perl" |
| 276 | "Major mode for editing Perl code. | 265 | "Major mode for editing Perl code. |
| 277 | Expression and list commands understand all Perl brackets. | 266 | Expression and list commands understand all Perl brackets. |
| 278 | Tab indents for Perl code. | 267 | Tab indents for Perl code. |
| @@ -317,13 +306,6 @@ Various indentation styles: K&R BSD BLK GNU LW | |||
| 317 | perl-label-offset -5 -8 -2 -2 -2 | 306 | perl-label-offset -5 -8 -2 -2 -2 |
| 318 | 307 | ||
| 319 | Turning on Perl mode runs the normal hook `perl-mode-hook'." | 308 | Turning on Perl mode runs the normal hook `perl-mode-hook'." |
| 320 | (interactive) | ||
| 321 | (kill-all-local-variables) | ||
| 322 | (use-local-map perl-mode-map) | ||
| 323 | (setq major-mode 'perl-mode) | ||
| 324 | (setq mode-name "Perl") | ||
| 325 | (setq local-abbrev-table perl-mode-abbrev-table) | ||
| 326 | (set-syntax-table perl-mode-syntax-table) | ||
| 327 | (make-local-variable 'paragraph-start) | 309 | (make-local-variable 'paragraph-start) |
| 328 | (setq paragraph-start (concat "$\\|" page-delimiter)) | 310 | (setq paragraph-start (concat "$\\|" page-delimiter)) |
| 329 | (make-local-variable 'paragraph-separate) | 311 | (make-local-variable 'paragraph-separate) |
| @@ -353,8 +335,7 @@ Turning on Perl mode runs the normal hook `perl-mode-hook'." | |||
| 353 | ;; Tell imenu how to handle Perl. | 335 | ;; Tell imenu how to handle Perl. |
| 354 | (make-local-variable 'imenu-generic-expression) | 336 | (make-local-variable 'imenu-generic-expression) |
| 355 | (setq imenu-generic-expression perl-imenu-generic-expression) | 337 | (setq imenu-generic-expression perl-imenu-generic-expression) |
| 356 | (setq imenu-case-fold-search nil) | 338 | (setq imenu-case-fold-search nil)) |
| 357 | (run-hooks 'perl-mode-hook)) | ||
| 358 | 339 | ||
| 359 | ;; This is used by indent-for-comment | 340 | ;; This is used by indent-for-comment |
| 360 | ;; to decide how much to indent a comment in Perl code | 341 | ;; to decide how much to indent a comment in Perl code |
| @@ -362,14 +343,10 @@ Turning on Perl mode runs the normal hook `perl-mode-hook'." | |||
| 362 | (defun perl-comment-indent () | 343 | (defun perl-comment-indent () |
| 363 | (if (and (bolp) (not (eolp))) | 344 | (if (and (bolp) (not (eolp))) |
| 364 | 0 ;Existing comment at bol stays there. | 345 | 0 ;Existing comment at bol stays there. |
| 365 | (save-excursion | 346 | comment-column)) |
| 366 | (skip-chars-backward " \t") | 347 | |
| 367 | (max (if (bolp) ;Else indent at comment column | 348 | (defalias 'electric-perl-terminator 'perl-electric-terminator) |
| 368 | 0 ; except leave at least one space if | 349 | (defun perl-electric-terminator (arg) |
| 369 | (1+ (current-column))) ; not at beginning of line. | ||
| 370 | comment-column)))) | ||
| 371 | |||
| 372 | (defun electric-perl-terminator (arg) | ||
| 373 | "Insert character and adjust indentation. | 350 | "Insert character and adjust indentation. |
| 374 | If at end-of-line, and not in a comment or a quote, correct the's indentation." | 351 | If at end-of-line, and not in a comment or a quote, correct the's indentation." |
| 375 | (interactive "P") | 352 | (interactive "P") |
| @@ -482,7 +459,7 @@ changed by, or (parse-state) if line starts in a quoted string." | |||
| 482 | (setq beg (point)) | 459 | (setq beg (point)) |
| 483 | (setq shift-amt | 460 | (setq shift-amt |
| 484 | (cond ((= (char-after bof) ?=) 0) | 461 | (cond ((= (char-after bof) ?=) 0) |
| 485 | ((listp (setq indent (calculate-perl-indent bof))) indent) | 462 | ((listp (setq indent (perl-calculate-indent bof))) indent) |
| 486 | ((looking-at (or nochange perl-nochange)) 0) | 463 | ((looking-at (or nochange perl-nochange)) 0) |
| 487 | (t | 464 | (t |
| 488 | (skip-chars-forward " \t\f") | 465 | (skip-chars-forward " \t\f") |
| @@ -503,7 +480,7 @@ changed by, or (parse-state) if line starts in a quoted string." | |||
| 503 | (goto-char (- (point-max) pos))) | 480 | (goto-char (- (point-max) pos))) |
| 504 | shift-amt)) | 481 | shift-amt)) |
| 505 | 482 | ||
| 506 | (defun calculate-perl-indent (&optional parse-start) | 483 | (defun perl-calculate-indent (&optional parse-start) |
| 507 | "Return appropriate indentation for current line as Perl code. | 484 | "Return appropriate indentation for current line as Perl code. |
| 508 | In usual case returns an integer: the column to indent to. | 485 | In usual case returns an integer: the column to indent to. |
| 509 | Returns (parse-state) if line starts inside a string." | 486 | Returns (parse-state) if line starts inside a string." |
| @@ -516,6 +493,15 @@ Returns (parse-state) if line starts inside a string." | |||
| 516 | (if parse-start ;used to avoid searching | 493 | (if parse-start ;used to avoid searching |
| 517 | (goto-char parse-start) | 494 | (goto-char parse-start) |
| 518 | (perl-beginning-of-function)) | 495 | (perl-beginning-of-function)) |
| 496 | ;; We might be now looking at a local function that has nothing to | ||
| 497 | ;; do with us because `indent-point' is past it. In this case | ||
| 498 | ;; look further back up for another `perl-beginning-of-function'. | ||
| 499 | (while (and (looking-at "{") | ||
| 500 | (save-excursion | ||
| 501 | (beginning-of-line) | ||
| 502 | (looking-at "\\s-+sub\\>")) | ||
| 503 | (> indent-point (save-excursion (forward-sexp 1) (point)))) | ||
| 504 | (perl-beginning-of-function)) | ||
| 519 | (while (< (point) indent-point) ;repeat until right sexp | 505 | (while (< (point) indent-point) ;repeat until right sexp |
| 520 | (setq parse-start (point)) | 506 | (setq parse-start (point)) |
| 521 | (setq state (parse-partial-sexp (point) indent-point 0)) | 507 | (setq state (parse-partial-sexp (point) indent-point 0)) |
| @@ -643,7 +629,8 @@ Returns (parse-state) if line starts inside a string." | |||
| 643 | (skip-chars-forward " \t\f")) | 629 | (skip-chars-forward " \t\f")) |
| 644 | 630 | ||
| 645 | ;; note: this may be slower than the c-mode version, but I can understand it. | 631 | ;; note: this may be slower than the c-mode version, but I can understand it. |
| 646 | (defun indent-perl-exp () | 632 | (defalias 'indent-perl-exp 'perl-indent-exp) |
| 633 | (defun perl-indent-exp () | ||
| 647 | "Indent each line of the Perl grouping following point." | 634 | "Indent each line of the Perl grouping following point." |
| 648 | (interactive) | 635 | (interactive) |
| 649 | (let* ((case-fold-search nil) | 636 | (let* ((case-fold-search nil) |
| @@ -741,7 +728,8 @@ With argument, repeat that many times; negative args move backward." | |||
| 741 | (goto-char (point-min))))) | 728 | (goto-char (point-min))))) |
| 742 | (setq arg (1+ arg))))) | 729 | (setq arg (1+ arg))))) |
| 743 | 730 | ||
| 744 | (defun mark-perl-function () | 731 | (defalias 'mark-perl-function 'perl-mark-function) |
| 732 | (defun perl-mark-function () | ||
| 745 | "Put mark at end of Perl function, point at beginning." | 733 | "Put mark at end of Perl function, point at beginning." |
| 746 | (interactive) | 734 | (interactive) |
| 747 | (push-mark (point)) | 735 | (push-mark (point)) |