1 ;;; old-c-mode.el --- Old and fast mode for C code in XEmacs
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
5 ;; Maintainer: Ben Wing <ben@xemacs.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Ported from GNU Emacs 20.7.
29 ;; cc-mode is dog-slow on large files, even on my 700 MHz Pentium III.
30 ;; This version works with no problems on the XEmacs source code and is
33 ;;; Original commentary:
35 ;; [[A smart editing mode for C code. It knows a lot about C syntax and tries
36 ;; to position the cursor according to C layout conventions. You can
37 ;; change the details of the layout style with option variables. Load it
38 ;; and do M-x describe-mode for details.]]
42 "Old C code editing commands for Emacs."
43 ;; This group should have no parent.
44 ;; We don't want it to be loaded except on explicit request.
48 (defvar c-mode-abbrev-table nil
49 "Abbrev table in use in C mode.")
50 (define-abbrev-table 'c-mode-abbrev-table ())
52 (defvar c-mode-map (make-sparse-keymap)
53 "Keymap used in C mode.")
55 (define-key c-mode-map "{" 'electric-c-brace)
56 (define-key c-mode-map "}" 'electric-c-brace)
57 (define-key c-mode-map ";" 'electric-c-semi)
58 (define-key c-mode-map "#" 'electric-c-sharp-sign)
59 (define-key c-mode-map ":" 'electric-c-terminator)
60 (define-key c-mode-map "\e\C-h" 'mark-c-function)
61 (define-key c-mode-map "\e\C-q" 'indent-c-exp)
62 (define-key c-mode-map "\ea" 'c-beginning-of-statement)
63 (define-key c-mode-map "\ee" 'c-end-of-statement)
64 (define-key c-mode-map "\C-c\C-n" 'c-forward-conditional)
65 (define-key c-mode-map "\C-c\C-p" 'c-backward-conditional)
66 (define-key c-mode-map "\C-c\C-u" 'c-up-conditional)
67 (define-key c-mode-map "\t" 'c-indent-command)
68 (define-key c-mode-map "\C-c\C-q" 'c-indent-defun)
69 (define-key c-mode-map "\C-c\C-\\" 'c-backslash-region)
70 (define-key c-mode-map "\C-c\C-c" 'comment-region)
72 (autoload 'c-macro-expand "cmacexp"
73 "Display the result of expanding all C macros occurring in the region.
74 The expansion is entirely correct because it uses the C preprocessor."
77 ;;; begin stuff from cc-mode
79 (defun c-mode-menu (modestr)
81 '(["%_Comment Out Region" comment-region
82 (c-fn-region-is-active-p)]
84 (comment-region (region-beginning) (region-end) '(4))
85 (c-fn-region-is-active-p)]
86 ["%_Fill Comment Paragraph" c-fill-paragraph t]
88 ["%_Indent Expression" c-indent-exp
89 (memq (char-after) '(?\( ?\[ ?\{))]
90 ["Indent %_Line or Region" c-indent-line-or-region t]
91 ["U%_p Conditional" c-up-conditional t]
92 ["B%_ackward Conditional" c-backward-conditional t]
93 ["F%_orward Conditional" c-forward-conditional t]
94 ["Insert Matching %_Endif" c-insert-matching-endif t]
95 ["Bac%_kward Statement" c-beginning-of-statement t]
96 ["Fo%_rward Statement" c-end-of-statement t]
98 ["%_Macro Expand Region" c-macro-expand (c-fn-region-is-active-p)]
99 ["%_Backslashify" c-backslash-region
100 (c-fn-region-is-active-p)]
102 (c-backslash-region (region-beginning) (region-end) '(4))
103 (c-fn-region-is-active-p)]
105 (or (featurep 'menu-accelerator-support)
106 (setq m (mapcar #'(lambda (def)
108 (aset def 0 (replace-in-string (aref def 0)
114 (defun c-populate-syntax-table (table)
115 ;; Populate the syntax TABLE
116 ;; DO NOT TRY TO SET _ (UNDERSCORE) TO WORD CLASS!
117 (modify-syntax-entry ?_ "_" table)
118 (modify-syntax-entry ?\\ "\\" table)
119 (modify-syntax-entry ?+ "." table)
120 (modify-syntax-entry ?- "." table)
121 (modify-syntax-entry ?= "." table)
122 (modify-syntax-entry ?% "." table)
123 (modify-syntax-entry ?< "." table)
124 (modify-syntax-entry ?> "." table)
125 (modify-syntax-entry ?& "." table)
126 (modify-syntax-entry ?| "." table)
127 (modify-syntax-entry ?\' "\"" table)
128 ;; Set up block and line oriented comments. The new C standard
129 ;; mandates both comment styles even in C, so since all languages
130 ;; now require dual comments, we make this the default.
133 ((memq '8-bit c-emacs-features)
134 (modify-syntax-entry ?/ ". 1456" table)
135 (modify-syntax-entry ?* ". 23" table))
137 ((memq '1-bit c-emacs-features)
138 (modify-syntax-entry ?/ ". 124b" table)
139 (modify-syntax-entry ?* ". 23" table))
141 (t (error "CC Mode is incompatible with this version of Emacs"))
143 (modify-syntax-entry ?\n "> b" table)
144 ;; Give CR the same syntax as newline, for selective-display
145 (modify-syntax-entry ?\^m "> b" table))
147 (defvar c-mode-syntax-table nil
148 "Syntax table used in c-mode buffers.")
149 (if c-mode-syntax-table
151 (setq c-mode-syntax-table (make-syntax-table))
152 (c-populate-syntax-table c-mode-syntax-table))
154 (defvar c-current-comment-prefix nil
155 "The current comment prefix regexp.
156 Set from `c-comment-prefix-regexp' at mode initialization.")
157 (make-variable-buffer-local 'c-current-comment-prefix)
159 ;(defcustom-c-stylevar c-comment-prefix-regexp
160 (defcustom c-comment-prefix-regexp
161 '((pike-mode . "//+!?\\|\\**")
162 (other . "//+\\|\\**"))
163 "*Regexp to match the line prefix inside comments.
164 This regexp is used to recognize the fill prefix inside comments for
165 correct paragraph filling and other things.
167 If this variable is a string, it will be used in all CC Mode major
168 modes. It can also be an association list, to associate specific
169 regexps to specific major modes. The symbol for the major mode is
170 looked up in the association list, and its value is used as the line
171 prefix regexp. If it's not found, then the symbol `other' is looked
172 up and its value is used instead.
174 The regexp should match the prefix used in both C++ style line
175 comments and C style block comments, but it does not need to match a
176 block comment starter. In other words, it should at least match
177 \"//\" for line comments and the string in `c-block-comment-prefix',
178 which is sometimes inserted by CC Mode inside block comments. It
179 should not match any surrounding whitespace.
181 Note that CC Mode modifies other variables from this one at mode
182 initialization, so you will need to do \\[c-mode] (or whatever mode
183 you're currently using) if you change it in a CC Mode buffer."
185 (regexp :tag "Regexp for all modes")
187 :tag "Mode-specific regexps"
189 :inline t :format "%v"
191 (const :format "C " c-mode) (regexp :format "%v"))
193 (const :format "C++ " c++-mode) (regexp :format "%v"))
195 (const :format "ObjC " objc-mode) (regexp :format "%v"))
197 (const :format "Java " java-mode) (regexp :format "%v"))
199 (const :format "IDL " idl-mode) (regexp :format "%v"))
201 (const :format "Pike " pike-mode) (regexp :format "%v")))
203 (const :format "Other " other) (regexp :format "%v"))))
206 (defvar c-append-paragraph-start "$")
207 (make-variable-buffer-local 'c-append-paragraph-start)
208 (defconst c-Java-javadoc-paragraph-start
209 "\\(@[a-zA-Z]+\\>\\|$\\)")
210 (defconst c-Pike-pikedoc-paragraph-start
211 "\\(@[a-zA-Z]+\\>\\([^{]\\|$\\)\\|$\\)")
213 ;; Regexp to append to paragraph-separate.
214 (defvar c-append-paragraph-separate "$")
215 (make-variable-buffer-local 'c-append-paragraph-separate)
216 (defconst c-Pike-pikedoc-paragraph-separate c-Pike-pikedoc-paragraph-start)
218 ;;; end stuff from cc-mode
220 (defcustom c-indent-level 2
221 "*Indentation of C statements with respect to containing block."
224 (defcustom c-brace-imaginary-offset 0
225 "*Imagined indentation of a C open brace that actually follows a statement."
228 (defcustom c-brace-offset 0
229 "*Extra indentation for braces, compared with other text in same context."
232 (defcustom c-argdecl-indent 5
233 "*Indentation level of declarations of C function arguments."
236 (defcustom c-label-offset -2
237 "*Offset of C label lines and case statements relative to usual indentation."
240 (defcustom c-continued-statement-offset 2
241 "*Extra indent for lines not starting new statements."
244 (defcustom c-continued-brace-offset 0
245 "*Extra indent for substatements that start with open-braces.
246 This is in addition to `c-continued-statement-offset'."
249 (defconst c-style-alist
252 (c-argdecl-indent . 5)
254 (c-continued-brace-offset . 0)
255 (c-label-offset . -2)
256 (c-continued-statement-offset . 2))
259 (c-argdecl-indent . 0)
261 (c-continued-brace-offset . -5)
262 (c-label-offset . -5)
263 (c-continued-statement-offset . 5))
266 (c-argdecl-indent . 4)
268 (c-continued-brace-offset . -4)
269 (c-label-offset . -4)
270 (c-continued-statement-offset . 4))
273 (c-argdecl-indent . 0)
275 (c-continued-brace-offset . -4)
276 (c-label-offset . -4)
277 (c-continued-statement-offset . 4)
278 (c-auto-newline . t))
281 (c-argdecl-indent . 4)
283 (c-continued-brace-offset . 0)
284 (c-label-offset . -4)
285 (c-continued-statement-offset . 4))))
287 (defcustom c-auto-newline nil
288 "*Non-nil means automatically newline before and after braces,
289 and after colons and semicolons, inserted in C code.
290 If you do not want a leading newline before braces then use:
291 (define-key c-mode-map \"{\" 'electric-c-semi)"
295 (defcustom c-tab-always-indent t
296 "*Non-nil means TAB in C mode should always reindent the current line,
297 regardless of where in the line point is when the TAB command is used."
301 ;;; Regular expression used internally to recognize labels in switch
303 (defconst c-switch-label-regexp "case[ \t'/(]\\|default[ \t]*:")
305 ;; This is actually the expression for C++ mode, but it's used for C too.
306 (defvar c-imenu-generic-expression
311 "^" ; beginning of line is required
312 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
313 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
314 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
316 "\\(" ; last type spec including */&
318 "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace
319 "\\)?" ; if there is a last type spec
320 "\\(" ; name; take that into the imenu entry
321 "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor...
322 ; (may not contain * because then
323 ; "a::operator char*" would become "char*"!)
325 "\\([a-zA-Z0-9_:~]*::\\)?operator"
326 "[^a-zA-Z1-9_][^(]*" ; ...or operator
328 "[ \t]*([^)]*)[ \t\n]*[^ ;]" ; require something other than a ; after
329 ; the (...) to avoid prototypes. Can't
330 ; catch cases with () inside the parentheses
331 ; surrounding the parameters
332 ; (like "int foo(int a=bar()) {...}"
337 "^" ; beginning of line is required
338 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
340 "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
343 ;; Example of generic expression for finding prototypes, structs, unions, enums.
344 ;; Uncomment if you want to find these too. It will be a bit slower gathering
349 ; "^" ; beginning of line is required
350 ; "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
351 ; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
352 ; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
354 ; "\\(" ; last type spec including */&
356 ; "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace
357 ; "\\)?" ; if there is a last type spec
358 ; "\\(" ; name; take that into the imenu entry
359 ; "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor...
360 ; ; (may not contain * because then
361 ; ; "a::operator char*" would become "char*"!)
363 ; "\\([a-zA-Z0-9_:~]*::\\)?operator"
364 ; "[^a-zA-Z1-9_][^(]*" ; ...or operator
366 ; "[ \t]*([^)]*)[ \t\n]*;" ; require ';' after
368 ; ; catch cases with () inside the parentheses
369 ; ; surrounding the parameters
370 ; ; (like "int foo(int a=bar());"
374 ; "^" ; beginning of line is required
375 ; "\\(static[ \t]+\\)?" ; there may be static or const.
376 ; "\\(const[ \t]+\\)?"
378 ; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
383 ; "^" ; beginning of line is required
384 ; "\\(static[ \t]+\\)?" ; there may be static or const.
385 ; "\\(const[ \t]+\\)?"
387 ; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
392 ; "^" ; beginning of line is required
393 ; "\\(static[ \t]+\\)?" ; there may be static or const.
394 ; "\\(const[ \t]+\\)?"
396 ; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
400 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
403 "Major mode for editing C code.
404 Expression and list commands understand all C brackets.
405 Tab indents for C code.
406 Comments are delimited with /* ... */.
407 Paragraphs are separated by blank lines only.
408 Delete converts tabs to spaces as it moves back.
410 Variables controlling indentation style:
412 Non-nil means TAB in C mode should always reindent the current line,
413 regardless of where in the line point is when the TAB command is used.
415 Non-nil means automatically newline before and after braces,
416 and after colons and semicolons, inserted in C code.
418 Indentation of C statements within surrounding block.
419 The surrounding block's indentation is the indentation
420 of the line on which the open-brace appears.
421 c-continued-statement-offset
422 Extra indentation given to a substatement, such as the
423 then-clause of an if or body of a while.
424 c-continued-brace-offset
425 Extra indentation given to a brace that starts a substatement.
426 This is in addition to c-continued-statement-offset.
428 Extra indentation for line if it starts with an open brace.
429 c-brace-imaginary-offset
430 An open brace following other text is treated as if it were
431 this far to the right of the start of its line.
433 Indentation level of declarations of C function arguments.
435 Extra indentation for line that is a label, or case or default.
437 Settings for K&R and BSD indentation styles are
439 c-continued-statement-offset 5 8
444 Turning on C mode calls the value of the variable c-mode-hook with no args,
445 if that value is non-nil."
447 (kill-all-local-variables)
448 (use-local-map c-mode-map)
449 (setq major-mode 'c-mode)
452 ;;; begin stuff from cc-mode
454 (make-local-variable 'require-final-newline)
455 (make-local-variable 'parse-sexp-ignore-comments)
456 (make-local-variable 'indent-line-function)
457 (make-local-variable 'indent-region-function)
458 (make-local-variable 'outline-regexp)
459 (make-local-variable 'outline-level)
460 (make-local-variable 'normal-auto-fill-function)
461 (make-local-variable 'comment-start)
462 (make-local-variable 'comment-end)
463 (make-local-variable 'comment-column)
464 (make-local-variable 'comment-start-skip)
465 (make-local-variable 'comment-multi-line)
466 (make-local-variable 'paragraph-start)
467 (make-local-variable 'paragraph-separate)
468 (make-local-variable 'paragraph-ignore-fill-prefix)
469 (make-local-variable 'adaptive-fill-mode)
470 (make-local-variable 'adaptive-fill-regexp)
471 (make-local-variable 'imenu-generic-expression) ;set in the mode functions
473 ; (and (boundp 'comment-line-break-function)
475 ; (make-local-variable 'comment-line-break-function)
476 ; (setq comment-line-break-function
477 ; 'c-indent-new-comment-line)))
478 ;; now set their values
479 (setq require-final-newline t
480 parse-sexp-ignore-comments t
481 indent-line-function 'c-indent-line
482 indent-region-function 'c-indent-region
483 outline-regexp "[^#\n\^M]"
484 outline-level 'c-outline-level
485 normal-auto-fill-function 'c-do-auto-fill
487 comment-start-skip "/\\*+ *\\|//+ *"
488 comment-multi-line t)
489 ; ;; now set the mode style based on c-default-style
490 ; (let ((style (if (stringp c-default-style)
492 ; (or (cdr (assq major-mode c-default-style))
493 ; (cdr (assq 'other c-default-style))
495 ; ;; Override style variables if `c-old-style-variable-behavior' is
496 ; ;; set. Also override if we are using global style variables,
497 ; ;; have already initialized a style once, and are switching to a
498 ; ;; different style. (It's doubtful whether this is desirable, but
499 ; ;; the whole situation with nonlocal style variables is a bit
500 ; ;; awkward. It's at least the most compatible way with the old
501 ; ;; style init procedure.)
502 ; (c-set-style style (not (or c-old-style-variable-behavior
503 ; (and (not c-style-variables-are-local-p)
504 ; c-indentation-style
505 ; (not (string-equal c-indentation-style
507 ;; Fix things up for paragraph recognition and filling inside
508 ;; comments by using c-current-comment-prefix in the relevant
509 ;; places. We use adaptive filling for this to make it possible to
510 ;; use filladapt or some other fancy package.
511 (setq c-current-comment-prefix
512 (if (listp c-comment-prefix-regexp)
513 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp)
514 (assoc 'other c-comment-prefix-regexp)))
515 c-comment-prefix-regexp))
516 (let ((comment-line-prefix
517 (concat "[ \t]*\\(" c-current-comment-prefix "\\)[ \t]*")))
518 (setq paragraph-start (concat comment-line-prefix
519 c-append-paragraph-start
522 paragraph-separate (concat comment-line-prefix
523 c-append-paragraph-separate
526 paragraph-ignore-fill-prefix t
529 (concat comment-line-prefix
530 (if adaptive-fill-regexp
531 (concat "\\(" adaptive-fill-regexp "\\)")
533 (when (boundp 'adaptive-fill-first-line-regexp)
534 ;; XEmacs (20.x) adaptive fill mode doesn't have this.
535 (make-local-variable 'adaptive-fill-first-line-regexp)
536 (setq adaptive-fill-first-line-regexp
537 (concat "\\`" comment-line-prefix
538 ;; Maybe we should incorporate the old value here,
539 ;; but then we have to do all sorts of kludges to
540 ;; deal with the \` and \' it probably contains.
542 ; ;; we have to do something special for c-offsets-alist so that the
543 ; ;; buffer local value has its own alist structure.
544 ; (setq c-offsets-alist (copy-alist c-offsets-alist))
545 ;; setup the comment indent variable in a Emacs version portable way
546 ;; ignore any byte compiler warnings you might get here
547 (make-local-variable 'comment-indent-function)
548 (setq comment-indent-function 'c-comment-indent)
549 ;; add menus to menubar
550 (easy-menu-add (c-mode-menu mode-name))
551 ; ;; put auto-hungry designators onto minor-mode-alist, but only once
552 ; (or (assq 'c-auto-hungry-string minor-mode-alist)
553 ; (setq minor-mode-alist
554 ; (cons '(c-auto-hungry-string c-auto-hungry-string)
555 ; minor-mode-alist)))
557 ;;; end stuff from cc-mode
559 (setq local-abbrev-table c-mode-abbrev-table)
560 (set-syntax-table c-mode-syntax-table)
561 (make-local-variable 'fill-paragraph-function)
562 (setq fill-paragraph-function 'c-fill-paragraph)
563 (setq comment-start "/* ")
564 (setq comment-end " */")
565 (make-local-variable 'imenu-generic-expression)
566 (setq imenu-generic-expression c-imenu-generic-expression)
567 (run-hooks 'c-mode-hook))
569 (defun c-outline-level ()
570 ;; This so that `current-column' DTRT in otherwise-hidden text.
571 (let (buffer-invisibility-spec)
573 (skip-chars-forward "\t ")
576 ;; This is used by indent-for-comment
577 ;; to decide how much to indent a comment in C code
578 ;; based on its context.
579 (defun c-comment-indent ()
580 (if (looking-at "^/\\*")
581 0 ;Existing comment at bol stays there.
582 (let ((opoint (point)))
585 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
586 ;; A comment following a solitary close-brace
587 ;; should have only one space.
589 (1+ (current-column)))
590 ((or (looking-at "^#[ \t]*endif[ \t]*")
591 (looking-at "^#[ \t]*else[ \t]*"))
592 7) ;2 spaces after #endif
595 (skip-chars-backward " \t")
596 (and (= comment-column 0) (bolp)))
597 ;; If comment-column is 0, and nothing but space
598 ;; before the comment, align it at 0 rather than 1.
601 (max (1+ (current-column)) ;Else indent at comment column
602 comment-column))))))) ; except leave at least one space.
604 (defun c-fill-paragraph (&optional arg)
605 "Like \\[fill-paragraph] but handle C comments.
606 If any of the current line is a comment or within a comment,
607 fill the comment or the paragraph of it that point is in,
608 preserving the comment indentation or line-starting decorations."
610 (let* (comment-start-place
612 ;; Check for obvious entry to comment.
615 (skip-chars-forward " \t\n")
616 (and (looking-at comment-start-skip)
617 (setq comment-start-place (point))))))
618 (if (and (eq major-mode 'c++-mode)
621 (looking-at ".*//")))
624 ;; Lines containing just a comment start or just an end
625 ;; should not be filled into paragraphs they are next to.
628 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
632 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")))
635 ;; Move up to first line of this comment.
636 (while (and (not (bobp)) (looking-at "[ \t]*//"))
638 (if (not (looking-at ".*//"))
640 ;; Find the comment start in this line.
641 (re-search-forward "[ \t]*//[ \t]*")
642 ;; Set the fill-prefix to be what all lines except the first
643 ;; should start with.
644 (let ((endcol (current-column)))
645 (skip-chars-backward " \t")
647 (concat (make-string (- (current-column) 2) ?\ )
649 (make-string (- endcol (current-column)) ?\ ))))
651 ;; Narrow down to just the lines of this comment.
652 (narrow-to-region (point)
655 (while (looking-at "[ \t]*//")
660 (delete-region (point-min)
661 (+ (point-min) (length fill-prefix))))))
663 ;; t if we enter a comment between start of function and this line.
664 (eq (calculate-c-indent) t)
665 ;; t if this line contains a comment starter.
670 (re-search-forward comment-start-skip
671 (save-excursion (end-of-line)
674 (setq comment-start-place (point))))))
675 ;; Inside a comment: fill one comment paragraph.
677 ;; The prefix for each line of this paragraph
678 ;; is the appropriate part of the start of this line,
679 ;; up to the column at which text should be indented.
682 (if (looking-at "[ \t]*/\\*.*\\*/")
683 (progn (re-search-forward comment-start-skip)
684 (make-string (current-column) ?\ ))
685 (if first-line (forward-line 1))
687 (let ((line-width (progn (end-of-line) (current-column))))
693 ;; How shall we decide where the end of the
695 ;; calculate-c-indent-within-comment bases its value
696 ;; on the indentation of previous lines; if they're
697 ;; indented specially, it could return a column
698 ;; that's well into the current line's text. So
699 ;; we'll take at most that many space, tab, or *
700 ;; characters, and use that as our fill prefix.
701 (let ((max-prefix-end
704 (calculate-c-indent-within-comment t)
708 (skip-chars-forward " \t*" max-prefix-end)
709 ;; Don't include part of comment terminator
710 ;; in the fill-prefix.
711 (and (eq (following-char) ?/)
712 (eq (preceding-char) ?*)
716 ;; If the comment is only one line followed by a blank
717 ;; line, calling move-to-column above may have added
718 ;; some spaces and tabs to the end of the line; the
719 ;; fill-paragraph function will then delete it and the
720 ;; newline following it, so we'll lose a blank line
721 ;; when we shouldn't. So delete anything
722 ;; move-to-column added to the end of the line. We
723 ;; record the line width instead of the position of the
724 ;; old line end because move-to-column might break a
725 ;; tab into spaces, and the new characters introduced
726 ;; there shouldn't be deleted.
728 ;; If you can see a better way to do this, please make
729 ;; the change. This seems very messy to me.
730 (delete-region (progn (move-to-column line-width)
732 (progn (end-of-line) (point))))))))
735 ;; Lines containing just a comment start or just an end
736 ;; should not be filled into paragraphs they are next to.
739 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
743 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
746 ;; Don't fill the comment together with the code following it.
747 ;; So temporarily exclude everything before the comment start,
748 ;; and everything after the line where the comment ends.
749 ;; If comment-start-place is non-nil, the comment starter is there.
750 ;; Otherwise, point is inside the comment.
751 (narrow-to-region (save-excursion
752 (if comment-start-place
753 (goto-char comment-start-place)
754 (search-backward "/*"))
755 ;; Protect text before the comment start
756 ;; by excluding it. Add spaces to bring back
757 ;; proper indentation of that point.
758 (let ((column (current-column)))
760 (setq chars-to-delete column)
761 (insert-char ?\ column))))
763 (if comment-start-place
764 (goto-char (+ comment-start-place 2)))
765 (search-forward "*/" nil 'move)
769 (goto-char (point-max))
771 ;; And comment terminator was on a separate line before,
773 ;; This also avoids another problem:
774 ;; if the fill-prefix ends in a *, it could eat up
775 ;; the * of the comment terminator.
776 (if (looking-at "[ \t]*\\*/")
777 (narrow-to-region (point-min) (point))))
780 ;; Delete the chars we inserted to avoid clobbering
781 ;; the stuff before the comment start.
782 (goto-char (point-min))
783 (if (> chars-to-delete 0)
784 (delete-region (point) (+ (point) chars-to-delete)))
785 ;; Find the comment ender (should be on last line of buffer,
786 ;; given the narrowing) and don't leave it on its own line.
787 ;; Do this with a fill command, so as to preserve sentence
789 (goto-char (point-max))
791 (search-forward "*/" nil 'move)
793 (if (looking-at "[ \t]*\\*/")
794 (let ((fill-column (+ fill-column 9999)))
796 (fill-region-as-paragraph (point) (point-max)))))))
797 ;; Outside of comments: do ordinary filling.
798 (fill-paragraph arg)))
801 (defun electric-c-brace (arg)
802 "Insert character and correct line's indentation."
808 (skip-chars-backward " \t")
810 (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
812 (insert last-command-char)
817 ;; (newline) may have done auto-fill
818 (setq insertpos (- (point) 2))
821 (if insertpos (goto-char (1+ insertpos)))
825 (goto-char insertpos)
826 (self-insert-command (prefix-numeric-value arg)))
827 (self-insert-command (prefix-numeric-value arg)))))
829 (defun electric-c-sharp-sign (arg)
830 "Insert character and correct line's indentation."
833 (skip-chars-backward " \t")
835 (let ((c-auto-newline nil))
836 (electric-c-terminator arg))
837 (self-insert-command (prefix-numeric-value arg))))
839 (defun electric-c-semi (arg)
840 "Insert character and correct line's indentation."
843 (electric-c-terminator arg)
844 (self-insert-command (prefix-numeric-value arg))))
846 (defun electric-c-terminator (arg)
847 "Insert character and correct line's indentation."
849 (let (insertpos (end (point)))
850 (if (and (not arg) (eolp)
853 (skip-chars-forward " \t")
854 (or (= (following-char) ?#)
855 ;; Colon is special only after a label, or case ....
856 ;; So quickly rule out most other uses of colon
857 ;; and do no indentation for them.
858 (and (eq last-command-char ?:)
859 (not (looking-at c-switch-label-regexp))
861 (skip-chars-forward "a-zA-Z0-9_$")
862 (skip-chars-forward " \t")
866 (let ((pps (parse-partial-sexp (point) end)))
867 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
869 (insert last-command-char)
872 (not (c-inside-parens-p))
875 ;; (newline) may have done auto-fill
876 (setq insertpos (- (point) 2))
879 (if insertpos (goto-char (1+ insertpos)))
883 (goto-char insertpos)
884 (self-insert-command (prefix-numeric-value arg)))
885 (self-insert-command (prefix-numeric-value arg)))))
887 (defun c-inside-parens-p ()
891 (narrow-to-region (point)
892 (progn (beginning-of-defun) (point)))
893 (goto-char (point-max))
894 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
897 (defun c-indent-command (&optional whole-exp)
898 "Indent current line as C code, or in some cases insert a tab character.
899 If `c-tab-always-indent' is non-nil (the default), always indent current line.
900 Otherwise, indent the current line only if point is at the left margin or
901 in the line's indentation; otherwise insert a tab.
903 A numeric argument, regardless of its value, means indent rigidly all the
904 lines of the expression starting after point so that this line becomes
905 properly indented. The relative indentation among the lines of the
906 expression are preserved."
909 ;; If arg, always indent this line as C
910 ;; and shift remaining lines of expression the same amount.
911 (let ((shift-amt (c-indent-line))
914 (if c-tab-always-indent
916 ;; Find beginning of following line.
918 (forward-line 1) (setq beg (point)))
919 ;; Find first beginning-of-sexp for sexp extending past this line.
920 (while (< (point) beg)
923 (skip-chars-forward " \t\n")))
925 (indent-code-rigidly beg end shift-amt "#")))
926 (if (and (not c-tab-always-indent)
928 (skip-chars-backward " \t")
933 (defun c-indent-line ()
934 "Indent current line as C code.
935 Return the amount the indentation changed by."
936 (let ((indent (calculate-c-indent nil))
938 (case-fold-search nil)
939 (pos (- (point-max) (point))))
942 (cond ((eq indent nil)
943 (setq indent (current-indentation)))
945 (setq indent (calculate-c-indent-within-comment)))
946 ((looking-at "[ \t]*#")
949 (skip-chars-forward " \t")
950 (if (listp indent) (setq indent (car indent)))
951 (cond ((or (looking-at c-switch-label-regexp)
952 (and (looking-at "[A-Za-z]")
956 (setq indent (max 1 (+ indent c-label-offset))))
957 ((and (looking-at "else\\b")
958 (not (looking-at "else\\s_")))
959 (setq indent (save-excursion
960 (c-backward-to-start-of-if)
961 (current-indentation))))
962 ((and (looking-at "}[ \t]*else\\b")
963 (not (looking-at "}[ \t]*else\\s_")))
964 (setq indent (save-excursion
967 (c-backward-to-start-of-if)
968 (current-indentation))))
969 ((and (looking-at "while\\b")
970 (not (looking-at "while\\s_"))
972 (c-backward-to-start-of-do)))
973 ;; This is a `while' that ends a do-while.
974 (setq indent (save-excursion
975 (c-backward-to-start-of-do)
976 (current-indentation))))
977 ((= (following-char) ?})
978 (setq indent (- indent c-indent-level)))
979 ((= (following-char) ?{)
980 (setq indent (+ indent c-brace-offset))))))
981 (skip-chars-forward " \t")
982 (setq shift-amt (- indent (current-column)))
983 (if (zerop shift-amt)
984 (if (> (- (point-max) pos) (point))
985 (goto-char (- (point-max) pos)))
986 (delete-region beg (point))
988 ;; If initial point was within line's indentation,
989 ;; position after the indentation. Else stay at same point in text.
990 (if (> (- (point-max) pos) (point))
991 (goto-char (- (point-max) pos))))
994 (defun calculate-c-indent (&optional parse-start)
995 "Return appropriate indentation for current line as C code.
996 In usual case returns an integer: the column to indent to.
997 Returns nil if line starts inside a string, t if in a comment."
1000 (let ((indent-point (point))
1001 (case-fold-search nil)
1005 (goto-char parse-start)
1006 (beginning-of-defun))
1007 (while (< (point) indent-point)
1008 (setq parse-start (point))
1009 (setq state (parse-partial-sexp (point) indent-point 0))
1010 (setq containing-sexp (car (cdr state))))
1011 (cond ((or (nth 3 state) (nth 4 state))
1012 ;; return nil or t if should not change this line
1014 ((null containing-sexp)
1015 ;; Line is at top level. May be data or function definition,
1016 ;; or may be function argument declaration.
1017 ;; Indent like the previous top level line
1018 ;; unless that ends in a closeparen without semicolon,
1019 ;; in which case this line is the first argument decl.
1020 (goto-char indent-point)
1021 (skip-chars-forward " \t")
1022 (if (= (following-char) ?{)
1023 0 ; Unless it starts a function body
1024 (c-backward-to-noncomment (or parse-start (point-min)))
1025 ;; Look at previous line that's at column 0
1026 ;; to determine whether we are in top-level decls
1027 ;; or function's arg decls. Set basic-indent accordingly.
1030 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
1032 ;; Recognize the DEFUN macro in Emacs.
1034 ;; Move down to the (putative) argnames line.
1035 (while (and (not (eobp))
1036 (not (looking-at " *[({}#/]")))
1038 ;; Go back to the DEFUN, if it is one.
1043 (looking-at "DEFUN\\b"))
1045 (if (and (looking-at "\\sw\\|\\s_")
1046 ;; This is careful to stop at the first
1048 ;; int foo Proto ((int, int));
1049 (looking-at "[^\"\n=(]*(")
1051 (goto-char (1- (match-end 0)))
1052 ;; Skip any number of paren-groups.
1053 ;; Consider typedef int (*fcn) (int);
1054 (while (= (following-char) ?\()
1059 (skip-chars-forward " \t\f"))
1060 ;; Have we reached something
1061 ;; that shows this isn't a function
1063 (and (< (point) indent-point)
1064 (not (memq (following-char)
1066 ;; Make sure the "function decl" we found
1067 ;; is not inside a comment.
1069 ;; Move back to the `(' starting arglist
1072 (while (and (not comment)
1073 (search-forward "/*" lim t))
1075 (not (search-forward "*/" lim t))))
1077 c-argdecl-indent 0))))))
1080 ;; ;; Now add a little if this is a continuation line.
1081 ;; (+ basic-indent (if (or (bobp)
1082 ;; (memq (preceding-char) '(?\) ?\; ?\}))
1083 ;; ;; Line with zero indentation
1084 ;; ;; is probably the return-type
1085 ;; ;; of a function definition,
1086 ;; ;; so following line is function name.
1087 ;; (= (current-indentation) 0))
1088 ;; 0 c-continued-statement-offset))
1090 ((/= (char-after containing-sexp) ?{)
1091 ;; line is expression, not statement:
1092 ;; indent to just after the surrounding open.
1093 (goto-char (1+ containing-sexp))
1096 ;; Statement level. Is it a continuation or a new statement?
1097 ;; Find previous non-comment character.
1098 (goto-char indent-point)
1099 (c-backward-to-noncomment containing-sexp)
1100 ;; Back up over label lines, since they don't
1101 ;; affect whether our line is a continuation.
1102 (while (or (eq (preceding-char) ?\,)
1103 (and (eq (preceding-char) ?:)
1104 (or (eq (char-after (- (point) 2)) ?\')
1105 (memq (char-syntax (char-after (- (point) 2)))
1107 (if (eq (preceding-char) ?\,)
1108 (progn (forward-char -1)
1109 (c-backward-to-start-of-continued-exp containing-sexp)))
1111 (c-backward-to-noncomment containing-sexp))
1112 ;; Check for a preprocessor statement or its continuation lines.
1113 ;; Move back to end of previous non-preprocessor line,
1114 ;; or possibly beginning of buffer.
1115 (let ((found (point)) stop)
1121 ((save-excursion (forward-char -1)
1122 (= (preceding-char) ?\\))
1124 ;; This line is not preceded by a backslash.
1125 ;; So either it starts a preprocessor command
1126 ;; or any following continuation lines
1127 ;; should not be skipped.
1128 ((= (following-char) ?#)
1130 (setq found (point)))
1133 ;; Now we get the answer.
1134 (if (and (not (memq (preceding-char) '(0 ?\, ?\; ?\} ?\{)))
1135 ;; But don't treat a line with a close-brace
1136 ;; as a continuation. It is probably the
1137 ;; end of an enum type declaration.
1139 (goto-char indent-point)
1140 (skip-chars-forward " \t")
1141 (not (= (following-char) ?}))))
1142 ;; This line is continuation of preceding line's statement;
1143 ;; indent c-continued-statement-offset more than the
1144 ;; previous line of the statement.
1146 (c-backward-to-start-of-continued-exp containing-sexp)
1147 (+ c-continued-statement-offset (current-column)
1148 (if (save-excursion (goto-char indent-point)
1149 (skip-chars-forward " \t")
1150 (eq (following-char) ?{))
1151 c-continued-brace-offset 0)))
1152 ;; This line starts a new statement.
1153 ;; Position following last unclosed open.
1154 (goto-char containing-sexp)
1155 ;; Is line first statement after an open-brace?
1157 ;; If no, find that first statement and indent like it.
1160 (let ((colon-line-end 0))
1161 (while (progn (skip-chars-forward " \t\n")
1162 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
1163 ;; Skip over comments and labels following openbrace.
1164 (cond ((= (following-char) ?\#)
1166 ((= (following-char) ?\/)
1168 (search-forward "*/" nil 'move))
1171 (save-excursion (end-of-line)
1172 (setq colon-line-end (point)))
1173 (search-forward ":"))))
1174 ;; The first following code counts
1175 ;; if it is before the line we want to indent.
1176 (and (< (point) indent-point)
1178 (if (> colon-line-end (point))
1179 (- (current-indentation) c-label-offset)
1181 ;; If prev stmt starts with open-brace, that
1182 ;; open brace was offset by c-brace-offset.
1183 ;; Compensate to get the column where
1184 ;; an ordinary statement would start.
1185 (if (= (following-char) ?\{) c-brace-offset 0)))))
1186 ;; If no previous statement,
1187 ;; indent it relative to line brace is on.
1188 (calculate-c-indent-after-brace))))))))
1190 (defun calculate-c-indent-after-brace ()
1191 "Return the proper C indent for the first line after an open-brace.
1192 This function is called with point before the brace."
1193 ;; For open brace in column zero, don't let statement
1194 ;; start there too. If c-indent-level is zero,
1195 ;; use c-brace-offset + c-continued-statement-offset instead.
1196 ;; For open-braces not the first thing in a line,
1197 ;; add in c-brace-imaginary-offset.
1198 (+ (if (and (bolp) (zerop c-indent-level))
1199 (+ c-brace-offset c-continued-statement-offset)
1201 ;; Move back over whitespace before the openbrace.
1202 ;; If openbrace is not first nonwhite thing on the line,
1203 ;; add the c-brace-imaginary-offset.
1204 (progn (skip-chars-backward " \t")
1205 (if (bolp) 0 c-brace-imaginary-offset))
1206 ;; If the openbrace is preceded by a parenthesized exp,
1207 ;; move to the beginning of that;
1208 ;; possibly a different line
1210 (if (eq (preceding-char) ?\))
1212 ;; Get initial indentation of the line we are on.
1213 (current-indentation))))
1215 (defun calculate-c-indent-within-comment (&optional after-star)
1216 "Return the indentation amount for line inside a block comment.
1217 Optional arg AFTER-STAR means, if lines in the comment have a leading star,
1218 return the indentation of the text that would follow this star."
1219 (let (end star-start)
1222 (skip-chars-forward " \t")
1223 (setq star-start (= (following-char) ?\*))
1224 (skip-chars-backward " \t\n")
1227 (skip-chars-forward " \t")
1229 (and (looking-at "\\*")
1230 (re-search-forward "\\*[ \t]*")))
1231 (and (re-search-forward "/\\*[ \t]*" end t)
1234 (goto-char (1+ (match-beginning 0))))
1235 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*))
1236 (1+ (current-column))
1237 (current-column)))))
1240 (defun c-backward-to-noncomment (lim)
1243 (skip-chars-backward " \t\n\f" lim)
1244 (setq opoint (point))
1245 (if (and (>= (point) (+ 2 lim))
1248 (looking-at "\\*/")))
1249 (search-backward "/*" lim 'move)
1250 (setq stop (or (<= (point) lim)
1253 (skip-chars-forward " \t")
1254 (not (looking-at "#")))))
1255 (or stop (beginning-of-line))))))
1257 (defun c-backward-to-start-of-continued-exp (lim)
1258 (if (memq (preceding-char) '(?\) ?\"))
1261 (if (<= (point) lim)
1262 (goto-char (1+ lim)))
1263 (skip-chars-forward " \t"))
1265 (defun c-backward-to-start-of-if (&optional limit)
1266 "Move to the start of the last \"unbalanced\" `if'."
1267 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
1269 (case-fold-search nil))
1270 (while (and (not (bobp)) (not (zerop if-level)))
1272 (cond ((and (looking-at "else\\b")
1273 (not (looking-at "else\\s_")))
1274 (setq if-level (1+ if-level)))
1275 ((and (looking-at "if\\b")
1276 (not (looking-at "if\\s_")))
1277 (setq if-level (1- if-level)))
1280 (goto-char limit))))))
1282 (defun c-backward-to-start-of-do (&optional limit)
1283 "If point follows a `do' statement, move to beginning of it and return t.
1284 Otherwise return nil and don't move point."
1285 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
1290 (let ((next-start (point)))
1292 ;; Move back one token or one brace or paren group.
1294 ;; If we find an open-brace, we lose.
1295 (error (setq done 'fail)))
1298 ;; If we reached a `do', we win.
1299 (if (looking-at "do\\b")
1300 (setq done 'succeed)
1301 ;; Otherwise, if we skipped a semicolon, we lose.
1302 ;; (Exception: we can skip one semicolon before getting
1303 ;; to a the last token of the statement, unless that token
1304 ;; is a close brace.)
1307 (or (and (not first) (= (preceding-char) ?}))
1308 (search-forward ";" next-start t
1310 (/= (preceding-char) ?}))
1314 ;; If we go too far back in the buffer, we lose.
1315 (if (< (point) limit)
1316 (setq done 'fail)))))))
1317 (if (eq done 'succeed)
1319 (goto-char startpos)
1322 (defun c-beginning-of-statement (count)
1323 "Go to the beginning of the innermost C statement.
1324 With prefix arg, go back N - 1 statements. If already at the beginning of a
1325 statement then go to the beginning of the preceding one.
1326 If within a string or comment, or next to a comment (only whitespace between),
1327 move by sentences instead of statements."
1329 (let ((here (point)) state)
1331 (beginning-of-defun)
1332 (setq state (parse-partial-sexp (point) here nil nil)))
1333 (if (or (nth 3 state) (nth 4 state)
1334 (looking-at (concat "[ \t]*" comment-start-skip))
1335 (save-excursion (skip-chars-backward " \t")
1336 (goto-char (- (point) 2))
1337 (looking-at "\\*/")))
1338 (forward-sentence (- count))
1340 (c-beginning-of-statement-1)
1341 (setq count (1- count)))
1343 (c-end-of-statement-1)
1344 (setq count (1+ count))))))
1346 (defun c-end-of-statement (count)
1347 "Go to the end of the innermost C statement.
1348 With prefix arg, go forward N - 1 statements.
1349 Move forward to end of the next statement if already at end.
1350 If within a string or comment, move by sentences instead of statements."
1352 (c-beginning-of-statement (- count)))
1354 (defun c-beginning-of-statement-1 ()
1355 (let ((last-begin (point))
1359 (while (and (not (bobp))
1363 (not (re-search-forward "[;{}]" last-begin t)))))
1364 (setq last-begin (point) first nil))
1365 (goto-char last-begin))
1366 (error (if first (backward-up-list 1) (goto-char last-begin))))))
1368 (defun c-end-of-statement-1 ()
1371 (while (and (not (eobp))
1372 (let ((beg (point)))
1374 (let ((end (point)))
1377 (not (re-search-forward "[;{}]" end t)))))))
1378 (re-search-backward "[;}]")
1381 (let ((beg (point)))
1382 (backward-up-list -1)
1383 (let ((end (point)))
1385 (search-forward ";" end 'move))))))
1387 (defun mark-c-function ()
1388 "Put mark at end of C function, point at beginning."
1392 (push-mark (point) nil t)
1393 (beginning-of-defun)
1394 (backward-paragraph))
1396 ;; Idea of ENDPOS is, indent each line, stopping when
1397 ;; ENDPOS is encountered. But it's too much of a pain to make that work.
1398 (defun indent-c-exp (&optional endpos)
1399 "Indent each line of the C grouping following point."
1401 (let* ((indent-stack (list nil))
1402 (opoint (point)) ;; May be altered below.
1406 ;; Find previous fcn-start.
1407 (save-excursion (forward-char 1)
1408 (beginning-of-defun)
1409 (setq funbeg (point)))
1410 (setq opoint funbeg)
1411 ;; Try to find containing open,
1412 ;; but don't scan past that fcn-start.
1414 (narrow-to-region funbeg (point))
1417 (backward-up-list 1)
1419 ;; We gave up: must be between fcns.
1420 ;; Set opoint to beg of prev fcn
1421 ;; since otherwise calculate-c-indent
1422 ;; will get wrong answers.
1423 (error (setq opoint funbeg)
1426 (case-fold-search nil)
1427 restart outer-loop-done inner-loop-done state ostate
1428 this-indent last-sexp
1429 at-else at-brace at-while
1430 last-depth this-point
1432 ;; If the braces don't match, get an error right away.
1435 ;; Realign the comment on the first line, even though we don't reindent it.
1437 (let ((beg (point)))
1438 (and (re-search-forward
1440 (save-excursion (end-of-line) (point)) t)
1441 ;; Make sure this isn't a comment alone on a line
1442 ;; (which should be indented like code instead).
1444 (goto-char (match-beginning 0))
1445 (skip-chars-backward " \t")
1447 ;; Make sure the comment starter we found
1448 ;; is not actually in a string or quoted.
1450 (parse-partial-sexp beg (point)
1452 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
1453 (progn (indent-for-comment) (beginning-of-line)))))
1455 (setq outer-loop-done nil)
1456 (while (and (not (eobp))
1457 (if endpos (< (point) endpos)
1458 (not outer-loop-done)))
1459 (setq last-depth next-depth)
1460 ;; Compute how depth changes over this line
1461 ;; plus enough other lines to get to one that
1462 ;; does not end inside a comment or string.
1463 ;; Meanwhile, do appropriate indentation on comment lines.
1464 (setq inner-loop-done nil)
1465 (while (and (not inner-loop-done)
1466 (not (and (eobp) (setq outer-loop-done t))))
1468 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1470 (setq next-depth (car state))
1471 (if (and (car (cdr (cdr state)))
1472 (>= (car (cdr (cdr state))) 0))
1473 (setq last-sexp (car (cdr (cdr state)))))
1474 ;; If this line started within a comment, indent it as such.
1475 (if (or (nth 4 ostate) (nth 7 ostate))
1477 ;; If it ends outside of comments or strings, exit the inner loop.
1478 ;; Otherwise move on to next line.
1479 (if (or (nth 3 state) (nth 4 state) (nth 7 state))
1481 (setq inner-loop-done t)))
1483 (while (< next-depth 0)
1484 (setq indent-stack (append indent-stack (list nil)))
1485 (setq contain-stack (append contain-stack (list nil)))
1486 (setq next-depth (1+ next-depth))
1487 (setq last-depth (1+ last-depth))
1488 (setcar (nthcdr 6 state) (1+ (nth 6 state)))))
1489 (setq outer-loop-done (and (not endpos) (<= next-depth 0)))
1492 ;; If this line had ..))) (((.. in it, pop out of the levels
1493 ;; that ended anywhere in this line, even if the final depth
1494 ;; doesn't indicate that they ended.
1495 (while (> last-depth (nth 6 state))
1496 (setq indent-stack (cdr indent-stack)
1497 contain-stack (cdr contain-stack)
1498 last-depth (1- last-depth)))
1499 (if (/= last-depth next-depth)
1500 (setq last-sexp nil))
1501 ;; Add levels for any parens that were started in this line.
1502 (while (< last-depth next-depth)
1503 (setq indent-stack (cons nil indent-stack)
1504 contain-stack (cons nil contain-stack)
1505 last-depth (1+ last-depth)))
1506 (if (null (car contain-stack))
1507 (setcar contain-stack (or (car (cdr state))
1508 (save-excursion (forward-sexp -1)
1511 (skip-chars-forward " \t")
1512 ;; Don't really reindent if the line is just whitespace,
1513 ;; or if it is past the endpos.
1514 ;; (The exit test in the outer while
1515 ;; does not exit until we have passed the first line
1516 ;; past the region.)
1517 (if (or (eolp) (and endpos (>= (point) endpos)))
1519 ;; Is this line in a new nesting level?
1520 ;; In other words, is this the first line that
1521 ;; starts in the new level?
1522 (if (and (car indent-stack)
1523 (>= (car indent-stack) 0))
1526 ;; Compute the standard indent for this level.
1528 (if (= (char-after (car contain-stack)) ?{)
1530 (goto-char (car contain-stack))
1531 (setq val (calculate-c-indent-after-brace)))
1532 (setq val (calculate-c-indent
1533 (if (car indent-stack)
1534 (- (car indent-stack))
1536 ;; t means we are in a block comment and should
1537 ;; calculate accordingly.
1539 (setq val (calculate-c-indent-within-comment)))
1540 (setcar indent-stack val)))
1541 ;; Adjust indent of this individual line
1542 ;; based on its predecessor.
1543 ;; Handle continuation lines, if, else, while, and so on.
1544 (if (/= (char-after (car contain-stack)) ?{)
1545 (setq this-indent (car indent-stack))
1546 ;; Line is at statement level.
1547 ;; Is it a new statement? Is it an else?
1548 ;; Find last non-comment character before this line
1550 (setq this-point (point))
1551 (setq at-else (and (looking-at "else\\b")
1552 (not (looking-at "else\\s_"))))
1553 (setq at-brace (= (following-char) ?{))
1554 (setq at-while (and (looking-at "while\\b")
1555 (not (looking-at "while\\s_"))))
1556 (if (= (following-char) ?})
1557 (setq this-indent (car indent-stack))
1558 (c-backward-to-noncomment opoint)
1559 (if (not (memq (preceding-char) '(0 ?\, ?\; ?} ?: ?{)))
1560 ;; Preceding line did not end in comma or semi;
1561 ;; indent this line c-continued-statement-offset
1562 ;; more than previous.
1564 (c-backward-to-start-of-continued-exp (car contain-stack))
1566 (+ c-continued-statement-offset (current-column)
1567 (if at-brace c-continued-brace-offset 0))))
1568 ;; Preceding line ended in comma or semi;
1569 ;; use the standard indent for this level.
1570 (cond (at-else (progn (c-backward-to-start-of-if opoint)
1572 (current-indentation))))
1573 ((and at-while (c-backward-to-start-of-do opoint))
1574 (setq this-indent (current-indentation)))
1575 ((eq (preceding-char) ?\,)
1576 (goto-char this-point)
1577 (setq this-indent (calculate-c-indent)))
1578 (t (setq this-indent (car indent-stack))))))))
1579 ;; Adjust line indentation according to its contents
1580 (if (or (looking-at c-switch-label-regexp)
1581 (and (looking-at "[A-Za-z]")
1585 (setq this-indent (max 1 (+ this-indent c-label-offset))))
1586 (if (= (following-char) ?})
1587 (setq this-indent (- this-indent c-indent-level)))
1588 (if (= (following-char) ?{)
1589 ;; Don't move an open-brace in column 0.
1590 ;; This is good when constructs such as
1591 ;; `extern "C" {' surround a function definition
1592 ;; that should be indented as usual.
1593 ;; It is also good for nested functions.
1594 ;; It is bad when an open-brace is indented at column 0
1595 ;; and you want to fix that, but we can't win 'em all.
1596 (if (zerop (current-column))
1597 (setq this-indent 0)
1598 (setq this-indent (+ this-indent c-brace-offset))))
1599 ;; Don't leave indentation in empty lines.
1600 (if (eolp) (setq this-indent 0))
1601 ;; Put chosen indentation into effect.
1602 (or (= (current-column) this-indent)
1603 (= (following-char) ?\#)
1605 (delete-region (point) (progn (beginning-of-line) (point)))
1606 (indent-to this-indent)))
1607 ;; Indent any comment following the text.
1608 (or (looking-at comment-start-skip)
1610 (let ((beg (point)))
1611 (and (re-search-forward
1613 (save-excursion (end-of-line) (point)) t)
1614 ;; Make sure the comment starter we found
1615 ;; is not actually in a string or quoted.
1617 (parse-partial-sexp beg (point)
1619 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
1620 (indent-for-comment)))))))))))
1622 ;; Look at all comment-start strings in the current line after point.
1623 ;; Return t if one of them starts a real comment.
1624 ;; This is not used yet, because indent-for-comment
1625 ;; isn't smart enough to handle the cases this can find.
1626 (defun indent-c-find-real-comment ()
1628 (while (and (not win)
1629 (re-search-forward comment-start-skip
1630 (save-excursion (end-of-line) (point))
1632 ;; Make sure the comment start is not quoted.
1635 (save-excursion (beginning-of-line) (point))
1636 (point) nil nil state)))
1637 (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1))))))
1640 ;; Indent every line whose first char is between START and END inclusive.
1641 (defun c-indent-region (start end)
1644 ;; Advance to first nonblank line.
1645 (skip-chars-forward " \t\n")
1647 (let ((endmark (copy-marker end))
1648 (c-tab-always-indent t))
1649 (while (and (bolp) (not (eobp)) (< (point) endmark))
1650 ;; Indent one line as with TAB.
1651 (let ((shift-amt (c-indent-line))
1652 nextline sexpbeg sexpend)
1653 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*#"))
1656 ;; Find beginning of following line.
1658 (forward-line 1) (setq nextline (point)))
1659 ;; Find first beginning-of-sexp for sexp extending past this line.
1661 (while (< (point) nextline)
1665 (setq sexpend (point-marker)))
1666 (error (setq sexpend nil)
1667 (goto-char nextline)))
1668 (skip-chars-forward " \t\n"))
1671 ;; Make sure the sexp we found really starts on the
1672 ;; current line and extends past it.
1675 (setq sexpbeg (point)))))
1676 ;; If that sexp ends within the region,
1677 ;; indent it all at once, fast.
1678 (if (and sexpend (> sexpend nextline) (<= sexpend endmark)
1679 (< sexpbeg nextline))
1682 (goto-char sexpend)))
1683 ;; Move to following line and try again.
1684 (and sexpend (set-marker sexpend nil))
1686 (set-marker endmark nil))))
1688 (defun set-c-style (style &optional global)
1689 "Set C-mode variables to use one of several different indentation styles.
1690 The arguments are a string representing the desired style
1691 and a flag which, if non-nil, means to set the style globally.
1692 \(Interactively, the flag comes from the prefix argument.)
1693 Available styles are GNU, K&R, BSD and Whitesmith."
1694 (interactive (list (let ((completion-ignore-case t))
1695 (completing-read "Use which C indentation style? "
1696 c-style-alist nil t))
1697 current-prefix-arg))
1698 (let ((vars (cdr (assoc style c-style-alist))))
1700 (error "Invalid C indentation style `%s'" style))
1703 (make-local-variable (car (car vars))))
1704 (set (car (car vars)) (cdr (car vars)))
1705 (setq vars (cdr vars)))))
1707 ;;; This page handles insertion and removal of backslashes for C macros.
1709 (defcustom c-backslash-column 48
1710 "*Minimum column for end-of-line backslashes of macro definitions."
1714 (defun c-backslash-region (from to delete-flag)
1715 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1716 With no argument, inserts backslashes and aligns existing backslashes.
1717 With an argument, deletes the backslashes.
1719 This function does not modify the last line of the region if the region ends
1720 right at the start of the following line; it does not modify blank lines
1721 at the start of the region. So you can put the region around an entire macro
1722 definition and conveniently use this command."
1723 (interactive "r\nP")
1726 (let ((column c-backslash-column)
1727 (endmark (make-marker)))
1728 (move-marker endmark to)
1729 ;; Compute the smallest column number past the ends of all the lines.
1730 (if (not delete-flag)
1731 (while (< (point) to)
1733 (if (= (preceding-char) ?\\)
1734 (progn (forward-char -1)
1735 (skip-chars-backward " \t")))
1736 (setq column (max column (1+ (current-column))))
1738 ;; Adjust upward to a tab column, if that doesn't push past the margin.
1739 (if (> (% column tab-width) 0)
1740 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
1741 (if (< adjusted (window-width))
1742 (setq column adjusted))))
1743 ;; Don't modify blank lines at start of region.
1745 (while (and (< (point) endmark) (eolp))
1747 ;; Add or remove backslashes on all the lines.
1748 (while (and (< (point) endmark)
1749 ;; Don't backslashify the last line
1750 ;; if the region ends right at the start of the next line.
1753 (< (point) endmark)))
1754 (if (not delete-flag)
1755 (c-append-backslash column)
1756 (c-delete-backslash))
1758 (move-marker endmark nil))))
1760 (defun c-append-backslash (column)
1762 ;; Note that "\\\\" is needed to get one backslash.
1763 (if (= (preceding-char) ?\\)
1764 (progn (forward-char -1)
1765 (delete-horizontal-space)
1770 (defun c-delete-backslash ()
1775 (if (looking-at "\\\\")
1776 (delete-region (1+ (point))
1777 (progn (skip-chars-backward " \t") (point)))))))
1779 (defun c-up-conditional (count)
1780 "Move back to the containing preprocessor conditional, leaving mark behind.
1781 A prefix argument acts as a repeat count. With a negative argument,
1782 move forward to the end of the containing preprocessor conditional.
1783 When going backwards, `#elif' is treated like `#else' followed by `#if'.
1784 When going forwards, `#elif' is ignored."
1786 (c-forward-conditional (- count) t))
1788 (defun c-backward-conditional (count &optional up-flag)
1789 "Move back across a preprocessor conditional, leaving mark behind.
1790 A prefix argument acts as a repeat count. With a negative argument,
1791 move forward across a preprocessor conditional."
1793 (c-forward-conditional (- count) up-flag))
1795 (defun c-forward-conditional (count &optional up-flag)
1796 "Move forward across a preprocessor conditional, leaving mark behind.
1797 A prefix argument acts as a repeat count. With a negative argument,
1798 move backward across a preprocessor conditional."
1800 (let* ((forward (> count 0))
1801 (increment (if forward -1 1))
1802 (search-function (if forward 're-search-forward 're-search-backward))
1807 (let ((depth (if up-flag 0 -1)) found)
1809 ;; Find the "next" significant line in the proper direction.
1810 (while (and (not found)
1811 ;; Rather than searching for a # sign that comes
1812 ;; at the beginning of a line aside from whitespace,
1813 ;; search first for a string starting with # sign.
1814 ;; Then verify what precedes it.
1815 ;; This is faster on account of the fastmap feature of
1816 ;; the regexp matcher.
1817 (funcall search-function
1818 "#[ \t]*\\(if\\|elif\\|endif\\)"
1821 ;; Now verify it is really a preproc line.
1822 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
1824 ;; Update depth according to what we found.
1826 (cond ((looking-at "[ \t]*#[ \t]*endif")
1827 (setq depth (+ depth increment)))
1828 ((looking-at "[ \t]*#[ \t]*elif")
1829 (if (and forward (= depth 0))
1830 (setq found (point))))
1831 (t (setq depth (- depth increment))))
1832 ;; If we are trying to move across, and we find
1833 ;; an end before we find a beginning, get an error.
1834 (if (and (< prev 0) (< depth prev))
1836 "No following conditional at this level"
1837 "No previous conditional at this level")))
1838 ;; When searching forward, start from next line
1839 ;; so that we don't find the same line again.
1840 (if forward (forward-line 1))
1841 ;; If this line exits a level of conditional, exit inner loop.
1843 (setq found (point))))
1844 ;; If the line is not really a conditional, skip past it.
1845 (if forward (end-of-line)))))
1847 (error "No containing preprocessor conditional"))
1848 (goto-char (setq new found)))
1849 (setq count (+ count increment))))
1853 ;;; begin stuff from cc-mode
1855 (defsubst c-region-is-active-p ()
1856 ;; Return t when the region is active. The determination of region
1857 ;; activeness is different in both Emacs and XEmacs.
1860 ((and (fboundp 'region-active-p)
1861 (boundp 'zmacs-regions)
1865 ((boundp 'mark-active) mark-active)
1866 ;; fallback; shouldn't get here
1869 (defun c-fn-region-is-active-p ()
1870 ;; Function version of the macro for use in places that aren't
1871 ;; compiled, e.g. in the menus.
1872 (c-region-is-active-p))
1874 (defun c-indent-line-or-region ()
1875 "When the region is active, indent it. Otherwise indent the current line."
1876 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p
1878 (if (c-region-is-active-p)
1879 (c-indent-region (region-beginning) (region-end))
1880 (indent-according-to-mode)))
1882 (defmacro c-point (position &optional point)
1883 ;; Returns the value of certain commonly referenced POSITIONs
1884 ;; relative to POINT. The current point is used if POINT isn't
1885 ;; specified. POSITION can be one of the following symbols:
1887 ;; bol -- beginning of line
1888 ;; eol -- end of line
1889 ;; bod -- beginning of defun
1890 ;; eod -- end of defun
1891 ;; boi -- back to indentation
1892 ;; ionl -- indentation of next line
1893 ;; iopl -- indentation of previous line
1894 ;; bonl -- beginning of next line
1895 ;; bopl -- beginning of previous line
1897 ;; This function does not modify point or mark.
1899 ,(if point `(goto-char ,point))
1900 ,(if (and (eq (car-safe position) 'quote)
1901 (symbolp (eval position)))
1902 (let ((position (eval position)))
1904 ((eq position 'bol) `(beginning-of-line))
1905 ((eq position 'eol) `(end-of-line))
1906 ((eq position 'boi) `(back-to-indentation))
1907 ((eq position 'bonl) `(forward-line 1))
1908 ((eq position 'bopl) `(forward-line -1))
1909 ((eq position 'bod) `(c-beginning-of-defun-1))
1910 ((eq position 'eod) `(c-end-of-defun-1))
1911 ((eq position 'iopl) `(progn
1913 (back-to-indentation)))
1914 ((eq position 'ionl) `(progn
1916 (back-to-indentation)))
1917 (t (error "unknown buffer position requested: %s" position))))
1918 ;;(message "c-point long expansion")
1919 `(let ((position ,position))
1921 ((eq position 'bol) (beginning-of-line))
1922 ((eq position 'eol) (end-of-line))
1923 ((eq position 'boi) (back-to-indentation))
1924 ((eq position 'bonl) (forward-line 1))
1925 ((eq position 'bopl) (forward-line -1))
1926 ((eq position 'bod) (c-beginning-of-defun-1))
1927 ((eq position 'eod) (c-end-of-defun-1))
1928 ((eq position 'iopl) (progn
1930 (back-to-indentation)))
1931 ((eq position 'ionl) (progn
1933 (back-to-indentation)))
1934 (t (error "unknown buffer position requested: %s" position)))))
1937 (defmacro c-safe (&rest body)
1938 ;; safely execute BODY, return nil if an error occurred
1939 `(condition-case nil
1943 (defmacro c-forward-sexp (&optional arg)
1944 ;; like forward-sexp except
1945 ;; 1. this is much stripped down from the XEmacs version
1946 ;; 2. this cannot be used as a command, so we're insulated from
1947 ;; XEmacs' losing efforts to make forward-sexp more user
1949 ;; 3. Preserves the semantics most of CC Mode is based on
1950 (or arg (setq arg 1))
1951 `(goto-char (or (scan-sexps (point) ,arg)
1953 (if (> arg 0) `(point-max) `(point-min))
1954 `(if (> ,arg 0) (point-max) (point-min))))))
1956 (defmacro c-backward-sexp (&optional arg)
1957 ;; See c-forward-sexp and reverse directions
1958 (or arg (setq arg 1))
1959 `(c-forward-sexp ,(if (numberp arg) (- arg) `(- ,arg))))
1961 (defun c-indent-exp (&optional shutup-p)
1962 "Indent each line in the balanced expression following point syntactically.
1963 If optional SHUTUP-P is non-nil, no errors are signalled if no
1964 balanced expression is found."
1966 (let ((here (point-marker))
1968 (set-marker-insertion-type here t)
1971 ;; try to be smarter about finding the range of
1972 ;; lines to indent. skip all following
1973 ;; whitespace, then try to find any
1974 ;; opening paren on the current line
1975 (skip-chars-forward " \t\n")
1977 (narrow-to-region (point-min) (c-point 'eol))
1978 (c-safe (1- (scan-lists (point) 1 -1)))))))
1979 ;; find balanced expression end
1980 (setq end (and (c-safe (progn (c-forward-sexp 1) t))
1985 (error "Cannot find start of balanced expression to indent"))
1988 (error "Cannot find end of balanced expression to indent"))
1989 (c-indent-region start end))))
1991 (set-marker here nil))))
1993 (defun c-insert-matching-endif ()
1994 "Insert a #endif expression matching the corresponding #if... statement.
1995 This skips across any number of intervening #if/#endif pairs to find the
1996 matching #if/#ifdef/#ifndef statement and inserts a #endif with a comment
1997 corresponding to the expression following the #if. If the statement is
1998 a #ifndef, the word \"not\" appears in the comment. If there are spaces
1999 between the '#' and the 'if', the same number of spaces appear in the
2000 #endif. Comments following the #if statement are ignored and do not appear
2001 in the #endif comment."
2003 (let (spaces expr notp)
2005 (c-up-conditional 1)
2006 ;; we have to construct the regexps carefully to avoid running into
2007 ;; problems due to the greedy operators. XEmacs has non-greedy
2008 ;; operators, which would simplify matters, but they're not present
2010 (or (looking-at "#\\( *\\)if\\(n?\\)[a-z]*\\(.*[^ \t\n]\\)[ \t]*/[*/].*")
2011 (looking-at "#\\( *\\)if\\(n?\\)[a-z]*\\(.*[^ \t\n]\\)")
2012 (error "Malformed #if... expression"))
2013 (setq spaces (match-string 1))
2014 (setq notp (> (match-end 2) (match-beginning 2)))
2015 (setq expr (match-string 3)))
2016 (insert "#" spaces "endif /*" (if notp " not" "") expr " */\n")))
2018 ;;; end stuff from cc-mode
2024 ;;; old-c-mode.el ends here