diff options
| -rw-r--r-- | lisp/ChangeLog | 8 | ||||
| -rw-r--r-- | lisp/newcomment.el | 115 |
2 files changed, 81 insertions, 42 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 88448bc6b45..ce34c9edcf2 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,5 +1,13 @@ | |||
| 1 | 2007-05-08 Stefan Monnier <monnier@iro.umontreal.ca> | 1 | 2007-05-08 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 2 | ||
| 3 | * newcomment.el (comment-search-forward): Make sure we search forward. | ||
| 4 | (comment-enter-backward): Try and distinguish the non-matching case at | ||
| 5 | EOB from the non-matching case with a missing comment-end-skip for | ||
| 6 | a 2-char comment ender. | ||
| 7 | (comment-choose-indent): New function extracted from comment-indent. | ||
| 8 | Improve the alignment algorithm. | ||
| 9 | (comment-indent): Use it. | ||
| 10 | |||
| 3 | * textmodes/sgml-mode.el (sgml-lexical-context): Add handling of | 11 | * textmodes/sgml-mode.el (sgml-lexical-context): Add handling of |
| 4 | XML style Processing Instructions. | 12 | XML style Processing Instructions. |
| 5 | (sgml-parse-tag-backward): Handle XML-style PIs. Also ensure progress. | 13 | (sgml-parse-tag-backward): Handle XML-style PIs. Also ensure progress. |
diff --git a/lisp/newcomment.el b/lisp/newcomment.el index b0a166465fa..55f96d21464 100644 --- a/lisp/newcomment.el +++ b/lisp/newcomment.el | |||
| @@ -97,10 +97,10 @@ Major modes should set this variable.") | |||
| 97 | ;;;###autoload | 97 | ;;;###autoload |
| 98 | (defcustom comment-column 32 | 98 | (defcustom comment-column 32 |
| 99 | "Column to indent right-margin comments to. | 99 | "Column to indent right-margin comments to. |
| 100 | Each mode establishes a different default value for this variable; you | 100 | Each mode may establish a different default value for this variable; you |
| 101 | can set the value for a particular mode using that mode's hook. | 101 | can set the value for a particular mode using that mode's hook. |
| 102 | Comments might be indented to a value smaller than this in order | 102 | Comments might be indented to a different value in order not to go beyond |
| 103 | not to go beyond `comment-fill-column'." | 103 | `comment-fill-column' or in order to align them with surrounding comments." |
| 104 | :type 'integer | 104 | :type 'integer |
| 105 | :group 'comment) | 105 | :group 'comment) |
| 106 | (make-variable-buffer-local 'comment-column) | 106 | (make-variable-buffer-local 'comment-column) |
| @@ -491,16 +491,24 @@ Point is assumed to be just at the end of a comment." | |||
| 491 | (goto-char (point-min)) | 491 | (goto-char (point-min)) |
| 492 | (re-search-forward (concat comment-end-skip "\\'") nil t)) | 492 | (re-search-forward (concat comment-end-skip "\\'") nil t)) |
| 493 | (goto-char (match-beginning 0))) | 493 | (goto-char (match-beginning 0))) |
| 494 | ;; comment-end-skip not found. Maybe we're at EOB which implicitly | 494 | ;; comment-end-skip not found probably because it was not set |
| 495 | ;; closes the comment. | 495 | ;; right. Since \\s> should catch the single-char case, let's |
| 496 | ((eobp) (skip-syntax-backward " ")) | 496 | ;; check that we're looking at a two-char comment ender. |
| 497 | (t | 497 | ((not (or (<= (- (point-max) (line-beginning-position)) 1) |
| 498 | ;; else comment-end-skip was not found probably because it was not | 498 | (zerop (logand (car (syntax-after (- (point) 1))) |
| 499 | ;; set right. Since \\s> should catch the single-char case, we'll | 499 | ;; Here we take advantage of the fact that |
| 500 | ;; blindly assume we're at the end of a two-char comment-end. | 500 | ;; the syntax class " " is encoded to 0, |
| 501 | ;; so " 4" gives us just the 4 bit. | ||
| 502 | (car (string-to-syntax " 4")))) | ||
| 503 | (zerop (logand (car (syntax-after (- (point) 2))) | ||
| 504 | (car (string-to-syntax " 3")))))) | ||
| 501 | (backward-char 2) | 505 | (backward-char 2) |
| 502 | (skip-chars-backward (string (char-after))) | 506 | (skip-chars-backward (string (char-after))) |
| 503 | (skip-syntax-backward " "))))) | 507 | (skip-syntax-backward " ")) |
| 508 | ;; No clue what's going on: maybe we're really not right after the | ||
| 509 | ;; end of a comment. Maybe we're at the "end" because of EOB rather | ||
| 510 | ;; than because of a marker. | ||
| 511 | (t (skip-syntax-backward " "))))) | ||
| 504 | 512 | ||
| 505 | ;;;; | 513 | ;;;; |
| 506 | ;;;; Commands | 514 | ;;;; Commands |
| @@ -516,6 +524,58 @@ Point is assumed to be just at the end of a comment." | |||
| 516 | (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)"))) | 524 | (and (> comment-add 0) (looking-at "\\s<\\(\\S<\\|\\'\\)"))) |
| 517 | comment-column))) | 525 | comment-column))) |
| 518 | 526 | ||
| 527 | (defun comment-choose-indent (&optional indent) | ||
| 528 | "Choose the indentation to use for a right-hand-side comment. | ||
| 529 | The criteria are (in this order): | ||
| 530 | - try to keep the comment's text within `comment-fill-column'. | ||
| 531 | - try to align with surrounding comments. | ||
| 532 | - prefer INDENT (or `comment-column' if nil). | ||
| 533 | Point is expected to be at the start of the comment." | ||
| 534 | (unless indent (setq indent comment-column)) | ||
| 535 | ;; Avoid moving comments past the fill-column. | ||
| 536 | (let ((max (+ (current-column) | ||
| 537 | (- (or comment-fill-column fill-column) | ||
| 538 | (save-excursion (end-of-line) (current-column))))) | ||
| 539 | (other nil) | ||
| 540 | (min (save-excursion (skip-chars-backward " \t") | ||
| 541 | (1+ (current-column))))) | ||
| 542 | ;; Fix up the range. | ||
| 543 | (if (< max min) (setq max min)) | ||
| 544 | ;; Don't move past the fill column. | ||
| 545 | (if (<= max indent) (setq indent max)) | ||
| 546 | ;; We can choose anywhere between min..max. | ||
| 547 | ;; Let's try to align to a comment on the previous line. | ||
| 548 | (save-excursion | ||
| 549 | (when (and (zerop (forward-line -1)) | ||
| 550 | (setq other (comment-search-forward | ||
| 551 | (line-end-position) t))) | ||
| 552 | (goto-char other) (setq other (current-column)))) | ||
| 553 | (if (and other (<= other max) (>= other min)) | ||
| 554 | ;; There is a comment and it's in the range: bingo! | ||
| 555 | other | ||
| 556 | ;; Can't align to a previous comment: let's try to align to comments | ||
| 557 | ;; on the following lines, then. These have not been re-indented yet, | ||
| 558 | ;; so we can't directly align ourselves with them. All we do is to try | ||
| 559 | ;; and choose an indentation point with which they will be able to | ||
| 560 | ;; align themselves. | ||
| 561 | (save-excursion | ||
| 562 | (while (and (zerop (forward-line 1)) | ||
| 563 | (setq other (comment-search-forward | ||
| 564 | (line-end-position) t))) | ||
| 565 | (goto-char other) | ||
| 566 | (let ((omax (+ (current-column) | ||
| 567 | (- (or comment-fill-column fill-column) | ||
| 568 | (save-excursion (end-of-line) (current-column))))) | ||
| 569 | (omin (save-excursion (skip-chars-backward " \t") | ||
| 570 | (1+ (current-column))))) | ||
| 571 | (if (and (>= omax min) (<= omin max)) | ||
| 572 | (progn (setq min (max omin min)) | ||
| 573 | (setq max (min omax max))) | ||
| 574 | ;; Can't align with this anyway, so exit the loop. | ||
| 575 | (goto-char (point-max)))))) | ||
| 576 | ;; Return the closest point to indent within min..max. | ||
| 577 | (max min (min max indent))))) | ||
| 578 | |||
| 519 | ;;;###autoload | 579 | ;;;###autoload |
| 520 | (defun comment-indent (&optional continue) | 580 | (defun comment-indent (&optional continue) |
| 521 | "Indent this line's comment to `comment-column', or insert an empty comment. | 581 | "Indent this line's comment to `comment-column', or insert an empty comment. |
| @@ -569,38 +629,9 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any." | |||
| 569 | (if (not indent) | 629 | (if (not indent) |
| 570 | ;; comment-indent-function refuses: delegate to line-indent. | 630 | ;; comment-indent-function refuses: delegate to line-indent. |
| 571 | (indent-according-to-mode) | 631 | (indent-according-to-mode) |
| 572 | ;; If the comment is at the left of code, adjust the indentation. | 632 | ;; If the comment is at the right of code, adjust the indentation. |
| 573 | (unless (save-excursion (skip-chars-backward " \t") (bolp)) | 633 | (unless (save-excursion (skip-chars-backward " \t") (bolp)) |
| 574 | ;; Avoid moving comments past the fill-column. | 634 | (setq indent (comment-choose-indent indent))) |
| 575 | (let ((max (+ (current-column) | ||
| 576 | (- (or comment-fill-column fill-column) | ||
| 577 | (save-excursion (end-of-line) (current-column)))))) | ||
| 578 | (if (<= max indent) | ||
| 579 | (setq indent max) ;Don't move past the fill column. | ||
| 580 | ;; We can choose anywhere between indent..max. | ||
| 581 | ;; Let's try to align to a comment on the previous line. | ||
| 582 | (let ((other nil) | ||
| 583 | (min (max indent | ||
| 584 | (save-excursion (skip-chars-backward " \t") | ||
| 585 | (1+ (current-column)))))) | ||
| 586 | (save-excursion | ||
| 587 | (when (and (zerop (forward-line -1)) | ||
| 588 | (setq other (comment-search-forward | ||
| 589 | (line-end-position) t))) | ||
| 590 | (goto-char other) (setq other (current-column)))) | ||
| 591 | (if (and other (<= other max) (>= other min)) | ||
| 592 | ;; There is a comment and it's in the range: bingo. | ||
| 593 | (setq indent other) | ||
| 594 | ;; Let's try to align to a comment on the next line, then. | ||
| 595 | (let ((other nil)) | ||
| 596 | (save-excursion | ||
| 597 | (when (and (zerop (forward-line 1)) | ||
| 598 | (setq other (comment-search-forward | ||
| 599 | (line-end-position) t))) | ||
| 600 | (goto-char other) (setq other (current-column)))) | ||
| 601 | (if (and other (<= other max) (> other min)) | ||
| 602 | ;; There is a comment and it's in the range: bingo. | ||
| 603 | (setq indent other)))))))) | ||
| 604 | ;; Update INDENT to leave at least one space | 635 | ;; Update INDENT to leave at least one space |
| 605 | ;; after other nonwhite text on the line. | 636 | ;; after other nonwhite text on the line. |
| 606 | (save-excursion | 637 | (save-excursion |