home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / mui-c-mode.lha / mui-c-mode.el next >
Encoding:
Text File  |  1994-09-27  |  25.7 KB  |  737 lines

  1. ;; C code editing commands for Emacs
  2. ;;
  3. ;; Modified to recognize MUI macros and tags, and attempt to
  4. ;; do the right thing with them.  It's ugly, but it works for me.
  5.  
  6. ;; Based on source which is part of GNU Emacs, which is
  7. ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  8.  
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 1, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;; The author of the modifications can be reached at:
  24. ;;
  25. ;; email: genoa@mit.edu
  26. ;;
  27. ;;  mail: Jack Holt
  28. ;;        1702A E. Birch St.
  29. ;;        Cottonwood, AZ  86326-3953
  30.  
  31. (defvar c-mode-abbrev-table nil
  32.   "Abbrev table in use in C-mode buffers.")
  33. (define-abbrev-table 'c-mode-abbrev-table ())
  34.  
  35. (defvar c-mode-map ()
  36.   "Keymap used in C mode.")
  37. (if c-mode-map
  38.     ()
  39.   (setq c-mode-map (make-sparse-keymap))
  40.   (define-key c-mode-map "{" 'electric-c-brace)
  41.   (define-key c-mode-map "}" 'electric-c-brace)
  42.   (define-key c-mode-map ";" 'electric-c-semi)
  43.   (define-key c-mode-map ":" 'electric-c-terminator)
  44.   (define-key c-mode-map "\e\C-h" 'mark-c-function)
  45.   (define-key c-mode-map "\e\C-q" 'indent-c-exp)
  46.   (define-key c-mode-map "\177" 'backward-delete-char-untabify)
  47.   (define-key c-mode-map "\t" 'c-indent-command))
  48.  
  49. (autoload 'c-macro-expand "cmacexp"
  50.   "Display the result of expanding all C macros occurring in the region.
  51. The expansion is entirely correct because it uses the C preprocessor."
  52.   t)
  53.  
  54. (defvar c-mode-syntax-table nil
  55.   "Syntax table in use in C-mode buffers.")
  56.  
  57. (if c-mode-syntax-table
  58.     ()
  59.   (setq c-mode-syntax-table (make-syntax-table))
  60.   (modify-syntax-entry ?\\ "\\" c-mode-syntax-table)
  61.   (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
  62.   (modify-syntax-entry ?* ". 23" c-mode-syntax-table)
  63.   (modify-syntax-entry ?+ "." c-mode-syntax-table)
  64.   (modify-syntax-entry ?- "." c-mode-syntax-table)
  65.   (modify-syntax-entry ?= "." c-mode-syntax-table)
  66.   (modify-syntax-entry ?% "." c-mode-syntax-table)
  67.   (modify-syntax-entry ?< "." c-mode-syntax-table)
  68.   (modify-syntax-entry ?> "." c-mode-syntax-table)
  69.   (modify-syntax-entry ?& "." c-mode-syntax-table)
  70.   (modify-syntax-entry ?| "." c-mode-syntax-table)
  71.   (modify-syntax-entry ?\' "\"" c-mode-syntax-table))
  72.  
  73. (defconst c-indent-level 2
  74.   "*Indentation of C statements with respect to containing block.")
  75. (defconst c-brace-imaginary-offset 0
  76.   "*Imagined indentation of a C open brace that actually follows a statement.")
  77. (defconst c-brace-offset 0
  78.   "*Extra indentation for braces, compared with other text in same context.")
  79. (defconst c-argdecl-indent 5
  80.   "*Indentation level of declarations of C function arguments.")
  81. (defconst c-label-offset -2
  82.   "*Offset of C label lines and case statements relative to usual indentation.")
  83. (defconst c-continued-statement-offset 2
  84.   "*Extra indent for lines not starting new statements.")
  85. (defconst c-continued-brace-offset 0
  86.   "*Extra indent for substatements that start with open-braces.
  87. This is in addition to c-continued-statement-offset.")
  88.  
  89. (defconst c-auto-newline nil
  90.   "*Non-nil means automatically newline before and after braces,
  91. and after colons and semicolons, inserted in C code.")
  92.  
  93. (defconst c-tab-always-indent t
  94.   "*Non-nil means TAB in C mode should always reindent the current line,
  95. regardless of where in the line point is when the TAB command is used.")
  96.  
  97. (defun c-mode ()
  98.   "Major mode for editing C code, 
  99. with modifications to accommodate standard MUI macros.
  100. Expression and list commands understand all C brackets.
  101. Tab indents for C code.
  102. Comments are delimited with /* ... */.
  103. Paragraphs are separated by blank lines only.
  104. Delete converts tabs to spaces as it moves back.
  105. \\{c-mode-map}
  106. Variables controlling indentation style:
  107.  c-tab-always-indent
  108.     Non-nil means TAB in C mode should always reindent the current line,
  109.     regardless of where in the line point is when the TAB command is used.
  110.  c-auto-newline
  111.     Non-nil means automatically newline before and after braces,
  112.     and after colons and semicolons, inserted in C code.
  113.  c-indent-level
  114.     Indentation of C statements within surrounding block.
  115.     The surrounding block's indentation is the indentation
  116.     of the line on which the open-brace appears.
  117.  c-continued-statement-offset
  118.     Extra indentation given to a substatement, such as the
  119.     then-clause of an if or body of a while.
  120.  c-continued-brace-offset
  121.     Extra indentation given to a brace that starts a substatement.
  122.     This is in addition to c-continued-statement-offset.
  123.  c-brace-offset
  124.     Extra indentation for line if it starts with an open brace.
  125.  c-brace-imaginary-offset
  126.     An open brace following other text is treated as if it were
  127.     this far to the right of the start of its line.
  128.  c-argdecl-indent
  129.     Indentation level of declarations of C function arguments.
  130.  c-label-offset
  131.     Extra indentation for line that is a label, or case or default.
  132.  
  133. Settings for K&R and BSD indentation styles are
  134.   c-indent-level                5    8
  135.   c-continued-statement-offset  5    8
  136.   c-brace-offset               -5   -8
  137.   c-argdecl-indent              0    8
  138.   c-label-offset               -5   -8
  139.  
  140. Turning on C mode calls the value of the variable c-mode-hook with no args,
  141. if that value is non-nil."
  142.   (interactive)
  143.   (kill-all-local-variables)
  144.   (use-local-map c-mode-map)
  145.   (setq major-mode 'c-mode)
  146.   (setq mode-name "C w/MUI")
  147.   (setq local-abbrev-table c-mode-abbrev-table)
  148.   (set-syntax-table c-mode-syntax-table)
  149.   (make-local-variable 'paragraph-start)
  150.   (setq paragraph-start (concat "^$\\|" page-delimiter))
  151.   (make-local-variable 'paragraph-separate)
  152.   (setq paragraph-separate paragraph-start)
  153.   (make-local-variable 'paragraph-ignore-fill-prefix)
  154.   (setq paragraph-ignore-fill-prefix t)
  155.   (make-local-variable 'indent-line-function)
  156.   (setq indent-line-function 'c-indent-line)
  157.   (make-local-variable 'require-final-newline)
  158.   (setq require-final-newline t)
  159.   (make-local-variable 'comment-start)
  160.   (setq comment-start "/* ")
  161.   (make-local-variable 'comment-end)
  162.   (setq comment-end " */")
  163.   (make-local-variable 'comment-column)
  164.   (setq comment-column 32)
  165.   (make-local-variable 'comment-start-skip)
  166.   (setq comment-start-skip "/\\*+ *")
  167.   (make-local-variable 'comment-indent-hook)
  168.   (setq comment-indent-hook 'c-comment-indent)
  169.   (make-local-variable 'parse-sexp-ignore-comments)
  170.   (setq parse-sexp-ignore-comments t)
  171.   (run-hooks 'c-mode-hook))
  172.  
  173. ;; This is used by indent-for-comment
  174. ;; to decide how much to indent a comment in C code
  175. ;; based on its context.
  176. (defun c-comment-indent ()
  177.   (if (looking-at "^/\\*")
  178.       0                ;Existing comment at bol stays there.
  179.     (save-excursion
  180.       (skip-chars-backward " \t")
  181.       (max (1+ (current-column))    ;Else indent at comment column
  182.        comment-column))))    ; except leave at least one space.
  183.  
  184. (defun electric-c-brace (arg)
  185.   "Insert character and correct line's indentation."
  186.   (interactive "P")
  187.   (let (insertpos)
  188.     (if (and (not arg)
  189.          (eolp)
  190.          (or (save-excursion
  191.            (skip-chars-backward " \t")
  192.            (bolp))
  193.          (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
  194.     (progn
  195.       (insert last-command-char)
  196.       (c-indent-line)
  197.       (if c-auto-newline
  198.           (progn
  199.         (newline)
  200.         ;; (newline) may have done auto-fill
  201.         (setq insertpos (- (point) 2))
  202.         (c-indent-line)))
  203.       (save-excursion
  204.         (if insertpos (goto-char (1+ insertpos)))
  205.         (delete-char -1))))
  206.     (if insertpos
  207.     (save-excursion
  208.       (goto-char insertpos)
  209.       (self-insert-command (prefix-numeric-value arg)))
  210.       (self-insert-command (prefix-numeric-value arg)))))
  211.  
  212. (defun electric-c-semi (arg)
  213.   "Insert character and correct line's indentation."
  214.   (interactive "P")
  215.   (if c-auto-newline
  216.       (electric-c-terminator arg)
  217.     (self-insert-command (prefix-numeric-value arg))))
  218.  
  219. (defun electric-c-terminator (arg)
  220.   "Insert character and correct line's indentation."
  221.   (interactive "P")
  222.   (let (insertpos (end (point)))
  223.     (if (and (not arg) (eolp)
  224.          (not (save-excursion
  225.             (beginning-of-line)
  226.             (skip-chars-forward " \t")
  227.             (or (= (following-char) ?#)
  228.             ;; Colon is special only after a label, or case ....
  229.             ;; So quickly rule out most other uses of colon
  230.             ;; and do no indentation for them.
  231.             (and (eq last-command-char ?:)
  232.                  (not (looking-at "case[ \t]"))
  233.                  (save-excursion
  234.                    (forward-word 1)
  235.                    (skip-chars-forward " \t")
  236.                    (< (point) end)))
  237.             (progn
  238.               (beginning-of-defun)
  239.               (let ((pps (parse-partial-sexp (point) end)))
  240.                 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
  241.     (progn
  242.       (insert last-command-char)
  243.       (c-indent-line)
  244.       (and c-auto-newline
  245.            (not (c-inside-parens-p))
  246.            (progn
  247.          (newline)
  248.          (setq insertpos (- (point) 2))
  249.          (c-indent-line)))
  250.       (save-excursion
  251.         (if insertpos (goto-char (1+ insertpos)))
  252.         (delete-char -1))))
  253.     (if insertpos
  254.     (save-excursion
  255.       (goto-char insertpos)
  256.       (self-insert-command (prefix-numeric-value arg)))
  257.       (self-insert-command (prefix-numeric-value arg)))))
  258.  
  259. (defun c-inside-parens-p ()
  260.   (condition-case ()
  261.       (save-excursion
  262.     (save-restriction
  263.       (narrow-to-region (point)
  264.                 (progn (beginning-of-defun) (point)))
  265.       (goto-char (point-max))
  266.       (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
  267.     (error nil)))
  268.  
  269. (defun c-indent-command (&optional whole-exp)
  270.   "Indent current line as C code, or in some cases insert a tab character.
  271. If c-tab-always-indent is non-nil (the default), always indent current line.
  272. Otherwise, indent the current line only if point is at the left margin
  273. or in the line's indentation; otherwise insert a tab.
  274.  
  275. A numeric argument, regardless of its value,
  276. means indent rigidly all the lines of the expression starting after point
  277. so that this line becomes properly indented.
  278. The relative indentation among the lines of the expression are preserved."
  279.   (interactive "P")
  280.   (if whole-exp
  281.       ;; If arg, always indent this line as C
  282.       ;; and shift remaining lines of expression the same amount.
  283.       (let ((shift-amt (c-indent-line))
  284.         beg end)
  285.     (save-excursion
  286.       (if c-tab-always-indent
  287.           (beginning-of-line))
  288.       (setq beg (point))
  289.       (forward-sexp 1)
  290.       (setq end (point))
  291.       (goto-char beg)
  292.       (forward-line 1)
  293.       (setq beg (point)))
  294.     (if (> end beg)
  295.         (indent-code-rigidly beg end shift-amt "#")))
  296.     (if (and (not c-tab-always-indent)
  297.          (save-excursion
  298.            (skip-chars-backward " \t")
  299.            (not (bolp))))
  300.     (insert-tab)
  301.       (c-indent-line))))
  302.  
  303. (defun c-indent-line ()
  304.   "Indent current line as C code.
  305. Return the amount the indentation changed by."
  306.   (let ((indent (calculate-c-indent nil))
  307.     beg shift-amt
  308.     (case-fold-search nil)
  309.     (pos (- (point-max) (point))))
  310.     (beginning-of-line)
  311.     (setq beg (point))
  312.     (cond ((eq indent nil)
  313.        (setq indent (current-indentation)))
  314.       ((eq indent t)
  315.        (setq indent (calculate-c-indent-within-comment)))
  316.       ((looking-at "[ \t]*#")
  317.        (setq indent 0))
  318.       (t
  319.        (skip-chars-forward " \t")
  320.        (if (listp indent) (setq indent (car indent)))
  321.        (cond ((or (looking-at "case[ \t]")
  322.               (and (looking-at "[A-Za-z]")
  323.                (save-excursion
  324.                  (forward-sexp 1)
  325.                  (looking-at ":"))))
  326.           (setq indent (max 1 (+ indent c-label-offset))))
  327.          ((and (looking-at "else\\b")
  328.                (not (looking-at "else\\s_")))
  329.           (setq indent (save-excursion
  330.                  (c-backward-to-start-of-if)
  331.                  (current-indentation))))
  332.          ((= (following-char) ?})
  333.           (setq indent (- indent c-indent-level)))
  334.          ((= (following-char) ?{)
  335.           (setq indent (+ indent c-brace-offset))))))
  336.     (skip-chars-forward " \t")
  337.     (setq shift-amt (- indent (current-column)))
  338.     (if (zerop shift-amt)
  339.     (if (> (- (point-max) pos) (point))
  340.         (goto-char (- (point-max) pos)))
  341.       (delete-region beg (point))
  342.       (indent-to indent)
  343.       ;; If initial point was within line's indentation,
  344.       ;; position after the indentation.  Else stay at same point in text.
  345.       (if (> (- (point-max) pos) (point))
  346.       (goto-char (- (point-max) pos))))
  347.     shift-amt))
  348.  
  349. (defun calculate-c-indent (&optional parse-start)
  350.   "Return appropriate indentation for current line as C code.
  351. In usual case returns an integer: the column to indent to.
  352. Returns nil if line starts inside a string, t if in a comment."
  353.   (save-excursion
  354.     (beginning-of-line)
  355.     (let ((indent-point (point))
  356.       (case-fold-search nil)
  357.       state
  358.       containing-sexp)
  359.       (if parse-start
  360.       (goto-char parse-start)
  361.     (beginning-of-defun))
  362.       (while (< (point) indent-point)
  363.     (setq parse-start (point))
  364.     (setq state (parse-partial-sexp (point) indent-point 0))
  365.     (setq containing-sexp (car (cdr state))))
  366.       (cond ((or (nth 3 state) (nth 4 state))
  367.          ;; return nil or t if should not change this line
  368.          (nth 4 state))
  369.         ((null containing-sexp)
  370.          ;; Line is at top level.  May be data or function definition,
  371.          ;; or may be function argument declaration.
  372.          ;; Indent like the previous top level line
  373.          ;; unless that ends in a closeparen without semicolon,
  374.          ;; in which case this line is the first argument decl.
  375.          (goto-char indent-point)
  376.          (skip-chars-forward " \t")
  377.          (if (= (following-char) ?{)
  378.          0   ; Unless it starts a function body
  379.            (c-backward-to-noncomment (or parse-start (point-min)))
  380.            ;; Look at previous line that's at column 0
  381.            ;; to determine whether we are in top-level decls
  382.            ;; or function's arg decls.  Set basic-indent accordingly.
  383.            (let ((basic-indent
  384.               (save-excursion
  385.             (re-search-backward "^[^ \^L\t\n#]" nil 'move)
  386.             (if (and (looking-at "\\sw\\|\\s_")
  387.                  (looking-at ".*(")
  388.                  (progn
  389.                    (goto-char (1- (match-end 0)))
  390.                    (forward-sexp 1)
  391.                    (and (< (point) indent-point)
  392.                     (not (memq (following-char)
  393.                            '(?\, ?\;))))))
  394.                 c-argdecl-indent 0))))
  395.          ;; Now add a little if this is a continuation line.
  396.          (+ basic-indent (if (or (bobp)
  397.                      (memq (preceding-char) '(?\) ?\; ?\})))
  398.                      0 c-continued-statement-offset)))))
  399.         ((/= (char-after containing-sexp) ?{)
  400.          ;; line is expression, not statement:
  401.          ;; indent to just after the surrounding open.
  402.          (goto-char (1+ containing-sexp))
  403.          (current-column)) 
  404.         (t
  405.          ;; Statement level.  Is it a continuation or a new statement?
  406.          ;; Find previous non-comment character.
  407.          (goto-char indent-point)
  408.          (c-backward-to-noncomment containing-sexp)
  409.          ;; Back up over label lines, since they don't
  410.          ;; affect whether our line is a continuation.
  411.          (while (or (eq (preceding-char) ?\,)
  412.             (and (eq (preceding-char) ?:)
  413.                  (or (eq (char-after (- (point) 2)) ?\')
  414.                  (memq (char-syntax (char-after (- (point) 2)))
  415.                        '(?w ?_)))))
  416.            (if (eq (preceding-char) ?\,)
  417.            (c-backward-to-start-of-continued-exp containing-sexp))
  418.            (beginning-of-line)
  419.            (c-backward-to-noncomment containing-sexp))
  420.          ;; Now we get the answer.
  421.          (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{)))
  422.          ;; This line is continuation of preceding line's statement;
  423.          ;; indent  c-continued-statement-offset  more than the
  424.          ;; previous line of the statement.
  425.          (progn
  426.            (c-backward-to-start-of-continued-exp containing-sexp)
  427.            (+ c-continued-statement-offset (current-column)
  428.               (if (save-excursion (goto-char indent-point)
  429.                       (skip-chars-forward " \t")
  430.                       (eq (following-char) ?{))
  431.               c-continued-brace-offset 0)))
  432.            ;; This line starts a new statement.
  433.            ;;; *** MUI magic starts here. ***
  434.            (save-excursion
  435.          (goto-char indent-point)
  436.          (skip-chars-forward " \t")
  437.          (cond 
  438.           ((or (looking-at "MUIA_")
  439.                (looking-at "Child,")
  440.                (looking-at "End[,;]")
  441.                (looking-at "SubWindow,")
  442.                (looking-at "WindowContents,")
  443.                (looking-at "GroupSpacing(")
  444.                (looking-at "ASL[A-Z][A-Z]_")
  445.                (looking-at "[A-Za-z]*Frame,"))
  446.            (save-excursion
  447.              (forward-line -1)
  448.              (let ((mui-offset 0)
  449.                (mui-start (current-indentation)))
  450.              ;;  (insert ?1)
  451.                (let ((doit t))
  452.              (while doit
  453.                (let ((ptchar (char-after (point))))
  454.                  (cond 
  455.                   ((eq ptchar ?M)
  456.                    (progn
  457.                  (if (looking-at "MenuObjectT(")
  458.                      (progn
  459.                        (setq mui-offset (+ mui-offset c-indent-level))
  460.                        (forward-char 11)
  461.                      ;;  (insert ?2)
  462.                        ))))
  463.                   ((eq ptchar ?O)
  464.                    (progn
  465.                  (if (looking-at "Object,")
  466.                      (progn
  467.                        (setq mui-offset (+ mui-offset c-indent-level))
  468.                        (forward-char 6)
  469.                      ;;  (insert ?3)
  470.                        ))))
  471.                   ((eq ptchar ?G)
  472.                    (progn
  473.                  (if (looking-at "Group[,V(]")
  474.                      (progn
  475.                        (setq mui-offset (+ mui-offset c-indent-level))
  476.                        (forward-char 5)
  477.                      ;;  (insert ?4)
  478.                        ))))
  479.                   ((eq ptchar ?\n)
  480.                    (setq doit nil)))
  481.                  (forward-char))))
  482.                (let ((doit t))
  483.              (while doit
  484.                (let ((ptchar (char-after (point))))
  485.                  (cond
  486.                   ((eq ptchar ?E)
  487.                    (progn
  488.                  (if (looking-at "End[,;]")
  489.                      (progn
  490.                        (setq mui-offset (- mui-offset c-indent-level))
  491.                        (forward-char 3)))))
  492.                   ((eq ptchar ?\n)
  493.                    (setq doit nil)))
  494.                  (forward-char))))
  495.                (+ mui-start mui-offset))))
  496.           ;;; *** MUI magic ends here. ***
  497.           (t
  498.            ;; Position following last unclosed open.
  499.            (goto-char containing-sexp)
  500.            ;; Is line first statement after an open-brace?
  501.            (or
  502.             ;; If no, find that first statement and indent like it.
  503.             (save-excursion
  504.               (forward-char 1)
  505.               (let ((colon-line-end 0))
  506.             (while (progn (skip-chars-forward " \t\n")
  507.                       (looking-at "#\\|/\\*\\|case[ \t\n].*:\\|[a-zA-Z0-9_$]*:"))
  508.               ;; Skip over comments and labels following openbrace.
  509.               (cond ((= (following-char) ?\#)
  510.                  (forward-line 1))
  511.                 ((= (following-char) ?\/)
  512.                  (forward-char 2)
  513.                  (search-forward "*/" nil 'move))
  514.                 ;; case or label:
  515.                 (t
  516.                  (save-excursion (end-of-line)
  517.                          (setq colon-line-end (point)))
  518.                  (search-forward ":"))))
  519.             ;; The first following code counts
  520.             ;; if it is before the line we want to indent.
  521.             (and (< (point) indent-point)
  522.                  (if (> colon-line-end (point))
  523.                  (- (current-indentation) c-label-offset)
  524.                    (current-column)))))
  525.             ;; If no previous statement,
  526.             ;; indent it relative to line brace is on.
  527.             ;; For open brace in column zero, don't let statement
  528.             ;; start there too.  If c-indent-level is zero,
  529.             ;; use c-brace-offset + c-continued-statement-offset instead.
  530.             ;; For open-braces not the first thing in a line,
  531.             ;; add in c-brace-imaginary-offset.
  532.             (+ (if (and (bolp) (zerop c-indent-level))
  533.                (+ c-brace-offset c-continued-statement-offset)
  534.              c-indent-level)
  535.                ;; Move back over whitespace before the openbrace.
  536.                ;; If openbrace is not first nonwhite thing on the line,
  537.                ;; add the c-brace-imaginary-offset.
  538.                (progn (skip-chars-backward " \t")
  539.                   (if (bolp) 0 c-brace-imaginary-offset))
  540.                ;; If the openbrace is preceded by a parenthesized exp,
  541.                ;; move to the beginning of that;
  542.                ;; possibly a different line
  543.                (progn
  544.              (if (eq (preceding-char) ?\))
  545.                  (forward-sexp -1))
  546.              ;; Get initial indentation of the line we are on.
  547.              (current-indentation)))))))))))))
  548.  
  549. (defun calculate-c-indent-within-comment ()
  550.   "Return the indentation amount for line, assuming that
  551. the current line is to be regarded as part of a block comment."
  552.   (let (end star-start)
  553.     (save-excursion
  554.       (beginning-of-line)
  555.       (skip-chars-forward " \t")
  556.       (setq star-start (= (following-char) ?\*))
  557.       (skip-chars-backward " \t\n")
  558.       (setq end (point))
  559.       (beginning-of-line)
  560.       (skip-chars-forward " \t")
  561.       (and (re-search-forward "/\\*[ \t]*" end t)
  562.        star-start
  563.        (goto-char (1+ (match-beginning 0))))
  564.       (current-column))))
  565.  
  566.  
  567. (defun c-backward-to-noncomment (lim)
  568.   (let (opoint stop)
  569.     (while (not stop)
  570.       (skip-chars-backward " \t\n\f" lim)
  571.       (setq opoint (point))
  572.       (if (and (>= (point) (+ 2 lim))
  573.            (save-excursion
  574.          (forward-char -2)
  575.          (looking-at "\\*/")))
  576.       (search-backward "/*" lim 'move)
  577.     (setq stop (or (<= (point) lim)
  578.                (save-excursion
  579.              (beginning-of-line)
  580.              (skip-chars-forward " \t")
  581.              (not (looking-at "#")))))
  582.     (or stop (beginning-of-line))))))
  583.  
  584. (defun c-backward-to-start-of-continued-exp (lim)
  585.   (if (= (preceding-char) ?\))
  586.       (forward-sexp -1))
  587.   (beginning-of-line)
  588.   (if (<= (point) lim)
  589.       (goto-char (1+ lim)))
  590.   (skip-chars-forward " \t"))
  591.  
  592. (defun c-backward-to-start-of-if (&optional limit)
  593.   "Move to the start of the last ``unbalanced'' if."
  594.   (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
  595.   (let ((if-level 1)
  596.     (case-fold-search nil))
  597.     (while (not (zerop if-level))
  598.       (backward-sexp 1)
  599.       (cond ((looking-at "else\\b")
  600.          (setq if-level (1+ if-level)))
  601.         ((looking-at "if\\b")
  602.          (setq if-level (1- if-level)))
  603.         ((< (point) limit)
  604.          (setq if-level 0)
  605.          (goto-char limit))))))
  606.  
  607.  
  608. (defun mark-c-function ()
  609.   "Put mark at end of C function, point at beginning."
  610.   (interactive)
  611.   (push-mark (point))
  612.   (end-of-defun)
  613.   (push-mark (point))
  614.   (beginning-of-defun)
  615.   (backward-paragraph))
  616.  
  617. (defun indent-c-exp ()
  618.   "Indent each line of the C grouping following point."
  619.   (interactive)
  620.   (let ((indent-stack (list nil))
  621.     (contain-stack (list (point)))
  622.     (case-fold-search nil)
  623.     restart outer-loop-done inner-loop-done state ostate
  624.     this-indent last-sexp
  625.     at-else at-brace
  626.     (opoint (point))
  627.     (next-depth 0))
  628.     (save-excursion
  629.       (forward-sexp 1))
  630.     (save-excursion
  631.       (setq outer-loop-done nil)
  632.       (while (and (not (eobp)) (not outer-loop-done))
  633.     (setq last-depth next-depth)
  634.     ;; Compute how depth changes over this line
  635.     ;; plus enough other lines to get to one that
  636.     ;; does not end inside a comment or string.
  637.     ;; Meanwhile, do appropriate indentation on comment lines.
  638.     (setq inner-loop-done nil)
  639.     (while (and (not inner-loop-done)
  640.             (not (and (eobp) (setq outer-loop-done t))))
  641.       (setq ostate state)
  642.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  643.                       nil nil state))
  644.       (setq next-depth (car state))
  645.       (if (and (car (cdr (cdr state)))
  646.            (>= (car (cdr (cdr state))) 0))
  647.           (setq last-sexp (car (cdr (cdr state)))))
  648.       (if (or (nth 4 ostate))
  649.           (c-indent-line))
  650.       (if (or (nth 3 state))
  651.           (forward-line 1)
  652.         (setq inner-loop-done t)))
  653.     (if (<= next-depth 0)
  654.         (setq outer-loop-done t))
  655.     (if outer-loop-done
  656.         nil
  657.       ;; If this line had ..))) (((.. in it, pop out of the levels
  658.       ;; that ended anywhere in this line, even if the final depth
  659.       ;; doesn't indicate that they ended.
  660.       (while (> last-depth (nth 6 state))
  661.         (setq indent-stack (cdr indent-stack)
  662.           contain-stack (cdr contain-stack)
  663.           last-depth (1- last-depth)))
  664.       (if (/= last-depth next-depth)
  665.           (setq last-sexp nil))
  666.       ;; Add levels for any parens that were started in this line.
  667.       (while (< last-depth next-depth)
  668.         (setq indent-stack (cons nil indent-stack)
  669.           contain-stack (cons nil contain-stack)
  670.           last-depth (1+ last-depth)))
  671.       (if (null (car contain-stack))
  672.           (setcar contain-stack (or (car (cdr state))
  673.                     (save-excursion (forward-sexp -1)
  674.                             (point)))))
  675.       (forward-line 1)
  676.       (skip-chars-forward " \t")
  677.       (if (eolp)
  678.           nil
  679.         (if (and (car indent-stack)
  680.              (>= (car indent-stack) 0))
  681.         ;; Line is on an existing nesting level.
  682.         ;; Lines inside parens are handled specially.
  683.         (if (/= (char-after (car contain-stack)) ?{)
  684.             (setq this-indent (car indent-stack))
  685.           ;; Line is at statement level.
  686.           ;; Is it a new statement?  Is it an else?
  687.           ;; Find last non-comment character before this line
  688.           (save-excursion
  689.             (setq at-else (looking-at "else\\W"))
  690.             (setq at-brace (= (following-char) ?{))
  691.             (c-backward-to-noncomment opoint)
  692.             (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{)))
  693.             ;; Preceding line did not end in comma or semi;
  694.             ;; indent this line  c-continued-statement-offset
  695.             ;; more than previous.
  696.             (progn
  697.               (c-backward-to-start-of-continued-exp (car contain-stack))
  698.               (setq this-indent
  699.                 (+ c-continued-statement-offset (current-column)
  700.                    (if at-brace c-continued-brace-offset 0))))
  701.               ;; Preceding line ended in comma or semi;
  702.               ;; use the standard indent for this level.
  703.               (if at-else
  704.               (progn (c-backward-to-start-of-if opoint)
  705.                  (setq this-indent (current-indentation)))
  706.             (setq this-indent (car indent-stack))))))
  707.           ;; Just started a new nesting level.
  708.           ;; Compute the standard indent for this level.
  709.           (let ((val (calculate-c-indent
  710.                (if (car indent-stack)
  711.                    (- (car indent-stack))))))
  712.         (setcar indent-stack
  713.             (setq this-indent val))))
  714.         ;; Adjust line indentation according to its contents
  715.         (if (or (looking-at "case[ \t]")
  716.             (and (looking-at "[A-Za-z]")
  717.              (save-excursion
  718.                (forward-sexp 1)
  719.                (looking-at ":"))))
  720.         (setq this-indent (max 1 (+ this-indent c-label-offset))))
  721.         (if (= (following-char) ?})
  722.         (setq this-indent (- this-indent c-indent-level)))
  723.         (if (= (following-char) ?{)
  724.         (setq this-indent (+ this-indent c-brace-offset)))
  725.         ;; Put chosen indentation into effect.
  726.         (or (= (current-column) this-indent)
  727.         (= (following-char) ?\#)
  728.         (progn
  729.           (delete-region (point) (progn (beginning-of-line) (point)))
  730.           (indent-to this-indent)))
  731.         ;; Indent any comment following the text.
  732.         (or (looking-at comment-start-skip)
  733.         (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
  734.             (progn (indent-for-comment) (beginning-of-line)))))))))
  735. ; (message "Indenting C expression...done")
  736.   )
  737.