aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/newcomment.el322
1 files changed, 196 insertions, 126 deletions
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 96c8f9cc41b..91943503f5e 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -129,6 +129,31 @@ the comment's starting delimiter and should return either the desired
129column indentation or nil. 129column indentation or nil.
130If nil is returned, indentation is delegated to `indent-according-to-mode'.") 130If nil is returned, indentation is delegated to `indent-according-to-mode'.")
131 131
132;;;###autoload
133(defvar comment-insert-comment-function nil
134 "Function to insert a comment when a line doesn't contain one.
135The function has no args.
136
137Applicable at least in modes for languages like fixed-format Fortran where
138comments always start in column zero.")
139
140(defvar comment-region-function nil
141 "Function to comment a region.
142Its args are the same as those of `comment-region', but BEG and END are
143guaranteed to be correctly ordered. It is called within `save-excursion'.
144
145Applicable at least in modes for languages like fixed-format Fortran where
146comments always start in column zero.")
147
148(defvar uncomment-region-function nil
149 "Function to uncomment a region.
150Its args are the same as those of `uncomment-region', but BEG and END are
151guaranteed to be correctly ordered. It is called within `save-excursion'.
152
153Applicable at least in modes for languages like fixed-format Fortran where
154comments always start in column zero.")
155
156;; ?? never set
132(defvar block-comment-start nil) 157(defvar block-comment-start nil)
133(defvar block-comment-end nil) 158(defvar block-comment-end nil)
134 159
@@ -224,10 +249,10 @@ terminated by the end of line (i.e. `comment-end' is empty)."
224Functions autoloaded from newcomment.el, being entry points, should call 249Functions autoloaded from newcomment.el, being entry points, should call
225this function before any other, so the rest of the code can assume that 250this function before any other, so the rest of the code can assume that
226the variables are properly set." 251the variables are properly set."
227 (if (not comment-start) 252 (unless (and (not comment-start) noerror)
228 (unless noerror 253 (unless comment-start
229 (set (make-local-variable 'comment-start) 254 (set (make-local-variable 'comment-start)
230 (read-string "No comment syntax is defined. Use: "))) 255 (read-string "No comment syntax is defined. Use: ")))
231 ;; comment-use-syntax 256 ;; comment-use-syntax
232 (when (eq comment-use-syntax 'undecided) 257 (when (eq comment-use-syntax 'undecided)
233 (set (make-local-variable 'comment-use-syntax) 258 (set (make-local-variable 'comment-use-syntax)
@@ -460,7 +485,7 @@ Point is assumed to be just at the end of a comment."
460 485
461;;;###autoload 486;;;###autoload
462(defun comment-indent (&optional continue) 487(defun comment-indent (&optional continue)
463 "Indent this line's comment to comment column, or insert an empty comment. 488 "Indent this line's comment to `comment-column', or insert an empty comment.
464If CONTINUE is non-nil, use the `comment-continue' markers if any." 489If CONTINUE is non-nil, use the `comment-continue' markers if any."
465 (interactive "*") 490 (interactive "*")
466 (comment-normalize-vars) 491 (comment-normalize-vars)
@@ -486,9 +511,12 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
486 (forward-char (/ (skip-chars-backward " \t") -2))) 511 (forward-char (/ (skip-chars-backward " \t") -2)))
487 (setq cpos (point-marker))) 512 (setq cpos (point-marker)))
488 ;; If none, insert one. 513 ;; If none, insert one.
514 (if comment-insert-comment-function
515 (funcall comment-insert-comment-function)
489 (save-excursion 516 (save-excursion
490 ;; Some comment-indent-function insist on not moving comments that 517 ;; Some `comment-indent-function's insist on not moving
491 ;; are in column 0, so we first go to the likely target column. 518 ;; comments that are in column 0, so we first go to the
519 ;; likely target column.
492 (indent-to comment-column) 520 (indent-to comment-column)
493 ;; Ensure there's a space before the comment for things 521 ;; Ensure there's a space before the comment for things
494 ;; like sh where it matters (as well as being neater). 522 ;; like sh where it matters (as well as being neater).
@@ -497,15 +525,20 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
497 (setq begpos (point)) 525 (setq begpos (point))
498 (insert starter) 526 (insert starter)
499 (setq cpos (point-marker)) 527 (setq cpos (point-marker))
500 (insert ender))) 528 (insert ender))))
501 (goto-char begpos) 529 (goto-char begpos)
502 ;; Compute desired indent. 530 ;; Compute desired indent.
503 (setq indent (save-excursion (funcall comment-indent-function))) 531 (setq indent (save-excursion (funcall comment-indent-function)))
532 ;; If `indent' is nil and there's code before the comment, we can't
533 ;; use `indent-according-to-mode', so we default to comment-column.
534 (unless (or indent (save-excursion (skip-chars-backward " \t") (bolp)))
535 (setq indent comment-column))
504 (if (not indent) 536 (if (not indent)
505 ;; comment-indent-function refuses: delegate to line-indent. 537 ;; comment-indent-function refuses: delegate to line-indent.
506 (indent-according-to-mode) 538 (indent-according-to-mode)
507 ;; Avoid moving comments past the fill-column. 539 ;; If the comment is at the left of code, adjust the indentation.
508 (unless (save-excursion (skip-chars-backward " \t") (bolp)) 540 (unless (save-excursion (skip-chars-backward " \t") (bolp))
541 ;; Avoid moving comments past the fill-column.
509 (let ((max (+ (current-column) 542 (let ((max (+ (current-column)
510 (- (or comment-fill-column fill-column) 543 (- (or comment-fill-column fill-column)
511 (save-excursion (end-of-line) (current-column)))))) 544 (save-excursion (end-of-line) (current-column))))))
@@ -513,13 +546,16 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
513 (setq indent max) ;Don't move past the fill column. 546 (setq indent max) ;Don't move past the fill column.
514 ;; We can choose anywhere between indent..max. 547 ;; We can choose anywhere between indent..max.
515 ;; Let's try to align to a comment on the previous line. 548 ;; Let's try to align to a comment on the previous line.
516 (let ((other nil)) 549 (let ((other nil)
550 (min (max indent
551 (save-excursion (skip-chars-backward " \t")
552 (1+ (current-column))))))
517 (save-excursion 553 (save-excursion
518 (when (and (zerop (forward-line -1)) 554 (when (and (zerop (forward-line -1))
519 (setq other (comment-search-forward 555 (setq other (comment-search-forward
520 (line-end-position) t))) 556 (line-end-position) t)))
521 (goto-char other) (setq other (current-column)))) 557 (goto-char other) (setq other (current-column))))
522 (if (and other (<= other max) (> other indent)) 558 (if (and other (<= other max) (>= other min))
523 ;; There is a comment and it's in the range: bingo. 559 ;; There is a comment and it's in the range: bingo.
524 (setq indent other) 560 (setq indent other)
525 ;; Let's try to align to a comment on the next line, then. 561 ;; Let's try to align to a comment on the next line, then.
@@ -529,7 +565,7 @@ If CONTINUE is non-nil, use the `comment-continue' markers if any."
529 (setq other (comment-search-forward 565 (setq other (comment-search-forward
530 (line-end-position) t))) 566 (line-end-position) t)))
531 (goto-char other) (setq other (current-column)))) 567 (goto-char other) (setq other (current-column))))
532 (if (and other (<= other max) (> other indent)) 568 (if (and other (<= other max) (> other min))
533 ;; There is a comment and it's in the range: bingo. 569 ;; There is a comment and it's in the range: bingo.
534 (setq indent other)))))))) 570 (setq indent other))))))))
535 (unless (= (current-column) indent) 571 (unless (= (current-column) indent)
@@ -662,32 +698,62 @@ The numeric prefix ARG can specify a number of chars to remove from the
662comment markers." 698comment markers."
663 (interactive "*r\nP") 699 (interactive "*r\nP")
664 (comment-normalize-vars) 700 (comment-normalize-vars)
665 (if (> beg end) (let (mid) (setq mid beg beg end end mid))) 701 (when (> beg end) (setq beg (prog1 end (setq end beg))))
666 (save-excursion 702 (save-excursion
667 (goto-char beg) 703 (if uncomment-region-function
668 (setq end (copy-marker end)) 704 (funcall uncomment-region-function beg end arg)
669 (let* ((numarg (prefix-numeric-value arg)) 705 (goto-char beg)
670 (ccs comment-continue) 706 (setq end (copy-marker end))
671 (srei (comment-padright ccs 're)) 707 (let* ((numarg (prefix-numeric-value arg))
672 (csre (comment-padright comment-start 're)) 708 (ccs comment-continue)
673 (sre (and srei (concat "^\\s-*?\\(" srei "\\)"))) 709 (srei (comment-padright ccs 're))
674 spt) 710 (csre (comment-padright comment-start 're))
675 (while (and (< (point) end) 711 (sre (and srei (concat "^\\s-*?\\(" srei "\\)")))
676 (setq spt (comment-search-forward end t))) 712 spt)
677 (let ((ipt (point)) 713 (while (and (< (point) end)
678 ;; Find the end of the comment. 714 (setq spt (comment-search-forward end t)))
679 (ept (progn 715 (let ((ipt (point))
680 (goto-char spt) 716 ;; Find the end of the comment.
681 (unless (comment-forward) 717 (ept (progn
682 (error "Can't find the comment end")) 718 (goto-char spt)
683 (point))) 719 (unless
684 (box nil) 720 (or
685 (box-equal nil)) ;Whether we might be using `=' for boxes. 721 (comment-forward)
686 (save-restriction 722 ;; Allow eob as comment-end instead of \n.
687 (narrow-to-region spt ept) 723 (and
688 724 (eobp)
689 ;; Remove the comment-start. 725 (let ((s1 (aref (syntax-table) (char-after spt)))
690 (goto-char ipt) 726 (s2 (aref (syntax-table)
727 (or (char-after (1+ spt)) 0)))
728 (sn (aref (syntax-table) ?\n))
729 (flag->b (car (string-to-syntax "> b")))
730 (flag-1b (car (string-to-syntax " 1b")))
731 (flag-2b (car (string-to-syntax " 2b"))))
732 (cond
733 ;; One-character comment-start terminated by
734 ;; \n.
735 ((and
736 (equal sn (string-to-syntax ">"))
737 (equal s1 (string-to-syntax "<")))
738 (insert-char ?\n 1)
739 t)
740 ;; Two-character type b comment-start
741 ;; terminated by \n.
742 ((and
743 (= (logand (car sn) flag->b) flag->b)
744 (= (logand (car s1) flag-1b) flag-1b)
745 (= (logand (car s2) flag-2b) flag-2b))
746 (insert-char ?\n 1)
747 t)))))
748 (error "Can't find the comment end"))
749 (point)))
750 (box nil)
751 (box-equal nil)) ;Whether we might be using `=' for boxes.
752 (save-restriction
753 (narrow-to-region spt ept)
754
755 ;; Remove the comment-start.
756 (goto-char ipt)
691 (skip-syntax-backward " ") 757 (skip-syntax-backward " ")
692 ;; A box-comment starts with a looong comment-start marker. 758 ;; A box-comment starts with a looong comment-start marker.
693 (when (and (or (and (= (- (point) (point-min)) 1) 759 (when (and (or (and (= (- (point) (point-min)) 1)
@@ -707,52 +773,52 @@ comment markers."
707 (goto-char (match-end 0))) 773 (goto-char (match-end 0)))
708 (if (null arg) (delete-region (point-min) (point)) 774 (if (null arg) (delete-region (point-min) (point))
709 (skip-syntax-backward " ") 775 (skip-syntax-backward " ")
710 (delete-char (- numarg)) 776 (delete-char (- numarg))
711 (unless (or (bobp) 777 (unless (or (bobp)
712 (save-excursion (goto-char (point-min)) 778 (save-excursion (goto-char (point-min))
713 (looking-at comment-start-skip))) 779 (looking-at comment-start-skip)))
714 ;; If there's something left but it doesn't look like 780 ;; If there's something left but it doesn't look like
715 ;; a comment-start any more, just remove it. 781 ;; a comment-start any more, just remove it.
716 (delete-region (point-min) (point)))) 782 (delete-region (point-min) (point))))
717 783
718 ;; Remove the end-comment (and leading padding and such). 784 ;; Remove the end-comment (and leading padding and such).
719 (goto-char (point-max)) (comment-enter-backward) 785 (goto-char (point-max)) (comment-enter-backward)
720 ;; Check for special `=' used sometimes in comment-box. 786 ;; Check for special `=' used sometimes in comment-box.
721 (when (and box-equal (not (eq (char-before (point-max)) ?\n))) 787 (when (and box-equal (not (eq (char-before (point-max)) ?\n)))
722 (let ((pos (point))) 788 (let ((pos (point)))
723 ;; skip `=' but only if there are at least 7. 789 ;; skip `=' but only if there are at least 7.
724 (when (> (skip-chars-backward "=") -7) (goto-char pos)))) 790 (when (> (skip-chars-backward "=") -7) (goto-char pos))))
725 (unless (looking-at "\\(\n\\|\\s-\\)*\\'") 791 (unless (looking-at "\\(\n\\|\\s-\\)*\\'")
726 (when (and (bolp) (not (bobp))) (backward-char)) 792 (when (and (bolp) (not (bobp))) (backward-char))
727 (if (null arg) (delete-region (point) (point-max)) 793 (if (null arg) (delete-region (point) (point-max))
728 (skip-syntax-forward " ") 794 (skip-syntax-forward " ")
729 (delete-char numarg) 795 (delete-char numarg)
730 (unless (or (eobp) (looking-at comment-end-skip)) 796 (unless (or (eobp) (looking-at comment-end-skip))
731 ;; If there's something left but it doesn't look like 797 ;; If there's something left but it doesn't look like
732 ;; a comment-end any more, just remove it. 798 ;; a comment-end any more, just remove it.
733 (delete-region (point) (point-max))))) 799 (delete-region (point) (point-max)))))
734 800
735 ;; Unquote any nested end-comment. 801 ;; Unquote any nested end-comment.
736 (comment-quote-nested comment-start comment-end t) 802 (comment-quote-nested comment-start comment-end t)
737 803
738 ;; Eliminate continuation markers as well. 804 ;; Eliminate continuation markers as well.
739 (when sre 805 (when sre
740 (let* ((cce (comment-string-reverse (or comment-continue 806 (let* ((cce (comment-string-reverse (or comment-continue
741 comment-start))) 807 comment-start)))
742 (erei (and box (comment-padleft cce 're))) 808 (erei (and box (comment-padleft cce 're)))
743 (ere (and erei (concat "\\(" erei "\\)\\s-*$")))) 809 (ere (and erei (concat "\\(" erei "\\)\\s-*$"))))
744 (goto-char (point-min)) 810 (goto-char (point-min))
745 (while (progn 811 (while (progn
746 (if (and ere (re-search-forward 812 (if (and ere (re-search-forward
747 ere (line-end-position) t)) 813 ere (line-end-position) t))
748 (replace-match "" t t nil (if (match-end 2) 2 1)) 814 (replace-match "" t t nil (if (match-end 2) 2 1))
749 (setq ere nil)) 815 (setq ere nil))
750 (forward-line 1) 816 (forward-line 1)
751 (re-search-forward sre (line-end-position) t)) 817 (re-search-forward sre (line-end-position) t))
752 (replace-match "" t t nil (if (match-end 2) 2 1))))) 818 (replace-match "" t t nil (if (match-end 2) 2 1)))))
753 ;; Go to the end for the next comment. 819 ;; Go to the end for the next comment.
754 (goto-char (point-max))))) 820 (goto-char (point-max)))))))
755 (set-marker end nil)))) 821 (set-marker end nil)))
756 822
757(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block) 823(defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
758 "Make the leading and trailing extra lines. 824 "Make the leading and trailing extra lines.
@@ -922,49 +988,52 @@ The strings used as comment starts are built from
922 (block (nth 1 style)) 988 (block (nth 1 style))
923 (multi (nth 0 style))) 989 (multi (nth 0 style)))
924 (save-excursion 990 (save-excursion
925 ;; we use `chars' instead of `syntax' because `\n' might be 991 (if comment-region-function
926 ;; of end-comment syntax rather than of whitespace syntax. 992 (funcall comment-region-function beg end arg)
927 ;; sanitize BEG and END 993 ;; we use `chars' instead of `syntax' because `\n' might be
928 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line) 994 ;; of end-comment syntax rather than of whitespace syntax.
929 (setq beg (max beg (point))) 995 ;; sanitize BEG and END
930 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line) 996 (goto-char beg) (skip-chars-forward " \t\n\r") (beginning-of-line)
931 (setq end (min end (point))) 997 (setq beg (max beg (point)))
932 (if (>= beg end) (error "Nothing to comment")) 998 (goto-char end) (skip-chars-backward " \t\n\r") (end-of-line)
933 999 (setq end (min end (point)))
934 ;; sanitize LINES 1000 (if (>= beg end) (error "Nothing to comment"))
935 (setq lines 1001
936 (and 1002 ;; sanitize LINES
937 lines ;; multi 1003 (setq lines
938 (progn (goto-char beg) (beginning-of-line) 1004 (and
939 (skip-syntax-forward " ") 1005 lines ;; multi
940 (>= (point) beg)) 1006 (progn (goto-char beg) (beginning-of-line)
941 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ") 1007 (skip-syntax-forward " ")
942 (<= (point) end)) 1008 (>= (point) beg))
943 (or block (not (string= "" comment-end))) 1009 (progn (goto-char end) (end-of-line) (skip-syntax-backward " ")
944 (or block (progn (goto-char beg) (search-forward "\n" end t)))))) 1010 (<= (point) end))
945 1011 (or block (not (string= "" comment-end)))
946 ;; don't add end-markers just because the user asked for `block' 1012 (or block (progn (goto-char beg) (search-forward "\n" end t))))))
947 (unless (or lines (string= "" comment-end)) (setq block nil)) 1013
948 1014 ;; don't add end-markers just because the user asked for `block'
949 (cond 1015 (unless (or lines (string= "" comment-end)) (setq block nil))
950 ((consp arg) (uncomment-region beg end)) 1016
951 ((< numarg 0) (uncomment-region beg end (- numarg))) 1017 (cond
952 (t 1018 ((consp arg) (uncomment-region beg end))
953 (setq numarg (if (and (null arg) (= (length comment-start) 1)) 1019 ((< numarg 0) (uncomment-region beg end (- numarg)))
954 add (1- numarg))) 1020 (t
955 (comment-region-internal 1021 (setq numarg (if (and (null arg) (= (length comment-start) 1))
956 beg end 1022 add (1- numarg)))
957 (let ((s (comment-padright comment-start numarg))) 1023 (comment-region-internal
958 (if (string-match comment-start-skip s) s 1024 beg end
959 (comment-padright comment-start))) 1025 (let ((s (comment-padright comment-start numarg)))
960 (let ((s (comment-padleft comment-end numarg))) 1026 (if (string-match comment-start-skip s) s
961 (and s (if (string-match comment-end-skip s) s 1027 (comment-padright comment-start)))
962 (comment-padright comment-end)))) 1028 (let ((s (comment-padleft comment-end numarg)))
963 (if multi (comment-padright comment-continue numarg)) 1029 (and s (if (string-match comment-end-skip s) s
964 (if multi (comment-padleft (comment-string-reverse comment-continue) numarg)) 1030 (comment-padright comment-end))))
965 block 1031 (if multi (comment-padright comment-continue numarg))
966 lines 1032 (if multi
967 (nth 3 style)))))) 1033 (comment-padleft (comment-string-reverse comment-continue) numarg))
1034 block
1035 lines
1036 (nth 3 style)))))))
968 1037
969(defun comment-box (beg end &optional arg) 1038(defun comment-box (beg end &optional arg)
970 "Comment out the BEG .. END region, putting it inside a box. 1039 "Comment out the BEG .. END region, putting it inside a box.
@@ -1139,4 +1208,5 @@ unless optional argument SOFT is non-nil."
1139 1208
1140(provide 'newcomment) 1209(provide 'newcomment)
1141 1210
1211;;; arch-tag: 01e3320a-00c8-44ea-a696-8f8e7354c858
1142;;; newcomment.el ends here 1212;;; newcomment.el ends here