[comp.lang.perl] PERL mode for GNU Emacs

dovich@uunet.uu.net (Steven J. Dovich) (03/21/90)

Here is the Emacs-Lisp for editing Perl scripts. As I mentioned in my
prior announcement, it is derived from the c-mode.el from GNU Emacs,
so the copy-left applies (including the lack of warranty).

I have some ideas for further development, but would appreciate
contributions or suggestions.

/sjd


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	perl-mode.el
#
if test -f 'perl-mode.el'
then
	echo shar: will not over-write existing file "'perl-mode.el'"
else
	echo x - 'perl-mode.el'
	sed 's/^X//' >'perl-mode.el' << 'SHAR_EOF'
X;; Perl code editing commands for Emacs
X;;   Steven J. Dovich <uunet!cadence!gda!dovich>
X;;   Last Edit: Tue Mar 20 10:27:49 1990 by dovich
X;;
X;;   This mode is derived from the C mode implementation of Richard Stallman
X;;   Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
X;;
X;; Features include:
X;;   - Uses C mode style variables for consistency
X;;   - calls c-mode-hook at mode init
X
X;; This file is part of GNU Emacs.
X;;
X;; GNU Emacs is distributed in the hope that it will be useful,
X;; but WITHOUT ANY WARRANTY.  No author or distributor
X;; accepts responsibility to anyone for the consequences of using it
X;; or for whether it serves any particular purpose or works at all,
X;; unless he says so in writing.  Refer to the GNU Emacs General Public
X;; License for full details.
X;;
X;; Everyone is granted permission to copy, modify and redistribute
X;; GNU Emacs, but only under the conditions described in the
X;; GNU Emacs General Public License.   A copy of this license is
X;; supposed to have been given to you along with GNU Emacs so you
X;; can know your rights and responsibilities.  It should be in a
X;; file named COPYING.  Among other things, the copyright notice
X;; and this notice must be preserved on all copies.
X
X
X(defvar perl-mode-abbrev-table nil
X  "Abbrev table in use in Perl-mode buffers.")
X(define-abbrev-table 'perl-mode-abbrev-table ())
X
X(defvar perl-mode-map ()
X  "Keymap used in Perl mode.")
X
X(if perl-mode-map
X    ()
X  (setq perl-mode-map (make-sparse-keymap))
X  (define-key perl-mode-map "{" 'electric-perl-brace)
X  (define-key perl-mode-map "}" 'electric-perl-brace)
X  (define-key perl-mode-map ";" 'electric-perl-semi)
X  (define-key perl-mode-map ":" 'electric-perl-terminator)
X  (define-key perl-mode-map "\e\C-h" 'mark-perl-function)
X  (define-key perl-mode-map "\e\C-q" 'indent-perl-exp)
X  (define-key perl-mode-map "\177" 'backward-delete-char-untabify)
X  (define-key perl-mode-map "\t" 'perl-indent-command))
X
X(defvar perl-mode-syntax-table nil
X  "Syntax table in use in Perl-mode buffers.")
X
X(if perl-mode-syntax-table
X    ()
X  (setq perl-mode-syntax-table (make-syntax-table c-mode-syntax-table))
X  (modify-syntax-entry ?/ "." perl-mode-syntax-table)
X  (modify-syntax-entry ?* "." perl-mode-syntax-table)
X  (modify-syntax-entry ?# "<" perl-mode-syntax-table)
X  (modify-syntax-entry ?\n ">" perl-mode-syntax-table)
X  (modify-syntax-entry ?\f ">" perl-mode-syntax-table)
X  (modify-syntax-entry ?@ "_" perl-mode-syntax-table)
X
X  ;; The "$" character is the beginning of a user symbol in the Perl
X  ;; language.  It escapes subsequent characters into a symbol
X  ;; consituent (including "#", the comment start character), but only
X  ;; if it starts the symbol. The "@" is similar, but doesn't escape
X  ;; the "#" char (yet...) 
X  ;;
X  ;; A "%" (which begins a reference to an associative array) is like
X  ;; "@", but it is also an operator class character.
X  ;;
X  ;; This stuff could be handled better (maybe in v19?)
X
X  (modify-syntax-entry ?$ "\\" perl-mode-syntax-table))  ;; change for v19 (?)
X	       
X(defconst c-indent-level 2
X  "*Indentation of C statements with respect to containing block.")
X(defconst c-brace-imaginary-offset 0
X  "*Imagined indentation of a C open brace that actually follows a statement.")
X(defconst c-brace-offset 0
X  "*Extra indentation for braces, compared with other text in same context.")
X(defconst c-argdecl-indent 5
X  "*Indentation level of declarations of C function arguments.")
X(defconst c-label-offset -2
X  "*Offset of C label lines and case statements relative to usual indentation.")
X(defconst c-continued-statement-offset 2
X  "*Extra indent for lines not starting new statements.")
X(defconst c-continued-brace-offset 0
X  "*Extra indent for substatements that start with open-braces.
XThis is in addition to c-continued-statement-offset.")
X
X(defconst c-auto-newline nil
X  "*Non-nil means automatically newline before and after braces,
Xand after colons and semicolons, inserted in C code.")
X
X(defconst c-tab-always-indent t
X  "*Non-nil means TAB in C mode should always reindent the current line,
Xregardless of where in the line point is when the TAB command is used.")
X
X(defun perl-mode ()
X  "Major mode for editing Perl code.
XExpression and list commands understand all Perl brackets.
XTab indents for Perl code.
XComments are delimited with # ... (newline).
XParagraphs are separated by blank lines only.
XDelete converts tabs to spaces as it moves back.
X\\{perl-mode-map}
XVariables controlling indentation style:
X c-tab-always-indent
X    Non-nil means TAB in Perl mode should always reindent the current line,
X    regardless of where in the line point is when the TAB command is used.
X c-auto-newline
X    Non-nil means automatically newline before and after braces,
X    and after colons and semicolons, inserted in Perl code.
X c-indent-level
X    Indentation of Perl statements within surrounding block.
X    The surrounding block's indentation is the indentation
X    of the line on which the open-brace appears.
X c-continued-statement-offset
X    Extra indentation given to a substatement, such as the
X    then-clause of an if or body of a while.
X c-continued-brace-offset
X    Extra indentation given to a brace that starts a substatement.
X    This is in addition to c-continued-statement-offset.
X c-brace-offset
X    Extra indentation for line if it starts with an open brace.
X c-brace-imaginary-offset
X    An open brace following other text is treated as if it were
X    this far to the right of the start of its line.
X c-argdecl-indent
X    Indentation level of declarations of Perl function arguments.
X c-label-offset
X    Extra indentation for line that is a label, or case or default.
X       
XSettings for K&R and BSD indentation styles are
X c-indent-level                5    8
X c-continued-statement-offset  5    8
X c-brace-offset               -5   -8
X c-argdecl-indent              0    8
X c-label-offset               -5   -8
X       
XTurning on Perl mode calls the value of the variable perl-mode-hook with no args,
Xif that value is non-nil."
X  (interactive)
X  (kill-all-local-variables)
X  (use-local-map perl-mode-map)
X  (setq major-mode 'perl-mode)
X  (setq mode-name "Perl")
X  (setq local-abbrev-table perl-mode-abbrev-table)
X  (set-syntax-table perl-mode-syntax-table)
X  (make-local-variable 'paragraph-start)
X  (setq paragraph-start (concat "^$\\|" page-delimiter))
X  (make-local-variable 'paragraph-separate)
X  (setq paragraph-separate paragraph-start)
X  (make-local-variable 'indent-line-function)
X  (setq indent-line-function 'perl-indent-line)
X  (make-local-variable 'require-final-newline)
X  (setq require-final-newline t)
X  (make-local-variable 'comment-start)
X  (setq comment-start "# ")
X  (make-local-variable 'comment-end)
X  (setq comment-end "")
X  (make-local-variable 'comment-column)
X  (setq comment-column 32)
X  (make-local-variable 'comment-start-skip)
X  (setq comment-start-skip "^#+ *\\|[^$]#+ *")
X  (make-local-variable 'comment-indent-hook)
X  (setq comment-indent-hook 'perl-comment-indent)
X  (make-local-variable 'parse-sexp-ignore-comments)
X  (setq parse-sexp-ignore-comments t)
X  (run-hooks 'c-mode-hook 'perl-mode-hook))
X
X;; This is used by indent-for-comment
X;; to decide how much to indent a comment in Perl code
X;; based on its context.
X(defun perl-comment-indent ()
X  (if (looking-at "^# ")
X      0				;Existing comment at bol stays there.
X    (save-excursion
X      (skip-chars-backward " \t")
X      (max (1+ (current-column))	;Else indent at comment column
X	   comment-column))))	; except leave at least one space.
X
X(defun electric-perl-brace (arg)
X  "Insert character and correct line's indentation."
X  (interactive "P")
X  (let (insertpos)
X    (if (and (not arg)
X	     (eolp)
X	     (or (save-excursion
X		   (skip-chars-backward " \t")
X		   (bolp))
X		 (if c-auto-newline (progn (perl-indent-line) (newline) t) nil)))
X	(progn
X	  (insert last-command-char)
X	  (perl-indent-line)
X	  (if c-auto-newline
X	      (progn
X		(newline)
X		;; (newline) may have done auto-fill
X		(setq insertpos (- (point) 2))
X		(perl-indent-line)))
X	  (save-excursion
X	    (if insertpos (goto-char (1+ insertpos)))
X	    (delete-char -1))))
X    (if insertpos
X	(save-excursion
X	  (goto-char insertpos)
X	  (self-insert-command (prefix-numeric-value arg)))
X      (self-insert-command (prefix-numeric-value arg)))))
X
X(defun electric-perl-semi (arg)
X  "Insert character and correct line's indentation."
X  (interactive "P")
X  (if c-auto-newline
X      (electric-perl-terminator arg)
X    (self-insert-command (prefix-numeric-value arg))))
X
X(defun electric-perl-terminator (arg)
X  "Insert character and correct line's indentation."
X  (interactive "P")
X  (let (insertpos (end (point)))
X    (if (and (not arg) (eolp)
X	     (not (save-excursion
X		    (beginning-of-line)
X		    (skip-chars-forward " \t")
X		    (or (= (following-char) ?#)
X			;; Colon is special only after a label, or case ....
X			;; So quickly rule out most other uses of colon
X			;; and do no indentation for them.
X			(and (eq last-command-char ?:)
X			     (not (looking-at "case"))
X			     (save-excursion
X			       (forward-word 2)
X			       (<= (point) end)))
X			(progn
X			  (beginning-of-defun)
X			  (let ((pps (parse-partial-sexp (point) end)))
X			    (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
X	(progn
X	  (insert last-command-char)
X	  (perl-indent-line)
X	  (and c-auto-newline
X	       (not (perl-inside-parens-p))
X	       (progn
X		 (newline)
X		 (setq insertpos (- (point) 2))
X		 (perl-indent-line)))
X	  (save-excursion
X	    (if insertpos (goto-char (1+ insertpos)))
X	    (delete-char -1))))
X    (if insertpos
X	(save-excursion
X	  (goto-char insertpos)
X	  (self-insert-command (prefix-numeric-value arg)))
X      (self-insert-command (prefix-numeric-value arg)))))
X
X(defun perl-inside-parens-p ()
X  (condition-case ()
X      (save-excursion
X	(save-restriction
X	  (narrow-to-region (point)
X			    (progn (beginning-of-defun) (point)))
X	  (goto-char (point-max))
X	  (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
X    (error nil)))
X
X(defun perl-indent-command (&optional whole-exp)
X  (interactive "P")
X  "Indent current line as Perl code, or in some cases insert a tab character.
XIf c-tab-always-indent is non-nil (the default), always indent current line.
XOtherwise, indent the current line only if point is at the left margin
Xor in the line's indentation; otherwise insert a tab.
X  
XA numeric argument, regardless of its value,
Xmeans indent rigidly all the lines of the expression starting after point
Xso that this line becomes properly indented.
XThe relative indentation among the lines of the expression are preserved."
X  (if whole-exp
X      ;; If arg, always indent this line as C
X      ;; and shift remaining lines of expression the same amount.
X      (let ((shift-amt (perl-indent-line))
X	    beg end)
X	(save-excursion
X	  (if c-tab-always-indent
X	      (beginning-of-line))
X	  (setq beg (point))
X	  (forward-sexp 1)
X	  (setq end (point))
X	  (goto-char beg)
X	  (forward-line 1)
X	  (setq beg (point)))
X	(if (> end beg)
X	    (indent-code-rigidly beg end shift-amt "#")))
X    (if (and (not c-tab-always-indent)
X	     (save-excursion
X	       (skip-chars-backward " \t")
X	       (not (bolp))))
X	(insert-tab)
X      (perl-indent-line))))
X
X(defun perl-indent-line ()
X  "Indent current line as Perl code.
XReturn the amount the indentation changed by."
X  (let ((indent (calculate-perl-indent nil))
X	beg shift-amt
X	(case-fold-search nil)
X	(pos (- (point-max) (point))))
X    (beginning-of-line)
X    (setq beg (point))
X    (cond ((eq indent nil)
X	   (setq indent (current-indentation)))
X	  ((eq indent t)
X	   (setq indent (calculate-perl-indent-within-comment)))
X	  ((looking-at "[ \t]*#(define|include|ifn?def|else|end|if)")
X	   (setq indent 0))
X	  (t
X	    (skip-chars-forward " \t")
X	    (if (listp indent) (setq indent (car indent)))
X	    (cond ((or (looking-at "case\\b")
X		       (and (looking-at "[A-Za-z]")
X			    (save-excursion
X			      (forward-sexp 1)
X			      (looking-at ":"))))
X		   (setq indent (max 1 (+ indent c-label-offset))))
X		  ((and (looking-at "(else|elsif)\\b")
X			(not (looking-at "(else|elsif)\\s_")))
X		   (setq indent (save-excursion
X				  (perl-backward-to-start-of-if)
X				  (current-indentation))))
X		  ((= (following-char) ?})
X		   (setq indent (- indent c-indent-level)))
X		  ((= (following-char) ?{)
X		   (setq indent (+ indent c-brace-offset))))))
X    (skip-chars-forward " \t")
X    (setq shift-amt (- indent (current-column)))
X    (if (zerop shift-amt)
X	(if (> (- (point-max) pos) (point))
X	    (goto-char (- (point-max) pos)))
X      (delete-region beg (point))
X      (indent-to indent)
X      ;; If initial point was within line's indentation,
X      ;; position after the indentation.  Else stay at same point in text.
X      (if (> (- (point-max) pos) (point))
X	  (goto-char (- (point-max) pos))))
X    shift-amt))
X
X(defun calculate-perl-indent (&optional parse-start)
X  "Return appropriate indentation for current line as Perl code.
XIn usual case returns an integer: the column to indent to.
XReturns nil if line starts inside a string, t if in a comment."
X  (save-excursion
X    (beginning-of-line)
X    (let ((indent-point (point))
X	  (case-fold-search nil)
X	  state
X	  containing-sexp)
X      (if parse-start
X	  (goto-char parse-start)
X	(beginning-of-defun))
X      (while (< (point) indent-point)
X	(setq parse-start (point))
X	(setq state (parse-partial-sexp (point) indent-point 0))
X	(setq containing-sexp (car (cdr state))))
X      (cond ((or (nth 3 state) (nth 4 state))
X	     ;; return nil or t if should not change this line
X	     (nth 4 state))
X	    ((null containing-sexp)
X	     ;; Line is at top level.  May be data or function definition,
X	     ;; or may be function argument declaration.
X	     ;; Indent like the previous top level line
X	     ;; unless that ends in a closeparen without semicolon,
X	     ;; in which case this line is the first argument decl.
X	     (goto-char indent-point)
X	     (skip-chars-forward " \t")
X	     (if (= (following-char) ?{)
X		 0   ; Unless it starts a function body
X	       (perl-backward-to-noncomment (or parse-start (point-min)))
X	       ;; Look at previous line that's at column 0
X	       ;; to determine whether we are in top-level decls
X	       ;; or function's arg decls.  Set basic-indent accordinglu.
X	       (let ((basic-indent
X		       (save-excursion
X			 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
X			 (if (and (looking-at "\\sw\\|\\s_")
X				  (looking-at ".*(")
X				  (progn
X				    (goto-char (1- (match-end 0)))
X				    (forward-sexp 1)
X				    (and (< (point) indent-point)
X					 (not (memq (following-char)
X						    '(?\, ?\;))))))
X			     c-argdecl-indent 0))))
X		 ;; Now add a little if this is a continuation line.
X		 (+ basic-indent (if (or (bobp)
X					 (memq (preceding-char) '(?\) ?\; ?\})))
X				     0 c-continued-statement-offset)))))
X	    ((/= (char-after containing-sexp) ?{)
X	     ;; line is expression, not statement:
X	     ;; indent to just after the surrounding open.
X	     (goto-char (1+ containing-sexp))
X	     (current-column))
X	    (t
X	      ;; Statement level.  Is it a continuation or a new statement?
X	      ;; Find previous non-comment character.
X	      (goto-char indent-point)
X	      (perl-backward-to-noncomment containing-sexp)
X	      ;; Back up over label lines, since they don't
X	      ;; affect whether our line is a continuation.
X	      (while (or (eq (preceding-char) ?\,)
X			 (and (eq (preceding-char) ?:)
X			      (or (eq (char-after (- (point) 2)) ?\')
X				  (memq (char-syntax (char-after (- (point) 2)))
X					'(?w ?_)))))
X		(if (eq (preceding-char) ?\,)
X		    (perl-backward-to-start-of-continued-exp containing-sexp))
X		(beginning-of-line)
X		(perl-backward-to-noncomment containing-sexp))
X	      ;; Now we get the answer.
X	      (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{)))
X		  ;; This line is continuation of preceding line's statement;
X		  ;; indent  c-continued-statement-offset  more than the
X		  ;; previous line of the statement.
X		  (progn
X		    (perl-backward-to-start-of-continued-exp containing-sexp)
X		    (+ c-continued-statement-offset (current-column)
X		       (if (save-excursion (goto-char indent-point)
X					   (skip-chars-forward " \t")
X					   (eq (following-char) ?{))
X			   c-continued-brace-offset 0)))
X		;; This line starts a new statement.
X		;; Position following last unclosed open.
X		(goto-char containing-sexp)
X		;; Is line first statement after an open-brace?
X		(or
X		  ;; If no, find that first statement and indent like it.
X		  (save-excursion
X		    (forward-char 1)
X		    (let ((colon-line-end 0))
X		      (while (progn (skip-chars-forward " \t\n")
X				    (looking-at "#\\|case[ \t\n].*:\\|[a-zA-Z0-9_$]*:"))
X			;; Skip over comments and labels following openbrace.
X			(cond ((= (following-char) ?\#)
X			       (forward-line 1))
X			      ;; case or label:
X			      (t
X				(save-excursion (end-of-line)
X						(setq colon-line-end (point)))
X				(search-forward ":"))))
X		      ;; The first following code counts
X		      ;; if it is before the line we want to indent.
X		      (and (< (point) indent-point)
X			   (if (> colon-line-end (point))
X			       (- (current-indentation) c-label-offset)
X			     (current-column)))))
X		  ;; If no previous statement,
X		  ;; indent it relative to line brace is on.
X		  ;; For open brace in column zero, don't let statement
X		  ;; start there too.  If c-indent-level is zero,
X		  ;; use c-brace-offset + c-continued-statement-offset instead.
X		  ;; For open-braces not the first thing in a line,
X		  ;; add in c-brace-imaginary-offset.
X		  (+ (if (and (bolp) (zerop c-indent-level))
X			 (+ c-brace-offset c-continued-statement-offset)
X		       c-indent-level)
X		     ;; Move back over whitespace before the openbrace.
X		     ;; If openbrace is not first nonwhite thing on the line,
X		     ;; add the c-brace-imaginary-offset.
X		     (progn (skip-chars-backward " \t")
X			    (if (bolp) 0 c-brace-imaginary-offset))
X		     ;; If the openbrace is preceded by a parenthesized exp,
X		     ;; move to the beginning of that;
X		     ;; possibly a different line
X		     (progn
X		       (if (eq (preceding-char) ?\))
X			   (forward-sexp -1))
X		       ;; Get initial indentation of the line we are on.
X		       (current-indentation))))))))))
X
X(defun calculate-perl-indent-within-comment ()
X  "Return the indentation amount for line, assuming that
Xthe current line is to be regarded as part of a block comment."
X  (let (end star-start)
X    (save-excursion
X      (beginning-of-line)
X      (skip-chars-forward " \t")
X      (setq star-start (= (following-char) ?\*))
X      (skip-chars-backward " \t\n")
X      (setq end (point))
X      (beginning-of-line)
X      (skip-chars-forward " \t")
X      (and (re-search-forward "#[ \t]*" end t)
X	   star-start
X	   (goto-char (1+ (match-beginning 0))))
X      (current-column))))
X
X
X(defun perl-backward-to-noncomment (lim)
X  (let (opoint stop)
X    (while (not stop)
X      (skip-chars-backward " \t\n\f" lim)
X      (setq opoint (point))
X      (if (and (>= (point) (+ 2 lim))
X	       (save-excursion
X		 (forward-char -2)
X		 (looking-at "$")))
X	  (search-backward "#" lim 'move)
X	(beginning-of-line)
X	(skip-chars-forward " \t")
X	(setq stop (or (not (looking-at "#")) (<= (point) lim)))
X	(if stop (goto-char opoint)
X	  (beginning-of-line))))))
X
X(defun perl-backward-to-start-of-continued-exp (lim)
X  (if (= (preceding-char) ?\))
X      (forward-sexp -1))
X  (beginning-of-line)
X  (if (<= (point) lim)
X      (goto-char (1+ lim)))
X  (skip-chars-forward " \t"))
X
X(defun perl-backward-to-start-of-if (&optional limit)
X  "Move to the start of the last ``unbalanced'' if."
X  (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
X  (let ((if-level 1)
X	(case-fold-search nil))
X    (while (not (zerop if-level))
X      (backward-sexp 1)
X      (cond ((looking-at "(else|elsif)\\b")
X	     (setq if-level (1+ if-level)))
X	    ((looking-at "if\\b")
X	     (setq if-level (1- if-level)))
X	    ((< (point) limit)
X	     (setq if-level 0)
X	     (goto-char limit))))))
X
X
X(defun mark-perl-function ()
X  "Put mark at end of Perl function, point at beginning."
X  (interactive)
X  (push-mark (point))
X  (end-of-defun)
X  (push-mark (point))
X  (beginning-of-defun)
X  (backward-paragraph))
X
X(defun indent-perl-exp ()
X  "Indent each line of the Perl grouping following point."
X  (interactive)
X  (let ((indent-stack (list nil))
X	(contain-stack (list (point)))
X	(case-fold-search nil)
X	restart outer-loop-done inner-loop-done state ostate
X	this-indent last-sexp
X	at-else at-brace
X	(opoint (point))
X	(next-depth 0))
X    (save-excursion
X      (forward-sexp 1))
X    (save-excursion
X      (setq outer-loop-done nil)
X      (while (and (not (eobp)) (not outer-loop-done))
X	(setq last-depth next-depth)
X	;; Compute how depth changes over this line
X	;; plus enough other lines to get to one that
X	;; does not end inside a comment or string.
X	;; Meanwhile, do appropriate indentation on comment lines.
X	(setq innerloop-done nil)
X	(while (and (not innerloop-done)
X		    (not (and (eobp) (setq outer-loop-done t))))
X	  (setq ostate state)
X	  (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
X					  nil nil state))
X	  (setq next-depth (car state))
X	  (if (and (car (cdr (cdr state)))
X		   (>= (car (cdr (cdr state))) 0))
X	      (setq last-sexp (car (cdr (cdr state)))))
X	  (if (or (nth 4 ostate))
X	      (perl-indent-line))
X	  (if (or (nth 3 state))
X	      (forward-line 1)
X	    (setq innerloop-done t)))
X	(if (<= next-depth 0)
X	    (setq outer-loop-done t))
X	(if outer-loop-done
X	    nil
X	  (if (/= last-depth next-depth)
X	      (setq last-sexp nil))
X	  (while (> last-depth next-depth)
X	    (setq indent-stack (cdr indent-stack)
X		  contain-stack (cdr contain-stack)
X		  last-depth (1- last-depth)))
X	  (while (< last-depth next-depth)
X	    (setq indent-stack (cons nil indent-stack)
X		  contain-stack (cons nil contain-stack)
X		  last-depth (1+ last-depth)))
X	  (if (null (car contain-stack))
X	      (setcar contain-stack (or (car (cdr state))
X					(save-excursion (forward-sexp -1)
X							(point)))))
X	  (forward-line 1)
X	  (skip-chars-forward " \t")
X	  (if (eolp)
X	      nil
X	    (if (and (car indent-stack)
X		     (>= (car indent-stack) 0))
X		;; Line is on an existing nesting level.
X		;; Lines inside parens are handled specially.
X		(if (/= (char-after (car contain-stack)) ?{)
X		    (setq this-indent (car indent-stack))
X		  ;; Line is at statement level.
X		  ;; Is it a new statement?  Is it an else?
X		  ;; Find last non-comment character before this line
X		  (save-excursion
X		    (setq at-else (looking-at "(else|elsif)\\W"))
X		    (setq at-brace (= (following-char) ?{))
X		    (perl-backward-to-noncomment opoint)
X		    (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{)))
X			;; Preceding line did not end in comma or semi;
X			;; indent this line  c-continued-statement-offset
X			;; more than previous.
X			(progn
X			  (perl-backward-to-start-of-continued-exp (car contain-stack))
X			  (setq this-indent
X				(+ c-continued-statement-offset (current-column)
X				   (if at-brace c-continued-brace-offset 0))))
X		      ;; Preceding line ended in comma or semi;
X		      ;; use the standard indent for this level.
X		      (if at-else
X			  (progn (perl-backward-to-start-of-if opoint)
X				 (setq this-indent (current-indentation)))
X			(setq this-indent (car indent-stack))))))
X	      ;; Just started a new nesting level.
X	      ;; Compute the standard indent for this level.
X	      (let ((val (calculate-perl-indent
X			   (if (car indent-stack)
X			       (- (car indent-stack))))))
X		(setcar indent-stack
X			(setq this-indent val))))
X	    ;; Adjust line indentation according to its contents
X	    (if (or (looking-at "case[ \t]")
X		    (and (looking-at "[A-Za-z]")
X			 (save-excursion
X			   (forward-sexp 1)
X			   (looking-at ":"))))
X		(setq this-indent (max 1 (+ this-indent c-label-offset))))
X	    (if (= (following-char) ?})
X		(setq this-indent (- this-indent c-indent-level)))
X	    (if (= (following-char) ?{)
X		(setq this-indent (+ this-indent c-brace-offset)))
X	    ;; Put chosen indentation into effect.
X	    (or (= (current-column) this-indent)
X		(= (following-char) ?\#)
X		(progn
X		  (delete-region (point) (progn (beginning-of-line) (point)))
X		  (indent-to this-indent)))
X	    ;; Indent any comment following the text.
X	    (or (looking-at comment-start-skip)
X		(if (re-search-forward
X		      comment-start-skip
X		      (save-excursion (end-of-line) (point))
X		      t)
X		    (progn (indent-for-comment) (beginning-of-line)))))))))
X  ; (message "Indenting Perl expression...done")
X  )
SHAR_EOF
if test 24500 -ne "`wc -c < 'perl-mode.el'`"
then
	echo shar: error transmitting "'perl-mode.el'" '(should have been 24500 characters)'
fi
fi
echo Done
exit 0

--
Steven J. Dovich                        <uunet.uu.net!cadence!gda!dovich>
Cadence Design Systems
2 Lowell Research Center Dr.
Lowell, MA  01852-4995                  Phone: (508) 458-1900 x272