Mercurial > emacs
comparison lisp/progmodes/cc-cmds.el @ 21107:d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
nil when calling fill-paragraph, to avoid bogus recursion which
will signal an error.
(c-fill-paragraph): Always keep point in the same
relative position. Fill comment before point if there's nothing
else on the same line. Fill block comments after code a little
better. Try harder to find a good fill-prefix when point is on a
block comment ender line. Use c-Java-javadoc-paragraph-start in
block comments in Java mode. Leave block comment ender alone when
c-hanging-comment-ender-p is nil and point is on that line.
Detect paragraph-separate in multiparagraph comments. Fix for bug
that may strip the `*' off `*/' if fill-prefix ends with `*' and
c-hanging-comment-ender-p is t. Added filling of multiline string
literals. Always return t to disable filling in any unhandled
area, i.e. actual code where fill-paragraph only mess things up.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 08 Mar 1998 06:50:21 +0000 |
parents | a0748eef9a76 |
children | 5b0864259a4b |
comparison
equal
deleted
inserted
replaced
21106:5e5fa0a34cb0 | 21107:d67e858e738b |
---|---|
25 ;; You should have received a copy of the GNU General Public License | 25 ;; You should have received a copy of the GNU General Public License |
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the | 26 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
28 ;; Boston, MA 02111-1307, USA. | 28 ;; Boston, MA 02111-1307, USA. |
29 | 29 |
30 (eval-when-compile | |
31 (require 'cc-defs)) | |
32 | 30 |
33 | 31 |
34 (defun c-calculate-state (arg prevstate) | 32 (defun c-calculate-state (arg prevstate) |
35 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If | 33 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If |
36 ;; arg is nil or zero, toggle the state. If arg is negative, turn | 34 ;; arg is nil or zero, toggle the state. If arg is negative, turn |
1449 "Like \\[fill-paragraph] but handles C and C++ style comments. | 1447 "Like \\[fill-paragraph] but handles C and C++ style comments. |
1450 If any of the current line is a comment or within a comment, | 1448 If any of the current line is a comment or within a comment, |
1451 fill the comment or the paragraph of it that point is in, | 1449 fill the comment or the paragraph of it that point is in, |
1452 preserving the comment indentation or line-starting decorations. | 1450 preserving the comment indentation or line-starting decorations. |
1453 | 1451 |
1452 If point is inside multiline string literal, fill it. This currently | |
1453 does not respect escaped newlines, except for the special case when it | |
1454 is the very first thing in the string. The intended use for this rule | |
1455 is in situations like the following: | |
1456 | |
1457 char description[] = \"\\ | |
1458 A very long description of something that you want to fill to make | |
1459 nicely formatted output.\"\; | |
1460 | |
1461 If point is in any other situation, i.e. in normal code, do nothing. | |
1462 | |
1454 Optional prefix ARG means justify paragraph as well." | 1463 Optional prefix ARG means justify paragraph as well." |
1455 (interactive "*P") | 1464 (interactive "*P") |
1456 (let* (comment-start-place | 1465 (let* ((point-save (point-marker)) |
1466 limits | |
1467 comment-start-place | |
1457 (first-line | 1468 (first-line |
1458 ;; Check for obvious entry to comment. | 1469 ;; Check for obvious entry to comment. |
1459 (save-excursion | 1470 (save-excursion |
1460 (beginning-of-line) | 1471 (beginning-of-line) |
1461 (skip-chars-forward " \t\n") | 1472 (skip-chars-forward " \t") |
1462 (and (looking-at comment-start-skip) | 1473 (and (looking-at comment-start-skip) |
1463 (setq comment-start-place (point))))) | 1474 (setq comment-start-place (point))))) |
1464 (re1 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")) | 1475 (re1 "\\|\\([ \t]*/\\*[ \t]*\\|[ \t]*\\*/[ \t]*\\|[ \t/*]*\\)")) |
1465 (if (save-excursion | 1476 (if (save-excursion |
1466 (beginning-of-line) | 1477 (beginning-of-line) |
1467 (looking-at ".*//")) | 1478 (looking-at ".*//")) |
1468 (let ((fill-prefix fill-prefix) | 1479 (let ((fill-prefix fill-prefix) |
1469 ;; Lines containing just a comment start or just an end | 1480 ;; Lines containing just a comment start or just an end |
1470 ;; should not be filled into paragraphs they are next | 1481 ;; should not be filled into paragraphs they are next |
1471 ;; to. | 1482 ;; to. |
1472 (paragraph-start (concat paragraph-start re1)) | 1483 (paragraph-start (concat paragraph-start re1 "$")) |
1473 (paragraph-separate (concat paragraph-separate re1))) | 1484 (paragraph-separate (concat paragraph-separate re1 "$"))) |
1474 (save-excursion | 1485 (save-excursion |
1475 (beginning-of-line) | 1486 (beginning-of-line) |
1476 ;; Move up to first line of this comment. | 1487 ;; Move up to first line of this comment. |
1477 (while (and (not (bobp)) | 1488 (while (and (not (bobp)) |
1478 (looking-at "[ \t]*//[ \t]*[^ \t\n]")) | 1489 (looking-at "[ \t]*//[ \t]*[^ \t\n]")) |
1493 (forward-line 1) | 1504 (forward-line 1) |
1494 (while | 1505 (while |
1495 (looking-at (regexp-quote fill-prefix)) | 1506 (looking-at (regexp-quote fill-prefix)) |
1496 (forward-line 1)) | 1507 (forward-line 1)) |
1497 (point))) | 1508 (point))) |
1498 (fill-paragraph arg) | 1509 (or (c-safe |
1499 t))) | 1510 ;; fill-paragraph sometimes fails to detect when we |
1511 ;; are between paragraphs. | |
1512 (beginning-of-line) | |
1513 (search-forward fill-prefix (c-point 'eol)) | |
1514 (looking-at paragraph-separate)) | |
1515 ;; Avoids recursion | |
1516 (let (fill-paragraph-function) | |
1517 (fill-paragraph arg)))))) | |
1500 ;; else C style comments | 1518 ;; else C style comments |
1501 (if (or first-line | 1519 (if (or first-line |
1502 ;; t if we enter a comment between start of function and | 1520 ;; t if we enter a comment between start of function and |
1503 ;; this line. | 1521 ;; this line. |
1504 (eq (c-in-literal) 'c) | 1522 (save-excursion |
1523 (setq limits (c-literal-limits)) | |
1524 (and (consp limits) | |
1525 (save-excursion | |
1526 (goto-char (car limits)) | |
1527 (looking-at c-comment-start-regexp)))) | |
1505 ;; t if this line contains a comment starter. | 1528 ;; t if this line contains a comment starter. |
1506 (setq first-line | 1529 (setq first-line |
1507 (save-excursion | 1530 (save-excursion |
1508 (beginning-of-line) | 1531 (beginning-of-line) |
1509 (prog1 | 1532 (prog1 |
1510 (re-search-forward comment-start-skip | 1533 (re-search-forward comment-start-skip |
1511 (save-excursion (end-of-line) | 1534 (save-excursion (end-of-line) |
1512 (point)) | 1535 (point)) |
1513 t) | 1536 t) |
1514 (setq comment-start-place (point)))))) | 1537 (setq comment-start-place (point))))) |
1538 ;; t if we're in the whitespace after a comment ender | |
1539 ;; which ends its line. | |
1540 (and (not limits) | |
1541 (when (and (looking-at "[ \t]*$") | |
1542 (save-excursion | |
1543 (beginning-of-line) | |
1544 (looking-at ".*\\*/[ \t]*$"))) | |
1545 (save-excursion | |
1546 (forward-comment -1) | |
1547 (setq comment-start-place (point))) | |
1548 t))) | |
1515 ;; Inside a comment: fill one comment paragraph. | 1549 ;; Inside a comment: fill one comment paragraph. |
1516 (let ((fill-prefix | 1550 (let ((fill-prefix |
1517 ;; The prefix for each line of this paragraph | 1551 ;; The prefix for each line of this paragraph |
1518 ;; is the appropriate part of the start of this line, | 1552 ;; is the appropriate part of the start of this line, |
1519 ;; up to the column at which text should be indented. | 1553 ;; up to the column at which text should be indented. |
1520 (save-excursion | 1554 (save-excursion |
1521 (beginning-of-line) | 1555 (beginning-of-line) |
1522 (if (looking-at "[ \t]*/\\*.*\\*/") | 1556 (if (looking-at ".*/\\*.*\\*/") |
1523 (progn (re-search-forward comment-start-skip) | 1557 (progn (re-search-forward comment-start-skip) |
1524 (make-string (current-column) ?\ )) | 1558 (make-string (current-column) ?\ )) |
1525 (if first-line (forward-line 1)) | 1559 (if first-line |
1560 (forward-line 1) | |
1561 (if (and (looking-at "[ \t]*\\*/") | |
1562 (not (save-excursion | |
1563 (forward-line -1) | |
1564 (looking-at ".*/\\*")))) | |
1565 (forward-line -1))) | |
1526 | 1566 |
1527 (let ((line-width (progn (end-of-line) (current-column)))) | 1567 (let ((line-width (progn (end-of-line) (current-column)))) |
1528 (beginning-of-line) | 1568 (beginning-of-line) |
1529 (prog1 | 1569 (prog1 |
1530 (buffer-substring | 1570 (buffer-substring |
1531 (point) | 1571 (point) |
1532 | 1572 |
1533 ;; How shall we decide where the end of the | 1573 ;; How shall we decide where the end of the |
1534 ;; fill-prefix is? | 1574 ;; fill-prefix is? |
1535 (progn | 1575 (progn |
1536 (beginning-of-line) | |
1537 (skip-chars-forward " \t*" (c-point 'eol)) | 1576 (skip-chars-forward " \t*" (c-point 'eol)) |
1538 ;; kludge alert, watch out for */, in | 1577 ;; kludge alert, watch out for */, in |
1539 ;; which case fill-prefix should *not* | 1578 ;; which case fill-prefix should *not* |
1540 ;; be "*"! | 1579 ;; be "*"! |
1541 (if (and (eq (char-after) ?/) | 1580 (if (and (eq (char-after) ?/) |
1565 (progn (end-of-line) (point)))))))) | 1604 (progn (end-of-line) (point)))))))) |
1566 | 1605 |
1567 ;; Lines containing just a comment start or just an end | 1606 ;; Lines containing just a comment start or just an end |
1568 ;; should not be filled into paragraphs they are next | 1607 ;; should not be filled into paragraphs they are next |
1569 ;; to. | 1608 ;; to. |
1570 (paragraph-start (concat paragraph-start re1)) | 1609 (paragraph-start (if (eq major-mode 'java-mode) |
1571 (paragraph-separate (concat paragraph-separate re1)) | 1610 (concat paragraph-start |
1611 re1 "\\(" | |
1612 c-Java-javadoc-paragraph-start | |
1613 "\\|$\\)") | |
1614 (concat paragraph-start re1 "$"))) | |
1615 (paragraph-separate (concat paragraph-separate re1 "$")) | |
1572 (chars-to-delete 0) | 1616 (chars-to-delete 0) |
1573 ) | 1617 ) |
1574 (save-restriction | 1618 (save-restriction |
1575 ;; Don't fill the comment together with the code | 1619 ;; Don't fill the comment together with the code |
1576 ;; following it. So temporarily exclude everything | 1620 ;; following it. So temporarily exclude everything |
1597 (insert-char ?\ column)))) | 1641 (insert-char ?\ column)))) |
1598 (save-excursion | 1642 (save-excursion |
1599 (if comment-start-place | 1643 (if comment-start-place |
1600 (goto-char (+ comment-start-place 2))) | 1644 (goto-char (+ comment-start-place 2))) |
1601 (search-forward "*/" nil 'move) | 1645 (search-forward "*/" nil 'move) |
1602 (forward-line 1) | 1646 (if (and (not c-hanging-comment-ender-p) |
1647 (save-excursion | |
1648 (beginning-of-line) | |
1649 (looking-at "[ \t]*\\*/"))) | |
1650 (beginning-of-line) | |
1651 (forward-line 1)) | |
1603 (point))) | 1652 (point))) |
1604 (fill-paragraph arg) | 1653 (or (c-safe |
1654 ;; fill-paragraph sometimes fails to detect when we | |
1655 ;; are between paragraphs. | |
1656 (beginning-of-line) | |
1657 (search-forward fill-prefix (c-point 'eol)) | |
1658 (looking-at paragraph-separate)) | |
1659 ;; Avoids recursion | |
1660 (let (fill-paragraph-function) | |
1661 (fill-paragraph arg))) | |
1605 (save-excursion | 1662 (save-excursion |
1606 ;; Delete the chars we inserted to avoid clobbering | 1663 ;; Delete the chars we inserted to avoid clobbering |
1607 ;; the stuff before the comment start. | 1664 ;; the stuff before the comment start. |
1608 (goto-char (point-min)) | 1665 (goto-char (point-min)) |
1609 (if (> chars-to-delete 0) | 1666 (if (> chars-to-delete 0) |
1618 (if (and c-hanging-comment-ender-p | 1675 (if (and c-hanging-comment-ender-p |
1619 (looking-at "[ \t]*\\*/")) | 1676 (looking-at "[ \t]*\\*/")) |
1620 ;(delete-indentation))))) | 1677 ;(delete-indentation))))) |
1621 (let ((fill-column (+ fill-column 9999))) | 1678 (let ((fill-column (+ fill-column 9999))) |
1622 (forward-line -1) | 1679 (forward-line -1) |
1623 (fill-region-as-paragraph (point) (point-max)))))) | 1680 (fill-region-as-paragraph (point) (point-max)) |
1624 t))))) | 1681 ;; If fill-prefix ended with a `*', it may be |
1682 ;; taken away from the comment ender. We got to | |
1683 ;; check this and put it back if that is the | |
1684 ;; case. | |
1685 (goto-char (- (point-max) 2)) | |
1686 (if (not (= (char-before) ?*)) | |
1687 (insert ?*)) | |
1688 ))))) | |
1689 ;; Else maybe a string. Fill it if it's a multiline string. | |
1690 ;; FIXME: This currently doesn't handle escaped newlines. | |
1691 ;; Doing that correctly is a bit tricky. | |
1692 (if (and limits | |
1693 (eq (char-syntax (char-after (car limits))) ?\") | |
1694 (save-excursion | |
1695 (goto-char (car limits)) | |
1696 (end-of-line) | |
1697 (< (point) (cdr limits)))) | |
1698 (let (fill-prefix | |
1699 fill-paragraph-function) | |
1700 (save-restriction | |
1701 (narrow-to-region (save-excursion | |
1702 (goto-char (1+ (car limits))) | |
1703 (if (looking-at "\\\\$") | |
1704 ;; Some DWIM: Leave the start | |
1705 ;; line if it's nothing but an | |
1706 ;; escaped newline. | |
1707 (1+ (match-end 0)) | |
1708 (point))) | |
1709 (save-excursion | |
1710 (goto-char (1- (cdr limits))) | |
1711 ;; Inserting a newline and | |
1712 ;; removing it again after | |
1713 ;; fill-paragraph makes it more | |
1714 ;; predictable. | |
1715 (insert ?\n) | |
1716 (point))) | |
1717 ;; Do not compensate for the narrowed column. This | |
1718 ;; way the literal will always be filled at the same | |
1719 ;; column internally. | |
1720 (fill-paragraph arg) | |
1721 (goto-char (1- (point-max))) | |
1722 (delete-char 1))) | |
1723 ))) | |
1724 (goto-char (marker-position point-save)) | |
1725 (set-marker point-save nil) | |
1726 ;; Always return t. This has the effect that if filling isn't | |
1727 ;; done above, it isn't done at all, and it's therefore | |
1728 ;; effectively disabled in normal code. | |
1729 t)) | |
1625 | 1730 |
1626 | 1731 |
1627 (provide 'cc-cmds) | 1732 (provide 'cc-cmds) |
1628 ;;; cc-cmds.el ends here | 1733 ;;; cc-cmds.el ends here |