cc-cmds.el 90.1 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
Gerd Moellmann's avatar
Gerd Moellmann committed
28 29
;; along with this program; see the file COPYING.  If not, write to
;; 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.

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

Gerd Moellmann's avatar
Gerd Moellmann committed
52

Richard M. Stallman's avatar
Richard M. Stallman committed
53 54 55 56 57 58 59 60 61 62
(defun c-calculate-state (arg prevstate)
  ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
  ;; arg is nil or zero, toggle the state. If arg is negative, turn
  ;; the state off, and if arg is positive, turn the state on
  (if (or (not arg)
	  (zerop (setq arg (prefix-numeric-value arg))))
      (not prevstate)
    (> arg 0)))

;; Auto-newline and hungry-delete
Gerd Moellmann's avatar
Gerd Moellmann committed
63
(defun c-toggle-auto-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
64
  "Toggle auto-newline feature.
Gerd Moellmann's avatar
Gerd Moellmann committed
65 66 67
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
68 69 70 71 72 73 74 75 76 77

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
78
(defun c-toggle-hungry-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
79
  "Toggle hungry-delete-key feature.
Gerd Moellmann's avatar
Gerd Moellmann committed
80 81 82
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
83 84 85 86 87 88 89 90 91

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
92
(defun c-toggle-auto-hungry-state (&optional arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
93
  "Toggle auto-newline and hungry-delete-key features.
Gerd Moellmann's avatar
Gerd Moellmann committed
94
Optional numeric ARG, if supplied, turns on auto-newline and
Richard M. Stallman's avatar
Richard M. Stallman committed
95
hungry-delete when positive, turns them off when negative, and just
Gerd Moellmann's avatar
Gerd Moellmann committed
96
toggles them when zero or left out.
Richard M. Stallman's avatar
Richard M. Stallman committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

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
112 113 114
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
115 116

See also \\[c-electric-delete]."
117
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
118 119 120 121 122 123 124 125 126 127 128
  (if (or (not c-hungry-delete-key)
	  arg
	  (c-in-literal))
      (funcall c-backspace-function (prefix-numeric-value arg))
    (let ((here (point)))
      (skip-chars-backward " \t\n")
      (if (/= (point) here)
	  (delete-region (point) here)
	(funcall c-backspace-function 1)
	))))

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
(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)))
      (skip-chars-forward " \t\n")
      (if (/= (point) here)
	  (delete-region (point) here)
	(funcall c-delete-function 1)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
147 148
(defun c-electric-delete (arg)
  "Deletes preceding or following character or whitespace.
149 150 151 152 153 154 155 156 157 158 159 160 161 162
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."
163
  (interactive "*P")
Barry A. Warsaw's avatar
Barry A. Warsaw committed
164 165 166 167
  (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
	       (delete-forward-p))
	  (and (boundp 'delete-key-deletes-forward) ;XEmacs 20
	       delete-key-deletes-forward))
168
      (c-electric-delete-forward arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
169 170 171 172 173 174 175
    (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
point is inside a literal, nothing special happens."
176
  (interactive "*P")
Barry A. Warsaw's avatar
Barry A. Warsaw committed
177 178 179 180
  (if (or arg
	  (not (memq 'alignleft c-electric-pound-behavior))
	  (save-excursion (skip-chars-backward " \t") (not (bolp)))
	  (c-in-literal))
Richard M. Stallman's avatar
Richard M. Stallman committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
      ;; 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)
      (insert-char last-command-char 1)
      (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'.

Also, the line is re-indented unless a numeric ARG is supplied, there
201 202 203
are non-whitespace characters present on the line after the brace, the
brace is inserted inside a literal, or `c-syntactic-indentation' is
nil.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
204 205 206

This function does various newline cleanups based on the value of
`c-cleanup-list'."
207
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
208 209 210 211 212 213
  (let* ((c-state-cache (c-parse-state))
	 (safepos (c-safe-position (point) c-state-cache))
	 (literal (c-in-literal safepos)))
    ;; if we're in a literal, or we're not at the end of the line, or
    ;; a numeric arg is provided, or auto-newlining is turned off,
    ;; then just insert the character.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
214 215
    (if (or literal
	    arg
Richard M. Stallman's avatar
Richard M. Stallman committed
216 217
	    (not (looking-at "[ \t]*$")))
	(self-insert-command (prefix-numeric-value arg))
218 219 220 221 222 223 224
      (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
225
		brace-list-intro brace-entry-open
226 227 228 229
		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
230
		inexpr-class-open inexpr-class-close
231
		))
Richard M. Stallman's avatar
Richard M. Stallman committed
232 233 234 235 236 237
	    ;; 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
	    (insertion-point (point))
	    delete-temp-newline
238 239
	    (preserve-p (and (not (bobp))
			     (eq ?\  (char-syntax (char-before)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
240 241 242 243 244 245 246 247 248 249
	    ;; shut this up too
	    (c-echo-syntactic-information-p nil)
	    (syntax (progn
		      ;; only insert a newline if there is
		      ;; non-whitespace behind us
		      (if (save-excursion
			    (skip-chars-backward " \t")
			    (not (bolp)))
			  (progn (newline)
				 (setq delete-temp-newline t)))
250 251
		      (if (eq last-command-char ?{)
			  (setq c-state-cache (cons (point) c-state-cache)))
Richard M. Stallman's avatar
Richard M. Stallman committed
252 253 254 255 256
		      (self-insert-command (prefix-numeric-value arg))
		      ;; state cache doesn't change
		      (c-guess-basic-syntax)))
	    (newlines (and
		       c-auto-newline
Barry A. Warsaw's avatar
Barry A. Warsaw committed
257 258 259 260 261 262 263 264 265 266 267 268 269
		       (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)
Richard M. Stallman's avatar
Richard M. Stallman committed
270
			   '(ignore before after)))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
271 272 273 274 275 276 277 278 279
	;; 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))
Richard M. Stallman's avatar
Richard M. Stallman committed
280 281 282 283 284 285 286 287 288 289 290
	;; 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?
	(if (memq 'before newlines)
	    ;; we leave the newline we've put in there before,
	    ;; but we need to re-indent the line above
Gerd Moellmann's avatar
Gerd Moellmann committed
291 292 293
	    (let (old-ind
		  (old-point-max (point-max))
		  (pos (- (point-max) (point)))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
294
		  (here (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
295
	      (forward-line -1)
Gerd Moellmann's avatar
Gerd Moellmann committed
296
	      (setq old-ind (c-point 'boi))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310
	      (let ((c-state-cache (c-whack-state (point) c-state-cache)))
		;; we may need to update the cache. this should
		;; still be faster than recalculating the state
		;; in many cases
		(save-excursion
		  (save-restriction
		    (narrow-to-region here (point))
		    (if (and (c-safe (progn (backward-up-list -1) t))
			     (memq (char-before) '(?\) ?}))
			     (progn (widen)
				    (c-safe (progn (c-forward-sexp -1)
						   t))))
			(setq c-state-cache
			      (c-hack-state (point) 'open c-state-cache)))))
311
		(if c-syntactic-indentation
Gerd Moellmann's avatar
Gerd Moellmann committed
312 313 314
		    (indent-according-to-mode)))
	      (setq c-state-cache (c-adjust-state (c-point 'bol) old-point-max
						  (- (c-point 'boi) old-ind)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
315
						  c-state-cache))
Richard M. Stallman's avatar
Richard M. Stallman committed
316 317 318 319 320 321 322 323 324 325 326 327 328
	      (goto-char (- (point-max) pos))
	      ;; if the buffer has changed due to the indentation, we
	      ;; need to recalculate syntax for the current line, but
	      ;; we won't need to update the state cache.
	      (if (/= (point) here)
		  (setq syntax (c-guess-basic-syntax))))
	  ;; must remove the newline we just stuck in (if we really did it)
	  (and delete-temp-newline
	       (save-excursion
		 ;; if there is whitespace before point, then preserve
		 ;; at least one space.
		 (delete-indentation)
		 (just-one-space)
329
		 (setq c-state-cache (c-whack-state (point) c-state-cache))
Richard M. Stallman's avatar
Richard M. Stallman committed
330 331 332 333 334 335 336 337 338 339
		 (if (not preserve-p)
		     (delete-char -1))))
	  ;; since we're hanging the brace, we need to recalculate
	  ;; syntax.  Update the state to accurately reflect the
	  ;; beginning of the line.  We punt if we cross any open or
	  ;; closed parens because its just too hard to modify the
	  ;; known state.  This limitation will be fixed in v5.
	  (save-excursion
	    (let ((bol (c-point 'bol)))
	      (if (zerop (car (parse-partial-sexp bol (1- (point)))))
340
		  (setq syntax (c-guess-basic-syntax))
Richard M. Stallman's avatar
Richard M. Stallman committed
341 342
		;; gotta punt. this requires some horrible kludgery
		(beginning-of-line)
Gerd Moellmann's avatar
Gerd Moellmann committed
343 344
		(setq c-state-cache nil
		      c-state-cache (c-parse-state)
Richard M. Stallman's avatar
Richard M. Stallman committed
345 346
		      syntax nil))))
	  )
Gerd Moellmann's avatar
Gerd Moellmann committed
347 348 349 350 351 352 353 354 355
	;; 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* ((old-ind (c-point 'boi))
	       (old-point-max (point-max))
	       (c-syntactic-context syntax))
	  (indent-according-to-mode)
	  (setq c-state-cache (c-adjust-state (c-point 'bol) old-point-max
					      (- (c-point 'boi) old-ind)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
356
					      c-state-cache)))
Richard M. Stallman's avatar
Richard M. Stallman committed
357 358 359
	;; Do all appropriate clean ups
	(let ((here (point))
	      (pos (- (point-max) (point)))
Gerd Moellmann's avatar
Gerd Moellmann committed
360
	      mbeg mend tmp)
Richard M. Stallman's avatar
Richard M. Stallman committed
361 362 363 364 365 366 367 368 369 370 371 372 373
	  ;; 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)
		     (skip-chars-backward " \t\n")
		     (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
374 375 376 377 378 379
	  ;; clean up brace-else-brace and brace-elseif-brace
	  (when (and c-auto-newline
		     (eq last-command-char ?\{)
		     (not (c-in-literal)))
	    (cond
	     ((and (memq 'brace-else-brace c-cleanup-list)
Richard M. Stallman's avatar
Richard M. Stallman committed
380 381 382 383
		   (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t)
		   (progn
		     (setq mbeg (match-beginning 0)
			   mend (match-end 0))
Gerd Moellmann's avatar
Gerd Moellmann committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
		     (eq (match-end 0) here)))
	      (delete-region mbeg mend)
	      (insert "} else {"))
	     ((and (memq 'brace-elseif-brace c-cleanup-list)
		   (progn
		     (goto-char (1- here))
		     (setq mend (point))
		     (skip-chars-backward " \t\n")
		     (setq mbeg (point))
		     (eq (char-before) ?\)))
		   (= (c-backward-token-1 1 t) 0)
		   (eq (char-after) ?\()
		   (progn
		     (setq tmp (point))
		     (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*"
					 nil t))
		   (eq (match-end 0) tmp))
	      (delete-region mbeg mend)
	      (goto-char mbeg)
	      (insert " "))))
Richard M. Stallman's avatar
Richard M. Stallman committed
404 405 406 407 408 409 410 411 412 413
	  (goto-char (- (point-max) pos))
	  )
	;; does a newline go after the brace?
	(if (memq 'after newlines)
	    (progn
	      (newline)
	      ;; update on c-state-cache
	      (let* ((bufpos (- (point) 2))
		     (which (if (eq (char-after bufpos) ?{) 'open 'close))
		     (c-state-cache (c-hack-state bufpos which c-state-cache)))
Gerd Moellmann's avatar
Gerd Moellmann committed
414
		(indent-according-to-mode))))
Richard M. Stallman's avatar
Richard M. Stallman committed
415 416 417 418 419 420 421
	;; blink the paren
	(and (eq last-command-char ?\})
	     old-blink-paren
	     (save-excursion
	       (c-backward-syntactic-ws safepos)
	       (funcall old-blink-paren)))
	))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
422

Richard M. Stallman's avatar
Richard M. Stallman committed
423 424
(defun c-electric-slash (arg)
  "Insert a slash character.
425 426 427 428 429 430 431 432 433

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.

434 435
If a numeric ARG is supplied, point is inside a literal, or
`c-syntactic-indentation' is nil, indentation is inhibited."
436
  (interactive "*P")
437
  (let* ((ch (char-before))
438 439
	 (indentp (and c-syntactic-indentation
		       (not arg)
440 441 442 443 444 445 446 447
		       (eq last-command-char ?/)
		       (or (and (eq ch ?/)
				(not (c-in-literal)))
			   (and (eq ch ?*)
				(c-in-literal)))
		       ))
	 ;; shut this up
	 (c-echo-syntactic-information-p nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
448 449
    (self-insert-command (prefix-numeric-value arg))
    (if indentp
Gerd Moellmann's avatar
Gerd Moellmann committed
450
	(indent-according-to-mode))))
Richard M. Stallman's avatar
Richard M. Stallman committed
451 452 453 454 455

(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.
456 457
If a numeric ARG is supplied, point is inside a literal, or
`c-syntactic-indentation' is nil, indentation is inhibited."
458
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
459 460 461
  (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.
462 463
  (if (and c-syntactic-indentation
	   (not arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
464 465 466 467 468 469 470 471 472
	   (memq (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
473 474
      (let (c-echo-syntactic-information-p) ; shut this up
	(indent-according-to-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
475 476 477 478 479 480 481 482 483 484 485
    ))

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

When semicolon is inserted, the line is re-indented unless a numeric
arg is supplied, point is inside a literal, or there are
486 487
non-whitespace characters on the line following the semicolon, or
`c-syntactic-indentation' is nil.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
488 489 490

Based on the value of `c-cleanup-list', this function cleans up commas
following brace lists and semicolons following defuns."
491
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
492 493 494 495 496 497 498 499 500 501 502
  (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))
    (if (or literal
	    arg
	    (not (looking-at "[ \t]*$")))
	(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
503 504 505
      ;; do all cleanups and newline insertions if c-auto-newline is
      ;; turned on
      (if (not c-auto-newline)
506
	  (if c-syntactic-indentation
Gerd Moellmann's avatar
Gerd Moellmann committed
507
	      (indent-according-to-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	;; 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)
		     (skip-chars-backward " \t\n")
		     (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
525
	(if c-syntactic-indentation
Gerd Moellmann's avatar
Gerd Moellmann committed
526
	    (indent-according-to-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540
	;; 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
	      (progn (newline)
Gerd Moellmann's avatar
Gerd Moellmann committed
541
		     (indent-according-to-mode)))
Richard M. Stallman's avatar
Richard M. Stallman committed
542 543 544 545 546 547 548 549 550 551
	  )))))

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

Also, the line is re-indented unless a numeric ARG is supplied, there
552 553 554
are non-whitespace characters present on the line after the colon, the
colon is inserted inside a literal, or `c-syntactic-indentation' is
nil.
Richard M. Stallman's avatar
Richard M. Stallman committed
555 556 557

This function cleans up double colon scope operators based on the
value of `c-cleanup-list'."
558
  (interactive "*P")
Richard M. Stallman's avatar
Richard M. Stallman committed
559 560
  (let* ((bod (c-point 'bod))
	 (literal (c-in-literal bod))
561
	 syntax newlines is-scope-op
Richard M. Stallman's avatar
Richard M. Stallman committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	 ;; shut this up
	 (c-echo-syntactic-information-p nil))
    (if (or literal
	    arg
	    (not (looking-at "[ \t]*$")))
	(self-insert-command (prefix-numeric-value arg))
      ;; 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)
		   (skip-chars-backward " \t\n")
		   (eq (char-before) ?:))
		 (not (c-in-literal))
		 (not (eq (char-after (- (point) 2)) ?:)))
581 582 583
	    (progn
	      (delete-region (point) (1- here))
	      (setq is-scope-op t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
584 585 586 587 588 589 590 591 592 593 594 595
	(goto-char (- (point-max) pos)))
      ;; lets do some special stuff with the colon character
      (setq syntax (c-guess-basic-syntax)
	    ;; 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
	    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)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
596 597 598 599 600
				     (let ((buffer-undo-list t))
				       (insert "\n")
				       (unwind-protect
					   (c-guess-basic-syntax)
					 (delete-char -1)))
Richard M. Stallman's avatar
Richard M. Stallman committed
601
				     c-hanging-colons-alist))))
602 603
      ;; indent the current line if it's done syntactically.
      (if c-syntactic-indentation
Gerd Moellmann's avatar
Gerd Moellmann committed
604 605
	  (let ((c-syntactic-context syntax))
	    (indent-according-to-mode)))
Richard M. Stallman's avatar
Richard M. Stallman committed
606 607 608 609
      ;; 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)
610
	       (not is-scope-op)
Richard M. Stallman's avatar
Richard M. Stallman committed
611 612 613 614 615 616
	       (save-excursion
		 (skip-chars-backward ": \t")
		 (not (bolp))))
	  (let ((pos (- (point-max) (point))))
	    (forward-char -1)
	    (newline)
Gerd Moellmann's avatar
Gerd Moellmann committed
617
	    (indent-according-to-mode)
Richard M. Stallman's avatar
Richard M. Stallman committed
618 619
	    (goto-char (- (point-max) pos))))
      ;; does a newline go after the colon?
620 621
      (if (and (memq 'after (cdr-safe newlines))
	       (not is-scope-op))
Richard M. Stallman's avatar
Richard M. Stallman committed
622 623
	  (progn
	    (newline)
Gerd Moellmann's avatar
Gerd Moellmann committed
624
	    (indent-according-to-mode)))
Richard M. Stallman's avatar
Richard M. Stallman committed
625 626 627 628
      )))

(defun c-electric-lt-gt (arg)
  "Insert a less-than, or greater-than character.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
629 630
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.
631 632 633
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."
634
  (interactive "*P")
635 636
  (let ((indentp (and c-syntactic-indentation
		      (not arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
637 638 639 640 641 642
		      (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
643
	(indent-according-to-mode))))
Richard M. Stallman's avatar
Richard M. Stallman committed
644

Barry A. Warsaw's avatar
Barry A. Warsaw committed
645 646 647
(defun c-electric-paren (arg)
  "Insert a parenthesis.

Gerd Moellmann's avatar
Gerd Moellmann committed
648 649
Some newline cleanups are done if appropriate; see the variable
`c-cleanup-list'.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
650 651

Also, the line is re-indented unless a numeric ARG is supplied, there
Gerd Moellmann's avatar
Gerd Moellmann committed
652
are non-whitespace characters present on the line after the
653 654
parenthesis, the parenthesis is inserted inside a literal, or
`c-syntactic-indentation' is nil."
Barry A. Warsaw's avatar
Barry A. Warsaw committed
655 656 657 658 659 660 661 662 663 664 665 666 667
  (interactive "*P")
  (let (;; shut this up
	(c-echo-syntactic-information-p nil))
    (if (or arg
	    (c-in-literal (c-point 'bod)))
	(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))
Gerd Moellmann's avatar
Gerd Moellmann committed
668 669 670 671 672 673 674 675 676 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
	(when (looking-at "[ \t]*$")
	  (if c-syntactic-indentation
	      (indent-according-to-mode))
	  (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 ?\()
		       (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\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 "} else if (")))
	      ;; clean up brace-catch-brace
	      (if (and (memq 'brace-catch-brace c-cleanup-list)
		       (eq last-command-char ?\()
		       (re-search-backward "}[ \t\n]*catch[ \t\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 "} catch (")))
	      (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)
		   (insert " ")))
		((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))))
Gerd Moellmann's avatar
Gerd Moellmann committed
725 726
	(if old-blink-paren
	    (funcall old-blink-paren))))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
727

Gerd Moellmann's avatar
Gerd Moellmann committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
(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))))
      (indent-according-to-mode))))
Richard M. Stallman's avatar
Richard M. Stallman committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777


;; 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."
778
  (interactive "*")
Richard M. Stallman's avatar
Richard M. Stallman committed
779 780
  (insert "::"))

781 782 783 784 785 786 787 788 789 790
(defun c-beginning-of-defun (&optional arg)
  "Move backward to the beginning of a defun.
With argument, do it that many times.  Negative arg -N
means move forward to Nth following beginning of defun.
Returns t unless search stops due to beginning or end of buffer.

Unlike the built-in `beginning-of-defun' this tries to be smarter
about finding the char with open-parenthesis syntax that starts the
defun."
  (interactive "p")
Gerd Moellmann's avatar
Gerd Moellmann committed
791
  (unless arg (setq arg 1))
792 793 794 795 796 797 798 799 800 801 802 803 804 805
  (if (< arg 0)
      (c-end-of-defun (- arg))
    (while (> arg 0)
      (let ((state (nreverse (c-parse-state)))
	    prevbod bod)
	(while (and state (not bod))
	  (setq bod (car state)
		state (cdr state))
	  (if (consp bod)
	      (setq prevbod (car bod)
		    bod nil)))
	(cond
	 (bod (goto-char bod))
	 (prevbod (goto-char prevbod))
Gerd Moellmann's avatar
Gerd Moellmann committed
806 807 808 809 810
	 (t (goto-char (point-min))
	    (setq arg 0)))
	(setq arg (1- arg))))
    (c-keep-region-active)
    (= arg 0)))
811 812 813 814

(defun c-end-of-defun (&optional arg)
  "Move forward to next end of defun.  With argument, do it that many times.
Negative argument -N means move back to Nth preceding end of defun.
Gerd Moellmann's avatar
Gerd Moellmann committed
815
Returns t unless search stops due to beginning or end of buffer.
816 817 818 819

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")
Barry A. Warsaw's avatar
Barry A. Warsaw committed
820 821
  (if (not arg)
      (setq arg 1))
822 823 824
  (if (< arg 0)
      (c-beginning-of-defun (- arg))
    (while (> arg 0)
Gerd Moellmann's avatar
Gerd Moellmann committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
      (let ((pos (point))
	    eol)
	(while (and (c-safe (down-list 1) t)
		    (not (eq (char-before) ?{)))
	  ;; skip down into the next defun-block
	  (forward-char -1)
	  (c-forward-sexp))
	(c-beginning-of-defun 1)
	(setq eol (c-point 'eol))
	(c-forward-sexp)
	(if (< eol (point))
	    ;; Don't move to next line for one line defuns.
	    (forward-line 1))
	(when (<= (point) pos)
	  (goto-char (point-max))
	  (setq arg 0))
	(setq arg (1- arg))))
    (c-keep-region-active)
    (= arg 0)))
844

Richard M. Stallman's avatar
Richard M. Stallman committed
845 846 847 848

(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
849 850
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
851 852
\\[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
853 854 855

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
856 857
to search for the syntactic context, and a flag saying whether to do
sentence motion in or near comments and multiline strings."
Richard M. Stallman's avatar
Richard M. Stallman committed
858 859
  (interactive (list (prefix-numeric-value current-prefix-arg)
		     nil t))
860 861 862 863 864 865 866 867 868 869
  (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)
Gerd Moellmann's avatar
Gerd Moellmann committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
		(if (c-forward-comment -1)
		    (setq range (cons (point)
				      (progn (c-forward-comment 1) (point))))
		  (skip-chars-backward " \t\n\r\f")
		  (setq range (point))
		  (setq range
			(if (eq (char-before) ?\")
			    (c-safe (c-backward-sexp 1)
				    (cons (point) range)))))
	      ;; skip-syntax-* doesn't count \n as whitespace..
	      (skip-chars-forward " \t\n\r\f")
	      (if (eq (char-after) ?\")
		  (setq range (cons (point)
				    (progn
				      (c-forward-sexp 1)
				      (point))))
		(setq range (point))
		(setq range (if (c-forward-comment 1)
				(cons range (point))
			      nil))))
890 891 892 893 894 895
	    (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
896 897 898 899 900 901
		   (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
902
	      (let* ((lit-type (c-literal-type range))
Gerd Moellmann's avatar
Gerd Moellmann committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
		     (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)))))))
923
		(save-restriction
Gerd Moellmann's avatar
Gerd Moellmann committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
		  ;; 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
940 941 942 943
		  (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
944 945
			   (/= (point) (point-min))
			   (/= (point) (point-max))
Gerd Moellmann's avatar
Gerd Moellmann committed
946 947
			   (save-excursion
			     (beginning-of-line)
Gerd Moellmann's avatar
Gerd Moellmann committed
948
			     (looking-at line-prefix))
Gerd Moellmann's avatar
Gerd Moellmann committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
			   (>= (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
978 979
			  (when (and (eq lit-type 'c)
				     (eq (point) (point-max)))
Gerd Moellmann's avatar
Gerd Moellmann committed
980
			    (widen)
Gerd Moellmann's avatar
Gerd Moellmann committed
981 982 983
			    (when (or (= (skip-chars-backward " \t") 0)
				      (eq (point) (point-max))
				      (bolp))
Gerd Moellmann's avatar
Gerd Moellmann committed
984
			      (goto-char (cdr range)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
985 986
		      (when (and (eq (point) (point-min))
				 (looking-at "[ \t]*$"))
Gerd Moellmann's avatar
Gerd Moellmann committed
987 988 989
			;; Stop before instead of after the comment
			;; starter if nothing follows it.
			(widen)
Gerd Moellmann's avatar
Gerd Moellmann committed
990 991 992 993
			(goto-char (car range))
			(if (and (eq lit-type 'string) (/= (point) here))
			    (setq count (1+ count)
				  range nil))))))
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		;; 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))
	;; Below we do approximately the same as
Gerd Moellmann's avatar
Gerd Moellmann committed
1007
	;; c-beginning-of-statement-1 and c-end-of-statement-1, and
1008 1009 1010 1011
	;; perhaps they should be changed, but that'd likely break a
	;; lot in cc-engine.
	(goto-char here)
	(if (> count 0)
Gerd Moellmann's avatar
Gerd Moellmann committed
1012 1013 1014 1015
	    (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
1016
		;; into parens.  Also stop before `#' when it's at boi
Gerd Moellmann's avatar
Gerd Moellmann committed
1017
		;; on a line.
Gerd Moellmann's avatar
Gerd Moellmann committed
1018
		(let ((literal-pos (not sentence-flag))
Gerd Moellmann's avatar
Gerd Moellmann committed
1019 1020
		      (large-enough (- (point-max)))
		      last last-below-line)
1021
		  (catch 'done
Gerd Moellmann's avatar
Gerd Moellmann committed
1022 1023
		    (while t
		      (setq last (point))
Gerd Moellmann's avatar
Gerd Moellmann committed
1024 1025 1026 1027 1028
		      (when (and (or (eq (char-after) ?\{)
				     (and (eq (char-after) ?#)
					  (eq (point) (c-point 'boi)))
				     )
				 (/= here last))
Gerd Moellmann's avatar
Gerd Moellmann committed
1029 1030 1031 1032 1033 1034 1035 1036
			(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)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1037
		      (if literal-pos
Gerd Moellmann's avatar
Gerd Moellmann committed
1038 1039 1040 1041 1042
			  (c-forward-comment large-enough)
			(when (c-forward-comment -1)
			  ;; Record position of first comment.
			  (save-excursion
			    (c-forward-comment 1)
Gerd Moellmann's avatar
Gerd Moellmann committed
1043
			    (setq literal-pos (point)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
			  (c-forward-comment large-enough)))
		      (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
1060
			       (if (or (= here last)
Gerd Moellmann's avatar
Gerd Moellmann committed
1061 1062 1063 1064
				       (memq (char-after last) '(?\) ?})))
				   (if (and (eq (char-before) ?})
					    (eq (char-after) ?\;))
				       (backward-char))
1065
				 (goto-char last)
Gerd Moellmann's avatar
Gerd Moellmann committed
1066 1067
				 (throw 'done t))))
			    ((= (char-syntax (char-after)) ?\")
Gerd Moellmann's avatar
Gerd Moellmann committed
1068 1069 1070 1071 1072 1073 1074 1075 1076
			     (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
1077 1078
			    (t (skip-syntax-backward "w_")) ; Speedup only.
			    )))
Gerd Moellmann's avatar
Gerd Moellmann committed
1079 1080 1081 1082 1083
		  (if (and (numberp literal-pos)
			   (< (point) literal-pos))
		      ;; We jumped over a comment or string that
		      ;; should be investigated.
		      (goto-char literal-pos)
Gerd Moellmann's avatar
Gerd Moellmann committed
1084 1085 1086 1087 1088
		    (setq count (1- count))))
	      (error
	       (goto-char (point-min))
	       (setq count 0)))
	  (condition-case nil
Gerd Moellmann's avatar
Gerd Moellmann committed
1089
	      ;; Stop before `{', `}', and `#' when it's at boi on a
Gerd Moellmann's avatar
Gerd Moellmann committed
1090 1091 1092 1093
	      ;; line, but on the other side of the syntactic ws, and
	      ;; after `;', `}' and `};'.  Only stop before `{' if at
	      ;; top level or inside braces, though.  Move by sexps
	      ;; and move into parens.  Also stop at eol of lines
Gerd Moellmann's avatar
Gerd Moellmann committed
1094 1095
	      ;; with `#' at the boi.
	      (let ((literal-pos (not sentence-flag))
Gerd Moellmann's avatar
Gerd Moellmann committed
1096 1097 1098 1099 1100
		    (large-enough (point-max))
		    last)
		(catch 'done
		  (while t
		    (setq last (point))
Gerd Moellmann's avatar
Gerd Moellmann committed
1101
		    (if literal-pos
Gerd Moellmann's avatar
Gerd Moellmann committed
1102 1103 1104 1105
			(c-forward-comment large-enough)
		      (if (progn
			    (skip-chars-forward " \t\n\r\f")
			    ;; Record position of first comment.
Gerd Moellmann's avatar
Gerd Moellmann committed
1106
			    (setq literal-pos (point))
Gerd Moellmann's avatar
Gerd Moellmann committed
1107 1108
			    (c-forward-comment 1))
			  (c-forward-comment large-enough)
Gerd Moellmann's avatar
Gerd Moellmann committed
1109
			(setq literal-pos nil)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
		    (cond ((and (eq (char-after) ?{)
				(not (and c-special-brace-lists
					  (c-looking-at-special-brace-list)))
				(/= here last)
				(save-excursion
				  (or (not (c-safe (up-list -1) t))
				      (= (char-after) ?{))))
			   (goto-char last)
			   (throw 'done t))
			  ((and c-special-brace-lists
				(eq (char-after) ?})
				(save-excursion
				  (and (c-safe (up-list -1) t)
				       (c-looking-at-special-brace-list))))
			   (forward-char 1)
			   (skip-syntax-forward "w_")) ; Speedup only.
			  ((and (eq (char-after) ?})
				(/= here last))
			   (goto-char last)
			   (throw 'done t))
Gerd Moellmann's avatar
Gerd Moellmann committed
1130 1131
			  ((and (eq (char-after) ?#)
				(= (point) (c-point 'boi)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1132 1133 1134 1135 1136 1137 1138 1139 1140
			   (if (= here last)
			       (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t)
				   (goto-char (point-max)))
			     (goto-char last))
			   (throw 'done t))
			  ((looking-at ";\\|};?")
			   (goto-char (match-end 0))
			   (throw 'done t))
			  ((= (char-syntax (char-after)) ?\")
Gerd Moellmann's avatar
Gerd Moellmann committed
1141 1142 1143 1144 1145 1146 1147 1148
			   (let ((beg (point)))
			     (c-forward-sexp)
			     (save-excursion
			       (skip-chars-backward "^\n" beg)
			       (when (> (point) beg)
				 ;; Break at multiline string.
				 (setq literal-pos beg)
				 (throw 'done t)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1149 1150 1151 1152
			  (t
			   (forward-char 1)
			   (skip-syntax-forward "w_")) ; Speedup only.
			  )))
Gerd Moellmann's avatar
Gerd Moellmann committed
1153 1154
		(if (and (numberp literal-pos)
			 (> (point) literal-pos))
Gerd Moellmann's avatar
Gerd Moellmann committed
1155
		    ;; We jumped over a comment that should be investigated.
Gerd Moellmann's avatar
Gerd Moellmann committed
1156
		    (goto-char literal-pos)
Gerd Moellmann's avatar
Gerd Moellmann committed
1157 1158 1159 1160 1161
		  (setq count (1+ count))))
	    (error
	     (goto-char (point-max))
	     (setq count 0)))
	  ))
1162
      ;; If we haven't moved we're near a buffer limit.
Gerd Moellmann's avatar
Gerd Moellmann committed
1163
      (when (and (not (zerop count)) (= (point) here))
1164
	(goto-char (if (> count 0) (point-min) (point-max)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1165
	(setq count 0))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1166 1167 1168 1169
  (c-keep-region-active))

(defun c-end-of-statement (&optional count lim sentence-flag)
  "Go to the end of the innermost C statement.
1170 1171
With prefix arg, go forward N - 1 statements.  Move forward to the end
of the next statement if already at end, and move into nested blocks
Gerd Moellmann's avatar
Gerd Moellmann committed
1172 1173
\(use \\[forward-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
1174 1175 1176

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
1177 1178
to search for the syntactic context, and a flag saying whether to do
sentence motion in or near comments and multiline strings."
Richard M. Stallman's avatar
Richard M. Stallman committed
1179 1180 1181 1182 1183 1184 1185 1186
  (interactive (list (prefix-numeric-value current-prefix-arg)
		     nil t))
  (c-beginning-of-statement (- (or count 1)) lim sentence-flag)
  (c-keep-region-active))


;; set up electric character functions to work with pending-del,
;; (a.k.a. delsel) mode.  All symbols get the t value except
1187
;; the functions which delete, which gets 'supersede.
Richard M. Stallman's avatar
Richard M. Stallman committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
(mapcar
 (function
  (lambda (sym)
    (put sym 'delete-selection t)	; for delsel (Emacs)
    (put sym 'pending-delete t)))	; for pending-del (XEmacs)
 '(c-electric-pound
   c-electric-brace
   c-electric-slash
   c-electric-star
   c-electric-semi&comma
   c-electric-lt-gt
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1199 1200
   c-electric-colon
   c-electric-paren))
1201 1202 1203 1204
(put 'c-electric-delete    'delete-selection 'supersede) ; delsel
(put 'c-electric-delete    'pending-delete   'supersede) ; pending-del
(put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
(put 'c-electric-backspace 'pending-delete   'supersede) ; pending-del
1205 1206
(put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
(put 'c-electric-delete-forward 'pending-delete   'supersede) ; pending-del
Richard M. Stallman's avatar
Richard M. Stallman committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226


;; This is used by indent-for-comment to decide how much to indent a
;; comment in C code based on its context.
(defun c-comment-indent ()
  (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)"))
      0				;Existing comment at bol stays there.
    (let ((opoint (point))
	  placeholder)
      (save-excursion
	(beginning-of-line)
	(cond
	 ;; CASE 1: A comment following a solitary close-brace should
	 ;; have only one space.
	 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|"
			      c-comment-start-regexp
			      "\\)"))
	  (search-forward "}")
	  (1+ (current-column)))
	 ;; CASE 2: 2 spaces after #endif
Gerd Moellmann's avatar
Gerd Moellmann committed
1227 1228
	 ((or (looking-at "[ \t]*#[ \t]*endif[ \t]*")
	      (looking-at "[ \t]*#[ \t]*else[ \t]*"))
Richard M. Stallman's avatar
Richard M. Stallman committed
1229
	  7)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1230 1231 1232
	 ;; CASE 3: when c-indent-comments-syntactically-p is t,
	 ;; calculate the offset according to c-offsets-alist.
	 ;; E.g. identical to hitting TAB.
Richard M. Stallman's avatar
Richard M. Stallman committed
1233 1234 1235
	 ((and c-indent-comments-syntactically-p
	       (save-excursion
		 (skip-chars-forward " \t")
Barry A. Warsaw's avatar
Barry A. Warsaw committed
1236
		 (or (looking-at c-comment-start-regexp)
Richard M. Stallman's avatar
Richard M. Stallman committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
		     (eolp))))
	  (let ((syntax (c-guess-basic-syntax)))
	    ;; BOGOSITY ALERT: if we're looking at the eol, its
	    ;; because indent-for-comment hasn't put the comment-start
	    ;; in the buffer yet.  this will screw up the syntactic
	    ;; analysis so we kludge in the necessary info.  Another
	    ;; kludge is that if we're at the bol, then we really want
	    ;; to ignore any anchoring as specified by
	    ;; c-comment-only-line-offset since it doesn't apply here.
	    (if (save-excursion
Gerd Moellmann's avatar
Gerd Moellmann committed
1247
		  (back-to-indentation)
Richard M. Stallman's avatar
Richard M. Stallman committed
1248 1249 1250 1251 1252 1253 1254
		  (eolp))
		(c-add-syntax 'comment-intro))
	    (let ((c-comment-only-line-offset
		   (if (consp c-comment-only-line-offset)
		       c-comment-only-line-offset
		     (cons c-comment-only-line-offset
			   c-comment-only-line-offset))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1255
	      (c-get-syntactic-indentation syntax))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1256 1257 1258 1259
	 ;; CASE 4: If previous line is a comment-only line, use its
	 ;; indentation if it's greater than comment-column.  Leave at