Mercurial > emacs
comparison lisp/progmodes/cc-cmds.el @ 75027:9f636bd65206
Update CC Mode to version 5.31.4. The detailed changes are those
recorded in the ChangeLog for 2007-01-01.
author | Alan Mackenzie <acm@muc.de> |
---|---|
date | Mon, 01 Jan 2007 22:11:28 +0000 |
parents | cf3e4c6cd4c3 |
children | a1cc89e6be8b e4e4a56ef723 |
comparison
equal
deleted
inserted
replaced
75026:a39dfae2c4f7 | 75027:9f636bd65206 |
---|---|
51 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge | 51 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge |
52 ; which looks at this. | 52 ; which looks at this. |
53 (cc-bytecomp-defun c-forward-subword) | 53 (cc-bytecomp-defun c-forward-subword) |
54 (cc-bytecomp-defun c-backward-subword) | 54 (cc-bytecomp-defun c-backward-subword) |
55 | 55 |
56 ;; Indentation / Display syntax functions | |
56 (defvar c-fix-backslashes t) | 57 (defvar c-fix-backslashes t) |
57 | 58 |
58 (defun c-indent-line (&optional syntax quiet ignore-point-pos) | 59 (defun c-indent-line (&optional syntax quiet ignore-point-pos) |
59 "Indent the current line according to the syntactic context, | 60 "Indent the current line according to the syntactic context, |
60 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the | 61 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the |
250 (while (not (eobp)) | 251 (while (not (eobp)) |
251 (c-show-syntactic-information '(0)) | 252 (c-show-syntactic-information '(0)) |
252 (forward-line))))) | 253 (forward-line))))) |
253 | 254 |
254 | 255 |
256 ;; Minor mode functions. | |
255 (defun c-update-modeline () | 257 (defun c-update-modeline () |
256 (let ((fmt (format "/%s%s%s%s" | 258 (let ((fmt (format "/%s%s%s%s" |
257 (if c-electric-flag "l" "") | 259 (if c-electric-flag "l" "") |
258 (if (and c-electric-flag c-auto-newline) | 260 (if (and c-electric-flag c-auto-newline) |
259 "a" "") | 261 "a" "") |
841 ;; `c-syntactic-indentation' set. | 843 ;; `c-syntactic-indentation' set. |
842 (when (and (not arg) | 844 (when (and (not arg) |
843 (eq literal 'c) | 845 (eq literal 'c) |
844 (memq 'comment-close-slash c-cleanup-list) | 846 (memq 'comment-close-slash c-cleanup-list) |
845 (eq last-command-char ?/) | 847 (eq last-command-char ?/) |
848 (looking-at (concat "[ \t]*\\(" | |
849 (regexp-quote comment-end) "\\)?$")) | |
846 ; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */ | 850 ; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */ |
847 (save-excursion | 851 (save-excursion |
848 (back-to-indentation) | 852 (save-restriction |
849 (looking-at (concat c-current-comment-prefix "[ \t]*$")))) | 853 (narrow-to-region (point-min) (point)) |
850 (end-of-line) | 854 (back-to-indentation) |
851 (delete-horizontal-space) | 855 (looking-at (concat c-current-comment-prefix "[ \t]*$"))))) |
852 (or (eq (char-before) ?*) (insert-char ?* 1))) ; Do I need a t (retain sticky properties) here? | 856 (kill-region (progn (forward-line 0) (point)) |
857 (progn (end-of-line) (point))) | |
858 (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here? | |
853 | 859 |
854 (setq indentp (and (not arg) | 860 (setq indentp (and (not arg) |
855 c-syntactic-indentation | 861 c-syntactic-indentation |
856 c-electric-flag | 862 c-electric-flag |
857 (eq last-command-char ?/) | 863 (eq last-command-char ?/) |
1251 (eq last-command-char ?\() | 1257 (eq last-command-char ?\() |
1252 (save-excursion | 1258 (save-excursion |
1253 (backward-char) | 1259 (backward-char) |
1254 (skip-chars-backward " \t") | 1260 (skip-chars-backward " \t") |
1255 (setq beg (point)) | 1261 (setq beg (point)) |
1256 (c-save-buffer-state () (c-on-identifier)))) | 1262 (c-save-buffer-state () (c-on-identifier)) |
1263 ;; Don't add a space into #define FOO().... | |
1264 (not (and (c-beginning-of-macro) | |
1265 (c-forward-over-cpp-define-id) | |
1266 (eq (point) beg))))) | |
1257 (save-excursion | 1267 (save-excursion |
1258 (delete-region beg end) | 1268 (delete-region beg end) |
1259 (goto-char beg) | 1269 (goto-char beg) |
1260 (insert ?\ ))) | 1270 (insert ?\ ))) |
1261 | 1271 |
1306 (unwind-protect | 1316 (unwind-protect |
1307 (indent-according-to-mode) | 1317 (indent-according-to-mode) |
1308 (delete-char -2))))) | 1318 (delete-char -2))))) |
1309 | 1319 |
1310 | 1320 |
1321 ;; "nomenclature" functions + c-scope-operator. | |
1311 (defun c-forward-into-nomenclature (&optional arg) | 1322 (defun c-forward-into-nomenclature (&optional arg) |
1312 "Compatibility alias for `c-forward-subword'." | 1323 "Compatibility alias for `c-forward-subword'." |
1313 (interactive "p") | 1324 (interactive "p") |
1314 (require 'cc-subword) | 1325 (require 'cc-subword) |
1315 (c-forward-subword arg)) | 1326 (c-forward-subword arg)) |
1326 "Insert a double colon scope operator at point. | 1337 "Insert a double colon scope operator at point. |
1327 No indentation or other \"electric\" behavior is performed." | 1338 No indentation or other \"electric\" behavior is performed." |
1328 (interactive "*") | 1339 (interactive "*") |
1329 (insert-and-inherit "::")) | 1340 (insert-and-inherit "::")) |
1330 | 1341 |
1342 | |
1343 ;; Movement (etc.) by defuns. | |
1344 (defun c-in-function-trailer-p (&optional lim) | |
1345 ;; Return non-nil if point is between the closing brace and the semicolon of | |
1346 ;; a brace construct which needs a semicolon, e.g. within the "variables" | |
1347 ;; portion of a declaration like "struct foo {...} bar ;". | |
1348 ;; | |
1349 ;; Return the position of the main declaration. Otherwise, return nil. | |
1350 ;; Point is assumed to be at the top level and outside of any macro or | |
1351 ;; literal. | |
1352 ;; | |
1353 ;; If LIM is non-nil, it is the bound on a the backward search for the | |
1354 ;; beginning of the declaration. | |
1355 ;; | |
1356 ;; This function might do hidden buffer changes. | |
1357 (and c-opt-block-decls-with-vars-key | |
1358 (save-excursion | |
1359 (c-syntactic-skip-backward "^;}" lim) | |
1360 (and (eq (char-before) ?\}) | |
1361 (eq (car (c-beginning-of-decl-1 lim)) 'previous) | |
1362 (looking-at c-opt-block-decls-with-vars-key) | |
1363 (point))))) | |
1364 | |
1365 (defun c-where-wrt-brace-construct () | |
1366 ;; Determine where we are with respect to functions (or other brace | |
1367 ;; constructs, included in the term "function" in the rest of this comment). | |
1368 ;; Point is assumed to be outside any macro or literal. | |
1369 ;; This is used by c-\(begining\|end\)-of-defun. | |
1370 ;; | |
1371 ;; Return one of these symbols: | |
1372 ;; at-header : we're at the start of a function's header. | |
1373 ;; in-header : we're inside a function's header, this extending right | |
1374 ;; up to the brace. This bit includes any k&r declarations. | |
1375 ;; in-block : we're inside a function's brace block. | |
1376 ;; in-trailer : we're in the area between the "}" and ";" of something | |
1377 ;; like "struct foo {...} bar, baz;". | |
1378 ;; at-function-end : we're just after the closing brace (or semicolon) that | |
1379 ;; terminates the function. | |
1380 ;; outwith-function: we're not at or in any function. Being inside a | |
1381 ;; non-brace construct also counts as 'outwith-function'. | |
1382 ;; | |
1383 ;; This function might do hidden buffer changes. | |
1384 (save-excursion | |
1385 (let* (pos | |
1386 kluge-start | |
1387 decl-result brace-decl-p | |
1388 (start (point)) | |
1389 (paren-state (c-parse-state)) | |
1390 (least-enclosing (c-least-enclosing-brace paren-state))) | |
1391 | |
1392 (cond | |
1393 ((and least-enclosing | |
1394 (eq (char-after least-enclosing) ?\{)) | |
1395 'in-block) | |
1396 ((c-in-function-trailer-p) | |
1397 'in-trailer) | |
1398 ((and (not least-enclosing) | |
1399 (consp paren-state) | |
1400 (consp (car paren-state)) | |
1401 (eq start (cdar paren-state))) | |
1402 'at-function-end) | |
1403 (t | |
1404 ;; Find the start of the current declaration. NOTE: If we're in the | |
1405 ;; variables after a "struct/eval" type block, we don't get to the | |
1406 ;; real declaration here - we detect and correct for this later. | |
1407 | |
1408 ;;If we're in the parameters' parens, move back out of them. | |
1409 (if least-enclosing (goto-char least-enclosing)) | |
1410 ;; Kluge so that c-beginning-of-decl-1 won't go back if we're already | |
1411 ;; at a declaration. | |
1412 (if (or (and (eolp) (not (eobp))) ; EOL is matched by "\\s>" | |
1413 (not (looking-at | |
1414 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)"))) | |
1415 (forward-char)) | |
1416 (setq kluge-start (point)) | |
1417 (setq decl-result | |
1418 (car (c-beginning-of-decl-1 | |
1419 (and least-enclosing ; LIMIT for c-b-of-decl-1 | |
1420 (c-safe-position least-enclosing paren-state))))) | |
1421 | |
1422 ;; Has the declaration we've gone back to got braces? | |
1423 (setq pos (point)) ; the search limit for c-recognize-knr-p | |
1424 (setq brace-decl-p | |
1425 (save-excursion | |
1426 (and (c-syntactic-re-search-forward "[;{]" nil t t) | |
1427 (or (eq (char-before) ?\{) | |
1428 (and c-recognize-knr-p | |
1429 ;; Might have stopped on the | |
1430 ;; ';' in a K&R argdecl. In | |
1431 ;; that case the declaration | |
1432 ;; should contain a block. | |
1433 (c-in-knr-argdecl pos)))))) | |
1434 | |
1435 (cond | |
1436 ((= (point) kluge-start) ; might be BOB or unbalanced parens. | |
1437 'outwith-function) | |
1438 ((eq decl-result 'same) | |
1439 (if brace-decl-p | |
1440 (if (eq (point) start) | |
1441 'at-header | |
1442 'in-header) | |
1443 'outwith-function)) | |
1444 ((eq decl-result 'previous) | |
1445 (if (and (not brace-decl-p) | |
1446 (c-in-function-trailer-p)) | |
1447 'at-function-end | |
1448 'outwith-function)) | |
1449 (t (error | |
1450 "c-where-wrt-brace-construct: c-beginning-of-decl-1 returned %s" | |
1451 decl-result)))))))) | |
1452 | |
1453 (defun c-backward-to-nth-BOF-{ (n where) | |
1454 ;; Skip to the opening brace of the Nth function before point. If | |
1455 ;; point is inside a function, this counts as the first. Point must be | |
1456 ;; outside any comment/string or macro. | |
1457 ;; | |
1458 ;; N must be strictly positive. | |
1459 ;; WHERE describes the position of point, one of the symbols `at-header', | |
1460 ;; `in-header', `in-block', `in-trailer', `at-function-end', | |
1461 ;; `outwith-function' as returned by c-where-wrt-brace-construct. | |
1462 ;; | |
1463 ;; If we run out of functions, leave point at BOB. Return zero on success, | |
1464 ;; otherwise the number of {s still to go. | |
1465 ;; | |
1466 ;; This function may do hidden buffer changes | |
1467 (cond | |
1468 ;; What we do to go back the first defun depends on where we start. | |
1469 ((bobp)) | |
1470 ((eq where 'in-block) | |
1471 (goto-char (c-least-enclosing-brace (c-parse-state))) | |
1472 (setq n (1- n))) | |
1473 ((eq where 'in-header) | |
1474 (c-syntactic-re-search-forward "{") | |
1475 (backward-char) | |
1476 (setq n (1- n))) | |
1477 (;; (or (eq where 'at-header) (eq where 'outwith-function) | |
1478 ;; (eq where 'at-function-end) (eq where 'in-trailer)) | |
1479 (memq where '(at-header outwith-function at-function-end in-trailer)) | |
1480 (c-syntactic-skip-backward "^}") | |
1481 (when (eq (char-before) ?\}) | |
1482 (backward-sexp) | |
1483 (setq n (1- n)))) | |
1484 (t (error "Unknown `where' %s in c-backward-to-nth-EOF-{" where))) | |
1485 | |
1486 ;; Each time round the loop, go back to a "{" at the outermost level. | |
1487 (while (and (> n 0) (not (bobp))) | |
1488 (c-parse-state) ; This call speeds up the following one | |
1489 ; by a factor of ~6. Hmmm. 2006/4/5. | |
1490 (c-syntactic-skip-backward "^}") | |
1491 (when (eq (char-before) ?\}) | |
1492 (backward-sexp) | |
1493 (setq n (1- n)))) | |
1494 n) | |
1495 | |
1331 (defun c-beginning-of-defun (&optional arg) | 1496 (defun c-beginning-of-defun (&optional arg) |
1332 "Move backward to the beginning of a defun. | 1497 "Move backward to the beginning of a defun. |
1333 Every top level declaration that contains a brace paren block is | 1498 Every top level declaration that contains a brace paren block is |
1334 considered to be a defun. | 1499 considered to be a defun. |
1335 | 1500 |
1342 defun." | 1507 defun." |
1343 | 1508 |
1344 (interactive "p") | 1509 (interactive "p") |
1345 (or arg (setq arg 1)) | 1510 (or arg (setq arg 1)) |
1346 | 1511 |
1347 (if (< arg 0) | 1512 (c-save-buffer-state |
1348 (when (c-end-of-defun (- arg)) | 1513 ((start (point)) |
1349 (c-save-buffer-state nil (c-forward-syntactic-ws)) | 1514 where paren-state pos) |
1350 t) | 1515 |
1351 | 1516 ;; Move back out of any macro/comment/string we happen to be in. |
1352 (c-save-buffer-state (paren-state lim pos) | 1517 (c-beginning-of-macro) |
1353 (catch 'exit | 1518 (setq pos (c-literal-limits)) |
1354 (while (> arg 0) | 1519 (if pos (goto-char (car pos))) |
1355 ;; Note: Partial code duplication in `c-end-of-defun' and | 1520 |
1356 ;; `c-declaration-limits'. | 1521 (setq where (c-where-wrt-brace-construct)) |
1357 | 1522 |
1358 (setq paren-state (c-parse-state)) | 1523 (if (< arg 0) |
1359 (unless (c-safe | 1524 ;; Move forward to the closing brace of a function. |
1360 (goto-char (c-least-enclosing-brace paren-state)) | 1525 (progn |
1361 ;; If we moved to the outermost enclosing paren | 1526 (if ;; (or (eq where 'at-function-end) (eq where 'outwith-function)) |
1362 ;; then we can use c-safe-position to set the | 1527 (memq where '(at-function-end outwith-function)) |
1363 ;; limit. Can't do that otherwise since the | 1528 (setq arg (1+ arg))) |
1364 ;; earlier paren pair on paren-state might very | 1529 (if (< arg 0) |
1365 ;; well be part of the declaration we should go | 1530 (setq arg (c-forward-to-nth-EOF-} (- arg) where))) |
1366 ;; to. | 1531 ;; Move forward to the next opening brace.... |
1367 (setq lim (c-safe-position (point) paren-state)) | 1532 (when (and (= arg 0) |
1368 t) | 1533 (c-syntactic-re-search-forward "{" nil t)) |
1369 ;; At top level. Make sure we aren't inside a literal. | 1534 (backward-char) |
1370 (setq pos (c-literal-limits | 1535 ;; ... and backward to the function header. |
1371 (c-safe-position (point) paren-state))) | 1536 (c-beginning-of-decl-1) |
1372 (if pos (goto-char (car pos)))) | 1537 t)) |
1373 | 1538 |
1374 (while (let ((start (point))) | 1539 ;; Move backward to the opening brace of a function. |
1375 (c-beginning-of-decl-1 lim) | 1540 (when (and (> arg 0) |
1376 (if (= (point) start) | 1541 (eq (setq arg (c-backward-to-nth-BOF-{ arg where)) 0)) |
1377 ;; Didn't move. Might be due to bob or unbalanced | 1542 |
1378 ;; parens. Try to continue if it's the latter. | 1543 ;; Go backward to this function's header. |
1379 (unless (c-safe (goto-char | 1544 (c-beginning-of-decl-1) |
1380 (c-down-list-backward (point)))) | 1545 |
1381 ;; Didn't work, so it's bob then. | 1546 (setq pos (point)) |
1382 (goto-char (point-min)) | 1547 ;; We're now there, modulo comments and whitespace. |
1383 (throw 'exit nil))) | 1548 ;; Try to be line oriented; position point at the closest |
1384 | 1549 ;; preceding boi that isn't inside a comment, but if we hit |
1385 (save-excursion | 1550 ;; the previous declaration then we use the current point |
1386 ;; Check if the declaration contains a brace | 1551 ;; instead. |
1387 ;; block. If not, we try another one. | 1552 (while (and (/= (point) (c-point 'boi)) |
1388 (setq pos (point)) | 1553 (c-backward-single-comment))) |
1389 (not (and (c-syntactic-re-search-forward "[;{]" nil t t) | 1554 (if (/= (point) (c-point 'boi)) |
1390 (or (eq (char-before) ?{) | 1555 (goto-char pos))) |
1391 (and c-recognize-knr-p | 1556 |
1392 ;; Might have stopped on the | 1557 (c-keep-region-active) |
1393 ;; ';' in a K&R argdecl. In | 1558 (= arg 0)))) |
1394 ;; that case the declaration | 1559 |
1395 ;; should contain a block. | 1560 (defun c-forward-to-nth-EOF-} (n where) |
1396 (c-in-knr-argdecl pos))))))) | 1561 ;; Skip to the closing brace of the Nth function after point. If |
1397 (setq lim nil)) | 1562 ;; point is inside a function, this counts as the first. Point must be |
1398 | 1563 ;; outside any comment/string or macro. |
1399 ;; Check if `c-beginning-of-decl-1' put us after the block | 1564 ;; |
1400 ;; in a declaration that doesn't end there. We're searching | 1565 ;; N must be strictly positive. |
1401 ;; back and forth over the block here, which can be | 1566 ;; WHERE describes the position of point, one of the symbols `at-header', |
1402 ;; expensive. | 1567 ;; `in-header', `in-block', `in-trailer', `at-function-end', |
1403 (setq pos (point)) | 1568 ;; `outwith-function' as returned by c-where-wrt-brace-construct. |
1404 (if (and c-opt-block-decls-with-vars-key | 1569 ;; |
1405 (progn | 1570 ;; If we run out of functions, leave point at EOB. Return zero on success, |
1406 (c-backward-syntactic-ws) | 1571 ;; otherwise the number of }s still to go. |
1407 (eq (char-before) ?})) | 1572 ;; |
1408 (eq (car (c-beginning-of-decl-1)) | 1573 ;; This function may do hidden buffer changes. |
1409 'previous) | 1574 |
1410 (save-excursion | 1575 (cond |
1411 (c-end-of-decl-1) | 1576 ;; What we do to go forward over the first defun depends on where we |
1412 (> (point) pos))) | 1577 ;; start. We go to the closing brace of that defun, even when we go |
1413 nil | 1578 ;; backwards to it (in a "struct foo {...} bar ;"). |
1414 (goto-char pos)) | 1579 ((eobp)) |
1415 | 1580 ((eq where 'in-block) |
1416 (setq pos (point)) | 1581 (goto-char (c-least-enclosing-brace (c-parse-state))) |
1417 ;; Try to be line oriented; position point at the closest | 1582 (forward-sexp) |
1418 ;; preceding boi that isn't inside a comment, but if we hit | 1583 (setq n (1- n))) |
1419 ;; the previous declaration then we use the current point | 1584 ((eq where 'in-trailer) |
1420 ;; instead. | 1585 (c-syntactic-skip-backward "^}") |
1421 (while (and (/= (point) (c-point 'boi)) | 1586 (setq n (1- n))) |
1422 (c-backward-single-comment))) | 1587 (;; (or (eq where 'at-function-end) (eq where 'outwith-function) |
1423 (if (/= (point) (c-point 'boi)) | 1588 ;; (eq where 'at-header) (eq where 'in-header)) |
1424 (goto-char pos)) | 1589 (memq where '(at-function-end outwith-function at-header in-header)) |
1425 | 1590 (c-syntactic-re-search-forward "{") |
1426 (setq arg (1- arg))))) | 1591 (backward-char) |
1427 (c-keep-region-active) | 1592 (forward-sexp) |
1428 (= arg 0))) | 1593 (setq n (1- n))) |
1594 (t (error "c-forward-to-nth-EOF-}: `where' is %s" where))) | |
1595 | |
1596 ;; Each time round the loop, go forward to a "}" at the outermost level. | |
1597 (while (and (> n 0) (not (eobp))) | |
1598 ;(c-parse-state) ; This call speeds up the following one by a factor | |
1599 ; of ~6. Hmmm. 2006/4/5. | |
1600 (when (c-syntactic-re-search-forward "{" nil 'eob) | |
1601 (backward-char) | |
1602 (forward-sexp)) | |
1603 (setq n (1- n))) | |
1604 n) | |
1429 | 1605 |
1430 (defun c-end-of-defun (&optional arg) | 1606 (defun c-end-of-defun (&optional arg) |
1431 "Move forward to the end of a top level declaration. | 1607 "Move forward to the end of a top level declaration. |
1432 With argument, do it that many times. Negative argument -N means move | 1608 With argument, do it that many times. Negative argument -N means move |
1433 back to Nth preceding end. Returns t unless search stops due to | 1609 back to Nth preceding end. Returns t unless search stops due to |
1434 beginning or end of buffer. | 1610 beginning or end of buffer. |
1435 | 1611 |
1436 An end of a defun occurs right after the close-parenthesis that matches | 1612 An end of a defun occurs right after the close-parenthesis that matches |
1437 the open-parenthesis that starts a defun; see `beginning-of-defun'." | 1613 the open-parenthesis that starts a defun; see `beginning-of-defun'." |
1438 | |
1439 (interactive "p") | 1614 (interactive "p") |
1440 (or arg (setq arg 1)) | 1615 (or arg (setq arg 1)) |
1441 | 1616 |
1442 (if (< arg 0) | 1617 (c-save-buffer-state |
1443 (when (c-beginning-of-defun (- arg)) | 1618 ((start (point)) |
1444 (c-save-buffer-state nil (c-backward-syntactic-ws)) | 1619 where paren-state pos) |
1445 t) | 1620 |
1446 | 1621 ;; Move back out of any macro/comment/string we happen to be in. |
1447 (c-save-buffer-state (paren-state lim pos) | 1622 (c-beginning-of-macro) |
1448 (catch 'exit | 1623 (setq pos (c-literal-limits)) |
1449 (while (> arg 0) | 1624 (if pos (goto-char (car pos))) |
1450 ;; Note: Partial code duplication in `c-beginning-of-defun' | 1625 |
1451 ;; and `c-declaration-limits'. | 1626 (setq where (c-where-wrt-brace-construct)) |
1452 | 1627 |
1453 (setq paren-state (c-parse-state)) | 1628 (if (< arg 0) |
1454 (unless (c-safe | 1629 ;; Move backwards to the } of a function |
1455 (goto-char (c-least-enclosing-brace paren-state)) | 1630 (progn |
1456 ;; If we moved to the outermost enclosing paren | 1631 (if ;; (or (eq where 'at-header) (eq where 'outwith-function)) |
1457 ;; then we can use c-safe-position to set the | 1632 (memq where '(at-header outwith-function)) |
1458 ;; limit. Can't do that otherwise since the | 1633 (setq arg (1+ arg))) |
1459 ;; earlier paren pair on paren-state might very | 1634 (if (< arg 0) |
1460 ;; well be part of the declaration we should go | 1635 (setq arg (c-backward-to-nth-BOF-{ (- arg) where))) |
1461 ;; to. | 1636 (when (and (= arg 0) |
1462 (setq lim (c-safe-position (point) paren-state)) | 1637 (c-syntactic-skip-backward "^}") |
1463 t) | 1638 (eq (char-before) ?\})) |
1464 ;; At top level. Make sure we aren't inside a literal. | 1639 t)) |
1465 (setq pos (car-safe (c-literal-limits | 1640 |
1466 (c-safe-position (point) paren-state)))) | 1641 ;; Move forward to the } of a function |
1467 (if pos (goto-char pos))) | 1642 (if (> arg 0) |
1468 | 1643 (setq arg (c-forward-to-nth-EOF-} arg where)))) |
1469 ;; Have to move to the start first so that `c-end-of-decl-1' | 1644 |
1470 ;; has the correct start position. | 1645 ;; Do we need to move forward from the brace to the semicolon? |
1471 (setq pos (point)) | 1646 (when (eq arg 0) |
1472 (when (memq (car (c-beginning-of-decl-1 lim)) | 1647 (if (c-in-function-trailer-p) ; after "}" of struct/enum, etc. |
1473 '(previous macro)) | 1648 (c-syntactic-re-search-forward ";")) |
1474 ;; We moved back over the previous defun or a macro. Move | 1649 |
1475 ;; to the next token; it's the start of the next | 1650 (setq pos (point)) |
1476 ;; declaration. We can also be directly after the block | 1651 ;; We're there now, modulo comments and whitespace. |
1477 ;; in a `c-opt-block-decls-with-vars-key' declaration, but | 1652 ;; Try to be line oriented; position point after the next |
1478 ;; then we won't move significantly far here. | 1653 ;; newline that isn't inside a comment, but if we hit the |
1479 (goto-char pos) | 1654 ;; next declaration then we use the current point instead. |
1480 (c-forward-token-2 0)) | 1655 (while (and (not (bolp)) |
1481 | 1656 (not (looking-at "\\s *$")) |
1482 (while (let ((start (point))) | 1657 (c-forward-single-comment))) |
1483 (c-end-of-decl-1) | 1658 (cond ((bolp)) |
1484 (if (= (point) start) | 1659 ((looking-at "\\s *$") |
1485 ;; Didn't move. Might be due to eob or unbalanced | 1660 (forward-line 1)) |
1486 ;; parens. Try to continue if it's the latter. | 1661 (t |
1487 (if (c-safe (goto-char (c-up-list-forward (point)))) | 1662 (goto-char pos)))) |
1488 t | 1663 |
1489 ;; Didn't work, so it's eob then. | |
1490 (goto-char (point-max)) | |
1491 (throw 'exit nil)) | |
1492 | |
1493 (save-excursion | |
1494 ;; Check if the declaration contains a brace | |
1495 ;; block. If not, we try another one. | |
1496 (setq pos (point)) | |
1497 (goto-char start) | |
1498 (not (c-syntactic-re-search-forward "{" pos t t)))))) | |
1499 | |
1500 (setq pos (point)) | |
1501 ;; Try to be line oriented; position point after the next | |
1502 ;; newline that isn't inside a comment, but if we hit the | |
1503 ;; next declaration then we use the current point instead. | |
1504 (while (and (not (bolp)) | |
1505 (not (looking-at "\\s *$")) | |
1506 (c-forward-single-comment))) | |
1507 (cond ((bolp)) | |
1508 ((looking-at "\\s *$") | |
1509 (forward-line 1)) | |
1510 (t | |
1511 (goto-char pos))) | |
1512 | |
1513 (setq arg (1- arg))))) | |
1514 (c-keep-region-active) | 1664 (c-keep-region-active) |
1515 (= arg 0))) | 1665 (= arg 0))) |
1516 | 1666 |
1517 (defun c-declaration-limits (near) | 1667 (defun c-declaration-limits (near) |
1518 ;; Return a cons of the beginning and end positions of the current | 1668 ;; Return a cons of the beginning and end positions of the current |
1644 (error "Cannot find any declaration") | 1794 (error "Cannot find any declaration") |
1645 (goto-char (car decl-limits)) | 1795 (goto-char (car decl-limits)) |
1646 (push-mark (cdr decl-limits) nil t)))) | 1796 (push-mark (cdr decl-limits) nil t)))) |
1647 | 1797 |
1648 | 1798 |
1799 ;; Movement by statements. | |
1649 (defun c-in-comment-line-prefix-p () | 1800 (defun c-in-comment-line-prefix-p () |
1650 ;; Point is within a comment. Is it also within a comment-prefix? | 1801 ;; Point is within a comment. Is it also within a comment-prefix? |
1651 ;; Space at BOL which precedes a comment-prefix counts as part of it. | 1802 ;; Space at BOL which precedes a comment-prefix counts as part of it. |
1652 ;; | 1803 ;; |
1653 ;; This function might do hidden buffer changes. | 1804 ;; This function might do hidden buffer changes. |
2427 (car res)))) | 2578 (car res)))) |
2428 | 2579 |
2429 (if (/= count 0) (setq count (1- count)))) | 2580 (if (/= count 0) (setq count (1- count)))) |
2430 (c-keep-region-active)))) | 2581 (c-keep-region-active)))) |
2431 | 2582 |
2432 | |
2433 | 2583 |
2434 ;; set up electric character functions to work with pending-del, | 2584 ;; set up electric character functions to work with pending-del, |
2435 ;; (a.k.a. delsel) mode. All symbols get the t value except | 2585 ;; (a.k.a. delsel) mode. All symbols get the t value except |
2436 ;; the functions which delete, which gets 'supersede. | 2586 ;; the functions which delete, which gets 'supersede. |
2437 (mapcar | 2587 (mapcar |
2453 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del | 2603 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del |
2454 (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel | 2604 (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel |
2455 (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del | 2605 (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del |
2456 | 2606 |
2457 | 2607 |
2608 ;; Inserting/indenting comments | |
2458 (defun c-calc-comment-indent (entry) | 2609 (defun c-calc-comment-indent (entry) |
2459 ;; This function might do hidden buffer changes. | 2610 ;; This function might do hidden buffer changes. |
2460 (if (symbolp entry) | 2611 (if (symbolp entry) |
2461 (setq entry (or (assq entry c-indent-comment-alist) | 2612 (setq entry (or (assq entry c-indent-comment-alist) |
2462 (assq 'other c-indent-comment-alist) | 2613 (assq 'other c-indent-comment-alist) |
2548 (save-excursion | 2699 (save-excursion |
2549 (skip-chars-forward "\t ") | 2700 (skip-chars-forward "\t ") |
2550 (current-column)))) | 2701 (current-column)))) |
2551 | 2702 |
2552 | 2703 |
2704 ;; Movement by CPP conditionals. | |
2553 (defun c-up-conditional (count) | 2705 (defun c-up-conditional (count) |
2554 "Move back to the containing preprocessor conditional, leaving mark behind. | 2706 "Move back to the containing preprocessor conditional, leaving mark behind. |
2555 A prefix argument acts as a repeat count. With a negative argument, | 2707 A prefix argument acts as a repeat count. With a negative argument, |
2556 move forward to the end of the containing preprocessor conditional. | 2708 move forward to the end of the containing preprocessor conditional. |
2557 | 2709 |
3619 (c-lit-type c-lit-type)) | 3771 (c-lit-type c-lit-type)) |
3620 | 3772 |
3621 ;; Restore point on undo. It's necessary since we do a lot of | 3773 ;; Restore point on undo. It's necessary since we do a lot of |
3622 ;; hidden inserts and deletes below that should be as transparent | 3774 ;; hidden inserts and deletes below that should be as transparent |
3623 ;; as possible. | 3775 ;; as possible. |
3624 (if (and buffer-undo-list (not (eq buffer-undo-list t))) | 3776 (if (and buffer-undo-list (not (eq buffer-undo-list t))) |
3625 (setq buffer-undo-list (cons (point) buffer-undo-list))) | 3777 (setq buffer-undo-list (cons (point) buffer-undo-list))) |
3626 | 3778 |
3779 ;; Determine the limits and type of the containing literal (if any): | |
3780 ;; C-LIT-LIMITS, C-LIT-TYPE; and the limits of the current paragraph: | |
3781 ;; BEG and END. | |
3627 (c-save-buffer-state () | 3782 (c-save-buffer-state () |
3628 (save-restriction | 3783 (save-restriction |
3629 ;; Widen to catch comment limits correctly. | 3784 ;; Widen to catch comment limits correctly. |
3630 (widen) | 3785 (widen) |
3631 (unless c-lit-limits | 3786 (unless c-lit-limits |
3649 (backward-paragraph)) | 3804 (backward-paragraph)) |
3650 (setq beg (point)))) | 3805 (setq beg (point)))) |
3651 | 3806 |
3652 (unwind-protect | 3807 (unwind-protect |
3653 (progn | 3808 (progn |
3809 ;; For each of the possible types of text (string, C comment ...) | |
3810 ;; determine BEG and END, the region we will narrow to. If we're in | |
3811 ;; a literal, constrain BEG and END to the limits of this literal. | |
3812 ;; | |
3813 ;; For some of these text types, particularly a block comment, we | |
3814 ;; may need to massage whitespace near literal delimiters, so that | |
3815 ;; these don't get filled inappropriately. | |
3654 (cond | 3816 (cond |
3655 | 3817 |
3656 ((eq c-lit-type 'c++) ; Line comment. | 3818 ((eq c-lit-type 'c++) ; Line comment. |
3657 (save-excursion | 3819 (save-excursion |
3658 ;; Limit to the comment or paragraph end, whichever | 3820 ;; Limit to the comment or paragraph end, whichever |
3673 | 3835 |
3674 (setq apply-outside-literal t)) | 3836 (setq apply-outside-literal t)) |
3675 | 3837 |
3676 ((eq c-lit-type 'c) ; Block comment. | 3838 ((eq c-lit-type 'c) ; Block comment. |
3677 (when (>= end (cdr c-lit-limits)) | 3839 (when (>= end (cdr c-lit-limits)) |
3678 ;; The region includes the comment ender which we might | 3840 ;; The region includes the comment ender. If it's on its own |
3679 ;; want to keep together with the last word. | 3841 ;; line, it stays on its own line. If it's got company on the |
3680 (unless (save-excursion | 3842 ;; line, it keeps (at least one word of) it. "=====*/" counts |
3681 (goto-char (cdr c-lit-limits)) | 3843 ;; as a comment ender here, but "===== */" doesn't and "foo*/" |
3682 (beginning-of-line) | 3844 ;; doesn't. |
3683 (and (looking-at (concat "[ \t]*\\(" | 3845 (unless |
3684 c-current-comment-prefix | 3846 (save-excursion |
3685 "\\)\\*/")) | 3847 (goto-char (cdr c-lit-limits)) |
3686 (eq (cdr c-lit-limits) (match-end 0)) | 3848 (beginning-of-line) |
3687 ;; The comment ender is on a line of its | 3849 (and (search-forward-regexp |
3688 ;; own. Keep it that way. | 3850 (concat "\\=[ \t]*\\(" c-current-comment-prefix "\\)") |
3689 (set-marker end (point)))) | 3851 (- (cdr c-lit-limits) 2) t) |
3690 | 3852 (not (search-forward-regexp |
3691 ;; The comment ender should hang. Replace all space between | 3853 "\\(\\s \\|\\sw\\)" |
3692 ;; it and the last word either by one or two 'x's (when | 3854 (- (cdr c-lit-limits) 2) 'limit)) |
3855 ;; The comment ender IS on its own line. Exclude | |
3856 ;; this line from the filling. | |
3857 (set-marker end (c-point 'bol)))) | |
3858 | |
3859 ;; The comment ender is hanging. Replace all space between it | |
3860 ;; and the last word either by one or two 'x's (when | |
3693 ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width | 3861 ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width |
3694 ;; as the whitespace (when auto filling), and include it in | 3862 ;; as the whitespace (when auto filling), and include it in |
3695 ;; the region. We'll change them back to whitespace | 3863 ;; the region. We'll change them back to whitespace |
3696 ;; afterwards. The effect of this is to glue the comment | 3864 ;; afterwards. The effect of this is to glue the comment |
3697 ;; ender to the last word in the comment during filling. | 3865 ;; ender to the last word in the comment during filling. |
3704 (current-column))) | 3872 (current-column))) |
3705 (point-rel (- ender-start here)) | 3873 (point-rel (- ender-start here)) |
3706 spaces) | 3874 spaces) |
3707 | 3875 |
3708 (save-excursion | 3876 (save-excursion |
3877 ;; Insert a CR after the "*/", adjust END | |
3709 (goto-char (cdr c-lit-limits)) | 3878 (goto-char (cdr c-lit-limits)) |
3710 (setq tmp-post (point-marker)) | 3879 (setq tmp-post (point-marker)) |
3711 (insert ?\n) | 3880 (insert ?\n) |
3712 (set-marker end (point)) | 3881 (set-marker end (point)) |
3882 | |
3713 (forward-line -1) ; last line of the comment | 3883 (forward-line -1) ; last line of the comment |
3714 (if (and (looking-at (concat "[ \t]*\\(\\(" | 3884 (if (and (looking-at (concat "[ \t]*\\(\\(" |
3715 c-current-comment-prefix | 3885 c-current-comment-prefix |
3716 "\\)[ \t]*\\)")) | 3886 "\\)[ \t]*\\)")) |
3717 (eq ender-start (match-end 0))) | 3887 (eq ender-start (match-end 0))) |
3718 ;; The comment ender is prefixed by nothing | 3888 ;; The comment ender is prefixed by nothing but a |
3719 ;; but a comment line prefix. Remove it | 3889 ;; comment line prefix. IS THIS POSSIBLE? (ACM, |
3720 ;; along with surrounding ws. | 3890 ;; 2006/4/28). Remove it along with surrounding ws. |
3721 (setq spaces (- (match-end 1) (match-end 2))) | 3891 (setq spaces (- (match-end 1) (match-end 2))) |
3722 (goto-char ender-start)) | 3892 (goto-char ender-start)) |
3723 (skip-chars-backward " \t\r\n") ; Surely this can be | 3893 (skip-chars-backward " \t\r\n") ; Surely this can be |
3724 ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18) | 3894 ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18) |
3725 | 3895 |
3896 ;; What's being tested here? 2006/4/20. FIXME!!! | |
3726 (if (/= (point) ender-start) | 3897 (if (/= (point) ender-start) |
3727 (progn | 3898 (progn |
3728 (if (<= here (point)) | 3899 (if (<= here (point)) |
3729 ;; Don't adjust point below if it's | 3900 ;; Don't adjust point below if it's |
3730 ;; before the string we replace. | 3901 ;; before the string we replace. |
4170 it. | 4341 it. |
4171 | 4342 |
4172 When point is inside a comment, continue it with the appropriate | 4343 When point is inside a comment, continue it with the appropriate |
4173 comment prefix (see the `c-comment-prefix-regexp' and | 4344 comment prefix (see the `c-comment-prefix-regexp' and |
4174 `c-block-comment-prefix' variables for details). The end of a | 4345 `c-block-comment-prefix' variables for details). The end of a |
4175 C++-style line comment doesn't count as inside it." | 4346 C++-style line comment doesn't count as inside it. |
4347 | |
4348 When point is inside a string, only insert a backslash when it is also | |
4349 inside a preprocessor directive." | |
4176 | 4350 |
4177 (interactive "*") | 4351 (interactive "*") |
4178 (let* (c-lit-limits c-lit-type | 4352 (let* (c-lit-limits c-lit-type |
4179 (c-macro-start c-macro-start)) | 4353 (c-macro-start c-macro-start)) |
4180 | 4354 |
4181 (if (c-save-buffer-state () | 4355 (c-save-buffer-state () |
4182 (setq c-lit-limits (c-literal-limits nil nil t) | 4356 (setq c-lit-limits (c-literal-limits nil nil t) |
4183 c-lit-type (c-literal-type c-lit-limits)) | 4357 c-lit-type (c-literal-type c-lit-limits)) |
4184 (or (eq c-lit-type 'c) | 4358 (when (eq c-lit-type 'c++) |
4185 (and (eq c-lit-type 'c++) | 4359 (setq c-lit-limits (c-collect-line-comments c-lit-limits))) |
4186 (< (save-excursion | 4360 (c-query-and-set-macro-start)) |
4187 (skip-chars-forward " \t") | 4361 |
4188 (point)) | 4362 (cond |
4189 (1- (cdr (setq c-lit-limits (c-collect-line-comments | 4363 ((or (eq c-lit-type 'c) |
4190 c-lit-limits)))))) | 4364 (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it. |
4191 (and (or (not (looking-at "\\s *$")) | 4365 (< (save-excursion |
4192 (eq (char-before) ?\\)) | 4366 (skip-chars-forward " \t") |
4193 (c-query-and-set-macro-start) | 4367 (point)) |
4194 (<= (save-excursion | 4368 (1- (cdr c-lit-limits)))) |
4195 (goto-char c-macro-start) | 4369 (and (numberp c-macro-start) ; Macro, but not at the very end of |
4196 (if (looking-at c-opt-cpp-start) | 4370 ; it, not in a string, and not in the |
4197 (goto-char (match-end 0))) | 4371 ; cpp keyword. |
4198 (point)) | 4372 (not (eq c-lit-type 'string)) |
4199 (point))))) | 4373 (or (not (looking-at "\\s *$")) |
4200 | 4374 (eq (char-before) ?\\)) |
4201 (let ((comment-multi-line t) | 4375 (<= (save-excursion |
4202 (fill-prefix nil)) | 4376 (goto-char c-macro-start) |
4203 (c-indent-new-comment-line nil t)) | 4377 (if (looking-at c-opt-cpp-start) |
4204 | 4378 (goto-char (match-end 0))) |
4205 (delete-horizontal-space) | 4379 (point)) |
4206 (newline) | 4380 (point)))) |
4207 | 4381 (let ((comment-multi-line t) |
4382 (fill-prefix nil)) | |
4383 (c-indent-new-comment-line nil t))) | |
4384 | |
4385 ((eq c-lit-type 'string) | |
4386 (if (and (numberp c-macro-start) | |
4387 (not (eq (char-before) ?\\))) | |
4388 (insert ?\\)) | |
4389 (newline)) | |
4390 | |
4391 (t (delete-horizontal-space) | |
4392 (newline) | |
4208 ;; c-indent-line may look at the current indentation, so let's | 4393 ;; c-indent-line may look at the current indentation, so let's |
4209 ;; start out with the same indentation as the previous line. | 4394 ;; start out with the same indentation as the previous line. |
4210 (let ((col (save-excursion | 4395 (let ((col (save-excursion |
4211 (forward-line -1) | 4396 (backward-char) |
4212 (while (and (looking-at "[ \t]*\\\\?$") | 4397 (forward-line 0) |
4213 (= (forward-line -1) 0))) | 4398 (while (and (looking-at "[ \t]*\\\\?$") |
4214 (current-indentation)))) | 4399 (= (forward-line -1) 0))) |
4215 (indent-to col)) | 4400 (current-indentation)))) |
4216 | 4401 (indent-to col)) |
4217 (indent-according-to-mode)))) | 4402 (indent-according-to-mode))))) |
4218 | 4403 |
4219 (defun c-context-open-line () | 4404 (defun c-context-open-line () |
4220 "Insert a line break suitable to the context and leave point before it. | 4405 "Insert a line break suitable to the context and leave point before it. |
4221 This is the `c-context-line-break' equivalent to `open-line', which is | 4406 This is the `c-context-line-break' equivalent to `open-line', which is |
4222 normally bound to C-o. See `c-context-line-break' for the details." | 4407 normally bound to C-o. See `c-context-line-break' for the details." |