aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1998-03-08 06:50:21 +0000
committerRichard M. Stallman1998-03-08 06:50:21 +0000
commitf23da29ac2a085c1dcc4ab258aaa49e6e0a01a24 (patch)
treeb3950e394485c5995777a8000d01bdd8910ef849
parentb2acd789585ac55b73175ef4f30d970638c6f06d (diff)
downloademacs-f23da29ac2a085c1dcc4ab258aaa49e6e0a01a24.tar.gz
emacs-f23da29ac2a085c1dcc4ab258aaa49e6e0a01a24.zip
(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.
-rw-r--r--lisp/progmodes/cc-cmds.el145
1 files changed, 125 insertions, 20 deletions
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index 31adde4f908..5dfa7507041 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -27,8 +27,6 @@
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)
@@ -1451,17 +1449,30 @@ If any of the current line is a comment or within a comment,
1451fill the comment or the paragraph of it that point is in, 1449fill the comment or the paragraph of it that point is in,
1452preserving the comment indentation or line-starting decorations. 1450preserving the comment indentation or line-starting decorations.
1453 1451
1452If point is inside multiline string literal, fill it. This currently
1453does not respect escaped newlines, except for the special case when it
1454is the very first thing in the string. The intended use for this rule
1455is in situations like the following:
1456
1457char description[] = \"\\
1458A very long description of something that you want to fill to make
1459nicely formatted output.\"\;
1460
1461If point is in any other situation, i.e. in normal code, do nothing.
1462
1454Optional prefix ARG means justify paragraph as well." 1463Optional 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 ".*//"))
@@ -1469,8 +1480,8 @@ Optional prefix ARG means justify paragraph as well."
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.
@@ -1495,13 +1506,25 @@ Optional prefix ARG means justify paragraph as well."
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
@@ -1511,7 +1534,18 @@ Optional prefix ARG means justify paragraph as well."
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
@@ -1519,10 +1553,16 @@ Optional prefix ARG means justify paragraph as well."
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)
@@ -1533,7 +1573,6 @@ Optional prefix ARG means justify paragraph as well."
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*
@@ -1567,8 +1606,13 @@ Optional prefix ARG means justify paragraph as well."
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
@@ -1599,9 +1643,22 @@ Optional prefix ARG means justify paragraph as well."
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.
@@ -1620,8 +1677,56 @@ Optional prefix ARG means justify paragraph as well."
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)