cc-cmds.el 114 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

Gerd Moellmann's avatar
Gerd Moellmann committed
3
;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4

5 6
;; Authors:    2000- Martin Stjernholm
;;	       1998-1999 Barry A. Warsaw and Martin Stjernholm
Barry A. Warsaw's avatar
Barry A. Warsaw committed
7
;;             1992-1997 Barry A. Warsaw
Richard M. Stallman's avatar
Richard M. Stallman committed
8 9
;;             1987 Dave Detlefs and Stewart Clamen
;;             1985 Richard M. Stallman
Barry A. Warsaw's avatar
Barry A. Warsaw committed
10
;; Maintainer: bug-cc-mode@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
11
;; Created:    22-Apr-1997 (split from cc-mode.el)
12
;; Version:    See cc-mode.el
Richard M. Stallman's avatar
Richard M. Stallman committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
;; 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
28
;; along with GNU Emacs; see the file COPYING.  If not, write to
Gerd Moellmann's avatar
Gerd Moellmann committed
29
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Richard M. Stallman's avatar
Richard M. Stallman committed
30 31
;; Boston, MA 02111-1307, USA.

32 33 34 35
;;; Commentary:

;;; Code:

Barry A. Warsaw's avatar
Barry A. Warsaw committed
36
(eval-when-compile
Gerd Moellmann's avatar
Gerd Moellmann committed
37
  (let ((load-path
Gerd Moellmann's avatar
Gerd Moellmann committed
38 39 40
	 (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
41
	   load-path)))
Gerd Moellmann's avatar
Gerd Moellmann committed
42 43 44 45 46 47 48 49 50 51 52 53 54
    (require 'cc-bytecomp)))

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

;; Silence the compiler.
(cc-bytecomp-defvar delete-key-deletes-forward) ; XEmacs 20+
(cc-bytecomp-defun delete-forward-p)	; XEmacs 21+
(cc-bytecomp-obsolete-fun insert-and-inherit) ; Marked obsolete in XEmacs 19
(cc-bytecomp-defvar filladapt-mode)	; c-fill-paragraph contains a kludge
					; which looks at this.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
55

Gerd Moellmann's avatar
Gerd Moellmann committed
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 191 192 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
(defvar c-fix-backslashes t)

(defun c-shift-line-indentation (shift-amt)
  (let ((pos (- (point-max) (point)))
	(c-macro-start c-macro-start)
	tmp-char-inserted)
    (if (zerop shift-amt)
	nil
      (when (and (c-query-and-set-macro-start)
		 (looking-at "[ \t]*\\\\$")
		 (save-excursion
		   (skip-chars-backward " \t")
		   (bolp)))
	(insert ?x)
	(backward-char)
	(setq tmp-char-inserted t))
      (unwind-protect
	  (let ((col (current-indentation)))
	    (delete-region (c-point 'bol) (c-point 'boi))
	    (beginning-of-line)
	    (indent-to (+ col shift-amt)))
	(when tmp-char-inserted
	  (delete-char 1))))
    ;; If initial point was within line's indentation and we're not on
    ;; a line with a line continuation in a macro, position after the
    ;; indentation.  Else stay at same point in text.
    (if (and (< (point) (c-point 'boi))
	     (not tmp-char-inserted))
	(back-to-indentation)
      (if (> (- (point-max) pos) (point))
	  (goto-char (- (point-max) pos))))))

(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)."
  (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
				 "\\(\\s *\\)\\\\$"
			       "\\(\\s *\\)$")))
	       (<= (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
	      (or (let* ((c-parsing-error nil)
			 (c-syntactic-context (or syntax
						  c-syntactic-context
						  (c-guess-basic-syntax)))
			 indent)
		    (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)))
		    (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)
  "Inserts a newline and indents the new line.
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."
  ;; FIXME: Backslashes before eol in comments and literals aren't
  ;; 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.
      (let ((c-syntactic-context (c-guess-basic-syntax)))
	;; 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)
	 (syntax (c-guess-basic-syntax)))
    (if (not (consp arg))
	(message "syntactic analysis: %s" syntax)
      (indent-for-comment)
      (insert-and-inherit (format "%s" syntax))
      ))
  (c-keep-region-active))

(defun c-syntactic-information-on-region (from to)
  "Inserts a comment with the syntactic analysis on every line in the region."
  (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)))))


