home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / gnu / emacs / sources / 822 < prev    next >
Encoding:
Text File  |  1992-11-23  |  25.7 KB  |  687 lines

  1. Newsgroups: gnu.emacs.sources
  2. Path: sparky!uunet!psinntp!ctp!ebail
  3. From: ebail@ctp.com (Erik Bailey)
  4. Subject: Re: cs-mode.el
  5. Message-ID: <1992Nov23.140304.21769@ctp.com>
  6. Keywords: cstar, emacs major mode
  7. Sender: news@ctp.com
  8. Nntp-Posting-Host: earth.ctp.com
  9. Organization: Cambridge Technology Partners
  10. References: <1ehse2INNk2e@crcnis1.unl.edu>
  11. Date: Mon, 23 Nov 1992 14:03:04 GMT
  12. Lines: 673
  13.  
  14. In article <1ehse2INNk2e@crcnis1.unl.edu> stevew@helios.unl.edu (Steve Wu) writes:
  15. >  Does anyone have cs-mode.el or something like that which I can get
  16. >it by any means? I am working on c* and I wish someone has written the
  17. >cs major mode. Thanks for  you help
  18.  
  19. ;; C* (Connection Machine C) mode for Emacs
  20.  
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;; This file is a derivative of the c-mode file provided with GNU Emacs.
  23. ;; All changes by Leban@cs.umass.edu (C) Copyright 1990 Bruce Leban
  24. ;; under the exact same terms as the GNU public license agreement.
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. ;;
  27. ;; SUMMARY: C* mode knows about the syntax of C* version 6.0:
  28. ''
  29. ;; * It knows how to distinguish the `:' in a label from the `:' which
  30. ;;   precedes a shape designator.
  31. ;; * It recognizes the `where' keyword.
  32. ;;
  33. ;; OTHER CHANGES:
  34. ;;
  35. ;; * The electric `:' character was changed to reindent only for case labels.
  36. ;; * The newline character was made electric.  It reindents a line if `:' is
  37. ;;   the last character (electric-c*-newline)
  38. ;; * Added a variable `c-newline-indents' which controls whether or not
  39. ;;   newline automatically indents the next line.  (This is similar to
  40. ;;   rebinding newline to `newline-and-indent' except it doesn't conflict
  41. ;;   with the previous change.)
  42. ;; * The `#' character was made electric.  It reindents a line when `#' is
  43. ;;   the first character.  (electric-c*-pound)
  44. ;; * More graceful error handling when a missing if or unbalanced braces
  45. ;;   are encountered. (c*-backward-to-start-of-if)
  46. ;; * The c-newline-indents variable mentioned above.
  47. ;; * Changed "case[ \t]" to "case\\b" throughout (bug fix).
  48. ;; * Fixed a bug which misindented lines like ``case (...):''.
  49. ;; * Fixed a bug that misindented the first line of code if the first line
  50. ;;   of the file started with a # (fairly common on .h files).
  51. ;; * Occurrences of ?{ and ?} in the code were changed to ?\{ and ?\} to
  52. ;;   make editing easier.
  53. ;; NOTE: most of the other changes above should also be applied to c-mode.
  54.  
  55. ;; The electric colon was changed to reindent the line only used when the
  56. ;; line starts with the word `case'.  Correspondingly, an electric newline
  57. ;; was added to reindent the line if the last character was a colon (since
  58. ;; it's probably a label).  A variable c-newline-indents controls whether 
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60. ;; The fine print: C* mode believes a regular label is a line that starts
  61. ;; with a symbol followed by a colon and a whitespace character or the end of
  62. ;; line.  A case label is identified by the word `case' at the start of a
  63. ;; line.
  64. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65. ;; This file requires c-mode to be loaded in order to work.  It appears to
  66. ;; be loaded by default in some (all?) Emacs.  If c-mode is not loaded when
  67. ;; this file is loaded, it is loaded first.
  68. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  69. ;; Most changes are marked by ;;C* for C* changes and ;;BPL otherwise.
  70.  
  71. ;; This file is *not* part of GNU Emacs (although c-mode is).
  72.  
  73. ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  74.  
  75. ;; GNU Emacs is distributed in the hope that it will be useful,
  76. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  77. ;; accepts responsibility to anyone for the consequences of using it
  78. ;; or for whether it serves any particular purpose or works at all,
  79. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  80. ;; License for full details.
  81.  
  82. ;; Everyone is granted permission to copy, modify and redistribute
  83. ;; GNU Emacs, but only under the conditions described in the
  84. ;; GNU Emacs General Public License.   A copy of this license is
  85. ;; supposed to have been given to you along with GNU Emacs so you
  86. ;; can know your rights and responsibilities.  It should be in a
  87. ;; file named COPYING.  Among other things, the copyright notice
  88. ;; and this notice must be preserved on all copies.
  89.  
  90.  
  91. (provide 'c*-mode)
  92. (provide 'cstar-mode)            ;because many things call it that
  93.                     ;-mjab Fri Feb 22 13:22:44 1991
  94.  
  95. ; (require 'c-mode) ... doesn't work so ...
  96. (if (eq 'autoload (car (symbol-function 'c-mode)))
  97.     (load (car (cdr (symbol-function 'c-mode)))))
  98.  
  99. (defconst c-newline-indents nil
  100.   "*Non-nil means make newline act like newline-and-indent.")
  101.  
  102. (defvar c*-mode-abbrev-table nil
  103.   "Abbrev table in use in c*-mode buffers.")
  104.  
  105. (define-abbrev-table 'c*-mode-abbrev-table ())
  106.  
  107. (defvar c*-mode-map ()
  108.   "Keymap used in C mode.")
  109. (if c*-mode-map
  110.     ()
  111.   (setq c*-mode-map (make-sparse-keymap))
  112.   (define-key c*-mode-map "\r" 'electric-c*-newline)
  113.   (define-key c*-mode-map "{" 'electric-c*-brace)
  114.   (define-key c*-mode-map "}" 'electric-c*-brace)
  115.   (define-key c*-mode-map "#" 'electric-c*-pound)
  116.   (define-key c*-mode-map ";" 'electric-c*-semi)
  117.   (define-key c*-mode-map ":" 'electric-c*-colon)
  118.   (define-key c*-mode-map "\e\C-h" 'mark-c-function)
  119.   (define-key c*-mode-map "\e\C-q" 'indent-c*-exp)
  120.   (define-key c*-mode-map "\177" 'backward-delete-char-untabify)
  121.   (define-key c*-mode-map "\t" 'c*-indent-command))
  122.  
  123. (defvar c*-mode-syntax-table nil
  124.   "Syntax table in use in c*-mode buffers.")
  125.  
  126. (if c*-mode-syntax-table
  127.     ()
  128.   (setq c*-mode-syntax-table (make-syntax-table))
  129.   (modify-syntax-entry ?\\ "\\" c*-mode-syntax-table)
  130.   (modify-syntax-entry ?/ ". 14" c*-mode-syntax-table)
  131.   (modify-syntax-entry ?* ". 23" c*-mode-syntax-table)
  132.   (modify-syntax-entry ?+ "." c*-mode-syntax-table)
  133.   (modify-syntax-entry ?- "." c*-mode-syntax-table)
  134.   (modify-syntax-entry ?= "." c*-mode-syntax-table)
  135.   (modify-syntax-entry ?% "." c*-mode-syntax-table)
  136.   (modify-syntax-entry ?< "." c*-mode-syntax-table)
  137.   (modify-syntax-entry ?> "." c*-mode-syntax-table)
  138.   (modify-syntax-entry ?& "." c*-mode-syntax-table)
  139.   (modify-syntax-entry ?| "." c*-mode-syntax-table)
  140.   (modify-syntax-entry ?\' "\"" c*-mode-syntax-table))
  141.  
  142. (defun cstar-mode ()            ;so things like auto-modes will
  143.   (interactive)                ;work -mjab Fri Feb 22 13:26:26 1991
  144.   (c*-mode))
  145.  
  146. (defun c*-mode ()
  147.   "Major mode for editing C* code.
  148. \\{c-mode-map}
  149. This is the same as C mode except it understands about C* syntax.  It
  150. knows about the where statement and the difference between labels
  151. and shape declarations.
  152.    case 3:
  153. is indented like a label
  154.    int:current a;
  155. is indented like a statement.  It does not automatically reindent the line
  156. when it sees a trailing colon (i.e., after you type int:).
  157.  
  158. The variables controlling indentation style are those used for C mode.
  159. Set c-indent-level, not c*-indent-level.
  160.  
  161. Turning on C* mode calls the value of the variable c*-mode-hook with no
  162. args, if that value is non-nil."
  163.   (interactive)
  164.   (kill-all-local-variables)
  165.   (use-local-map c*-mode-map)
  166.   (setq major-mode 'c*-mode)
  167.   (setq mode-name "C*")
  168.   (setq local-abbrev-table c*-mode-abbrev-table)
  169.   (set-syntax-table c*-mode-syntax-table)
  170.   (make-local-variable 'paragraph-start)
  171.   (setq paragraph-start (concat "^$\\|" page-delimiter))
  172.   (make-local-variable 'paragraph-separate)
  173.   (setq paragraph-separate paragraph-start)
  174.   (make-local-variable 'paragraph-ignore-fill-prefix)
  175.   (setq paragraph-ignore-fill-prefix t)
  176.   (make-local-variable 'indent-line-function)
  177.   (setq indent-line-function 'c*-indent-line)
  178.   (make-local-variable 'require-final-newline)
  179.   (setq require-final-newline t)
  180.   (make-local-variable 'comment-start)
  181.   (setq comment-start "/* ")
  182.   (make-local-variable 'comment-end)
  183.   (setq comment-end " */")
  184.   (make-local-variable 'comment-column)
  185.   (setq comment-column 32)
  186.   (make-local-variable 'comment-start-skip)
  187.   (setq comment-start-skip "/\\*+ *")
  188.   (make-local-variable 'comment-indent-hook)
  189.   (setq comment-indent-hook 'c-comment-indent)
  190.   (make-local-variable 'parse-sexp-ignore-comments)
  191.   (setq parse-sexp-ignore-comments t)
  192.   (run-hooks 'c*-mode-hook))
  193. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  194.  
  195.  
  196. (defun c*-indent-line ()
  197.   "Indent current line as C* code.
  198. Return the amount the indentation changed by.
  199. Changed to respect the `:'s in declarations."
  200.   (let ((indent (calculate-c*-indent nil))
  201.     beg shift-amt
  202.     (case-fold-search nil)
  203.     (pos (- (point-max) (point))))
  204.     (beginning-of-line)
  205.     (setq beg (point))
  206.     (cond ((eq indent nil)
  207.        (setq indent (current-indentation)))
  208.       ((eq indent t)
  209.        (setq indent (calculate-c-indent-within-comment)))
  210.       ((looking-at "[ \t]*#")
  211.        (setq indent 0))
  212.       (t
  213.        (skip-chars-forward " \t")
  214.        (if (listp indent) (setq indent (car indent)))
  215.        (cond ((or (looking-at "case\\b")  ;;C*
  216.               (and (looking-at "[A-Za-z]")
  217.                (save-excursion
  218.                  (forward-sexp 1)
  219.                  (or (looking-at ":[\t ]")    ;;C*
  220.                  (looking-at ":$")))))    ;;C*
  221.           (setq indent (max 1 (+ indent c-label-offset))))
  222.          ((and (looking-at "else\\b")
  223.                (not (looking-at "else\\s_")))
  224.           (setq indent (save-excursion
  225.                  (c*-backward-to-start-of-if)
  226.                  (current-indentation))))
  227.          ((= (following-char) ?\})
  228.           (setq indent (- indent c-indent-level)))
  229.          ((= (following-char) ?\{)
  230.           (setq indent (+ indent c-brace-offset))))))
  231.     (skip-chars-forward " \t")
  232.     (setq shift-amt (- indent (current-column)))
  233.     (if (zerop shift-amt)
  234.     (if (> (- (point-max) pos) (point))
  235.         (goto-char (- (point-max) pos)))
  236.       (delete-region beg (point))
  237.       (indent-to indent)
  238.       ;; If initial point was within line's indentation,
  239.       ;; position after the indentation.  Else stay at same point in text.
  240.       (if (> (- (point-max) pos) (point))
  241.       (goto-char (- (point-max) pos))))
  242.     shift-amt))
  243.  
  244. (defun calculate-c*-indent (&optional parse-start)
  245.   "Return appropriate indentation for current line as C* code.
  246. In usual case returns an integer: the column to indent to.
  247. Returns nil if line starts inside a string, t if in a comment.
  248. Changed to respect the `:'s in declarations."
  249.   (save-excursion
  250.     (beginning-of-line)
  251.     (let ((indent-point (point))
  252.       (case-fold-search nil)
  253.       state
  254.       containing-sexp)
  255.       (if parse-start
  256.       (goto-char parse-start)
  257.     (beginning-of-defun))
  258.       (while (< (point) indent-point)
  259.     (setq parse-start (point))
  260.     (setq state (parse-partial-sexp (point) indent-point 0))
  261.     (setq containing-sexp (car (cdr state))))
  262.       (cond ((or (nth 3 state) (nth 4 state))
  263.          ;; return nil or t if should not change this line
  264.          (nth 4 state))
  265.         ((null containing-sexp)
  266.          ;; Line is at top level.  May be data or function definition,
  267.          ;; or may be function argument declaration.
  268.          ;; Indent like the previous top level line
  269.          ;; unless that ends in a closeparen without semicolon,
  270.          ;; in which case this line is the first argument decl.
  271.          (goto-char indent-point)
  272.          (skip-chars-forward " \t")
  273.          (if (= (following-char) ?\{)
  274.          0   ; Unless it starts a function body
  275.            (c-backward-to-noncomment (or parse-start (point-min)))
  276.            ;; Look at previous line that's at column 0
  277.            ;; to determine whether we are in top-level decls
  278.            ;; or function's arg decls.  Set basic-indent accordinglu.
  279.            (let ((basic-indent
  280.               (save-excursion
  281.             (re-search-backward "^[^ \^L\t\n#]" nil 'move)
  282.             (if (and (looking-at "\\sw\\|\\s_")
  283.                  (looking-at ".*(")
  284.                  (progn
  285.                    (goto-char (1- (match-end 0)))
  286.                    (forward-sexp 1)
  287.                    (and (< (point) indent-point)
  288.                     (not (memq (following-char)
  289.                            '(?\, ?\;))))))
  290.                 c-argdecl-indent 0))))
  291.  
  292.            ;; Now add a little if this is a continuation line.
  293.            (+ basic-indent
  294.               (if (or (bobp)
  295.                   (memq (preceding-char) '(?\) ?\; ?\}))
  296.                   (progn (beginning-of-line)
  297.                      (= (following-char) ?#)))
  298.                     ; BPL fix misindentation bug
  299.               0 c-continued-statement-offset)))))
  300.         ((/= (char-after containing-sexp) ?\{)
  301.          ;; line is expression, not statement:
  302.          ;; indent to just after the surrounding open.
  303.          (goto-char (1+ containing-sexp))
  304.          (current-column))
  305.         (t
  306.          ;; Statement level.  Is it a continuation or a new statement?
  307.          ;; Find previous non-comment character.
  308.          (goto-char indent-point)
  309.          (c-backward-to-noncomment containing-sexp)
  310.          ;; Back up over label lines, since they don't
  311.          ;; affect whether our line is a continuation.
  312.          (while (or (eq (preceding-char) ?\,)
  313.             (and (eq (preceding-char) ?:)
  314.                  (not (memq (following-char) '(? ?\n ?\t)))  ;;C*
  315.                  (or (eq (char-after (- (point) 2)) ?\')
  316.                  (memq (char-syntax (char-after (- (point) 2)))
  317.                        '(?w ?_)))))
  318.            (if (eq (preceding-char) ?\,)
  319.            (c-backward-to-start-of-continued-exp containing-sexp))
  320.            (beginning-of-line)
  321.            (c-backward-to-noncomment containing-sexp))
  322.          ;; Now we get the answer.
  323.          (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{ ?:)))
  324.          ;; BPL above fixes bug in indenting some case lines.
  325.          ;; This line is continuation of preceding line's statement;
  326.          ;; indent  c-continued-statement-offset  more than the
  327.          ;; previous line of the statement.
  328.          (progn
  329.            (c-backward-to-start-of-continued-exp containing-sexp)
  330.            (+ c-continued-statement-offset (current-column)
  331.               (if (save-excursion (goto-char indent-point)
  332.                       (skip-chars-forward " \t")
  333.                       (eq (following-char) ?\{))
  334.               c-continued-brace-offset 0)))
  335.            ;; This line starts a new statement.
  336.            ;; Position following last unclosed open.
  337.            (goto-char containing-sexp)
  338.            ;; Is line first statement after an open-brace?
  339.            (or
  340.          ;; If no, find that first statement and indent like it.
  341.          (save-excursion
  342.            (forward-char 1)
  343.            (let ((colon-line-end 0))
  344.              (while (progn (skip-chars-forward " \t\n")
  345.                    (looking-at "#\\|/\\*\\|case\\b.*:\\|[a-zA-Z0-9_$]*[ \t]:\\|[a-zA-Z0-9_$]*:$"))    ;;C*
  346.                ;; Skip over comments and labels following openbrace.
  347.                (cond ((= (following-char) ?\#)
  348.                   (forward-line 1))
  349.                  ((= (following-char) ?\/)
  350.                   (forward-char 2)
  351.                   (search-forward "*/" nil 'move))
  352.                  ;; case or label:
  353.                  (t
  354.                   (save-excursion (end-of-line)
  355.                           (setq colon-line-end (point)))
  356.                   (search-forward ":"))))
  357.              ;; The first following code counts
  358.              ;; if it is before the line we want to indent.
  359.              (and (< (point) indent-point)
  360.               (if (> colon-line-end (point))
  361.                   (- (current-indentation) c-label-offset)
  362.                 (current-column)))))
  363.          ;; If no previous statement,
  364.          ;; indent it relative to line brace is on.
  365.          ;; For open brace in column zero, don't let statement
  366.          ;; start there too.  If c-indent-level is zero,
  367.          ;; use c-brace-offset + c-continued-statement-offset instead.
  368.          ;; For open-braces not the first thing in a line,
  369.          ;; add in c-brace-imaginary-offset.
  370.          (+ (if (and (bolp) (zerop c-indent-level))
  371.             (+ c-brace-offset c-continued-statement-offset)
  372.               c-indent-level)
  373.             ;; Move back over whitespace before the openbrace.
  374.             ;; If openbrace is not first nonwhite thing on the line,
  375.             ;; add the c-brace-imaginary-offset.
  376.             (progn (skip-chars-backward " \t")
  377.                (if (bolp) 0 c-brace-imaginary-offset))
  378.             ;; If the openbrace is preceded by a parenthesized exp,
  379.             ;; move to the beginning of that;
  380.             ;; possibly a different line
  381.             (progn
  382.               (if (eq (preceding-char) ?\))
  383.               (forward-sexp -1))
  384.               ;; Get initial indentation of the line we are on.
  385.               (current-indentation))))))))))
  386.  
  387. (defun indent-c*-exp ()
  388.   "Indent each line of the C* grouping following point.
  389. Changed to respect the `:'s in declarations."
  390.   (interactive)
  391.   (let ((indent-stack (list nil))
  392.     (contain-stack (list (point)))
  393.     (case-fold-search nil)
  394.     restart outer-loop-done inner-loop-done state ostate
  395.     this-indent last-sexp
  396.     at-else at-brace
  397.     (opoint (point))
  398.     (next-depth 0))
  399.     (save-excursion
  400.       (forward-sexp 1))
  401.     (save-excursion
  402.       (setq outer-loop-done nil)
  403.       (while (and (not (eobp)) (not outer-loop-done))
  404.     (setq last-depth next-depth)
  405.     ;; Compute how depth changes over this line
  406.     ;; plus enough other lines to get to one that
  407.     ;; does not end inside a comment or string.
  408.     ;; Meanwhile, do appropriate indentation on comment lines.
  409.     (setq innerloop-done nil)
  410.     (while (and (not innerloop-done)
  411.             (not (and (eobp) (setq outer-loop-done t))))
  412.       (setq ostate state)
  413.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  414.                       nil nil state))
  415.       (setq next-depth (car state))
  416.       (if (and (car (cdr (cdr state)))
  417.            (>= (car (cdr (cdr state))) 0))
  418.           (setq last-sexp (car (cdr (cdr state)))))
  419.       (if (or (nth 4 ostate))
  420.           (c-indent-line))
  421.       (if (or (nth 3 state))
  422.           (forward-line 1)
  423.         (setq innerloop-done t)))
  424.     (if (<= next-depth 0)
  425.         (setq outer-loop-done t))
  426.     (if outer-loop-done
  427.         nil
  428.       ;; If this line had ..))) (((.. in it, pop out of the levels
  429.       ;; that ended anywhere in this line, even if the final depth
  430.       ;; doesn't indicate that they ended.
  431.       (while (> last-depth (nth 6 state))
  432.         (setq indent-stack (cdr indent-stack)
  433.           contain-stack (cdr contain-stack)
  434.           last-depth (1- last-depth)))
  435.       (if (/= last-depth next-depth)
  436.           (setq last-sexp nil))
  437.       ;; Add levels for any parens that were started in this line.
  438.       (while (< last-depth next-depth)
  439.         (setq indent-stack (cons nil indent-stack)
  440.           contain-stack (cons nil contain-stack)
  441.           last-depth (1+ last-depth)))
  442.       (if (null (car contain-stack))
  443.           (setcar contain-stack (or (car (cdr state))
  444.                     (save-excursion (forward-sexp -1)
  445.                             (point)))))
  446.       (forward-line 1)
  447.       (skip-chars-forward " \t")
  448.       (if (eolp)
  449.           nil
  450.         (if (and (car indent-stack)
  451.              (>= (car indent-stack) 0))
  452.         ;; Line is on an existing nesting level.
  453.         ;; Lines inside parens are handled specially.
  454.         (if (/= (char-after (car contain-stack)) ?\{)
  455.             (setq this-indent (car indent-stack))
  456.           ;; Line is at statement level.
  457.           ;; Is it a new statement?  Is it an else?
  458.           ;; Find last non-comment character before this line
  459.           (save-excursion
  460.             (setq at-else (looking-at "else\\W"))
  461.             (setq at-brace (= (following-char) ?\{))
  462.             (c-backward-to-noncomment opoint)
  463.             (if (not (memq (preceding-char) '(nil ?\, ?\; ?\{ ?: ?\})))
  464.             ;; Preceding line did not end in comma or semi;
  465.             ;; indent this line  c-continued-statement-offset
  466.             ;; more than previous.
  467.             (progn
  468.               (c-backward-to-start-of-continued-exp (car contain-stack))
  469.               (setq this-indent
  470.                 (+ c-continued-statement-offset (current-column)
  471.                    (if at-brace c-continued-brace-offset 0))))
  472.               ;; Preceding line ended in comma or semi;
  473.               ;; use the standard indent for this level.
  474.               (if at-else
  475.               (progn (c*-backward-to-start-of-if opoint)
  476.                  (setq this-indent (current-indentation)))
  477.             (setq this-indent (car indent-stack))))))
  478.           ;; Just started a new nesting level.
  479.           ;; Compute the standard indent for this level.
  480.           (let ((val (calculate-c-indent
  481.                (if (car indent-stack)
  482.                    (- (car indent-stack))))))
  483.         (setcar indent-stack
  484.             (setq this-indent val))))
  485.         ;; Adjust line indentation according to its contents
  486.         (if (or (looking-at "case\\b")  ;;C*
  487.             (and (looking-at "[A-Za-z]")
  488.              (save-excursion
  489.                (forward-sexp 1)
  490.                (or (looking-at ":[ \t]")  ;;C*
  491.                    (looking-at ":$")))))  ;;C*
  492.         (setq this-indent (max 1 (+ this-indent c-label-offset))))
  493.         (if (= (following-char) ?\})
  494.         (setq this-indent (- this-indent c-indent-level)))
  495.         (if (= (following-char) ?\{)
  496.         (setq this-indent (+ this-indent c-brace-offset)))
  497.         ;; Put chosen indentation into effect.
  498.         (or (= (current-column) this-indent)
  499.         (= (following-char) ?\#)
  500.         (progn
  501.           (delete-region (point) (progn (beginning-of-line) (point)))
  502.           (indent-to this-indent)))
  503.         ;; Indent any comment following the text.
  504.         (or (looking-at comment-start-skip)
  505.         (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
  506.             (progn (indent-for-comment) (beginning-of-line)))))))))
  507. ; (message "Indenting C expression...done")
  508.   )
  509.  
  510. (defun c*-backward-to-start-of-if (&optional limit)
  511.     "Move to the start of the last ``unbalanced'' if.
  512. Treats `where' the same as `if'."
  513.     (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
  514.     (let ((if-level 1)
  515.       (case-fold-search nil))
  516.     (while (not (zerop if-level))
  517.         (let ((oldpt (point)))    ; BPL handle errors gracefully
  518.         (catch t (unwind-protect (backward-sexp 1) (throw t t)))
  519.         (if (= (point) oldpt)
  520.             (error "Unmatched else or unbalanced braces.")))
  521.         (cond ((looking-at "else\\b")
  522.            (setq if-level (1+ if-level)))
  523.           ((or (looking-at "if\\b")
  524.                (looking-at "where\\b"))     ;;C*
  525.            (setq if-level (1- if-level)))
  526.           ((< (point) limit)
  527.            (setq if-level 0)
  528.            (goto-char limit))))))
  529.  
  530. (defun electric-c*-terminator (arg)
  531.   "Insert character and correct line's indentation."
  532.   (interactive "P")
  533.   (let (insertpos (end (point)))
  534.     (if (and (not arg) (eolp)
  535.          (not (save-excursion
  536.             (beginning-of-line)
  537.             (skip-chars-forward " \t")
  538.             (or (= (following-char) ?#)
  539.             ;; Colon is special only after a label, or case ....
  540.             ;; So quickly rule out most other uses of colon
  541.             ;; and do no indentation for them.
  542.             (and (eq last-command-char ?:)
  543.                  (not (looking-at "case\\b"))  ;;C*
  544.                  (save-excursion
  545.                    (forward-word 1)
  546.                    (skip-chars-forward " \t")
  547.                    (< (point) end)))
  548.             (progn
  549.               (beginning-of-defun)
  550.               (let ((pps (parse-partial-sexp (point) end)))
  551.                 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
  552.     (progn
  553.       (insert last-command-char)
  554.       (c*-indent-line)
  555.       (and c-auto-newline
  556.            (not (c-inside-parens-p))
  557.            (progn
  558.          (newline)
  559.          (setq insertpos (- (point) 2))
  560.          (c*-indent-line)))
  561.       (save-excursion
  562.         (if insertpos (goto-char (1+ insertpos)))
  563.         (delete-char -1))))
  564.     (if insertpos
  565.     (save-excursion
  566.       (goto-char insertpos)
  567.       (self-insert-command (prefix-numeric-value arg)))
  568.       (self-insert-command (prefix-numeric-value arg)))))
  569.  
  570. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  571. ;
  572. ; The following functions are new but should really be in C mode too.
  573.  
  574. (defun electric-c*-pound (arg)
  575.   "Insert # character and possibly correct line's indentation."
  576.   (interactive "P")
  577.   (self-insert-command (prefix-numeric-value arg))
  578.   (if (save-excursion
  579.       (beginning-of-line)
  580.       (skip-chars-forward " \t")
  581.       (eq (following-char) ?#))
  582.       (c*-indent-line)))
  583.  
  584. (defun electric-c*-colon (arg)
  585.   "Insert colon and possibly correct line's indentation.
  586. Does not reindent line if is not unambiguously a label."
  587.   (interactive "P")
  588.   (if (save-excursion
  589.       (beginning-of-line)
  590.       (looking-at "[ \t]*case\\b"))
  591.       (if c-auto-newline
  592.       (electric-c*-terminator arg)
  593.       (c*-indent-line)
  594.       (self-insert-command (prefix-numeric-value arg)))
  595.       (self-insert-command (prefix-numeric-value arg))))
  596.  
  597. (defun electric-c*-newline (arg)
  598.   "Insert newline and possibly correct line's indentation.
  599. Also checks the c-newline-indents variable to indent the next line."
  600.   (interactive "P")
  601.   (if (save-excursion
  602.       (skip-chars-backward " \t")
  603.       (eq (preceding-char) ?:))
  604.       (c*-indent-line))
  605.   (if c-newline-indents
  606.       (newline (prefix-numeric-value arg))
  607.       (newline-and-indent)))
  608.  
  609. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  610. ;
  611. ; The following functions had to be changed to refer to c*- functions.
  612.  
  613. (defun c*-indent-command (&optional whole-exp)
  614.   (interactive "P")
  615.   "Indent current line as C* code, or in some cases insert a tab character.
  616. If c-tab-always-indent is non-nil (the default), always indent current line.
  617. Otherwise, indent the current line only if point is at the left margin
  618. or in the line's indentation; otherwise insert a tab.
  619.  
  620. A numeric argument, regardless of its value,
  621. means indent rigidly all the lines of the expression starting after point
  622. so that this line becomes properly indented.
  623. The relative indentation among the lines of the expression are preserved."
  624.   (if whole-exp
  625.       ;; If arg, always indent this line as C
  626.       ;; and shift remaining lines of expression the same amount.
  627.       (let ((shift-amt (c*-indent-line))
  628.         beg end)
  629.     (save-excursion
  630.       (if c-tab-always-indent
  631.           (beginning-of-line))
  632.       (setq beg (point))
  633.       (forward-sexp 1)
  634.       (setq end (point))
  635.       (goto-char beg)
  636.       (forward-line 1)
  637.       (setq beg (point)))
  638.     (if (> end beg)
  639.         (indent-code-rigidly beg end shift-amt "#")))
  640.     (if (and (not c-tab-always-indent)
  641.          (save-excursion
  642.            (skip-chars-backward " \t")
  643.            (not (bolp))))
  644.     (insert-tab)
  645.       (c*-indent-line))))
  646.  
  647.  
  648. (defun electric-c*-brace (arg)
  649.   "Insert { or } character and correct line's indentation."
  650.   (interactive "P")
  651.   (let (insertpos)
  652.     (if (and (not arg)
  653.          (eolp)
  654.          (or (save-excursion
  655.            (skip-chars-backward " \t")
  656.            (bolp))
  657.          (if c-auto-newline (progn (c*-indent-line) (newline) t) nil)))
  658.     (progn
  659.       (insert last-command-char)
  660.       (c*-indent-line)
  661.       (if c-auto-newline
  662.           (progn
  663.         (newline)
  664.         ;; (newline) may have done auto-fill
  665.         (setq insertpos (- (point) 2))
  666.         (c*-indent-line)))
  667.       (save-excursion
  668.         (if insertpos (goto-char (1+ insertpos)))
  669.         (delete-char -1))))
  670.     (if insertpos
  671.     (save-excursion
  672.       (goto-char insertpos)
  673.       (self-insert-command (prefix-numeric-value arg)))
  674.       (self-insert-command (prefix-numeric-value arg)))))
  675.  
  676. (defun electric-c*-semi (arg)
  677.   "Insert ; character and correct line's indentation."
  678.   (interactive "P")
  679.   (if c-auto-newline
  680.       (electric-c*-terminator arg)
  681.     (self-insert-command (prefix-numeric-value arg))))
  682. -- 
  683. Erik Bailey   | Cambridge Technology Partners      | The usual disclaimer
  684.               | 304 Vassar St., Cambridge MA 02139 | applies: this post is
  685. ebail@ctp.com | (617) 374-8418                     | mine, not my employer's.
  686.       /earth is 98% full; please remove any excess inhabitants.
  687.