comparison lisp/progmodes/cc-cmds.el @ 44728:7a3ac6c387fe

CC Mode update to version 5.29. This is for testing; it's not a released version.
author Martin Stjernholm <mast@lysator.liu.se>
date Mon, 22 Apr 2002 00:35:36 +0000
parents 7a94f1c588c4
children 52388a234829
comparison
equal deleted inserted replaced
44727:3936c522b4d3 44728:7a3ac6c387fe
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details. 25 ;; GNU General Public License for more details.
26 26
27 ;; You should have received a copy of the GNU General Public License 27 ;; You should have received a copy of the GNU General Public License
28 ;; along with this program; see the file COPYING. If not, write to 28 ;; along with GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA. 30 ;; Boston, MA 02111-1307, USA.
31 31
32 ;;; Commentary: 32 ;;; Commentary:
33 33
52 (cc-bytecomp-obsolete-fun insert-and-inherit) ; Marked obsolete in XEmacs 19 52 (cc-bytecomp-obsolete-fun insert-and-inherit) ; Marked obsolete in XEmacs 19
53 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge 53 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge
54 ; which looks at this. 54 ; which looks at this.
55 55
56 56
57 (defun c-calculate-state (arg prevstate) 57 (defvar c-fix-backslashes t)
58 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If 58
59 ;; arg is nil or zero, toggle the state. If arg is negative, turn 59 (defun c-shift-line-indentation (shift-amt)
60 ;; the state off, and if arg is positive, turn the state on 60 (let ((pos (- (point-max) (point)))
61 (if (or (not arg) 61 (c-macro-start c-macro-start)
62 (zerop (setq arg (prefix-numeric-value arg)))) 62 tmp-char-inserted)
63 (not prevstate) 63 (if (zerop shift-amt)
64 (> arg 0))) 64 nil
65 65 (when (and (c-query-and-set-macro-start)
66 ;; Auto-newline and hungry-delete 66 (looking-at "[ \t]*\\\\$")
67 (save-excursion
68 (skip-chars-backward " \t")
69 (bolp)))
70 (insert ?x)
71 (backward-char)
72 (setq tmp-char-inserted t))
73 (unwind-protect
74 (let ((col (current-indentation)))
75 (delete-region (c-point 'bol) (c-point 'boi))
76 (beginning-of-line)
77 (indent-to (+ col shift-amt)))
78 (when tmp-char-inserted
79 (delete-char 1))))
80 ;; If initial point was within line's indentation and we're not on
81 ;; a line with a line continuation in a macro, position after the
82 ;; indentation. Else stay at same point in text.
83 (if (and (< (point) (c-point 'boi))
84 (not tmp-char-inserted))
85 (back-to-indentation)
86 (if (> (- (point-max) pos) (point))
87 (goto-char (- (point-max) pos))))))
88
89 (defun c-indent-line (&optional syntax quiet ignore-point-pos)
90 "Indent the current line according to the syntactic context,
91 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
92 syntactic information for the current line. Be silent about syntactic
93 errors if the optional argument QUIET is non-nil, even if
94 `c-report-syntactic-errors' is non-nil. Normally the position of
95 point is used to decide where the old indentation is on a lines that
96 is otherwise empty \(ignoring any line continuation backslash), but
97 that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
98 indentation change \(in columns)."
99 (let ((line-cont-backslash (save-excursion
100 (end-of-line)
101 (eq (char-before) ?\\)))
102 (c-fix-backslashes c-fix-backslashes)
103 bs-col
104 shift-amt)
105 (when (and (not ignore-point-pos)
106 (save-excursion
107 (beginning-of-line)
108 (looking-at (if line-cont-backslash
109 "\\(\\s *\\)\\\\$"
110 "\\(\\s *\\)$")))
111 (<= (point) (match-end 1)))
112 ;; Delete all whitespace after point if there's only whitespace
113 ;; on the line, so that any code that does back-to-indentation
114 ;; or similar gets the current column in this case. If this
115 ;; removes a line continuation backslash it'll be restored
116 ;; at the end.
117 (unless c-auto-align-backslashes
118 ;; Should try to keep the backslash alignment
119 ;; in this case.
120 (save-excursion
121 (goto-char (match-end 0))
122 (setq bs-col (1- (current-column)))))
123 (delete-region (point) (match-end 0))
124 (setq c-fix-backslashes t))
125 (if c-syntactic-indentation
126 (setq c-parsing-error
127 (or (let* ((c-parsing-error nil)
128 (c-syntactic-context (or syntax
129 c-syntactic-context
130 (c-guess-basic-syntax)))
131 indent)
132 (setq indent (c-get-syntactic-indentation
133 c-syntactic-context))
134 (and (not (c-echo-parsing-error quiet))
135 c-echo-syntactic-information-p
136 (message "syntax: %s, indent: %d"
137 c-syntactic-context indent))
138 (setq shift-amt (- indent (current-indentation)))
139 (c-shift-line-indentation shift-amt)
140 (run-hooks 'c-special-indent-hook)
141 c-parsing-error)
142 c-parsing-error))
143 (let ((indent 0))
144 (save-excursion
145 (while (and (= (forward-line -1) 0)
146 (if (looking-at "\\s *\\\\?$")
147 t
148 (setq indent (current-indentation))
149 nil))))
150 (setq shift-amt (- indent (current-indentation)))
151 (c-shift-line-indentation shift-amt)))
152 (when (and c-fix-backslashes line-cont-backslash)
153 (if bs-col
154 (save-excursion
155 (indent-to bs-col)
156 (insert ?\\))
157 (when c-auto-align-backslashes
158 ;; Realign the line continuation backslash.
159 (c-backslash-region (point) (point) nil t))))
160 shift-amt))
161
162 (defun c-newline-and-indent (&optional newline-arg)
163 "Inserts a newline and indents the new line.
164 This function fixes line continuation backslashes if inside a macro,
165 and takes care to set the indentation before calling
166 `indent-according-to-mode', so that lineup functions like
167 `c-lineup-dont-change' works better."
168 ;; FIXME: Backslashes before eol in comments and literals aren't
169 ;; kept intact.
170 (let ((c-macro-start (c-query-macro-start))
171 ;; Avoid calling c-backslash-region from c-indent-line if it's
172 ;; called during the newline call, which can happen due to
173 ;; c-electric-continued-statement, for example. We also don't
174 ;; want any backslash alignment from indent-according-to-mode.
175 (c-fix-backslashes nil)
176 has-backslash insert-backslash
177 start col)
178 (save-excursion
179 (beginning-of-line)
180 (setq start (point))
181 (while (and (looking-at "[ \t]*\\\\?$")
182 (= (forward-line -1) 0)))
183 (setq col (current-indentation)))
184 (when c-macro-start
185 (if (and (eolp) (eq (char-before) ?\\))
186 (setq insert-backslash t
187 has-backslash t)
188 (setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
189 (newline newline-arg)
190 (indent-to col)
191 (when c-macro-start
192 (if insert-backslash
193 (progn
194 ;; The backslash stayed on the previous line. Insert one
195 ;; before calling c-backslash-region, so that
196 ;; bs-col-after-end in it works better. Fixup the
197 ;; backslashes on the newly inserted line.
198 (insert ?\\)
199 (backward-char)
200 (c-backslash-region (point) (point) nil t))
201 ;; The backslash moved to the new line, if there was any. Let
202 ;; c-backslash-region fix a backslash on the previous line,
203 ;; and the one that might be on the new line.
204 ;; c-auto-align-backslashes is intentionally ignored here;
205 ;; maybe the moved backslash should be left alone if it's set,
206 ;; but we fix both lines on the grounds that the old backslash
207 ;; has been moved anyway and is now in a different context.
208 (c-backslash-region start (if has-backslash (point) start) nil t)))
209 (when c-syntactic-indentation
210 ;; Reindent syntactically. The indentation done above is not
211 ;; wasted, since c-indent-line might look at the current
212 ;; indentation.
213 (let ((c-syntactic-context (c-guess-basic-syntax)))
214 ;; We temporarily insert another line break, so that the
215 ;; lineup functions will see the line as empty. That makes
216 ;; e.g. c-lineup-cpp-define more intuitive since it then
217 ;; proceeds to the preceding line in this case.
218 (insert ?\n)
219 (delete-horizontal-space)
220 (setq start (- (point-max) (point)))
221 (unwind-protect
222 (progn
223 (backward-char)
224 (indent-according-to-mode))
225 (goto-char (- (point-max) start))
226 (delete-char -1)))
227 (when has-backslash
228 ;; Must align the backslash again after reindentation. The
229 ;; c-backslash-region call above can't be optimized to ignore
230 ;; this line, since it then won't align correctly with the
231 ;; lines below if the first line in the macro is broken.
232 (c-backslash-region (point) (point) nil t)))))
233
234 (defun c-show-syntactic-information (arg)
235 "Show syntactic information for current line.
236 With universal argument, inserts the analysis as a comment on that line."
237 (interactive "P")
238 (let* ((c-parsing-error nil)
239 (syntax (c-guess-basic-syntax)))
240 (if (not (consp arg))
241 (message "syntactic analysis: %s" syntax)
242 (indent-for-comment)
243 (insert-and-inherit (format "%s" syntax))
244 ))
245 (c-keep-region-active))
246
247 (defun c-syntactic-information-on-region (from to)
248 "Inserts a comment with the syntactic analysis on every line in the region."
249 (interactive "*r")
250 (save-excursion
251 (save-restriction
252 (narrow-to-region from to)
253 (goto-char (point-min))
254 (while (not (eobp))
255 (c-show-syntactic-information '(0))
256 (forward-line)))))
257
258
259 (defun c-toggle-syntactic-indentation (&optional arg)
260 "Toggle syntactic indentation.
261 Optional numeric ARG, if supplied, turns on syntactic indentation when
262 positive, turns it off when negative, and just toggles it when zero or
263 left out.
264
265 When syntactic indentation is turned on (the default), the indentation
266 functions and the electric keys indent according to the syntactic
267 context keys, when applicable.
268
269 When it's turned off, the electric keys does no reindentation, the
270 indentation functions indents every new line to the same level as the
271 previous nonempty line, and \\[c-indent-command] adjusts the
272 indentation in seps specified `c-basic-offset'. The indentation style
273 has no effect in this mode, nor any of the indentation associated
274 variables, e.g. `c-special-indent-hook'.
275
276 This command sets the variable `c-syntactic-indentation'."
277 (interactive "P")
278 (setq c-syntactic-indentation
279 (c-calculate-state arg c-syntactic-indentation))
280 (c-keep-region-active))
281
67 (defun c-toggle-auto-state (&optional arg) 282 (defun c-toggle-auto-state (&optional arg)
68 "Toggle auto-newline feature. 283 "Toggle auto-newline feature.
69 Optional numeric ARG, if supplied, turns on auto-newline when 284 Optional numeric ARG, if supplied, turns on auto-newline when
70 positive, turns it off when negative, and just toggles it when zero or 285 positive, turns it off when negative, and just toggles it when zero or
71 left out. 286 left out.
122 (if (or (not c-hungry-delete-key) 337 (if (or (not c-hungry-delete-key)
123 arg 338 arg
124 (c-in-literal)) 339 (c-in-literal))
125 (funcall c-backspace-function (prefix-numeric-value arg)) 340 (funcall c-backspace-function (prefix-numeric-value arg))
126 (let ((here (point))) 341 (let ((here (point)))
127 (skip-chars-backward " \t\n") 342 (c-skip-ws-backward)
128 (if (/= (point) here) 343 (if (/= (point) here)
129 (delete-region (point) here) 344 (delete-region (point) here)
130 (funcall c-backspace-function 1) 345 (funcall c-backspace-function 1)
131 )))) 346 ))))
132 347
141 (if (or (not c-hungry-delete-key) 356 (if (or (not c-hungry-delete-key)
142 arg 357 arg
143 (c-in-literal)) 358 (c-in-literal))
144 (funcall c-delete-function (prefix-numeric-value arg)) 359 (funcall c-delete-function (prefix-numeric-value arg))
145 (let ((here (point))) 360 (let ((here (point)))
146 (skip-chars-forward " \t\n") 361 (c-skip-ws-forward)
147 (if (/= (point) here) 362 (if (/= (point) here)
148 (delete-region (point) here) 363 (delete-region (point) here)
149 (funcall c-delete-function 1))))) 364 (funcall c-delete-function 1)))))
150 365
151 (defun c-electric-delete (arg) 366 (defun c-electric-delete (arg)
174 389
175 (defun c-electric-pound (arg) 390 (defun c-electric-pound (arg)
176 "Electric pound (`#') insertion. 391 "Electric pound (`#') insertion.
177 Inserts a `#' character specially depending on the variable 392 Inserts a `#' character specially depending on the variable
178 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if 393 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
179 point is inside a literal, nothing special happens." 394 point is inside a literal or a macro, nothing special happens."
180 (interactive "*P") 395 (interactive "*P")
181 (if (or arg 396 (if (or arg
182 (not (memq 'alignleft c-electric-pound-behavior)) 397 (not (memq 'alignleft c-electric-pound-behavior))
183 (save-excursion (skip-chars-backward " \t") (not (bolp))) 398 (save-excursion
399 (skip-chars-backward " \t")
400 (not (bolp)))
401 (save-excursion
402 (and (= (forward-line -1) 0)
403 (progn (end-of-line)
404 (eq (char-before) ?\\))))
184 (c-in-literal)) 405 (c-in-literal))
185 ;; do nothing special 406 ;; do nothing special
186 (self-insert-command (prefix-numeric-value arg)) 407 (self-insert-command (prefix-numeric-value arg))
187 ;; place the pound character at the left edge 408 ;; place the pound character at the left edge
188 (let ((pos (- (point-max) (point))) 409 (let ((pos (- (point-max) (point)))
189 (bolp (bolp))) 410 (bolp (bolp)))
190 (beginning-of-line) 411 (beginning-of-line)
191 (delete-horizontal-space) 412 (delete-horizontal-space)
192 (insert-char last-command-char 1) 413 (insert last-command-char)
193 (and (not bolp) 414 (and (not bolp)
194 (goto-char (- (point-max) pos))) 415 (goto-char (- (point-max) pos)))
195 ))) 416 )))
196 417
197 (defun c-electric-brace (arg) 418 (defun c-electric-brace (arg)
199 420
200 If the auto-newline feature is turned on, as evidenced by the \"/a\" 421 If the auto-newline feature is turned on, as evidenced by the \"/a\"
201 or \"/ah\" string on the mode line, newlines are inserted before and 422 or \"/ah\" string on the mode line, newlines are inserted before and
202 after braces based on the value of `c-hanging-braces-alist'. 423 after braces based on the value of `c-hanging-braces-alist'.
203 424
204 Also, the line is re-indented unless a numeric ARG is supplied, there 425 Also, the line is re-indented unless a numeric ARG is supplied, the
205 are non-whitespace characters present on the line after the brace, the
206 brace is inserted inside a literal, or `c-syntactic-indentation' is 426 brace is inserted inside a literal, or `c-syntactic-indentation' is
207 nil. 427 nil.
208 428
209 This function does various newline cleanups based on the value of 429 This function does various newline cleanups based on the value of
210 `c-cleanup-list'." 430 `c-cleanup-list'."
211 (interactive "*P") 431 (interactive "*P")
212 (let* ((c-state-cache (c-parse-state)) 432 (let* ((safepos (c-safe-position (point) (c-parse-state)))
213 (safepos (c-safe-position (point) c-state-cache)) 433 (literal (c-in-literal safepos))
214 (literal (c-in-literal safepos))) 434 ;; We want to inhibit blinking the paren since this will be
215 ;; if we're in a literal, or we're not at the end of the line, or 435 ;; most disruptive. We'll blink it ourselves later on.
216 ;; a numeric arg is provided, or auto-newlining is turned off, 436 (old-blink-paren blink-paren-function)
217 ;; then just insert the character. 437 blink-paren-function)
218 (if (or literal 438 (cond
219 arg 439 ((or literal arg)
220 (not (looking-at "[ \t]*$"))) 440 (self-insert-command (prefix-numeric-value arg)))
221 (self-insert-command (prefix-numeric-value arg)) 441 ((not (looking-at "[ \t]*\\\\?$"))
442 (self-insert-command (prefix-numeric-value arg))
443 (if c-syntactic-indentation
444 (indent-according-to-mode)))
445 (t
222 (let* ((syms 446 (let* ((syms
223 ;; This is the list of brace syntactic symbols that can 447 ;; This is the list of brace syntactic symbols that can
224 ;; hang. If any new ones are added to c-offsets-alist, 448 ;; hang. If any new ones are added to c-offsets-alist,
225 ;; they should be added here as well. 449 ;; they should be added here as well.
226 '(class-open class-close defun-open defun-close 450 '(class-open class-close defun-open defun-close
231 substatement-open statement-case-open 455 substatement-open statement-case-open
232 extern-lang-open extern-lang-close 456 extern-lang-open extern-lang-close
233 namespace-open namespace-close 457 namespace-open namespace-close
234 inexpr-class-open inexpr-class-close 458 inexpr-class-open inexpr-class-close
235 )) 459 ))
236 ;; we want to inhibit blinking the paren since this will 460 (insertion-point (point))
237 ;; be most disruptive. we'll blink it ourselves later on 461 (preserve-p (and (not (bobp))
238 (old-blink-paren blink-paren-function) 462 (eq ?\ (char-syntax (char-before)))))
239 blink-paren-function 463 ;; shut this up too
240 (insertion-point (point)) 464 (c-echo-syntactic-information-p nil)
241 delete-temp-newline 465 delete-temp-newline syntax newlines)
242 (preserve-p (and (not (bobp)) 466 ;; only insert a newline if there is non-whitespace behind us
243 (eq ?\ (char-syntax (char-before))))) 467 (when (save-excursion
244 ;; shut this up too 468 (skip-chars-backward " \t")
245 (c-echo-syntactic-information-p nil) 469 (not (bolp)))
246 (syntax (progn 470 (setq delete-temp-newline
247 ;; only insert a newline if there is 471 (list (point-marker)))
248 ;; non-whitespace behind us 472 (c-newline-and-indent)
249 (if (save-excursion 473 (setcdr delete-temp-newline (point-marker)))
250 (skip-chars-backward " \t") 474 (unwind-protect
251 (not (bolp))) 475 (progn
252 (progn (newline) 476 (if (eq last-command-char ?{)
253 (setq delete-temp-newline t))) 477 (setq c-state-cache (cons (point) c-state-cache)))
254 (if (eq last-command-char ?{) 478 (self-insert-command (prefix-numeric-value arg))
255 (setq c-state-cache (cons (point) c-state-cache))) 479 (let ((c-syntactic-indentation-in-macros t))
256 (self-insert-command (prefix-numeric-value arg)) 480 ;; Turn on syntactic macro analysis to help with auto
257 ;; state cache doesn't change 481 ;; newlines only.
258 (c-guess-basic-syntax))) 482 (setq syntax (c-guess-basic-syntax)))
259 (newlines (and
260 c-auto-newline
261 (or (c-lookup-lists
262 syms
263 ;; Substitute inexpr-class and class-open
264 ;; or class-close with inexpr-class-open
265 ;; or inexpr-class-close.
266 (if (assq 'inexpr-class syntax)
267 (cond ((assq 'class-open syntax)
268 '((inexpr-class-open)))
269 ((assq 'class-close syntax)
270 '((inexpr-class-close)))
271 (t syntax))
272 syntax)
273 c-hanging-braces-alist)
274 '(ignore before after)))))
275 ;; Do not try to insert newlines around a special (Pike-style)
276 ;; brace list.
277 (if (and c-special-brace-lists
278 (save-excursion
279 (c-safe (if (= (char-before) ?{)
280 (forward-char -1)
281 (c-forward-sexp -1))
282 (c-looking-at-special-brace-list))))
283 (setq newlines nil))
284 ;; If syntax is a function symbol, then call it using the
285 ;; defined semantics.
286 (if (and (not (consp (cdr newlines)))
287 (functionp (cdr newlines)))
288 (let ((c-syntactic-context syntax))
289 (setq newlines 483 (setq newlines
290 (funcall (cdr newlines) (car newlines) insertion-point)))) 484 (and
291 ;; does a newline go before the open brace? 485 c-auto-newline
292 (if (memq 'before newlines) 486 (or (c-lookup-lists
293 ;; we leave the newline we've put in there before, 487 syms
294 ;; but we need to re-indent the line above 488 ;; Substitute inexpr-class and class-open or
295 (let (old-ind 489 ;; class-close with inexpr-class-open or
296 (old-point-max (point-max)) 490 ;; inexpr-class-close.
297 (pos (- (point-max) (point))) 491 (if (assq 'inexpr-class syntax)
298 (here (point))) 492 (cond ((assq 'class-open syntax)
299 (forward-line -1) 493 '((inexpr-class-open)))
300 (setq old-ind (c-point 'boi)) 494 ((assq 'class-close syntax)
301 (let ((c-state-cache (c-whack-state (point) c-state-cache))) 495 '((inexpr-class-close)))
302 ;; we may need to update the cache. this should 496 (t syntax))
303 ;; still be faster than recalculating the state 497 syntax)
304 ;; in many cases 498 c-hanging-braces-alist)
305 (save-excursion 499 '(ignore before after))))
306 (save-restriction 500 ;; Do not try to insert newlines around a special
307 (narrow-to-region here (point)) 501 ;; (Pike-style) brace list.
308 (if (and (c-safe (progn (backward-up-list -1) t)) 502 (if (and c-special-brace-lists
309 (memq (char-before) '(?\) ?})) 503 (save-excursion
310 (progn (widen) 504 (c-safe (if (= (char-before) ?{)
311 (c-safe (progn (c-forward-sexp -1) 505 (forward-char -1)
312 t)))) 506 (c-forward-sexp -1))
313 (setq c-state-cache 507 (c-looking-at-special-brace-list))))
314 (c-hack-state (point) 'open c-state-cache))))) 508 (setq newlines nil))
315 (if c-syntactic-indentation 509 ;; If syntax is a function symbol, then call it using the
316 (indent-according-to-mode))) 510 ;; defined semantics.
317 (setq c-state-cache (c-adjust-state (c-point 'bol) old-point-max 511 (if (and (not (consp (cdr newlines)))
318 (- (c-point 'boi) old-ind) 512 (functionp (cdr newlines)))
319 c-state-cache)) 513 (let ((c-syntactic-context syntax))
320 (goto-char (- (point-max) pos)) 514 (setq newlines
321 ;; if the buffer has changed due to the indentation, we 515 (funcall (cdr newlines)
322 ;; need to recalculate syntax for the current line, but 516 (car newlines)
323 ;; we won't need to update the state cache. 517 insertion-point))))
324 (if (/= (point) here) 518 ;; does a newline go before the open brace?
325 (setq syntax (c-guess-basic-syntax)))) 519 (when (memq 'before newlines)
520 ;; we leave the newline we've put in there before,
521 ;; but we need to re-indent the line above
522 (when delete-temp-newline
523 (set-marker (car delete-temp-newline) nil)
524 (set-marker (cdr delete-temp-newline) nil)
525 (setq delete-temp-newline nil))
526 (when c-syntactic-indentation
527 (let ((pos (- (point-max) (point)))
528 (here (point)))
529 (forward-line -1)
530 (indent-according-to-mode)
531 (goto-char (- (point-max) pos))
532 ;; if the buffer has changed due to the
533 ;; indentation, we need to recalculate syntax for
534 ;; the current line.
535 (if (/= (point) here)
536 (let ((c-syntactic-indentation-in-macros t))
537 ;; Turn on syntactic macro analysis to help
538 ;; with auto newlines only.
539 (setq syntax (c-guess-basic-syntax))))))))
326 ;; must remove the newline we just stuck in (if we really did it) 540 ;; must remove the newline we just stuck in (if we really did it)
327 (and delete-temp-newline 541 (when delete-temp-newline
328 (save-excursion 542 (save-excursion
329 ;; if there is whitespace before point, then preserve 543 (delete-region (car delete-temp-newline)
330 ;; at least one space. 544 (cdr delete-temp-newline))
331 (delete-indentation) 545 (goto-char (car delete-temp-newline))
332 (just-one-space) 546 (set-marker (car delete-temp-newline) nil)
333 (setq c-state-cache (c-whack-state (point) c-state-cache)) 547 (set-marker (cdr delete-temp-newline) nil)
334 (if (not preserve-p) 548 ;; if there is whitespace before point, then preserve
335 (delete-char -1)))) 549 ;; at least one space.
336 ;; since we're hanging the brace, we need to recalculate 550 (just-one-space)
337 ;; syntax. Update the state to accurately reflect the 551 (if (not preserve-p)
338 ;; beginning of the line. We punt if we cross any open or 552 (delete-char -1)))))
339 ;; closed parens because its just too hard to modify the 553 (if (not (memq 'before newlines))
340 ;; known state. This limitation will be fixed in v5. 554 ;; since we're hanging the brace, we need to recalculate
341 (save-excursion 555 ;; syntax.
342 (let ((bol (c-point 'bol))) 556 (let ((c-syntactic-indentation-in-macros t))
343 (if (zerop (car (parse-partial-sexp bol (1- (point))))) 557 ;; Turn on syntactic macro analysis to help with auto
344 (setq syntax (c-guess-basic-syntax)) 558 ;; newlines only.
345 ;; gotta punt. this requires some horrible kludgery 559 (setq syntax (c-guess-basic-syntax))))
346 (beginning-of-line) 560 (when c-syntactic-indentation
347 (setq c-state-cache nil 561 ;; Now adjust the line's indentation. Don't update the state
348 c-state-cache (c-parse-state) 562 ;; cache since c-guess-basic-syntax isn't called when
349 syntax nil)))) 563 ;; c-syntactic-context is set.
350 ) 564 (let* ((c-syntactic-context syntax))
351 ;; Now adjust the line's indentation. Don't update the state 565 (indent-according-to-mode)))
352 ;; cache since c-guess-basic-syntax isn't called when
353 ;; c-syntactic-context is set.
354 (let* ((old-ind (c-point 'boi))
355 (old-point-max (point-max))
356 (c-syntactic-context syntax))
357 (indent-according-to-mode)
358 (setq c-state-cache (c-adjust-state (c-point 'bol) old-point-max
359 (- (c-point 'boi) old-ind)
360 c-state-cache)))
361 ;; Do all appropriate clean ups 566 ;; Do all appropriate clean ups
362 (let ((here (point)) 567 (let ((here (point))
363 (pos (- (point-max) (point))) 568 (pos (- (point-max) (point)))
364 mbeg mend tmp) 569 mbeg mend tmp)
365 ;; clean up empty defun braces 570 ;; clean up empty defun braces
368 (eq last-command-char ?\}) 573 (eq last-command-char ?\})
369 (c-intersect-lists '(defun-close class-close inline-close) 574 (c-intersect-lists '(defun-close class-close inline-close)
370 syntax) 575 syntax)
371 (progn 576 (progn
372 (forward-char -1) 577 (forward-char -1)
373 (skip-chars-backward " \t\n") 578 (c-skip-ws-backward)
374 (eq (char-before) ?\{)) 579 (eq (char-before) ?\{))
375 ;; make sure matching open brace isn't in a comment 580 ;; make sure matching open brace isn't in a comment
376 (not (c-in-literal))) 581 (not (c-in-literal)))
377 (delete-region (point) (1- here))) 582 (delete-region (point) (1- here)))
378 ;; clean up brace-else-brace and brace-elseif-brace 583 ;; clean up brace-else-brace and brace-elseif-brace
379 (when (and c-auto-newline 584 (when (and c-auto-newline
380 (eq last-command-char ?\{) 585 (eq last-command-char ?\{))
381 (not (c-in-literal)))
382 (cond 586 (cond
383 ((and (memq 'brace-else-brace c-cleanup-list) 587 ((and (memq 'brace-else-brace c-cleanup-list)
384 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t) 588 (re-search-backward
589 (concat "}"
590 "\\([ \t\n]\\|\\\\\n\\)*"
591 "else"
592 "\\([ \t\n]\\|\\\\\n\\)*"
593 "{")
594 nil t)
385 (progn 595 (progn
386 (setq mbeg (match-beginning 0) 596 (setq mbeg (match-beginning 0)
387 mend (match-end 0)) 597 mend (match-end 0))
388 (eq (match-end 0) here))) 598 (eq (match-end 0) here)))
389 (delete-region mbeg mend) 599 (delete-region mbeg mend)
390 (insert "} else {")) 600 (insert-and-inherit "} else {"))
391 ((and (memq 'brace-elseif-brace c-cleanup-list) 601 ((and (memq 'brace-elseif-brace c-cleanup-list)
392 (progn 602 (progn
393 (goto-char (1- here)) 603 (goto-char (1- here))
394 (setq mend (point)) 604 (setq mend (point))
395 (skip-chars-backward " \t\n") 605 (c-skip-ws-backward)
396 (setq mbeg (point)) 606 (setq mbeg (point))
397 (eq (char-before) ?\))) 607 (eq (char-before) ?\)))
398 (= (c-backward-token-1 1 t) 0) 608 (= (c-backward-token-1 1 t) 0)
399 (eq (char-after) ?\() 609 (eq (char-after) ?\()
400 (progn 610 (progn
401 (setq tmp (point)) 611 (setq tmp (point))
402 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" 612 (re-search-backward
403 nil t)) 613 (concat "}"
614 "\\([ \t\n]\\|\\\\\n\\)*"
615 "else"
616 "\\([ \t\n]\\|\\\\\n\\)+"
617 "if"
618 "\\([ \t\n]\\|\\\\\n\\)*")
619 nil t))
404 (eq (match-end 0) tmp)) 620 (eq (match-end 0) tmp))
405 (delete-region mbeg mend) 621 (delete-region mbeg mend)
406 (goto-char mbeg) 622 (goto-char mbeg)
407 (insert " ")))) 623 (insert ?\ ))))
408 (goto-char (- (point-max) pos)) 624 (goto-char (- (point-max) pos))
409 ) 625 )
410 ;; does a newline go after the brace? 626 ;; does a newline go after the brace?
411 (if (memq 'after newlines) 627 (if (memq 'after newlines)
412 (progn 628 (c-newline-and-indent))
413 (newline) 629 )))
414 ;; update on c-state-cache 630 ;; blink the paren
415 (let* ((bufpos (- (point) 2)) 631 (and (eq last-command-char ?\})
416 (which (if (eq (char-after bufpos) ?{) 'open 'close)) 632 (not executing-kbd-macro)
417 (c-state-cache (c-hack-state bufpos which c-state-cache))) 633 old-blink-paren
418 (indent-according-to-mode)))) 634 (save-excursion
419 ;; blink the paren 635 (c-backward-syntactic-ws safepos)
420 (and (eq last-command-char ?\}) 636 (funcall old-blink-paren)))))
421 old-blink-paren
422 (save-excursion
423 (c-backward-syntactic-ws safepos)
424 (funcall old-blink-paren)))
425 ))))
426 637
427 (defun c-electric-slash (arg) 638 (defun c-electric-slash (arg)
428 "Insert a slash character. 639 "Insert a slash character.
429 640
430 Indent the line as a comment, if: 641 Indent the line as a comment, if:
437 648
438 If a numeric ARG is supplied, point is inside a literal, or 649 If a numeric ARG is supplied, point is inside a literal, or
439 `c-syntactic-indentation' is nil, indentation is inhibited." 650 `c-syntactic-indentation' is nil, indentation is inhibited."
440 (interactive "*P") 651 (interactive "*P")
441 (let* ((ch (char-before)) 652 (let* ((ch (char-before))
653 (literal (c-in-literal))
442 (indentp (and c-syntactic-indentation 654 (indentp (and c-syntactic-indentation
443 (not arg) 655 (not arg)
444 (eq last-command-char ?/) 656 (eq last-command-char ?/)
445 (or (and (eq ch ?/) 657 (or (and (eq ch ?/)
446 (not (c-in-literal))) 658 (not literal))
447 (and (eq ch ?*) 659 (and (eq ch ?*)
448 (c-in-literal))) 660 literal))
449 )) 661 ))
450 ;; shut this up 662 ;; shut this up
451 (c-echo-syntactic-information-p nil)) 663 (c-echo-syntactic-information-p nil))
452 (self-insert-command (prefix-numeric-value arg)) 664 (self-insert-command (prefix-numeric-value arg))
453 (if indentp 665 (if indentp
463 (self-insert-command (prefix-numeric-value arg)) 675 (self-insert-command (prefix-numeric-value arg))
464 ;; if we are in a literal, or if arg is given do not re-indent the 676 ;; if we are in a literal, or if arg is given do not re-indent the
465 ;; current line, unless this star introduces a comment-only line. 677 ;; current line, unless this star introduces a comment-only line.
466 (if (and c-syntactic-indentation 678 (if (and c-syntactic-indentation
467 (not arg) 679 (not arg)
468 (memq (c-in-literal) '(c)) 680 (eq (c-in-literal) 'c)
469 (eq (char-before) ?*) 681 (eq (char-before) ?*)
470 (save-excursion 682 (save-excursion
471 (forward-char -1) 683 (forward-char -1)
472 (skip-chars-backward "*") 684 (skip-chars-backward "*")
473 (if (eq (char-before) ?/) 685 (if (eq (char-before) ?/)
483 When the auto-newline feature is turned on, as evidenced by the \"/a\" 695 When the auto-newline feature is turned on, as evidenced by the \"/a\"
484 or \"/ah\" string on the mode line, a newline might be inserted. See 696 or \"/ah\" string on the mode line, a newline might be inserted. See
485 the variable `c-hanging-semi&comma-criteria' for how newline insertion 697 the variable `c-hanging-semi&comma-criteria' for how newline insertion
486 is determined. 698 is determined.
487 699
488 When semicolon is inserted, the line is re-indented unless a numeric 700 When a semicolon is inserted, the line is re-indented unless a numeric
489 arg is supplied, point is inside a literal, or there are 701 arg is supplied, point is inside a literal, or
490 non-whitespace characters on the line following the semicolon, or
491 `c-syntactic-indentation' is nil. 702 `c-syntactic-indentation' is nil.
492 703
493 Based on the value of `c-cleanup-list', this function cleans up commas 704 Based on the value of `c-cleanup-list', this function cleans up commas
494 following brace lists and semicolons following defuns." 705 following brace lists and semicolons following defuns."
495 (interactive "*P") 706 (interactive "*P")
496 (let* ((lim (c-most-enclosing-brace (c-parse-state))) 707 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
497 (literal (c-in-literal lim)) 708 (literal (c-in-literal lim))
498 (here (point)) 709 (here (point))
499 ;; shut this up 710 ;; shut this up
500 (c-echo-syntactic-information-p nil)) 711 (c-echo-syntactic-information-p nil))
501 (if (or literal 712 (if (or literal arg)
502 arg
503 (not (looking-at "[ \t]*$")))
504 (self-insert-command (prefix-numeric-value arg)) 713 (self-insert-command (prefix-numeric-value arg))
505 ;; do some special stuff with the character 714 ;; do some special stuff with the character
506 (self-insert-command (prefix-numeric-value arg)) 715 (self-insert-command (prefix-numeric-value arg))
507 ;; do all cleanups and newline insertions if c-auto-newline is 716 ;; do all cleanups and newline insertions if c-auto-newline is
508 ;; turned on 717 ;; turned on
509 (if (not c-auto-newline) 718 (if (or (not c-auto-newline)
719 (not (looking-at "[ \t]*\\\\?$")))
510 (if c-syntactic-indentation 720 (if c-syntactic-indentation
511 (indent-according-to-mode)) 721 (indent-according-to-mode))
512 ;; clean ups 722 ;; clean ups
513 (let ((pos (- (point-max) (point)))) 723 (let ((pos (- (point-max) (point))))
514 (if (and (or (and 724 (if (and (or (and
517 (and 727 (and
518 (eq last-command-char ?\;) 728 (eq last-command-char ?\;)
519 (memq 'defun-close-semi c-cleanup-list))) 729 (memq 'defun-close-semi c-cleanup-list)))
520 (progn 730 (progn
521 (forward-char -1) 731 (forward-char -1)
522 (skip-chars-backward " \t\n") 732 (c-skip-ws-backward)
523 (eq (char-before) ?})) 733 (eq (char-before) ?}))
524 ;; make sure matching open brace isn't in a comment 734 ;; make sure matching open brace isn't in a comment
525 (not (c-in-literal lim))) 735 (not (c-in-literal lim)))
526 (delete-region (point) here)) 736 (delete-region (point) here))
527 (goto-char (- (point-max) pos))) 737 (goto-char (- (point-max) pos)))
539 (setq criteria nil) 749 (setq criteria nil)
540 ;; only 'stop specifically says do not add a newline 750 ;; only 'stop specifically says do not add a newline
541 (setq add-newline-p (not (eq answer 'stop))) 751 (setq add-newline-p (not (eq answer 'stop)))
542 )) 752 ))
543 (if add-newline-p 753 (if add-newline-p
544 (progn (newline) 754 (c-newline-and-indent))
545 (indent-according-to-mode)))
546 ))))) 755 )))))
547 756
548 (defun c-electric-colon (arg) 757 (defun c-electric-colon (arg)
549 "Insert a colon. 758 "Insert a colon.
550 759
551 If the auto-newline feature is turned on, as evidenced by the \"/a\" 760 If the auto-newline feature is turned on, as evidenced by the \"/a\"
552 or \"/ah\" string on the mode line, newlines are inserted before and 761 or \"/ah\" string on the mode line, newlines are inserted before and
553 after colons based on the value of `c-hanging-colons-alist'. 762 after colons based on the value of `c-hanging-colons-alist'.
554 763
555 Also, the line is re-indented unless a numeric ARG is supplied, there 764 Also, the line is re-indented unless a numeric ARG is supplied, the
556 are non-whitespace characters present on the line after the colon, the
557 colon is inserted inside a literal, or `c-syntactic-indentation' is 765 colon is inserted inside a literal, or `c-syntactic-indentation' is
558 nil. 766 nil.
559 767
560 This function cleans up double colon scope operators based on the 768 This function cleans up double colon scope operators based on the
561 value of `c-cleanup-list'." 769 value of `c-cleanup-list'."
562 (interactive "*P") 770 (interactive "*P")
563 (let* ((bod (c-point 'bod)) 771 (let* ((bod (c-point 'bod))
564 (literal (c-in-literal bod)) 772 (literal (c-in-literal bod))
565 syntax newlines is-scope-op 773 newlines is-scope-op
566 ;; shut this up 774 ;; shut this up
567 (c-echo-syntactic-information-p nil)) 775 (c-echo-syntactic-information-p nil))
568 (if (or literal 776 (cond
569 arg 777 ((or literal arg)
570 (not (looking-at "[ \t]*$"))) 778 (self-insert-command (prefix-numeric-value arg)))
571 (self-insert-command (prefix-numeric-value arg)) 779 ((not (looking-at "[ \t]*\\\\?$"))
780 (self-insert-command (prefix-numeric-value arg))
781 (if c-syntactic-indentation
782 (indent-according-to-mode)))
783 (t
572 ;; insert the colon, then do any specified cleanups 784 ;; insert the colon, then do any specified cleanups
573 (self-insert-command (prefix-numeric-value arg)) 785 (self-insert-command (prefix-numeric-value arg))
574 (let ((pos (- (point-max) (point))) 786 (let ((pos (- (point-max) (point)))
575 (here (point))) 787 (here (point)))
576 (if (and c-auto-newline 788 (if (and c-auto-newline
577 (memq 'scope-operator c-cleanup-list) 789 (memq 'scope-operator c-cleanup-list)
578 (eq (char-before) ?:) 790 (eq (char-before) ?:)
579 (progn 791 (progn
580 (forward-char -1) 792 (forward-char -1)
581 (skip-chars-backward " \t\n") 793 (c-skip-ws-backward)
582 (eq (char-before) ?:)) 794 (eq (char-before) ?:))
583 (not (c-in-literal)) 795 (not (c-in-literal))
584 (not (eq (char-after (- (point) 2)) ?:))) 796 (not (eq (char-after (- (point) 2)) ?:)))
585 (progn 797 (progn
586 (delete-region (point) (1- here)) 798 (delete-region (point) (1- here))
587 (setq is-scope-op t))) 799 (setq is-scope-op t)))
588 (goto-char (- (point-max) pos))) 800 (goto-char (- (point-max) pos)))
589 ;; lets do some special stuff with the colon character
590 (setq syntax (c-guess-basic-syntax)
591 ;; some language elements can only be determined by
592 ;; checking the following line. Lets first look for ones
593 ;; that can be found when looking on the line with the
594 ;; colon
595 newlines
596 (and c-auto-newline
597 (or (c-lookup-lists '(case-label label access-label)
598 syntax c-hanging-colons-alist)
599 (c-lookup-lists '(member-init-intro inher-intro)
600 (let ((buffer-undo-list t))
601 (insert "\n")
602 (unwind-protect
603 (c-guess-basic-syntax)
604 (delete-char -1)))
605 c-hanging-colons-alist))))
606 ;; indent the current line if it's done syntactically. 801 ;; indent the current line if it's done syntactically.
607 (if c-syntactic-indentation 802 (if c-syntactic-indentation
608 (let ((c-syntactic-context syntax)) 803 ;; Cannot use the same syntax analysis as we find below,
609 (indent-according-to-mode))) 804 ;; since that's made with c-syntactic-indentation-in-macros
805 ;; always set to t.
806 (indent-according-to-mode))
807 (let* ((c-syntactic-indentation-in-macros t)
808 ;; Turn on syntactic macro analysis to help with auto newlines
809 ;; only.
810 (syntax (c-guess-basic-syntax))
811 (elem syntax))
812 ;; Translate substatement-label to label for this operation.
813 (while elem
814 (if (eq (car (car elem)) 'substatement-label)
815 (setcar (car elem) 'label))
816 (setq elem (cdr elem)))
817 ;; some language elements can only be determined by checking
818 ;; the following line. Lets first look for ones that can be
819 ;; found when looking on the line with the colon
820 (setq newlines
821 (and c-auto-newline
822 (or (c-lookup-lists '(case-label label access-label)
823 syntax c-hanging-colons-alist)
824 (c-lookup-lists '(member-init-intro inher-intro)
825 (let ((buffer-undo-list t))
826 (insert ?\n)
827 (unwind-protect
828 (c-guess-basic-syntax)
829 (delete-char -1)))
830 c-hanging-colons-alist)))))
610 ;; does a newline go before the colon? Watch out for already 831 ;; does a newline go before the colon? Watch out for already
611 ;; non-hung colons. However, we don't unhang them because that 832 ;; non-hung colons. However, we don't unhang them because that
612 ;; would be a cleanup (and anti-social). 833 ;; would be a cleanup (and anti-social).
613 (if (and (memq 'before newlines) 834 (if (and (memq 'before newlines)
614 (not is-scope-op) 835 (not is-scope-op)
615 (save-excursion 836 (save-excursion
616 (skip-chars-backward ": \t") 837 (skip-chars-backward ": \t")
617 (not (bolp)))) 838 (not (bolp))))
618 (let ((pos (- (point-max) (point)))) 839 (let ((pos (- (point-max) (point))))
619 (forward-char -1) 840 (forward-char -1)
620 (newline) 841 (c-newline-and-indent)
621 (indent-according-to-mode)
622 (goto-char (- (point-max) pos)))) 842 (goto-char (- (point-max) pos))))
623 ;; does a newline go after the colon? 843 ;; does a newline go after the colon?
624 (if (and (memq 'after (cdr-safe newlines)) 844 (if (and (memq 'after (cdr-safe newlines))
625 (not is-scope-op)) 845 (not is-scope-op))
626 (progn 846 (c-newline-and-indent))
627 (newline) 847 ))))
628 (indent-according-to-mode)))
629 )))
630 848
631 (defun c-electric-lt-gt (arg) 849 (defun c-electric-lt-gt (arg)
632 "Insert a less-than, or greater-than character. 850 "Insert a less-than, or greater-than character.
633 The line will be re-indented if the character inserted is the second 851 The line will be re-indented if the character inserted is the second
634 of a C++ style stream operator and the buffer is in C++ mode. 852 of a C++ style stream operator and the buffer is in C++ mode.
650 "Insert a parenthesis. 868 "Insert a parenthesis.
651 869
652 Some newline cleanups are done if appropriate; see the variable 870 Some newline cleanups are done if appropriate; see the variable
653 `c-cleanup-list'. 871 `c-cleanup-list'.
654 872
655 Also, the line is re-indented unless a numeric ARG is supplied, there 873 Also, the line is re-indented unless a numeric ARG is supplied, the
656 are non-whitespace characters present on the line after the 874 parenthesis is inserted inside a literal, or `c-syntactic-indentation'
657 parenthesis, the parenthesis is inserted inside a literal, or 875 is nil."
658 `c-syntactic-indentation' is nil."
659 (interactive "*P") 876 (interactive "*P")
660 (let (;; shut this up 877 (let ((literal (c-in-literal (c-point 'bod)))
878 ;; shut this up
661 (c-echo-syntactic-information-p nil)) 879 (c-echo-syntactic-information-p nil))
662 (if (or arg 880 (if (or arg literal)
663 (c-in-literal (c-point 'bod)))
664 (self-insert-command (prefix-numeric-value arg)) 881 (self-insert-command (prefix-numeric-value arg))
665 ;; do some special stuff with the character 882 ;; do some special stuff with the character
666 (let* (;; We want to inhibit blinking the paren since this will 883 (let* (;; We want to inhibit blinking the paren since this will
667 ;; be most disruptive. We'll blink it ourselves 884 ;; be most disruptive. We'll blink it ourselves
668 ;; afterwards. 885 ;; afterwards.
669 (old-blink-paren blink-paren-function) 886 (old-blink-paren blink-paren-function)
670 blink-paren-function) 887 blink-paren-function)
671 (self-insert-command (prefix-numeric-value arg)) 888 (self-insert-command (prefix-numeric-value arg))
672 (when (looking-at "[ \t]*$") 889 (if c-syntactic-indentation
673 (if c-syntactic-indentation 890 (indent-according-to-mode))
674 (indent-according-to-mode)) 891 (when (looking-at "[ \t]*\\\\?$")
675 (when c-auto-newline 892 (when c-auto-newline
676 ;; Do all appropriate clean ups 893 ;; Do all appropriate clean ups
677 (let ((here (point)) 894 (let ((here (point))
678 (pos (- (point-max) (point))) 895 (pos (- (point-max) (point)))
679 mbeg mend) 896 mbeg mend)
680 ;; clean up brace-elseif-brace 897 ;; clean up brace-elseif-brace
681 (if (and (memq 'brace-elseif-brace c-cleanup-list) 898 (if (and (memq 'brace-elseif-brace c-cleanup-list)
682 (eq last-command-char ?\() 899 (eq last-command-char ?\()
683 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*(" 900 (re-search-backward
684 nil t) 901 (concat "}"
902 "\\([ \t\n]\\|\\\\\n\\)*"
903 "else"
904 "\\([ \t\n]\\|\\\\\n\\)+"
905 "if"
906 "\\([ \t\n]\\|\\\\\n\\)*"
907 "(")
908 nil t)
685 (save-excursion 909 (save-excursion
686 (setq mbeg (match-beginning 0) 910 (setq mbeg (match-beginning 0)
687 mend (match-end 0)) 911 mend (match-end 0))
688 (= mend here)) 912 (= mend here))
689 (not (c-in-literal))) 913 (not (c-in-literal)))
690 (progn 914 (progn
691 (delete-region mbeg mend) 915 (delete-region mbeg mend)
692 (insert "} else if ("))) 916 (insert-and-inherit "} else if ("))
693 ;; clean up brace-catch-brace 917 ;; clean up brace-catch-brace
694 (if (and (memq 'brace-catch-brace c-cleanup-list) 918 (goto-char here)
695 (eq last-command-char ?\() 919 (if (and (memq 'brace-catch-brace c-cleanup-list)
696 (re-search-backward "}[ \t\n]*catch[ \t\n]*(" nil t) 920 (eq last-command-char ?\()
697 (save-excursion 921 (re-search-backward
698 (setq mbeg (match-beginning 0) 922 (concat "}"
699 mend (match-end 0)) 923 "\\([ \t\n]\\|\\\\\n\\)*"
700 (= mend here)) 924 "catch"
701 (not (c-in-literal))) 925 "\\([ \t\n]\\|\\\\\n\\)*"
702 (progn 926 "(")
703 (delete-region mbeg mend) 927 nil t)
704 (insert "} catch ("))) 928 (save-excursion
929 (setq mbeg (match-beginning 0)
930 mend (match-end 0))
931 (= mend here))
932 (not (c-in-literal)))
933 (progn
934 (delete-region mbeg mend)
935 (insert-and-inherit "} catch ("))))
705 (goto-char (- (point-max) pos)) 936 (goto-char (- (point-max) pos))
706 ))) 937 )))
707 (let (beg (end (1- (point)))) 938 (let (beg (end (1- (point))))
708 (cond ((and (memq 'space-before-funcall c-cleanup-list) 939 (cond ((and (memq 'space-before-funcall c-cleanup-list)
709 (eq last-command-char ?\() 940 (eq last-command-char ?\()
713 (setq beg (point)) 944 (setq beg (point))
714 (c-on-identifier))) 945 (c-on-identifier)))
715 (save-excursion 946 (save-excursion
716 (delete-region beg end) 947 (delete-region beg end)
717 (goto-char beg) 948 (goto-char beg)
718 (insert " "))) 949 (insert ?\ )))
719 ((and (memq 'compact-empty-funcall c-cleanup-list) 950 ((and (memq 'compact-empty-funcall c-cleanup-list)
720 (eq last-command-char ?\)) 951 (eq last-command-char ?\))
721 (save-excursion 952 (save-excursion
722 (c-safe (backward-char 2)) 953 (c-safe (backward-char 2))
723 (when (looking-at "()") 954 (when (looking-at "()")
724 (setq end (point)) 955 (setq end (point))
725 (skip-chars-backward " \t") 956 (skip-chars-backward " \t")
726 (setq beg (point)) 957 (setq beg (point))
727 (c-on-identifier)))) 958 (c-on-identifier))))
728 (delete-region beg end)))) 959 (delete-region beg end))))
729 (if old-blink-paren 960 (and (not executing-kbd-macro)
730 (funcall old-blink-paren)))))) 961 old-blink-paren
962 (funcall old-blink-paren))))))
731 963
732 (defun c-electric-continued-statement () 964 (defun c-electric-continued-statement ()
733 "Reindent the current line if appropriate. 965 "Reindent the current line if appropriate.
734 966
735 This function is used to reindent the line after a keyword which 967 This function is used to reindent the line after a keyword which
746 (= (save-excursion 978 (= (save-excursion
747 (skip-syntax-backward "w") 979 (skip-syntax-backward "w")
748 (point)) 980 (point))
749 (c-point 'boi)) 981 (c-point 'boi))
750 (not (c-in-literal (c-point 'bod)))) 982 (not (c-in-literal (c-point 'bod))))
751 (indent-according-to-mode)))) 983 ;; Have to temporarily insert a space so that
984 ;; c-guess-basic-syntax recognizes the keyword. Follow the
985 ;; space with a nonspace to avoid messing up any whitespace
986 ;; sensitive meddling that might be done, e.g. by
987 ;; `c-backslash-region'.
988 (insert-and-inherit " x")
989 (unwind-protect
990 (indent-according-to-mode)
991 (delete-char -2)))))
752 992
753 993
754 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus 994 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
755 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com 995 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
756 (defun c-forward-into-nomenclature (&optional arg) 996 (defun c-forward-into-nomenclature (&optional arg)
778 1018
779 (defun c-scope-operator () 1019 (defun c-scope-operator ()
780 "Insert a double colon scope operator at point. 1020 "Insert a double colon scope operator at point.
781 No indentation or other \"electric\" behavior is performed." 1021 No indentation or other \"electric\" behavior is performed."
782 (interactive "*") 1022 (interactive "*")
783 (insert "::")) 1023 (insert-and-inherit "::"))
784 1024
785 (defun c-beginning-of-defun (&optional arg) 1025 (defun c-beginning-of-defun (&optional arg)
786 "Move backward to the beginning of a defun. 1026 "Move backward to the beginning of a defun.
787 With argument, do it that many times. Negative arg -N 1027 Every top level declaration that contains a brace paren block is
788 means move forward to Nth following beginning of defun. 1028 considered to be a defun.
789 Returns t unless search stops due to beginning or end of buffer. 1029
1030 With a positive argument, move backward that many defuns. A negative
1031 argument -N means move forward to the Nth following beginning. Return
1032 t unless search stops due to beginning or end of buffer.
790 1033
791 Unlike the built-in `beginning-of-defun' this tries to be smarter 1034 Unlike the built-in `beginning-of-defun' this tries to be smarter
792 about finding the char with open-parenthesis syntax that starts the 1035 about finding the char with open-parenthesis syntax that starts the
793 defun." 1036 defun."
1037
794 (interactive "p") 1038 (interactive "p")
795 (unless arg (setq arg 1)) 1039 (or arg (setq arg 1))
1040
796 (if (< arg 0) 1041 (if (< arg 0)
797 (c-end-of-defun (- arg)) 1042 (when (c-end-of-defun (- arg))
798 (while (> arg 0) 1043 (c-forward-syntactic-ws)
799 (let ((state (nreverse (c-parse-state))) 1044 t)
800 prevbod bod) 1045
801 (while (and state (not bod)) 1046 (catch 'exit
802 (setq bod (car state) 1047 (while (> arg 0)
803 state (cdr state)) 1048 ;; Note: Partial code duplication in `c-end-of-defun' and
804 (if (consp bod) 1049 ;; `c-declaration-limits'.
805 (setq prevbod (car bod) 1050
806 bod nil))) 1051 (let ((paren-state (c-parse-state)) lim pos)
807 (cond 1052 (unless (c-safe
808 (bod (goto-char bod)) 1053 (goto-char (c-least-enclosing-brace paren-state))
809 (prevbod (goto-char prevbod)) 1054 ;; If we moved to the outermost enclosing paren
810 (t (goto-char (point-min)) 1055 ;; then we can use c-safe-position to set the
811 (setq arg 0))) 1056 ;; limit. Can't do that otherwise since the
812 (setq arg (1- arg)))) 1057 ;; earlier paren pair on paren-state might very
1058 ;; well be part of the declaration we should go
1059 ;; to.
1060 (setq lim (c-safe-position (point) paren-state))
1061 t)
1062 ;; At top level. Make sure we aren't inside a literal.
1063 (setq pos (c-literal-limits
1064 (c-safe-position (point) paren-state)))
1065 (if pos (goto-char (car pos))))
1066
1067 (while (let ((start (point)))
1068 (c-beginning-of-decl-1 lim)
1069 (if (= (point) start)
1070 ;; Didn't move. Might be due to bob or unbalanced
1071 ;; parens. Try to continue if it's the latter.
1072 (unless (c-safe (goto-char
1073 (c-down-list-backward (point))))
1074 ;; Didn't work, so it's bob then.
1075 (goto-char (point-min))
1076 (throw 'exit nil)))
1077
1078 (save-excursion
1079 ;; Check if the declaration contains a brace
1080 ;; block. If not, we try another one.
1081 (setq pos (point))
1082 (not (and (c-syntactic-re-search-forward
1083 "[;{]" nil t 1 t)
1084 (or (eq (char-before) ?{)
1085 (and c-recognize-knr-p
1086 ;; Might have stopped on the
1087 ;; ';' in a K&R argdecl. In
1088 ;; that case the declaration
1089 ;; should contain a block.
1090 (c-in-knr-argdecl pos)))))))
1091 (setq lim nil))
1092
1093 ;; Check if `c-beginning-of-decl-1' put us after the block
1094 ;; in a declaration that doesn't end there. We're searching
1095 ;; back and forth over the block here, which can be
1096 ;; expensive.
1097 (setq pos (point))
1098 (if (and c-opt-block-decls-with-vars-key
1099 (progn
1100 (c-backward-syntactic-ws)
1101 (eq (char-before) ?}))
1102 (eq (car (c-beginning-of-decl-1))
1103 'previous)
1104 (save-excursion
1105 (c-end-of-decl-1)
1106 (> (point) pos)))
1107 nil
1108 (goto-char pos))
1109
1110 (setq pos (point))
1111 ;; Try to be line oriented; position point at the closest
1112 ;; preceding boi that isn't inside a comment, but if we hit
1113 ;; the previous declaration then we use the current point
1114 ;; instead.
1115 (while (and (/= (point) (c-point 'boi))
1116 (c-forward-comment -1)))
1117 (if (/= (point) (c-point 'boi))
1118 (goto-char pos))
1119
1120 (setq arg (1- arg)))))
813 (c-keep-region-active) 1121 (c-keep-region-active)
814 (= arg 0))) 1122 (= arg 0)))
815 1123
816 (defun c-end-of-defun (&optional arg) 1124 (defun c-end-of-defun (&optional arg)
817 "Move forward to next end of defun. With argument, do it that many times. 1125 "Move forward to the end of a top level declaration.
818 Negative argument -N means move back to Nth preceding end of defun. 1126 With argument, do it that many times. Negative argument -N means move
819 Returns t unless search stops due to beginning or end of buffer. 1127 back to Nth preceding end. Returns t unless search stops due to
1128 beginning or end of buffer.
820 1129
821 An end of a defun occurs right after the close-parenthesis that matches 1130 An end of a defun occurs right after the close-parenthesis that matches
822 the open-parenthesis that starts a defun; see `beginning-of-defun'." 1131 the open-parenthesis that starts a defun; see `beginning-of-defun'."
1132
823 (interactive "p") 1133 (interactive "p")
824 (if (not arg) 1134 (or arg (setq arg 1))
825 (setq arg 1)) 1135
826 (if (< arg 0) 1136 (if (< arg 0)
827 (c-beginning-of-defun (- arg)) 1137 (when (c-beginning-of-defun (- arg))
828 (while (> arg 0) 1138 (c-backward-syntactic-ws)
829 (let ((pos (point)) 1139 t)
830 eol) 1140
831 (while (and (c-safe (down-list 1) t) 1141 (catch 'exit
832 (not (eq (char-before) ?{))) 1142 (while (> arg 0)
833 ;; skip down into the next defun-block 1143 ;; Note: Partial code duplication in `c-beginning-of-defun'
834 (forward-char -1) 1144 ;; and `c-declaration-limits'.
835 (c-forward-sexp)) 1145
836 (c-beginning-of-defun 1) 1146 (let ((paren-state (c-parse-state)) lim pos)
837 (setq eol (c-point 'eol)) 1147 (unless (c-safe
838 (c-forward-sexp) 1148 (goto-char (c-least-enclosing-brace paren-state))
839 (if (< eol (point)) 1149 ;; If we moved to the outermost enclosing paren
840 ;; Don't move to next line for one line defuns. 1150 ;; then we can use c-safe-position to set the
841 (forward-line 1)) 1151 ;; limit. Can't do that otherwise since the
842 (when (<= (point) pos) 1152 ;; earlier paren pair on paren-state might very
843 (goto-char (point-max)) 1153 ;; well be part of the declaration we should go
844 (setq arg 0)) 1154 ;; to.
845 (setq arg (1- arg)))) 1155 (setq lim (c-safe-position (point) paren-state))
1156 t)
1157 ;; At top level. Make sure we aren't inside a literal.
1158 (setq pos (car-safe (c-literal-limits
1159 (c-safe-position (point) paren-state))))
1160 (if pos (goto-char pos)))
1161
1162 ;; Have to move to the start first so that `c-end-of-decl-1'
1163 ;; has the correct start position.
1164 (setq pos (point))
1165 (when (memq (car (c-beginning-of-decl-1 lim))
1166 '(previous macro))
1167 ;; We moved back over the previous defun or a macro. Move
1168 ;; to the next token; it's the start of the next
1169 ;; declaration. We can also be directly after the block
1170 ;; in a `c-opt-block-decls-with-vars-key' declaration, but
1171 ;; then we won't move significantly far here.
1172 (goto-char pos)
1173 (c-forward-token-1 0))
1174
1175 (while (let ((start (point)))
1176 (c-end-of-decl-1)
1177 (if (= (point) start)
1178 ;; Didn't move. Might be due to eob or unbalanced
1179 ;; parens. Try to continue if it's the latter.
1180 (if (c-safe (goto-char (c-up-list-forward (point))))
1181 t
1182 ;; Didn't work, so it's eob then.
1183 (goto-char (point-max))
1184 (throw 'exit nil))
1185
1186 (save-excursion
1187 ;; Check if the declaration contains a brace
1188 ;; block. If not, we try another one.
1189 (setq pos (point))
1190 (goto-char start)
1191 (not (c-syntactic-re-search-forward "{" pos t 1 t))))))
1192
1193 (setq pos (point))
1194 ;; Try to be line oriented; position point after the next
1195 ;; newline that isn't inside a comment, but if we hit the
1196 ;; next declaration then we use the current point instead.
1197 (while (and (not (bolp))
1198 (not (looking-at "\\s *$"))
1199 (c-forward-comment 1)))
1200 (cond ((bolp))
1201 ((looking-at "\\s *$")
1202 (forward-line 1))
1203 (t
1204 (goto-char pos)))
1205
1206 (setq arg (1- arg)))))
846 (c-keep-region-active) 1207 (c-keep-region-active)
847 (= arg 0))) 1208 (= arg 0)))
1209
1210 (defun c-declaration-limits (near)
1211 ;; Return a cons of the beginning and end positions of the current
1212 ;; top level declaration or macro. If point is not inside any then
1213 ;; nil is returned, unless NEAR is non-nil in which case the closest
1214 ;; following one is chosen instead (if there is any). The end
1215 ;; position is at the next line, providing there is one before the
1216 ;; declaration.
1217 (save-excursion
1218
1219 ;; Note: Some code duplication in `c-beginning-of-defun' and
1220 ;; `c-end-of-defun'.
1221 (catch 'exit
1222 (let ((start (point))
1223 (paren-state (c-parse-state))
1224 lim pos end-pos)
1225 (unless (c-safe
1226 (goto-char (c-least-enclosing-brace paren-state))
1227 ;; If we moved to the outermost enclosing paren then we
1228 ;; can use c-safe-position to set the limit. Can't do
1229 ;; that otherwise since the earlier paren pair on
1230 ;; paren-state might very well be part of the
1231 ;; declaration we should go to.
1232 (setq lim (c-safe-position (point) paren-state))
1233 t)
1234 ;; At top level. Make sure we aren't inside a literal.
1235 (setq pos (c-literal-limits
1236 (c-safe-position (point) paren-state)))
1237 (if pos (goto-char (car pos))))
1238
1239 (when (c-beginning-of-macro)
1240 (throw 'exit
1241 (cons (point)
1242 (save-excursion
1243 (c-end-of-macro)
1244 (forward-line 1)
1245 (point)))))
1246
1247 (setq pos (point))
1248 (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1249 (= pos (point)))
1250 ;; We moved back over the previous defun. Skip to the next
1251 ;; one. Not using c-forward-syntactic-ws here since we
1252 ;; should not skip a macro. We can also be directly after
1253 ;; the block in a `c-opt-block-decls-with-vars-key'
1254 ;; declaration, but then we won't move significantly far
1255 ;; here.
1256 (goto-char pos)
1257 (while (c-forward-comment 10))
1258
1259 (when (and near (c-beginning-of-macro))
1260 (throw 'exit
1261 (cons (point)
1262 (save-excursion
1263 (c-end-of-macro)
1264 (forward-line 1)
1265 (point))))))
1266
1267 (if (eobp) (throw 'exit nil))
1268
1269 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1270 ;; declaration that doesn't end there. We're searching back and
1271 ;; forth over the block here, which can be expensive.
1272 (setq pos (point))
1273 (if (and c-opt-block-decls-with-vars-key
1274 (progn
1275 (c-backward-syntactic-ws)
1276 (eq (char-before) ?}))
1277 (eq (car (c-beginning-of-decl-1))
1278 'previous)
1279 (save-excursion
1280 (c-end-of-decl-1)
1281 (and (> (point) pos)
1282 (setq end-pos (point)))))
1283 nil
1284 (goto-char pos))
1285
1286 (if (and (not near) (> (point) start))
1287 nil
1288
1289 ;; Try to be line oriented; position the limits at the
1290 ;; closest preceding boi, and after the next newline, that
1291 ;; isn't inside a comment, but if we hit a neighboring
1292 ;; declaration then we instead use the exact declaration
1293 ;; limit in that direction.
1294 (cons (progn
1295 (setq pos (point))
1296 (while (and (/= (point) (c-point 'boi))
1297 (c-forward-comment -1)))
1298 (if (/= (point) (c-point 'boi))
1299 pos
1300 (point)))
1301 (progn
1302 (if end-pos
1303 (goto-char end-pos)
1304 (c-end-of-decl-1))
1305 (setq pos (point))
1306 (while (and (not (bolp))
1307 (not (looking-at "\\s *$"))
1308 (c-forward-comment 1)))
1309 (cond ((bolp)
1310 (point))
1311 ((looking-at "\\s *$")
1312 (forward-line 1)
1313 (point))
1314 (t
1315 pos)))))
1316 ))))
1317
1318 (defun c-mark-function ()
1319 "Put mark at end of the current top-level declaration or macro, point at beginning.
1320 If point is not inside any then the closest following one is chosen.
1321
1322 As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
1323 function does not require the declaration to contain a brace block."
1324 (interactive)
1325
1326 ;; We try to be line oriented, unless there are several
1327 ;; declarations on the same line.
1328 (if (looking-at c-syntactic-eol)
1329 (c-backward-token-1 1 nil (c-point 'bol)))
1330
1331 (let ((decl-limits (c-declaration-limits t)))
1332 (if (not decl-limits)
1333 (error "Cannot find any declaration")
1334 (goto-char (car decl-limits))
1335 (push-mark (cdr decl-limits) nil t))))
848 1336
849 1337
850 (defun c-beginning-of-statement (&optional count lim sentence-flag) 1338 (defun c-beginning-of-statement (&optional count lim sentence-flag)
851 "Go to the beginning of the innermost C statement. 1339 "Go to the beginning of the innermost C statement.
852 With prefix arg, go back N - 1 statements. If already at the 1340 With prefix arg, go back N - 1 statements. If already at the
856 comment or multiline string, move by sentences instead of statements. 1344 comment or multiline string, move by sentences instead of statements.
857 1345
858 When called from a program, this function takes 3 optional args: the 1346 When called from a program, this function takes 3 optional args: the
859 repetition count, a buffer position limit which is the farthest back 1347 repetition count, a buffer position limit which is the farthest back
860 to search for the syntactic context, and a flag saying whether to do 1348 to search for the syntactic context, and a flag saying whether to do
861 sentence motion in or near comments and multiline strings." 1349 sentence motion in or near comments and multiline strings.
1350
1351 Note that `c-beginning-of-statement-1' is usually better to use from
1352 programs. It has much more well defined semantics than this one,
1353 which is intended for interactive use and might therefore change to be
1354 more \"DWIM:ey\"."
862 (interactive (list (prefix-numeric-value current-prefix-arg) 1355 (interactive (list (prefix-numeric-value current-prefix-arg)
863 nil t)) 1356 nil t))
864 (let* ((count (or count 1)) 1357 (let* ((count (or count 1))
865 here 1358 here
866 (range (c-collect-line-comments (c-literal-limits lim)))) 1359 (range (c-collect-line-comments (c-literal-limits lim))))
869 (setq here (point)) 1362 (setq here (point))
870 (if (and (not range) sentence-flag) 1363 (if (and (not range) sentence-flag)
871 (save-excursion 1364 (save-excursion
872 ;; Find the comment next to point if we're not in one. 1365 ;; Find the comment next to point if we're not in one.
873 (if (> count 0) 1366 (if (> count 0)
874 (if (c-forward-comment -1) 1367 (if (c-forward-comment-lc -1)
875 (setq range (cons (point) 1368 (setq range (cons (point)
876 (progn (c-forward-comment 1) (point)))) 1369 (progn (c-forward-comment-lc 1)
877 (skip-chars-backward " \t\n\r\f") 1370 (point))))
1371 (c-skip-ws-backward)
878 (setq range (point)) 1372 (setq range (point))
879 (setq range 1373 (setq range
880 (if (eq (char-before) ?\") 1374 (if (eq (char-before) ?\")
881 (c-safe (c-backward-sexp 1) 1375 (c-safe (c-backward-sexp 1)
882 (cons (point) range))))) 1376 (cons (point) range)))))
883 ;; skip-syntax-* doesn't count \n as whitespace.. 1377 (c-skip-ws-forward)
884 (skip-chars-forward " \t\n\r\f")
885 (if (eq (char-after) ?\") 1378 (if (eq (char-after) ?\")
886 (setq range (cons (point) 1379 (setq range (cons (point)
887 (progn 1380 (progn
888 (c-forward-sexp 1) 1381 (c-forward-sexp 1)
889 (point)))) 1382 (point))))
890 (setq range (point)) 1383 (setq range (point))
891 (setq range (if (c-forward-comment 1) 1384 (setq range (if (c-forward-comment-lc 1)
892 (cons range (point)) 1385 (cons range (point))
893 nil)))) 1386 nil))))
894 (setq range (c-collect-line-comments range)))) 1387 (setq range (c-collect-line-comments range))))
895 (if (and (< count 0) (= here (point-max))) 1388 (if (and (< count 0) (= here (point-max)))
896 ;; Special case because eob might be in a literal. 1389 ;; Special case because eob might be in a literal.
985 (when (or (= (skip-chars-backward " \t") 0) 1478 (when (or (= (skip-chars-backward " \t") 0)
986 (eq (point) (point-max)) 1479 (eq (point) (point-max))
987 (bolp)) 1480 (bolp))
988 (goto-char (cdr range))))) 1481 (goto-char (cdr range)))))
989 (when (and (eq (point) (point-min)) 1482 (when (and (eq (point) (point-min))
990 (looking-at "[ \t]*$")) 1483 (looking-at "[ \t]*\\\\?$"))
991 ;; Stop before instead of after the comment 1484 ;; Stop before instead of after the comment
992 ;; starter if nothing follows it. 1485 ;; starter if nothing follows it.
993 (widen) 1486 (widen)
994 (goto-char (car range)) 1487 (goto-char (car range))
995 (if (and (eq lit-type 'string) (/= (point) here)) 1488 (if (and (eq lit-type 'string) (/= (point) here))
1005 (setq count (1+ count)) 1498 (setq count (1+ count))
1006 (goto-char (cdr range)) 1499 (goto-char (cdr range))
1007 (setq range nil)))) 1500 (setq range nil))))
1008 (goto-char (if (> count 0) (car range) (cdr range))) 1501 (goto-char (if (> count 0) (car range) (cdr range)))
1009 (setq range nil)) 1502 (setq range nil))
1010 ;; Below we do approximately the same as
1011 ;; c-beginning-of-statement-1 and c-end-of-statement-1, and
1012 ;; perhaps they should be changed, but that'd likely break a
1013 ;; lot in cc-engine.
1014 (goto-char here) 1503 (goto-char here)
1015 (if (> count 0) 1504 (if (> count 0)
1016 (condition-case nil 1505 (condition-case nil
1017 ;; Stop before `{' and after `;', `{', `}' and `};' 1506 ;; Stop before `{' and after `;', `{', `}' and `};'
1018 ;; when not followed by `}' or `)', but on the other 1507 ;; when not followed by `}' or `)', but on the other
1036 (if (and (eq (char-after) ?#) 1525 (if (and (eq (char-after) ?#)
1037 (numberp last-below-line) 1526 (numberp last-below-line)
1038 (not (eq last-below-line here))) 1527 (not (eq last-below-line here)))
1039 (goto-char last-below-line)) 1528 (goto-char last-below-line))
1040 (throw 'done t))) 1529 (throw 'done t)))
1530 ;; Don't know why I added the following, but it
1531 ;; doesn't work when point is preceded by a line
1532 ;; style comment. /mast
1533 ;;(c-skip-ws-backward)
1041 (if literal-pos 1534 (if literal-pos
1042 (c-forward-comment large-enough) 1535 (c-forward-comment-lc large-enough)
1043 (when (c-forward-comment -1) 1536 (when (c-forward-comment-lc -1)
1044 ;; Record position of first comment. 1537 ;; Record position of first comment.
1045 (save-excursion 1538 (save-excursion
1046 (c-forward-comment 1) 1539 (c-forward-comment-lc 1)
1047 (setq literal-pos (point))) 1540 (setq literal-pos (point)))
1048 (c-forward-comment large-enough))) 1541 (c-forward-comment-lc large-enough)))
1049 (unless last-below-line 1542 (unless last-below-line
1050 (if (save-excursion 1543 (if (save-excursion
1051 (re-search-forward "\\(^\\|[^\\]\\)$" last t)) 1544 (re-search-forward "\\(^\\|[^\\]\\)$" last t))
1052 (setq last-below-line last))) 1545 (setq last-below-line last)))
1053 (cond ((bobp) ; Must handle bob specially. 1546 (cond ((bobp) ; Must handle bob specially.
1101 last) 1594 last)
1102 (catch 'done 1595 (catch 'done
1103 (while t 1596 (while t
1104 (setq last (point)) 1597 (setq last (point))
1105 (if literal-pos 1598 (if literal-pos
1106 (c-forward-comment large-enough) 1599 (c-forward-comment-lc large-enough)
1107 (if (progn 1600 (if (progn
1108 (skip-chars-forward " \t\n\r\f") 1601 (c-skip-ws-forward)
1109 ;; Record position of first comment. 1602 ;; Record position of first comment.
1110 (setq literal-pos (point)) 1603 (setq literal-pos (point))
1111 (c-forward-comment 1)) 1604 (c-forward-comment-lc 1))
1112 (c-forward-comment large-enough) 1605 (c-forward-comment-lc large-enough)
1113 (setq literal-pos nil))) 1606 (setq literal-pos nil)))
1114 (cond ((and (eq (char-after) ?{) 1607 (cond ((and (eq (char-after) ?{)
1115 (not (and c-special-brace-lists 1608 (not (and c-special-brace-lists
1116 (c-looking-at-special-brace-list))) 1609 (c-looking-at-special-brace-list)))
1117 (/= here last) 1610 (/= here last)
1129 (skip-syntax-forward "w_")) ; Speedup only. 1622 (skip-syntax-forward "w_")) ; Speedup only.
1130 ((and (eq (char-after) ?}) 1623 ((and (eq (char-after) ?})
1131 (/= here last)) 1624 (/= here last))
1132 (goto-char last) 1625 (goto-char last)
1133 (throw 'done t)) 1626 (throw 'done t))
1134 ((and (eq (char-after) ?#) 1627 ; ((and (eq (char-after) ?#)
1135 (= (point) (c-point 'boi))) 1628 ; (= (point) (c-point 'boi)))
1136 (if (= here last) 1629 ; (if (= here last)
1137 (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t) 1630 ; (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t)
1138 (goto-char (point-max))) 1631 ; (goto-char (point-max)))
1139 (goto-char last)) 1632 ; (goto-char last))
1140 (throw 'done t)) 1633 ; (throw 'done t))
1141 ((looking-at ";\\|};?") 1634 ((looking-at ";\\|};?")
1142 (goto-char (match-end 0)) 1635 (goto-char (match-end 0))
1143 (throw 'done t)) 1636 (throw 'done t))
1144 ((= (char-syntax (char-after)) ?\") 1637 ((= (char-syntax (char-after)) ?\")
1145 (let ((beg (point))) 1638 (let ((beg (point)))
1208 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del 1701 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
1209 (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel 1702 (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
1210 (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del 1703 (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
1211 1704
1212 1705
1213 ;; This is used by indent-for-comment to decide how much to indent a 1706 (defun c-calc-comment-indent (entry)
1214 ;; comment in C code based on its context. 1707 (if (symbolp entry)
1708 (setq entry (or (assq entry c-indent-comment-alist)
1709 (assq 'other c-indent-comment-alist)
1710 '(default . (column . nil)))))
1711 (let ((action (car (cdr entry)))
1712 (value (cdr (cdr entry)))
1713 (col (current-column)))
1714 (cond ((eq action 'space)
1715 (+ col value))
1716 ((eq action 'column)
1717 (unless value (setq value comment-column))
1718 (if (bolp)
1719 ;; Do not pad with one space if we're at bol.
1720 value
1721 (max (1+ col) value)))
1722 ((eq action 'align)
1723 (or (save-excursion
1724 (beginning-of-line)
1725 (unless (bobp)
1726 (backward-char)
1727 (let ((lim (c-literal-limits (c-point 'bol) t)))
1728 (when (consp lim)
1729 (goto-char (car lim))
1730 (when (looking-at "/[/*]")
1731 ;; Found comment to align with.
1732 (if (bolp)
1733 ;; Do not pad with one space if we're at bol.
1734 0
1735 (max (1+ col) (current-column))))))))
1736 ;; Recurse to handle value as a new spec.
1737 (c-calc-comment-indent (cdr entry)))))))
1738
1739 ;; To avoid warning about assignment without reference wrt
1740 ;; c-add-syntax below.
1741 (cc-bytecomp-defvar syntactic-relpos)
1742
1215 (defun c-comment-indent () 1743 (defun c-comment-indent ()
1216 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)")) 1744 "Used by `indent-for-comment' to create and indent comments.
1217 0 ;Existing comment at bol stays there. 1745 See `c-indent-comment-alist' for a description."
1218 (let ((opoint (point)) 1746 (save-excursion
1219 placeholder) 1747 (end-of-line)
1220 (save-excursion 1748 (let* ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
1221 (beginning-of-line) 1749 (or (when (consp lim)
1222 (cond 1750 (goto-char (car lim))
1223 ;; CASE 1: A comment following a solitary close-brace should 1751 (when (looking-at "/[/*]")
1224 ;; have only one space. 1752 (skip-chars-backward " \t")
1225 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|" 1753 (point)))
1226 c-comment-start-regexp 1754 (progn
1227 "\\)")) 1755 (skip-chars-backward " \t")
1228 (search-forward "}") 1756 (point)))))
1229 (1+ (current-column))) 1757 (line-type
1230 ;; CASE 2: 2 spaces after #endif 1758 (cond ((looking-at "^/[/*]")
1231 ((or (looking-at "[ \t]*#[ \t]*endif[ \t]*") 1759 'anchored-comment)
1232 (looking-at "[ \t]*#[ \t]*else[ \t]*")) 1760 ((progn (beginning-of-line)
1233 7) 1761 (eq (point) eot))
1234 ;; CASE 3: when c-indent-comments-syntactically-p is t, 1762 'empty-line)
1235 ;; calculate the offset according to c-offsets-alist. 1763 ((progn (back-to-indentation)
1236 ;; E.g. identical to hitting TAB. 1764 (and (eq (char-after) ?})
1237 ((and c-indent-comments-syntactically-p 1765 (eq (point) (1- eot))))
1238 (save-excursion 1766 'end-block)
1239 (skip-chars-forward " \t") 1767 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
1240 (or (looking-at c-comment-start-regexp) 1768 (eq (match-end 0) eot))
1241 (eolp)))) 1769 'cpp-end-block)
1242 (let ((syntax (c-guess-basic-syntax))) 1770 (t
1771 'other))))
1772 (if (and (memq line-type '(anchored-comment empty-line))
1773 c-indent-comments-syntactically-p)
1774 (let ((syntax (c-guess-basic-syntax))
1775 syntactic-relpos)
1243 ;; BOGOSITY ALERT: if we're looking at the eol, its 1776 ;; BOGOSITY ALERT: if we're looking at the eol, its
1244 ;; because indent-for-comment hasn't put the comment-start 1777 ;; because indent-for-comment hasn't put the comment-start
1245 ;; in the buffer yet. this will screw up the syntactic 1778 ;; in the buffer yet. this will screw up the syntactic
1246 ;; analysis so we kludge in the necessary info. Another 1779 ;; analysis so we kludge in the necessary info. Another
1247 ;; kludge is that if we're at the bol, then we really want 1780 ;; kludge is that if we're at the bol, then we really want
1248 ;; to ignore any anchoring as specified by 1781 ;; to ignore any anchoring as specified by
1249 ;; c-comment-only-line-offset since it doesn't apply here. 1782 ;; c-comment-only-line-offset since it doesn't apply here.
1250 (if (save-excursion 1783 (if (eolp)
1251 (back-to-indentation)
1252 (eolp))
1253 (c-add-syntax 'comment-intro)) 1784 (c-add-syntax 'comment-intro))
1254 (let ((c-comment-only-line-offset 1785 (let ((c-comment-only-line-offset
1255 (if (consp c-comment-only-line-offset) 1786 (if (consp c-comment-only-line-offset)
1256 c-comment-only-line-offset 1787 c-comment-only-line-offset
1257 (cons c-comment-only-line-offset 1788 (cons c-comment-only-line-offset
1258 c-comment-only-line-offset)))) 1789 c-comment-only-line-offset))))
1259 (c-get-syntactic-indentation syntax)))) 1790 (c-get-syntactic-indentation syntax)))
1260 ;; CASE 4: If previous line is a comment-only line, use its 1791 (goto-char eot)
1261 ;; indentation if it's greater than comment-column. Leave at 1792 (c-calc-comment-indent line-type)))))
1262 ;; least one space between the comment and the last nonblank
1263 ;; character in any case.
1264 ((save-excursion
1265 (beginning-of-line)
1266 (and (not (bobp))
1267 (forward-line -1))
1268 (skip-chars-forward " \t")
1269 (prog1
1270 (looking-at c-comment-start-regexp)
1271 (setq placeholder (current-column))))
1272 (goto-char opoint)
1273 (skip-chars-backward " \t")
1274 (max (if (bolp) 0 (1+ (current-column)))
1275 placeholder
1276 comment-column))
1277 ;; CASE 5: If comment-column is 0, and nothing but space
1278 ;; before the comment, align it at 0 rather than 1.
1279 ((progn
1280 (goto-char opoint)
1281 (skip-chars-backward " \t")
1282 (and (= comment-column 0) (bolp)))
1283 0)
1284 ;; CASE 6: indent at comment column except leave at least one
1285 ;; space.
1286 (t (max (1+ (current-column))
1287 comment-column))
1288 )))))
1289 1793
1290 1794
1291 ;; used by outline-minor-mode 1795 ;; used by outline-minor-mode
1292 (defun c-outline-level () 1796 (defun c-outline-level ()
1293 ;; This so that `current-column' DTRT in otherwise-hidden text. 1797 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
1294 (let (buffer-invisibility-spec) 1798 ;; in otherwise-hidden text.
1295 (save-excursion 1799 (save-excursion
1296 (skip-chars-forward "\t ") 1800 (skip-chars-forward "\t ")
1297 (current-column)))) 1801 (current-column))))
1298 1802
1299 1803
1446 other than nil or t, then some whitespace[*] is inserted only within 1950 other than nil or t, then some whitespace[*] is inserted only within
1447 literals (comments and strings) and inside preprocessor directives, 1951 literals (comments and strings) and inside preprocessor directives,
1448 but the line is always reindented. 1952 but the line is always reindented.
1449 1953
1450 If `c-syntactic-indentation' is t, indentation is done according to 1954 If `c-syntactic-indentation' is t, indentation is done according to
1451 the syntactic context. If it's nil, the line is just indented one 1955 the syntactic context. A numeric argument, regardless of its value,
1956 means indent rigidly all the lines of the expression starting after
1957 point so that this line becomes properly indented. The relative
1958 indentation among the lines of the expression is preserved.
1959
1960 If `c-syntactic-indentation' is nil, the line is just indented one
1452 step according to `c-basic-offset'. In this mode, a numeric argument 1961 step according to `c-basic-offset'. In this mode, a numeric argument
1453 indents a number of such steps, positive or negative, and an empty 1962 indents a number of such steps, positive or negative, and an empty
1454 prefix argument is equivalent to -1. 1963 prefix argument is equivalent to -1.
1455
1456 If `c-syntactic-indentation' is t, then a numeric argument, regardless
1457 of its value, means indent rigidly all the lines of the expression
1458 starting after point so that this line becomes properly indented. The
1459 relative indentation among the lines of the expression is preserved.
1460 1964
1461 [*] The amount and kind of whitespace inserted is controlled by the 1965 [*] The amount and kind of whitespace inserted is controlled by the
1462 variable `c-insert-tab-function', which is called to do the actual 1966 variable `c-insert-tab-function', which is called to do the actual
1463 insertion of whitespace. Normally the function in this variable 1967 insertion of whitespace. Normally the function in this variable
1464 just inserts a tab character, or the equivalent number of spaces, 1968 just inserts a tab character, or the equivalent number of spaces,
1465 depending on the variable `indent-tabs-mode'." 1969 depending on the variable `indent-tabs-mode'."
1466 1970
1467 (interactive "p") 1971 (interactive "p")
1468 (let ((bod (c-point 'bod)) 1972 (let ((indent-function
1469 (indent-function
1470 (if c-syntactic-indentation 1973 (if c-syntactic-indentation
1471 (symbol-function 'indent-according-to-mode) 1974 (symbol-function 'indent-according-to-mode)
1472 (lambda () 1975 (lambda ()
1473 (let ((steps (cond ((not current-prefix-arg) 1) 1976 (let ((c-macro-start c-macro-start)
1977 (steps (cond ((not current-prefix-arg) 1)
1474 ((equal current-prefix-arg '(4)) -1) 1978 ((equal current-prefix-arg '(4)) -1)
1475 (t arg)))) 1979 (t arg))))
1476 (c-shift-line-indentation (* steps c-basic-offset))) 1980 (c-shift-line-indentation (* steps c-basic-offset))
1981 (when (and c-auto-align-backslashes
1982 (save-excursion
1983 (end-of-line)
1984 (eq (char-before) ?\\))
1985 (c-query-and-set-macro-start))
1986 ;; Realign the line continuation backslash if inside a macro.
1987 (c-backslash-region (point) (point) nil t)))
1477 )))) 1988 ))))
1478 (if (and c-syntactic-indentation current-prefix-arg) 1989 (if (and c-syntactic-indentation current-prefix-arg)
1479 ;; If c-syntactic-indentation and got arg, always indent this 1990 ;; If c-syntactic-indentation and got arg, always indent this
1480 ;; line as C and shift remaining lines of expression the same 1991 ;; line as C and shift remaining lines of expression the same
1481 ;; amount. 1992 ;; amount.
1513 ((eq c-tab-always-indent t) 2024 ((eq c-tab-always-indent t)
1514 (funcall indent-function)) 2025 (funcall indent-function))
1515 ;; CASE 3: if in a literal, insert a tab, but always indent the 2026 ;; CASE 3: if in a literal, insert a tab, but always indent the
1516 ;; line 2027 ;; line
1517 (t 2028 (t
1518 (if (c-in-literal bod) 2029 (if (c-in-literal)
1519 (funcall c-insert-tab-function)) 2030 (funcall c-insert-tab-function))
1520 (funcall indent-function) 2031 (funcall indent-function)
1521 ))))) 2032 )))))
1522 2033
1523 (defun c-indent-exp (&optional shutup-p) 2034 (defun c-indent-exp (&optional shutup-p)
1527 (interactive "*P") 2038 (interactive "*P")
1528 (let ((here (point-marker)) 2039 (let ((here (point-marker))
1529 end) 2040 end)
1530 (set-marker-insertion-type here t) 2041 (set-marker-insertion-type here t)
1531 (unwind-protect 2042 (unwind-protect
1532 (let ((start (progn 2043 (let ((start (save-restriction
1533 ;; try to be smarter about finding the range of 2044 ;; Find the closest following open paren that
1534 ;; lines to indent. skip all following 2045 ;; ends on another line.
1535 ;; whitespace, then try to find any 2046 (narrow-to-region (point-min) (c-point 'eol))
1536 ;; opening paren on the current line 2047 (let (beg (end (point)))
1537 (skip-chars-forward " \t\n") 2048 (while (and (setq beg (c-down-list-forward end))
1538 (save-restriction 2049 (setq end (c-up-list-forward beg))))
1539 (narrow-to-region (point-min) (c-point 'eol)) 2050 (and beg
1540 (c-safe (1- (scan-lists (point) 1 -1))))))) 2051 (eq (char-syntax (char-before beg)) ?\()
1541 ;; find balanced expression end 2052 (1- beg))))))
1542 (setq end (and (c-safe (progn (c-forward-sexp 1) t))
1543 (point)))
1544 ;; sanity check 2053 ;; sanity check
1545 (if (not start) 2054 (if (not start)
1546 (unless shutup-p 2055 (unless shutup-p
1547 (error "Cannot find start of balanced expression to indent")) 2056 (error "Cannot find start of balanced expression to indent"))
1548 (goto-char start) 2057 (goto-char start)
1549 (forward-line) 2058 (setq end (c-safe (scan-sexps (point) 1)))
1550 (setq start (point))
1551 (if (not end) 2059 (if (not end)
1552 (unless shutup-p 2060 (unless shutup-p
1553 (error "Cannot find end of balanced expression to indent")) 2061 (error "Cannot find end of balanced expression to indent"))
1554 (if (< start end) 2062 (forward-line)
1555 (c-indent-region start end))))) 2063 (if (< (point) end)
2064 (c-indent-region (point) end)))))
1556 (goto-char here) 2065 (goto-char here)
1557 (set-marker here nil)))) 2066 (set-marker here nil))))
1558 2067
1559 (defun c-indent-defun () 2068 (defun c-indent-defun ()
1560 "Indent the current top-level function def, struct or class declaration 2069 "Indent the current top-level declaration or macro syntactically.
1561 syntactically." 2070 In the macro case this also has the effect of realigning any line
2071 continuation backslashes, unless `c-auto-align-backslashes' is nil."
1562 (interactive "*") 2072 (interactive "*")
1563 (let ((here (point-marker)) 2073 (let ((here (point-marker)) decl-limits)
1564 (c-echo-syntactic-information-p nil) 2074 ;; We try to be line oriented, unless there are several
1565 (brace (c-least-enclosing-brace (c-parse-state)))) 2075 ;; declarations on the same line.
1566 (goto-char (or brace (c-point 'bod))) 2076 (if (looking-at c-syntactic-eol)
1567 ;; if we're sitting at b-o-b, it might be because there was no 2077 (c-backward-token-1 1 nil (c-point 'bol))
1568 ;; least enclosing brace and we were sitting on the defun's open 2078 (c-forward-token-1 0 nil (c-point 'eol)))
1569 ;; brace.
1570 (if (and (bobp) (not (eq (char-after) ?\{)))
1571 (goto-char here))
1572 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at
1573 ;; the open brace. I consider this an Emacs bug.
1574 (and (boundp 'defun-prompt-regexp)
1575 defun-prompt-regexp
1576 (looking-at defun-prompt-regexp)
1577 (goto-char (match-end 0)))
1578 ;; catch all errors in c-indent-exp so we can 1. give more
1579 ;; meaningful error message, and 2. restore point
1580 (unwind-protect 2079 (unwind-protect
1581 (c-indent-exp) 2080 (if (setq decl-limits (c-declaration-limits nil))
2081 (c-indent-region (car decl-limits)
2082 (cdr decl-limits)))
1582 (goto-char here) 2083 (goto-char here)
1583 (set-marker here nil)))) 2084 (set-marker here nil))))
1584 2085
1585 (defun c-indent-region (start end &optional quiet) 2086 (defun c-indent-region (start end &optional quiet)
1586 "Indent every line whose first char is between START and END inclusive. 2087 "Indent syntactically every line whose first char is between START
1587 Be silent about syntactic errors if the optional argument QUIET is non-nil." 2088 and END inclusive. If the optional argument QUIET is non-nil then no
2089 syntactic errors are reported, even if `c-report-syntactic-errors' is
2090 non-nil."
1588 (save-excursion 2091 (save-excursion
2092 (goto-char end)
2093 (skip-chars-backward " \t\n\r")
2094 (setq end (point))
1589 (goto-char start) 2095 (goto-char start)
1590 ;; Advance to first nonblank line. 2096 ;; Advance to first nonblank line.
1591 (skip-chars-forward " \t\n") 2097 (beginning-of-line)
2098 (skip-chars-forward " \t\n\r")
2099 (setq start (point))
1592 (beginning-of-line) 2100 (beginning-of-line)
1593 (setq c-parsing-error 2101 (setq c-parsing-error
1594 (or (let ((endmark (copy-marker end)) 2102 (or (let ((endmark (copy-marker end))
1595 (c-parsing-error nil) 2103 (c-parsing-error nil)
1596 ;; shut up any echo msgs on indiv lines 2104 ;; shut up any echo msgs on indiv lines
1597 (c-echo-syntactic-information-p nil)) 2105 (c-echo-syntactic-information-p nil)
2106 (in-macro (and c-auto-align-backslashes
2107 (save-excursion (c-beginning-of-macro))
2108 start))
2109 (c-fix-backslashes nil)
2110 syntax)
1598 (unwind-protect 2111 (unwind-protect
1599 (progn 2112 (progn
1600 (c-progress-init start end 'c-indent-region) 2113 (c-progress-init start end 'c-indent-region)
1601 (while (and (bolp) 2114 (while (and (bolp)
1602 (not (eobp)) 2115 (not (eobp))
1603 (< (point) endmark)) 2116 (< (point) endmark))
1604 ;; update progress 2117 ;; update progress
1605 (c-progress-update) 2118 (c-progress-update)
1606 ;; skip blank lines 2119 ;; skip empty lines
1607 (skip-chars-forward " \t\n") 2120 (skip-chars-forward " \t\n")
1608 (beginning-of-line) 2121 (beginning-of-line)
1609 ;; indent the current line 2122 ;; Get syntax and indent.
1610 (c-indent-line nil t) 2123 (setq syntax (c-guess-basic-syntax))
1611 (forward-line))) 2124 (if (and c-auto-align-backslashes
2125 (assq 'cpp-macro syntax))
2126 ;; Record macro start.
2127 (setq in-macro (point)))
2128 (if in-macro
2129 (if (looking-at "\\s *\\\\$")
2130 (forward-line)
2131 (c-indent-line syntax t t)
2132 (if (progn (end-of-line)
2133 (not (eq (char-before) ?\\)))
2134 (progn
2135 ;; Fixup macro backslashes.
2136 (forward-line)
2137 (c-backslash-region in-macro (point) nil)
2138 (setq in-macro nil))
2139 (forward-line)))
2140 (c-indent-line syntax t t)
2141 (forward-line)))
2142 (if in-macro
2143 (c-backslash-region in-macro (c-point 'bopl) nil t)))
1612 (set-marker endmark nil) 2144 (set-marker endmark nil)
1613 (c-progress-fini 'c-indent-region)) 2145 (c-progress-fini 'c-indent-region))
1614 (c-echo-parsing-error quiet)) 2146 (c-echo-parsing-error quiet))
1615 c-parsing-error)))) 2147 c-parsing-error))))
1616 2148
1617 (defun c-mark-function ()
1618 "Put mark at end of current top-level defun, point at beginning."
1619 (interactive)
1620 (let ((here (point))
1621 (eod (c-point 'eod))
1622 (state (c-parse-state)))
1623 ;; Are we sitting at the top level, someplace between either the
1624 ;; beginning of buffer, or the nearest preceding defun? If so,
1625 ;; try first to figure out whether we're sitting on the
1626 ;; introduction to a top-level defun, in which case we want to
1627 ;; mark the entire defun we're sitting on.
1628 ;;
1629 ;; If we're sitting on anything else at the top-level, we want to
1630 ;; just mark the statement that we're on
1631 (if (or (and (consp (car state))
1632 (= (length state) 1))
1633 (null state))
1634 ;; Are we in the whitespace after the nearest preceding defun?
1635 (if (and state
1636 (looking-at "[ \t]*$")
1637 (= (save-excursion
1638 (c-backward-syntactic-ws)
1639 (skip-chars-backward ";")
1640 (point))
1641 (cdr (car state))))
1642 (progn
1643 (setq eod (point))
1644 (goto-char (car (car state)))
1645 (c-beginning-of-statement-1))
1646 (if (= ?{ (save-excursion
1647 (c-end-of-statement-1)
1648 (char-before)))
1649 ;; We must be in a defuns's introduction
1650 (progn
1651 (c-end-of-statement-1)
1652 (skip-chars-backward "{")
1653 (c-beginning-of-statement-1)
1654 (c-forward-syntactic-ws))
1655 ;; Just mark the statement
1656 (c-end-of-statement-1)
1657 (forward-line 1)
1658 (setq eod (point))
1659 (c-beginning-of-statement-1)))
1660 ;; We are inside some enclosing brace structure, so we first
1661 ;; need to find our way to the least enclosing brace. Then, in
1662 ;; both cases, we to mark the region from the beginning of the
1663 ;; current statement, until the end of the next following defun
1664 (while (and state)
1665 (or (consp (car state))
1666 (goto-char (car state)))
1667 (setq state (cdr state)))
1668 (c-beginning-of-statement-1))
1669 (push-mark here)
1670 (push-mark eod nil t)))
1671
1672 (defun c-fn-region-is-active-p () 2149 (defun c-fn-region-is-active-p ()
1673 ;; Function version of the macro for use in places that aren't 2150 ;; Function version of the macro for use in places that aren't
1674 ;; compiled, e.g. in the menus. 2151 ;; compiled, e.g. in the menus.
1675 (c-region-is-active-p)) 2152 (c-region-is-active-p))
1676 2153
1677 (defun c-indent-line-or-region () 2154 (defun c-indent-line-or-region ()
1678 "When the region is active, indent it. Otherwise indent the current line." 2155 "When the region is active, indent it syntactically. Otherwise
2156 indent the current line syntactically."
1679 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p 2157 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p
1680 (interactive) 2158 (interactive)
1681 (if (c-region-is-active-p) 2159 (if (c-region-is-active-p)
1682 (c-indent-region (region-beginning) (region-end)) 2160 (c-indent-region (region-beginning) (region-end))
1683 (indent-according-to-mode))) 2161 (c-indent-line)))
1684 2162
1685 2163
1686 ;; for progress reporting 2164 ;; for progress reporting
1687 (defvar c-progress-info nil) 2165 (defvar c-progress-info nil)
1688 2166
1737 2215
1738 2216
1739 2217
1740 ;;; This page handles insertion and removal of backslashes for C macros. 2218 ;;; This page handles insertion and removal of backslashes for C macros.
1741 2219
1742 (defun c-backslash-region (from to delete-flag) 2220 (defun c-backslash-region (from to delete-flag &optional line-mode)
1743 "Insert, align, or delete end-of-line backslashes on the lines in the region. 2221 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1744 With no argument, inserts backslashes and aligns existing backslashes. 2222 With no argument, inserts backslashes and aligns existing backslashes.
1745 With an argument, deletes the backslashes. 2223 With an argument, deletes the backslashes. The backslash alignment is
2224 done according to the settings in `c-backslash-column',
2225 `c-backslash-max-column' and `c-auto-align-backslashes'.
1746 2226
1747 This function does not modify blank lines at the start of the region. 2227 This function does not modify blank lines at the start of the region.
1748 If the region ends at the start of a line, it always deletes the 2228 If the region ends at the start of a line and the macro doesn't
1749 backslash (if any) at the end of the previous line. 2229 continue below it, the backslash (if any) at the end of the previous
2230 line is deleted.
1750 2231
1751 You can put the region around an entire macro definition and use this 2232 You can put the region around an entire macro definition and use this
1752 command to conveniently insert and align the necessary backslashes." 2233 command to conveniently insert and align the necessary backslashes."
1753 (interactive "*r\nP") 2234 (interactive "*r\nP")
1754 (save-excursion 2235 (let ((endmark (make-marker))
1755 (goto-char from) 2236 ;; Keep the backslash trimming functions from changing the
1756 (let ((column c-backslash-column) 2237 ;; whitespace around point, since in this case it's only the
1757 (endmark (make-marker))) 2238 ;; position of point that tells the indentation of the line.
1758 (move-marker endmark to) 2239 (point-pos (if (save-excursion
1759 ;; Compute the smallest column number past the ends of all the lines. 2240 (skip-chars-backward " \t")
1760 (if (not delete-flag) 2241 (and (bolp) (looking-at "[ \t]*\\\\?$")))
1761 (while (< (point) to) 2242 (point-marker)
1762 (end-of-line) 2243 (point-min)))
1763 (if (eq (char-before) ?\\) 2244 column longest-line-col bs-col-after-end)
1764 (progn (forward-char -1) 2245 (save-excursion
1765 (skip-chars-backward " \t"))) 2246 (goto-char to)
1766 (setq column (max column (1+ (current-column)))) 2247 (if (and (not line-mode) (bobp))
1767 (forward-line 1))) 2248 ;; Nothing to do if to is at bob, since we should back up
1768 ;; Adjust upward to a tab column, if that doesn't push past the margin. 2249 ;; and there's no line to back up to.
1769 (if (> (% column tab-width) 0) 2250 nil
1770 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width))) 2251 (when (and (not line-mode) (bolp))
1771 (if (< adjusted (window-width)) 2252 ;; Do not back up the to line if line-mode is set, to make
1772 (setq column adjusted)))) 2253 ;; e.g. c-newline-and-indent consistent regardless whether
1773 ;; Don't modify blank lines at start of region. 2254 ;; the (newline) call leaves point at bol or not.
1774 (goto-char from) 2255 (backward-char)
1775 (while (and (< (point) endmark) (eolp)) 2256 (setq to (point)))
1776 (forward-line 1)) 2257 (if delete-flag
1777 ;; Add or remove backslashes on all the lines. 2258 (progn
1778 (while (< (point) endmark) 2259 (set-marker endmark (point))
1779 (if (and (not delete-flag) 2260 (goto-char from)
1780 ;; Un-backslashify the last line 2261 (c-delete-backslashes-forward endmark point-pos))
1781 ;; if the region ends right at the start of the next line. 2262 ;; Set bs-col-after-end to the column of any backslash
1782 (save-excursion 2263 ;; following the region, or nil if there is none.
1783 (forward-line 1) 2264 (setq bs-col-after-end
1784 (< (point) endmark))) 2265 (and (progn (end-of-line)
1785 (c-append-backslash column) 2266 (eq (char-before) ?\\))
1786 (c-delete-backslash)) 2267 (= (forward-line 1) 0)
1787 (forward-line 1)) 2268 (progn (end-of-line)
1788 (move-marker endmark nil)))) 2269 (eq (char-before) ?\\))
1789 2270 (1- (current-column))))
1790 (defun c-append-backslash (column) 2271 (when line-mode
1791 (end-of-line) 2272 ;; Back up the to line if line-mode is set, since the line
1792 (if (eq (char-before) ?\\) 2273 ;; after the newly inserted line break should not be
1793 (progn (forward-char -1) 2274 ;; touched in c-newline-and-indent.
1794 (delete-horizontal-space) 2275 (setq to (max from (or (c-safe (c-point 'eopl)) from)))
1795 (indent-to column)) 2276 (unless bs-col-after-end
1796 (indent-to column) 2277 ;; Set bs-col-after-end to non-nil in any case, since we
1797 (insert "\\"))) 2278 ;; do not want to delete the backslash at the last line.
1798 2279 (setq bs-col-after-end t)))
1799 (defun c-delete-backslash () 2280 (if (and line-mode
1800 (end-of-line) 2281 (not c-auto-align-backslashes))
1801 (or (bolp) 2282 (goto-char from)
1802 (progn 2283 ;; Compute the smallest column number past the ends of all
1803 (forward-char -1) 2284 ;; the lines.
1804 (if (looking-at "\\\\") 2285 (setq longest-line-col 0)
1805 (delete-region (1+ (point)) 2286 (goto-char to)
1806 (progn (skip-chars-backward " \t") (point))))))) 2287 (if bs-col-after-end
2288 ;; Include one more line in the max column
2289 ;; calculation, since the to line will be backslashed
2290 ;; too.
2291 (forward-line 1))
2292 (end-of-line)
2293 (while (and (>= (point) from)
2294 (progn
2295 (if (eq (char-before) ?\\)
2296 (forward-char -1))
2297 (skip-chars-backward " \t")
2298 (setq longest-line-col (max longest-line-col
2299 (1+ (current-column))))
2300 (beginning-of-line)
2301 (not (bobp))))
2302 (backward-char))
2303 ;; Try to align with surrounding backslashes.
2304 (goto-char from)
2305 (beginning-of-line)
2306 (if (and (not (bobp))
2307 (progn (backward-char)
2308 (eq (char-before) ?\\)))
2309 (progn
2310 (setq column (1- (current-column)))
2311 (if (numberp bs-col-after-end)
2312 ;; Both a preceding and a following backslash.
2313 ;; Choose the greatest of them.
2314 (setq column (max column bs-col-after-end)))
2315 (goto-char from))
2316 ;; No preceding backslash. Try to align with one
2317 ;; following the region. Disregard the backslash at the
2318 ;; to line since it's likely to be bogus (e.g. when
2319 ;; called from c-newline-and-indent).
2320 (if (numberp bs-col-after-end)
2321 (setq column bs-col-after-end))
2322 ;; Don't modify blank lines at start of region.
2323 (goto-char from)
2324 (while (and (< (point) to) (bolp) (eolp))
2325 (forward-line 1)))
2326 (if (and column (< column longest-line-col))
2327 ;; Don't try to align with surrounding backslashes if
2328 ;; any line is too long.
2329 (setq column nil))
2330 (unless column
2331 ;; Impose minimum limit and tab width alignment only if
2332 ;; we can't align with surrounding backslashes.
2333 (if (> (% longest-line-col tab-width) 0)
2334 (setq longest-line-col
2335 (* (/ (+ longest-line-col tab-width -1)
2336 tab-width)
2337 tab-width)))
2338 (setq column (max c-backslash-column
2339 longest-line-col)))
2340 ;; Always impose maximum limit.
2341 (setq column (min column c-backslash-max-column)))
2342 (if bs-col-after-end
2343 ;; Add backslashes on all lines if the macro continues
2344 ;; after the to line.
2345 (progn
2346 (set-marker endmark to)
2347 (c-append-backslashes-forward endmark column point-pos))
2348 ;; Add backslashes on all lines except the last, and
2349 ;; remove any on the last line.
2350 (if (save-excursion
2351 (goto-char to)
2352 (beginning-of-line)
2353 (if (not (bobp))
2354 (set-marker endmark (1- (point)))))
2355 (progn
2356 (c-append-backslashes-forward endmark column point-pos)
2357 ;; The function above leaves point on the line
2358 ;; following endmark.
2359 (set-marker endmark (point)))
2360 (set-marker endmark to))
2361 (c-delete-backslashes-forward endmark point-pos)))))
2362 (set-marker endmark nil)
2363 (if (markerp point-pos)
2364 (set-marker point-pos nil))))
2365
2366 (defun c-append-backslashes-forward (to-mark column point-pos)
2367 (let ((state (parse-partial-sexp (c-point 'bol) (point))))
2368 (if column
2369 (while
2370 (and
2371 (<= (point) to-mark)
2372
2373 (let ((start (point)) (inserted nil) end col)
2374 (end-of-line)
2375 (unless (eq (char-before) ?\\)
2376 (insert ?\\)
2377 (setq inserted t))
2378 (setq state (parse-partial-sexp
2379 start (point) nil nil state))
2380 (backward-char)
2381 (setq col (current-column))
2382
2383 ;; Avoid unnecessary changes of the buffer.
2384 (cond ((and (not inserted) (nth 3 state))
2385 ;; Don't realign backslashes in string literals
2386 ;; since that would change them.
2387 )
2388
2389 ((< col column)
2390 (delete-region
2391 (point)
2392 (progn
2393 (skip-chars-backward
2394 " \t" (if (>= (point) point-pos) point-pos))
2395 (point)))
2396 (indent-to column))
2397
2398 ((and (= col column)
2399 (memq (char-before) '(?\ ?\t))))
2400
2401 ((progn
2402 (setq end (point))
2403 (or (/= (skip-chars-backward
2404 " \t" (if (>= (point) point-pos) point-pos))
2405 -1)
2406 (/= (char-after) ?\ )))
2407 (delete-region (point) end)
2408 (indent-to column 1)))
2409
2410 (= (forward-line 1) 0))))
2411
2412 ;; Make sure there are backslashes with at least one space in
2413 ;; front of them.
2414 (while
2415 (and
2416 (<= (point) to-mark)
2417
2418 (let ((start (point)))
2419 (end-of-line)
2420 (setq state (parse-partial-sexp
2421 start (point) nil nil state))
2422
2423 (if (eq (char-before) ?\\)
2424 (unless (nth 3 state)
2425 (backward-char)
2426 (unless (and (memq (char-before) '(?\ ?\t))
2427 (/= (point) point-pos))
2428 (insert ?\ )))
2429
2430 (if (and (memq (char-before) '(?\ ?\t))
2431 (/= (point) point-pos))
2432 (insert ?\\)
2433 (insert ?\ ?\\)))
2434
2435 (= (forward-line 1) 0)))))))
2436
2437 (defun c-delete-backslashes-forward (to-mark point-pos)
2438 (while
2439 (and (<= (point) to-mark)
2440 (progn
2441 (end-of-line)
2442 (if (eq (char-before) ?\\)
2443 (delete-region
2444 (point)
2445 (progn (backward-char)
2446 (skip-chars-backward " \t" (if (>= (point) point-pos)
2447 point-pos))
2448 (point))))
2449 (= (forward-line 1) 0)))))
1807 2450
1808 2451
1809 2452
1810 ;;; Line breaking and paragraph filling. 2453 ;;; Line breaking and paragraph filling.
1811 2454
1852 (let ((buffer-modified (buffer-modified-p)) 2495 (let ((buffer-modified (buffer-modified-p))
1853 (buffer-undo-list t) 2496 (buffer-undo-list t)
1854 (start (point))) 2497 (start (point)))
1855 (unwind-protect 2498 (unwind-protect
1856 (progn 2499 (progn
1857 (insert ?\n fill-prefix) 2500 (insert-and-inherit "\n" fill-prefix)
1858 (current-column)) 2501 (current-column))
1859 (delete-region start (point)) 2502 (delete-region start (point))
1860 (set-buffer-modified-p buffer-modified)))))) 2503 (set-buffer-modified-p buffer-modified))))))
1861 ((eq lit-type 'c++) 2504 ((eq lit-type 'c++)
1862 (save-excursion 2505 (save-excursion
1880 (goto-char (car lit-limits)) 2523 (goto-char (car lit-limits))
1881 (if (looking-at prefix-regexp) 2524 (if (looking-at prefix-regexp)
1882 (goto-char (match-end 0)) 2525 (goto-char (match-end 0))
1883 (forward-char 2) 2526 (forward-char 2)
1884 (skip-chars-forward " \t")) 2527 (skip-chars-forward " \t"))
1885 (setq res 2528 (let (str col)
1886 (if (eq (c-point 'boi) (car lit-limits)) 2529 (if (eq (c-point 'boi) (car lit-limits))
1887 ;; There is only whitespace before the comment 2530 ;; There is only whitespace before the comment
1888 ;; starter; take the prefix straight from this 2531 ;; starter; take the prefix straight from this line.
1889 ;; line. 2532 (setq str (buffer-substring-no-properties
1890 (cons (buffer-substring-no-properties
1891 (c-point 'bol) (point)) 2533 (c-point 'bol) (point))
1892 (current-column)) 2534 col (current-column))
1893 ;; There is code before the comment starter, so we 2535 ;; There is code before the comment starter, so we
1894 ;; have to temporarily insert and indent a new 2536 ;; have to temporarily insert and indent a new line to
1895 ;; line to get the right space/tab mix in the 2537 ;; get the right space/tab mix in the indentation.
1896 ;; indentation. 2538 (let ((buffer-modified (buffer-modified-p))
1897 (let ((buffer-modified (buffer-modified-p)) 2539 (buffer-undo-list t)
1898 (buffer-undo-list t) 2540 (prefix-len (- (point) (car lit-limits)))
1899 (prefix-len (- (point) (car lit-limits))) 2541 tmp)
1900 tmp) 2542 (unwind-protect
1901 (unwind-protect 2543 (progn
1902 (progn 2544 (goto-char (car lit-limits))
1903 (goto-char (car lit-limits)) 2545 (indent-to (prog1 (current-column)
1904 (indent-to (prog1 (current-column) 2546 (insert ?\n)))
1905 (insert ?\n))) 2547 (setq tmp (point))
1906 (setq tmp (point)) 2548 (forward-char prefix-len)
1907 (forward-char prefix-len) 2549 (setq str (buffer-substring-no-properties
1908 (cons (buffer-substring-no-properties
1909 (c-point 'bol) (point)) 2550 (c-point 'bol) (point))
1910 (current-column))) 2551 col (current-column)))
1911 (delete-region (car lit-limits) tmp) 2552 (delete-region (car lit-limits) tmp)
1912 (set-buffer-modified-p buffer-modified)))) 2553 (set-buffer-modified-p buffer-modified))))
1913 ))))) 2554 (setq res
2555 (if (or (string-match "\\s \\'" str) (not (eolp)))
2556 (cons str col)
2557 ;; The prefix ends the line with no whitespace
2558 ;; after it. Default to a single space.
2559 (cons (concat str " ") (1+ col))))
2560 )))))
1914 (t 2561 (t
2562 (setq comment-text-end
2563 (save-excursion
2564 (goto-char (- (cdr lit-limits) 2))
2565 (if (looking-at "\\*/") (point) (cdr lit-limits))))
1915 (save-excursion 2566 (save-excursion
1916 (setq comment-text-end (- (cdr lit-limits) 2))
1917 (beginning-of-line) 2567 (beginning-of-line)
1918 (if (and (> (point) (car lit-limits)) 2568 (if (and (> (point) (car lit-limits))
1919 (not (and (looking-at "[ \t]*\\*/") 2569 (not (and (looking-at "[ \t]*\\*/")
1920 (eq (cdr lit-limits) (match-end 0))))) 2570 (eq (cdr lit-limits) (match-end 0)))))
1921 ;; The current line is not the comment starter and 2571 ;; The current line is not the comment starter and
1929 (eq (cdr lit-limits) (match-end 0))) 2579 (eq (cdr lit-limits) (match-end 0)))
1930 (and (looking-at prefix-regexp) 2580 (and (looking-at prefix-regexp)
1931 (<= (1- (cdr lit-limits)) (match-end 0))) 2581 (<= (1- (cdr lit-limits)) (match-end 0)))
1932 (and (< here (point)) 2582 (and (< here (point))
1933 (or (not (match-beginning 0)) 2583 (or (not (match-beginning 0))
1934 (looking-at "[ \t]*$")))) 2584 (looking-at "[ \t]*\\\\?$"))))
1935 ;; The comment is either one line or the next line 2585 ;; The comment is either one line or the next line
1936 ;; contains just the comment ender. Also, if point is 2586 ;; contains just the comment ender. Also, if point is
1937 ;; on the comment opener line and the following line is 2587 ;; on the comment opener line and the following line is
1938 ;; empty or doesn't match c-current-comment-prefix we 2588 ;; empty or doesn't match c-current-comment-prefix we
1939 ;; assume that this is in fact a not yet closed one line 2589 ;; assume that this is in fact a not yet closed one line
1951 ;; indent it to find the correct column. 2601 ;; indent it to find the correct column.
1952 (unwind-protect 2602 (unwind-protect
1953 (progn 2603 (progn
1954 (goto-char (car lit-limits)) 2604 (goto-char (car lit-limits))
1955 (if (looking-at comment-start-regexp) 2605 (if (looking-at comment-start-regexp)
1956 (goto-char (match-end 0)) 2606 (goto-char (min (match-end 0)
2607 comment-text-end))
1957 (forward-char 2) 2608 (forward-char 2)
1958 (skip-chars-forward " \t")) 2609 (skip-chars-forward " \t"))
1959 (when (eq (char-syntax (char-before)) ?\ ) 2610 (when (eq (char-syntax (char-before)) ?\ )
1960 ;; If there's ws on the current 2611 ;; If there's ws on the current
1961 ;; line, we'll use it instead of 2612 ;; line, we'll use it instead of
1973 (setq tmp-pre (point-marker)) 2624 (setq tmp-pre (point-marker))
1974 ;; We insert an extra non-whitespace 2625 ;; We insert an extra non-whitespace
1975 ;; character before the line break and 2626 ;; character before the line break and
1976 ;; after comment-prefix in case it's 2627 ;; after comment-prefix in case it's
1977 ;; "" or ends with whitespace. 2628 ;; "" or ends with whitespace.
1978 (insert "x\n" comment-prefix ?x) 2629 (insert-and-inherit "x\n" comment-prefix "x")
1979 (setq tmp-post (point-marker)) 2630 (setq tmp-post (point-marker))
1980 (indent-according-to-mode) 2631 (indent-according-to-mode)
1981 (goto-char (1- tmp-post)) 2632 (goto-char (1- tmp-post))
1982 (cons (buffer-substring-no-properties 2633 (cons (buffer-substring-no-properties
1983 (c-point 'bol) (point)) 2634 (c-point 'bol) (point))
2048 (goto-char prefix-line) 2699 (goto-char prefix-line)
2049 nil) 2700 nil)
2050 (when fb-string 2701 (when fb-string
2051 ;; A good line wasn't found, but at least we have a 2702 ;; A good line wasn't found, but at least we have a
2052 ;; fallback that matches the comment prefix regexp. 2703 ;; fallback that matches the comment prefix regexp.
2053 (cond ((string-match "\\s \\'" fb-string) 2704 (cond ((or (string-match "\\s \\'" fb-string)
2054 ;; There are ws after the prefix, so let's use it. 2705 (progn
2055 (cons fb-string 2706 (goto-char fb-endpos)
2056 (progn (goto-char fb-endpos) (current-column)))) 2707 (not (eolp))))
2708 ;; There are ws or text after the prefix, so
2709 ;; let's use it.
2710 (cons fb-string (current-column)))
2057 ((progn 2711 ((progn
2058 ;; Check if there's any whitespace padding 2712 ;; Check if there's any whitespace padding
2059 ;; on the comment start line that we can 2713 ;; on the comment start line that we can
2060 ;; use after the prefix. 2714 ;; use after the prefix.
2061 (goto-char (car lit-limits)) 2715 (goto-char (car lit-limits))
2062 (if (looking-at comment-start-regexp) 2716 (if (looking-at comment-start-regexp)
2063 (goto-char (match-end 0)) 2717 (goto-char (match-end 0))
2064 (forward-char 2) 2718 (forward-char 2)
2065 (skip-chars-forward " \t")) 2719 (skip-chars-forward " \t"))
2066 (eq (char-syntax (char-before)) ?\ )) 2720 (or (not (eolp))
2721 (eq (char-syntax (char-before)) ?\ )))
2067 (setq fb-string (buffer-substring-no-properties 2722 (setq fb-string (buffer-substring-no-properties
2068 (save-excursion 2723 (save-excursion
2069 (skip-chars-backward " \t") 2724 (skip-chars-backward " \t")
2070 (point)) 2725 (point))
2071 (point))) 2726 (point)))
2076 (tmp (point))) 2731 (tmp (point)))
2077 ;; Got to mess in the buffer once again to 2732 ;; Got to mess in the buffer once again to
2078 ;; ensure the column gets correct. :P 2733 ;; ensure the column gets correct. :P
2079 (unwind-protect 2734 (unwind-protect
2080 (progn 2735 (progn
2081 (insert fb-string) 2736 (insert-and-inherit fb-string)
2082 (cons (buffer-substring-no-properties 2737 (cons (buffer-substring-no-properties
2083 (c-point 'bol) 2738 (c-point 'bol)
2084 (point)) 2739 (point))
2085 (current-column))) 2740 (current-column)))
2086 (delete-region tmp (point)) 2741 (delete-region tmp (point))
2104 ;; prefix at all and we should just fill it as 2759 ;; prefix at all and we should just fill it as
2105 ;; normal text. 2760 ;; normal text.
2106 '("" . 0)))))) 2761 '("" . 0))))))
2107 )) 2762 ))
2108 2763
2109 (defun c-fill-paragraph (&optional arg) 2764 (defun c-mask-comment (fill-include-ender apply-outside-literal fun &rest args)
2110 "Like \\[fill-paragraph] but handles C and C++ style comments. 2765 ;; Calls FUN with ARGS ar arguments. If point is inside a comment,
2111 If any of the current line is a comment or within a comment, fill the 2766 ;; the comment starter and ender are masked and the buffer is
2112 comment or the paragraph of it that point is in, preserving the 2767 ;; narrowed to make it look like a normal paragraph during the call.
2113 comment indentation or line-starting decorations (see the 2768 (let (fill
2114 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for 2769 ;; beg and end limits the region to narrow. end is a marker.
2115 details).
2116
2117 If point is inside multiline string literal, fill it. This currently
2118 does not respect escaped newlines, except for the special case when it
2119 is the very first thing in the string. The intended use for this rule
2120 is in situations like the following:
2121
2122 char description[] = \"\\
2123 A very long description of something that you want to fill to make
2124 nicely formatted output.\"\;
2125
2126 If point is in any other situation, i.e. in normal code, do nothing.
2127
2128 Optional prefix ARG means justify paragraph as well."
2129 (interactive "*P")
2130 (let (lit-limits lit-type fill
2131 ;; beg and end limits the region to be filled. end is a marker.
2132 beg end 2770 beg end
2133 ;; tmp-pre and tmp-post mark strings that are temporarily 2771 ;; tmp-pre and tmp-post mark strings that are temporarily
2134 ;; inserted at the start and end of the region. tmp-pre is a 2772 ;; inserted at the start and end of the region. tmp-pre is a
2135 ;; cons of the positions of the prepended string. tmp-post is 2773 ;; cons of the positions of the prepended string. tmp-post is
2136 ;; a marker pointing to the single character of the appended 2774 ;; a marker pointing to the single character of the appended
2138 tmp-pre tmp-post 2776 tmp-pre tmp-post
2139 ;; If hang-ender-stuck isn't nil, the comment ender is 2777 ;; If hang-ender-stuck isn't nil, the comment ender is
2140 ;; hanging. In that case it's set to the number of spaces 2778 ;; hanging. In that case it's set to the number of spaces
2141 ;; that should be between the text and the ender. 2779 ;; that should be between the text and the ender.
2142 hang-ender-stuck 2780 hang-ender-stuck
2143 (here (point))) 2781 (here (point))
2782 (c-lit-limits c-lit-limits)
2783 (c-lit-type c-lit-type))
2144 ;; Restore point on undo. It's necessary since we do a lot of 2784 ;; Restore point on undo. It's necessary since we do a lot of
2145 ;; hidden inserts and deletes below that should be as transparent 2785 ;; hidden inserts and deletes below that should be as transparent
2146 ;; as possible. 2786 ;; as possible.
2147 (if (and buffer-undo-list (not (eq buffer-undo-list t))) 2787 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
2148 (setq buffer-undo-list (cons (point) buffer-undo-list))) 2788 (setq buffer-undo-list (cons (point) buffer-undo-list)))
2149 (save-restriction 2789 (save-restriction
2150 ;; Widen to catch comment limits correctly. 2790 ;; Widen to catch comment limits correctly.
2151 (widen) 2791 (widen)
2152 (setq lit-limits (c-collect-line-comments (c-literal-limits nil t)) 2792 (unless c-lit-limits
2153 lit-type (c-literal-type lit-limits))) 2793 (setq c-lit-limits (c-literal-limits nil t)))
2794 (setq c-lit-limits (c-collect-line-comments c-lit-limits))
2795 (unless c-lit-type
2796 (setq c-lit-type (c-literal-type c-lit-limits))))
2154 (save-excursion 2797 (save-excursion
2155 (unless (c-safe (backward-char) 2798 (unless (c-safe (backward-char)
2156 (forward-paragraph) 2799 (forward-paragraph)
2157 (>= (point) here)) 2800 (>= (point) here))
2158 (goto-char here) 2801 (goto-char here)
2166 (backward-paragraph)) 2809 (backward-paragraph))
2167 (setq beg (point))) 2810 (setq beg (point)))
2168 (unwind-protect 2811 (unwind-protect
2169 (progn 2812 (progn
2170 (cond 2813 (cond
2171 ((eq lit-type 'c++) ; Line comment. 2814 ((eq c-lit-type 'c++) ; Line comment.
2172 (save-excursion 2815 (save-excursion
2173 ;; Fill to the comment or paragraph end, whichever 2816 ;; Limit to the comment or paragraph end, whichever
2174 ;; comes first. 2817 ;; comes first.
2175 (set-marker end (min end (cdr lit-limits))) 2818 (set-marker end (min end (cdr c-lit-limits)))
2176 (when (<= beg (car lit-limits)) 2819 (when (<= beg (car c-lit-limits))
2177 ;; The region to be filled includes the comment 2820 ;; The region includes the comment starter, so we must
2178 ;; starter, so we must check it. 2821 ;; check it.
2179 (goto-char (car lit-limits)) 2822 (goto-char (car c-lit-limits))
2180 (back-to-indentation) 2823 (back-to-indentation)
2181 (if (eq (point) (car lit-limits)) 2824 (if (eq (point) (car c-lit-limits))
2182 ;; Include the first line in the fill. 2825 ;; Include the first line in the region.
2183 (setq beg (c-point 'bol)) 2826 (setq beg (c-point 'bol))
2184 ;; The first line contains code before the 2827 ;; The first line contains code before the
2185 ;; comment. We must fake a line that doesn't. 2828 ;; comment. We must fake a line that doesn't.
2186 (setq tmp-pre t))) 2829 (setq tmp-pre t)))
2187 )) 2830 ))
2188 ((eq lit-type 'c) ; Block comment. 2831 ((eq c-lit-type 'c) ; Block comment.
2189 (when (>= end (cdr lit-limits)) 2832 (when (>= end (cdr c-lit-limits))
2190 ;; The region to be filled includes the comment ender. 2833 ;; The region includes the comment ender which we might
2834 ;; want to keep together with the last word.
2191 (unless (save-excursion 2835 (unless (save-excursion
2192 (goto-char (cdr lit-limits)) 2836 (goto-char (cdr c-lit-limits))
2193 (beginning-of-line) 2837 (beginning-of-line)
2194 (and (looking-at (concat "[ \t]*\\(" 2838 (and (looking-at (concat "[ \t]*\\("
2195 c-current-comment-prefix 2839 c-current-comment-prefix
2196 "\\)\\*/")) 2840 "\\)\\*/"))
2197 (eq (cdr lit-limits) (match-end 0)) 2841 (eq (cdr c-lit-limits) (match-end 0))
2198 ;; Leave the comment ender on its own line. 2842 ;; Leave the comment ender on its own line.
2199 (set-marker end (point)))) 2843 (set-marker end (point))))
2200 ;; The comment ender should hang. Replace all cruft 2844 (when fill-include-ender
2201 ;; between it and the last word with one or two 'x' 2845 ;; The comment ender should hang. Replace all cruft
2202 ;; and include it in the fill. We'll change them back 2846 ;; between it and the last word with one or two 'x'
2203 ;; spaces afterwards. 2847 ;; and include it in the region. We'll change them
2204 (let* ((ender-start (save-excursion 2848 ;; back to spaces afterwards.
2205 (goto-char (cdr lit-limits)) 2849 (let* ((ender-start (save-excursion
2206 (skip-syntax-backward "^w ") 2850 (goto-char (cdr c-lit-limits))
2207 (point))) 2851 (skip-syntax-backward "^w ")
2208 (point-rel (- ender-start here)) 2852 (point)))
2209 spaces) 2853 (point-rel (- ender-start here))
2210 (save-excursion 2854 spaces)
2211 (goto-char (cdr lit-limits)) 2855 (save-excursion
2212 (setq tmp-post (point-marker)) 2856 (goto-char (cdr c-lit-limits))
2213 (insert ?\n) 2857 (setq tmp-post (point-marker))
2214 (set-marker end (point)) 2858 (insert ?\n)
2215 (forward-line -1) 2859 (set-marker end (point))
2216 (if (and (looking-at (concat "[ \t]*\\(\\(" 2860 (forward-line -1)
2217 c-current-comment-prefix 2861 (if (and (looking-at (concat "[ \t]*\\(\\("
2218 "\\)[ \t]*\\)")) 2862 c-current-comment-prefix
2219 (eq ender-start (match-end 0))) 2863 "\\)[ \t]*\\)"))
2220 ;; The comment ender is prefixed by nothing 2864 (eq ender-start (match-end 0)))
2221 ;; but a comment line prefix. Remove it 2865 ;; The comment ender is prefixed by nothing
2222 ;; along with surrounding ws. 2866 ;; but a comment line prefix. Remove it
2223 (setq spaces (- (match-end 1) (match-end 2))) 2867 ;; along with surrounding ws.
2224 (goto-char ender-start)) 2868 (setq spaces (- (match-end 1) (match-end 2)))
2225 (skip-chars-backward " \t\r\n") 2869 (goto-char ender-start))
2226 (if (/= (point) ender-start) 2870 (skip-chars-backward " \t\r\n")
2227 (progn 2871 (if (/= (point) ender-start)
2228 (if (<= here (point)) 2872 (progn
2229 ;; Don't adjust point below if it's 2873 (if (<= here (point))
2230 ;; before the string we replace. 2874 ;; Don't adjust point below if it's
2231 (setq point-rel -1)) 2875 ;; before the string we replace.
2232 ;; Keep one or two spaces between the text and 2876 (setq point-rel -1))
2233 ;; the ender, depending on how many there are now. 2877 ;; Keep one or two spaces between the text and
2234 (unless spaces (setq spaces (- ender-start (point)))) 2878 ;; the ender, depending on how many there are now.
2235 (setq spaces (max (min spaces 2) 1)) 2879 (unless spaces
2236 ;; Insert the filler first to keep marks right. 2880 (setq spaces (- ender-start (point))))
2237 (insert (make-string spaces ?x)) 2881 (setq spaces
2238 (delete-region (point) (+ ender-start spaces)) 2882 (max (min spaces
2239 (setq hang-ender-stuck spaces) 2883 (if sentence-end-double-space 2 1))
2240 (setq point-rel 2884 1))
2241 (and (>= point-rel 0) 2885 ;; Insert the filler first to keep marks right.
2242 (- (point) (min point-rel spaces))))) 2886 (insert-char ?x spaces t)
2243 (setq point-rel nil))) 2887 (delete-region (point) (+ ender-start spaces))
2244 (if point-rel 2888 (setq hang-ender-stuck spaces)
2245 ;; Point was in the middle of the string we 2889 (setq point-rel
2246 ;; replaced above, so put it back in the same 2890 (and (>= point-rel 0)
2247 ;; relative position, counting from the end. 2891 (- (point) (min point-rel spaces)))))
2248 (goto-char point-rel)) 2892 (setq point-rel nil)))
2249 ))) 2893 (if point-rel
2250 (when (<= beg (car lit-limits)) 2894 ;; Point was in the middle of the string we
2251 ;; The region to be filled includes the comment starter. 2895 ;; replaced above, so put it back in the same
2896 ;; relative position, counting from the end.
2897 (goto-char point-rel))
2898 ))))
2899 (when (<= beg (car c-lit-limits))
2900 ;; The region includes the comment starter.
2252 (save-excursion 2901 (save-excursion
2253 (goto-char (car lit-limits)) 2902 (goto-char (car c-lit-limits))
2254 (if (looking-at (concat "\\(" comment-start-skip "\\)$")) 2903 (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
2255 ;; Begin filling with the next line. 2904 ;; Begin with the next line.
2256 (setq beg (c-point 'bonl)) 2905 (setq beg (c-point 'bonl))
2257 ;; Fake the fill prefix in the first line. 2906 ;; Fake the fill prefix in the first line.
2258 (setq tmp-pre t))))) 2907 (setq tmp-pre t)))))
2259 ((eq lit-type 'string) ; String. 2908 ((eq c-lit-type 'string) ; String.
2260 (save-excursion 2909 (save-excursion
2261 (when (>= end (cdr lit-limits)) 2910 (when (>= end (cdr c-lit-limits))
2262 (goto-char (1- (cdr lit-limits))) 2911 (goto-char (1- (cdr c-lit-limits)))
2263 (setq tmp-post (point-marker)) 2912 (setq tmp-post (point-marker))
2264 (insert ?\n) 2913 (insert ?\n)
2265 (set-marker end (point))) 2914 (set-marker end (point)))
2266 (when (<= beg (car lit-limits)) 2915 (when (<= beg (car c-lit-limits))
2267 (goto-char (1+ (car lit-limits))) 2916 (goto-char (1+ (car c-lit-limits)))
2268 (setq beg (if (looking-at "\\\\$") 2917 (setq beg (if (looking-at "\\\\$")
2269 ;; Leave the start line if it's 2918 ;; Leave the start line if it's
2270 ;; nothing but an escaped newline. 2919 ;; nothing but an escaped newline.
2271 (1+ (match-end 0)) 2920 (1+ (match-end 0))
2272 (point)))))) 2921 (point))))))
2273 (t (setq beg nil))) 2922 (t (setq beg nil)))
2274 (when tmp-pre 2923 (when tmp-pre
2275 ;; Temporarily insert the fill prefix after the comment 2924 ;; Temporarily insert the fill prefix after the comment
2276 ;; starter so that the first line looks like any other 2925 ;; starter so that the first line looks like any other
2277 ;; comment line in the narrowed region. 2926 ;; comment line in the narrowed region.
2278 (setq fill (c-guess-fill-prefix lit-limits lit-type)) 2927 (setq fill (c-guess-fill-prefix c-lit-limits c-lit-type))
2279 (unless (string-match (concat "\\`[ \t]*\\(" 2928 (unless (string-match (concat "\\`[ \t]*\\("
2280 c-current-comment-prefix 2929 c-current-comment-prefix
2281 "\\)[ \t]*\\'") 2930 "\\)[ \t]*\\'")
2282 (car fill)) 2931 (car fill))
2283 ;; Oops, the prefix doesn't match the comment prefix 2932 ;; Oops, the prefix doesn't match the comment prefix
2292 ;; Find the right spot on the line, break it, insert 2941 ;; Find the right spot on the line, break it, insert
2293 ;; the fill prefix and make sure we're back in the 2942 ;; the fill prefix and make sure we're back in the
2294 ;; same column by temporarily prefixing the first word 2943 ;; same column by temporarily prefixing the first word
2295 ;; with a number of 'x'. 2944 ;; with a number of 'x'.
2296 (save-excursion 2945 (save-excursion
2297 (goto-char (car lit-limits)) 2946 (goto-char (car c-lit-limits))
2298 (if (looking-at (if (eq lit-type 'c++) 2947 (if (looking-at (if (eq c-lit-type 'c++)
2299 c-current-comment-prefix 2948 c-current-comment-prefix
2300 comment-start-skip)) 2949 comment-start-skip))
2301 (goto-char (match-end 0)) 2950 (goto-char (match-end 0))
2302 (forward-char 2) 2951 (forward-char 2)
2303 (skip-chars-forward " \t")) 2952 (skip-chars-forward " \t"))
2305 (let ((col (current-column))) 2954 (let ((col (current-column)))
2306 (setq beg (1+ (point)) 2955 (setq beg (1+ (point))
2307 tmp-pre (list (point))) 2956 tmp-pre (list (point)))
2308 (unwind-protect 2957 (unwind-protect
2309 (progn 2958 (progn
2310 (insert ?\n (car fill)) 2959 (insert-and-inherit "\n" (car fill))
2311 (insert (make-string (- col (current-column)) ?x))) 2960 (insert-char ?x (- col (current-column)) t))
2312 (setcdr tmp-pre (point)))))) 2961 (setcdr tmp-pre (point))))))
2313 (when beg 2962 (if beg
2314 (let ((fill-paragraph-function 2963 (let ((fill-prefix
2315 ;; Avoid infinite recursion. 2964 (or fill-prefix
2316 (if (not (eq fill-paragraph-function 'c-fill-paragraph)) 2965 ;; Kludge: If the function that adapts the
2317 fill-paragraph-function)) 2966 ;; fill prefix doesn't produce the required
2318 (fill-prefix 2967 ;; comment starter for line comments, then
2319 (or fill-prefix 2968 ;; force it by setting fill-prefix.
2320 ;; Kludge: If the function that adapts the fill prefix 2969 (when (and (eq c-lit-type 'c++)
2321 ;; doesn't produce the required comment starter for line 2970 ;; Kludge the kludge:
2322 ;; comments, then force it by setting fill-prefix. 2971 ;; filladapt-mode doesn't have
2323 (when (and (eq lit-type 'c++) 2972 ;; this problem, but it doesn't
2324 ;; Kludge the kludge: filladapt-mode doesn't 2973 ;; override fill-context-prefix
2325 ;; have this problem, but it doesn't override 2974 ;; currently (version 2.12).
2326 ;; fill-context-prefix currently (version 2975 (not (and (boundp 'filladapt-mode)
2327 ;; 2.12). 2976 filladapt-mode))
2328 (not (and (boundp 'filladapt-mode) 2977 (not (string-match
2329 filladapt-mode)) 2978 "\\`[ \t]*//"
2330 (not (string-match 2979 (or (fill-context-prefix beg end)
2331 "\\`[ \t]*//" 2980 ""))))
2332 (or (fill-context-prefix beg end) 2981 (car (or fill (c-guess-fill-prefix
2333 "")))) 2982 c-lit-limits c-lit-type))))))
2334 (car (or fill (c-guess-fill-prefix 2983 ;; Don't remember why I added this, but it doesn't
2335 lit-limits lit-type)))))) 2984 ;; work correctly since `here' can point anywhere
2336 (point-rel (cond ((< here beg) (- here beg)) 2985 ;; after the deletes and inserts above.
2337 ((> here end) (- here end))))) 2986 ;(point-rel (cond ((< here beg) (- here beg))
2338 ;; Preparations finally done! Now we can call the 2987 ; ((> here end) (- here end))))
2339 ;; real fill function. 2988 )
2340 (save-restriction 2989 ;; Preparations finally done! Now we can call the
2341 (narrow-to-region beg end) 2990 ;; actual function.
2342 (fill-paragraph arg)) 2991 (prog1
2343 (if point-rel 2992 (save-restriction
2344 ;; Restore point if it was outside the region. 2993 (narrow-to-region beg end)
2345 (if (< point-rel 0) 2994 (apply fun args))
2346 (goto-char (+ beg point-rel)) 2995 ;(if point-rel
2347 (goto-char (+ end point-rel)))) 2996 ; ;; Restore point if it was outside the region.
2348 ))) 2997 ; (if (< point-rel 0)
2998 ; (goto-char (+ beg point-rel))
2999 ; (goto-char (+ end point-rel))))
3000 ))
3001 (when apply-outside-literal
3002 (apply fun args))))
2349 (when (consp tmp-pre) 3003 (when (consp tmp-pre)
2350 (delete-region (car tmp-pre) (cdr tmp-pre))) 3004 (delete-region (car tmp-pre) (cdr tmp-pre)))
2351 (when tmp-post 3005 (when tmp-post
2352 (save-excursion 3006 (save-excursion
2353 (goto-char tmp-post) 3007 (goto-char tmp-post)
2357 ;; we replace; save-excursion doesn't work in that case. 3011 ;; we replace; save-excursion doesn't work in that case.
2358 (setq here (point)) 3012 (setq here (point))
2359 (goto-char tmp-post) 3013 (goto-char tmp-post)
2360 (skip-syntax-backward "^w ") 3014 (skip-syntax-backward "^w ")
2361 (forward-char (- hang-ender-stuck)) 3015 (forward-char (- hang-ender-stuck))
2362 (insert (make-string hang-ender-stuck ?\ )) 3016 (insert-char ?\ hang-ender-stuck t)
2363 (delete-char hang-ender-stuck) 3017 (delete-char hang-ender-stuck)
2364 (goto-char here)) 3018 (goto-char here))
2365 (set-marker tmp-post nil)) 3019 (set-marker tmp-post nil))
2366 (set-marker end nil))) 3020 (set-marker end nil))))
3021
3022 (defun c-fill-paragraph (&optional arg)
3023 "Like \\[fill-paragraph] but handles C and C++ style comments.
3024 If any of the current line is a comment or within a comment, fill the
3025 comment or the paragraph of it that point is in, preserving the
3026 comment indentation or line-starting decorations (see the
3027 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3028 details).
3029
3030 If point is inside multiline string literal, fill it. This currently
3031 does not respect escaped newlines, except for the special case when it
3032 is the very first thing in the string. The intended use for this rule
3033 is in situations like the following:
3034
3035 char description[] = \"\\
3036 A very long description of something that you want to fill to make
3037 nicely formatted output.\"\;
3038
3039 If point is in any other situation, i.e. in normal code, do nothing.
3040
3041 Optional prefix ARG means justify paragraph as well."
3042 (interactive "*P")
3043 (let ((fill-paragraph-function
3044 ;; Avoid infinite recursion.
3045 (if (not (eq fill-paragraph-function 'c-fill-paragraph))
3046 fill-paragraph-function)))
3047 (c-mask-comment t nil 'fill-paragraph arg))
2367 ;; Always return t. This has the effect that if filling isn't done 3048 ;; Always return t. This has the effect that if filling isn't done
2368 ;; above, it isn't done at all, and it's therefore effectively 3049 ;; above, it isn't done at all, and it's therefore effectively
2369 ;; disabled in normal code. 3050 ;; disabled in normal code.
2370 t) 3051 t)
2371 3052
2378 ;; calls to break lines. We just set this special variable 3059 ;; calls to break lines. We just set this special variable
2379 ;; so that we'll know when we're called from there. It's 3060 ;; so that we'll know when we're called from there. It's
2380 ;; also used to detect whether fill-prefix is user set or 3061 ;; also used to detect whether fill-prefix is user set or
2381 ;; generated automatically by do-auto-fill. 3062 ;; generated automatically by do-auto-fill.
2382 fill-prefix)) 3063 fill-prefix))
2383 (do-auto-fill))) 3064 (c-mask-comment nil t 'do-auto-fill)))
2384 3065
2385 (defun c-indent-new-comment-line (&optional soft) 3066 (defun c-indent-new-comment-line (&optional soft allow-auto-fill)
2386 "Break line at point and indent, continuing comment if within one. 3067 "Break line at point and indent, continuing comment or macro if within one.
2387 If inside a comment and `comment-multi-line' is non-nil, the 3068 If inside a comment and `comment-multi-line' is non-nil, the
2388 indentation and line prefix are preserved (see the 3069 indentation and line prefix are preserved (see the
2389 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for 3070 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
2390 details). If inside a single line comment and `comment-multi-line' is 3071 details). If inside a single line comment and `comment-multi-line' is
2391 nil, a new comment of the same type is started on the next line and 3072 nil, a new comment of the same type is started on the next line and
2392 indented as appropriate for comments. 3073 indented as appropriate for comments. If inside a macro, a line
3074 continuation backslash is inserted and aligned as appropriate, and the
3075 new line is indented according to `c-syntactic-indentation'.
2393 3076
2394 If a fill prefix is specified, it overrides all the above." 3077 If a fill prefix is specified, it overrides all the above."
3078 ;; allow-auto-fill is used from c-context-line-break to allow auto
3079 ;; filling to break the line more than once. Since this function is
3080 ;; used from auto-fill itself, that's normally disabled to avoid
3081 ;; unnecessary recursion.
2395 (interactive) 3082 (interactive)
2396 (let ((fill-prefix fill-prefix) 3083 (let ((fill-prefix fill-prefix)
2397 (do-line-break 3084 (do-line-break
2398 (lambda () 3085 (lambda ()
2399 (delete-region (progn (skip-chars-backward " \t") (point)) 3086 (delete-horizontal-space)
2400 (progn (skip-chars-forward " \t") (point))) 3087 (if soft
2401 (if soft (insert-and-inherit ?\n) (newline 1)))) 3088 (insert-and-inherit ?\n)
3089 (newline (if allow-auto-fill nil 1)))))
2402 ;; Already know the literal type and limits when called from 3090 ;; Already know the literal type and limits when called from
2403 ;; c-context-line-break. 3091 ;; c-context-line-break.
2404 (c-lit-limits c-lit-limits) 3092 (c-lit-limits c-lit-limits)
2405 (c-lit-type c-lit-type)) 3093 (c-lit-type c-lit-type)
3094 (c-macro-start c-macro-start))
2406 (when (not (eq c-auto-fill-prefix t)) 3095 (when (not (eq c-auto-fill-prefix t))
2407 ;; Called from do-auto-fill. 3096 ;; Called from do-auto-fill.
2408 (unless c-lit-limits 3097 (unless c-lit-limits
2409 (setq c-lit-limits (c-literal-limits nil nil t))) 3098 (setq c-lit-limits (c-literal-limits nil nil t)))
2410 (unless c-lit-type 3099 (unless c-lit-type
2411 (setq c-lit-type (c-literal-type c-lit-limits))) 3100 (setq c-lit-type (c-literal-type c-lit-limits)))
2412 (if (memq (cond ((eq c-lit-type 'pound) 3101 (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
2413 ;; Come to think about it, "pound" is a bit
2414 ;; of a misnomer, so call it "cpp" instead
2415 ;; in user interaction.
2416 'cpp)
2417 ((null c-lit-type) 'code) 3102 ((null c-lit-type) 'code)
2418 (t c-lit-type)) 3103 (t c-lit-type))
2419 c-ignore-auto-fill) 3104 c-ignore-auto-fill)
2420 (setq fill-prefix t) ; Used as flag in the cond. 3105 (setq fill-prefix t) ; Used as flag in the cond.
2421 (if (and (null c-auto-fill-prefix) 3106 (if (and (null c-auto-fill-prefix)
2439 ;; A fill-prefix overrides anything. 3124 ;; A fill-prefix overrides anything.
2440 (funcall do-line-break) 3125 (funcall do-line-break)
2441 (insert-and-inherit fill-prefix)) 3126 (insert-and-inherit fill-prefix))
2442 ((progn 3127 ((progn
2443 (unless c-lit-limits 3128 (unless c-lit-limits
2444 (setq c-lit-limits (c-literal-limits nil nil t))) 3129 (setq c-lit-limits (c-literal-limits)))
2445 (unless c-lit-type 3130 (unless c-lit-type
2446 (setq c-lit-type (c-literal-type c-lit-limits))) 3131 (setq c-lit-type (c-literal-type c-lit-limits)))
2447 (memq c-lit-type '(c c++))) 3132 (memq c-lit-type '(c c++)))
3133 ;; Some sort of comment.
2448 (if (or comment-multi-line 3134 (if (or comment-multi-line
2449 (save-excursion 3135 (save-excursion
2450 (goto-char (car c-lit-limits)) 3136 (goto-char (car c-lit-limits))
2451 (end-of-line) 3137 (end-of-line)
2452 (< (point) (cdr c-lit-limits)))) 3138 (< (point) (cdr c-lit-limits))))
2453 ;; Inside a comment that should be continued. 3139 ;; Inside a comment that should be continued.
2454 (let ((fill (c-guess-fill-prefix 3140 (let ((fill (c-guess-fill-prefix
2455 (setq c-lit-limits 3141 (setq c-lit-limits
2456 (c-collect-line-comments c-lit-limits)) 3142 (c-collect-line-comments c-lit-limits))
2457 c-lit-type)) 3143 c-lit-type))
2458 (pos (point))) 3144 (pos (point))
2459 (if (save-excursion 3145 (comment-text-end
2460 (back-to-indentation) 3146 (or (and (eq c-lit-type 'c)
2461 (> (point) (car c-lit-limits)) 3147 (save-excursion
2462 (looking-at c-current-comment-prefix)) 3148 (goto-char (- (cdr c-lit-limits) 2))
3149 (if (looking-at "\\*/") (point))))
3150 (cdr c-lit-limits))))
3151 ;; Skip forward past the fill prefix in case
3152 ;; we're standing in it.
3153 ;;
3154 ;; FIXME: This doesn't work well in cases like
3155 ;;
3156 ;; /* Bla bla bla bla bla
3157 ;; bla bla
3158 ;;
3159 ;; If point is on the 'B' then the line will be
3160 ;; broken after "Bla b".
3161 (while (and (< (current-column) (cdr fill))
3162 (not (eolp)))
3163 (forward-char 1))
3164 (if (and (> (point) comment-text-end)
3165 (> (c-point 'bol) (car c-lit-limits)))
2463 (progn 3166 (progn
2464 ;; Skip forward past the fill prefix in case 3167 ;; The skip takes us out of the (block)
2465 ;; we're standing in it. 3168 ;; comment; insert the fill prefix at bol
2466 (while (and (< (current-column) (cdr fill)) 3169 ;; instead and keep the position.
2467 (not (eolp))) 3170 (setq pos (copy-marker pos t))
2468 (forward-char 1)) 3171 (beginning-of-line)
2469 (if (> (point) (if (and (eq c-lit-type 'c) 3172 (insert-and-inherit (car fill))
2470 (save-excursion 3173 (if soft (insert-and-inherit ?\n) (newline 1))
2471 (forward-char -2) 3174 (goto-char pos)
2472 (looking-at "\\*/"))) 3175 (set-marker pos nil))
2473 (- (cdr c-lit-limits) 2) 3176 ;; Don't break in the middle of a comment starter
2474 (cdr c-lit-limits))) 3177 ;; or ender.
2475 (progn 3178 (cond ((> (point) comment-text-end)
2476 ;; The skip takes us out of the comment; 3179 (goto-char comment-text-end))
2477 ;; insert the fill prefix at bol instead 3180 ((< (point) (+ (car c-lit-limits) 2))
2478 ;; and keep the position. 3181 (goto-char (+ (car c-lit-limits) 2))))
2479 (setq pos (copy-marker pos t))
2480 (beginning-of-line)
2481 (insert-and-inherit (car fill))
2482 (if soft (insert-and-inherit ?\n) (newline 1))
2483 (goto-char pos)
2484 (set-marker pos nil))
2485 (funcall do-line-break)
2486 (insert-and-inherit (car fill))))
2487 (funcall do-line-break) 3182 (funcall do-line-break)
2488 (insert-and-inherit (car fill)))) 3183 (insert-and-inherit (car fill))))
2489 ;; Inside a comment that should be broken. 3184 ;; Inside a comment that should be broken.
2490 (let ((comment-start comment-start) 3185 (let ((comment-start comment-start)
2491 (comment-end comment-end) 3186 (comment-end comment-end)
2507 ;; indentation, so let's start out with the same 3202 ;; indentation, so let's start out with the same
2508 ;; indentation as the previous one. 3203 ;; indentation as the previous one.
2509 (indent-to col) 3204 (indent-to col)
2510 (insert-and-inherit comment-start) 3205 (insert-and-inherit comment-start)
2511 (indent-for-comment)))) 3206 (indent-for-comment))))
3207 ((c-query-and-set-macro-start)
3208 ;; In a macro.
3209 (unless (looking-at "[ \t]*\\\\$")
3210 ;; Do not clobber the alignment of the line continuation
3211 ;; slash; c-backslash-region might look at it.
3212 (delete-horizontal-space))
3213 ;; Got an asymmetry here: In normal code this command
3214 ;; doesn't indent the next line syntactically, and otoh a
3215 ;; normal syntactically indenting newline doesn't continue
3216 ;; the macro.
3217 (c-newline-and-indent (if allow-auto-fill nil 1)))
2512 (t 3218 (t
2513 ;; Somewhere else in the code. 3219 ;; Somewhere else in the code.
2514 (let ((col (save-excursion 3220 (let ((col (save-excursion
2515 (while (progn (back-to-indentation) 3221 (beginning-of-line)
2516 (and (looking-at "^\\s *$") 3222 (while (and (looking-at "[ \t]*\\\\?$")
2517 (= (forward-line -1) 0)))) 3223 (= (forward-line -1) 0)))
2518 (current-column)))) 3224 (current-indentation))))
2519 (funcall do-line-break) 3225 (funcall do-line-break)
2520 (indent-to col)))))) 3226 (indent-to col))))))
2521 3227
2522 (defalias 'c-comment-line-break-function 'c-indent-new-comment-line) 3228 (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
2523 (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line) 3229 (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line)
2535 (c-indent-new-comment-line (ad-get-arg 0)))))) 3241 (c-indent-new-comment-line (ad-get-arg 0))))))
2536 3242
2537 (defun c-context-line-break () 3243 (defun c-context-line-break ()
2538 "Do a line break suitable to the context. 3244 "Do a line break suitable to the context.
2539 3245
2540 When point is outside a comment, insert a newline and indent according 3246 When point is outside a comment or macro, insert a newline and indent
2541 to the syntactic context. 3247 according to the syntactic context, unless `c-syntactic-indentation'
3248 is nil, in which case the new line is indented as the previous
3249 non-empty line instead.
3250
3251 When point is inside the content of a preprocessor directive, a line
3252 continuation backslash is inserted before the line break and aligned
3253 appropriately. The end of the cpp directive doesn't count as inside
3254 it.
2542 3255
2543 When point is inside a comment, continue it with the appropriate 3256 When point is inside a comment, continue it with the appropriate
2544 comment prefix (see the `c-comment-prefix-regexp' and 3257 comment prefix (see the `c-comment-prefix-regexp' and
2545 `c-block-comment-prefix' variables for details). The end of a 3258 `c-block-comment-prefix' variables for details). The end of a
2546 C++-style line comment doesn't count as inside the comment, though." 3259 C++-style line comment doesn't count as inside it."
2547 (interactive "*") 3260 (interactive "*")
2548 (let* ((c-lit-limits (c-literal-limits nil nil t)) 3261 (let* ((c-lit-limits (c-literal-limits nil nil t))
2549 (c-lit-type (c-literal-type c-lit-limits))) 3262 (c-lit-type (c-literal-type c-lit-limits))
3263 (c-macro-start c-macro-start))
2550 (if (or (eq c-lit-type 'c) 3264 (if (or (eq c-lit-type 'c)
2551 (and (eq c-lit-type 'c++) 3265 (and (eq c-lit-type 'c++)
2552 (< (point) 3266 (< (save-excursion
3267 (skip-chars-forward " \t")
3268 (point))
2553 (1- (cdr (setq c-lit-limits 3269 (1- (cdr (setq c-lit-limits
2554 (c-collect-line-comments c-lit-limits))))))) 3270 (c-collect-line-comments c-lit-limits))))))
3271 (and (or (not (looking-at "\\s *$"))
3272 (eq (char-before) ?\\))
3273 (c-query-and-set-macro-start)
3274 (<= (save-excursion
3275 (goto-char c-macro-start)
3276 (if (looking-at "#[ \t]*[a-zA-Z0-9!]+")
3277 (goto-char (match-end 0)))
3278 (point))
3279 (point))))
2555 (let ((comment-multi-line t) 3280 (let ((comment-multi-line t)
2556 (fill-prefix nil)) 3281 (fill-prefix nil))
2557 (c-indent-new-comment-line)) 3282 (c-indent-new-comment-line nil t))
2558 (delete-region (point) (progn (skip-chars-backward " \t") (point))) 3283 (delete-horizontal-space)
2559 (newline) 3284 (newline)
2560 ;; c-indent-line may look at the current indentation, so let's 3285 ;; c-indent-line may look at the current indentation, so let's
2561 ;; start out with the same indentation as the previous line. 3286 ;; start out with the same indentation as the previous line.
2562 (let ((col (save-excursion 3287 (let ((col (save-excursion
2563 (forward-line -1) 3288 (forward-line -1)
2564 (while (progn (back-to-indentation) 3289 (while (and (looking-at "[ \t]*\\\\?$")
2565 (and (looking-at "^\\s *$") 3290 (= (forward-line -1) 0)))
2566 (= (forward-line -1) 0)))) 3291 (current-indentation))))
2567 (current-column))))
2568 (indent-to col)) 3292 (indent-to col))
2569 (indent-according-to-mode)))) 3293 (indent-according-to-mode))))
2570 3294
3295 (defun c-context-open-line ()
3296 "Insert a line break suitable to the context and leave point before it.
3297 This is the `c-context-line-break' equivalent to `open-line', which is
3298 normally bound to C-o. See `c-context-line-break' for the details."
3299 (interactive "*")
3300 (let ((here (point)))
3301 (unwind-protect
3302 (progn
3303 ;; Temporarily insert a non-whitespace char to keep any
3304 ;; preceding whitespace intact.
3305 (insert ?x)
3306 (c-context-line-break))
3307 (goto-char here)
3308 (delete-char 1))))
3309
2571 3310
2572 (cc-provide 'cc-cmds) 3311 (cc-provide 'cc-cmds)
2573 3312
2574 ;;; cc-cmds.el ends here 3313 ;;; cc-cmds.el ends here