home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / lisp / cplus-md.el < prev    next >
Text File  |  1996-09-28  |  35KB  |  919 lines

  1. ;;; cplus-md.el --- C++ code editing commands for Emacs
  2. ;;; Copyright (C) 1985, 1992, 1994 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. ;; Maintainer: Dave Detlefs <dld@cs.cmu.edu>
  20. ;; Keywords: c
  21.  
  22. ;;; Commentary:
  23.  
  24. ;; 1987 Dave Detlefs  <dld@cs.cmu.edu> 
  25. ;; and  Stewart Clamen <clamen@cs.cmu.edu>.
  26. ;; Done by fairly faithful modification of:
  27.  
  28. ;;; Change Log:
  29.  
  30. ;; Feb, 1990 (Dave Detlefs, dld@cs.cmu.edu)
  31. ;;   Fixed electric-c++-terminator to handle double colons, at the
  32. ;;   request of John Hagerman.
  33. ;; 
  34. ;; Jan, 1990 (Doug Lea, dl@oswego.edu)
  35. ;;   Replaced c++-comment-region and c++-uncomment-region with
  36. ;;     versions from Igor Metz that avoid potential infinite loops.
  37. ;;
  38. ;; Oct, 1989 (Dave Detlefs, dld@cs.cmu.edu)
  39. ;;   Added contribution from Igor Metz <metz@iam.unibe.ch>:
  40. ;;     functions c++-comment-region and c++-uncomment-region and 
  41. ;;     corresponding key-binding.
  42. ;;   Also fixed bug in indentation of second line after an empty
  43. ;;   arglist with empty-arglist non-null.
  44. ;;   
  45. ;; Sept, 1989 (Glen Ditchfield, gjditchfield@violet.uwaterloo.ca):
  46. ;;   Textual changes to more closely imitate Emacs 18.55's c-mode.
  47. ;;   Fixed handling of "default:", where ":" was the last character in the
  48. ;;   buffer.  Fixed indentation of comments starting in column 0, and when
  49. ;;   previous line contained more than one comment start string.  Fixed
  50. ;;   handling of "friend".
  51. ;;
  52. ;; Aug 7, 1989; John Hagerman (hagerman@ece.cmu.edu):
  53. ;;   Changed calculate-c++-indent to handle member initializations
  54. ;;   more flexibly. Two new variables are used to control behavior:
  55. ;;   c++-member-init-indent and c++-continued-member-init-offset.
  56. ;;   Note the assumption that member initializations and argument
  57. ;;   declarations are not mixed in one function definition.
  58. ;;
  59. ;; June 1989 (Dave Detlefs, dld@cs.cmu.edu)
  60. ;;   Fixed calculate-c++-indent to handle continued lines ending in
  61. ;;   {'s.  (I wasn't following C-mode closely enough, or C-mode
  62. ;;   changed.)  Made ' a quote character, at the behest of someone
  63. ;;   whose mail I apparently deleted (if they send me mail I'll credit
  64. ;;   them here in a future revision.)
  65. ;;   Dan Weinreb (dlw@odi.com) pointed out that 'c++-mode successively
  66. ;;   bound c++-indent-exp and c++-indent-defun to ESC-^q.  ESC-^q is
  67. ;;   now bound to c++-indent-exp, while, c++-indent-defun is invoked
  68. ;;   with ESC-^x.
  69.  
  70. ;; February 1989 (Dave Detlefs, dld@cs.cmu.edu)
  71. ;;   Fixed some errors in c++-indent-defun, as pointed out by Sam
  72. ;;   Haradhvala (odi!sam@talcott.harvard.edu).
  73. ;; October 1988 (Dave Detlefs, dld@cs.cmu.edu)
  74. ;;   It turns out I had only *thought* I had made
  75. ;;   beginning(end)-of-defun work.  It should work better now -- you
  76. ;;   can either attempt to match defun headers "strongly," using a
  77. ;;   very complicated regexp, or "weakly," using a simple one.  This
  78. ;;   is settable by a variable; the default is the cheaper weak
  79. ;;   method.  (Stewart Clamen was intimately involved in this, too.)
  80. ;;
  81. ;;   I made "'" *not* be a string delimiter, because that was causing
  82. ;;   comments containing contractions to ("// don't") to mess up paren
  83. ;;   balancing.
  84. ;;
  85. ;;   I also incorporated another slight indentation fix from Glen
  86. ;;   Ditchfield.
  87. ;;
  88. ;;   We hope this is will make into version 19 of gnu-emacs.
  89. ;;
  90. ;; September 1988: incorporated changes from Fred Calm at Schlumberger.
  91. ;;   Also, made beginning(end)-of-defun, indent-defun work.
  92. ;;
  93. ;; August 1987: incorporated changes done by Glen Ditchfield of Waterloo.
  94.  
  95. ;;; Code:
  96.  
  97. (defvar c++-mode-abbrev-table nil
  98.   "Abbrev table used in C++ mode.")
  99. (define-abbrev-table 'c++-mode-abbrev-table ())
  100.  
  101. (defvar c++-mode-map ()
  102.   "Keymap used in C++ mode.")
  103. (if c++-mode-map
  104.     ()
  105.   (setq c++-mode-map (make-sparse-keymap))
  106.   (define-key c++-mode-map "\C-j" 'reindent-then-newline-and-indent)
  107.   (define-key c++-mode-map "{" 'electric-c++-brace)
  108.   (define-key c++-mode-map "}" 'electric-c++-brace)
  109.   (define-key c++-mode-map ";" 'electric-c++-semi)
  110.   (define-key c++-mode-map "\e\C-h" 'mark-c-function)
  111.   (define-key c++-mode-map "\e\C-q" 'indent-c++-exp)
  112.   (define-key c++-mode-map "\177" 'backward-delete-char-untabify)
  113.   (define-key c++-mode-map "\t" 'c++-indent-command)
  114. ;;   (define-key c++-mode-map "\C-c\C-i" 'c++-insert-header)
  115.   (define-key c++-mode-map "\C-c\C-\\" 'c-backslash-region))
  116. ;;   (define-key c++-mode-map "\e\C-a" 'c++-beginning-of-defun)
  117. ;;   (define-key c++-mode-map "\e\C-e" 'c++-end-of-defun)
  118. ;;   (define-key c++-mode-map "\e\C-x" 'c++-indent-defun))
  119.  
  120. (defvar c++-mode-syntax-table nil
  121.   "Syntax table used in C++ mode.")
  122.  
  123. (if c++-mode-syntax-table
  124.     ()
  125.   (setq c++-mode-syntax-table (copy-syntax-table c-mode-syntax-table))
  126.   (modify-syntax-entry ?* ". 23b" c++-mode-syntax-table)
  127.   (modify-syntax-entry ?/ ". 124" c++-mode-syntax-table)
  128.   (modify-syntax-entry ?\n ">" c++-mode-syntax-table)
  129.   (modify-syntax-entry ?\^m ">" c++-mode-syntax-table))
  130.  
  131. (defvar c++-continued-member-init-offset nil
  132.   "*Extra indent for continuation lines of member inits;
  133. nil means to align with previous initializations rather than
  134. with the colon on the first line.")
  135. (defvar c++-member-init-indent 0
  136.   "*Indentation level of member initializations in function declarations.")
  137. (defvar c++-friend-offset -4
  138.   "*Offset of C++ friend declarations relative to member declarations.")
  139. (defvar c++-electric-colon t
  140.   "*If t, colon is an electric terminator.")
  141. (defvar c++-empty-arglist-indent nil
  142.   "*Indicates how far to indent an line following an empty argument
  143. list.  Nil indicates to just after the paren.")
  144.  
  145.  
  146. ;;;###autoload
  147. (defun c++-mode ()
  148.   "Major mode for editing C++ code.  Very much like editing C code.
  149. Expression and list commands understand all C++ brackets.
  150. Tab at left margin indents for C++ code
  151. Comments are delimited with /* ... */ {or with // ... <newline>}
  152. Paragraphs are separated by blank lines only.
  153. Delete converts tabs to spaces as it moves back.
  154. \\{c++-mode-map}
  155. Variables controlling indentation style:
  156.  c-tab-always-indent
  157.     Non-nil means TAB in C mode should always reindent the current line,
  158.     regardless of where in the line point is when the TAB command is used.
  159.     Default is t.
  160.  c-auto-newline
  161.     Non-nil means automatically newline before and after braces,
  162.     and after colons and semicolons, inserted in C code.
  163.  c-indent-level
  164.     Indentation of C statements within surrounding block.
  165.     The surrounding block's indentation is the indentation
  166.     of the line on which the open-brace appears.
  167.  c-continued-statement-offset
  168.     Extra indentation given to a substatement, such as the
  169.     then-clause of an if or body of a while.
  170.  c-continued-brace-offset
  171.     Extra indentation given to a brace that starts a substatement.
  172.     This is in addition to c-continued-statement-offset.
  173.  c-brace-offset
  174.     Extra indentation for line if it starts with an open brace.
  175.  c-brace-imaginary-offset
  176.     An open brace following other text is treated as if it were
  177.     this far to the right of the start of its line.
  178.  c-argdecl-indent
  179.     Indentation level of declarations of C function arguments.
  180.  c-label-offset
  181.     Extra indentation for line that is a label, or case or ``default:'', or
  182.     ``public:'' or ``private:'', or ``protected:''.
  183.  c++-electric-colon
  184.     If non-nil at invocation of c++-mode (t is the default) colon electricly
  185.     indents.
  186.  c++-empty-arglist-indent
  187.     If non-nil, a function declaration or invocation which ends a line with a
  188.     left paren is indented this many extra spaces, instead of flush with the
  189.     left paren.
  190.  c++-friend-offset
  191.     Offset of C++ friend declarations relative to member declarations.
  192.  c++-member-init-indent
  193.     Indentation level of member initializations in function declarations,
  194.     if they are on a separate line beginning with a colon.
  195.  c++-continued-member-init-offset
  196.     Extra indentation for continuation lines of member initializations; NIL
  197.     means to align with previous initializations rather than with the colon.
  198.  
  199. Settings for K&R, BSD, and Stroustrup indentation styles are
  200.   c-indent-level                5    8    4
  201.   c-continued-statement-offset  5    8    4
  202.   c-continued-brace-offset                0
  203.   c-brace-offset               -5   -8    0
  204.   c-brace-imaginary-offset                0
  205.   c-argdecl-indent              0    8    4
  206.   c-label-offset               -5   -8   -4
  207.   c++-empty-arglist-indent                4
  208.   c++-friend-offset                       0
  209.  
  210. Turning on C++ mode calls the value of the variable `c++-mode-hook' with
  211. no args if that value is non-nil."
  212.   (interactive)
  213.   (kill-all-local-variables)
  214.   (use-local-map c++-mode-map)
  215.   (set-syntax-table c++-mode-syntax-table)
  216.   (setq major-mode 'c++-mode
  217.     mode-name "C++"
  218.     comment-column 32
  219.     local-abbrev-table c++-mode-abbrev-table)
  220.   (set (make-local-variable 'indent-line-function) 'c++-indent-line)
  221.   (set (make-local-variable 'comment-start) "// ")
  222.   (set (make-local-variable 'comment-end) "")
  223.   (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
  224.   (set (make-local-variable 'comment-indent-function) 'c++-comment-indent)
  225.   (set (make-local-variable 'paragraph-start) (concat "^$\\|" page-delimiter))
  226.   (set (make-local-variable 'paragraph-separate) paragraph-start)
  227.   (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
  228.   (set (make-local-variable 'require-final-newline) t)
  229.   (set (make-local-variable 'parse-sexp-ignore-comments) t)
  230.   (run-hooks 'c++-mode-hook)
  231.   (if c++-electric-colon
  232.       (define-key c++-mode-map ":" 'electric-c++-terminator)))
  233.  
  234. ;; This is used by indent-for-comment
  235. ;; to decide how much to indent a comment in C++ code
  236. ;; based on its context.
  237. (defun c++-comment-indent ()
  238.   (if (looking-at "^\\(/\\*\\|//\\)")
  239.       0                    ; Existing comment at bol stays there.
  240.     (save-excursion
  241.       (skip-chars-backward " \t")
  242.       (max
  243.        ;; leave at least one space on non-empty lines.
  244.        (if (zerop (current-column)) 0 (1+ (current-column)))
  245.        (let ((cur-pt (point)))
  246.      (beginning-of-line 0)
  247.      ;; If previous line had a comment, use it's indent
  248.      (if (re-search-forward comment-start-skip cur-pt t)
  249.          (progn
  250.            (goto-char (match-beginning 0))
  251.            (current-column))
  252.        comment-column))))))        ; otherwise indent at comment column.
  253.  
  254. (defun electric-c++-brace (arg)
  255.   "Insert character and correct line's indentation."
  256.   (interactive "P")
  257.   (let (insertpos)
  258.     (if (and (not arg)
  259.          (eolp)
  260.          (or (save-excursion
  261.            (skip-chars-backward " \t")
  262.            (bolp))
  263.          (if c-auto-newline (progn (c++-indent-line) (newline) t))))
  264.     (progn
  265.       (insert last-command-char)
  266.       (c++-indent-line)
  267.       (if c-auto-newline
  268.           (progn
  269.         (newline)
  270.         ;; (newline) may have done auto-fill
  271.         (setq insertpos (- (point) 2))
  272.         (c++-indent-line)))
  273.       (save-excursion
  274.         (if insertpos (goto-char (1+ insertpos)))
  275.         (delete-char -1))))
  276.     (if insertpos
  277.     (save-excursion
  278.       (goto-char insertpos)
  279.       (self-insert-command (prefix-numeric-value arg)))
  280.       (self-insert-command (prefix-numeric-value arg)))))
  281.  
  282. (defun electric-c++-semi (arg)
  283.   "Insert character and correct line's indentation."
  284.   (interactive "P")
  285.   (if c-auto-newline
  286.       (electric-c++-terminator arg)
  287.     (self-insert-command (prefix-numeric-value arg))))
  288.  
  289. (defun electric-c++-terminator (arg)
  290.   "Insert character and correct line's indentation."
  291.   (interactive "P")
  292.   (let (insertpos (end (point)))
  293.     (if (and (not arg) (eolp)
  294.          (not (save-excursion
  295.             (beginning-of-line)
  296.             (skip-chars-forward " \t")
  297.             (or (= (following-char) ?#)
  298.             ;; Colon is special only after a label, or
  299.             ;; case, or another colon.
  300.             ;; So quickly rule out most other uses of colon
  301.             ;; and do no indentation for them.
  302.             (and (eq last-command-char ?:)
  303.                  (not (looking-at "case[ \t]"))
  304.                  (save-excursion
  305.                    (forward-word 1)
  306.                    (skip-chars-forward " \t")
  307.                    (< (point) end))
  308.                  ;; Do re-indent double colons
  309.                  (save-excursion
  310.                    (end-of-line 1)
  311.                    (looking-at ":")))
  312.             (progn
  313.               (beginning-of-defun)
  314.               (let ((pps (parse-partial-sexp (point) end)))
  315.                 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
  316.     (progn
  317.       (insert last-command-char)
  318.       (c++-indent-line)
  319.       (and c-auto-newline
  320.            (not (c-inside-parens-p))
  321.            (progn
  322.          ;; the new marker object, used to be just an integer
  323.          (setq insertpos (make-marker))
  324.          ;; changed setq to set-marker
  325.          (set-marker insertpos (1- (point)))
  326.          ;; do this before the newline, since in auto fill can break
  327.          (newline)
  328.          (c-indent-line)))
  329.       (save-excursion
  330.         (if insertpos (goto-char (1+ insertpos)))
  331.         (delete-char -1))))
  332.     (if insertpos
  333.     (save-excursion
  334.       (goto-char insertpos)
  335.       (self-insert-command (prefix-numeric-value arg)))
  336.       (self-insert-command (prefix-numeric-value arg)))))
  337.  
  338. (defun c++-indent-command (&optional whole-exp)
  339.   "Indent current line as C++ code, or in some cases insert a tab character.
  340. If `c-tab-always-indent' is non-nil (the default), always indent current
  341. line.  Otherwise, indent the current line only if point is at the left
  342. margin or in the line's indentation; otherwise insert a tab.
  343.  
  344. A numeric argument, regardless of its value, means indent rigidly all means
  345. indent rigidly all the lines of the expression starting after point so that
  346. this line becomes properly indented.  The relative indentation among the
  347. lines of the expression are preserved."
  348.   (interactive "P")
  349.   (if whole-exp
  350.       ;; If arg, always indent this line as C
  351.       ;; and shift remaining lines of expression the same amount.
  352.       (let ((shift-amt (c++-indent-line))
  353.         beg end)
  354.     (save-excursion
  355.       (if c-tab-always-indent
  356.           (beginning-of-line))
  357.       (setq beg (point))
  358.       (forward-sexp 1)
  359.       (setq end (point))
  360.       (goto-char beg)
  361.       (forward-line 1)
  362.       (setq beg (point)))
  363.     (if (> end beg)
  364.         (indent-code-rigidly beg end shift-amt "#")))
  365.     (if (and (not c-tab-always-indent)
  366.          (save-excursion
  367.            (skip-chars-backward " \t")
  368.            (not (bolp))))
  369.     (insert-tab)
  370.       (c++-indent-line))))
  371.  
  372. (defun c++-indent-line ()
  373.   "Indent current line as C++ code.
  374. Return the amount the indentation changed by."
  375.   (let ((indent (calculate-c++-indent nil))
  376.     beg shift-amt
  377.     (case-fold-search nil)
  378.     (pos (- (point-max) (point))))
  379.     (beginning-of-line)
  380.     (setq beg (point))
  381.     (cond ((eq indent nil)
  382.        (setq indent (current-indentation)))
  383.       ((eq indent t)
  384.        (setq indent (calculate-c-indent-within-comment)))
  385.       ((looking-at "[ \t]*#")
  386.        (setq indent 0))
  387.       (t
  388.        (skip-chars-forward " \t")
  389.        (if (listp indent) (setq indent (car indent)))
  390.        (cond ((looking-at "\\(default\\|public\\|private\\|protected\\):")
  391.           (setq indent (+ indent c-label-offset)))
  392.          ((or (looking-at "case\\b")
  393.               (and (looking-at "[A-Za-z]")
  394.                (save-excursion
  395.                  (forward-sexp 1)
  396.                  (looking-at ":[^:]"))))
  397.           (setq indent (max 1 (+ indent c-label-offset))))
  398.          ((and (looking-at "else\\b")
  399.                (not (looking-at "else\\s_")))
  400.           (setq indent (save-excursion
  401.                  (c-backward-to-start-of-if)
  402.                  (current-indentation))))
  403.          ((looking-at "friend\[ \t]")
  404.           (setq indent (+ indent c++-friend-offset)))
  405.          ((= (following-char) ?})
  406.           (setq indent (- indent c-indent-level)))
  407.          ((= (following-char) ?{)
  408.           (setq indent (+ indent c-brace-offset))))))
  409.     (skip-chars-forward " \t")
  410.     (setq shift-amt (- indent (current-column)))
  411.     (if (zerop shift-amt)
  412.     (if (> (- (point-max) pos) (point))
  413.         (goto-char (- (point-max) pos)))
  414.       (delete-region beg (point))
  415.       (indent-to indent)
  416.       ;; If initial point was within line's indentation,
  417.       ;; position after the indentation.  Else stay at same point in text.
  418.       (if (> (- (point-max) pos) (point))
  419.       (goto-char (- (point-max) pos))))
  420.     shift-amt))
  421.  
  422. (defun calculate-c++-indent (&optional parse-start)
  423.   "Return appropriate indentation for current line as C++ code.
  424. In usual case returns an integer: the column to indent to.
  425. Returns nil if line starts inside a string, t if in a comment."
  426.   (save-excursion
  427.     (beginning-of-line)
  428.     (let ((indent-point (point))
  429.       (case-fold-search nil)
  430.       state
  431.       containing-sexp)
  432.       (if parse-start
  433.       (goto-char parse-start)
  434.     (beginning-of-defun))
  435.       (while (< (point) indent-point)
  436.     (setq parse-start (point))
  437.     (setq state (parse-partial-sexp (point) indent-point 0))
  438.     (setq containing-sexp (car (cdr state))))
  439.       (cond ((or (nth 3 state) (nth 4 state))
  440.          ;; return nil or t if should not change this line
  441.          (nth 4 state))
  442.         ((null containing-sexp)
  443.          ;; Line is at top level.  May be data or function definition, or
  444.          ;; may be function argument declaration or member initialization.
  445.          ;; Indent like the previous top level line unless
  446.          ;; (1) the previous line ends in a closeparen without semicolon,
  447.          ;; in which case this line is the first argument declaration or
  448.          ;; member initialization, or
  449.          ;; (2) the previous line begins with a colon,
  450.          ;; in which case this is the second line of member inits.
  451.          ;; It is assumed that arg decls and member inits are not mixed.
  452.          (goto-char indent-point)
  453.          (skip-chars-forward " \t")
  454.          (if (= (following-char) ?{)
  455.          0   ; Unless it starts a function body
  456.            (c++-backward-to-noncomment (or parse-start (point-min)))
  457.            (if (= (preceding-char) ?\))
  458.            (progn        ; first arg decl or member init
  459.              (goto-char indent-point)
  460.              (skip-chars-forward " \t")
  461.              (if (= (following-char) ?:)
  462.              c++-member-init-indent
  463.                c-argdecl-indent))
  464.          (if (= (preceding-char) ?\;)
  465.              (backward-char 1))
  466.          (if (= (preceding-char) ?})
  467.              0
  468.            (beginning-of-line)    ; continued arg decls or member inits
  469.            (skip-chars-forward " \t")
  470.            (if (= (following-char) ?:)
  471.                (if c++-continued-member-init-offset
  472.                (+ (current-indentation)
  473.                   c++-continued-member-init-offset)
  474.              (progn
  475.                (forward-char 1)
  476.                (skip-chars-forward " \t")
  477.                (current-column)))
  478.              (current-indentation)))
  479.          )))
  480.         ((/= (char-after containing-sexp) ?{)
  481.          ;; line is expression, not statement:
  482.          ;; indent to just after the surrounding open -- unless
  483.          ;; empty arg list, in which case we do what
  484.          ;; c++-empty-arglist-indent says to do.
  485.          (if (and c++-empty-arglist-indent
  486.               (or (null (nth 2 state))    ;; indicates empty arg
  487.                         ;; list.
  488.               ;; Use a heuristic: if the first
  489.               ;; non-whitespace following left paren on
  490.               ;; same line is not a comment,
  491.               ;; is not an empty arglist.
  492.               (save-excursion
  493.                 (goto-char (1+ containing-sexp))
  494.                 (not
  495.                  (looking-at "\\( \\|\t\\)*[^/\n]")))))
  496.          (progn
  497.            (goto-char containing-sexp)
  498.            (beginning-of-line)
  499.            (skip-chars-forward " \t")
  500.            (goto-char (min (+ (point) c++-empty-arglist-indent)
  501.                    (1+ containing-sexp)))
  502.            (current-column))
  503.            ;; In C-mode, we would always indent to one after the
  504.            ;; left paren.  Here, though, we may have an
  505.            ;; empty-arglist, so we'll indent to the min of that
  506.            ;; and the beginning of the first argument.
  507.            (goto-char (1+ containing-sexp))
  508.            (current-column)))
  509.         (t
  510.          ;; Statement.  Find previous non-comment character.
  511.          (goto-char indent-point)
  512.          (c++-backward-to-noncomment containing-sexp)
  513.          (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?\{)))
  514.          ;; This line is continuation of preceding line's statement;
  515.          ;; indent  c-continued-statement-offset  more than the
  516.          ;; previous line of the statement.
  517.          (progn
  518.            (c-backward-to-start-of-continued-exp containing-sexp)
  519.            (+ c-continued-statement-offset (current-column)
  520.                       (if (save-excursion (goto-char indent-point)
  521.                       (skip-chars-forward " \t")
  522.                       (eq (following-char) ?{))
  523.               c-continued-brace-offset 0)))
  524.            ;; This line starts a new statement.
  525.            ;; Position following last unclosed open.
  526.            (goto-char containing-sexp)
  527.            ;; Is line first statement after an open-brace?
  528.            (or
  529.          ;; If no, find that first statement and indent like it.
  530.          (save-excursion
  531.            (forward-char 1)
  532.            (while (progn (skip-chars-forward " \t\n")
  533.                  (looking-at
  534.                   (concat
  535.                    "#\\|/\\*\\|//"
  536.                    "\\|case[ \t]"
  537.                    "\\|[a-zA-Z0-9_$]*:[^:]"
  538.                    "\\|friend[ \t]")))
  539.              ;; Skip over comments and labels following openbrace.
  540.              (cond ((= (following-char) ?\#)
  541.                 (forward-line 1))
  542.                ((looking-at "/\\*")
  543.                 (search-forward "*/" nil 'move))
  544.                ((looking-at "//\\|friend[ \t]")
  545.                 (forward-line 1))
  546.                (t
  547.                 (re-search-forward ":[^:]" nil 'move))))
  548.               ;; The first following code counts
  549.               ;; if it is before the line we want to indent.
  550.               (and (< (point) indent-point)
  551.                (current-column)))
  552.          ;; If no previous statement,
  553.          ;; indent it relative to line brace is on.
  554.          ;; For open brace in column zero, don't let statement
  555.          ;; start there too.  If c-indent-offset is zero,
  556.          ;; use c-brace-offset + c-continued-statement-offset instead.
  557.          ;; For open-braces not the first thing in a line,
  558.          ;; add in c-brace-imaginary-offset.
  559.          (+ (if (and (bolp) (zerop c-indent-level))
  560.             (+ c-brace-offset c-continued-statement-offset)
  561.               c-indent-level)
  562.             ;; Move back over whitespace before the openbrace.
  563.             ;; If openbrace is not first nonwhite thing on the line,
  564.             ;; add the c-brace-imaginary-offset.
  565.             (progn (skip-chars-backward " \t")
  566.                (if (bolp) 0 c-brace-imaginary-offset))
  567.             ;; If the openbrace is preceded by a parenthesized exp,
  568.             ;; move to the beginning of that;
  569.             ;; possibly a different line
  570.             (progn
  571.               (if (eq (preceding-char) ?\))
  572.               (forward-sexp -1))
  573.               ;; Get initial indentation of the line we are on.
  574.               (current-indentation))))))))))
  575.  
  576. (defun c++-backward-to-noncomment (lim)
  577.   (let (opoint stop)
  578.     (while (not stop)
  579.       (skip-chars-backward " \t\n\r\f" lim)
  580.       (setq opoint (point))
  581.       (cond ((and (>= (point) (+ 2 lim))
  582.           (save-excursion
  583.             (forward-char -2)
  584.             (looking-at "\\*/")))
  585.          (search-backward "/*" lim 'move))
  586.         ((and
  587.           (search-backward "//" (max (c++-point-bol) lim) 'move)
  588.           (not (c++-within-string-p (point) opoint))))
  589.       (t (beginning-of-line)
  590.          (skip-chars-forward " \t")
  591.          (if (looking-at "#")
  592.          (setq stop (<= (point) lim))
  593.            (setq stop t)
  594.            (goto-char opoint)))))))
  595.  
  596. (defun indent-c++-exp ()
  597.   "Indent each line of the C++ grouping following point."
  598.   (interactive)
  599.   (let ((indent-stack (list nil))
  600.     (contain-stack (list (point)))
  601.     (case-fold-search nil)
  602.     restart outer-loop-done inner-loop-done state ostate
  603.     this-indent last-sexp last-depth
  604.     at-else at-brace
  605.     (opoint (point))
  606.     (next-depth 0))
  607.     (save-excursion
  608.       (forward-sexp 1))
  609.     (save-excursion
  610.       (setq outer-loop-done nil)
  611.       (while (and (not (eobp)) (not outer-loop-done))
  612.     (setq last-depth next-depth)
  613.     ;; Compute how depth changes over this line
  614.     ;; plus enough other lines to get to one that
  615.     ;; does not end inside a comment or string.
  616.     ;; Meanwhile, do appropriate indentation on comment lines.
  617.     (setq inner-loop-done nil)
  618.     (while (and (not inner-loop-done)
  619.             (not (and (eobp) (setq outer-loop-done t))))
  620.       (setq ostate state)
  621.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  622.                       nil nil state))
  623.       (setq next-depth (car state))
  624.       (if (and (car (cdr (cdr state)))
  625.            (>= (car (cdr (cdr state))) 0))
  626.           (setq last-sexp (car (cdr (cdr state)))))
  627.       (if (or (nth 4 ostate))
  628.           (c++-indent-line))
  629.       (if (or (nth 3 state))
  630.           (forward-line 1)
  631.         (setq inner-loop-done t)))
  632.     (if (<= next-depth 0)
  633.         (setq outer-loop-done t))
  634.     (if outer-loop-done
  635.         nil
  636.       ;; If this line had ..))) (((.. in it, pop out of the levels
  637.       ;; that ended anywhere in this line, even if the final depth
  638.       ;; doesn't indicate that they ended.
  639.       (while (> last-depth (nth 6 state))
  640.         (setq indent-stack (cdr indent-stack)
  641.           contain-stack (cdr contain-stack)
  642.           last-depth (1- last-depth)))
  643.       (if (/= last-depth next-depth)
  644.           (setq last-sexp nil))
  645.       ;; Add levels for any parens that were started in this line.
  646.       (while (< last-depth next-depth)
  647.         (setq indent-stack (cons nil indent-stack)
  648.           contain-stack (cons nil contain-stack)
  649.           last-depth (1+ last-depth)))
  650.       (if (null (car contain-stack))
  651.           (setcar contain-stack (or (car (cdr state))
  652.                     (save-excursion (forward-sexp -1)
  653.                             (point)))))
  654.       (forward-line 1)
  655.       (skip-chars-forward " \t")
  656.       (if (eolp)
  657.           nil
  658.         (if (and (car indent-stack)
  659.              (>= (car indent-stack) 0))
  660.         ;; Line is on an existing nesting level.
  661.         ;; Lines inside parens are handled specially.
  662.         (if (/= (char-after (car contain-stack)) ?\{)
  663.             (setq this-indent (car indent-stack))
  664.           ;; Line is at statement level.
  665.           ;; Is it a new statement?  Is it an else?
  666.           ;; Find last non-comment character before this line
  667.           (save-excursion
  668.             (setq at-else (looking-at "else\\W"))
  669.             (setq at-brace (= (following-char) ?\{))
  670.             (c++-backward-to-noncomment opoint)
  671.             (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?: ?\{)))
  672.             ;; Preceding line did not end in comma or semi;
  673.             ;; indent this line  c-continued-statement-offset
  674.             ;; more than previous.
  675.             (progn
  676.               (c-backward-to-start-of-continued-exp
  677.                (car contain-stack))
  678.               (setq this-indent
  679.                 (+ c-continued-statement-offset
  680.                    (current-column)
  681.                    (if at-brace c-continued-brace-offset 0))))
  682.               ;; Preceding line ended in comma or semi;
  683.               ;; use the standard indent for this level.
  684.               (if at-else
  685.               (progn (c-backward-to-start-of-if opoint)
  686.                  (setq this-indent (current-indentation)))
  687.             (setq this-indent (car indent-stack))))))
  688.           ;; Just started a new nesting level.
  689.           ;; Compute the standard indent for this level.
  690.           (let ((val (calculate-c++-indent
  691.               (if (car indent-stack)
  692.                   (- (car indent-stack))))))
  693.         (setcar indent-stack
  694.             (setq this-indent val))))
  695.         ;; Adjust line indentation according to its contents
  696.         (if (looking-at "\\(public\\|private\\|protected\\):")
  697.         (setq this-indent (- this-indent c-indent-level)))
  698.         (if (or (looking-at "case[ \t]")
  699.             (and (looking-at "[A-Za-z]")
  700.              (save-excursion
  701.                (forward-sexp 1)
  702.                (looking-at ":[^:]"))))
  703.         (setq this-indent (max 1 (+ this-indent c-label-offset))))
  704.         (if (looking-at "friend[ \t]")
  705.         (setq this-indent (+ this-indent c++-friend-offset)))
  706.         (if (= (following-char) ?\})
  707.         (setq this-indent (- this-indent c-indent-level)))
  708.         (if (= (following-char) ?\{)
  709.         (setq this-indent (+ this-indent c-brace-offset)))
  710.         ;; Put chosen indentation into effect.
  711.         (or (= (current-column) this-indent)
  712.         (= (following-char) ?\#)
  713.         (progn
  714.           (delete-region (point) (progn (beginning-of-line) (point)))
  715.           (indent-to this-indent)))
  716.         ;; Indent any comment following the text.
  717.         (or (looking-at comment-start-skip)
  718.         (if (re-search-forward comment-start-skip
  719.                        (save-excursion (end-of-line)
  720.                                (point)) t)
  721.             (progn
  722.               (indent-for-comment)
  723.               (beginning-of-line))))))))))
  724.  
  725. (defun fill-c++-comment ()
  726.   "Fill a comment contained in consecutive lines containing point.
  727. The fill lines remain a comment."
  728.   (interactive)
  729.   (save-excursion
  730.     (let ((save fill-prefix))
  731.       (beginning-of-line 1)
  732.       (save-excursion
  733.     (re-search-forward comment-start-skip
  734.                (save-excursion (end-of-line) (point))
  735.                t)
  736.     (goto-char (match-end 0))
  737.     (set-fill-prefix))
  738.       (while (looking-at fill-prefix)
  739.     (previous-line 1))
  740.       (next-line 1)
  741.       (insert-string "\n")
  742.       (fill-paragraph nil)
  743.       (delete-char -1)
  744.       (setq fill-prefix save))))
  745.  
  746. (defun c++-point-bol ()
  747.   "Returns the value of the point at the beginning of the current line."
  748.   (save-excursion
  749.     (beginning-of-line)
  750.     (point)))
  751.  
  752. ;; (defun c++-insert-header ()
  753. ;;   "Insert header denoting C++ code at top of buffer."
  754. ;;   (interactive)
  755. ;;   (save-excursion
  756. ;;     (goto-char (point-min))
  757. ;;     (insert "// "
  758. ;;         "This may look like C code, but it is really "
  759. ;;         "-*- C++ -*-"
  760. ;;         "\n\n")))
  761.  
  762. (defun c++-within-string-p (point1 point2)
  763.   "Returns true if number of double quotes between two points is odd."
  764.   (let ((s (buffer-substring point1 point2)))
  765.     (not (zerop (% (c++-count-char-in-string ?\" s) 2)))))
  766.  
  767. (defun c++-count-char-in-string (c s)
  768.   (let ((count 0)
  769.     (pos 0))
  770.     (while (< pos (length s))
  771.       (setq count (+ count (if (\= (aref s pos) c) 1 0)))
  772.       (setq pos (1+ pos)))
  773.     count))
  774.  
  775. ;; rms: This page is creeping featurism, and not worth having.
  776.  
  777. ;;; Below are two regular expressions that attempt to match defuns
  778. ;;; "strongly" and "weakly."  The strong one almost reconstructs the
  779. ;;; grammar of C++; the weak one just figures anything id or curly on
  780. ;;; the left begins a defun.  The constant "c++-match-header-strongly"
  781. ;;; determines which to use; the default is the weak one.
  782.  
  783. ;; (defvar c++-match-header-strongly nil
  784. ;;   "*If nil, use `c++-defun-header-weak' to identify beginning of definitions.
  785. ;; If non-nil, use `c++-defun-header-strong'.")
  786. ;; 
  787. ;; (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)"
  788. ;;   "Regexp to match names of structure declaration blocks in C++.")
  789. ;; 
  790. ;; (defconst c++-defun-header-strong
  791. ;;   (let*
  792. ;;       (; valid identifiers
  793. ;;        ;; There's a real weirdness here -- if I switch the below
  794. ;;        (id "\\(\\w\\|_\\)+")
  795. ;;        ;; to be
  796. ;;        ;; (id "\\(_\\|\\w\\)+")
  797. ;;        ;; things no longer work right.  Try it and see!
  798. ;; 
  799. ;;        ; overloadable operators
  800. ;;        (op-sym1
  801. ;;      "[-+*/%^&|~!=<>]\\|[-+*/%^&|<>=!]=\\|<<=?\\|>>=?")
  802. ;;        (op-sym2
  803. ;;      "&&\\|||\\|\\+\\+\\|--\\|()\\|\\[\\]")     
  804. ;;        (op-sym (concat "\\(" op-sym1 "\\|" op-sym2 "\\)"))
  805. ;;        ; whitespace
  806. ;;        (middle "[^\\*]*\\(\\*+[^/\\*][^\\*]*\\)*")
  807. ;;        (c-comment (concat "/\\*" middle "\\*+/"))
  808. ;;        (wh (concat "\\(\\s \\|\n\\|//.*$\\|" c-comment "\\)"))
  809. ;;        (wh-opt (concat wh "*"))
  810. ;;        (wh-nec (concat wh "+"))
  811. ;;        (oper (concat "\\(" "operator" "\\("
  812. ;;              wh-opt op-sym "\\|" wh-nec id "\\)" "\\)"))
  813. ;;        (dcl-list "([^():]*)")
  814. ;;        (func-name (concat "\\(" oper "\\|" id "::" id "\\|" id "\\)"))
  815. ;;        (inits
  816. ;;      (concat "\\(:"
  817. ;;          "\\(" wh-opt id "(.*\\()" wh-opt "," "\\)\\)*"
  818. ;;          wh-opt id "(.*)" wh-opt "{"
  819. ;;          "\\|" wh-opt "{\\)"))
  820. ;;        (type-name (concat
  821. ;;             "\\(" c++-defun-header-strong-struct-equivs wh-nec "\\)?"
  822. ;;             id))
  823. ;;        (type (concat "\\(const" wh-nec "\\)?"
  824. ;;              "\\(" type-name "\\|" type-name wh-opt "\\*+" "\\|"
  825. ;;              type-name wh-opt "&" "\\)"))
  826. ;;        (modifier "\\(inline\\|virtual\\|overload\\|auto\\|static\\)")
  827. ;;        (modifiers (concat "\\(" modifier wh-nec "\\)*"))
  828. ;;        (func-header
  829. ;;      ;;     type               arg-dcl
  830. ;;      (concat modifiers type wh-nec func-name wh-opt dcl-list wh-opt inits))
  831. ;;        (inherit (concat "\\(:" wh-opt "\\(public\\|private\\)?"
  832. ;;             wh-nec id "\\)"))
  833. ;;        (cs-header (concat
  834. ;;             c++-defun-header-strong-struct-equivs
  835. ;;             wh-nec id wh-opt inherit "?" wh-opt "{")))
  836. ;;     (concat "^\\(" func-header "\\|" cs-header "\\)"))
  837. ;;   "Strongly-defined regexp to match beginning of structure or function def.")
  838. ;; 
  839. ;; 
  840. ;; ;; This part has to do with recognizing defuns.
  841. ;; 
  842. ;; ;; The weak convention we will use is that a defun begins any time
  843. ;; ;; there is a left curly brace, or some identifier on the left margin,
  844. ;; ;; followed by a left curly somewhere on the line.  (This will also
  845. ;; ;; incorrectly match some continued strings, but this is after all
  846. ;; ;; just a weak heuristic.)  Suggestions for improvement (short of the
  847. ;; ;; strong scheme shown above) are welcomed.
  848. ;; 
  849. ;; (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{"
  850. ;;   "Weakly-defined regexp to match beginning of structure or function def.")
  851. ;; 
  852. ;; (defun c++-beginning-of-defun (arg)
  853. ;;   (interactive "p")
  854. ;;   (let ((c++-defun-header (if c++-match-header-strongly
  855. ;;                   c++-defun-header-strong
  856. ;;                 c++-defun-header-weak)))
  857. ;;     (cond ((or (= arg 0) (and (> arg 0) (bobp))) nil)
  858. ;;       ((and (not (looking-at c++-defun-header))
  859. ;;         (let ((curr-pos (point))
  860. ;;               (open-pos (if (search-forward "{" nil 'move)
  861. ;;                     (point)))
  862. ;;               (beg-pos
  863. ;;             (if (re-search-backward c++-defun-header nil 'move)
  864. ;;                 (match-beginning 0))))
  865. ;;           (if (and open-pos beg-pos
  866. ;;                (< beg-pos curr-pos)
  867. ;;                (> open-pos curr-pos))
  868. ;;               (progn
  869. ;;             (goto-char beg-pos)
  870. ;;             (if (= arg 1) t nil));; Are we done?
  871. ;;             (goto-char curr-pos)
  872. ;;             nil))))
  873. ;;       (t
  874. ;;         (if (and (looking-at c++-defun-header) (not (bobp)))
  875. ;;         (forward-char (if (< arg 0) 1 -1)))
  876. ;;         (and (re-search-backward c++-defun-header nil 'move (or arg 1))
  877. ;;          (goto-char (match-beginning 0)))))))
  878. ;; 
  879. ;; 
  880. ;; (defun c++-end-of-defun (arg)
  881. ;;   (interactive "p")
  882. ;;   (let ((c++-defun-header (if c++-match-header-strongly
  883. ;;                   c++-defun-header-strong
  884. ;;                 c++-defun-header-weak)))
  885. ;;     (if (and (eobp) (> arg 0))
  886. ;;     nil
  887. ;;       (if (and (> arg 0) (looking-at c++-defun-header)) (forward-char 1))
  888. ;;       (let ((pos (point)))
  889. ;;     (c++-beginning-of-defun 
  890. ;;       (if (< arg 0)
  891. ;;           (- (- arg (if (eobp) 0 1)))
  892. ;;         arg))
  893. ;;     (if (and (< arg 0) (bobp))
  894. ;;         t
  895. ;;       (if (re-search-forward c++-defun-header nil 'move)
  896. ;;           (progn (forward-char -1)
  897. ;;              (forward-sexp)
  898. ;;              (beginning-of-line 2)))
  899. ;;       (if (and (= pos (point)) 
  900. ;;            (re-search-forward c++-defun-header nil 'move))
  901. ;;           (c++-end-of-defun 1))))
  902. ;;       t)))
  903. ;; 
  904. ;; (defun c++-indent-defun ()
  905. ;;   "Indents the current function definition, struct or class declaration."
  906. ;;   (interactive)
  907. ;;   (let ((restore (point)))
  908. ;;     (c++-end-of-defun 1)
  909. ;;     (beginning-of-line 1)
  910. ;;     (let ((end (point)))
  911. ;;       (c++-beginning-of-defun 1)
  912. ;;       (while (<= (point) end)
  913. ;;     (c++-indent-line)
  914. ;;     (next-line 1)
  915. ;;     (beginning-of-line 1)))
  916. ;;     (goto-char restore)))
  917.  
  918. ;;; cplus-md.el ends here
  919.