(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.

When it's turned off, the electric keys does no reindentation, the
indentation functions indents every new line to the same level as the
previous nonempty line, and \\[c-indent-command] adjusts the
indentation in seps specified `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'.

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))

Gerd Moellmann's avatar
Gerd Moellmann committed
282
(defun c-toggle-auto-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
283
  "Toggle auto-newline feature.
Gerd Moellmann's avatar
Gerd Moellmann committed
284 285 286
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
287 288 289 290 291 292 293 294 295 296

When the auto-newline feature is enabled (as evidenced by the `/a' or
`/ah' on the modeline after the mode name) newlines are automatically
inserted after special characters such as brace, comma, semi-colon,
and colon."
  (interactive "P")
  (setq c-auto-newline (c-calculate-state arg c-auto-newline))
  (c-update-modeline)
  (c-keep-region-active))

Gerd Moellmann's avatar
Gerd Moellmann committed
297
(defun c-toggle-hungry-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
298
  "Toggle hungry-delete-key feature.
Gerd Moellmann's avatar
Gerd Moellmann committed
299 300 301
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
302 303 304 305 306 307 308 309 310

When the hungry-delete-key feature is enabled (as evidenced by the
`/h' or `/ah' on the modeline after the mode name) the delete key
gobbles all preceding whitespace in one fell swoop."
  (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
311
(defun c-toggle-auto-hungry-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
312
  "Toggle auto-newline and hungry-delete-key features.
Gerd Moellmann's avatar
Gerd Moellmann committed
313
Optional numeric ARG, if supplied, turns on auto-newline and
Richard M. Stallman's avatar
Richard M. Stallman committed
314
hungry-delete when positive, turns them off when negative, and just
Gerd Moellmann's avatar
Gerd Moellmann committed
315
toggles them when zero or left out.
Richard M. Stallman's avatar
Richard M. Stallman committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
  (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))


;; Electric keys

(defun c-electric-backspace (arg)
  "Deletes preceding character or whitespace.
If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
\"/ah\" string on the mode line, then all preceding whitespace is
331 332 333
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.
Richard M. Stallman's avatar
Richard M. Stallman committed
334 335

See also \\[c-electric-delete]."
336
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
337 338 339 340 341
  (if (or (not c-hungry-delete-key)
	  arg
	  (c-in-literal))
      (funcall c-backspace-function (prefix-numeric-value arg))
    (let ((here (point)))
342
      (c-skip-ws-backward)
Richard M. Stallman's avatar
Richard M. Stallman committed
343 344 345 346 347
      (if (/= (point) here)
	  (delete-region (point) here)
	(funcall c-backspace-function 1)
	))))

348 349 350 351 352 353 354 355 356 357 358 359 360
(defun c-electric-delete-forward (arg)
  "Deletes following character or whitespace.
If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
\"/ah\" string 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."
  (interactive "*P")
  (if (or (not c-hungry-delete-key)
	  arg
	  (c-in-literal))
      (funcall c-delete-function (prefix-numeric-value arg))
    (let ((here (point)))
361
      (c-skip-ws-forward)
362 363 364 365
      (if (/= (point) here)
	  (delete-region (point) here)
	(funcall c-delete-function 1)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
366 367
(defun c-electric-delete (arg)
  "Deletes preceding or following character or whitespace.
368 369 370 371 372 373 374 375 376 377 378 379 380 381
This function either deletes forward as `c-electric-delete-forward' or
backward as `c-electric-backspace', depending on the configuration:

If the function `delete-forward-p' is defined (XEmacs 21) and returns
non-nil, it deletes forward.  Else, if the variable
`delete-key-deletes-forward' is defined (XEmacs 20) and is set to
non-nil, it deletes forward.  Otherwise it deletes backward.

Note: This is the way in XEmacs 20 and later to choose the correct
action for the [delete] key, whichever key that means.  In other
flavors this function isn't used, instead it's left to the user to
bind [delete] to either \\[c-electric-delete-forward] or \\[c-electric-backspace] as appropriate
\(the keymap `function-key-map' is useful for that).  Emacs 21 handles
that automatically, though."
382
  (interactive "*P")
Barry A. Warsaw's avatar
Barry A. Warsaw committed
383 384 385 386
  (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
	       (delete-forward-p))
	  (and (boundp 'delete-key-deletes-forward) ;XEmacs 20
	       delete-key-deletes-forward))
387
      (c-electric-delete-forward arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
388 389 390 391 392 393
    (c-electric-backspace arg)))

(defun c-electric-pound (arg)
  "Electric pound (`#') insertion.
Inserts a `#' character specially depending on the variable
`c-electric-pound-behavior'.  If a numeric ARG is supplied, or if
394
point is inside a literal or a macro, nothing special happens."
395
  (interactive "*P")
Barry A. Warsaw's avatar
Barry A. Warsaw committed
396 397
  (if (or arg
	  (not (memq 'alignleft c-electric-pound-behavior))
398 399 400 401 402 403 404
	  (save-excursion
	    (skip-chars-backward " \t")
	    (not (bolp)))
	  (save-excursion
	    (and (= (forward-line -1) 0)
		 (progn (end-of-line)
			(eq (char-before) ?\\))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
405
	  (c-in-literal))
Richard M. Stallman's avatar
Richard M. Stallman committed
406 407 408 409 410 411 412
      ;; 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)
413
      (insert last-command-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
414 415 416 417 418 419 420 421 422 423 424
      (and (not bolp)
	   (goto-char (- (point-max) pos)))
      )))

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

If the auto-newline feature is turned on, as evidenced by the \"/a\"
or \"/ah\" string on the mode line, newlines are inserted before and
after braces based on the value of `c-hanging-braces-alist'.

425
Also, the line is re-indented unless a numeric ARG is supplied, the
426 427
brace is inserted inside a literal, or `c-syntactic-indentation' is
nil.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
428 429 430

This function does various newline cleanups based on the value of
`c-cleanup-list'."
431
  (interactive "*P")
432 433 434 435 436 437 438 439 440 441 442 443 444 445
  (let* ((safepos (c-safe-position (point) (c-parse-state)))
	 (literal (c-in-literal safepos))
	 ;; We want to inhibit blinking the paren since this will be
	 ;; most disruptive.  We'll blink it ourselves later on.
	 (old-blink-paren blink-paren-function)
	 blink-paren-function)
    (cond
     ((or literal arg)
      (self-insert-command (prefix-numeric-value arg)))
     ((not (looking-at "[ \t]*\\\\?$"))
      (self-insert-command (prefix-numeric-value arg))
      (if c-syntactic-indentation
	  (indent-according-to-mode)))
     (t
446 447 448 449 450 451 452
      (let* ((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
Barry A. Warsaw's avatar
Barry A. Warsaw committed
453
		brace-list-intro brace-entry-open
454 455 456 457
		block-open block-close
		substatement-open statement-case-open
		extern-lang-open extern-lang-close
		namespace-open namespace-close
Barry A. Warsaw's avatar
Barry A. Warsaw committed
458
		inexpr-class-open inexpr-class-close
459
		))
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
	     (insertion-point (point))
	     (preserve-p (and (not (bobp))
			      (eq ?\  (char-syntax (char-before)))))
	     ;; shut this up too
	     (c-echo-syntactic-information-p nil)
	     delete-temp-newline syntax newlines)
	;; only insert a newline if there is non-whitespace behind us
	(when (save-excursion
		(skip-chars-backward " \t")
		(not (bolp)))
	  (setq delete-temp-newline
		(list (point-marker)))
	  (c-newline-and-indent)
	  (setcdr delete-temp-newline (point-marker)))
	(unwind-protect
	    (progn
	      (if (eq last-command-char ?{)
		  (setq c-state-cache (cons (point) c-state-cache)))
	      (self-insert-command (prefix-numeric-value arg))
	      (let ((c-syntactic-indentation-in-macros t))
		;; Turn on syntactic macro analysis to help with auto
		;; newlines only.
		(setq syntax (c-guess-basic-syntax)))
Richard M. Stallman's avatar
Richard M. Stallman committed
483
	      (setq newlines
484 485 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
		    (and
		     c-auto-newline
		     (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))))
	      ;; 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))))
		  (setq newlines nil))
	      ;; If syntax is a function symbol, then call it using the
	      ;; defined semantics.
	      (if (and (not (consp (cdr newlines)))
		       (functionp (cdr newlines)))
		  (let ((c-syntactic-context syntax))
		    (setq newlines
			  (funcall (cdr newlines)
				   (car newlines)
				   insertion-point))))
	      ;; does a newline go before the open brace?
	      (when (memq 'before newlines)
		;; we leave the newline we've put in there before,
		;; but we need to re-indent the line above
		(when delete-temp-newline
		  (set-marker (car delete-temp-newline) nil)
		  (set-marker (cdr delete-temp-newline) nil)
		  (setq delete-temp-newline nil))
		(when c-syntactic-indentation
		  (let ((pos (- (point-max) (point)))
			(here (point)))
		    (forward-line -1)
		    (indent-according-to-mode)
		    (goto-char (- (point-max) pos))
		    ;; if the buffer has changed due to the
		    ;; indentation, we need to recalculate syntax for
		    ;; the current line.
		    (if (/= (point) here)
			(let ((c-syntactic-indentation-in-macros t))
			  ;; Turn on syntactic macro analysis to help
			  ;; with auto newlines only.
			  (setq syntax (c-guess-basic-syntax))))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
540
	  ;; must remove the newline we just stuck in (if we really did it)
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	  (when delete-temp-newline
	    (save-excursion
	      (delete-region (car delete-temp-newline)
			     (cdr delete-temp-newline))
	      (goto-char (car delete-temp-newline))
	      (set-marker (car delete-temp-newline) nil)
	      (set-marker (cdr delete-temp-newline) nil)
	      ;; if there is whitespace before point, then preserve
	      ;; at least one space.
	      (just-one-space)
	      (if (not preserve-p)
		  (delete-char -1)))))
	(if (not (memq 'before newlines))
	    ;; since we're hanging the brace, we need to recalculate
	    ;; syntax.
	    (let ((c-syntactic-indentation-in-macros t))
	      ;; Turn on syntactic macro analysis to help with auto
	      ;; newlines only.
	      (setq syntax (c-guess-basic-syntax))))
	(when c-syntactic-indentation
	  ;; Now adjust the line's indentation.  Don't update the state
	  ;; cache since c-guess-basic-syntax isn't called when
	  ;; c-syntactic-context is set.
	  (let* ((c-syntactic-context syntax))
	    (indent-according-to-mode)))
Richard M. Stallman's avatar
Richard M. Stallman committed
566 567 568
	;; Do all appropriate clean ups
	(let ((here (point))
	      (pos (- (point-max) (point)))
Gerd Moellmann's avatar
Gerd Moellmann committed
569
	      mbeg mend tmp)
Richard M. Stallman's avatar
Richard M. Stallman committed
570 571 572 573 574 575 576 577
	  ;; clean up empty defun braces
	  (if (and c-auto-newline
		   (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)
578
		     (c-skip-ws-backward)
Richard M. Stallman's avatar
Richard M. Stallman committed
579 580 581 582
		     (eq (char-before) ?\{))
		   ;; make sure matching open brace isn't in a comment
		   (not (c-in-literal)))
	      (delete-region (point) (1- here)))
Gerd Moellmann's avatar
Gerd Moellmann committed
583 584
	  ;; clean up brace-else-brace and brace-elseif-brace
	  (when (and c-auto-newline
585
		     (eq last-command-char ?\{))
Gerd Moellmann's avatar
Gerd Moellmann committed
586 587
	    (cond
	     ((and (memq 'brace-else-brace c-cleanup-list)
588 589 590 591 592 593 594
		   (re-search-backward
		    (concat "}"
			    "\\([ \t\n]\\|\\\\\n\\)*"
			    "else"
			    "\\([ \t\n]\\|\\\\\n\\)*"
			    "{")
		    nil t)
Richard M. Stallman's avatar
Richard M. Stallman committed
595 596 597
		   (progn
		     (setq mbeg (match-beginning 0)
			   mend (match-end 0))
Gerd Moellmann's avatar
Gerd Moellmann committed
598 599
		     (eq (match-end 0) here)))
	      (delete-region mbeg mend)
600
	      (insert-and-inherit "} else {"))
Gerd Moellmann's avatar
Gerd Moellmann committed
601 602 603 604
	     ((and (memq 'brace-elseif-brace c-cleanup-list)
		   (progn
		     (goto-char (1- here))
		     (setq mend (point))
605
		     (c-skip-ws-backward)
Gerd Moellmann's avatar
Gerd Moellmann committed
606 607 608 609 610 611
		     (setq mbeg (point))
		     (eq (char-before) ?\)))
		   (= (c-backward-token-1 1 t) 0)
		   (eq (char-after) ?\()
		   (progn
		     (setq tmp (point))
612 613 614 615 616 617 618 619
		     (re-search-backward
		      (concat "}"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "else"
			      "\\([ \t\n]\\|\\\\\n\\)+"
			      "if"
			      "\\([ \t\n]\\|\\\\\n\\)*")
		      nil t))
Gerd Moellmann's avatar
Gerd Moellmann committed
620 621 622
		   (eq (match-end 0) tmp))
	      (delete-region mbeg mend)
	      (goto-char mbeg)
623
	      (insert ?\ ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
624 625 626 627
	  (goto-char (- (point-max) pos))
	  )
	;; does a newline go after the brace?
	(if (memq 'after newlines)
628 629 630 631 632 633 634 635 636
	    (c-newline-and-indent))
	)))
    ;; blink the paren
    (and (eq last-command-char ?\})
	 (not executing-kbd-macro)
	 old-blink-paren
	 (save-excursion
	   (c-backward-syntactic-ws safepos)
	   (funcall old-blink-paren)))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
637

Richard M. Stallman's avatar
Richard M. Stallman committed
638 639
(defun c-electric-slash (arg)
  "Insert a slash character.
640 641 642 643 644 645 646 647 648

Indent the line as a comment, if:

  1. The slash is second of a `//' line oriented comment introducing
     token and we are on a comment-only-line, or

  2. The slash is part of a `*/' token that closes a block oriented
     comment.

649 650
If a numeric ARG is supplied, point is inside a literal, or
`c-syntactic-indentation' is nil, indentation is inhibited."
651
  (interactive "*P")
652
  (let* ((ch (char-before))
653
	 (literal (c-in-literal))
654 655
	 (indentp (and c-syntactic-indentation
		       (not arg)
656 657
		       (eq last-command-char ?/)
		       (or (and (eq ch ?/)
658
				(not literal))
659
			   (and (eq ch ?*)
660
				literal))
661 662 663
		       ))
	 ;; shut this up
	 (c-echo-syntactic-information-p nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
664 665
    (self-insert-command (prefix-numeric-value arg))
    (if indentp
Gerd Moellmann's avatar
Gerd Moellmann committed
666
	(indent-according-to-mode))))
Richard M. Stallman's avatar
Richard M. Stallman committed
667 668 669 670 671

(defun c-electric-star (arg)
  "Insert a star character.
If the star is the second character of a C style comment introducing
construct, and we are on a comment-only-line, indent line as comment.
672 673
If a numeric ARG is supplied, point is inside a literal, or
`c-syntactic-indentation' is nil, indentation is inhibited."
674
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
675 676 677
  (self-insert-command (prefix-numeric-value arg))
  ;; if we are in a literal, or if arg is given do not re-indent the
  ;; current line, unless this star introduces a comment-only line.
678 679
  (if (and c-syntactic-indentation
	   (not arg)
680
	   (eq (c-in-literal) 'c)
Richard M. Stallman's avatar
Richard M. Stallman committed
681 682 683 684 685 686 687 688
	   (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
689 690
      (let (c-echo-syntactic-information-p) ; shut this up
	(indent-according-to-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
691 692 693 694 695 696 697 698 699
    ))

(defun c-electric-semi&comma (arg)
  "Insert a comma or semicolon.
When the auto-newline feature is turned on, as evidenced by the \"/a\"
or \"/ah\" string on the mode line, a newline might be inserted.  See
the variable `c-hanging-semi&comma-criteria' for how newline insertion
is determined.

700 701
When a semicolon is inserted, the line is re-indented unless a numeric
arg is supplied, point is inside a literal, or
702
`c-syntactic-indentation' is nil.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
703 704 705

Based on the value of `c-cleanup-list', this function cleans up commas
following brace lists and semicolons following defuns."
706
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
707 708 709 710 711
  (let* ((lim (c-most-enclosing-brace (c-parse-state)))
	 (literal (c-in-literal lim))
	 (here (point))
	 ;; shut this up
	 (c-echo-syntactic-information-p nil))
712
    (if (or literal arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
713 714 715
	(self-insert-command (prefix-numeric-value arg))
      ;; do some special stuff with the character
      (self-insert-command (prefix-numeric-value arg))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
716 717
      ;; do all cleanups and newline insertions if c-auto-newline is
      ;; turned on
718 719
      (if (or (not c-auto-newline)
	      (not (looking-at "[ \t]*\\\\?$")))
720
	  (if c-syntactic-indentation
Gerd Moellmann's avatar
Gerd Moellmann committed
721
	      (indent-according-to-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
722 723 724 725 726 727 728 729 730 731
	;; clean ups
	(let ((pos (- (point-max) (point))))
	  (if (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)
732
		     (c-skip-ws-backward)
Richard M. Stallman's avatar
Richard M. Stallman committed
733 734 735 736 737 738
		     (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)))
	;; re-indent line
739
	(if c-syntactic-indentation
Gerd Moellmann's avatar
Gerd Moellmann committed
740
	    (indent-according-to-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
741 742 743 744 745 746 747 748 749 750 751 752 753
	;; 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
754
	      (c-newline-and-indent))
Richard M. Stallman's avatar
Richard M. Stallman committed
755 756 757 758 759 760 761 762 763
	  )))))

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

If the auto-newline feature is turned on, as evidenced by the \"/a\"
or \"/ah\" string on the mode line, newlines are inserted before and
after colons based on the value of `c-hanging-colons-alist'.

764
Also, the line is re-indented unless a numeric ARG is supplied, the
765 766
colon is inserted inside a literal, or `c-syntactic-indentation' is
nil.
Richard M. Stallman's avatar
Richard M. Stallman committed
767 768 769

This function cleans up double colon scope operators based on the
value of `c-cleanup-list'."
770
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
771 772
  (let* ((bod (c-point 'bod))
	 (literal (c-in-literal bod))
773
	 newlines is-scope-op
Richard M. Stallman's avatar
Richard M. Stallman committed
774 775
	 ;; shut this up
	 (c-echo-syntactic-information-p nil))
776 777 778 779 780 781 782 783
    (cond
     ((or literal arg)
      (self-insert-command (prefix-numeric-value arg)))
     ((not (looking-at "[ \t]*\\\\?$"))
      (self-insert-command (prefix-numeric-value arg))
      (if c-syntactic-indentation
	  (indent-according-to-mode)))
     (t
Richard M. Stallman's avatar
Richard M. Stallman committed
784 785 786 787 788 789 790 791 792
      ;; insert the colon, then do any specified cleanups
      (self-insert-command (prefix-numeric-value arg))
      (let ((pos (- (point-max) (point)))
	    (here (point)))
	(if (and c-auto-newline
		 (memq 'scope-operator c-cleanup-list)
		 (eq (char-before) ?:)
		 (progn
		   (forward-char -1)
793
		   (c-skip-ws-backward)
Richard M. Stallman's avatar
Richard M. Stallman committed
794 795 796
		   (eq (char-before) ?:))
		 (not (c-in-literal))
		 (not (eq (char-after (- (point) 2)) ?:)))
797 798 799
	    (progn
	      (delete-region (point) (1- here))
	      (setq is-scope-op t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
800
	(goto-char (- (point-max) pos)))
801 802
      ;; indent the current line if it's done syntactically.
      (if c-syntactic-indentation
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	  ;; 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))
      (let* ((c-syntactic-indentation-in-macros 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)
				       (let ((buffer-undo-list t))
					 (insert ?\n)
					 (unwind-protect
					     (c-guess-basic-syntax)
					   (delete-char -1)))
				       c-hanging-colons-alist)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
831 832 833 834
      ;; 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)
835
	       (not is-scope-op)
Richard M. Stallman's avatar
Richard M. Stallman committed
836 837 838 839 840
	       (save-excursion
		 (skip-chars-backward ": \t")
		 (not (bolp))))
	  (let ((pos (- (point-max) (point))))
	    (forward-char -1)
841
	    (c-newline-and-indent)
Richard M. Stallman's avatar
Richard M. Stallman committed
842 843
	    (goto-char (- (point-max) pos))))
      ;; does a newline go after the colon?
844 845
      (if (and (memq 'after (cdr-safe newlines))
	       (not is-scope-op))
846 847
	  (c-newline-and-indent))
      ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
848 849 850

(defun c-electric-lt-gt (arg)
  "Insert a less-than, or greater-than character.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
851 852
The line will be re-indented if the character inserted is the second
of a C++ style stream operator and the buffer is in C++ mode.
853 854 855
Exceptions are when a numeric argument is supplied, point is inside a
literal, or `c-syntactic-indentation' is nil, in which case the line
will not be re-indented."
856
  (interactive "*P")
857 858
  (let ((indentp (and c-syntactic-indentation
		      (not arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
859 860 861 862 863 864
		      (eq (char-before) last-command-char)
		      (not (c-in-literal))))
	;; shut this up
	(c-echo-syntactic-information-p nil))
    (self-insert-command (prefix-numeric-value arg))
    (if indentp
Gerd Moellmann's avatar
Gerd Moellmann committed
865
	(indent-according-to-mode))))
Richard M. Stallman's avatar
Richard M. Stallman committed
866

Barry A. Warsaw's avatar
Barry A. Warsaw committed
867 868 869
(defun c-electric-paren (arg)
  "Insert a parenthesis.

Gerd Moellmann's avatar
Gerd Moellmann committed
870 871
Some newline cleanups are done if appropriate; see the variable
`c-cleanup-list'.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
872

873 874 875
Also, the line is re-indented unless a numeric ARG is supplied, the
parenthesis is inserted inside a literal, or `c-syntactic-indentation'
is nil."
Barry A. Warsaw's avatar
Barry A. Warsaw committed
876
  (interactive "*P")
877 878
  (let ((literal (c-in-literal (c-point 'bod)))
	;; shut this up
Barry A. Warsaw's avatar
Barry A. Warsaw committed
879
	(c-echo-syntactic-information-p nil))
880
    (if (or arg literal)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
881 882 883 884 885 886 887 888
	(self-insert-command (prefix-numeric-value arg))
      ;; do some special stuff with the character
      (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)
	(self-insert-command (prefix-numeric-value arg))
889 890 891
	(if c-syntactic-indentation
	    (indent-according-to-mode))
	(when (looking-at "[ \t]*\\\\?$")
Gerd Moellmann's avatar
Gerd Moellmann committed
892 893 894 895 896 897 898 899
	  (when c-auto-newline
	    ;; Do all appropriate clean ups
	    (let ((here (point))
		  (pos (- (point-max) (point)))
		  mbeg mend)
	      ;; clean up brace-elseif-brace
	      (if (and (memq 'brace-elseif-brace c-cleanup-list)
		       (eq last-command-char ?\()
900 901 902 903 904 905 906 907 908
		       (re-search-backward
			(concat "}"
				"\\([ \t\n]\\|\\\\\n\\)*"
				"else"
				"\\([ \t\n]\\|\\\\\n\\)+"
				"if"
				"\\([ \t\n]\\|\\\\\n\\)*"
				"(")
			nil t)
Gerd Moellmann's avatar
Gerd Moellmann committed
909 910 911 912 913 914 915
		       (save-excursion
			 (setq mbeg (match-beginning 0)
			       mend (match-end 0))
			 (= mend here))
		       (not (c-in-literal)))
		  (progn
		    (delete-region mbeg mend)
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
		    (insert-and-inherit "} else if ("))
		;; clean up brace-catch-brace
		(goto-char here)
		(if (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)
			 (save-excursion
			   (setq mbeg (match-beginning 0)
				 mend (match-end 0))
			   (= mend here))
			 (not (c-in-literal)))
		    (progn
		      (delete-region mbeg mend)
		      (insert-and-inherit "} catch ("))))
Gerd Moellmann's avatar
Gerd Moellmann committed
936 937 938 939 940 941 942 943 944 945 946 947 948
	      (goto-char (- (point-max) pos))
	      )))
	(let (beg (end (1- (point))))
	  (cond ((and (memq 'space-before-funcall c-cleanup-list)
		      (eq last-command-char ?\()
		      (save-excursion
			(backward-char)
			(skip-chars-backward " \t")
			(setq beg (point))
			(c-on-identifier)))
		 (save-excursion
		   (delete-region beg end)
		   (goto-char beg)
949
		   (insert ?\ )))
Gerd Moellmann's avatar
Gerd Moellmann committed
950 951 952 953 954 955 956 957 958 959
		((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))))
960 961 962
	(and (not executing-kbd-macro)
	     old-blink-paren
	     (funcall old-blink-paren))))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
963

Gerd Moellmann's avatar
Gerd Moellmann committed
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
(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
`c-syntactic-indentation' is non-nil."
  (let (;; shut this up
	(c-echo-syntactic-information-p nil))
    (when (and 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))))
983 984 985 986 987 988 989 990 991
      ;; 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
992 993 994 995 996 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


;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
(defun c-forward-into-nomenclature (&optional arg)
  "Move forward to end of a nomenclature section or word.
With arg, to it arg times."
  (interactive "p")
  (let ((case-fold-search nil))
    (if (> arg 0)
	(re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg)
      (while (and (< arg 0)
		  (re-search-backward
		   "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
		   (point-min) 0))
	(forward-char 1)
	(setq arg (1+ arg)))))
  (c-keep-region-active))

(defun c-backward-into-nomenclature (&optional arg)
  "Move backward to beginning of a nomenclature section or word.
With optional ARG, move that many times.  If ARG is negative, move
forward."
  (interactive "p")
  (c-forward-into-nomenclature (- arg))
  (c-keep-region-active))

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

1025 1026
(defun c-beginning-of-defun (&optional arg)
  "Move backward to the beginning of a defun.
1027 1028 1029 1030 1031 1032
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.
1033 1034 1035 1036

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

1038
  (interactive "p")
1039 1040
  (or arg (setq arg 1))

1041
  (if (< arg 0)
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 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 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
      (when (c-end-of-defun (- arg))
	(c-forward-syntactic-ws)
	t)

    (catch 'exit
      (while (> arg 0)
	;; Note: Partial code duplication in `c-end-of-defun' and
	;; `c-declaration-limits'.

	(let ((paren-state (c-parse-state)) lim pos)
	  (unless (c-safe
		    (goto-char (c-least-enclosing-brace paren-state))
		    ;; If we moved to the outermost enclosing paren
		    ;; then we can use c-safe-position to set the
		    ;; limit.  Can't do that otherwise since the
		    ;; earlier paren pair on paren-state might very
		    ;; well be part of the declaration we should go
		    ;; to.
		    (setq lim (c-safe-position (point) paren-state))
		    t)
	    ;; At top level.  Make sure we aren't inside a literal.
	    (setq pos (c-literal-limits
		       (c-safe-position (point) paren-state)))
	    (if pos (goto-char (car pos))))

	  (while (let ((start (point)))
		   (c-beginning-of-decl-1 lim)
		   (if (= (point) start)
		       ;; Didn't move.  Might be due to bob or unbalanced
		       ;; parens.  Try to continue if it's the latter.
		       (unless (c-safe (goto-char
					(c-down-list-backward (point))))
			 ;; Didn't work, so it's bob then.
			 (goto-char (point-min))
			 (throw 'exit nil)))

		   (save-excursion
		     ;; Check if the declaration contains a brace
		     ;; block.  If not, we try another one.
		     (setq pos (point))
		     (not (and (c-syntactic-re-search-forward
				"[;{]" nil t 1 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.
					(c-in-knr-argdecl pos)))))))
	    (setq lim nil))

	  ;; Check if `c-beginning-of-decl-1' put us after the block
	  ;; in a declaration that doesn't end there.  We're searching
	  ;; back and forth over the block here, which can be
	  ;; expensive.
	  (setq pos (point))
	  (if (and c-opt-block-decls-with-vars-key
		   (progn
		     (c-backward-syntactic-ws)
		     (eq (char-before) ?}))
		   (eq (car (c-beginning-of-decl-1))
		       'previous)
		   (save-excursion
		     (c-end-of-decl-1)
		     (> (point) pos)))
	      nil
	    (goto-char pos))

	  (setq pos (point))
	  ;; 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-forward-comment -1)))
	  (if (/= (point) (c-point 'boi))
	      (goto-char pos))

	  (setq arg (1- arg)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1121 1122
    (c-keep-region-active)
    (= arg 0)))
1123 1124

(defun c-end-of-defun (&optional arg)
1125 1126 1127 1128
  "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.
1129 1130 1131

An end of a defun occurs right after the close-parenthesis that matches
the open-parenthesis that starts a defun; see `beginning-of-defun'."
1132

1133
  (interactive "p")
1134 1135
  (or arg (setq arg 1))

1136
  (if (< arg 0)
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 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
      (when (c-beginning-of-defun (- arg))
	(c-backward-syntactic-ws)
	t)

    (catch 'exit
      (while (> arg 0)
	;; Note: Partial code duplication in `c-beginning-of-defun'
	;; and `c-declaration-limits'.

	(let ((paren-state (c-parse-state)) lim pos)
	  (unless (c-safe
		    (goto-char (c-least-enclosing-brace paren-state))
		    ;; If we moved to the outermost enclosing paren
		    ;; then we can use c-safe-position to set the
		    ;; limit.  Can't do that otherwise since the
		    ;; earlier paren pair on paren-state might very
		    ;; well be part of the declaration we should go
		    ;; to.
		    (setq lim (c-safe-position (point) paren-state))
		    t)
	    ;; At top level.  Make sure we aren't inside a literal.
	    (setq pos (car-safe (c-literal-limits
				 (c-safe-position (point) paren-state))))
	    (if pos (goto-char pos)))

	  ;; Have to move to the start first so that `c-end-of-decl-1'
	  ;; has the correct start position.
	  (setq pos (point))
	  (when (memq (car (c-beginning-of-decl-1 lim))
		      '(previous macro))
	    ;; We moved back over the previous defun or a macro.  Move
	    ;; to the next token; it's the start of the next
	    ;; declaration.  We can also be directly after the block
	    ;; in a `c-opt-block-decls-with-vars-key' declaration, but
	    ;; then we won't move significantly far here.
	    (goto-char pos)
	    (c-forward-token-1 0))

	  (while (let ((start (point)))
		   (c-end-of-decl-1)
		   (if (= (point) start)
		       ;; Didn't move.  Might be due to eob or unbalanced
		       ;; parens.  Try to continue if it's the latter.
		       (if (c-safe (goto-char (c-up-list-forward (point))))
			   t
			 ;; Didn't work, so it's eob then.
			 (goto-char (point-max))
			 (throw 'exit nil))

		     (save-excursion
		       ;; Check if the declaration contains a brace
		       ;; block.  If not, we try another one.
		       (setq pos (point))
		       (goto-char start)
		       (not (c-syntactic-re-search-forward "{" pos t 1 t))))))

	  (setq pos (point))
	  ;; Try to be line oriented; position point after the next
	  ;; newline that isn't inside a comment, but if we hit the
	  ;; next declaration then we use the current point instead.
	  (while (and (not (bolp))
		      (not (looking-at "\\s *$"))
		      (c-forward-comment 1)))
	  (cond ((bolp))
		((looking-at "\\s *$")
		 (forward-line 1))
		(t
		 (goto-char pos)))

	  (setq arg (1- arg)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1207 1208
    (c-keep-region-active)
    (= arg 0)))
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 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
(defun c-declaration-limits (near)
  ;; Return a cons of the beginning and end positions of the current
  ;; top level declaration or macro.  If point is not inside any then
  ;; nil is returned, unless NEAR is non-nil in which case the closest
  ;; following one is chosen instead (if there is any).  The end
  ;; position is at the next line, providing there is one before the
  ;; declaration.
  (save-excursion

    ;; Note: Some code duplication in `c-beginning-of-defun' and
    ;; `c-end-of-defun'.
    (catch 'exit
      (let ((start (point))
	    (paren-state (c-parse-state))
	    lim pos end-pos)
	(unless (c-safe
		  (goto-char (c-least-enclosing-brace paren-state))
		  ;; If we moved to the outermost enclosing paren then we
		  ;; can use c-safe-position to set the limit.  Can't do
		  ;; that otherwise since the earlier paren pair on
		  ;; paren-state might very well be part of the
		  ;; declaration we should go to.
		  (setq lim (c-safe-position (point) paren-state))
		  t)
	  ;; At top level.  Make sure we aren't inside a literal.
	  (setq pos (c-literal-limits
		     (c-safe-position (point) paren-state)))
	  (if pos (goto-char (car pos))))

	(when (c-beginning-of-macro)
	  (throw 'exit
		 (cons (point)
		       (save-excursion
			 (c-end-of-macro)
			 (forward-line 1)
			 (point)))))

	(setq pos (point))
	(when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
		  (= pos (point)))
	  ;; We moved back over the previous defun.  Skip to the next
	  ;; one.  Not using c-forward-syntactic-ws here since we
	  ;; should not skip a macro.  We can also be directly after
	  ;; the block in a `c-opt-block-decls-with-vars-key'
	  ;; declaration, but then we won't move significantly far
	  ;; here.
	  (goto-char pos)
	  (while (c-forward-comment 10))

	  (when (and near (c-beginning-of-macro))
	    (throw 'exit
		   (cons (point)
			 (save-excursion
			   (c-end-of-macro)
			   (forward-line 1)
			   (point))))))

	(if (eobp) (throw 'exit nil))

	;; Check if `c-beginning-of-decl-1' put us after the block in a
	;; declaration that doesn't end there.  We're searching back and
	;; forth over the block here, which can be expensive.
	(setq pos (point))
	(if (and c-opt-block-decls-with-vars-key
		 (progn
		   (c-backward-syntactic-ws)
		   (eq (char-before) ?}))
		 (eq (car (c-beginning-of-decl-1))
		     'previous)
		 (save-excursion
		   (c-end-of-decl-1)
		   (and (> (point) pos)
			(setq end-pos (point)))))
	    nil
	  (goto-char pos))

	(if (and (not near) (> (point) start))
	    nil

	  ;; Try to be line oriented; position the limits at the
	  ;; closest preceding boi, and after the next newline, that
	  ;; isn't inside a comment, but if we hit a neighboring
	  ;; declaration then we instead use the exact declaration
	  ;; limit in that direction.
	  (cons (progn
		  (setq pos (point))
		  (while (and (/= (point) (c-point 'boi))
			      (c-forward-comment -1)))
		  (if (/= (point) (c-point 'boi))
		      pos
		    (point)))
		(progn
		  (if end-pos
		      (goto-char end-pos)
		    (c-end-of-decl-1))
		  (setq pos (point))
		  (while (and (not (bolp))
			      (not (looking-at "\\s *$"))
			      (c-forward-comment 1)))
		  (cond ((bolp)
			 (point))
			((looking-at "\\s *$")
			 (forward-line 1)
			 (point))
			(t
			 pos)))))
	))))

(defun c-mark-function ()
  "Put mark at end of the current top-level declaration or macro, point at beginning.
If point is not inside any then the closest following one is chosen.

As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
function does not require the declaration to contain a brace block."
  (interactive)

  ;; We try to be line oriented, unless there are several
  ;; declarations on the same line.
  (if (looking-at c-syntactic-eol)
      (c-backward-token-1 1 nil (c-point 'bol)))

  (let ((decl-limits (c-declaration-limits t)))
    (if (not decl-limits)
	(error "Cannot find any declaration")
      (goto-char (car decl-limits))
      (push-mark (cdr decl-limits) nil t))))

Richard M. Stallman's avatar
Richard M. Stallman committed
1337 1338 1339 1340

(defun c-beginning-of-statement (&optional count lim sentence-flag)
  "Go to the beginning of the innermost C statement.
With prefix arg, go back N - 1 statements.  If already at the
1341 1342
beginning of a statement then go to the beginning of the closest
preceding one, moving into nested blocks if necessary (use
Gerd Moellmann's avatar
Gerd Moellmann committed
1343 1344
\\[backward-sexp] to skip over a block).  If within or next to a
comment or multiline string, move by sentences instead of statements.
Richard M. Stallman's avatar
Richard M. Stallman committed
1345 1346 1347

When called from a program, this function takes 3 optional args: the
repetition count, a buffer position limit which is the farthest back
Gerd Moellmann's avatar
Gerd Moellmann committed
1348
to search for the syntactic context, and a flag saying whether to do
1349 1350 1351 1352 1353 1354
sentence motion in or near comments and multiline strings.

Note that `c-beginning-of-statement-1' is usually better to use from
programs.  It has much more well defined semantics than this one,
which is intended for interactive use and might therefore change to be
more \"DWIM:ey\"."
Richard M. Stallman's avatar
Richard M. Stallman committed
1355 1356
  (interactive (list (prefix-numeric-value current-prefix-arg)
		     nil t))
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
  (let* ((count (or count 1))
	 here
	 (range (c-collect-line-comments (c-literal-limits lim))))
    (while (and (/= count 0)
		(or (not lim) (> (point) lim)))
      (setq here (point))
      (if (and (not range) sentence-flag)
	  (save-excursion
	    ;; Find the comment next to point if we're not in one.
	    (if (> count 0)
1367
		(if (c-forward-comment-lc -1)
Gerd Moellmann's avatar
Gerd Moellmann committed
1368
		    (setq range (cons (point)
1369 1370 1371
				      (progn (c-forward-comment-lc 1)
					     (point))))
		  (c-skip-ws-backward)
Gerd Moellmann's avatar
Gerd Moellmann committed
1372 1373 1374 1375 1376
		  (setq range (point))
		  (setq range
			(if (eq (char-before) ?\")
			    (c-safe (c-backward-sexp 1)
				    (cons (point) range)))))
1377
	      (c-skip-ws-forward)
Gerd Moellmann's avatar
Gerd Moellmann committed
1378 1379 1380 1381 1382 1383
	      (if (eq (char-after) ?\")
		  (setq range (cons (point)
				    (progn
				      (c-forward-sexp 1)
				      (point))))
		(setq range (point))
1384
		(setq range (if (c-forward-comment-lc 1)
Gerd Moellmann's avatar
Gerd Moellmann committed
1385 1386
				(cons range (point))
			      nil))))
1387 1388 1389 1390 1391 1392
	    (setq range (c-collect-line-comments range))))
      (if (and (< count 0) (= here (point-max)))
	  ;; Special case because eob might be in a literal.
	  (setq range nil))
      (if range
	  (if (and sentence-flag
Gerd Moellmann's avatar
Gerd Moellmann committed
1393 1394 1395 1396 1397 1398
		   (or (/= (char-syntax (char-after (car range))) ?\")
		       ;; Only visit a string if it spans more than one line.
		       (save-excursion
			 (goto-char (car range))
			 (skip-chars-forward "^\n" (cdr range))
			 (< (point) (cdr range)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1399
	      (let* ((lit-type (c-literal-type range))
Gerd Moellmann's avatar
Gerd Moellmann committed
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
		     (line-prefix (concat "[ \t]*\\("
					  c-current-comment-prefix
					  "\\)[ \t]*"))
		     (beg (if (eq lit-type 'string)
			      (1+ (car range))
			    (save-excursion
			      (goto-char (car range))
			      (max (progn
				     (looking-at comment-start-skip)
				     (match-end 0))
				   (progn
				     (looking-at line-prefix)
				     (match-end 0))))))
		     (end (- (cdr range) (if (eq lit-type 'c) 2 1)))
		     (beg-of-para (if (eq lit-type 'string)
				      (lambda ())
				    (lambda ()
				      (beginning-of-line)
				      (if (looking-at line-prefix)
					  (goto-char (match-end 0)))))))
1420
		(save-restriction
Gerd Moellmann's avatar
Gerd Moellmann committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
		  ;; Move by sentence, but not past the limit of the
		  ;; literal, narrowed to the appropriate
		  ;; paragraph(s).
		  (narrow-to-region (save-excursion
				      (let ((pos (min here end)))
					(goto-char pos)
					(forward-paragraph -1)
					(if (looking-at paragraph-separate)
					    (forward-line))
					(when (> (point) beg)
					  (funcall beg-of-para)
					  (when (>= (point) pos)
					    (forward-paragraph -2)
					    (funcall beg-of-para)))
					(max (point) beg)))
				    end)
Gerd Moellmann's avatar
Gerd Moellmann committed
1437 1438 1439 1440
		  (c-safe (forward-sentence (if (< count 0) 1 -1)))
		  (if (and (memq lit-type '(c c++))
			   ;; Check if we stopped due to a comment
			   ;; prefix and not a sentence end.
Gerd Moellmann's avatar
Gerd Moellmann committed
1441 1442
			   (/= (point) (point-min))
			   (/= (point) (point-max))
Gerd Moellmann's avatar
Gerd Moellmann committed
1443 1444
			   (save-excursion
			     (beginning-of-line)
Gerd Moellmann's avatar
Gerd Moellmann committed
1445
			     (looking-at line-prefix))
Gerd Moellmann's avatar
Gerd Moellmann committed
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
			   (>= (point) (match-beginning 0))
			   (/= (match-beginning 1) (match-end 1))
			   (or (< (point) (match-end 0))
			       (and
				(= (point) (match-end 0))
				;; The comment prefix may contain
				;; characters that is regarded as end
				;; of sentence.
				(or (eolp)
				    (and
				     (save-excursion
				       (forward-paragraph -1)
				       (< (point) (match-beginning 0)))
				     (save-excursion
				       (beginning-of-line)
				       (or (not (re-search-backward
						 sentence-end
						 (c-point 'bopl)
						 t))
					   (< (match-end 0)
					      (c-point 'eol)))))))))
		      (setq count (+ count (if (< count 0) -1 1)))
		    (if (< count 0)
			(progn
			  ;; In block comments, if there's only
			  ;; horizontal ws between the text and the
			  ;; comment ender, stop before it.  Stop after
			  ;; the ender if there's either nothing or
			  ;; newlines between.
Gerd Moellmann's avatar
Gerd Moellmann committed
1475 1476
			  (when (and (eq lit-type 'c)
				     (eq (point) (point-max)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1477
			    (widen)
Gerd Moellmann's avatar
Gerd Moellmann committed
1478 1479 1480
			    (when (or (= (skip-chars-backward " \t") 0)
				      (eq (point) (point-max))
				      (bolp))
Gerd Moellmann's avatar
Gerd Moellmann committed
1481
			      (goto-char (cdr range)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1482
		      (when (and (eq (point) (point-min))
1483
				 (looking-at "[ \t]*\\\\?$"))
Gerd Moellmann's avatar
Gerd Moellmann committed
1484 1485 1486
			;; Stop before instead of after the comment
			;; starter if nothing follows it.
			(widen)
Gerd Moellmann's avatar
Gerd Moellmann committed
1487 1488 1489 1490
			(goto-char (car range))
			(if (and (eq lit-type 'string) (/= (point) here))
			    (setq count (1+ count)
				  range nil))))))
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
		;; See if we should escape the literal.
		(if (> count 0)
		    (if (< (point) here)
			(setq count (1- count))
		      (goto-char (car range))
		      (setq range nil))
		  (if (> (point) here)
		      (setq count (1+ count))
		    (goto-char (cdr range))
		    (setq range nil))))
	    (goto-char (if (> count 0) (car range) (cdr range)))
	    (setq range nil))
	(goto-char here)
	(if (> count 0)
Gerd Moellmann's avatar
Gerd Moellmann committed
1505 1506 1507 1508
	    (condition-case nil
		;; Stop before `{' and after `;', `{', `}' and `};'
		;; when not followed by `}' or `)', but on the other
		;; side of the syntactic ws.  Move by sexps and move
Gerd Moellmann's avatar
Gerd Moellmann committed
1509
		;; into parens.  Also stop before `#' when it's at boi
Gerd Moellmann's avatar
Gerd Moellmann committed
1510
		;; on a line.
Gerd Moellmann's avatar
Gerd Moellmann committed
1511
		(let ((literal-pos (not sentence-flag))
Gerd Moellmann's avatar
Gerd Moellmann committed
1512 1513
		      (large-enough (- (point-max)))
		      last last-below-line)
1514
		  (catch 'done
Gerd Moellmann's avatar
Gerd Moellmann committed
1515 1516
		    (while t
		      (setq last (point))
Gerd Moellmann's avatar
Gerd Moellmann committed
1517 1518 1519 1520 1521
		      (when (and (or (eq (char-after) ?\{)
				     (and (eq (char-after) ?#)
					  (eq (point) (c-point 'boi)))
				     )
				 (/= here last))
Gerd Moellmann's avatar
Gerd Moellmann committed
1522 1523 1524 1525 1526 1527 1528 1529
			(unless (and c-special-brace-lists
				     (eq (char-after) ?{)
				     (c-looking-at-special-brace-list))
			  (if (and (eq (char-after) ?#)
				   (numberp last-below-line)
				   (not (eq last-below-line here)))
			      (goto-char last-below-line))
			  (throw 'done t)))
1530 1531 1532 1533
		      ;; Don't know why I added the following, but it
		      ;; doesn't work when point is preceded by a line
		      ;; style comment. /mast
		      ;;(c-skip-ws-backward)
Gerd Moellmann's avatar
Gerd Moellmann committed
1534
		      (if literal-pos
1535 1536
			  (c-forward-comment-lc large-enough)
			(when (c-forward-comment-lc -1)
Gerd Moellmann's avatar
Gerd Moellmann committed
1537 1538
			  ;; Record position of first comment.
			  (save-excursion
1539
			    (c-forward-comment-lc 1)
Gerd Moellmann's avatar
Gerd Moellmann committed
1540
			    (setq literal-pos (point)))
1541
			  (c-forward-comment-lc large-enough)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
		      (unless last-below-line
			(if (save-excursion
			      (re-search-forward "\\(^\\|[^\\]\\)$" last t))
			    (setq last-below-line last)))
		      (cond ((bobp)	; Must handle bob specially.
			     (if (= here last)
				 (throw 'done t)
			       (goto-char last)
			       (throw 'done t)))
			    ((progn (backward-char)
				    (looking-at "[;{}]"))
			     (if (and c-special-brace-lists
				      (eq (char-after) ?{)
				      (c-looking-at-special-brace-list))
				 (skip-syntax-backward "w_") ; Speedup only.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1557
			       (if (or (= here last)
Gerd Moellmann's avatar
Gerd Moellmann committed
1558 1559 1560 1561
				       (memq (char-after last) '(?\) ?})))
				   (if (and (eq (char-before) ?})
					    (eq (char-after) ?\;))
				       (backward-char))
1562
				 (goto-char last)
Gerd Moellmann's avatar
Gerd Moellmann committed
1563 1564
				 (throw 'done t))))
			    ((= (char-syntax (char-after)) ?\")
Gerd Moellmann's avatar
Gerd Moellmann committed
1565 1566 1567 1568 1569 1570 1571 1572 1573
			     (let ((end (point)))
			       (forward-char)
			       (c-backward-sexp)
			       (save-excursion
				 (skip-chars-forward "^\n" end)
				 (when (< (point) end)
				   ;; Break at multiline string.
				   (setq literal-pos (1+ end))
				   (throw 'done t)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1574 1575
			    (t (skip-syntax-backward "w_")) ; Speedup only.
			    )))
Gerd Moellmann's avatar
Gerd Moellmann committed
1576 1577 1578 1579 1580
		  (if (and (numberp literal-pos)
			   (< (point) literal-pos))
		      ;; We jumped over a comment or string that
		      ;; should be investigated.
		      (goto-char literal-pos)