comparison lisp/modes/cc-mode.el @ 114:8619ce7e4c50 r20-1b9

Import from CVS: tag r20-1b9
author cvs
date Mon, 13 Aug 2007 09:21:54 +0200
parents fe104dbd9147
children 9f59509498e1
comparison
equal deleted inserted replaced
113:2ec2fe4a4c89 114:8619ce7e4c50
4 4
5 ;; Authors: 1992-1997 Barry A. Warsaw 5 ;; Authors: 1992-1997 Barry A. Warsaw
6 ;; 1987 Dave Detlefs and Stewart Clamen 6 ;; 1987 Dave Detlefs and Stewart Clamen
7 ;; 1985 Richard M. Stallman 7 ;; 1985 Richard M. Stallman
8 ;; Created: a long, long, time ago. adapted from the original c-mode.el 8 ;; Created: a long, long, time ago. adapted from the original c-mode.el
9 ;; Version: 4.379 9 ;; Version: 4.387
10 ;; Last Modified: 1997/03/10 00:59:10 10 ;; Last Modified: 1997/03/21 20:25:33
11 ;; Keywords: c languages oop 11 ;; Keywords: c languages oop
12 12
13 ;; NOTE: Read the commentary below for the right way to submit bug reports! 13 ;; NOTE: Read the commentary below for the right way to submit bug reports!
14 ;; NOTE: See the accompanying texinfo manual for details on using this mode! 14 ;; NOTE: See the accompanying texinfo manual for details on using this mode!
15 15
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 ;; Boston, MA 02111-1307, USA. 31 ;; Boston, MA 02111-1307, USA.
32 32
33 ;;; Commentary: 33 ;;; Commentary:
34 34
35 ;; This package provides modes in GNU Emacs for editing C, C++, 35 ;; This package provides GNU Emacs major modes for editing C, C++,
36 ;; Objective-C, and Java code. It is intended to be a replacement for 36 ;; Objective-C, and Java code. As of the latest Emacs and XEmacs
37 ;; c-mode.el (a.k.a. BOCM -- Boring Old C-Mode), c++-mode.el, 37 ;; releases, it is the default package for editing these languages.
38 ;; cplus-md.el, and cplus-md1.el, all of which are in some way 38 ;; This package is called "CC Mode", and should be spelled exactly
39 ;; ancestors of this file. A number of important improvements have 39 ;; this way. It supports K&R and ANSI C, ANSI C++, Objective-C, and
40 ;; been made, briefly: complete K&R C, ANSI C, `ARM' C++, Objective-C, 40 ;; Java, with a consistent indentation model across all modes. This
41 ;; and Java support with consistent indentation across all modes, more 41 ;; indentation model is intuitive and very flexible, so that almost
42 ;; intuitive indentation controlling variables, compatibility across 42 ;; any desired style of indentation can be supported. Installation,
43 ;; all known Emacsen, nice new features, and tons of bug fixes. This 43 ;; usage, and programming details are contained in an accompanying
44 ;; package is called "CC Mode" to distinguish it from its ancestors, 44 ;; texinfo manual.
45 ;; but there is no cc-mode command. Usage and programming details are 45
46 ;; contained in an accompanying texinfo manual. 46 ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
47 ;; cplus-md1.el..
47 48
48 ;; NOTE: This mode does not perform font-locking (a.k.a syntactic 49 ;; NOTE: This mode does not perform font-locking (a.k.a syntactic
49 ;; coloring, keyword highlighting, etc.) for any of the supported 50 ;; coloring, keyword highlighting, etc.) for any of the supported
50 ;; modes. Typically this is done by a package called font-lock.el 51 ;; modes. Typically this is done by a package called font-lock.el
51 ;; which I do *not* maintain. You should contact the Emacs 52 ;; which I do *not* maintain. You should contact the Emacs
65 ;; 19 (formerly Lucid Emacs), and GNU Emacs 18 all do things 66 ;; 19 (formerly Lucid Emacs), and GNU Emacs 18 all do things
66 ;; differently and there's no way to shut the byte-compiler up at the 67 ;; differently and there's no way to shut the byte-compiler up at the
67 ;; necessary granularity. Let me say this again: YOU CAN IGNORE ALL 68 ;; necessary granularity. Let me say this again: YOU CAN IGNORE ALL
68 ;; BYTE-COMPILER WARNINGS (you'd be surprised at how many people don't 69 ;; BYTE-COMPILER WARNINGS (you'd be surprised at how many people don't
69 ;; follow this advice :-). 70 ;; follow this advice :-).
70
71 ;; As of this writing (24-Feb-1997), CC Mode comes with the latest
72 ;; Emacs and XEmacs distributions (19.34 and 19.14 respectively), but
73 ;; is no longer pre-loaded by default. To use the latest version of
74 ;; CC Mode, you need only make sure that this copy of cc-mode.el is
75 ;; found first on your load-path.
76
77 ;; If your Emacs is pre-loaded with any version of cc-mode.el,
78 ;; c-mode.el, or c++-mode.el, you will need to consult the
79 ;; cc-mode.texinfo manual for details on upgrading your Emacs.
80
81 ;; There are four major mode entry points provided by this package,
82 ;; one for editing C++ code, one for editing C code (both K&R and
83 ;; ANSI), one for editing Objective-C code, and one for editing Java
84 ;; code. The commands are M-x c-mode, M-x c++-mode, M-x objc-mode,
85 ;; and M-x java-mode.
86 71
87 ;; Many, many thanks go out to all the folks on the beta test list. 72 ;; Many, many thanks go out to all the folks on the beta test list.
88 ;; Without their patience, testing, insight, code contributions, and 73 ;; Without their patience, testing, insight, code contributions, and
89 ;; encouragement CC Mode would be a far inferior package. 74 ;; encouragement CC Mode would be a far inferior package.
90 75
419 (defvar c-delete-function 'backward-delete-char-untabify 404 (defvar c-delete-function 'backward-delete-char-untabify
420 "*Function called by `c-electric-delete' when deleting characters.") 405 "*Function called by `c-electric-delete' when deleting characters.")
421 (defvar c-electric-pound-behavior nil 406 (defvar c-electric-pound-behavior nil
422 "*List of behaviors for electric pound insertion. 407 "*List of behaviors for electric pound insertion.
423 Only currently supported behavior is `alignleft'.") 408 Only currently supported behavior is `alignleft'.")
409
424 (defvar c-label-minimum-indentation 1 410 (defvar c-label-minimum-indentation 1
425 "*Minimum indentation for labels and case tags in `gnu' style.") 411 "*Minimum indentation for lines inside of top-level constructs.
412 This variable typically only affects code using the `gnu' style, which
413 mandates a minimum of one space in front of every line inside
414 top-level constructs. Specifically, the function
415 `c-gnu-impose-minimum' on your `c-special-indent-hook' is what
416 enforces this.")
426 417
427 (defvar c-progress-interval 5 418 (defvar c-progress-interval 5
428 "*Interval used to update progress status during long re-indentation. 419 "*Interval used to update progress status during long re-indentation.
429 If a number, percentage complete gets updated after each interval of 420 If a number, percentage complete gets updated after each interval of
430 that many seconds. Set to nil to inhibit updating. This is only 421 that many seconds. Set to nil to inhibit updating. This is only
1211 (defconst c-C-class-key "\\(struct\\|union\\)" 1202 (defconst c-C-class-key "\\(struct\\|union\\)"
1212 "Regexp describing a C struct declaration.") 1203 "Regexp describing a C struct declaration.")
1213 (defconst c-inher-key 1204 (defconst c-inher-key
1214 (concat "\\(\\<static\\>\\s +\\)?" 1205 (concat "\\(\\<static\\>\\s +\\)?"
1215 c-C++-class-key "[ \t]+" c-symbol-key 1206 c-C++-class-key "[ \t]+" c-symbol-key
1216 "\\([ \t]*:[ \t]*\\)?\\s *[^;]") 1207 "\\([ \t]*:[ \t]*\\)\\s *[^;]")
1217 "Regexp describing a class inheritance declaration.") 1208 "Regexp describing a class inheritance declaration.")
1218 (defconst c-switch-label-key 1209 (defconst c-switch-label-key
1219 "\\(\\(case[( \t]+\\S .*\\)\\|default[ \t]*\\):" 1210 "\\(\\(case[( \t]+\\S .*\\)\\|default[ \t]*\\):"
1220 "Regexp describing a switch's case or default label") 1211 "Regexp describing a switch's case or default label")
1221 (defconst c-C++-access-key 1212 (defconst c-C++-access-key
1237 "\\)\\b[^_]") 1228 "\\)\\b[^_]")
1238 "Regexp describing a conditional control for C++.") 1229 "Regexp describing a conditional control for C++.")
1239 (defconst c-C++-friend-key 1230 (defconst c-C++-friend-key
1240 "friend[ \t]+\\|template[ \t]*<.+>[ \t]*friend[ \t]+" 1231 "friend[ \t]+\\|template[ \t]*<.+>[ \t]*friend[ \t]+"
1241 "Regexp describing friend declarations in C++ classes.") 1232 "Regexp describing friend declarations in C++ classes.")
1242 (defconst c-C++-comment-start-regexp "/[/*]" 1233
1243 "Dual comment value for `c-comment-start-regexp'.") 1234 ;; comment starter definitions for various languages. the language
1244 (defconst c-C-comment-start-regexp "/\\*" 1235 ;; modes will set c-comment-start-regexp to this value.
1245 "Single comment style value for `c-comment-start-regexp'.") 1236 (defconst c-C++-comment-start-regexp "/[/*]")
1237 (defconst c-C-comment-start-regexp "/[*]")
1238 ;; We need to match all 3 Java style comments
1239 ;; 1) Traditional C block; 2) javadoc /** ...; 3) C++ style
1240 (defconst c-Java-comment-start-regexp "/\\(/\\|[*][*]?\\)")
1246 1241
1247 (defconst c-ObjC-method-key 1242 (defconst c-ObjC-method-key
1248 (concat 1243 (concat
1249 "^\\s *[+-]\\s *" 1244 "^\\s *[+-]\\s *"
1250 "\\(([^)]*)\\)?" ; return type 1245 "\\(([^)]*)\\)?" ; return type
1451 (c-common-init) 1446 (c-common-init)
1452 (setq comment-start "// " 1447 (setq comment-start "// "
1453 comment-end "" 1448 comment-end ""
1454 comment-multi-line nil 1449 comment-multi-line nil
1455 c-conditional-key c-Java-conditional-key 1450 c-conditional-key c-Java-conditional-key
1456 c-comment-start-regexp c-C++-comment-start-regexp 1451 c-comment-start-regexp c-Java-comment-start-regexp
1457 c-class-key c-Java-class-key 1452 c-class-key c-Java-class-key
1458 c-method-key c-Java-method-key 1453 c-method-key c-Java-method-key
1459 c-double-slash-is-comments-p t 1454 c-double-slash-is-comments-p t
1460 c-baseclass-key nil 1455 c-baseclass-key nil
1461 c-recognize-knr-p nil 1456 c-recognize-knr-p nil
1595 (c-setup-dual-comments c-mode-syntax-table) 1590 (c-setup-dual-comments c-mode-syntax-table)
1596 (setq-default c-C-comment-start-regexp c-C++-comment-start-regexp)) 1591 (setq-default c-C-comment-start-regexp c-C++-comment-start-regexp))
1597 1592
1598 1593
1599 ;; macros must be defined before first use 1594 ;; macros must be defined before first use
1595 (defmacro c-add-syntax (symbol &optional relpos)
1596 ;; a simple macro to append the syntax in symbol to the syntax list.
1597 ;; try to increase performance by using this macro
1598 (` (setq syntax (cons (cons (, symbol) (, relpos)) syntax))))
1599
1600 (defmacro c-point (position) 1600 (defmacro c-point (position)
1601 ;; Returns the value of point at certain commonly referenced POSITIONs. 1601 ;; Returns the value of point at certain commonly referenced POSITIONs.
1602 ;; POSITION can be one of the following symbols: 1602 ;; POSITION can be one of the following symbols:
1603 ;; 1603 ;;
1604 ;; bol -- beginning of line 1604 ;; bol -- beginning of line
2561 ;; How shall we decide where the end of the 2561 ;; How shall we decide where the end of the
2562 ;; fill-prefix is? 2562 ;; fill-prefix is?
2563 (progn 2563 (progn
2564 (beginning-of-line) 2564 (beginning-of-line)
2565 (skip-chars-forward " \t*" (c-point 'eol)) 2565 (skip-chars-forward " \t*" (c-point 'eol))
2566 ;; kludge alert, watch out for */, in
2567 ;; which case fill-prefix should *not*
2568 ;; be "*"!
2569 (if (and (= (following-char) ?/)
2570 (= (preceding-char) ?*))
2571 (forward-char -1))
2566 (point))) 2572 (point)))
2567 2573
2568 ;; If the comment is only one line followed 2574 ;; If the comment is only one line followed
2569 ;; by a blank line, calling move-to-column 2575 ;; by a blank line, calling move-to-column
2570 ;; above may have added some spaces and tabs 2576 ;; above may have added some spaces and tabs
2603 (narrow-to-region (save-excursion 2609 (narrow-to-region (save-excursion
2604 (if comment-start-place 2610 (if comment-start-place
2605 (goto-char comment-start-place) 2611 (goto-char comment-start-place)
2606 (search-backward "/*")) 2612 (search-backward "/*"))
2607 (if (and (not c-hanging-comment-starter-p) 2613 (if (and (not c-hanging-comment-starter-p)
2608 (looking-at "/\\*[ \t]*$")) 2614 (looking-at
2615 (concat c-comment-start-regexp
2616 "[ \t]*$")))
2609 (forward-line 1)) 2617 (forward-line 1))
2610 ;; Protect text before the comment 2618 ;; Protect text before the comment
2611 ;; start by excluding it. Add 2619 ;; start by excluding it. Add
2612 ;; spaces to bring back proper 2620 ;; spaces to bring back proper
2613 ;; indentation of that point. 2621 ;; indentation of that point.
3849 )) 3857 ))
3850 3858
3851 3859
3852 ;; defuns for calculating the syntactic state and indenting a single 3860 ;; defuns for calculating the syntactic state and indenting a single
3853 ;; line of C/C++/ObjC code 3861 ;; line of C/C++/ObjC code
3854 (defmacro c-add-syntax (symbol &optional relpos)
3855 ;; a simple macro to append the syntax in symbol to the syntax list.
3856 ;; try to increase performance by using this macro
3857 (` (setq syntax (cons (cons (, symbol) (, relpos)) syntax))))
3858
3859 (defun c-most-enclosing-brace (state) 3862 (defun c-most-enclosing-brace (state)
3860 ;; return the bufpos of the most enclosing brace that hasn't been 3863 ;; return the bufpos of the most enclosing brace that hasn't been
3861 ;; narrowed out by any enclosing class, or nil if none was found 3864 ;; narrowed out by any enclosing class, or nil if none was found
3862 (let (enclosingp) 3865 (let (enclosingp)
3863 (while (and state (not enclosingp)) 3866 (while (and state (not enclosingp))
4929 (if (not (looking-at "[*]+")) 4932 (if (not (looking-at "[*]+"))
4930 (progn 4933 (progn
4931 ;; we now have to figure out where this comment begins. 4934 ;; we now have to figure out where this comment begins.
4932 (goto-char here) 4935 (goto-char here)
4933 (back-to-indentation) 4936 (back-to-indentation)
4934 (if (re-search-forward "[*]+/" (c-point 'eol) t) 4937 (if (looking-at "[*]+/")
4935 (forward-comment -1) 4938 (progn (goto-char (match-end 0))
4939 (forward-comment -1))
4936 (goto-char (cdr langelem)) 4940 (goto-char (cdr langelem))
4937 (back-to-indentation)))) 4941 (back-to-indentation))))
4938 (- (current-column) cs-curcol)) 4942 (- (current-column) cs-curcol))
4939 (if (zerop stars) 4943 (if (zerop stars)
4940 (skip-chars-forward " \t")) 4944 (skip-chars-forward " \t"))
5197 (progn (skip-chars-backward " \t") (point))))))) 5201 (progn (skip-chars-backward " \t") (point)))))))
5198 5202
5199 5203
5200 ;; defuns for submitting bug reports 5204 ;; defuns for submitting bug reports
5201 5205
5202 (defconst c-version "4.379" 5206 (defconst c-version "4.387"
5203 "CC Mode version number.") 5207 "CC Mode version number.")
5204 (defconst c-mode-help-address 5208 (defconst c-mode-help-address
5205 "bug-gnu-emacs@prep.ai.mit.edu, cc-mode-help@python.org" 5209 "bug-gnu-emacs@prep.ai.mit.edu, cc-mode-help@python.org"
5206 "Address for CC Mode bug reports.") 5210 "Address for CC Mode bug reports.")
5207 5211
5218 (defun c-submit-bug-report () 5222 (defun c-submit-bug-report ()
5219 "Submit via mail a bug report on CC Mode." 5223 "Submit via mail a bug report on CC Mode."
5220 (interactive) 5224 (interactive)
5221 ;; load in reporter 5225 ;; load in reporter
5222 (let ((reporter-prompt-for-summary-p t) 5226 (let ((reporter-prompt-for-summary-p t)
5223 (reporter-dont-compact-list '(c-offsets-alist))) 5227 (reporter-dont-compact-list '(c-offsets-alist))
5228 (style c-indentation-style)
5229 (hook c-special-indent-hook)
5230 (c-features c-emacs-features))
5224 (and 5231 (and
5225 (if (y-or-n-p "Do you want to submit a report on CC Mode? ") 5232 (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
5226 t (message "") nil) 5233 t (message "") nil)
5227 (require 'reporter) 5234 (require 'reporter)
5228 (reporter-submit-bug-report 5235 (reporter-submit-bug-report
5245 'c-electric-pound-behavior 5252 'c-electric-pound-behavior
5246 'c-hanging-braces-alist 5253 'c-hanging-braces-alist
5247 'c-hanging-colons-alist 5254 'c-hanging-colons-alist
5248 'c-hanging-comment-starter-p 5255 'c-hanging-comment-starter-p
5249 'c-hanging-comment-ender-p 5256 'c-hanging-comment-ender-p
5257 'c-indent-comments-syntactically-p
5250 'c-tab-always-indent 5258 'c-tab-always-indent
5251 'c-recognize-knr-p 5259 'c-recognize-knr-p
5252 'c-label-minimum-indentation 5260 'c-label-minimum-indentation
5253 'defun-prompt-regexp 5261 'defun-prompt-regexp
5254 'tab-width 5262 'tab-width
5257 (delq 'defun-prompt-regexp vars) 5265 (delq 'defun-prompt-regexp vars)
5258 vars)) 5266 vars))
5259 (function 5267 (function
5260 (lambda () 5268 (lambda ()
5261 (insert 5269 (insert
5262 "Buffer Style: " c-indentation-style "\n\n" 5270 "Buffer Style: " style "\n\n"
5263 (if c-special-indent-hook 5271 (if hook
5264 (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" 5272 (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
5265 "c-special-indent-hook is set to '" 5273 "c-special-indent-hook is set to '"
5266 (format "%s" c-special-indent-hook) 5274 (format "%s" hook)
5267 ".\nPerhaps this is your problem?\n" 5275 ".\nPerhaps this is your problem?\n"
5268 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n") 5276 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
5269 "\n") 5277 "\n")
5270 (format "c-emacs-features: %s\n" c-emacs-features) 5278 (format "c-emacs-features: %s\n" c-features)
5271 ))) 5279 )))
5272 nil 5280 nil
5273 "Dear Barry," 5281 "Dear Barry,"
5274 )))) 5282 ))))
5275 5283