home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / c-mode.el < prev    next >
Encoding:
Text File  |  1992-09-08  |  32.2 KB  |  911 lines

  1. ;; C code editing commands for Emacs
  2. ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (defvar c-mode-abbrev-table nil
  22.   "Abbrev table in use in C-mode buffers.")
  23. (define-abbrev-table 'c-mode-abbrev-table ())
  24.  
  25. (defvar c-mode-map ()
  26.   "Keymap used in C mode.")
  27. (if (null c-mode-map)
  28.     (setq c-mode-map (make-sparse-keymap)))
  29.  
  30. (define-key c-mode-map "{" 'electric-c-brace)
  31. (define-key c-mode-map "}" 'electric-c-brace)
  32. (define-key c-mode-map ";" 'electric-c-semi)
  33. (define-key c-mode-map "#" 'electric-c-sharp-sign)
  34. (define-key c-mode-map ":" 'electric-c-terminator)
  35. (define-key c-mode-map "\e{" 'c-insert-braces)
  36. ;;; Commented out eletric square brackets because nobody likes them.
  37. ;;;(define-key c-mode-map "[" 'c-insert-brackets)
  38. (define-key c-mode-map "\e\C-h" 'mark-c-function)
  39. (define-key c-mode-map "\e\C-q" 'indent-c-exp)
  40. (define-key c-mode-map "\eq" 'c-fill-paragraph)
  41. (define-key c-mode-map "\177" 'backward-delete-char-untabify)
  42. (define-key c-mode-map "\t" 'c-indent-command)
  43.  
  44. (autoload 'c-macro-expand "cmacexp"
  45.   "Display the result of expanding all C macros occurring in the region.
  46. The expansion is entirely correct because it uses the C preprocessor."
  47.   t)
  48.  
  49. (defvar c-mode-syntax-table nil
  50.   "Syntax table in use in C-mode buffers.")
  51.  
  52. (if c-mode-syntax-table
  53.     ()
  54.   (setq c-mode-syntax-table (make-syntax-table))
  55.   (modify-syntax-entry ?\\ "\\" c-mode-syntax-table)
  56.   (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
  57.   (modify-syntax-entry ?* ". 23" c-mode-syntax-table)
  58.   (modify-syntax-entry ?+ "." c-mode-syntax-table)
  59.   (modify-syntax-entry ?- "." c-mode-syntax-table)
  60.   (modify-syntax-entry ?= "." c-mode-syntax-table)
  61.   (modify-syntax-entry ?% "." c-mode-syntax-table)
  62.   (modify-syntax-entry ?< "." 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.  
  68. (defconst c-indent-level 2
  69.   "*Indentation of C statements with respect to containing block.")
  70. (defconst c-brace-imaginary-offset 0
  71.   "*Imagined indentation of a C open brace that actually follows a statement.")
  72. (defconst c-brace-offset 0
  73.   "*Extra indentation for braces, compared with other text in same context.")
  74. (defconst c-argdecl-indent 5
  75.   "*Indentation level of declarations of C function arguments.")
  76. (defconst c-label-offset -2
  77.   "*Offset of C label lines and case statements relative to usual indentation.")
  78. (defconst c-continued-statement-offset 2
  79.   "*Extra indent for lines not starting new statements.")
  80. (defconst c-continued-brace-offset 0
  81.   "*Extra indent for substatements that start with open-braces.
  82. This is in addition to c-continued-statement-offset.")
  83.  
  84. (defconst c-auto-newline nil
  85.   "*Non-nil means automatically newline before and after braces,
  86. and after colons and semicolons, inserted in C code.
  87. If you do not want a leading newline before braces then use:
  88.   (define-key c-mode-map \"{\" 'electric-c-semi)")
  89.  
  90. (defconst c-tab-always-indent t
  91.   "*Non-nil means TAB in C mode should always reindent the current line,
  92. regardless of where in the line point is when the TAB command is used.")
  93.  
  94. (defun c-mode ()
  95.   "Major mode for editing C code.
  96. Expression and list commands understand all C brackets.
  97. Tab indents for C code.
  98. Comments are delimited with /* ... */.
  99. Paragraphs are separated by blank lines only.
  100. Delete converts tabs to spaces as it moves back.
  101. \\{c-mode-map}
  102. Variables controlling indentation style:
  103.  c-tab-always-indent
  104.     Non-nil means TAB in C mode should always reindent the current line,
  105.     regardless of where in the line point is when the TAB command is used.
  106.  c-auto-newline
  107.     Non-nil means automatically newline before and after braces,
  108.     and after colons and semicolons, inserted in C code.
  109.  c-indent-level
  110.     Indentation of C statements within surrounding block.
  111.     The surrounding block's indentation is the indentation
  112.     of the line on which the open-brace appears.
  113.  c-continued-statement-offset
  114.     Extra indentation given to a substatement, such as the
  115.     then-clause of an if or body of a while.
  116.  c-continued-brace-offset
  117.     Extra indentation given to a brace that starts a substatement.
  118.     This is in addition to c-continued-statement-offset.
  119.  c-brace-offset
  120.     Extra indentation for line if it starts with an open brace.
  121.  c-brace-imaginary-offset
  122.     An open brace following other text is treated as if it were
  123.     this far to the right of the start of its line.
  124.  c-argdecl-indent
  125.     Indentation level of declarations of C function arguments.
  126.  c-label-offset
  127.     Extra indentation for line that is a label, or case or default.
  128.  
  129. Settings for K&R and BSD indentation styles are
  130.   c-indent-level                5    8
  131.   c-continued-statement-offset  5    8
  132.   c-brace-offset               -5   -8
  133.   c-argdecl-indent              0    8
  134.   c-label-offset               -5   -8
  135.  
  136. Turning on C mode calls the value of the variable c-mode-hook with no args,
  137. if that value is non-nil."
  138.   (interactive)
  139.   (kill-all-local-variables)
  140.   (use-local-map c-mode-map)
  141.   (setq major-mode 'c-mode)
  142.   (setq mode-name "C")
  143.   (setq local-abbrev-table c-mode-abbrev-table)
  144.   (set-syntax-table c-mode-syntax-table)
  145.   (make-local-variable 'paragraph-start)
  146.   (setq paragraph-start (concat "^$\\|" page-delimiter))
  147.   (make-local-variable 'paragraph-separate)
  148.   (setq paragraph-separate paragraph-start)
  149.   (make-local-variable 'paragraph-ignore-fill-prefix)
  150.   (setq paragraph-ignore-fill-prefix t)
  151.   (make-local-variable 'indent-line-function)
  152.   (setq indent-line-function 'c-indent-line)
  153.   (make-local-variable 'indent-region-function)
  154.   (setq indent-region-function 'c-indent-region)
  155.   (make-local-variable 'require-final-newline)
  156.   (setq require-final-newline t)
  157.   (make-local-variable 'comment-start)
  158.   (setq comment-start "/* ")
  159.   (make-local-variable 'comment-end)
  160.   (setq comment-end " */")
  161.   (make-local-variable 'comment-column)
  162.   (setq comment-column 32)
  163.   (make-local-variable 'comment-start-skip)
  164.   (setq comment-start-skip "/\\*+ *")
  165.   (make-local-variable 'comment-indent-hook)
  166.   (setq comment-indent-hook 'c-comment-indent)
  167.   (make-local-variable 'parse-sexp-ignore-comments)
  168.   (setq parse-sexp-ignore-comments t)
  169.   (run-hooks 'c-mode-hook))
  170.  
  171. ;; This is used by indent-for-comment
  172. ;; to decide how much to indent a comment in C code
  173. ;; based on its context.
  174. (defun c-comment-indent ()
  175.   (if (looking-at "^/\\*")
  176.       0                ;Existing comment at bol stays there.
  177.     (let ((opoint (point)))
  178.       (save-excursion
  179.     (beginning-of-line)
  180.     (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
  181.            ;; A comment following a solitary close-brace
  182.            ;; should have only one space.
  183.            (search-forward "}")
  184.            (1+ (current-column)))
  185.           ((or (looking-at "^#[ \t]*endif[ \t]*")
  186.            (looking-at "^#[ \t]*else[ \t]*"))
  187.            7)            ;2 spaces after #endif
  188.           ((progn
  189.          (goto-char opoint)
  190.          (skip-chars-backward " \t")
  191.          (and (= comment-column 0) (bolp)))
  192.            ;; If comment-column is 0, and nothing but space
  193.            ;; before the comment, align it at 0 rather than 1.
  194.            0)
  195.           (t
  196.            (max (1+ (current-column))    ;Else indent at comment column
  197.             comment-column)))))))    ; except leave at least one space.
  198.  
  199. (defun c-fill-paragraph (&optional arg)
  200.   "Like \\[fill-paragraph] but handle C comments.
  201. If point is inside a comment, the current paragraph of the comment
  202. is filled, preserving the comment indentation or line-starting decorations."
  203.   (interactive "P")
  204.   (let ((first-line
  205.      (save-excursion
  206.        (beginning-of-line)
  207.        (skip-chars-forward " \t")
  208.        (looking-at comment-start-skip))))
  209.     (if (or first-line
  210.         (eq (calculate-c-indent) t))
  211.     ;; Inside a comment: fill one comment paragraph.
  212.     (let ((fill-prefix
  213.            ;; The prefix for each line of this paragraph
  214.            ;; is the appropriate part of the start of this line,
  215.            ;; up to the column at which text should be indented.
  216.            (save-excursion
  217.          (beginning-of-line)
  218.          (if (looking-at "[ \t]*/\\*.*\\*/")
  219.              (progn (re-search-forward comment-start-skip)
  220.                 (make-string (current-column) ?\ ))
  221.            (if first-line (forward-line 1))
  222.            (buffer-substring (point)
  223.                      (progn (move-to-column
  224.                          (calculate-c-indent-within-comment t)
  225.                          t)
  226.                         (point))))))
  227.           (paragraph-start
  228.            ;; Lines containing just a comment start or just an end
  229.            ;; should not be filled into paragraphs they are next to.
  230.            (concat paragraph-start
  231.                "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]"))
  232.           (paragraph-separate
  233.            (concat paragraph-separate
  234.                "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]")))
  235.       (save-restriction
  236.         ;; Don't fill the comment together with the code following it.
  237.         (narrow-to-region (point-min)
  238.                   (save-excursion (search-forward "*/" nil 'move)
  239.                           (forward-line 1)
  240.                           (point)))
  241.         (fill-paragraph arg)
  242.         (save-excursion
  243.           (search-forward "*/")
  244.           (beginning-of-line)
  245.           (if (looking-at "[ \t]*\\*/")
  246.           (delete-indentation)))))
  247.       ;; Outside of comments: do ordinary filling.
  248.       (fill-paragraph arg))))
  249.  
  250. (defun electric-c-brace (arg)
  251.   "Insert character and correct line's indentation."
  252.   (interactive "P")
  253.   (let (insertpos)
  254.     (if (and (not arg)
  255.          (eolp)
  256.          (or (save-excursion
  257.            (skip-chars-backward " \t")
  258.            (bolp))
  259.          (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
  260.     (progn
  261.       (insert last-command-char)
  262.       (c-indent-line)
  263.       (if c-auto-newline
  264.           (progn
  265.         (newline)
  266.         ;; (newline) may have done auto-fill
  267.         (setq insertpos (- (point) 2))
  268.         (c-indent-line)))
  269.       (save-excursion
  270.         (if insertpos (goto-char (1+ insertpos)))
  271.         (delete-char -1))))
  272.     (if insertpos
  273.     (save-excursion
  274.       (goto-char insertpos)
  275.       (self-insert-command (prefix-numeric-value arg)))
  276.       (if (> last-command-char 127) ; simply incredible kludge
  277.       (setq last-command-char (- last-command-char 128)))
  278.       (self-insert-command (prefix-numeric-value arg)))))
  279.  
  280. (defun c-insert-brackets ()
  281.   (interactive)
  282.   (insert ?[)
  283.   (save-excursion
  284.     (insert ?])))
  285.  
  286. (defun c-insert-braces ()
  287.   (interactive)
  288.   (electric-c-brace 1)
  289.   (newline)
  290.   (c-indent-line)
  291.   (save-excursion
  292.     (newline)
  293.     (insert ?})
  294.     (c-indent-line)))
  295.  
  296. (defun electric-c-sharp-sign (arg)
  297.   "Insert character and correct line's indentation."
  298.   (interactive "P")
  299.   (if (save-excursion
  300.     (skip-chars-backward " \t")
  301.     (bolp))
  302.       (let ((c-auto-newline nil))
  303.     (electric-c-terminator arg))
  304.     (self-insert-command (prefix-numeric-value arg))))
  305.  
  306. (defun electric-c-semi (arg)
  307.   "Insert character and correct line's indentation."
  308.   (interactive "P")
  309.   (if c-auto-newline
  310.       (electric-c-terminator arg)
  311.     (self-insert-command (prefix-numeric-value arg))))
  312.  
  313. (defun electric-c-terminator (arg)
  314.   "Insert character and correct line's indentation."
  315.   (interactive "P")
  316.   (let (insertpos (end (point)))
  317.     (if (and (not arg) (eolp)
  318.          (not (save-excursion
  319.             (beginning-of-line)
  320.             (skip-chars-forward " \t")
  321.             (or (= (following-char) ?#)
  322.             ;; Colon is special only after a label, or case ....
  323.             ;; So quickly rule out most other uses of colon
  324.             ;; and do no indentation for them.
  325.             (and (eq last-command-char ?:)
  326.                  (not (looking-at "case[ \t'/(]"))
  327.                  (save-excursion
  328.                    (skip-chars-forward "a-zA-Z0-9_$")
  329.                    (skip-chars-forward " \t")
  330.                    (< (point) end)))
  331.             (progn
  332.               (beginning-of-defun)
  333.               (let ((pps (parse-partial-sexp (point) end)))
  334.                 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
  335.     (progn
  336.       (insert last-command-char)
  337.       (c-indent-line)
  338.       (and c-auto-newline
  339.            (not (c-inside-parens-p))
  340.            (progn
  341.          (newline)
  342.          ;; (newline) may have done auto-fill
  343.          (setq insertpos (- (point) 2))
  344.          (c-indent-line)))
  345.       (save-excursion
  346.         (if insertpos (goto-char (1+ insertpos)))
  347.         (delete-char -1))))
  348.     (if insertpos
  349.     (save-excursion
  350.       (goto-char insertpos)
  351.       (self-insert-command (prefix-numeric-value arg)))
  352.       (self-insert-command (prefix-numeric-value arg)))))
  353.  
  354. (defun c-inside-parens-p ()
  355.   (condition-case ()
  356.       (save-excursion
  357.     (save-restriction
  358.       (narrow-to-region (point)
  359.                 (progn (beginning-of-defun) (point)))
  360.       (goto-char (point-max))
  361.       (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
  362.     (error nil)))
  363.  
  364. (defun c-indent-command (&optional whole-exp)
  365.   "Indent current line as C code, or in some cases insert a tab character.
  366. If c-tab-always-indent is non-nil (the default), always indent current line.
  367. Otherwise, indent the current line only if point is at the left margin
  368. or in the line's indentation; otherwise insert a tab.
  369.  
  370. A numeric argument, regardless of its value,
  371. means indent rigidly all the lines of the expression starting after point
  372. so that this line becomes properly indented.
  373. The relative indentation among the lines of the expression are preserved."
  374.   (interactive "P")
  375.   (if whole-exp
  376.       ;; If arg, always indent this line as C
  377.       ;; and shift remaining lines of expression the same amount.
  378.       (let ((shift-amt (c-indent-line))
  379.         beg end)
  380.     (save-excursion
  381.       (if c-tab-always-indent
  382.           (beginning-of-line))
  383.       ;; Find beginning of following line.
  384.       (save-excursion
  385.         (forward-line 1) (setq beg (point)))
  386.       ;; Find first beginning-of-sexp for sexp extending past this line.
  387.       (while (< (point) beg)
  388.         (forward-sexp 1)
  389.         (setq end (point))
  390.         (skip-chars-forward " \t\n")))
  391.     (if (> end beg)
  392.         (indent-code-rigidly beg end shift-amt "#")))
  393.     (if (and (not c-tab-always-indent)
  394.          (save-excursion
  395.            (skip-chars-backward " \t")
  396.            (not (bolp))))
  397.     (insert-tab)
  398.       (c-indent-line))))
  399.  
  400. (defun c-indent-line ()
  401.   "Indent current line as C code.
  402. Return the amount the indentation changed by."
  403.   (let ((indent (calculate-c-indent nil))
  404.     beg shift-amt
  405.     (case-fold-search nil)
  406.     (pos (- (point-max) (point))))
  407.     (beginning-of-line)
  408.     (setq beg (point))
  409.     (cond ((eq indent nil)
  410.        (setq indent (current-indentation)))
  411.       ((eq indent t)
  412.        (setq indent (calculate-c-indent-within-comment)))
  413.       ((looking-at "[ \t]*#")
  414.        (setq indent 0))
  415.       (t
  416.        (skip-chars-forward " \t")
  417.        (if (listp indent) (setq indent (car indent)))
  418.        (cond ((or (looking-at "case[ \t'/(]")
  419.               (and (looking-at "[A-Za-z]")
  420.                (save-excursion
  421.                  (forward-sexp 1)
  422.                  (looking-at ":"))))
  423.           (setq indent (max 1 (+ indent c-label-offset))))
  424.          ((and (looking-at "else\\b")
  425.                (not (looking-at "else\\s_")))
  426.           (setq indent (save-excursion
  427.                  (c-backward-to-start-of-if)
  428.                  (current-indentation))))
  429.          ((and (looking-at "while\\b")
  430.                (save-excursion
  431.              (c-backward-to-start-of-do)))
  432.           ;; This is a `while' that ends a do-while.
  433.           (setq indent (save-excursion
  434.                  (c-backward-to-start-of-do)
  435.                  (current-indentation))))
  436.          ((= (following-char) ?})
  437.           (setq indent (- indent c-indent-level)))
  438.          ((= (following-char) ?{)
  439.           (setq indent (+ indent c-brace-offset))))))
  440.     (skip-chars-forward " \t")
  441.     (setq shift-amt (- indent (current-column)))
  442.     (if (zerop shift-amt)
  443.     (if (> (- (point-max) pos) (point))
  444.         (goto-char (- (point-max) pos)))
  445.       (delete-region beg (point))
  446.       (indent-to indent)
  447.       ;; If initial point was within line's indentation,
  448.       ;; position after the indentation.  Else stay at same point in text.
  449.       (if (> (- (point-max) pos) (point))
  450.       (goto-char (- (point-max) pos))))
  451.     shift-amt))
  452.  
  453. (defun calculate-c-indent (&optional parse-start)
  454.   "Return appropriate indentation for current line as C code.
  455. In usual case returns an integer: the column to indent to.
  456. Returns nil if line starts inside a string, t if in a comment."
  457.   (save-excursion
  458.     (beginning-of-line)
  459.     (let ((indent-point (point))
  460.       (case-fold-search nil)
  461.       state
  462.       containing-sexp)
  463.       (if parse-start
  464.       (goto-char parse-start)
  465.     (beginning-of-defun))
  466.       (while (< (point) indent-point)
  467.     (setq parse-start (point))
  468.     (setq state (parse-partial-sexp (point) indent-point 0))
  469.     (setq containing-sexp (car (cdr state))))
  470.       (cond ((or (nth 3 state) (nth 4 state))
  471.          ;; return nil or t if should not change this line
  472.          (nth 4 state))
  473.         ((null containing-sexp)
  474.          ;; Line is at top level.  May be data or function definition,
  475.          ;; or may be function argument declaration.
  476.          ;; Indent like the previous top level line
  477.          ;; unless that ends in a closeparen without semicolon,
  478.          ;; in which case this line is the first argument decl.
  479.          (goto-char indent-point)
  480.          (skip-chars-forward " \t")
  481.          (if (= (following-char) ?{)
  482.          0   ; Unless it starts a function body
  483.            (c-backward-to-noncomment (or parse-start (point-min)))
  484.            ;; Look at previous line that's at column 0
  485.            ;; to determine whether we are in top-level decls
  486.            ;; or function's arg decls.  Set basic-indent accordingly.
  487.            (let ((basic-indent
  488.               (save-excursion
  489.             (re-search-backward "^[^ \^L\t\n#]" nil 'move)
  490.             (if (and (looking-at "\\sw\\|\\s_")
  491.                  (looking-at "[^\"\n=]*(")
  492.                  (progn
  493.                    (goto-char (1- (match-end 0)))
  494.                    (forward-sexp 1)
  495.                    (and (< (point) indent-point)
  496.                     (not (memq (following-char)
  497.                            '(?\, ?\;))))))
  498.                 c-argdecl-indent 0))))
  499.          basic-indent)))
  500.  
  501. ;;          ;; Now add a little if this is a continuation line.
  502. ;;          (+ basic-indent (if (or (bobp)
  503. ;;                      (memq (preceding-char) '(?\) ?\; ?\}))
  504. ;;                      ;; Line with zero indentation
  505. ;;                      ;; is probably the return-type
  506. ;;                      ;; of a function definition,
  507. ;;                      ;; so following line is function name.
  508. ;;                      (= (current-indentation) 0))
  509. ;;                     0 c-continued-statement-offset))
  510.  
  511.         ((/= (char-after containing-sexp) ?{)
  512.          ;; line is expression, not statement:
  513.          ;; indent to just after the surrounding open.
  514.          (goto-char (1+ containing-sexp))
  515.          (current-column))
  516.         (t
  517.          ;; Statement level.  Is it a continuation or a new statement?
  518.          ;; Find previous non-comment character.
  519.          (goto-char indent-point)
  520.          (c-backward-to-noncomment containing-sexp)
  521.          ;; Back up over label lines, since they don't
  522.          ;; affect whether our line is a continuation.
  523.          (while (or (eq (preceding-char) ?\,)
  524.             (and (eq (preceding-char) ?:)
  525.                  (or (eq (char-after (- (point) 2)) ?\')
  526.                  (memq (char-syntax (char-after (- (point) 2)))
  527.                        '(?w ?_)))))
  528.            (if (eq (preceding-char) ?\,)
  529.            (progn (forward-char -1)
  530.               (c-backward-to-start-of-continued-exp containing-sexp)))
  531.            (beginning-of-line)
  532.            (c-backward-to-noncomment containing-sexp))
  533.          ;; Now we get the answer.
  534.          (if (and (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{)))
  535.               ;; But don't treat a line with a close-brace
  536.               ;; as a continuation.  It is probably the
  537.               ;; end of an enum type declaration.
  538.               (save-excursion
  539.             (goto-char indent-point)
  540.             (skip-chars-forward " \t")
  541.             (not (= (following-char) ?}))))
  542.          ;; This line is continuation of preceding line's statement;
  543.          ;; indent  c-continued-statement-offset  more than the
  544.          ;; previous line of the statement.
  545.          (progn
  546.            (c-backward-to-start-of-continued-exp containing-sexp)
  547.            (+ c-continued-statement-offset (current-column)
  548.               (if (save-excursion (goto-char indent-point)
  549.                       (skip-chars-forward " \t")
  550.                       (eq (following-char) ?{))
  551.               c-continued-brace-offset 0)))
  552.            ;; This line starts a new statement.
  553.            ;; Position following last unclosed open.
  554.            (goto-char containing-sexp)
  555.            ;; Is line first statement after an open-brace?
  556.            (or
  557.          ;; If no, find that first statement and indent like it.
  558.          (save-excursion
  559.            (forward-char 1)
  560.            (let ((colon-line-end 0))
  561.              (while (progn (skip-chars-forward " \t\n")
  562.                    (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
  563.                ;; Skip over comments and labels following openbrace.
  564.                (cond ((= (following-char) ?\#)
  565.                   (forward-line 1))
  566.                  ((= (following-char) ?\/)
  567.                   (forward-char 2)
  568.                   (search-forward "*/" nil 'move))
  569.                  ;; case or label:
  570.                  (t
  571.                   (save-excursion (end-of-line)
  572.                           (setq colon-line-end (point)))
  573.                   (search-forward ":"))))
  574.              ;; The first following code counts
  575.              ;; if it is before the line we want to indent.
  576.              (and (< (point) indent-point)
  577.               (if (> colon-line-end (point))
  578.                   (- (current-indentation) c-label-offset)
  579.                 (current-column)))))
  580.          ;; If no previous statement,
  581.          ;; indent it relative to line brace is on.
  582.          ;; For open brace in column zero, don't let statement
  583.          ;; start there too.  If c-indent-level is zero,
  584.          ;; use c-brace-offset + c-continued-statement-offset instead.
  585.          ;; For open-braces not the first thing in a line,
  586.          ;; add in c-brace-imaginary-offset.
  587.          (+ (if (and (bolp) (zerop c-indent-level))
  588.             (+ c-brace-offset c-continued-statement-offset)
  589.               c-indent-level)
  590.             ;; Move back over whitespace before the openbrace.
  591.             ;; If openbrace is not first nonwhite thing on the line,
  592.             ;; add the c-brace-imaginary-offset.
  593.             (progn (skip-chars-backward " \t")
  594.                (if (bolp) 0 c-brace-imaginary-offset))
  595.             ;; If the openbrace is preceded by a parenthesized exp,
  596.             ;; move to the beginning of that;
  597.             ;; possibly a different line
  598.             (progn
  599.               (if (eq (preceding-char) ?\))
  600.               (forward-sexp -1))
  601.               ;; Get initial indentation of the line we are on.
  602.               (current-indentation))))))))))
  603.  
  604. (defun calculate-c-indent-within-comment (&optional after-star)
  605.   "Return the indentation amount for line inside a block comment.
  606. Non-nil arg AFTER-STAR means, if lines in the comment have a leading star,
  607. return the indentation of the text that would follow this star."
  608.   (let (end star-start)
  609.     (save-excursion
  610.       (beginning-of-line)
  611.       (skip-chars-forward " \t")
  612.       (setq star-start (= (following-char) ?\*))
  613.       (skip-chars-backward " \t\n")
  614.       (setq end (point))
  615.       (beginning-of-line)
  616.       (skip-chars-forward " \t")
  617.       (if after-star
  618.       (and (looking-at "\\*")
  619.            (re-search-forward "\\*[ \t]*")))
  620.       (and (re-search-forward "/\\*[ \t]*" end t)
  621.        star-start
  622.        (not after-star)
  623.        (goto-char (1+ (match-beginning 0))))
  624.       (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*))
  625.       (1+ (current-column))
  626.     (current-column)))))
  627.  
  628.  
  629. (defun c-backward-to-noncomment (lim)
  630.   (let (opoint stop)
  631.     (while (not stop)
  632.       (skip-chars-backward " \t\n\f" lim)
  633.       (setq opoint (point))
  634.       (if (and (>= (point) (+ 2 lim))
  635.            (save-excursion
  636.          (forward-char -2)
  637.          (looking-at "\\*/")))
  638.       (search-backward "/*" lim 'move)
  639.     (setq stop (or (<= (point) lim)
  640.                (save-excursion
  641.              (while (progn
  642.                   (beginning-of-line)
  643.                   (eq ?\\ (char-after (- (point) 2))))
  644.                (forward-char -1)
  645.                (beginning-of-line))
  646.              (skip-chars-forward " \t")
  647.              (not (looking-at "#")))))
  648.     (or stop (beginning-of-line))))))
  649.  
  650. (defun c-backward-to-start-of-continued-exp (lim)
  651.   (if (memq (preceding-char) '(?\) ?\"))
  652.       (forward-sexp -1))
  653.   (beginning-of-line)
  654.   (if (<= (point) lim)
  655.       (goto-char (1+ lim)))
  656.   (skip-chars-forward " \t"))
  657.  
  658. (defun c-backward-to-start-of-if (&optional limit)
  659.   "Move to the start of the last ``unbalanced'' if."
  660.   (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
  661.   (let ((if-level 1)
  662.     (case-fold-search nil))
  663.     (while (and (not (bobp)) (not (zerop if-level)))
  664.       (backward-sexp 1)
  665.       (cond ((looking-at "else\\b")
  666.          (setq if-level (1+ if-level)))
  667.         ((looking-at "if\\b")
  668.          (setq if-level (1- if-level)))
  669.         ((< (point) limit)
  670.          (setq if-level 0)
  671.          (goto-char limit))))))
  672.  
  673. (defun c-backward-to-start-of-do (&optional limit)
  674.   "If point follows a `do' statement, move to beginning of it and return `t'.
  675. Otherwise return `nil' and don't move point."
  676.   (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
  677.   (let ((first t)
  678.     (startpos (point))
  679.     (done nil))
  680.     (while (not done)
  681.       (let ((next-start (point)))
  682.     (condition-case nil
  683.         ;; Move back one token or one brace or paren group.
  684.         (backward-sexp 1)
  685.       ;; If we find an open-brace, we lose.
  686.       (error (setq done 'fail)))
  687.     (if done
  688.         nil
  689.       ;; If we reached a `do', we win.
  690.       (if (looking-at "do\\b")
  691.           (setq done 'succeed)
  692.         ;; Otherwise, if we skipped a semicolon, we lose.
  693.         ;; (Exception: we can skip one semicolon before getting
  694.         ;; to a the last token of the statement, unless that token
  695.         ;; is a close brace.)
  696.         (if (save-excursion
  697.           (forward-sexp 1)
  698.           (or (and (not first) (= (preceding-char) ?}))
  699.               (search-forward ";" next-start t
  700.                       (if (and first
  701.                            (/= (preceding-char) ?}))
  702.                       2 1))))
  703.         (setq done 'fail)
  704.           (setq first nil)
  705.           ;; If we go too far back in the buffer, we lose.
  706.           (if (< (point) limit)
  707.           (setq done 'fail)))))))
  708.     (if (eq done 'succeed)
  709.     t
  710.       (goto-char startpos)
  711.       nil)))
  712.  
  713. (defun mark-c-function ()
  714.   "Put mark at end of C function, point at beginning."
  715.   (interactive)
  716.   (push-mark (point))
  717.   (end-of-defun)
  718.   (push-mark (point))
  719.   (beginning-of-defun)
  720.   (backward-paragraph)
  721.   (zmacs-activate-region))
  722.  
  723. (defun indent-c-exp (&optional endpos)
  724.   "Indent each line of the C grouping following point.
  725. If optional arg ENDPOS is given, indent each line, stopping when
  726. ENDPOS is encountered."
  727.   (interactive)
  728.   (let* ((indent-stack (list nil))
  729.      (opoint (point))  ;; May be altered below.
  730.      (contain-stack
  731.       (list (if endpos
  732.             (let (funbeg)
  733.               ;; Find previous fcn-start.
  734.               (save-excursion (forward-char 1)
  735.                       (beginning-of-defun)
  736.                       (setq funbeg (point)))
  737.               ;; Try to find containing open,
  738.               ;; but don't scan past that fcn-start.
  739.               (save-restriction
  740.             (narrow-to-region funbeg (point))
  741.             (condition-case nil
  742.                 (save-excursion
  743.                   (backward-up-list 1) (point))
  744.               ;; We gave up: must be between fcns.
  745.               ;; Set opoint to beg of prev fcn
  746.               ;; since otherwise calculate-c-indent
  747.               ;; will get wrong answers.
  748.               (error (setq opoint funbeg)
  749.                  (point)))))
  750.           (point))))
  751.      (case-fold-search nil)
  752.      restart outer-loop-done inner-loop-done state ostate
  753.      this-indent last-sexp
  754.      at-else at-brace at-while
  755.      last-depth
  756.      (next-depth 0))
  757.     ;; If the braces don't match, get an error right away.
  758.     (save-excursion
  759.       (forward-sexp 1))
  760.     ;; Realign the comment on the first line, even though we don't reindent it.
  761.     (save-excursion
  762.       (let ((beg (point)))
  763.     (and (re-search-forward
  764.           comment-start-skip
  765.           (save-excursion (end-of-line) (point)) t)
  766.          ;; Make sure the comment starter we found
  767.          ;; is not actually in a string or quoted.
  768.          (let ((new-state
  769.             (parse-partial-sexp beg (point)
  770.                     nil nil state)))
  771.            (and (not (nth 3 new-state)) (not (nth 5 new-state))))
  772.         (progn (indent-for-comment) (beginning-of-line)))))
  773.     (save-excursion
  774.       (setq outer-loop-done nil)
  775.       (while (and (not (eobp))
  776.           (if endpos (< (point) endpos)
  777.             (not outer-loop-done)))
  778.     (setq last-depth next-depth)
  779.     ;; Compute how depth changes over this line
  780.     ;; plus enough other lines to get to one that
  781.     ;; does not end inside a comment or string.
  782.     ;; Meanwhile, do appropriate indentation on comment lines.
  783.     (setq inner-loop-done nil)
  784.     (while (and (not inner-loop-done)
  785.             (not (and (eobp) (setq outer-loop-done t))))
  786.       (setq ostate state)
  787.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  788.                       nil nil state))
  789.       (setq next-depth (car state))
  790.       (if (and (car (cdr (cdr state)))
  791.            (>= (car (cdr (cdr state))) 0))
  792.           (setq last-sexp (car (cdr (cdr state)))))
  793.       (if (or (nth 4 ostate))
  794.           (c-indent-line))
  795.       (if (or (nth 3 state))
  796.           (forward-line 1)
  797.         (setq inner-loop-done t)))
  798.     (and endpos
  799.          (while (< next-depth 0)
  800.            (setq indent-stack (append indent-stack (list nil)))
  801.            (setq contain-stack (append contain-stack (list nil)))
  802.            (setq next-depth (1+ next-depth))
  803.            (setq last-depth (1+ last-depth))
  804.            (setcar (nthcdr 6 state) (1+ (nth 6 state)))))
  805.     (setq outer-loop-done (and (not endpos) (<= next-depth 0)))
  806.     (if outer-loop-done
  807.         nil
  808.       ;; If this line had ..))) (((.. in it, pop out of the levels
  809.       ;; that ended anywhere in this line, even if the final depth
  810.       ;; doesn't indicate that they ended.
  811.       (while (> last-depth (nth 6 state))
  812.         (setq indent-stack (cdr indent-stack)
  813.           contain-stack (cdr contain-stack)
  814.           last-depth (1- last-depth)))
  815.       (if (/= last-depth next-depth)
  816.           (setq last-sexp nil))
  817.       ;; Add levels for any parens that were started in this line.
  818.       (while (< last-depth next-depth)
  819.         (setq indent-stack (cons nil indent-stack)
  820.           contain-stack (cons nil contain-stack)
  821.           last-depth (1+ last-depth)))
  822.       (if (null (car contain-stack))
  823.           (setcar contain-stack (or (car (cdr state))
  824.                     (save-excursion (forward-sexp -1)
  825.                             (point)))))
  826.       (forward-line 1)
  827.       (skip-chars-forward " \t")
  828.       (if (eolp)
  829.           nil
  830.         (if (and (car indent-stack)
  831.              (>= (car indent-stack) 0))
  832.         ;; Line is on an existing nesting level.
  833.         ;; Lines inside parens are handled specially.
  834.         (if (/= (char-after (car contain-stack)) ?{)
  835.             (setq this-indent (car indent-stack))
  836.           ;; Line is at statement level.
  837.           ;; Is it a new statement?  Is it an else?
  838.           ;; Find last non-comment character before this line
  839.           (save-excursion
  840.             (setq at-else (looking-at "else\\W"))
  841.             (setq at-brace (= (following-char) ?{))
  842.             (setq at-while (looking-at "while\\b"))
  843.             (c-backward-to-noncomment opoint)
  844.             (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{)))
  845.             ;; Preceding line did not end in comma or semi;
  846.             ;; indent this line  c-continued-statement-offset
  847.             ;; more than previous.
  848.             (progn
  849.               (c-backward-to-start-of-continued-exp (car contain-stack))
  850.               (setq this-indent
  851.                 (+ c-continued-statement-offset (current-column)
  852.                    (if at-brace c-continued-brace-offset 0))))
  853.               ;; Preceding line ended in comma or semi;
  854.               ;; use the standard indent for this level.
  855.               (cond (at-else (progn (c-backward-to-start-of-if opoint)
  856.                         (setq this-indent
  857.                           (current-indentation))))
  858.                 ((and at-while (c-backward-to-start-of-do opoint))
  859.                  (setq this-indent (current-indentation)))
  860.                 (t (setq this-indent (car indent-stack)))))))
  861.           ;; Just started a new nesting level.
  862.           ;; Compute the standard indent for this level.
  863.           (let ((val (calculate-c-indent
  864.                (if (car indent-stack)
  865.                    (- (car indent-stack))
  866.                  opoint))))
  867.         (setcar indent-stack
  868.             (setq this-indent val))))
  869.         ;; Adjust line indentation according to its contents
  870.         (if (or (looking-at "case[ \t'/(]")
  871.             (and (looking-at "[A-Za-z]")
  872.              (save-excursion
  873.                (forward-sexp 1)
  874.                (looking-at ":"))))
  875.         (setq this-indent (max 1 (+ this-indent c-label-offset))))
  876.         (if (= (following-char) ?})
  877.         (setq this-indent (- this-indent c-indent-level)))
  878.         (if (= (following-char) ?{)
  879.         (setq this-indent (+ this-indent c-brace-offset)))
  880.         ;; Don't leave indentation in empty lines.
  881.         (if (eolp) (setq this-indent 0))
  882.         ;; Put chosen indentation into effect.
  883.         (or (= (current-column) this-indent)
  884.         (= (following-char) ?\#)
  885.         (progn
  886.           (delete-region (point) (progn (beginning-of-line) (point)))
  887.           (indent-to this-indent)))
  888.         ;; Indent any comment following the text.
  889.         (or (looking-at comment-start-skip)
  890.         (let ((beg (point)))
  891.           (and (re-search-forward
  892.             comment-start-skip
  893.             (save-excursion (end-of-line) (point)) t)
  894.                ;; Make sure the comment starter we found
  895.                ;; is not actually in a string or quoted.
  896.                (let ((new-state
  897.                   (parse-partial-sexp beg (point)
  898.                           nil nil state)))
  899.              (and (not (nth 3 new-state)) (not (nth 5 new-state))))
  900.               (progn (indent-for-comment) (beginning-of-line)))))))))))
  901.  
  902. ;; Indent every line whose first char is between START and END inclusive.
  903. (defun c-indent-region (start end)
  904.   (save-excursion
  905.     (goto-char start)
  906.     (let ((endmark (copy-marker end)))
  907.       (and (bolp) (not (eolp))
  908.        (c-indent-line))
  909.       (indent-c-exp endmark)
  910.       (set-marker endmark nil))))
  911.