aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Whitton2025-04-24 15:38:34 +0800
committerSean Whitton2025-04-25 09:12:57 +0800
commita6829a0c35326361b2b04bd4a010a30fe99802b1 (patch)
treeda4f4b4ccddb9c84c3e1597f72fbeaadeb339089
parente38401e71bddaa16b6de4783fb90e75955faba38 (diff)
downloademacs-a6829a0c35326361b2b04bd4a010a30fe99802b1.tar.gz
emacs-a6829a0c35326361b2b04bd4a010a30fe99802b1.zip
comment-indent: Handle BOL already within a multiline comment
* lisp/newcomment.el (comment-indent): Newly handle the case that BOL is already within a multiline comment (bug#78003). Thanks to Stefan Monnier for review and reworking the control flow.
-rw-r--r--lisp/newcomment.el119
1 files changed, 64 insertions, 55 deletions
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 23fcbc05372..8ccedb0ef0f 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -713,61 +713,70 @@ Point is expected to be at the start of the comment."
713If CONTINUE is non-nil, use the `comment-continue' markers if any." 713If CONTINUE is non-nil, use the `comment-continue' markers if any."
714 (interactive "*") 714 (interactive "*")
715 (comment-normalize-vars) 715 (comment-normalize-vars)
716 (let ((starter (or (and continue comment-continue) 716 (beginning-of-line)
717 comment-start)) 717 (let* ((starter (or (and continue comment-continue)
718 (ender (or (and continue comment-continue "") 718 comment-start
719 comment-end))) 719 (error "No comment syntax defined")))
720 (unless starter (error "No comment syntax defined")) 720 (ender (or (and continue comment-continue "")
721 (beginning-of-line) 721 comment-end))
722 (let* ((eolpos (line-end-position)) 722 (begpos (comment-search-forward (line-end-position) t))
723 (begpos (comment-search-forward eolpos t)) 723 cpos indent)
724 cpos indent) 724 (cond
725 (if (and comment-insert-comment-function (not begpos)) 725 ;; If we couldn't find a comment *starting* on this line, see if we
726 ;; If no comment and c-i-c-f is set, let it do everything. 726 ;; are already within a multiline comment at BOL (bug#78003).
727 (funcall comment-insert-comment-function) 727 ((and (not begpos) (not continue)
728 ;; An existing comment? 728 comment-use-syntax comment-use-global-state
729 (if begpos 729 (nth 4 (syntax-ppss (line-beginning-position))))
730 (progn 730 ;; We don't know anything about the nature of the multiline
731 (if (and (not (looking-at "[\t\n ]")) 731 ;; construct, so immediately delegate to the mode.
732 (looking-at comment-end-skip)) 732 (indent-according-to-mode))
733 ;; The comment is empty and we have skipped all its space 733 ((and (not begpos) comment-insert-comment-function)
734 ;; and landed right before the comment-ender: 734 ;; If no comment and c-i-c-f is set, let it do everything.
735 ;; Go back to the middle of the space. 735 (funcall comment-insert-comment-function))
736 (forward-char (/ (skip-chars-backward " \t") -2))) 736 (t
737 (setq cpos (point-marker))) 737 ;; An existing comment?
738 ;; If none, insert one. 738 (if begpos
739 (save-excursion 739 (progn
740 ;; Some `comment-indent-function's insist on not moving 740 (if (and (not (looking-at "[\t\n ]"))
741 ;; comments that are in column 0, so we first go to the 741 (looking-at comment-end-skip))
742 ;; likely target column. 742 ;; The comment is empty and we have skipped all its space
743 (indent-to comment-column) 743 ;; and landed right before the comment-ender:
744 ;; Ensure there's a space before the comment for things 744 ;; Go back to the middle of the space.
745 ;; like sh where it matters (as well as being neater). 745 (forward-char (/ (skip-chars-backward " \t") -2)))
746 (unless (memq (char-before) '(nil ?\n ?\t ?\s)) 746 (setq cpos (point-marker)))
747 (insert ?\s)) 747 ;; If none, insert one.
748 (setq begpos (point)) 748 (save-excursion
749 (insert starter) 749 ;; Some `comment-indent-function's insist on not moving
750 (setq cpos (point-marker)) 750 ;; comments that are in column 0, so we first go to the
751 (insert ender))) 751 ;; likely target column.
752 (goto-char begpos) 752 (indent-to comment-column)
753 ;; Compute desired indent. 753 ;; Ensure there's a space before the comment for things
754 (setq indent (save-excursion (funcall comment-indent-function))) 754 ;; like sh where it matters (as well as being neater).
755 ;; If `indent' is nil and there's code before the comment, we can't 755 (unless (memq (char-before) '(nil ?\n ?\t ?\s))
756 ;; use `indent-according-to-mode', so we default to comment-column. 756 (insert ?\s))
757 (unless (or indent (save-excursion (skip-chars-backward " \t") (bolp))) 757 (setq begpos (point))
758 (setq indent comment-column)) 758 (insert starter)
759 (if (not indent) 759 (setq cpos (point-marker))
760 ;; comment-indent-function refuses: delegate to line-indent. 760 (insert ender)))
761 (indent-according-to-mode) 761 (goto-char begpos)
762 ;; If the comment is at the right of code, adjust the indentation. 762 ;; Compute desired indent.
763 (unless (save-excursion (skip-chars-backward " \t") (bolp)) 763 (setq indent (save-excursion (funcall comment-indent-function)))
764 (setq indent (comment-choose-indent indent))) 764 ;; If `indent' is nil and there's code before the comment, we can't
765 ;; If that's different from comment's current position, change it. 765 ;; use `indent-according-to-mode', so we default to comment-column.
766 (unless (= (current-column) indent) 766 (unless (or indent (save-excursion (skip-chars-backward " \t") (bolp)))
767 (delete-region (point) (progn (skip-chars-backward " \t") (point))) 767 (setq indent comment-column))
768 (indent-to indent))) 768 (if (not indent)
769 (goto-char cpos) 769 ;; comment-indent-function refuses: delegate to line-indent.
770 (set-marker cpos nil))))) 770 (indent-according-to-mode)
771 ;; If the comment is at the right of code, adjust the indentation.
772 (unless (save-excursion (skip-chars-backward " \t") (bolp))
773 (setq indent (comment-choose-indent indent)))
774 ;; If that's different from comment's current position, change it.
775 (unless (= (current-column) indent)
776 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
777 (indent-to indent)))
778 (goto-char cpos)
779 (set-marker cpos nil)))))
771 780
772;;;###autoload 781;;;###autoload
773(defun comment-set-column (arg) 782(defun comment-set-column (arg)