cc-cmds.el 156 KB
Newer Older
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1
;;; cc-cmds.el --- user level commands for CC Mode
Richard M. Stallman's avatar
Richard M. Stallman committed
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
Glenn Morris's avatar
Glenn Morris committed
4 5
;;   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
;;   Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
6

Alan Mackenzie's avatar
Alan Mackenzie committed
7 8
;; Authors:    2003- Alan Mackenzie
;;             1998- Martin Stjernholm
9
;;             1992-1999 Barry A. Warsaw
Richard M. Stallman's avatar
Richard M. Stallman committed
10 11
;;             1987 Dave Detlefs and Stewart Clamen
;;             1985 Richard M. Stallman
Barry A. Warsaw's avatar
Barry A. Warsaw committed
12
;; Maintainer: bug-cc-mode@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
13
;; Created:    22-Apr-1997 (split from cc-mode.el)
14
;; Version:    See cc-mode.el
Richard M. Stallman's avatar
Richard M. Stallman committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
;; Keywords:   c languages oop

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
30
;; along with this program; see the file COPYING.  If not, write to
Lute Kamstra's avatar
Lute Kamstra committed
31 32
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
33

34 35 36 37
;;; Commentary:

;;; Code:

Barry A. Warsaw's avatar
Barry A. Warsaw committed
38
(eval-when-compile
Gerd Moellmann's avatar
Gerd Moellmann committed
39
  (let ((load-path
Gerd Moellmann's avatar
Gerd Moellmann committed
40 41 42
	 (if (and (boundp 'byte-compile-dest-file)
		  (stringp byte-compile-dest-file))
	     (cons (file-name-directory byte-compile-dest-file) load-path)
Gerd Moellmann's avatar
Gerd Moellmann committed
43
	   load-path)))
44
    (load "cc-bytecomp" nil t)))
Gerd Moellmann's avatar
Gerd Moellmann committed
45 46 47 48 49 50

(cc-require 'cc-defs)
(cc-require 'cc-vars)
(cc-require 'cc-engine)

;; Silence the compiler.
51
(cc-bytecomp-defun delete-forward-p)	; XEmacs
Gerd Moellmann's avatar
Gerd Moellmann committed
52 53
(cc-bytecomp-defvar filladapt-mode)	; c-fill-paragraph contains a kludge
					; which looks at this.
54 55
(cc-bytecomp-defun c-forward-subword)
(cc-bytecomp-defun c-backward-subword)
Gerd Moellmann's avatar
Gerd Moellmann committed
56

57
;; Indentation / Display syntax functions
58 59 60 61 62 63 64 65 66 67 68 69
(defvar c-fix-backslashes t)

(defun c-indent-line (&optional syntax quiet ignore-point-pos)
  "Indent the current line according to the syntactic context,
if `c-syntactic-indentation' is non-nil.  Optional SYNTAX is the
syntactic information for the current line.  Be silent about syntactic
errors if the optional argument QUIET is non-nil, even if
`c-report-syntactic-errors' is non-nil.  Normally the position of
point is used to decide where the old indentation is on a lines that
is otherwise empty \(ignoring any line continuation backslash), but
that's not done if IGNORE-POINT-POS is non-nil.  Returns the amount of
indentation change \(in columns)."
70

71 72 73 74 75 76 77 78 79 80
  (let ((line-cont-backslash (save-excursion
			       (end-of-line)
			       (eq (char-before) ?\\)))
	(c-fix-backslashes c-fix-backslashes)
	bs-col
	shift-amt)
    (when (and (not ignore-point-pos)
	       (save-excursion
		 (beginning-of-line)
		 (looking-at (if line-cont-backslash
81 82 83 84
				 ;; Don't use "\\s " - ^L doesn't count as WS
				 ;; here
				 "\\([ \t]*\\)\\\\$"
			       "\\([ \t]*\\)$")))
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	       (<= (point) (match-end 1)))
      ;; Delete all whitespace after point if there's only whitespace
      ;; on the line, so that any code that does back-to-indentation
      ;; or similar gets the current column in this case.  If this
      ;; removes a line continuation backslash it'll be restored
      ;; at the end.
      (unless c-auto-align-backslashes
	;; Should try to keep the backslash alignment
	;; in this case.
	(save-excursion
	  (goto-char (match-end 0))
	  (setq bs-col (1- (current-column)))))
      (delete-region (point) (match-end 0))
      (setq c-fix-backslashes t))
    (if c-syntactic-indentation
	(setq c-parsing-error
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	      (or (let ((c-parsing-error nil)
			(c-syntactic-context
			 (or syntax
			     (and (boundp 'c-syntactic-context)
				  c-syntactic-context))))
		    (c-save-buffer-state (indent)
		      (unless c-syntactic-context
			(setq c-syntactic-context (c-guess-basic-syntax)))
		      (setq indent (c-get-syntactic-indentation
				    c-syntactic-context))
		      (and (not (c-echo-parsing-error quiet))
			   c-echo-syntactic-information-p
			   (message "syntax: %s, indent: %d"
				    c-syntactic-context indent))
		      (setq shift-amt (- indent (current-indentation))))
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		    (c-shift-line-indentation shift-amt)
		    (run-hooks 'c-special-indent-hook)
		    c-parsing-error)
		  c-parsing-error))
      (let ((indent 0))
	(save-excursion
	  (while (and (= (forward-line -1) 0)
		      (if (looking-at "\\s *\\\\?$")
			  t
			(setq indent (current-indentation))
			nil))))
	(setq shift-amt (- indent (current-indentation)))
	(c-shift-line-indentation shift-amt)))
    (when (and c-fix-backslashes line-cont-backslash)
      (if bs-col
	  (save-excursion
	    (indent-to bs-col)
	    (insert ?\\))
	(when c-auto-align-backslashes
	  ;; Realign the line continuation backslash.
	  (c-backslash-region (point) (point) nil t))))
    shift-amt))

(defun c-newline-and-indent (&optional newline-arg)
140
  "Insert a newline and indent the new line.
141 142 143 144
This function fixes line continuation backslashes if inside a macro,
and takes care to set the indentation before calling
`indent-according-to-mode', so that lineup functions like
`c-lineup-dont-change' works better."
145 146

  ;; TODO: Backslashes before eol in comments and literals aren't
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  ;; kept intact.
  (let ((c-macro-start (c-query-macro-start))
	;; Avoid calling c-backslash-region from c-indent-line if it's
	;; called during the newline call, which can happen due to
	;; c-electric-continued-statement, for example.  We also don't
	;; want any backslash alignment from indent-according-to-mode.
	(c-fix-backslashes nil)
	has-backslash insert-backslash
	start col)
    (save-excursion
      (beginning-of-line)
      (setq start (point))
      (while (and (looking-at "[ \t]*\\\\?$")
		  (= (forward-line -1) 0)))
      (setq col (current-indentation)))
    (when c-macro-start
      (if (and (eolp) (eq (char-before) ?\\))
	  (setq insert-backslash t
		has-backslash t)
	(setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
    (newline newline-arg)
    (indent-to col)
    (when c-macro-start
      (if insert-backslash
	  (progn
	    ;; The backslash stayed on the previous line.  Insert one
	    ;; before calling c-backslash-region, so that
	    ;; bs-col-after-end in it works better.  Fixup the
	    ;; backslashes on the newly inserted line.
	    (insert ?\\)
	    (backward-char)
	    (c-backslash-region (point) (point) nil t))
	;; The backslash moved to the new line, if there was any.  Let
	;; c-backslash-region fix a backslash on the previous line,
	;; and the one that might be on the new line.
	;; c-auto-align-backslashes is intentionally ignored here;
	;; maybe the moved backslash should be left alone if it's set,
	;; but we fix both lines on the grounds that the old backslash
	;; has been moved anyway and is now in a different context.
	(c-backslash-region start (if has-backslash (point) start) nil t)))
    (when c-syntactic-indentation
      ;; Reindent syntactically.  The indentation done above is not
      ;; wasted, since c-indent-line might look at the current
      ;; indentation.
191 192
      (let ((c-syntactic-context (c-save-buffer-state nil
				   (c-guess-basic-syntax))))
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	;; We temporarily insert another line break, so that the
	;; lineup functions will see the line as empty.  That makes
	;; e.g. c-lineup-cpp-define more intuitive since it then
	;; proceeds to the preceding line in this case.
	(insert ?\n)
	(delete-horizontal-space)
	(setq start (- (point-max) (point)))
	(unwind-protect
	    (progn
	      (backward-char)
	      (indent-according-to-mode))
	  (goto-char (- (point-max) start))
	  (delete-char -1)))
      (when has-backslash
	;; Must align the backslash again after reindentation.  The
	;; c-backslash-region call above can't be optimized to ignore
	;; this line, since it then won't align correctly with the
	;; lines below if the first line in the macro is broken.
	(c-backslash-region (point) (point) nil t)))))

(defun c-show-syntactic-information (arg)
  "Show syntactic information for current line.
With universal argument, inserts the analysis as a comment on that line."
  (interactive "P")
  (let* ((c-parsing-error nil)
218 219 220 221 222 223
	 (syntax (if (boundp 'c-syntactic-context)
		     ;; Use `c-syntactic-context' in the same way as
		     ;; `c-indent-line', to be consistent.
		     c-syntactic-context
		   (c-save-buffer-state nil
		     (c-guess-basic-syntax)))))
224
    (if (not (consp arg))
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	(let (elem pos ols)
	  (message "Syntactic analysis: %s" syntax)
	  (unwind-protect
	      (progn
		(while syntax
		  (setq elem (pop syntax))
		  (when (setq pos (c-langelem-pos elem))
		    (push (c-put-overlay pos (1+ pos)
					 'face 'highlight)
			  ols))
		  (when (setq pos (c-langelem-2nd-pos elem))
		    (push (c-put-overlay pos (1+ pos)
					 'face 'secondary-selection)
			  ols)))
		(sit-for 10))
	    (while ols
	      (c-delete-overlay (pop ols)))))
242 243 244 245 246 247
      (indent-for-comment)
      (insert-and-inherit (format "%s" syntax))
      ))
  (c-keep-region-active))

(defun c-syntactic-information-on-region (from to)
248
  "Insert a comment with the syntactic analysis on every line in the region."
249 250 251 252 253 254 255 256 257 258
  (interactive "*r")
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char (point-min))
      (while (not (eobp))
	(c-show-syntactic-information '(0))
	(forward-line)))))


259
;; Minor mode functions.
260 261 262 263 264 265 266 267 268 269 270
(defun c-update-modeline ()
  (let ((fmt (format "/%s%s%s%s"
		     (if c-electric-flag "l" "")
		     (if (and c-electric-flag c-auto-newline)
			 "a" "")
		     (if c-hungry-delete-key "h" "")
		     (if (and
			  ;; cc-subword might not be loaded.
			  (boundp 'c-subword-mode)
			  (symbol-value 'c-subword-mode))
			 "w"
271 272 273 274 275 276 277 278
		       "")))
	(bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name)
			    (substring mode-name (match-beginning 1) (match-end 1))
			  mode-name)))
;;     (setq c-submode-indicators
;; 	  (if (> (length fmt) 1)
;; 	      fmt))
    (setq mode-name
279
	  (if (> (length fmt) 1)
280
	      (concat bare-mode-name fmt)
281
	bare-mode-name))
282 283
    (force-mode-line-update)))

284 285 286 287 288 289 290 291 292 293
(defun c-toggle-syntactic-indentation (&optional arg)
  "Toggle syntactic indentation.
Optional numeric ARG, if supplied, turns on syntactic indentation when
positive, turns it off when negative, and just toggles it when zero or
left out.

When syntactic indentation is turned on (the default), the indentation
functions and the electric keys indent according to the syntactic
context keys, when applicable.

294 295 296 297 298 299
When it's turned off, the electric keys don't reindent, the indentation
functions indents every new line to the same level as the previous
nonempty line, and \\[c-indent-command] adjusts the indentation in steps
specified by `c-basic-offset'.  The indentation style has no effect in
this mode, nor any of the indentation associated variables,
e.g. `c-special-indent-hook'.
300 301 302 303 304 305 306

This command sets the variable `c-syntactic-indentation'."
  (interactive "P")
  (setq c-syntactic-indentation
	(c-calculate-state arg c-syntactic-indentation))
  (c-keep-region-active))

307
(defun c-toggle-auto-newline (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
308
  "Toggle auto-newline feature.
Gerd Moellmann's avatar
Gerd Moellmann committed
309 310 311
Optional numeric ARG, if supplied, turns on auto-newline when
positive, turns it off when negative, and just toggles it when zero or
left out.
Richard M. Stallman's avatar
Richard M. Stallman committed
312

313 314 315 316 317
Turning on auto-newline automatically enables electric indentation.

When the auto-newline feature is enabled (indicated by \"/la\" on the
modeline after the mode name) newlines are automatically inserted
after special characters such as brace, comma, semi-colon, and colon."
Richard M. Stallman's avatar
Richard M. Stallman committed
318
  (interactive "P")
319 320 321
  (setq c-auto-newline
	(c-calculate-state arg (and c-auto-newline c-electric-flag)))
  (if c-auto-newline (setq c-electric-flag t))
Richard M. Stallman's avatar
Richard M. Stallman committed
322 323 324
  (c-update-modeline)
  (c-keep-region-active))

325 326 327
(defalias 'c-toggle-auto-state 'c-toggle-auto-newline)
(make-obsolete 'c-toggle-auto-state 'c-toggle-auto-newline)

Gerd Moellmann's avatar
Gerd Moellmann committed
328
(defun c-toggle-hungry-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
329
  "Toggle hungry-delete-key feature.
Gerd Moellmann's avatar
Gerd Moellmann committed
330 331 332
Optional numeric ARG, if supplied, turns on hungry-delete when
positive, turns it off when negative, and just toggles it when zero or
left out.
Richard M. Stallman's avatar
Richard M. Stallman committed
333

334 335 336
When the hungry-delete-key feature is enabled (indicated by \"/h\" on
the modeline after the mode name) the delete key gobbles all preceding
whitespace in one fell swoop."
Richard M. Stallman's avatar
Richard M. Stallman committed
337 338 339 340 341
  (interactive "P")
  (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
  (c-update-modeline)
  (c-keep-region-active))

Gerd Moellmann's avatar
Gerd Moellmann committed
342
(defun c-toggle-auto-hungry-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
343
  "Toggle auto-newline and hungry-delete-key features.
Gerd Moellmann's avatar
Gerd Moellmann committed
344
Optional numeric ARG, if supplied, turns on auto-newline and
Richard M. Stallman's avatar
Richard M. Stallman committed
345
hungry-delete when positive, turns them off when negative, and just
Gerd Moellmann's avatar
Gerd Moellmann committed
346
toggles them when zero or left out.
Richard M. Stallman's avatar
Richard M. Stallman committed
347

348
See `c-toggle-auto-newline' and `c-toggle-hungry-state' for details."
Richard M. Stallman's avatar
Richard M. Stallman committed
349 350 351 352 353 354
  (interactive "P")
  (setq c-auto-newline (c-calculate-state arg c-auto-newline))
  (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
  (c-update-modeline)
  (c-keep-region-active))

355 356 357 358 359 360 361 362 363 364
(defun c-toggle-electric-state (&optional arg)
  "Toggle the electric indentation feature.
Optional numeric ARG, if supplied, turns on electric indentation when
positive, turns it off when negative, and just toggles it when zero or
left out."
  (interactive "P")
  (setq c-electric-flag (c-calculate-state arg c-electric-flag))
  (c-update-modeline)
  (c-keep-region-active))

Richard M. Stallman's avatar
Richard M. Stallman committed
365 366 367 368

;; Electric keys

(defun c-electric-backspace (arg)
369
  "Delete the preceding character or whitespace.
370 371 372 373 374
If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
line) then all preceding whitespace is consumed.  If however a prefix
argument is supplied, or `c-hungry-delete-key' is nil, or point is
inside a literal then the function in the variable
`c-backspace-function' is called."
375
  (interactive "*P")
376 377 378 379
  (if (c-save-buffer-state ()
	(or (not c-hungry-delete-key)
	    arg
	    (c-in-literal)))
Richard M. Stallman's avatar
Richard M. Stallman committed
380
      (funcall c-backspace-function (prefix-numeric-value arg))
381
    (c-hungry-delete-backwards)))
382

383
(defun c-hungry-delete-backwards ()
384 385 386 387 388 389 390 391 392
  "Delete the preceding character or all preceding whitespace
back to the previous non-whitespace character.
See also \\[c-hungry-delete-forward]."
  (interactive)
  (let ((here (point)))
    (c-skip-ws-backward)
    (if (/= (point) here)
	(delete-region (point) here)
      (funcall c-backspace-function 1))))
Richard M. Stallman's avatar
Richard M. Stallman committed
393

394 395
(defalias 'c-hungry-backspace 'c-hungry-delete-backwards)

396
(defun c-electric-delete-forward (arg)
397
  "Delete the following character or whitespace.
398 399 400 401 402
If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
line) then all following whitespace is consumed.  If however a prefix
argument is supplied, or `c-hungry-delete-key' is nil, or point is
inside a literal then the function in the variable `c-delete-function'
is called."
403
  (interactive "*P")
404 405 406 407
  (if (c-save-buffer-state ()
	(or (not c-hungry-delete-key)
	    arg
	    (c-in-literal)))
408
      (funcall c-delete-function (prefix-numeric-value arg))
409 410 411 412 413
    (c-hungry-delete-forward)))

(defun c-hungry-delete-forward ()
  "Delete the following character or all following whitespace
up to the next non-whitespace character.
414
See also \\[c-hungry-delete-backwards]."
415 416 417 418 419 420
  (interactive)
  (let ((here (point)))
    (c-skip-ws-forward)
    (if (/= (point) here)
	(delete-region (point) here)
      (funcall c-delete-function 1))))
421

422
;; This function is only used in XEmacs.
Richard M. Stallman's avatar
Richard M. Stallman committed
423 424
(defun c-electric-delete (arg)
  "Deletes preceding or following character or whitespace.
425
This function either deletes forward as `c-electric-delete-forward' or
426 427 428 429 430 431 432
backward as `c-electric-backspace', depending on the configuration: If
the function `delete-forward-p' is defined and returns non-nil, it
deletes forward.  Otherwise it deletes backward.

Note: This is the way in XEmacs to choose the correct action for the
\[delete] key, whichever key that means.  Other flavors don't use this
function to control that."
433
  (interactive "*P")
434 435
  (if (and (fboundp 'delete-forward-p)
	   (delete-forward-p))
436
      (c-electric-delete-forward arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
437 438
    (c-electric-backspace arg)))

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
;; This function is only used in XEmacs.
(defun c-hungry-delete ()
  "Delete a non-whitespace char, or all whitespace up to the next non-whitespace char.
The direction of deletion depends on the configuration: If the
function `delete-forward-p' is defined and returns non-nil, it deletes
forward using `c-hungry-delete-forward'.  Otherwise it deletes
backward using `c-hungry-backspace'.

Note: This is the way in XEmacs to choose the correct action for the
\[delete] key, whichever key that means.  Other flavors don't use this
function to control that."
  (interactive)
  (if (and (fboundp 'delete-forward-p)
	   (delete-forward-p))
      (c-hungry-delete-forward)
454
    (c-hungry-delete-backwards)))
455

Richard M. Stallman's avatar
Richard M. Stallman committed
456
(defun c-electric-pound (arg)
457 458 459 460
  "Insert a \"#\".
If `c-electric-flag' is set, handle it specially according to the variable
`c-electric-pound-behavior'.  If a numeric ARG is supplied, or if point is
inside a literal or a macro, nothing special happens."
461
  (interactive "*P")
462 463 464 465 466 467 468 469 470 471 472 473
  (if (c-save-buffer-state ()
	(or arg
	    (not c-electric-flag)
	    (not (memq 'alignleft c-electric-pound-behavior))
	    (save-excursion
	      (skip-chars-backward " \t")
	      (not (bolp)))
	    (save-excursion
	      (and (= (forward-line -1) 0)
		   (progn (end-of-line)
			  (eq (char-before) ?\\))))
	    (c-in-literal)))
Richard M. Stallman's avatar
Richard M. Stallman committed
474 475 476 477 478 479 480
      ;; do nothing special
      (self-insert-command (prefix-numeric-value arg))
    ;; place the pound character at the left edge
    (let ((pos (- (point-max) (point)))
	  (bolp (bolp)))
      (beginning-of-line)
      (delete-horizontal-space)
481
      (insert last-command-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
482 483 484 485
      (and (not bolp)
	   (goto-char (- (point-max) pos)))
      )))

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
(defun c-point-syntax ()
  ;; Return the syntactic context of the construct at point.  (This is NOT
  ;; nec. the same as the s.c. of the line point is on).  N.B. This won't work
  ;; between the `#' of a cpp thing and what follows (see c-opt-cpp-prefix).
  (c-save-buffer-state (;; shut this up too
	(c-echo-syntactic-information-p nil)
	syntax)
    (c-tentative-buffer-changes
      ;; insert a newline to isolate the construct at point for syntactic
      ;; analysis.
      (insert-char ?\n 1)
      ;; In AWK (etc.) or in a macro, make sure this CR hasn't changed
      ;; the syntax.  (There might already be an escaped NL there.)
      (when (or (c-at-vsemi-p (1- (point)))
		(let ((pt (point)))
		  (save-excursion
		    (backward-char)
		    (and (c-beginning-of-macro)
			 (progn (c-end-of-macro)
				(< (point) pt))))))
	(backward-char)
	(insert-char ?\\ 1)
	(forward-char))
      (let ((c-syntactic-indentation-in-macros t)
	    (c-auto-newline-analysis t))
	;; Turn on syntactic macro analysis to help with auto
	;; newlines only.
	(setq syntax (c-guess-basic-syntax))
	nil))
    syntax))

(defun c-brace-newlines (syntax)
  ;; A brace stands at point.  SYNTAX is the syntactic context of this brace
  ;; (not necessarily the same as the S.C. of the line it is on).  Return
  ;; NEWLINES, the list containing some combination of the symbols `before'
  ;; and `after' saying where newlines should be inserted.
  (c-save-buffer-state
      ((syms
	;; This is the list of brace syntactic symbols that can hang.
	;; If any new ones are added to c-offsets-alist, they should be
	;; added here as well.
	'(class-open class-close defun-open defun-close
		     inline-open inline-close
		     brace-list-open brace-list-close
		     brace-list-intro brace-entry-open
		     block-open block-close
		     substatement-open statement-case-open
		     extern-lang-open extern-lang-close
		     namespace-open namespace-close
		     module-open module-close
		     composition-open composition-close
		     inexpr-class-open inexpr-class-close
		     ;; `statement-cont' is here for the case with a brace
		     ;; list opener inside a statement.  C.f. CASE B.2 in
		     ;; `c-guess-continued-construct'.
		     statement-cont))
       ;; shut this up too
       (c-echo-syntactic-information-p nil)
       symb-newlines)		     ; e.g. (substatement-open . (after))
545

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    (setq symb-newlines
	  ;; Do not try to insert newlines around a special
	  ;; (Pike-style) brace list.
	  (if (and c-special-brace-lists
		   (save-excursion
		     (c-safe (if (= (char-before) ?{)
				 (forward-char -1)
			       (c-forward-sexp -1))
			     (c-looking-at-special-brace-list))))
	      nil
	    ;; Seek the matching entry in c-hanging-braces-alist.
	    (or (c-lookup-lists
		 syms
		 ;; Substitute inexpr-class and class-open or
		 ;; class-close with inexpr-class-open or
		 ;; inexpr-class-close.
		 (if (assq 'inexpr-class syntax)
		     (cond ((assq 'class-open syntax)
			    '((inexpr-class-open)))
			   ((assq 'class-close syntax)
			    '((inexpr-class-close)))
			   (t syntax))
		   syntax)
		 c-hanging-braces-alist)
		'(ignore before after)))) ; Default, when not in c-h-b-l.

    ;; If syntax is a function symbol, then call it using the
    ;; defined semantics.
    (if (and (not (consp (cdr symb-newlines)))
	     (functionp (cdr symb-newlines)))
	(let ((c-syntactic-context syntax))
	  (funcall (cdr symb-newlines)
		   (car symb-newlines)
		   (point)))
      (cdr symb-newlines))))

(defun c-try-one-liner ()
  ;; Point is just after a newly inserted }.  If the non-whitespace
  ;; content of the braces is a single line of code, compact the whole
  ;; construct to a single line, if this line isn't too long.  The Right
  ;; Thing is done with comments.
  ;;
  ;; Point will be left after the }, regardless of whether the clean-up is
  ;; done.  Return NON-NIL if the clean-up happened, NIL if it didn't.

  (let ((here (point))
	(pos (- (point-max) (point)))
	mbeg1 mend1 mbeg4 mend4
	eol-col cmnt-pos cmnt-col cmnt-gap)

    (when
	(save-excursion
	  (save-restriction
	    ;; Avoid backtracking over a very large block.  The one we
	    ;; deal with here can never be more than three lines.
	    (narrow-to-region (save-excursion
				(forward-line -2)
				(point))
			      (point))
	    (and (c-safe (c-backward-sexp))
		 (progn
		   (forward-char)
		   (narrow-to-region (point) (1- here)) ; innards of {.}
		   (looking-at
		    (cc-eval-when-compile
		      (concat
		       "\\("		; (match-beginning 1)
		       "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
		       "\\)"		; (match-end 1)
		       "[^ \t\r\n]+\\([ \t]+[^ \t\r\n]+\\)*" ; non-WS
		       "\\("		; (match-beginning 4)
		       "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
		       "\\)\\'")))))))	; (match-end 4) at EOB.

      (if (c-tentative-buffer-changes
	    (setq mbeg1 (match-beginning 1) mend1 (match-end 1)
		  mbeg4 (match-beginning 4) mend4 (match-end 4))
	    (backward-char)		; back over the `}'
	    (save-excursion
	      (setq cmnt-pos (and (c-backward-single-comment)
				  (- (point) (- mend1 mbeg1)))))
	    (delete-region mbeg4 mend4)
	    (delete-region mbeg1 mend1)
	    (setq eol-col (save-excursion (end-of-line) (current-column)))

	    ;; Necessary to put the closing brace before any line
	    ;; oriented comment to keep it syntactically significant.
	    ;; This isn't necessary for block comments, but the result
	    ;; looks nicer anyway.
	    (when cmnt-pos
	      (delete-char 1)		; the `}' has blundered into a comment
	      (goto-char cmnt-pos)
	      (setq cmnt-col (1+ (current-column)))
	      (setq cmnt-pos (1+ cmnt-pos)) ; we're inserting a `}'
	      (c-skip-ws-backward)
	      (insert-char ?\} 1)	; reinsert the `}' before the comment.
	      (setq cmnt-gap (- cmnt-col (current-column)))
	      (when (zerop cmnt-gap)
		(insert-char ?\  1)	; Put a space before a bare comment.
		(setq cmnt-gap 1)))

	    (or (null c-max-one-liner-length)
		(zerop c-max-one-liner-length)
		(<= eol-col c-max-one-liner-length)
		;; Can we trim space before comment to make the line fit?
		(and cmnt-gap
		     (< (- eol-col cmnt-gap) c-max-one-liner-length)
		     (progn (goto-char cmnt-pos)
			    (backward-delete-char-untabify
			     (- eol-col c-max-one-liner-length))
			    t))))
	  (goto-char (- (point-max) pos))))))

Richard M. Stallman's avatar
Richard M. Stallman committed
659 660 661
(defun c-electric-brace (arg)
  "Insert a brace.

662 663 664
If `c-electric-flag' is non-nil, the brace is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:
Richard M. Stallman's avatar
Richard M. Stallman committed
665

666
\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
667 668 669 670 671 672 673 674
the mode line) newlines are inserted before and after the brace as
directed by the settings in `c-hanging-braces-alist'.

\(b) Any auto-newlines are indented.  The original line is also
reindented unless `c-syntactic-indentation' is nil.

\(c) If auto-newline is turned on, various newline cleanups based on the
settings of `c-cleanup-list' are done."
Barry A. Warsaw's avatar
Barry A. Warsaw committed
675

676
  (interactive "*P")
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
  (let (safepos literal
	;; We want to inhibit blinking the paren since this would be
	;; most disruptive.  We'll blink it ourselves later on.
	(old-blink-paren blink-paren-function)
	blink-paren-function)

    (c-save-buffer-state ()
      (setq safepos (c-safe-position (point) (c-parse-state))
	    literal (c-in-literal safepos)))

    ;; Insert the brace.  Note that expand-abbrev might reindent
    ;; the line here if there's a preceding "else" or something.
    (self-insert-command (prefix-numeric-value arg))

    (when (and c-electric-flag (not literal) (not arg))
      (if (not (looking-at "[ \t]*\\\\?$"))
	  (if c-syntactic-indentation
	      (indent-according-to-mode))

	(let ( ;; shut this up too
	      (c-echo-syntactic-information-p nil)
	      newlines
	      ln-syntax br-syntax syntax) ; Syntactic context of the original line,
			; of the brace itself, of the line the brace ends up on.
	  (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
				(c-auto-newline-analysis t))
	    (setq ln-syntax (c-guess-basic-syntax)))
	  (if c-syntactic-indentation
	      (c-indent-line ln-syntax))

	  (when c-auto-newline
	    (backward-char)
	    (setq br-syntax (c-point-syntax)
		  newlines (c-brace-newlines br-syntax))

	    ;; Insert the BEFORE newline, if wanted, and reindent the newline.
	    (if (and (memq 'before newlines)
		     (> (current-column) (current-indentation)))
		(if c-syntactic-indentation
		    ;; Only a plain newline for now - it's indented
		    ;; after the cleanups when the line has its final
		    ;; appearance.
		    (newline)
		  (c-newline-and-indent)))
	    (forward-char)

	    ;; `syntax' is the syntactic context of the line which ends up
	    ;; with the brace on it.
	    (setq syntax (if (memq 'before newlines) br-syntax ln-syntax))

	    ;; Do all appropriate clean ups
	    (let ((here (point))
		  (pos (- (point-max) (point)))
		  mbeg mend
		  )

	      ;; `}': clean up empty defun braces
	      (when (c-save-buffer-state ()
		      (and (memq 'empty-defun-braces c-cleanup-list)
			   (eq last-command-char ?\})
			   (c-intersect-lists '(defun-close class-close inline-close)
					      syntax)
			   (progn
			     (forward-char -1)
			     (c-skip-ws-backward)
			     (eq (char-before) ?\{))
			   ;; make sure matching open brace isn't in a comment
			   (not (c-in-literal))))
		(delete-region (point) (1- here))
		(setq here (- (point-max) pos)))
	      (goto-char here)

	      ;; `}': compact to a one-liner defun?
	      (save-match-data
		(when
		    (and (eq last-command-char ?\})
			 (memq 'one-liner-defun c-cleanup-list)
			 (c-intersect-lists '(defun-close) syntax)
			 (c-try-one-liner))
		  (setq here (- (point-max) pos))))

	      ;; `{': clean up brace-else-brace and brace-elseif-brace
	      (when (eq last-command-char ?\{)
		(cond
		 ((and (memq 'brace-else-brace c-cleanup-list)
		       (re-search-backward
			(concat "}"
				"\\([ \t\n]\\|\\\\\n\\)*"
				"else"
				"\\([ \t\n]\\|\\\\\n\\)*"
				"{"
				"\\=")
			nil t))
770
		  (delete-region (match-beginning 0) (match-end 0))
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
		  (insert-and-inherit "} else {"))
		 ((and (memq 'brace-elseif-brace c-cleanup-list)
		       (progn
			 (goto-char (1- here))
			 (setq mend (point))
			 (c-skip-ws-backward)
			 (setq mbeg (point))
			 (eq (char-before) ?\)))
		       (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
		       (eq (char-after) ?\()
		      ; (progn
			; (setq tmp (point))
			 (re-search-backward
			  (concat "}"
				  "\\([ \t\n]\\|\\\\\n\\)*"
				  "else"
				  "\\([ \t\n]\\|\\\\\n\\)+"
				  "if"
				  "\\([ \t\n]\\|\\\\\n\\)*"
				  "\\=")
			  nil t);)
		       ;(eq (match-end 0) tmp);
			 )
		  (delete-region mbeg mend)
		  (goto-char mbeg)
		  (insert ?\ ))))

	      (goto-char (- (point-max) pos))

	      ;; Indent the line after the cleanups since it might
	      ;; very well indent differently due to them, e.g. if
	      ;; c-indent-one-line-block is used together with the
	      ;; one-liner-defun cleanup.
	      (when c-syntactic-indentation
		(c-indent-line)))

	    ;; does a newline go after the brace?
	    (if (memq 'after newlines)
		(c-newline-and-indent))
	    ))))

812 813 814 815 816
    ;; blink the paren
    (and (eq last-command-char ?\})
	 (not executing-kbd-macro)
	 old-blink-paren
	 (save-excursion
817 818
	   (c-save-buffer-state nil
	     (c-backward-syntactic-ws safepos))
819
	   (funcall old-blink-paren)))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
820

Richard M. Stallman's avatar
Richard M. Stallman committed
821 822
(defun c-electric-slash (arg)
  "Insert a slash character.
823

824 825 826 827
If the slash is inserted immediately after the comment prefix in a c-style
comment, the comment might get closed by removing whitespace and possibly
inserting a \"*\".  See the variable `c-cleanup-list'.

828 829
Indent the line as a comment, if:

830
  1. The slash is second of a \"//\" line oriented comment introducing
831 832
     token and we are on a comment-only-line, or

833
  2. The slash is part of a \"*/\" token that closes a block oriented
834 835
     comment.

836
If a numeric ARG is supplied, point is inside a literal, or
837 838
`c-syntactic-indentation' is nil or `c-electric-flag' is nil, indentation
is inhibited."
839
  (interactive "*P")
840 841 842 843 844 845 846 847 848 849 850
  (let ((literal (c-save-buffer-state () (c-in-literal)))
	indentp
	;; shut this up
	(c-echo-syntactic-information-p nil))

    ;; comment-close-slash cleanup?  This DOESN'T need `c-electric-flag' or
    ;; `c-syntactic-indentation' set.
    (when (and (not arg)
	       (eq literal 'c)
	       (memq 'comment-close-slash c-cleanup-list)
	       (eq last-command-char ?/)
851 852
	       (looking-at (concat "[ \t]*\\("
				   (regexp-quote comment-end) "\\)?$"))
853 854
	; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */
	       (save-excursion
855 856 857 858
		 (save-restriction
		   (narrow-to-region (point-min) (point))
		   (back-to-indentation)
		   (looking-at (concat c-current-comment-prefix "[ \t]*$")))))
859
      (delete-region (progn (forward-line 0) (point))
860 861
		   (progn (end-of-line) (point)))
      (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here?
862 863 864 865

    (setq indentp (and (not arg)
		       c-syntactic-indentation
		       c-electric-flag
866
		       (eq last-command-char ?/)
867
		       (eq (char-before) (if literal ?* ?/))))
Richard M. Stallman's avatar
Richard M. Stallman committed
868 869
    (self-insert-command (prefix-numeric-value arg))
    (if indentp
Gerd Moellmann's avatar
Gerd Moellmann committed
870
	(indent-according-to-mode))))
Richard M. Stallman's avatar
Richard M. Stallman committed
871 872 873

(defun c-electric-star (arg)
  "Insert a star character.
874 875 876 877 878 879
If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, and
the star is the second character of a C style comment starter on a
comment-only-line, indent the line as a comment.  If a numeric ARG is
supplied, point is inside a literal, or `c-syntactic-indentation' is nil,
this indentation is inhibited."

880
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
881
  (self-insert-command (prefix-numeric-value arg))
882
  ;; if we are in a literal, or if arg is given do not reindent the
Richard M. Stallman's avatar
Richard M. Stallman committed
883
  ;; current line, unless this star introduces a comment-only line.
884 885 886 887 888 889 890 891 892 893 894 895 896
  (if (c-save-buffer-state ()
	(and c-syntactic-indentation
	     c-electric-flag
	     (not arg)
	     (eq (c-in-literal) 'c)
	     (eq (char-before) ?*)
	     (save-excursion
	       (forward-char -1)
	       (skip-chars-backward "*")
	       (if (eq (char-before) ?/)
		   (forward-char -1))
	       (skip-chars-backward " \t")
	       (bolp))))
Gerd Moellmann's avatar
Gerd Moellmann committed
897 898
      (let (c-echo-syntactic-information-p) ; shut this up
	(indent-according-to-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
899 900 901 902 903
    ))

(defun c-electric-semi&comma (arg)
  "Insert a comma or semicolon.

904 905 906 907
If `c-electric-flag' is non-nil, point isn't inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:

908
\(a) When the auto-newline feature is turned on (indicated by \"/la\" on
909 910
the mode line) a newline might be inserted.  See the variable
`c-hanging-semi&comma-criteria' for how newline insertion is determined.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
911

912 913 914 915 916 917
\(b) Any auto-newlines are indented.  The original line is also
reindented unless `c-syntactic-indentation' is nil.

\(c) If auto-newline is turned on, a comma following a brace list or a
semicolon following a defun might be cleaned up, depending on the
settings of `c-cleanup-list'."
918
  (interactive "*P")
919
  (let* (lim literal c-syntactic-context
Richard M. Stallman's avatar
Richard M. Stallman committed
920 921 922
	 (here (point))
	 ;; shut this up
	 (c-echo-syntactic-information-p nil))
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

    (c-save-buffer-state ()
      (setq lim (c-most-enclosing-brace (c-parse-state))
	    literal (c-in-literal lim)))

    (self-insert-command (prefix-numeric-value arg))

    (if (and c-electric-flag (not literal) (not arg))
	;; do all cleanups and newline insertions if c-auto-newline is on.
	(if (or (not c-auto-newline)
		(not (looking-at "[ \t]*\\\\?$")))
	    (if c-syntactic-indentation
		(c-indent-line))
	  ;; clean ups: list-close-comma or defun-close-semi
	  (let ((pos (- (point-max) (point))))
	    (if (c-save-buffer-state ()
		  (and (or (and
			    (eq last-command-char ?,)
			    (memq 'list-close-comma c-cleanup-list))
			   (and
			    (eq last-command-char ?\;)
			    (memq 'defun-close-semi c-cleanup-list)))
		       (progn
			 (forward-char -1)
			 (c-skip-ws-backward)
			 (eq (char-before) ?}))
		       ;; make sure matching open brace isn't in a comment
		       (not (c-in-literal lim))))
		(delete-region (point) here))
	    (goto-char (- (point-max) pos)))
	  ;; reindent line
	  (when c-syntactic-indentation
	    (setq c-syntactic-context (c-guess-basic-syntax))
	    (c-indent-line c-syntactic-context))
	  ;; check to see if a newline should be added
	  (let ((criteria c-hanging-semi&comma-criteria)
		answer add-newline-p)
	    (while criteria
	      (setq answer (funcall (car criteria)))
	      ;; only nil value means continue checking
	      (if (not answer)
		  (setq criteria (cdr criteria))
		(setq criteria nil)
		;; only 'stop specifically says do not add a newline
		(setq add-newline-p (not (eq answer 'stop)))
		))
	    (if add-newline-p
		(c-newline-and-indent))
	    )))))
Richard M. Stallman's avatar
Richard M. Stallman committed
972 973 974 975

(defun c-electric-colon (arg)
  "Insert a colon.

976 977 978 979
If `c-electric-flag' is non-nil, the colon is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:

980
\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
981 982
the mode line) newlines are inserted before and after the colon based on
the settings in `c-hanging-colons-alist'.
Richard M. Stallman's avatar
Richard M. Stallman committed
983

984 985 986 987 988 989
\(b) Any auto-newlines are indented.  The original line is also
reindented unless `c-syntactic-indentation' is nil.

\(c) If auto-newline is turned on, whitespace between two colons will be
\"cleaned up\" leaving a scope operator, if this action is set in
`c-cleanup-list'."
Richard M. Stallman's avatar
Richard M. Stallman committed
990

991
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
992
  (let* ((bod (c-point 'bod))
993
	 (literal (c-save-buffer-state () (c-in-literal bod)))
994
	 newlines is-scope-op
Richard M. Stallman's avatar
Richard M. Stallman committed
995 996
	 ;; shut this up
	 (c-echo-syntactic-information-p nil))
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    (self-insert-command (prefix-numeric-value arg))
    ;; Any electric action?
    (if (and c-electric-flag (not literal) (not arg))
	;; Unless we're at EOL, only re-indentation happens.
	(if (not (looking-at "[ \t]*\\\\?$"))
	    (if c-syntactic-indentation
		(indent-according-to-mode))

	  ;; scope-operator clean-up?
	  (let ((pos (- (point-max) (point)))
		(here (point)))
	    (if (c-save-buffer-state ()	; Why do we need this? [ACM, 2003-03-12]
		  (and c-auto-newline
		       (memq 'scope-operator c-cleanup-list)
		       (eq (char-before) ?:)
		       (progn
			 (forward-char -1)
			 (c-skip-ws-backward)
			 (eq (char-before) ?:))
		       (not (c-in-literal))
		       (not (eq (char-after (- (point) 2)) ?:))))
		(progn
		  (delete-region (point) (1- here))
		  (setq is-scope-op t)))
	    (goto-char (- (point-max) pos)))

	  ;; indent the current line if it's done syntactically.
	  (if c-syntactic-indentation
	      ;; Cannot use the same syntax analysis as we find below,
	      ;; since that's made with c-syntactic-indentation-in-macros
	      ;; always set to t.
	      (indent-according-to-mode))

	  ;; Calculate where, if anywhere, we want newlines.
	  (c-save-buffer-state
	      ((c-syntactic-indentation-in-macros t)
	       (c-auto-newline-analysis t)
	       ;; Turn on syntactic macro analysis to help with auto newlines
	       ;; only.
	       (syntax (c-guess-basic-syntax))
	       (elem syntax))
	    ;; Translate substatement-label to label for this operation.
	    (while elem
	      (if (eq (car (car elem)) 'substatement-label)
		  (setcar (car elem) 'label))
	      (setq elem (cdr elem)))
	    ;; some language elements can only be determined by checking
	    ;; the following line.  Lets first look for ones that can be
	    ;; found when looking on the line with the colon
	    (setq newlines
		  (and c-auto-newline
		       (or (c-lookup-lists '(case-label label access-label)
					   syntax c-hanging-colons-alist)
			   (c-lookup-lists '(member-init-intro inher-intro)
					   (progn
					     (insert ?\n)
					     (unwind-protect
						 (c-guess-basic-syntax)
					       (delete-char -1)))
					   c-hanging-colons-alist)))))
	  ;; does a newline go before the colon?  Watch out for already
	  ;; non-hung colons.  However, we don't unhang them because that
	  ;; would be a cleanup (and anti-social).
	  (if (and (memq 'before newlines)
		   (not is-scope-op)
		   (save-excursion
		     (skip-chars-backward ": \t")
		     (not (bolp))))
	      (let ((pos (- (point-max) (point))))
		(forward-char -1)
		(c-newline-and-indent)
		(goto-char (- (point-max) pos))))
	  ;; does a newline go after the colon?
	  (if (and (memq 'after (cdr-safe newlines))
		   (not is-scope-op))
	      (c-newline-and-indent))
	  ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1074 1075

(defun c-electric-lt-gt (arg)
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  "Insert a \"<\" or \">\" character.
If the current language uses angle bracket parens (e.g. template
arguments in C++), try to find out if the inserted character is a
paren and give it paren syntax if appropriate.

If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, the
line will be reindented if the inserted character is a paren or if it
finishes a C++ style stream operator in C++ mode.  Exceptions are when a
numeric argument is supplied, or the point is inside a literal."

1086
  (interactive "*P")
1087 1088 1089
  (let ((c-echo-syntactic-information-p nil)
	final-pos close-paren-inserted)

Richard M. Stallman's avatar
Richard M. Stallman committed
1090
    (self-insert-command (prefix-numeric-value arg))
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    (setq final-pos (point))

    (c-save-buffer-state (c-parse-and-markup-<>-arglists
			  c-restricted-<>-arglists
			  <-pos)

      (when c-recognize-<>-arglists
	(if (eq last-command-char ?<)
	    (when (and (progn
			 (backward-char)
			 (= (point)
			    (progn
			      (c-beginning-of-current-token)
			      (point))))
		       (progn
			 (c-backward-token-2)
			 (looking-at c-opt-<>-sexp-key)))
	      (c-mark-<-as-paren (1- final-pos)))

	  ;; It's a ">".  Check if there's an earlier "<" which either has
	  ;; open paren syntax already or that can be recognized as an arglist
	  ;; together with this ">".  Note that this won't work in cases like
	  ;; "template <x, a < b, y>" but they ought to be rare.

	  (save-restriction
	    ;; Narrow to avoid that `c-forward-<>-arglist' below searches past
	    ;; our position.
	    (narrow-to-region (point-min) final-pos)

	    (while (and
		    (progn
		      (goto-char final-pos)
		      (c-syntactic-skip-backward "^<;}" nil t)
		      (eq (char-before) ?<))
		    (progn
		      (backward-char)
		      ;; If the "<" already got open paren syntax we know we
		      ;; have the matching closer.  Handle it and exit the
		      ;; loop.
		      (if (looking-at "\\s\(")
			  (progn
			    (c-mark->-as-paren (1- final-pos))
			    (setq close-paren-inserted t)
			    nil)
			t))

		    (progn
		      (setq <-pos (point))
		      (c-backward-syntactic-ws)
		      (c-simple-skip-symbol-backward))
		    (or (looking-at c-opt-<>-sexp-key)
			(not (looking-at c-keywords-regexp)))

		    (let ((c-parse-and-markup-<>-arglists t)
			  c-restricted-<>-arglists
			  (containing-sexp
			   (c-most-enclosing-brace (c-parse-state))))
		      (when (and containing-sexp
				 (progn (goto-char containing-sexp)
					(eq (char-after) ?\())
				 (not (eq (get-text-property (point) 'c-type)
					  'c-decl-arg-start)))
			(setq c-restricted-<>-arglists t))
		      (goto-char <-pos)
		      (c-forward-<>-arglist nil))

		    ;; Loop here if the "<" we found above belongs to a nested
		    ;; angle bracket sexp.  When we start over we'll find the
		    ;; previous or surrounding sexp.
		    (if (< (point) final-pos)
			t
		      (setq close-paren-inserted t)
		      nil)))))))
    (goto-char final-pos)

    ;; Indent the line if appropriate.
    (when (and c-electric-flag c-syntactic-indentation)
      (backward-char)
      (when (prog1 (or (looking-at "\\s\(\\|\\s\)")
		       (and (c-major-mode-is 'c++-mode)
			    (progn
			      (c-beginning-of-current-token)
			      (looking-at "<<\\|>>"))
			    (= (match-end 0) final-pos)))
	      (goto-char final-pos))
	(indent-according-to-mode)))

    (when (and close-paren-inserted
	       (not executing-kbd-macro)
	       blink-paren-function)
      ;; Note: Most paren blink functions, such as the standard
      ;; `blink-matching-open', currently doesn't handle paren chars
      ;; marked with text properties very well.  Maybe we should avoid
      ;; this call for the time being?
      (funcall blink-paren-function))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1186

Barry A. Warsaw's avatar
Barry A. Warsaw committed
1187 1188 1189
(defun c-electric-paren (arg)
  "Insert a parenthesis.

1190 1191 1192
If `c-syntactic-indentation' and `c-electric-flag' are both non-nil, the
line is reindented unless a numeric ARG is supplied, or the parenthesis
is inserted inside a literal.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1193

1194 1195 1196 1197 1198
Whitespace between a function name and the parenthesis may get added or
removed; see the variable `c-cleanup-list'.

Also, if `c-electric-flag' and `c-auto-newline' are both non-nil, some
newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1199
  (interactive "*P")
1200
  (let ((literal (c-save-buffer-state () (c-in-literal)))
1201
	;; shut this up
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1202
	(c-echo-syntactic-information-p nil))
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    (self-insert-command (prefix-numeric-value arg))

    (if (and (not arg) (not literal))
	(let* (	;; We want to inhibit blinking the paren since this will
	       ;; be most disruptive.  We'll blink it ourselves
	       ;; afterwards.
	       (old-blink-paren blink-paren-function)
	       blink-paren-function)
	  (if (and c-syntactic-indentation c-electric-flag)
	      (indent-according-to-mode))

	  ;; If we're at EOL, check for new-line clean-ups.
	  (when (and c-electric-flag c-auto-newline
		     (looking-at "[ \t]*\\\\?$"))

	    ;; clean up brace-elseif-brace
	    (when
		(and (memq 'brace-elseif-brace c-cleanup-list)
		     (eq last-command-char ?\()
		     (re-search-backward
		      (concat "}"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "else"
			      "\\([ \t\n]\\|\\\\\n\\)+"
			      "if"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "("
			      "\\=")
		      nil t)
		     (not  (c-save-buffer-state () (c-in-literal))))
	      (delete-region (match-beginning 0) (match-end 0))
	      (insert-and-inherit "} else if ("))

	    ;; clean up brace-catch-brace
	    (when
		(and (memq 'brace-catch-brace c-cleanup-list)
		     (eq last-command-char ?\()
		     (re-search-backward
		      (concat "}"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "catch"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "("
			      "\\=")
		      nil t)
		     (not  (c-save-buffer-state () (c-in-literal))))
	      (delete-region (match-beginning 0) (match-end 0))
	      (insert-and-inherit "} catch (")))

	  ;; Check for clean-ups at function calls.  These two DON'T need
	  ;; `c-electric-flag' or `c-syntactic-indentation' set.
	  ;; Point is currently just after the inserted paren.
	  (let (beg (end (1- (point))))
	    (cond

	     ;; space-before-funcall clean-up?
	     ((and (memq 'space-before-funcall c-cleanup-list)
		   (eq last-command-char ?\()
		   (save-excursion
		     (backward-char)
		     (skip-chars-backward " \t")
		     (setq beg (point))
1265 1266 1267 1268 1269
		     (and (c-save-buffer-state () (c-on-identifier))
                          ;; Don't add a space into #define FOO()....
                          (not (and (c-beginning-of-macro)
                                    (c-forward-over-cpp-define-id)
                                    (eq (point) beg))))))
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	      (save-excursion
		(delete-region beg end)
		(goto-char beg)
		(insert ?\ )))

	     ;; compact-empty-funcall clean-up?
		  ((c-save-buffer-state ()
		     (and (memq 'compact-empty-funcall c-cleanup-list)
			  (eq last-command-char ?\))
			  (save-excursion
			    (c-safe (backward-char 2))
			    (when (looking-at "()")
			      (setq end (point))
			      (skip-chars-backward " \t")
			      (setq beg (point))
			      (c-on-identifier)))))
		   (delete-region beg end))))
	  (and (eq last-input-event ?\))
	       (not executing-kbd-macro)
	       old-blink-paren
	       (funcall old-blink-paren))))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1291

Gerd Moellmann's avatar
Gerd Moellmann committed
1292 1293 1294 1295 1296 1297 1298 1299 1300
(defun c-electric-continued-statement ()
  "Reindent the current line if appropriate.

This function is used to reindent the line after a keyword which
continues an earlier statement is typed, e.g. an \"else\" or the
\"while\" in a do-while block.

The line is reindented if there is nothing but whitespace before the
keyword on the line, the keyword is not inserted inside a literal, and
1301
`c-electric-flag' and `c-syntactic-indentation' are both non-nil."
Gerd Moellmann's avatar
Gerd Moellmann committed
1302 1303
  (let (;; shut this up
	(c-echo-syntactic-information-p nil))
1304 1305 1306 1307 1308 1309 1310 1311 1312
    (when (c-save-buffer-state ()
	    (and c-electric-flag
		 c-syntactic-indentation
		 (not (eq last-command-char ?_))
		 (= (save-excursion
		      (skip-syntax-backward "w")
		      (point))
		    (c-point 'boi))
		 (not (c-in-literal (c-point 'bod)))))
1313 1314 1315 1316 1317 1318 1319 1320 1321
      ;; Have to temporarily insert a space so that
      ;; c-guess-basic-syntax recognizes the keyword.  Follow the
      ;; space with a nonspace to avoid messing up any whitespace
      ;; sensitive meddling that might be done, e.g. by
      ;; `c-backslash-region'.
      (insert-and-inherit " x")
      (unwind-protect
	  (indent-according-to-mode)
	(delete-char -2)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1322 1323


1324
;; "nomenclature" functions + c-scope-operator.
Richard M. Stallman's avatar
Richard M. Stallman committed
1325
(defun c-forward-into-nomenclature (&optional arg)
1326
  "Compatibility alias for `c-forward-subword'."
Richard M. Stallman's avatar
Richard M. Stallman committed
1327
  (interactive "p")
1328 1329 1330
  (require 'cc-subword)
  (c-forward-subword arg))
(make-obsolete 'c-forward-into-nomenclature 'c-forward-subword)
Richard M. Stallman's avatar
Richard M. Stallman committed
1331 1332

(defun c-backward-into-nomenclature (&optional arg)
1333
  "Compatibility alias for `c-backward-subword'."
Richard M. Stallman's avatar
Richard M. Stallman committed
1334
  (interactive "p")
1335 1336 1337
  (require 'cc-subword)
  (c-backward-subword arg))
(make-obsolete 'c-backward-into-nomenclature 'c-backward-subword)
Richard M. Stallman's avatar
Richard M. Stallman committed
1338 1339 1340 1341

(defun c-scope-operator ()
  "Insert a double colon scope operator at point.
No indentation or other \"electric\" behavior is performed."
1342
  (interactive "*")
1343
  (insert-and-inherit "::"))
Richard M. Stallman's avatar
Richard M. Stallman committed
1344

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362

;; Movement (etc.) by defuns.
(defun c-in-function-trailer-p (&optional lim)
  ;; Return non-nil if point is between the closing brace and the semicolon of
  ;; a brace construct which needs a semicolon, e.g. within the "variables"
  ;; portion of a declaration like "struct foo {...} bar ;".
  ;;
  ;; Return the position of the main declaration.  Otherwise, return nil.
  ;; Point is assumed to be at the top level and outside of any macro or
  ;; literal.
  ;;
  ;; If LIM is non-nil, it is the bound on a the backward search for the
  ;; beginning of the declaration.
  ;;
  ;; This function might do hidden buffer changes.
  (and c-opt-block-decls-with-vars-key
       (save-excursion
	 (c-syntactic-skip-backward "^;}" lim)
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	 (let ((eo-block (point))
	       bod)
	   (and (eq (char-before) ?\})
		(eq (car (c-beginning-of-decl-1 lim)) 'previous)
		(setq bod (point))
		;; Look for struct or union or ...  If we find one, it might
		;; be the return type of a function, or the like.  Exclude
		;; this case.
		(c-syntactic-re-search-forward
		 (concat "[;=\(\[{]\\|\\("
			 c-opt-block-decls-with-vars-key
			 "\\)")
		 eo-block t t t)
		(match-beginning 1)	; Is there a "struct" etc., somewhere?
		(not (eq (char-before) ?_))
		(c-syntactic-re-search-forward "[;=\(\[{]" eo-block t t t)
		(eq (char-before) ?\{)
		bod)))))
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

(defun c-where-wrt-brace-construct ()
  ;; Determine where we are with respect to functions (or other brace
  ;; constructs, included in the term "function" in the rest of this comment).
  ;; Point is assumed to be outside any macro or literal.
  ;; This is used by c-\(begining\|end\)-of-defun.
  ;;
  ;; Return one of these symbols:
  ;; at-header       : we're at the start of a function's header.
  ;; in-header       : we're inside a function's header, this extending right
  ;;                   up to the brace.  This bit includes any k&r declarations.
  ;; in-block        : we're inside a function's brace block.
  ;; in-trailer      : we're in the area between the "}" and ";" of something
  ;;                  like "struct foo {...} bar, baz;".
  ;; at-function-end : we're just after the closing brace (or semicolon) that
  ;;                   terminates the function.
  ;; outwith-function: we're not at or in any function.  Being inside a
  ;;                   non-brace construct also counts as 'outwith-function'.
  ;;
  ;; This function might do hidden buffer changes.
  (save-excursion
1402
    (let* (kluge-start
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
	   decl-result brace-decl-p
	   (start (point))
	   (paren-state (c-parse-state))
	   (least-enclosing (c-least-enclosing-brace paren-state)))

      (cond
       ((and least-enclosing
	     (eq (char-after least-enclosing) ?\{))
	'in-block)
       ((c-in-function-trailer-p)
	'in-trailer)
       ((and (not least-enclosing)
	     (consp paren-state)
	     (consp (car paren-state))
	     (eq start (cdar paren-state)))
	'at-function-end)
       (t
	;; Find the start of the current declaration.  NOTE: If we're in the
	;; variables after a "struct/eval" type block, we don't get to the
	;; real declaration here - we detect and correct for this later.

	;;If we're in the parameters' parens, move back out of them.
	(if least-enclosing (goto-char least-enclosing))
	;; Kluge so that c-beginning-of-decl-1 won't go back if we're already
	;; at a declaration.
	(if (or (and (eolp) (not (eobp))) ; EOL is matched by "\\s>"
		(not (looking-at
"\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")))
	    (forward-char))
	(setq kluge-start (point))
	(setq decl-result
	      (car (c-beginning-of-decl-1
1435 1436
		    ;; NOTE: If we're in a K&R region, this might be the start
		    ;; of a parameter declaration, not the actual function.
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
		    (and least-enclosing ; LIMIT for c-b-of-decl-1
			 (c-safe-position least-enclosing paren-state)))))

	;; Has the declaration we've gone back to got braces?
	(setq brace-decl-p
	      (save-excursion
		    (and (c-syntactic-re-search-forward "[;{]" nil t t)
			 (or (eq (char-before) ?\{)
			     (and c-recognize-knr-p
				  ;; Might have stopped on the
				  ;; ';' in a K&R argdecl.  In
				  ;; that case the declaration
				  ;; should contain a block.
1450
				  (c-in-knr-argdecl))))))
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493

	(cond
	 ((= (point) kluge-start)	; might be BOB or unbalanced parens.
	  'outwith-function)
	 ((eq decl-result 'same)
	  (if brace-decl-p
	      (if (eq (point) start)
		  'at-header
		'in-header)
	    'outwith-function))
	 ((eq decl-result 'previous)
	  (if (and (not brace-decl-p)
		   (c-in-function-trailer-p))
	      'at-function-end
	    'outwith-function))
	 (t (error
	     "c-where-wrt-brace-construct: c-beginning-of-decl-1 returned %s"
	     decl-result))))))))

(defun c-backward-to-nth-BOF-{ (n where)
  ;; Skip to the opening brace of the Nth function before point.  If
  ;; point is inside a function, this counts as the first.  Point must be
  ;; outside any comment/string or macro.
  ;;
  ;; N must be strictly positive.
  ;; WHERE describes the position of point, one of the symbols `at-header',
  ;; `in-header', `in-block', `in-trailer', `at-function-end',
  ;; `outwith-function' as returned by c-where-wrt-brace-construct.
  ;;
  ;; If we run out of functions, leave point at BOB.  Return zero on success,
  ;; otherwise the number of {s still to go.
  ;;
  ;; This function may do hidden buffer changes
  (cond
   ;; What we do to go back the first defun depends on where we start.
   ((bobp))
   ((eq where 'in-block)
    (goto-char (c-least-enclosing-brace (c-parse-state)))
    (setq n (1- n)))
   ((eq where 'in-header)
    (c-syntactic-re-search-forward "{")
    (backward-char)
    (setq n (1- n)))
1494
   ((memq where '(at-header outwith-function at-function-end in-trailer))
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
    (c-syntactic-skip-backward "^}")
    (when (eq (char-before) ?\})
      (backward-sexp)
      (setq n (1- n))))
   (t (error "Unknown `where' %s in c-backward-to-nth-EOF-{" where)))

   ;; Each time round the loop, go back to a "{" at the outermost level.
  (while (and (> n 0) (not (bobp)))
    (c-parse-state)		       ; This call speeds up the following one
					; by a factor of ~6.  Hmmm.  2006/4/5.
    (c-syntactic-skip-backward "^}")
    (when (eq (char-before) ?\})
      (backward-sexp)
      (setq n (1- n))))
   n)

1511 1512
(defun c-beginning-of-defun (&optional arg)
  "Move backward to the beginning of a defun.
1513 1514 1515 1516 1517 1518
Every top level declaration that contains a brace paren block is
considered to be a defun.

With a positive argument, move backward that many defuns.  A negative
argument -N means move forward to the Nth following beginning.  Return
t unless search stops due to beginning or end of buffer.
1519 1520 1521 1522

Unlike the built-in `beginning-of-defun' this tries to be smarter
about finding the char with open-parenthesis syntax that starts the
defun."
1523

1524
  (interactive "p")
1525 1526
  (or arg (setq arg 1))

1527
  (c-save-buffer-state
1528 1529
      (beginning-of-defun-function end-of-defun-function
       (start (point))
1530
       where paren-state pos)
1531

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
    ;; Move back out of any macro/comment/string we happen to be in.
    (c-beginning-of-macro)
    (setq pos (c-literal-limits))
    (if pos (goto-char (car pos)))

    (setq where (c-where-wrt-brace-construct))

    (if (< arg 0)
	;; Move forward to the closing brace of a function.
	(progn
1542
	  (if (memq where '(at-function-end outwith-function))
1543 1544 1545 1546 1547
	      (setq arg (1+ arg)))
	  (if (< arg 0)
	      (setq arg (c-forward-to-nth-EOF-} (- arg) where)))
	  ;; Move forward to the next opening brace....
	  (when (and (= arg 0)
1548
		     (c-syntactic-re-search-forward "{" nil 'eob))
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
	    (backward-char)
	    ;; ... and backward to the function header.
	    (c-beginning-of-decl-1)
	    t))

      ;; Move backward to the opening brace of a function.
      (when (and (> arg 0)
		 (eq (setq arg (c-backward-to-nth-BOF-{ arg where)) 0))

	;; Go backward to this function's header.
	(c-beginning-of-decl-1)

	(setq pos (point))
	;; We're now there, modulo comments and whitespace.
	;; Try to be line oriented; position point at the closest
	;; preceding boi that isn't inside a comment, but if we hit
	;; the previous declaration then we use the current point
	;; instead.
	(while (and (/= (point) (c-point 'boi))
		    (c-backward-single-comment)))
	(if (/= (point) (c-point 'boi))
	    (goto-char pos)))

      (c-keep-region-active)
      (= arg 0))))

(defun c-forward-to-nth-EOF-} (n where)
  ;; Skip to the closing brace of the Nth function after point.  If
  ;; point is inside a function, this counts as the first.  Point must be
  ;; outside any comment/string or macro.
  ;;
  ;; N must be strictly positive.
  ;; WHERE describes the position of point, one of the symbols `at-header',
  ;; `in-header', `in-block', `in-trailer', `at-function-end',
  ;; `outwith-function' as returned by c-where-wrt-brace-construct.
  ;;
  ;; If we run out of functions, leave point at EOB.  Return zero on success,
  ;; otherwise the number of }s still to go.
  ;;
  ;; This function may do hidden buffer changes.

  (cond
  ;; What we do to go forward over the first defun depends on where we
  ;; start.  We go to the closing brace of that defun, even when we go
  ;; backwards to it (in a "struct foo {...} bar ;").
   ((eobp))
   ((eq where 'in-block)
    (goto-char (c-least-enclosing-brace (c-parse-state)))
    (forward-sexp)
    (setq n (1- n)))
   ((eq where 'in-trailer)
    (c-syntactic-skip-backward "^}")
    (setq n (1- n)))
1602 1603 1604 1605 1606
   ((memq where '(at-function-end outwith-function at-header in-header))
    (when (c-syntactic-re-search-forward "{" nil 'eob)
      (backward-char)
      (forward-sexp)
      (setq n (1- n))))
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
   (t (error "c-forward-to-nth-EOF-}: `where' is %s" where)))

  ;; Each time round the loop, go forward to a "}" at the outermost level.
  (while (and (> n 0) (not (eobp)))
					;(c-parse-state)	; This call speeds up the following one by a factor
					; of ~6.  Hmmm.  2006/4/5.
    (when (c-syntactic-re-search-forward "{" nil 'eob)
      (backward-char)
      (forward-sexp))
    (setq n (1- n)))
  n)
1618 1619

(defun c-end-of-defun (&optional arg)
1620 1621 1622 1623
  "Move forward to the end of a top level declaration.
With argument, do it that many times.  Negative argument -N means move
back to Nth preceding end.  Returns t unless search stops due to
beginning or end of buffer.
1624 1625 1626 1627

An end of a defun occurs right after the close-parenthesis that matches
the open-parenthesis that starts a defun; see `beginning-of-defun'."
  (interactive "p")
1628 1629
  (or arg (setq arg 1))